^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Kprobes-based tracing events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Created by Masami Hiramatsu <mhiramat@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define pr_fmt(fmt) "trace_kprobe: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/error-injection.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "trace_dynevent.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "trace_kprobe_selftest.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "trace_probe.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "trace_probe_tmpl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define KPROBE_EVENT_SYSTEM "kprobes"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define KRETPROBE_MAXACTIVE_MAX 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* Kprobe early definition from command line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static char kprobe_boot_events_buf[COMMAND_LINE_SIZE] __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static int __init set_kprobe_boot_events(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) strlcpy(kprobe_boot_events_buf, str, COMMAND_LINE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) disable_tracing_selftest("running kprobe events");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) __setup("kprobe_event=", set_kprobe_boot_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int trace_kprobe_create(int argc, const char **argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static int trace_kprobe_release(struct dyn_event *ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static bool trace_kprobe_is_busy(struct dyn_event *ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static bool trace_kprobe_match(const char *system, const char *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int argc, const char **argv, struct dyn_event *ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static struct dyn_event_operations trace_kprobe_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) .create = trace_kprobe_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) .show = trace_kprobe_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .is_busy = trace_kprobe_is_busy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .free = trace_kprobe_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .match = trace_kprobe_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * Kprobe event core functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct trace_kprobe {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct dyn_event devent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct kretprobe rp; /* Use rp.kp for kprobe use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) unsigned long __percpu *nhit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) const char *symbol; /* symbol name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct trace_probe tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static bool is_trace_kprobe(struct dyn_event *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return ev->ops == &trace_kprobe_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static struct trace_kprobe *to_trace_kprobe(struct dyn_event *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return container_of(ev, struct trace_kprobe, devent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * for_each_trace_kprobe - iterate over the trace_kprobe list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * @pos: the struct trace_kprobe * for each entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * @dpos: the struct dyn_event * to use as a loop cursor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define for_each_trace_kprobe(pos, dpos) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) for_each_dyn_event(dpos) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (is_trace_kprobe(dpos) && (pos = to_trace_kprobe(dpos)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define SIZEOF_TRACE_KPROBE(n) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) (offsetof(struct trace_kprobe, tp.args) + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) (sizeof(struct probe_arg) * (n)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return tk->rp.handler != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return tk->symbol ? tk->symbol : "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return tk->rp.kp.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return !!(kprobe_gone(&tk->rp.kp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int len = strlen(module_name(mod));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) const char *name = trace_kprobe_symbol(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return strncmp(module_name(mod), name, len) == 0 && name[len] == ':';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (!tk->symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) p = strchr(tk->symbol, ':');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) mutex_lock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) ret = !!find_module(tk->symbol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) mutex_unlock(&module_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) *p = ':';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static bool trace_kprobe_is_busy(struct dyn_event *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct trace_kprobe *tk = to_trace_kprobe(ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return trace_probe_is_enabled(&tk->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static bool trace_kprobe_match_command_head(struct trace_kprobe *tk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) char buf[MAX_ARGSTR_LEN + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (!argc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (!tk->symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) snprintf(buf, sizeof(buf), "0x%p", tk->rp.kp.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) else if (tk->rp.kp.offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) snprintf(buf, sizeof(buf), "%s+%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) trace_kprobe_symbol(tk), tk->rp.kp.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) snprintf(buf, sizeof(buf), "%s", trace_kprobe_symbol(tk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (strcmp(buf, argv[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) argc--; argv++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return trace_probe_match_command_args(&tk->tp, argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static bool trace_kprobe_match(const char *system, const char *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int argc, const char **argv, struct dyn_event *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct trace_kprobe *tk = to_trace_kprobe(ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return strcmp(trace_probe_name(&tk->tp), event) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) (!system || strcmp(trace_probe_group_name(&tk->tp), system) == 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) trace_kprobe_match_command_head(tk, argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) unsigned long nhit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) for_each_possible_cpu(cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) nhit += *per_cpu_ptr(tk->nhit, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return nhit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static nokprobe_inline bool trace_kprobe_is_registered(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return !(list_empty(&tk->rp.kp.list) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) hlist_unhashed(&tk->rp.kp.hlist));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* Return 0 if it fails to find the symbol address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static nokprobe_inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) unsigned long trace_kprobe_address(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (tk->symbol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) addr = (unsigned long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) kallsyms_lookup_name(trace_kprobe_symbol(tk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) addr += tk->rp.kp.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) addr = (unsigned long)tk->rp.kp.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static nokprobe_inline struct trace_kprobe *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) trace_kprobe_primary_from_call(struct trace_event_call *call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct trace_probe *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) tp = trace_probe_primary_from_call(call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (WARN_ON_ONCE(!tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return container_of(tp, struct trace_kprobe, tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) bool trace_kprobe_on_func_entry(struct trace_event_call *call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return tk ? (kprobe_on_func_entry(tk->rp.kp.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) tk->rp.kp.addr ? 0 : tk->rp.kp.offset) == 0) : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) bool trace_kprobe_error_injectable(struct trace_event_call *call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return tk ? within_error_injection_list(trace_kprobe_address(tk)) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static int register_kprobe_event(struct trace_kprobe *tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static int unregister_kprobe_event(struct trace_kprobe *tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static int kretprobe_dispatcher(struct kretprobe_instance *ri,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct pt_regs *regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static void free_trace_kprobe(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (tk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) trace_probe_cleanup(&tk->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) kfree(tk->symbol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) free_percpu(tk->nhit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) kfree(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * Allocate new trace_probe and initialize it (including kprobes).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static struct trace_kprobe *alloc_trace_kprobe(const char *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) const char *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) void *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) const char *symbol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) unsigned long offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) int maxactive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) int nargs, bool is_return)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct trace_kprobe *tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (!tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) tk->nhit = alloc_percpu(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (!tk->nhit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (symbol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) tk->symbol = kstrdup(symbol, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (!tk->symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) tk->rp.kp.symbol_name = tk->symbol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) tk->rp.kp.offset = offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) tk->rp.kp.addr = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (is_return)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) tk->rp.handler = kretprobe_dispatcher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) tk->rp.kp.pre_handler = kprobe_dispatcher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) tk->rp.maxactive = maxactive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) INIT_HLIST_NODE(&tk->rp.kp.hlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) INIT_LIST_HEAD(&tk->rp.kp.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) ret = trace_probe_init(&tk->tp, event, group, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) dyn_event_init(&tk->devent, &trace_kprobe_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) free_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static struct trace_kprobe *find_trace_kprobe(const char *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) const char *group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct dyn_event *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct trace_kprobe *tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) for_each_trace_kprobe(tk, pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (strcmp(trace_probe_name(&tk->tp), event) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) strcmp(trace_probe_group_name(&tk->tp), group) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (trace_kprobe_is_registered(tk) && !trace_kprobe_has_gone(tk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (trace_kprobe_is_return(tk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) ret = enable_kretprobe(&tk->rp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) ret = enable_kprobe(&tk->rp.kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static void __disable_trace_kprobe(struct trace_probe *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct trace_probe *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct trace_kprobe *tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) tk = container_of(pos, struct trace_kprobe, tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (!trace_kprobe_is_registered(tk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (trace_kprobe_is_return(tk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) disable_kretprobe(&tk->rp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) disable_kprobe(&tk->rp.kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * Enable trace_probe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * if the file is NULL, enable "perf" handler, or enable "trace" handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static int enable_trace_kprobe(struct trace_event_call *call,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct trace_event_file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct trace_probe *pos, *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct trace_kprobe *tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) bool enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) tp = trace_probe_primary_from_call(call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (WARN_ON_ONCE(!tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) enabled = trace_probe_is_enabled(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /* This also changes "enabled" state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) ret = trace_probe_add_file(tp, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) trace_probe_set_flag(tp, TP_FLAG_PROFILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) tk = container_of(pos, struct trace_kprobe, tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (trace_kprobe_has_gone(tk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) ret = __enable_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /* Failed to enable one of them. Roll back all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) __disable_trace_kprobe(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) trace_probe_remove_file(tp, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * Disable trace_probe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * if the file is NULL, disable "perf" handler, or disable "trace" handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static int disable_trace_kprobe(struct trace_event_call *call,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) struct trace_event_file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) struct trace_probe *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) tp = trace_probe_primary_from_call(call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (WARN_ON_ONCE(!tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (!trace_probe_get_file_link(tp, file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (!trace_probe_has_single_file(tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) trace_probe_clear_flag(tp, TP_FLAG_TRACE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (!trace_probe_is_enabled(tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) __disable_trace_kprobe(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * Synchronization is done in below function. For perf event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * file == NULL and perf_trace_event_unreg() calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * tracepoint_synchronize_unregister() to ensure synchronize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * event. We don't need to care about it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) trace_probe_remove_file(tp, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) #if defined(CONFIG_DYNAMIC_FTRACE) && \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) static bool __within_notrace_func(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) unsigned long offset, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) /* Get the entry address of the target function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) addr -= offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * Since ftrace_location_range() does inclusive range check, we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * to subtract 1 byte from the end address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) return !ftrace_location_range(addr, addr + size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) static bool within_notrace_func(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) unsigned long addr = trace_kprobe_address(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) char symname[KSYM_NAME_LEN], *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (!__within_notrace_func(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) /* Check if the address is on a suffixed-symbol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (!lookup_symbol_name(addr, symname)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) p = strchr(symname, '.');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) addr = (unsigned long)kprobe_lookup_name(symname, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) return __within_notrace_func(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) #define within_notrace_func(tk) (false)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /* Internal register function - just handle k*probes and flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static int __register_trace_kprobe(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) ret = security_locked_down(LOCKDOWN_KPROBES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (trace_kprobe_is_registered(tk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (within_notrace_func(tk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) pr_warn("Could not probe notrace function %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) trace_kprobe_symbol(tk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) for (i = 0; i < tk->tp.nr_args; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) ret = traceprobe_update_arg(&tk->tp.args[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) /* Set/clear disabled flag according to tp->flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (trace_probe_is_enabled(&tk->tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (trace_kprobe_is_return(tk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) ret = register_kretprobe(&tk->rp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) ret = register_kprobe(&tk->rp.kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) /* Internal unregister function - just handle k*probes and flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static void __unregister_trace_kprobe(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (trace_kprobe_is_registered(tk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (trace_kprobe_is_return(tk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) unregister_kretprobe(&tk->rp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) unregister_kprobe(&tk->rp.kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) /* Cleanup kprobe for reuse and mark it unregistered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) INIT_HLIST_NODE(&tk->rp.kp.hlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) INIT_LIST_HEAD(&tk->rp.kp.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (tk->rp.kp.symbol_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) tk->rp.kp.addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) /* Unregister a trace_probe and probe_event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) static int unregister_trace_kprobe(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) /* If other probes are on the event, just unregister kprobe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (trace_probe_has_sibling(&tk->tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) goto unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) /* Enabled event can not be unregistered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (trace_probe_is_enabled(&tk->tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) /* Will fail if probe is being used by ftrace or perf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (unregister_kprobe_event(tk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) __unregister_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) dyn_event_remove(&tk->devent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) trace_probe_unlink(&tk->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) static bool trace_kprobe_has_same_kprobe(struct trace_kprobe *orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) struct trace_kprobe *comp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) struct trace_probe_event *tpe = orig->tp.event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) struct trace_probe *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) list_for_each_entry(pos, &tpe->probes, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) orig = container_of(pos, struct trace_kprobe, tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (strcmp(trace_kprobe_symbol(orig),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) trace_kprobe_symbol(comp)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) trace_kprobe_offset(orig) != trace_kprobe_offset(comp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * trace_probe_compare_arg_type() ensured that nr_args and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * each argument name and type are same. Let's compare comm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) for (i = 0; i < orig->tp.nr_args; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) if (strcmp(orig->tp.args[i].comm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) comp->tp.args[i].comm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (i == orig->tp.nr_args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) static int append_trace_kprobe(struct trace_kprobe *tk, struct trace_kprobe *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) ret = trace_probe_compare_arg_type(&tk->tp, &to->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) /* Note that argument starts index = 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) trace_probe_log_set_index(ret + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) trace_probe_log_err(0, DIFF_ARG_TYPE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if (trace_kprobe_has_same_kprobe(to, tk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) trace_probe_log_set_index(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) trace_probe_log_err(0, SAME_PROBE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) /* Append to existing event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) ret = trace_probe_append(&tk->tp, &to->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) /* Register k*probe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) ret = __register_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) trace_probe_unlink(&tk->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) dyn_event_add(&tk->devent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) /* Register a trace_probe and probe_event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) static int register_trace_kprobe(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) struct trace_kprobe *old_tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) mutex_lock(&event_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) old_tk = find_trace_kprobe(trace_probe_name(&tk->tp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) trace_probe_group_name(&tk->tp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (old_tk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) if (trace_kprobe_is_return(tk) != trace_kprobe_is_return(old_tk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) trace_probe_log_set_index(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) trace_probe_log_err(0, DIFF_PROBE_TYPE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) ret = append_trace_kprobe(tk, old_tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) /* Register new event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) ret = register_kprobe_event(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (ret == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) trace_probe_log_set_index(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) trace_probe_log_err(0, EVENT_EXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) pr_warn("Failed to register probe event(%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) /* Register k*probe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) ret = __register_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) unregister_kprobe_event(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) dyn_event_add(&tk->devent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) mutex_unlock(&event_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) /* Module notifier call back, checking event on the module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) static int trace_kprobe_module_callback(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) unsigned long val, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) struct module *mod = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) struct dyn_event *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) struct trace_kprobe *tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (val != MODULE_STATE_COMING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) /* Update probes on coming module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) mutex_lock(&event_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) for_each_trace_kprobe(tk, pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) if (trace_kprobe_within_module(tk, mod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) /* Don't need to check busy - this should have gone. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) __unregister_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) ret = __register_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) pr_warn("Failed to re-register probe %s on %s: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) trace_probe_name(&tk->tp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) module_name(mod), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) mutex_unlock(&event_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) static struct notifier_block trace_kprobe_module_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) .notifier_call = trace_kprobe_module_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) .priority = 1 /* Invoked after kprobe module callback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) /* Convert certain expected symbols into '_' when generating event names */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) static inline void sanitize_event_name(char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) while (*name++ != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (*name == ':' || *name == '.')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) *name = '_';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) static int trace_kprobe_create(int argc, const char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * Argument syntax:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * - Add kprobe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) * - Add kretprobe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * Or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * p:[GRP/]EVENT] [MOD:]KSYM[+0]%return [FETCHARGS]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * Fetch args:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * $retval : fetch return value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) * $stack : fetch stack address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) * $stackN : fetch Nth of stack (N:0-)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) * $comm : fetch current task comm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) * %REG : fetch register REG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) * Dereferencing memory fetch:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) * +|-offs(ARG) : fetch memory at ARG +|- offs address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * Alias name of args:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) * NAME=FETCHARG : set NAME as alias of FETCHARG.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) * Type of args:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) * FETCHARG:TYPE : use TYPE instead of unsigned long.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) struct trace_kprobe *tk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) int i, len, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) bool is_return = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) char *symbol = NULL, *tmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) const char *event = NULL, *group = KPROBE_EVENT_SYSTEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) int maxactive = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) long offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) void *addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) char buf[MAX_EVENT_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) unsigned int flags = TPARG_FL_KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) switch (argv[0][0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) case 'r':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) is_return = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) case 'p':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) if (argc < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) return -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) trace_probe_log_init("trace_kprobe", argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) event = strchr(&argv[0][1], ':');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) if (event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) event++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) if (isdigit(argv[0][1])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (!is_return) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) trace_probe_log_err(1, MAXACT_NO_KPROBE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) goto parse_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) if (event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) len = event - &argv[0][1] - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) len = strlen(&argv[0][1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (len > MAX_EVENT_NAME_LEN - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) trace_probe_log_err(1, BAD_MAXACT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) goto parse_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) memcpy(buf, &argv[0][1], len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) buf[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) ret = kstrtouint(buf, 0, &maxactive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) if (ret || !maxactive) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) trace_probe_log_err(1, BAD_MAXACT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) goto parse_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) /* kretprobes instances are iterated over via a list. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) * maximum should stay reasonable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) trace_probe_log_err(1, MAXACT_TOO_BIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) goto parse_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) /* try to parse an address. if that fails, try to read the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) * input as a symbol. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) trace_probe_log_set_index(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) /* Check whether uprobe event specified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) ret = -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) /* a symbol specified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) symbol = kstrdup(argv[1], GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) if (!symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) tmp = strchr(symbol, '%');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) if (tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (!strcmp(tmp, "%return")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) *tmp = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) is_return = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) trace_probe_log_err(tmp - symbol, BAD_ADDR_SUFFIX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) goto parse_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) /* TODO: support .init module functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) ret = traceprobe_split_symbol_offset(symbol, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) if (ret || offset < 0 || offset > UINT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) trace_probe_log_err(0, BAD_PROBE_ADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) goto parse_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) if (is_return)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) flags |= TPARG_FL_RETURN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) ret = kprobe_on_func_entry(NULL, symbol, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) flags |= TPARG_FL_FENTRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) /* Defer the ENOENT case until register kprobe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) if (ret == -EINVAL && is_return) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) trace_probe_log_err(0, BAD_RETPROBE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) goto parse_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) trace_probe_log_set_index(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) if (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) ret = traceprobe_parse_event_name(&event, &group, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) event - argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) goto parse_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) /* Make a new event name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) is_return ? 'r' : 'p', symbol, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) is_return ? 'r' : 'p', addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) sanitize_event_name(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) event = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) /* setup a probe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) argc - 2, is_return);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) if (IS_ERR(tk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) ret = PTR_ERR(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) /* This must return -ENOMEM, else there is a bug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) WARN_ON_ONCE(ret != -ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) goto out; /* We know tk is not allocated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) argc -= 2; argv += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) /* parse arguments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) tmp = kstrdup(argv[i], GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) if (!tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) trace_probe_log_set_index(i + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) ret = traceprobe_parse_probe_arg(&tk->tp, i, tmp, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) kfree(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) goto error; /* This can be -ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) ret = traceprobe_set_print_fmt(&tk->tp, is_return);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) ret = register_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) trace_probe_log_set_index(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) if (ret == -EILSEQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) trace_probe_log_err(0, BAD_INSN_BNDRY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) else if (ret == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) trace_probe_log_err(0, BAD_PROBE_ADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) else if (ret != -ENOMEM && ret != -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) trace_probe_log_err(0, FAIL_REG_PROBE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) trace_probe_log_clear();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) kfree(symbol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) parse_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) free_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) static int create_or_delete_trace_kprobe(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) if (argv[0][0] == '-')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) return dyn_event_release(argc, argv, &trace_kprobe_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) ret = trace_kprobe_create(argc, (const char **)argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) return ret == -ECANCELED ? -EINVAL : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) static int trace_kprobe_run_command(struct dynevent_cmd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) return trace_run_command(cmd->seq.buffer, create_or_delete_trace_kprobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * kprobe_event_cmd_init - Initialize a kprobe event command object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) * @cmd: A pointer to the dynevent_cmd struct representing the new event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) * @buf: A pointer to the buffer used to build the command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) * @maxlen: The length of the buffer passed in @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) * Initialize a synthetic event command object. Use this before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) * calling any of the other kprobe_event functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) void kprobe_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_KPROBE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) trace_kprobe_run_command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) EXPORT_SYMBOL_GPL(kprobe_event_cmd_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) * __kprobe_event_gen_cmd_start - Generate a kprobe event command from arg list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) * @cmd: A pointer to the dynevent_cmd struct representing the new event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) * @name: The name of the kprobe event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) * @loc: The location of the kprobe event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) * @kretprobe: Is this a return probe?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) * @args: Variable number of arg (pairs), one pair for each field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) * NOTE: Users normally won't want to call this function directly, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) * rather use the kprobe_event_gen_cmd_start() wrapper, which automatically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) * adds a NULL to the end of the arg list. If this function is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) * directly, make sure the last arg in the variable arg list is NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) * Generate a kprobe event command to be executed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) * kprobe_event_gen_cmd_end(). This function can be used to generate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) * complete command or only the first part of it; in the latter case,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) * kprobe_event_add_fields() can be used to add more fields following this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * Unlikely the synth_event_gen_cmd_start(), @loc must be specified. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) * returns -EINVAL if @loc == NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) * Return: 0 if successful, error otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) int __kprobe_event_gen_cmd_start(struct dynevent_cmd *cmd, bool kretprobe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) const char *name, const char *loc, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) char buf[MAX_EVENT_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) struct dynevent_arg arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) if (cmd->type != DYNEVENT_TYPE_KPROBE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) if (!loc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) if (kretprobe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) snprintf(buf, MAX_EVENT_NAME_LEN, "r:kprobes/%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) snprintf(buf, MAX_EVENT_NAME_LEN, "p:kprobes/%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) ret = dynevent_str_add(cmd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) dynevent_arg_init(&arg, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) arg.str = loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) ret = dynevent_arg_add(cmd, &arg, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) va_start(args, loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) const char *field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) field = va_arg(args, const char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) if (!field)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) if (++cmd->n_fields > MAX_TRACE_ARGS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) arg.str = field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) ret = dynevent_arg_add(cmd, &arg, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) EXPORT_SYMBOL_GPL(__kprobe_event_gen_cmd_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) * __kprobe_event_add_fields - Add probe fields to a kprobe command from arg list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) * @cmd: A pointer to the dynevent_cmd struct representing the new event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) * @args: Variable number of arg (pairs), one pair for each field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) * NOTE: Users normally won't want to call this function directly, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) * rather use the kprobe_event_add_fields() wrapper, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) * automatically adds a NULL to the end of the arg list. If this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) * function is used directly, make sure the last arg in the variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) * arg list is NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) * Add probe fields to an existing kprobe command using a variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) * list of args. Fields are added in the same order they're listed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) * Return: 0 if successful, error otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) int __kprobe_event_add_fields(struct dynevent_cmd *cmd, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) struct dynevent_arg arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) if (cmd->type != DYNEVENT_TYPE_KPROBE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) dynevent_arg_init(&arg, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) va_start(args, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) const char *field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) field = va_arg(args, const char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) if (!field)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) if (++cmd->n_fields > MAX_TRACE_ARGS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) arg.str = field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) ret = dynevent_arg_add(cmd, &arg, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) EXPORT_SYMBOL_GPL(__kprobe_event_add_fields);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) * kprobe_event_delete - Delete a kprobe event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) * @name: The name of the kprobe event to delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) * Delete a kprobe event with the give @name from kernel code rather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) * than directly from the command line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) * Return: 0 if successful, error otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) int kprobe_event_delete(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) char buf[MAX_EVENT_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) snprintf(buf, MAX_EVENT_NAME_LEN, "-:%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) return trace_run_command(buf, create_or_delete_trace_kprobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) EXPORT_SYMBOL_GPL(kprobe_event_delete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) static int trace_kprobe_release(struct dyn_event *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) struct trace_kprobe *tk = to_trace_kprobe(ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) int ret = unregister_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) free_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) struct trace_kprobe *tk = to_trace_kprobe(ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) if (trace_kprobe_is_return(tk) && tk->rp.maxactive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) seq_printf(m, "%d", tk->rp.maxactive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) seq_printf(m, ":%s/%s", trace_probe_group_name(&tk->tp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) trace_probe_name(&tk->tp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) if (!tk->symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) seq_printf(m, " 0x%p", tk->rp.kp.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) else if (tk->rp.kp.offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) tk->rp.kp.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) seq_printf(m, " %s", trace_kprobe_symbol(tk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) for (i = 0; i < tk->tp.nr_args; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) seq_putc(m, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) static int probes_seq_show(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) struct dyn_event *ev = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) if (!is_trace_kprobe(ev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) return trace_kprobe_show(m, ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) static const struct seq_operations probes_seq_op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) .start = dyn_event_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) .next = dyn_event_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) .stop = dyn_event_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) .show = probes_seq_show
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) static int probes_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) ret = security_locked_down(LOCKDOWN_TRACEFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) ret = dyn_events_release_all(&trace_kprobe_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) return seq_open(file, &probes_seq_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) static ssize_t probes_write(struct file *file, const char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) return trace_parse_run_command(file, buffer, count, ppos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) create_or_delete_trace_kprobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) static const struct file_operations kprobe_events_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) .open = probes_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) .read = seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) .llseek = seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) .release = seq_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) .write = probes_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) /* Probes profiling interfaces */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) static int probes_profile_seq_show(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) struct dyn_event *ev = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) struct trace_kprobe *tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) unsigned long nmissed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) if (!is_trace_kprobe(ev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) tk = to_trace_kprobe(ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) nmissed = trace_kprobe_is_return(tk) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) tk->rp.kp.nmissed + tk->rp.nmissed : tk->rp.kp.nmissed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) seq_printf(m, " %-44s %15lu %15lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) trace_probe_name(&tk->tp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) trace_kprobe_nhit(tk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) nmissed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) static const struct seq_operations profile_seq_op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) .start = dyn_event_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) .next = dyn_event_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) .stop = dyn_event_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) .show = probes_profile_seq_show
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) static int profile_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) ret = security_locked_down(LOCKDOWN_TRACEFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) return seq_open(file, &profile_seq_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) static const struct file_operations kprobe_profile_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) .open = profile_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) .read = seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) .llseek = seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) .release = seq_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) /* Kprobe specific fetch functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) /* Return the length of string -- including null terminal byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) static nokprobe_inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) fetch_store_strlen_user(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) const void __user *uaddr = (__force const void __user *)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) return strnlen_user_nofault(uaddr, MAX_STRING_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) /* Return the length of string -- including null terminal byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) static nokprobe_inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) fetch_store_strlen(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) int ret, len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) u8 c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) if (addr < TASK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) return fetch_store_strlen_user(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) ret = copy_from_kernel_nofault(&c, (u8 *)addr + len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) } while (c && ret == 0 && len < MAX_STRING_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) return (ret < 0) ? ret : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) * Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) * with max length and relative data location.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) static nokprobe_inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) fetch_store_string_user(unsigned long addr, void *dest, void *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) const void __user *uaddr = (__force const void __user *)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) int maxlen = get_loc_len(*(u32 *)dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) void *__dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) if (unlikely(!maxlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) __dest = get_loc_data(dest, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) ret = strncpy_from_user_nofault(__dest, uaddr, maxlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) *(u32 *)dest = make_data_loc(ret, __dest - base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) * length and relative data location.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) static nokprobe_inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) fetch_store_string(unsigned long addr, void *dest, void *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) int maxlen = get_loc_len(*(u32 *)dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) void *__dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) if ((unsigned long)addr < TASK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) return fetch_store_string_user(addr, dest, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) if (unlikely(!maxlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) __dest = get_loc_data(dest, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) * Try to get string again, since the string can be changed while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) * probing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) ret = strncpy_from_kernel_nofault(__dest, (void *)addr, maxlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) *(u32 *)dest = make_data_loc(ret, __dest - base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) static nokprobe_inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) probe_mem_read_user(void *dest, void *src, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) const void __user *uaddr = (__force const void __user *)src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) return copy_from_user_nofault(dest, uaddr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) static nokprobe_inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) probe_mem_read(void *dest, void *src, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) if ((unsigned long)src < TASK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) return probe_mem_read_user(dest, src, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) return copy_from_kernel_nofault(dest, src, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) /* Note that we don't verify it, since the code does not come from user space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) void *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) /* 1st stage: get value from context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) switch (code->op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) case FETCH_OP_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) val = regs_get_register(regs, code->param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) case FETCH_OP_STACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) val = regs_get_kernel_stack_nth(regs, code->param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) case FETCH_OP_STACKP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) val = kernel_stack_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) case FETCH_OP_RETVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) val = regs_return_value(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) case FETCH_OP_IMM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) val = code->immediate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) case FETCH_OP_COMM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) val = (unsigned long)current->comm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) case FETCH_OP_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) val = (unsigned long)code->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) case FETCH_OP_ARG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) val = regs_get_kernel_argument(regs, code->param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) case FETCH_NOP_SYMBOL: /* Ignore a place holder */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) code++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) code++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) return process_fetch_insn_bottom(code, val, dest, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) NOKPROBE_SYMBOL(process_fetch_insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) /* Kprobe handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) static nokprobe_inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) struct trace_event_file *trace_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) struct kprobe_trace_entry_head *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) struct trace_event_call *call = trace_probe_event_call(&tk->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) struct trace_event_buffer fbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) int dsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) WARN_ON(call != trace_file->event_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) if (trace_trigger_soft_disabled(trace_file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) local_save_flags(fbuffer.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) fbuffer.pc = preempt_count();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) fbuffer.trace_file = trace_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) dsize = __get_data_size(&tk->tp, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) fbuffer.event =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) trace_event_buffer_lock_reserve(&fbuffer.buffer, trace_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) call->event.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) sizeof(*entry) + tk->tp.size + dsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) fbuffer.flags, fbuffer.pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) if (!fbuffer.event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) fbuffer.regs = regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) entry->ip = (unsigned long)tk->rp.kp.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) trace_event_buffer_commit(&fbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) struct event_file_link *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) trace_probe_for_each_link_rcu(link, &tk->tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) __kprobe_trace_func(tk, regs, link->file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) NOKPROBE_SYMBOL(kprobe_trace_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) /* Kretprobe handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) static nokprobe_inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) struct trace_event_file *trace_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) struct kretprobe_trace_entry_head *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) struct trace_event_buffer fbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) struct trace_event_call *call = trace_probe_event_call(&tk->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) int dsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) WARN_ON(call != trace_file->event_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) if (trace_trigger_soft_disabled(trace_file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) local_save_flags(fbuffer.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) fbuffer.pc = preempt_count();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) fbuffer.trace_file = trace_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) dsize = __get_data_size(&tk->tp, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) fbuffer.event =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) trace_event_buffer_lock_reserve(&fbuffer.buffer, trace_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) call->event.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) sizeof(*entry) + tk->tp.size + dsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) fbuffer.flags, fbuffer.pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) if (!fbuffer.event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) fbuffer.regs = regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) entry->func = (unsigned long)tk->rp.kp.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) entry->ret_ip = (unsigned long)ri->ret_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) trace_event_buffer_commit(&fbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) struct event_file_link *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) trace_probe_for_each_link_rcu(link, &tk->tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) __kretprobe_trace_func(tk, ri, regs, link->file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) NOKPROBE_SYMBOL(kretprobe_trace_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) /* Event entry printers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) static enum print_line_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) print_kprobe_event(struct trace_iterator *iter, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) struct trace_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) struct kprobe_trace_entry_head *field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) struct trace_seq *s = &iter->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) struct trace_probe *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) field = (struct kprobe_trace_entry_head *)iter->ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) tp = trace_probe_primary_from_call(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) container_of(event, struct trace_event_call, event));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) if (WARN_ON_ONCE(!tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) trace_seq_printf(s, "%s: (", trace_probe_name(tp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) trace_seq_putc(s, ')');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) if (print_probe_args(s, tp->args, tp->nr_args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) (u8 *)&field[1], field) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) trace_seq_putc(s, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) return trace_handle_return(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) static enum print_line_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) print_kretprobe_event(struct trace_iterator *iter, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) struct trace_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) struct kretprobe_trace_entry_head *field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) struct trace_seq *s = &iter->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) struct trace_probe *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) field = (struct kretprobe_trace_entry_head *)iter->ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) tp = trace_probe_primary_from_call(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) container_of(event, struct trace_event_call, event));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) if (WARN_ON_ONCE(!tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) trace_seq_printf(s, "%s: (", trace_probe_name(tp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) trace_seq_puts(s, " <- ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) trace_seq_putc(s, ')');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) if (print_probe_args(s, tp->args, tp->nr_args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) (u8 *)&field[1], field) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) trace_seq_putc(s, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) return trace_handle_return(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) static int kprobe_event_define_fields(struct trace_event_call *event_call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) struct kprobe_trace_entry_head field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) struct trace_probe *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) tp = trace_probe_primary_from_call(event_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) if (WARN_ON_ONCE(!tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) static int kretprobe_event_define_fields(struct trace_event_call *event_call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) struct kretprobe_trace_entry_head field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) struct trace_probe *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) tp = trace_probe_primary_from_call(event_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) if (WARN_ON_ONCE(!tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) #ifdef CONFIG_PERF_EVENTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) /* Kprobe profile handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) struct trace_event_call *call = trace_probe_event_call(&tk->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) struct kprobe_trace_entry_head *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) int size, __size, dsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) int rctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) if (bpf_prog_array_valid(call)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) unsigned long orig_ip = instruction_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) ret = trace_call_bpf(call, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) * We need to check and see if we modified the pc of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) * pt_regs, and if so return 1 so that we don't do the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) * single stepping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) if (orig_ip != instruction_pointer(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) head = this_cpu_ptr(call->perf_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) if (hlist_empty(head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) dsize = __get_data_size(&tk->tp, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) __size = sizeof(*entry) + tk->tp.size + dsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) size = ALIGN(__size + sizeof(u32), sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) size -= sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) entry = perf_trace_buf_alloc(size, NULL, &rctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) entry->ip = (unsigned long)tk->rp.kp.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) memset(&entry[1], 0, dsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) head, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) NOKPROBE_SYMBOL(kprobe_perf_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) /* Kretprobe profile handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) struct trace_event_call *call = trace_probe_event_call(&tk->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) struct kretprobe_trace_entry_head *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) int size, __size, dsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) int rctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) head = this_cpu_ptr(call->perf_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) if (hlist_empty(head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) dsize = __get_data_size(&tk->tp, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) __size = sizeof(*entry) + tk->tp.size + dsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) size = ALIGN(__size + sizeof(u32), sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) size -= sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) entry = perf_trace_buf_alloc(size, NULL, &rctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) entry->func = (unsigned long)tk->rp.kp.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) entry->ret_ip = (unsigned long)ri->ret_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) head, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) NOKPROBE_SYMBOL(kretprobe_perf_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) const char **symbol, u64 *probe_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) u64 *probe_addr, bool perf_type_tracepoint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) const char *pevent = trace_event_name(event->tp_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) const char *group = event->tp_event->class->system;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) struct trace_kprobe *tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) if (perf_type_tracepoint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) tk = find_trace_kprobe(pevent, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) tk = trace_kprobe_primary_from_call(event->tp_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) if (!tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) : BPF_FD_TYPE_KPROBE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) if (tk->symbol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) *symbol = tk->symbol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) *probe_offset = tk->rp.kp.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) *probe_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) *symbol = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) *probe_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) *probe_addr = (unsigned long)tk->rp.kp.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) #endif /* CONFIG_PERF_EVENTS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) * lockless, but we can't race with this __init function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) static int kprobe_register(struct trace_event_call *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) enum trace_reg type, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) struct trace_event_file *file = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) case TRACE_REG_REGISTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) return enable_trace_kprobe(event, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) case TRACE_REG_UNREGISTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) return disable_trace_kprobe(event, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) #ifdef CONFIG_PERF_EVENTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) case TRACE_REG_PERF_REGISTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) return enable_trace_kprobe(event, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) case TRACE_REG_PERF_UNREGISTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) return disable_trace_kprobe(event, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) case TRACE_REG_PERF_OPEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) case TRACE_REG_PERF_CLOSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) case TRACE_REG_PERF_ADD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) case TRACE_REG_PERF_DEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) raw_cpu_inc(*tk->nhit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) kprobe_trace_func(tk, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) #ifdef CONFIG_PERF_EVENTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) ret = kprobe_perf_func(tk, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) NOKPROBE_SYMBOL(kprobe_dispatcher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) raw_cpu_inc(*tk->nhit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) kretprobe_trace_func(tk, ri, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) #ifdef CONFIG_PERF_EVENTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) kretprobe_perf_func(tk, ri, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) return 0; /* We don't tweek kernel, so just return 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) NOKPROBE_SYMBOL(kretprobe_dispatcher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) static struct trace_event_functions kretprobe_funcs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) .trace = print_kretprobe_event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) static struct trace_event_functions kprobe_funcs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) .trace = print_kprobe_event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) static struct trace_event_fields kretprobe_fields_array[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) { .type = TRACE_FUNCTION_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) .define_fields = kretprobe_event_define_fields },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) static struct trace_event_fields kprobe_fields_array[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) { .type = TRACE_FUNCTION_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) .define_fields = kprobe_event_define_fields },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) static inline void init_trace_event_call(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) struct trace_event_call *call = trace_probe_event_call(&tk->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) if (trace_kprobe_is_return(tk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) call->event.funcs = &kretprobe_funcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) call->class->fields_array = kretprobe_fields_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) call->event.funcs = &kprobe_funcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) call->class->fields_array = kprobe_fields_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) call->flags = TRACE_EVENT_FL_KPROBE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) call->class->reg = kprobe_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) static int register_kprobe_event(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) init_trace_event_call(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) return trace_probe_register_event_call(&tk->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) static int unregister_kprobe_event(struct trace_kprobe *tk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) return trace_probe_unregister_event_call(&tk->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) #ifdef CONFIG_PERF_EVENTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) /* create a trace_kprobe, but don't add it to global lists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) struct trace_event_call *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) bool is_return)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) struct trace_kprobe *tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) char *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) * local trace_kprobes are not added to dyn_event, so they are never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) * searched in find_trace_kprobe(). Therefore, there is no concern of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) * duplicated name here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) event = func ? func : "DUMMY_EVENT";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) offs, 0 /* maxactive */, 0 /* nargs */,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) is_return);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) if (IS_ERR(tk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) pr_info("Failed to allocate trace_probe.(%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) (int)PTR_ERR(tk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) return ERR_CAST(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) init_trace_event_call(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) ret = __register_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) return trace_probe_event_call(&tk->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) free_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) void destroy_local_trace_kprobe(struct trace_event_call *event_call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) struct trace_kprobe *tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) tk = trace_kprobe_primary_from_call(event_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) if (unlikely(!tk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) if (trace_probe_is_enabled(&tk->tp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) __unregister_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) free_trace_kprobe(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) #endif /* CONFIG_PERF_EVENTS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) static __init void enable_boot_kprobe_events(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) struct trace_array *tr = top_trace_array();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) struct trace_event_file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) struct trace_kprobe *tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) struct dyn_event *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) mutex_lock(&event_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) for_each_trace_kprobe(tk, pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) list_for_each_entry(file, &tr->events, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) if (file->event_call == trace_probe_event_call(&tk->tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) trace_event_enable_disable(file, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) mutex_unlock(&event_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) static __init void setup_boot_kprobe_events(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) char *p, *cmd = kprobe_boot_events_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) strreplace(kprobe_boot_events_buf, ',', ' ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) while (cmd && *cmd != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) p = strchr(cmd, ';');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) *p++ = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) ret = trace_run_command(cmd, create_or_delete_trace_kprobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) pr_warn("Failed to add event(%d): %s\n", ret, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) cmd = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) enable_boot_kprobe_events();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) * Register dynevent at core_initcall. This allows kernel to setup kprobe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) * events in postcore_initcall without tracefs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) static __init int init_kprobe_trace_early(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) ret = dyn_event_register(&trace_kprobe_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) if (register_module_notifier(&trace_kprobe_module_nb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) core_initcall(init_kprobe_trace_early);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) /* Make a tracefs interface for controlling probe points */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) static __init int init_kprobe_trace(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) struct dentry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) ret = tracing_init_dentry();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) entry = tracefs_create_file("kprobe_events", 0644, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) NULL, &kprobe_events_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) /* Event list interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) pr_warn("Could not create tracefs 'kprobe_events' entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) /* Profile interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) entry = tracefs_create_file("kprobe_profile", 0444, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) NULL, &kprobe_profile_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) setup_boot_kprobe_events();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) fs_initcall(init_kprobe_trace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) #ifdef CONFIG_FTRACE_STARTUP_TEST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) static __init struct trace_event_file *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) struct trace_event_file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) list_for_each_entry(file, &tr->events, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) if (file->event_call == trace_probe_event_call(&tk->tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) return file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) * stage, we can do this lockless.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) static __init int kprobe_trace_self_tests_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) int ret, warn = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) int (*target)(int, int, int, int, int, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) struct trace_kprobe *tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) struct trace_event_file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) if (tracing_is_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) if (tracing_selftest_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) target = kprobe_trace_selftest_target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) pr_info("Testing kprobe tracing: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) ret = trace_run_command("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) create_or_delete_trace_kprobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) if (WARN_ON_ONCE(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) pr_warn("error on probing function entry.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) /* Enable trace point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) if (WARN_ON_ONCE(tk == NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) pr_warn("error on getting new probe.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) file = find_trace_probe_file(tk, top_trace_array());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) if (WARN_ON_ONCE(file == NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) pr_warn("error on getting probe file.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) enable_trace_kprobe(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) trace_probe_event_call(&tk->tp), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target $retval",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) create_or_delete_trace_kprobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) if (WARN_ON_ONCE(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) pr_warn("error on probing function return.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) /* Enable trace point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) if (WARN_ON_ONCE(tk == NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) pr_warn("error on getting 2nd new probe.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) file = find_trace_probe_file(tk, top_trace_array());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) if (WARN_ON_ONCE(file == NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) pr_warn("error on getting probe file.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) enable_trace_kprobe(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) trace_probe_event_call(&tk->tp), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) if (warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) ret = target(1, 2, 3, 4, 5, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) * Not expecting an error here, the check is only to prevent the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) * optimizer from removing the call to target() as otherwise there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) * are no side-effects and the call is never performed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) if (ret != 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) /* Disable trace points before removing it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) if (WARN_ON_ONCE(tk == NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) pr_warn("error on getting test probe.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) if (trace_kprobe_nhit(tk) != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) pr_warn("incorrect number of testprobe hits\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) file = find_trace_probe_file(tk, top_trace_array());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) if (WARN_ON_ONCE(file == NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) pr_warn("error on getting probe file.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) disable_trace_kprobe(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) trace_probe_event_call(&tk->tp), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) if (WARN_ON_ONCE(tk == NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) pr_warn("error on getting 2nd test probe.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) if (trace_kprobe_nhit(tk) != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) pr_warn("incorrect number of testprobe2 hits\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) file = find_trace_probe_file(tk, top_trace_array());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) if (WARN_ON_ONCE(file == NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) pr_warn("error on getting probe file.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) disable_trace_kprobe(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) trace_probe_event_call(&tk->tp), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) ret = trace_run_command("-:testprobe", create_or_delete_trace_kprobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) if (WARN_ON_ONCE(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) pr_warn("error on deleting a probe.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) ret = trace_run_command("-:testprobe2", create_or_delete_trace_kprobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) if (WARN_ON_ONCE(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) pr_warn("error on deleting a probe.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) ret = dyn_events_release_all(&trace_kprobe_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) if (WARN_ON_ONCE(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) pr_warn("error on cleaning up probes.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) warn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) * Wait for the optimizer work to finish. Otherwise it might fiddle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) * with probes in already freed __init text.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) wait_for_kprobe_optimizer();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) if (warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) pr_cont("NG: Some tests are failed. Please check them.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) pr_cont("OK\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) late_initcall(kprobe_trace_self_tests_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) #endif