blob: eda220bf2065e6cfd441b3a74ba4251254680965 [file] [log] [blame]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001/*
Masami Hiramatsu77b44d12009-11-03 19:12:47 -05002 * Kprobes-based tracing events
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04003 *
4 * Created by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/module.h>
21#include <linux/uaccess.h>
22#include <linux/kprobes.h>
23#include <linux/seq_file.h>
24#include <linux/slab.h>
25#include <linux/smp.h>
26#include <linux/debugfs.h>
27#include <linux/types.h>
28#include <linux/string.h>
29#include <linux/ctype.h>
30#include <linux/ptrace.h>
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +020031#include <linux/perf_event.h>
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040032
33#include "trace.h"
34#include "trace_output.h"
35
Masami Hiramatsua82378d2009-08-13 16:35:18 -040036#define MAX_TRACE_ARGS 128
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040037#define MAX_ARGSTR_LEN 63
Masami Hiramatsu42635652009-08-13 16:35:26 -040038#define MAX_EVENT_NAME_LEN 64
Masami Hiramatsuf52487e2009-09-10 19:53:53 -040039#define KPROBE_EVENT_SYSTEM "kprobes"
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040040
Masami Hiramatsua703d942009-10-07 18:28:07 -040041/* Reserved field names */
Masami Hiramatsue93f4d82009-10-07 18:28:14 -040042#define FIELD_STRING_IP "__probe_ip"
43#define FIELD_STRING_NARGS "__probe_nargs"
44#define FIELD_STRING_RETIP "__probe_ret_ip"
45#define FIELD_STRING_FUNC "__probe_func"
Masami Hiramatsua703d942009-10-07 18:28:07 -040046
47const char *reserved_field_names[] = {
48 "common_type",
49 "common_flags",
50 "common_preempt_count",
51 "common_pid",
52 "common_tgid",
53 "common_lock_depth",
54 FIELD_STRING_IP,
55 FIELD_STRING_NARGS,
56 FIELD_STRING_RETIP,
57 FIELD_STRING_FUNC,
58};
59
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040060struct fetch_func {
61 unsigned long (*func)(struct pt_regs *, void *);
62 void *data;
63};
64
65static __kprobes unsigned long call_fetch(struct fetch_func *f,
66 struct pt_regs *regs)
67{
68 return f->func(regs, f->data);
69}
70
71/* fetch handlers */
72static __kprobes unsigned long fetch_register(struct pt_regs *regs,
73 void *offset)
74{
75 return regs_get_register(regs, (unsigned int)((unsigned long)offset));
76}
77
78static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
79 void *num)
80{
81 return regs_get_kernel_stack_nth(regs,
82 (unsigned int)((unsigned long)num));
83}
84
85static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
86{
87 unsigned long retval;
88
89 if (probe_kernel_address(addr, retval))
90 return 0;
91 return retval;
92}
93
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040094static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
95 void *dummy)
96{
97 return regs_return_value(regs);
98}
99
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400100static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
101 void *dummy)
102{
103 return kernel_stack_pointer(regs);
104}
105
106/* Memory fetching by symbol */
107struct symbol_cache {
108 char *symbol;
109 long offset;
110 unsigned long addr;
111};
112
113static unsigned long update_symbol_cache(struct symbol_cache *sc)
114{
115 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
116 if (sc->addr)
117 sc->addr += sc->offset;
118 return sc->addr;
119}
120
121static void free_symbol_cache(struct symbol_cache *sc)
122{
123 kfree(sc->symbol);
124 kfree(sc);
125}
126
127static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
128{
129 struct symbol_cache *sc;
130
131 if (!sym || strlen(sym) == 0)
132 return NULL;
133 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
134 if (!sc)
135 return NULL;
136
137 sc->symbol = kstrdup(sym, GFP_KERNEL);
138 if (!sc->symbol) {
139 kfree(sc);
140 return NULL;
141 }
142 sc->offset = offset;
143
144 update_symbol_cache(sc);
145 return sc;
146}
147
148static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
149{
150 struct symbol_cache *sc = data;
151
152 if (sc->addr)
153 return fetch_memory(regs, (void *)sc->addr);
154 else
155 return 0;
156}
157
158/* Special indirect memory access interface */
159struct indirect_fetch_data {
160 struct fetch_func orig;
161 long offset;
162};
163
164static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
165{
166 struct indirect_fetch_data *ind = data;
167 unsigned long addr;
168
169 addr = call_fetch(&ind->orig, regs);
170 if (addr) {
171 addr += ind->offset;
172 return fetch_memory(regs, (void *)addr);
173 } else
174 return 0;
175}
176
177static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
178{
179 if (data->orig.func == fetch_indirect)
180 free_indirect_fetch_data(data->orig.data);
181 else if (data->orig.func == fetch_symbol)
182 free_symbol_cache(data->orig.data);
183 kfree(data);
184}
185
186/**
Masami Hiramatsu77b44d12009-11-03 19:12:47 -0500187 * Kprobe event core functions
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400188 */
189
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400190struct probe_arg {
191 struct fetch_func fetch;
192 const char *name;
193};
194
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400195/* Flags for trace_probe */
196#define TP_FLAG_TRACE 1
197#define TP_FLAG_PROFILE 2
198
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400199struct trace_probe {
200 struct list_head list;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200201 struct kretprobe rp; /* Use rp.kp for kprobe use */
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400202 unsigned long nhit;
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400203 unsigned int flags; /* For TP_FLAG_* */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400204 const char *symbol; /* symbol name */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400205 struct ftrace_event_call call;
Masami Hiramatsuff50d992009-08-13 16:35:34 -0400206 struct trace_event event;
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400207 unsigned int nr_args;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400208 struct probe_arg args[];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400209};
210
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400211#define SIZEOF_TRACE_PROBE(n) \
212 (offsetof(struct trace_probe, args) + \
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400213 (sizeof(struct probe_arg) * (n)))
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400214
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400215static __kprobes int probe_is_return(struct trace_probe *tp)
216{
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200217 return tp->rp.handler != NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400218}
219
220static __kprobes const char *probe_symbol(struct trace_probe *tp)
221{
222 return tp->symbol ? tp->symbol : "unknown";
223}
224
Masami Hiramatsu30a7e072009-08-21 15:43:51 -0400225static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400226{
227 int ret = -EINVAL;
228
Masami Hiramatsu14640102010-01-05 17:46:48 -0500229 if (ff->func == fetch_register) {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400230 const char *name;
231 name = regs_query_register_name((unsigned int)((long)ff->data));
232 ret = snprintf(buf, n, "%%%s", name);
233 } else if (ff->func == fetch_stack)
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400234 ret = snprintf(buf, n, "$stack%lu", (unsigned long)ff->data);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400235 else if (ff->func == fetch_memory)
236 ret = snprintf(buf, n, "@0x%p", ff->data);
237 else if (ff->func == fetch_symbol) {
238 struct symbol_cache *sc = ff->data;
Lai Jiangshan52a11f32009-11-25 16:33:15 +0800239 if (sc->offset)
240 ret = snprintf(buf, n, "@%s%+ld", sc->symbol,
241 sc->offset);
242 else
243 ret = snprintf(buf, n, "@%s", sc->symbol);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400244 } else if (ff->func == fetch_retvalue)
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400245 ret = snprintf(buf, n, "$retval");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400246 else if (ff->func == fetch_stack_address)
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400247 ret = snprintf(buf, n, "$stack");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400248 else if (ff->func == fetch_indirect) {
249 struct indirect_fetch_data *id = ff->data;
250 size_t l = 0;
251 ret = snprintf(buf, n, "%+ld(", id->offset);
252 if (ret >= n)
253 goto end;
254 l += ret;
Masami Hiramatsu30a7e072009-08-21 15:43:51 -0400255 ret = probe_arg_string(buf + l, n - l, &id->orig);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400256 if (ret < 0)
257 goto end;
258 l += ret;
259 ret = snprintf(buf + l, n - l, ")");
260 ret += l;
261 }
262end:
263 if (ret >= n)
264 return -ENOSPC;
265 return ret;
266}
267
268static int register_probe_event(struct trace_probe *tp);
269static void unregister_probe_event(struct trace_probe *tp);
270
271static DEFINE_MUTEX(probe_lock);
272static LIST_HEAD(probe_list);
273
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400274static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
275static int kretprobe_dispatcher(struct kretprobe_instance *ri,
276 struct pt_regs *regs);
277
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500278/* Check the name is good for event/group */
279static int check_event_name(const char *name)
280{
281 if (!isalpha(*name) && *name != '_')
282 return 0;
283 while (*++name != '\0') {
284 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
285 return 0;
286 }
287 return 1;
288}
289
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200290/*
291 * Allocate new trace_probe and initialize it (including kprobes).
292 */
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400293static struct trace_probe *alloc_trace_probe(const char *group,
294 const char *event,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200295 void *addr,
296 const char *symbol,
297 unsigned long offs,
298 int nargs, int is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400299{
300 struct trace_probe *tp;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500301 int ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400302
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400303 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400304 if (!tp)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500305 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400306
307 if (symbol) {
308 tp->symbol = kstrdup(symbol, GFP_KERNEL);
309 if (!tp->symbol)
310 goto error;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200311 tp->rp.kp.symbol_name = tp->symbol;
312 tp->rp.kp.offset = offs;
313 } else
314 tp->rp.kp.addr = addr;
315
316 if (is_return)
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400317 tp->rp.handler = kretprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200318 else
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400319 tp->rp.kp.pre_handler = kprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200320
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500321 if (!event || !check_event_name(event)) {
322 ret = -EINVAL;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400323 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500324 }
325
Masami Hiramatsu42635652009-08-13 16:35:26 -0400326 tp->call.name = kstrdup(event, GFP_KERNEL);
327 if (!tp->call.name)
328 goto error;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400329
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500330 if (!group || !check_event_name(group)) {
331 ret = -EINVAL;
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400332 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500333 }
334
Steven Rostedt8f082012010-04-20 10:47:33 -0400335 tp->call.class->system = kstrdup(group, GFP_KERNEL);
336 if (!tp->call.class->system)
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400337 goto error;
338
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400339 INIT_LIST_HEAD(&tp->list);
340 return tp;
341error:
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400342 kfree(tp->call.name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400343 kfree(tp->symbol);
344 kfree(tp);
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500345 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400346}
347
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400348static void free_probe_arg(struct probe_arg *arg)
349{
350 if (arg->fetch.func == fetch_symbol)
351 free_symbol_cache(arg->fetch.data);
352 else if (arg->fetch.func == fetch_indirect)
353 free_indirect_fetch_data(arg->fetch.data);
354 kfree(arg->name);
355}
356
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400357static void free_trace_probe(struct trace_probe *tp)
358{
359 int i;
360
361 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400362 free_probe_arg(&tp->args[i]);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400363
Steven Rostedt8f082012010-04-20 10:47:33 -0400364 kfree(tp->call.class->system);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400365 kfree(tp->call.name);
366 kfree(tp->symbol);
367 kfree(tp);
368}
369
Masami Hiramatsudd004c42009-10-27 16:42:44 -0400370static struct trace_probe *find_probe_event(const char *event,
371 const char *group)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400372{
373 struct trace_probe *tp;
374
375 list_for_each_entry(tp, &probe_list, list)
Masami Hiramatsudd004c42009-10-27 16:42:44 -0400376 if (strcmp(tp->call.name, event) == 0 &&
Steven Rostedt8f082012010-04-20 10:47:33 -0400377 strcmp(tp->call.class->system, group) == 0)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400378 return tp;
379 return NULL;
380}
381
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400382/* Unregister a trace_probe and probe_event: call with locking probe_lock */
383static void unregister_trace_probe(struct trace_probe *tp)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400384{
385 if (probe_is_return(tp))
386 unregister_kretprobe(&tp->rp);
387 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200388 unregister_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400389 list_del(&tp->list);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400390 unregister_probe_event(tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400391}
392
393/* Register a trace_probe and probe_event */
394static int register_trace_probe(struct trace_probe *tp)
395{
396 struct trace_probe *old_tp;
397 int ret;
398
399 mutex_lock(&probe_lock);
400
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400401 /* register as an event */
Steven Rostedt8f082012010-04-20 10:47:33 -0400402 old_tp = find_probe_event(tp->call.name, tp->call.class->system);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400403 if (old_tp) {
404 /* delete old event */
405 unregister_trace_probe(old_tp);
406 free_trace_probe(old_tp);
407 }
408 ret = register_probe_event(tp);
409 if (ret) {
410 pr_warning("Faild to register probe event(%d)\n", ret);
411 goto end;
412 }
413
Masami Hiramatsu5a0d9052009-09-14 16:49:37 -0400414 tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400415 if (probe_is_return(tp))
416 ret = register_kretprobe(&tp->rp);
417 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200418 ret = register_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400419
420 if (ret) {
421 pr_warning("Could not insert probe(%d)\n", ret);
422 if (ret == -EILSEQ) {
423 pr_warning("Probing address(0x%p) is not an "
424 "instruction boundary.\n",
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200425 tp->rp.kp.addr);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400426 ret = -EINVAL;
427 }
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400428 unregister_probe_event(tp);
429 } else
430 list_add_tail(&tp->list, &probe_list);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400431end:
432 mutex_unlock(&probe_lock);
433 return ret;
434}
435
436/* Split symbol and offset. */
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400437static int split_symbol_offset(char *symbol, unsigned long *offset)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400438{
439 char *tmp;
440 int ret;
441
442 if (!offset)
443 return -EINVAL;
444
445 tmp = strchr(symbol, '+');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400446 if (tmp) {
447 /* skip sign because strict_strtol doesn't accept '+' */
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400448 ret = strict_strtoul(tmp + 1, 0, offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400449 if (ret)
450 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400451 *tmp = '\0';
452 } else
453 *offset = 0;
454 return 0;
455}
456
457#define PARAM_MAX_ARGS 16
458#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
459
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400460static int parse_probe_vars(char *arg, struct fetch_func *ff, int is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400461{
462 int ret = 0;
463 unsigned long param;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400464
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400465 if (strcmp(arg, "retval") == 0) {
466 if (is_return) {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400467 ff->func = fetch_retvalue;
468 ff->data = NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400469 } else
470 ret = -EINVAL;
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400471 } else if (strncmp(arg, "stack", 5) == 0) {
472 if (arg[5] == '\0') {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400473 ff->func = fetch_stack_address;
474 ff->data = NULL;
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400475 } else if (isdigit(arg[5])) {
476 ret = strict_strtoul(arg + 5, 10, &param);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400477 if (ret || param > PARAM_MAX_STACK)
478 ret = -EINVAL;
479 else {
480 ff->func = fetch_stack;
481 ff->data = (void *)param;
482 }
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400483 } else
484 ret = -EINVAL;
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400485 } else
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400486 ret = -EINVAL;
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400487 return ret;
488}
489
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500490/* Recursive argument parser */
491static int __parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400492{
493 int ret = 0;
494 unsigned long param;
495 long offset;
496 char *tmp;
497
498 switch (arg[0]) {
499 case '$':
500 ret = parse_probe_vars(arg + 1, ff, is_return);
501 break;
502 case '%': /* named register */
503 ret = regs_query_register_offset(arg + 1);
504 if (ret >= 0) {
505 ff->func = fetch_register;
506 ff->data = (void *)(unsigned long)ret;
507 ret = 0;
508 }
509 break;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400510 case '@': /* memory or symbol */
511 if (isdigit(arg[1])) {
512 ret = strict_strtoul(arg + 1, 0, &param);
513 if (ret)
514 break;
515 ff->func = fetch_memory;
516 ff->data = (void *)param;
517 } else {
518 ret = split_symbol_offset(arg + 1, &offset);
519 if (ret)
520 break;
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400521 ff->data = alloc_symbol_cache(arg + 1, offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400522 if (ff->data)
523 ff->func = fetch_symbol;
524 else
525 ret = -EINVAL;
526 }
527 break;
528 case '+': /* indirect memory */
529 case '-':
530 tmp = strchr(arg, '(');
531 if (!tmp) {
532 ret = -EINVAL;
533 break;
534 }
535 *tmp = '\0';
536 ret = strict_strtol(arg + 1, 0, &offset);
537 if (ret)
538 break;
539 if (arg[0] == '-')
540 offset = -offset;
541 arg = tmp + 1;
542 tmp = strrchr(arg, ')');
543 if (tmp) {
544 struct indirect_fetch_data *id;
545 *tmp = '\0';
546 id = kzalloc(sizeof(struct indirect_fetch_data),
547 GFP_KERNEL);
548 if (!id)
549 return -ENOMEM;
550 id->offset = offset;
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500551 ret = __parse_probe_arg(arg, &id->orig, is_return);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400552 if (ret)
553 kfree(id);
554 else {
555 ff->func = fetch_indirect;
556 ff->data = (void *)id;
557 }
558 } else
559 ret = -EINVAL;
560 break;
561 default:
562 /* TODO: support custom handler */
563 ret = -EINVAL;
564 }
565 return ret;
566}
567
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500568/* String length checking wrapper */
569static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
570{
571 if (strlen(arg) > MAX_ARGSTR_LEN) {
572 pr_info("Argument is too long.: %s\n", arg);
573 return -ENOSPC;
574 }
575 return __parse_probe_arg(arg, ff, is_return);
576}
577
Masami Hiramatsua703d942009-10-07 18:28:07 -0400578/* Return 1 if name is reserved or already used by another argument */
579static int conflict_field_name(const char *name,
580 struct probe_arg *args, int narg)
581{
582 int i;
583 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
584 if (strcmp(reserved_field_names[i], name) == 0)
585 return 1;
586 for (i = 0; i < narg; i++)
587 if (strcmp(args[i].name, name) == 0)
588 return 1;
589 return 0;
590}
591
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400592static int create_trace_probe(int argc, char **argv)
593{
594 /*
595 * Argument syntax:
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400596 * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
597 * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400598 * Fetch args:
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400599 * $retval : fetch return value
600 * $stack : fetch stack address
601 * $stackN : fetch Nth of stack (N:0-)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400602 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
603 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
604 * %REG : fetch register REG
605 * Indirect memory fetch:
606 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400607 * Alias name of args:
608 * NAME=FETCHARG : set NAME as alias of FETCHARG.
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400609 */
610 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400611 int i, ret = 0;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500612 int is_return = 0, is_delete = 0;
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400613 char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400614 unsigned long offset = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400615 void *addr = NULL;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200616 char buf[MAX_EVENT_NAME_LEN];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400617
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500618 /* argc must be >= 1 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400619 if (argv[0][0] == 'p')
620 is_return = 0;
621 else if (argv[0][0] == 'r')
622 is_return = 1;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500623 else if (argv[0][0] == '-')
624 is_delete = 1;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400625 else {
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500626 pr_info("Probe definition must be started with 'p', 'r' or"
627 " '-'.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400628 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400629 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400630
631 if (argv[0][1] == ':') {
632 event = &argv[0][2];
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400633 if (strchr(event, '/')) {
634 group = event;
635 event = strchr(group, '/') + 1;
636 event[-1] = '\0';
637 if (strlen(group) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800638 pr_info("Group name is not specified\n");
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400639 return -EINVAL;
640 }
641 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400642 if (strlen(event) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800643 pr_info("Event name is not specified\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400644 return -EINVAL;
645 }
646 }
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500647 if (!group)
648 group = KPROBE_EVENT_SYSTEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400649
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500650 if (is_delete) {
651 if (!event) {
652 pr_info("Delete command needs an event name.\n");
653 return -EINVAL;
654 }
655 tp = find_probe_event(event, group);
656 if (!tp) {
657 pr_info("Event %s/%s doesn't exist.\n", group, event);
658 return -ENOENT;
659 }
660 /* delete an event */
661 unregister_trace_probe(tp);
662 free_trace_probe(tp);
663 return 0;
664 }
665
666 if (argc < 2) {
667 pr_info("Probe point is not specified.\n");
668 return -EINVAL;
669 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400670 if (isdigit(argv[1][0])) {
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400671 if (is_return) {
672 pr_info("Return probe point must be a symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400673 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400674 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400675 /* an address specified */
Heiko Carstensa9bb18f2010-02-10 17:23:47 +0100676 ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400677 if (ret) {
678 pr_info("Failed to parse address.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400679 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400680 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400681 } else {
682 /* a symbol specified */
683 symbol = argv[1];
684 /* TODO: support .init module functions */
685 ret = split_symbol_offset(symbol, &offset);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400686 if (ret) {
687 pr_info("Failed to parse symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400688 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400689 }
690 if (offset && is_return) {
691 pr_info("Return probe must be used without offset.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400692 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400693 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400694 }
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400695 argc -= 2; argv += 2;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400696
697 /* setup a probe */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400698 if (!event) {
699 /* Make a new event name */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400700 if (symbol)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500701 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400702 is_return ? 'r' : 'p', symbol, offset);
703 else
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500704 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400705 is_return ? 'r' : 'p', addr);
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200706 event = buf;
707 }
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400708 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
709 is_return);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400710 if (IS_ERR(tp)) {
711 pr_info("Failed to allocate trace_probe.(%d)\n",
712 (int)PTR_ERR(tp));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400713 return PTR_ERR(tp);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400714 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400715
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400716 /* parse arguments */
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400717 ret = 0;
718 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400719 /* Parse argument name */
720 arg = strchr(argv[i], '=');
721 if (arg)
722 *arg++ = '\0';
723 else
724 arg = argv[i];
Masami Hiramatsua703d942009-10-07 18:28:07 -0400725
726 if (conflict_field_name(argv[i], tp->args, i)) {
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400727 pr_info("Argument%d name '%s' conflicts with "
728 "another field.\n", i, argv[i]);
Masami Hiramatsua703d942009-10-07 18:28:07 -0400729 ret = -EINVAL;
730 goto error;
731 }
732
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400733 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500734 if (!tp->args[i].name) {
735 pr_info("Failed to allocate argument%d name '%s'.\n",
736 i, argv[i]);
737 ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400738 goto error;
739 }
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500740
741 /* Parse fetch argument */
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400742 ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400743 if (ret) {
744 pr_info("Parse error at argument%d. (%d)\n", i, ret);
Lai Jiangshanabab9d32009-11-25 16:32:21 +0800745 kfree(tp->args[i].name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400746 goto error;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400747 }
Lai Jiangshanabab9d32009-11-25 16:32:21 +0800748
749 tp->nr_args++;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400750 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400751
752 ret = register_trace_probe(tp);
753 if (ret)
754 goto error;
755 return 0;
756
757error:
758 free_trace_probe(tp);
759 return ret;
760}
761
762static void cleanup_all_probes(void)
763{
764 struct trace_probe *tp;
765
766 mutex_lock(&probe_lock);
767 /* TODO: Use batch unregistration */
768 while (!list_empty(&probe_list)) {
769 tp = list_entry(probe_list.next, struct trace_probe, list);
770 unregister_trace_probe(tp);
771 free_trace_probe(tp);
772 }
773 mutex_unlock(&probe_lock);
774}
775
776
777/* Probes listing interfaces */
778static void *probes_seq_start(struct seq_file *m, loff_t *pos)
779{
780 mutex_lock(&probe_lock);
781 return seq_list_start(&probe_list, *pos);
782}
783
784static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
785{
786 return seq_list_next(v, &probe_list, pos);
787}
788
789static void probes_seq_stop(struct seq_file *m, void *v)
790{
791 mutex_unlock(&probe_lock);
792}
793
794static int probes_seq_show(struct seq_file *m, void *v)
795{
796 struct trace_probe *tp = v;
797 int i, ret;
798 char buf[MAX_ARGSTR_LEN + 1];
799
800 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
Steven Rostedt8f082012010-04-20 10:47:33 -0400801 seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400802
Lai Jiangshan52a11f32009-11-25 16:33:15 +0800803 if (!tp->symbol)
804 seq_printf(m, " 0x%p", tp->rp.kp.addr);
805 else if (tp->rp.kp.offset)
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200806 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400807 else
Lai Jiangshan52a11f32009-11-25 16:33:15 +0800808 seq_printf(m, " %s", probe_symbol(tp));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400809
810 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400811 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400812 if (ret < 0) {
813 pr_warning("Argument%d decoding error(%d).\n", i, ret);
814 return ret;
815 }
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400816 seq_printf(m, " %s=%s", tp->args[i].name, buf);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400817 }
818 seq_printf(m, "\n");
819 return 0;
820}
821
822static const struct seq_operations probes_seq_op = {
823 .start = probes_seq_start,
824 .next = probes_seq_next,
825 .stop = probes_seq_stop,
826 .show = probes_seq_show
827};
828
829static int probes_open(struct inode *inode, struct file *file)
830{
831 if ((file->f_mode & FMODE_WRITE) &&
832 (file->f_flags & O_TRUNC))
833 cleanup_all_probes();
834
835 return seq_open(file, &probes_seq_op);
836}
837
838static int command_trace_probe(const char *buf)
839{
840 char **argv;
841 int argc = 0, ret = 0;
842
843 argv = argv_split(GFP_KERNEL, buf, &argc);
844 if (!argv)
845 return -ENOMEM;
846
847 if (argc)
848 ret = create_trace_probe(argc, argv);
849
850 argv_free(argv);
851 return ret;
852}
853
854#define WRITE_BUFSIZE 128
855
856static ssize_t probes_write(struct file *file, const char __user *buffer,
857 size_t count, loff_t *ppos)
858{
859 char *kbuf, *tmp;
860 int ret;
861 size_t done;
862 size_t size;
863
864 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
865 if (!kbuf)
866 return -ENOMEM;
867
868 ret = done = 0;
869 while (done < count) {
870 size = count - done;
871 if (size >= WRITE_BUFSIZE)
872 size = WRITE_BUFSIZE - 1;
873 if (copy_from_user(kbuf, buffer + done, size)) {
874 ret = -EFAULT;
875 goto out;
876 }
877 kbuf[size] = '\0';
878 tmp = strchr(kbuf, '\n');
879 if (tmp) {
880 *tmp = '\0';
881 size = tmp - kbuf + 1;
882 } else if (done + size < count) {
883 pr_warning("Line length is too long: "
884 "Should be less than %d.", WRITE_BUFSIZE);
885 ret = -EINVAL;
886 goto out;
887 }
888 done += size;
889 /* Remove comments */
890 tmp = strchr(kbuf, '#');
891 if (tmp)
892 *tmp = '\0';
893
894 ret = command_trace_probe(kbuf);
895 if (ret)
896 goto out;
897 }
898 ret = done;
899out:
900 kfree(kbuf);
901 return ret;
902}
903
904static const struct file_operations kprobe_events_ops = {
905 .owner = THIS_MODULE,
906 .open = probes_open,
907 .read = seq_read,
908 .llseek = seq_lseek,
909 .release = seq_release,
910 .write = probes_write,
911};
912
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400913/* Probes profiling interfaces */
914static int probes_profile_seq_show(struct seq_file *m, void *v)
915{
916 struct trace_probe *tp = v;
917
918 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200919 tp->rp.kp.nmissed);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400920
921 return 0;
922}
923
924static const struct seq_operations profile_seq_op = {
925 .start = probes_seq_start,
926 .next = probes_seq_next,
927 .stop = probes_seq_stop,
928 .show = probes_profile_seq_show
929};
930
931static int profile_open(struct inode *inode, struct file *file)
932{
933 return seq_open(file, &profile_seq_op);
934}
935
936static const struct file_operations kprobe_profile_ops = {
937 .owner = THIS_MODULE,
938 .open = profile_open,
939 .read = seq_read,
940 .llseek = seq_lseek,
941 .release = seq_release,
942};
943
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400944/* Kprobe handler */
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +0800945static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400946{
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200947 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400948 struct kprobe_trace_entry *entry;
949 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200950 struct ring_buffer *buffer;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400951 int size, i, pc;
952 unsigned long irq_flags;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400953 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400954
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400955 tp->nhit++;
956
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400957 local_save_flags(irq_flags);
958 pc = preempt_count();
959
960 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
961
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200962 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400963 irq_flags, pc);
964 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +0800965 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400966
967 entry = ring_buffer_event_data(event);
968 entry->nargs = tp->nr_args;
969 entry->ip = (unsigned long)kp->addr;
970 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400971 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400972
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200973 if (!filter_current_check_discard(buffer, call, entry, event))
974 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400975}
976
977/* Kretprobe handler */
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +0800978static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400979 struct pt_regs *regs)
980{
981 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
982 struct kretprobe_trace_entry *entry;
983 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200984 struct ring_buffer *buffer;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400985 int size, i, pc;
986 unsigned long irq_flags;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400987 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400988
989 local_save_flags(irq_flags);
990 pc = preempt_count();
991
992 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
993
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200994 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400995 irq_flags, pc);
996 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +0800997 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400998
999 entry = ring_buffer_event_data(event);
1000 entry->nargs = tp->nr_args;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +02001001 entry->func = (unsigned long)tp->rp.kp.addr;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001002 entry->ret_ip = (unsigned long)ri->ret_addr;
1003 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001004 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001005
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001006 if (!filter_current_check_discard(buffer, call, entry, event))
1007 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001008}
1009
1010/* Event entry printers */
1011enum print_line_t
1012print_kprobe_event(struct trace_iterator *iter, int flags)
1013{
1014 struct kprobe_trace_entry *field;
1015 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001016 struct trace_event *event;
1017 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001018 int i;
1019
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001020 field = (struct kprobe_trace_entry *)iter->ent;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001021 event = ftrace_find_event(field->ent.type);
1022 tp = container_of(event, struct trace_probe, event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001023
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001024 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1025 goto partial;
1026
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001027 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1028 goto partial;
1029
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001030 if (!trace_seq_puts(s, ")"))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001031 goto partial;
1032
1033 for (i = 0; i < field->nargs; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001034 if (!trace_seq_printf(s, " %s=%lx",
1035 tp->args[i].name, field->args[i]))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001036 goto partial;
1037
1038 if (!trace_seq_puts(s, "\n"))
1039 goto partial;
1040
1041 return TRACE_TYPE_HANDLED;
1042partial:
1043 return TRACE_TYPE_PARTIAL_LINE;
1044}
1045
1046enum print_line_t
1047print_kretprobe_event(struct trace_iterator *iter, int flags)
1048{
1049 struct kretprobe_trace_entry *field;
1050 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001051 struct trace_event *event;
1052 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001053 int i;
1054
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001055 field = (struct kretprobe_trace_entry *)iter->ent;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001056 event = ftrace_find_event(field->ent.type);
1057 tp = container_of(event, struct trace_probe, event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001058
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001059 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1060 goto partial;
1061
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001062 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1063 goto partial;
1064
1065 if (!trace_seq_puts(s, " <- "))
1066 goto partial;
1067
1068 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1069 goto partial;
1070
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001071 if (!trace_seq_puts(s, ")"))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001072 goto partial;
1073
1074 for (i = 0; i < field->nargs; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001075 if (!trace_seq_printf(s, " %s=%lx",
1076 tp->args[i].name, field->args[i]))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001077 goto partial;
1078
1079 if (!trace_seq_puts(s, "\n"))
1080 goto partial;
1081
1082 return TRACE_TYPE_HANDLED;
1083partial:
1084 return TRACE_TYPE_PARTIAL_LINE;
1085}
1086
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001087static int probe_event_enable(struct ftrace_event_call *call)
1088{
1089 struct trace_probe *tp = (struct trace_probe *)call->data;
1090
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001091 tp->flags |= TP_FLAG_TRACE;
1092 if (probe_is_return(tp))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001093 return enable_kretprobe(&tp->rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001094 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +02001095 return enable_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001096}
1097
1098static void probe_event_disable(struct ftrace_event_call *call)
1099{
1100 struct trace_probe *tp = (struct trace_probe *)call->data;
1101
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001102 tp->flags &= ~TP_FLAG_TRACE;
1103 if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
1104 if (probe_is_return(tp))
1105 disable_kretprobe(&tp->rp);
1106 else
1107 disable_kprobe(&tp->rp.kp);
1108 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001109}
1110
1111static int probe_event_raw_init(struct ftrace_event_call *event_call)
1112{
1113 INIT_LIST_HEAD(&event_call->fields);
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001114
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001115 return 0;
1116}
1117
1118#undef DEFINE_FIELD
1119#define DEFINE_FIELD(type, item, name, is_signed) \
1120 do { \
1121 ret = trace_define_field(event_call, #type, name, \
1122 offsetof(typeof(field), item), \
1123 sizeof(field.item), is_signed, \
1124 FILTER_OTHER); \
1125 if (ret) \
1126 return ret; \
1127 } while (0)
1128
1129static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1130{
1131 int ret, i;
1132 struct kprobe_trace_entry field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001133 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1134
Masami Hiramatsua703d942009-10-07 18:28:07 -04001135 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1136 DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001137 /* Set argument names as fields */
1138 for (i = 0; i < tp->nr_args; i++)
1139 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001140 return 0;
1141}
1142
1143static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1144{
1145 int ret, i;
1146 struct kretprobe_trace_entry field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001147 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1148
Masami Hiramatsua703d942009-10-07 18:28:07 -04001149 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1150 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1151 DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001152 /* Set argument names as fields */
1153 for (i = 0; i < tp->nr_args; i++)
1154 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001155 return 0;
1156}
1157
Lai Jiangshana342a0282009-12-15 15:39:49 +08001158static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
1159{
1160 int i;
1161 int pos = 0;
1162
1163 const char *fmt, *arg;
1164
1165 if (!probe_is_return(tp)) {
1166 fmt = "(%lx)";
1167 arg = "REC->" FIELD_STRING_IP;
1168 } else {
1169 fmt = "(%lx <- %lx)";
1170 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1171 }
1172
1173 /* When len=0, we just calculate the needed length */
1174#define LEN_OR_ZERO (len ? len - pos : 0)
1175
1176 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
1177
1178 for (i = 0; i < tp->nr_args; i++) {
1179 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%%lx",
1180 tp->args[i].name);
1181 }
1182
1183 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
1184
1185 for (i = 0; i < tp->nr_args; i++) {
1186 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
1187 tp->args[i].name);
1188 }
1189
1190#undef LEN_OR_ZERO
1191
1192 /* return the length of print_fmt */
1193 return pos;
1194}
1195
1196static int set_print_fmt(struct trace_probe *tp)
1197{
1198 int len;
1199 char *print_fmt;
1200
1201 /* First: called with 0 length to calculate the needed length */
1202 len = __set_print_fmt(tp, NULL, 0);
1203 print_fmt = kmalloc(len + 1, GFP_KERNEL);
1204 if (!print_fmt)
1205 return -ENOMEM;
1206
1207 /* Second: actually write the @print_fmt */
1208 __set_print_fmt(tp, print_fmt, len + 1);
1209 tp->call.print_fmt = print_fmt;
1210
1211 return 0;
1212}
1213
Li Zefan07b139c2009-12-21 14:27:35 +08001214#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001215
1216/* Kprobe profile handler */
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001217static __kprobes void kprobe_perf_func(struct kprobe *kp,
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001218 struct pt_regs *regs)
1219{
1220 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1221 struct ftrace_event_call *call = &tp->call;
1222 struct kprobe_trace_entry *entry;
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001223 int size, __size, i;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001224 unsigned long irq_flags;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001225 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001226
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001227 __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1228 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1229 size -= sizeof(u32);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001230 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001231 "profile buffer not large enough"))
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001232 return;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001233
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001234 entry = perf_trace_buf_prepare(size, call->id, &rctx, &irq_flags);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001235 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001236 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001237
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001238 entry->nargs = tp->nr_args;
1239 entry->ip = (unsigned long)kp->addr;
1240 for (i = 0; i < tp->nr_args; i++)
1241 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001242
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001243 perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, irq_flags, regs);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001244}
1245
1246/* Kretprobe profile handler */
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001247static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001248 struct pt_regs *regs)
1249{
1250 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1251 struct ftrace_event_call *call = &tp->call;
1252 struct kretprobe_trace_entry *entry;
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001253 int size, __size, i;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001254 unsigned long irq_flags;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001255 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001256
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001257 __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1258 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1259 size -= sizeof(u32);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001260 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001261 "profile buffer not large enough"))
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001262 return;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001263
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001264 entry = perf_trace_buf_prepare(size, call->id, &rctx, &irq_flags);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001265 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001266 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001267
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001268 entry->nargs = tp->nr_args;
1269 entry->func = (unsigned long)tp->rp.kp.addr;
1270 entry->ret_ip = (unsigned long)ri->ret_addr;
1271 for (i = 0; i < tp->nr_args; i++)
1272 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001273
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001274 perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1,
Frederic Weisbeckerc5306652010-03-03 07:16:16 +01001275 irq_flags, regs);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001276}
1277
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001278static int probe_perf_enable(struct ftrace_event_call *call)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001279{
1280 struct trace_probe *tp = (struct trace_probe *)call->data;
1281
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001282 tp->flags |= TP_FLAG_PROFILE;
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001283
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001284 if (probe_is_return(tp))
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001285 return enable_kretprobe(&tp->rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001286 else
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001287 return enable_kprobe(&tp->rp.kp);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001288}
1289
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001290static void probe_perf_disable(struct ftrace_event_call *call)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001291{
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001292 struct trace_probe *tp = (struct trace_probe *)call->data;
1293
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001294 tp->flags &= ~TP_FLAG_PROFILE;
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001295
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001296 if (!(tp->flags & TP_FLAG_TRACE)) {
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001297 if (probe_is_return(tp))
1298 disable_kretprobe(&tp->rp);
1299 else
1300 disable_kprobe(&tp->rp.kp);
1301 }
1302}
Li Zefan07b139c2009-12-21 14:27:35 +08001303#endif /* CONFIG_PERF_EVENTS */
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001304
1305
1306static __kprobes
1307int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1308{
1309 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1310
1311 if (tp->flags & TP_FLAG_TRACE)
1312 kprobe_trace_func(kp, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001313#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001314 if (tp->flags & TP_FLAG_PROFILE)
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001315 kprobe_perf_func(kp, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001316#endif
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001317 return 0; /* We don't tweek kernel, so just return 0 */
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001318}
1319
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001320static __kprobes
1321int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1322{
1323 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1324
1325 if (tp->flags & TP_FLAG_TRACE)
1326 kretprobe_trace_func(ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001327#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001328 if (tp->flags & TP_FLAG_PROFILE)
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001329 kretprobe_perf_func(ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001330#endif
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001331 return 0; /* We don't tweek kernel, so just return 0 */
1332}
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001333
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001334static int register_probe_event(struct trace_probe *tp)
1335{
1336 struct ftrace_event_call *call = &tp->call;
1337 int ret;
1338
1339 /* Initialize ftrace_event_call */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001340 if (probe_is_return(tp)) {
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001341 tp->event.trace = print_kretprobe_event;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001342 call->raw_init = probe_event_raw_init;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001343 call->define_fields = kretprobe_event_define_fields;
1344 } else {
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001345 tp->event.trace = print_kprobe_event;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001346 call->raw_init = probe_event_raw_init;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001347 call->define_fields = kprobe_event_define_fields;
1348 }
Lai Jiangshana342a0282009-12-15 15:39:49 +08001349 if (set_print_fmt(tp) < 0)
1350 return -ENOMEM;
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001351 call->event = &tp->event;
1352 call->id = register_ftrace_event(&tp->event);
Lai Jiangshana342a0282009-12-15 15:39:49 +08001353 if (!call->id) {
1354 kfree(call->print_fmt);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001355 return -ENODEV;
Lai Jiangshana342a0282009-12-15 15:39:49 +08001356 }
Masami Hiramatsu5a0d9052009-09-14 16:49:37 -04001357 call->enabled = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001358 call->regfunc = probe_event_enable;
1359 call->unregfunc = probe_event_disable;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001360
Li Zefan07b139c2009-12-21 14:27:35 +08001361#ifdef CONFIG_PERF_EVENTS
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001362 call->perf_event_enable = probe_perf_enable;
1363 call->perf_event_disable = probe_perf_disable;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001364#endif
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001365 call->data = tp;
1366 ret = trace_add_event_call(call);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001367 if (ret) {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001368 pr_info("Failed to register kprobe event: %s\n", call->name);
Lai Jiangshana342a0282009-12-15 15:39:49 +08001369 kfree(call->print_fmt);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001370 unregister_ftrace_event(&tp->event);
1371 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001372 return ret;
1373}
1374
1375static void unregister_probe_event(struct trace_probe *tp)
1376{
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001377 /* tp->event is unregistered in trace_remove_event_call() */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001378 trace_remove_event_call(&tp->call);
Lai Jiangshana342a0282009-12-15 15:39:49 +08001379 kfree(tp->call.print_fmt);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001380}
1381
1382/* Make a debugfs interface for controling probe points */
1383static __init int init_kprobe_trace(void)
1384{
1385 struct dentry *d_tracer;
1386 struct dentry *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001387
1388 d_tracer = tracing_init_dentry();
1389 if (!d_tracer)
1390 return 0;
1391
1392 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1393 NULL, &kprobe_events_ops);
1394
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001395 /* Event list interface */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001396 if (!entry)
1397 pr_warning("Could not create debugfs "
1398 "'kprobe_events' entry\n");
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001399
1400 /* Profile interface */
1401 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1402 NULL, &kprobe_profile_ops);
1403
1404 if (!entry)
1405 pr_warning("Could not create debugfs "
1406 "'kprobe_profile' entry\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001407 return 0;
1408}
1409fs_initcall(init_kprobe_trace);
1410
1411
1412#ifdef CONFIG_FTRACE_STARTUP_TEST
1413
1414static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1415 int a4, int a5, int a6)
1416{
1417 return a1 + a2 + a3 + a4 + a5 + a6;
1418}
1419
1420static __init int kprobe_trace_self_tests_init(void)
1421{
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001422 int ret, warn = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001423 int (*target)(int, int, int, int, int, int);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001424 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001425
1426 target = kprobe_trace_selftest_target;
1427
1428 pr_info("Testing kprobe tracing: ");
1429
1430 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001431 "$stack $stack0 +0($stack)");
1432 if (WARN_ON_ONCE(ret)) {
1433 pr_warning("error on probing function entry.\n");
1434 warn++;
1435 } else {
1436 /* Enable trace point */
1437 tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
1438 if (WARN_ON_ONCE(tp == NULL)) {
1439 pr_warning("error on getting new probe.\n");
1440 warn++;
1441 } else
1442 probe_event_enable(&tp->call);
1443 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001444
1445 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
Masami Hiramatsuf397af02009-10-16 20:07:20 -04001446 "$retval");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001447 if (WARN_ON_ONCE(ret)) {
1448 pr_warning("error on probing function return.\n");
1449 warn++;
1450 } else {
1451 /* Enable trace point */
1452 tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
1453 if (WARN_ON_ONCE(tp == NULL)) {
1454 pr_warning("error on getting new probe.\n");
1455 warn++;
1456 } else
1457 probe_event_enable(&tp->call);
1458 }
1459
1460 if (warn)
1461 goto end;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001462
1463 ret = target(1, 2, 3, 4, 5, 6);
1464
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001465 ret = command_trace_probe("-:testprobe");
1466 if (WARN_ON_ONCE(ret)) {
1467 pr_warning("error on deleting a probe.\n");
1468 warn++;
1469 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001470
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001471 ret = command_trace_probe("-:testprobe2");
1472 if (WARN_ON_ONCE(ret)) {
1473 pr_warning("error on deleting a probe.\n");
1474 warn++;
1475 }
1476
1477end:
1478 cleanup_all_probes();
1479 if (warn)
1480 pr_cont("NG: Some tests are failed. Please check them.\n");
1481 else
1482 pr_cont("OK\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001483 return 0;
1484}
1485
1486late_initcall(kprobe_trace_self_tests_init);
1487
1488#endif