blob: 934078bca3f96a7c355c00ee21871ebaaf702580 [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 */
Steven Rostedt22392912010-04-21 12:27:06 -0400205 struct ftrace_event_class class;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400206 struct ftrace_event_call call;
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
Steven Rostedt22392912010-04-21 12:27:06 -0400326 tp->call.class = &tp->class;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400327 tp->call.name = kstrdup(event, GFP_KERNEL);
328 if (!tp->call.name)
329 goto error;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400330
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500331 if (!group || !check_event_name(group)) {
332 ret = -EINVAL;
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400333 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500334 }
335
Steven Rostedt22392912010-04-21 12:27:06 -0400336 tp->class.system = kstrdup(group, GFP_KERNEL);
337 if (!tp->class.system)
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400338 goto error;
339
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400340 INIT_LIST_HEAD(&tp->list);
341 return tp;
342error:
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400343 kfree(tp->call.name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400344 kfree(tp->symbol);
345 kfree(tp);
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500346 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400347}
348
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400349static void free_probe_arg(struct probe_arg *arg)
350{
351 if (arg->fetch.func == fetch_symbol)
352 free_symbol_cache(arg->fetch.data);
353 else if (arg->fetch.func == fetch_indirect)
354 free_indirect_fetch_data(arg->fetch.data);
355 kfree(arg->name);
356}
357
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400358static void free_trace_probe(struct trace_probe *tp)
359{
360 int i;
361
362 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400363 free_probe_arg(&tp->args[i]);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400364
Steven Rostedt8f082012010-04-20 10:47:33 -0400365 kfree(tp->call.class->system);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400366 kfree(tp->call.name);
367 kfree(tp->symbol);
368 kfree(tp);
369}
370
Masami Hiramatsudd004c42009-10-27 16:42:44 -0400371static struct trace_probe *find_probe_event(const char *event,
372 const char *group)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400373{
374 struct trace_probe *tp;
375
376 list_for_each_entry(tp, &probe_list, list)
Masami Hiramatsudd004c42009-10-27 16:42:44 -0400377 if (strcmp(tp->call.name, event) == 0 &&
Steven Rostedt8f082012010-04-20 10:47:33 -0400378 strcmp(tp->call.class->system, group) == 0)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400379 return tp;
380 return NULL;
381}
382
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400383/* Unregister a trace_probe and probe_event: call with locking probe_lock */
384static void unregister_trace_probe(struct trace_probe *tp)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400385{
386 if (probe_is_return(tp))
387 unregister_kretprobe(&tp->rp);
388 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200389 unregister_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400390 list_del(&tp->list);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400391 unregister_probe_event(tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400392}
393
394/* Register a trace_probe and probe_event */
395static int register_trace_probe(struct trace_probe *tp)
396{
397 struct trace_probe *old_tp;
398 int ret;
399
400 mutex_lock(&probe_lock);
401
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400402 /* register as an event */
Steven Rostedt8f082012010-04-20 10:47:33 -0400403 old_tp = find_probe_event(tp->call.name, tp->call.class->system);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400404 if (old_tp) {
405 /* delete old event */
406 unregister_trace_probe(old_tp);
407 free_trace_probe(old_tp);
408 }
409 ret = register_probe_event(tp);
410 if (ret) {
411 pr_warning("Faild to register probe event(%d)\n", ret);
412 goto end;
413 }
414
Masami Hiramatsu5a0d9052009-09-14 16:49:37 -0400415 tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400416 if (probe_is_return(tp))
417 ret = register_kretprobe(&tp->rp);
418 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200419 ret = register_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400420
421 if (ret) {
422 pr_warning("Could not insert probe(%d)\n", ret);
423 if (ret == -EILSEQ) {
424 pr_warning("Probing address(0x%p) is not an "
425 "instruction boundary.\n",
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200426 tp->rp.kp.addr);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400427 ret = -EINVAL;
428 }
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400429 unregister_probe_event(tp);
430 } else
431 list_add_tail(&tp->list, &probe_list);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400432end:
433 mutex_unlock(&probe_lock);
434 return ret;
435}
436
437/* Split symbol and offset. */
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400438static int split_symbol_offset(char *symbol, unsigned long *offset)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400439{
440 char *tmp;
441 int ret;
442
443 if (!offset)
444 return -EINVAL;
445
446 tmp = strchr(symbol, '+');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400447 if (tmp) {
448 /* skip sign because strict_strtol doesn't accept '+' */
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400449 ret = strict_strtoul(tmp + 1, 0, offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400450 if (ret)
451 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400452 *tmp = '\0';
453 } else
454 *offset = 0;
455 return 0;
456}
457
458#define PARAM_MAX_ARGS 16
459#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
460
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400461static int parse_probe_vars(char *arg, struct fetch_func *ff, int is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400462{
463 int ret = 0;
464 unsigned long param;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400465
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400466 if (strcmp(arg, "retval") == 0) {
467 if (is_return) {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400468 ff->func = fetch_retvalue;
469 ff->data = NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400470 } else
471 ret = -EINVAL;
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400472 } else if (strncmp(arg, "stack", 5) == 0) {
473 if (arg[5] == '\0') {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400474 ff->func = fetch_stack_address;
475 ff->data = NULL;
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400476 } else if (isdigit(arg[5])) {
477 ret = strict_strtoul(arg + 5, 10, &param);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400478 if (ret || param > PARAM_MAX_STACK)
479 ret = -EINVAL;
480 else {
481 ff->func = fetch_stack;
482 ff->data = (void *)param;
483 }
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400484 } else
485 ret = -EINVAL;
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400486 } else
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400487 ret = -EINVAL;
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400488 return ret;
489}
490
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500491/* Recursive argument parser */
492static int __parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400493{
494 int ret = 0;
495 unsigned long param;
496 long offset;
497 char *tmp;
498
499 switch (arg[0]) {
500 case '$':
501 ret = parse_probe_vars(arg + 1, ff, is_return);
502 break;
503 case '%': /* named register */
504 ret = regs_query_register_offset(arg + 1);
505 if (ret >= 0) {
506 ff->func = fetch_register;
507 ff->data = (void *)(unsigned long)ret;
508 ret = 0;
509 }
510 break;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400511 case '@': /* memory or symbol */
512 if (isdigit(arg[1])) {
513 ret = strict_strtoul(arg + 1, 0, &param);
514 if (ret)
515 break;
516 ff->func = fetch_memory;
517 ff->data = (void *)param;
518 } else {
519 ret = split_symbol_offset(arg + 1, &offset);
520 if (ret)
521 break;
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400522 ff->data = alloc_symbol_cache(arg + 1, offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400523 if (ff->data)
524 ff->func = fetch_symbol;
525 else
526 ret = -EINVAL;
527 }
528 break;
529 case '+': /* indirect memory */
530 case '-':
531 tmp = strchr(arg, '(');
532 if (!tmp) {
533 ret = -EINVAL;
534 break;
535 }
536 *tmp = '\0';
537 ret = strict_strtol(arg + 1, 0, &offset);
538 if (ret)
539 break;
540 if (arg[0] == '-')
541 offset = -offset;
542 arg = tmp + 1;
543 tmp = strrchr(arg, ')');
544 if (tmp) {
545 struct indirect_fetch_data *id;
546 *tmp = '\0';
547 id = kzalloc(sizeof(struct indirect_fetch_data),
548 GFP_KERNEL);
549 if (!id)
550 return -ENOMEM;
551 id->offset = offset;
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500552 ret = __parse_probe_arg(arg, &id->orig, is_return);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400553 if (ret)
554 kfree(id);
555 else {
556 ff->func = fetch_indirect;
557 ff->data = (void *)id;
558 }
559 } else
560 ret = -EINVAL;
561 break;
562 default:
563 /* TODO: support custom handler */
564 ret = -EINVAL;
565 }
566 return ret;
567}
568
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500569/* String length checking wrapper */
570static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
571{
572 if (strlen(arg) > MAX_ARGSTR_LEN) {
573 pr_info("Argument is too long.: %s\n", arg);
574 return -ENOSPC;
575 }
576 return __parse_probe_arg(arg, ff, is_return);
577}
578
Masami Hiramatsua703d942009-10-07 18:28:07 -0400579/* Return 1 if name is reserved or already used by another argument */
580static int conflict_field_name(const char *name,
581 struct probe_arg *args, int narg)
582{
583 int i;
584 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
585 if (strcmp(reserved_field_names[i], name) == 0)
586 return 1;
587 for (i = 0; i < narg; i++)
588 if (strcmp(args[i].name, name) == 0)
589 return 1;
590 return 0;
591}
592
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400593static int create_trace_probe(int argc, char **argv)
594{
595 /*
596 * Argument syntax:
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400597 * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
598 * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400599 * Fetch args:
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400600 * $retval : fetch return value
601 * $stack : fetch stack address
602 * $stackN : fetch Nth of stack (N:0-)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400603 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
604 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
605 * %REG : fetch register REG
606 * Indirect memory fetch:
607 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400608 * Alias name of args:
609 * NAME=FETCHARG : set NAME as alias of FETCHARG.
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400610 */
611 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400612 int i, ret = 0;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500613 int is_return = 0, is_delete = 0;
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400614 char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400615 unsigned long offset = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400616 void *addr = NULL;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200617 char buf[MAX_EVENT_NAME_LEN];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400618
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500619 /* argc must be >= 1 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400620 if (argv[0][0] == 'p')
621 is_return = 0;
622 else if (argv[0][0] == 'r')
623 is_return = 1;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500624 else if (argv[0][0] == '-')
625 is_delete = 1;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400626 else {
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500627 pr_info("Probe definition must be started with 'p', 'r' or"
628 " '-'.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400629 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400630 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400631
632 if (argv[0][1] == ':') {
633 event = &argv[0][2];
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400634 if (strchr(event, '/')) {
635 group = event;
636 event = strchr(group, '/') + 1;
637 event[-1] = '\0';
638 if (strlen(group) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800639 pr_info("Group name is not specified\n");
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400640 return -EINVAL;
641 }
642 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400643 if (strlen(event) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800644 pr_info("Event name is not specified\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400645 return -EINVAL;
646 }
647 }
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500648 if (!group)
649 group = KPROBE_EVENT_SYSTEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400650
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500651 if (is_delete) {
652 if (!event) {
653 pr_info("Delete command needs an event name.\n");
654 return -EINVAL;
655 }
656 tp = find_probe_event(event, group);
657 if (!tp) {
658 pr_info("Event %s/%s doesn't exist.\n", group, event);
659 return -ENOENT;
660 }
661 /* delete an event */
662 unregister_trace_probe(tp);
663 free_trace_probe(tp);
664 return 0;
665 }
666
667 if (argc < 2) {
668 pr_info("Probe point is not specified.\n");
669 return -EINVAL;
670 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400671 if (isdigit(argv[1][0])) {
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400672 if (is_return) {
673 pr_info("Return probe point must be a symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400674 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400675 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400676 /* an address specified */
Heiko Carstensa9bb18f2010-02-10 17:23:47 +0100677 ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400678 if (ret) {
679 pr_info("Failed to parse address.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400680 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400681 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400682 } else {
683 /* a symbol specified */
684 symbol = argv[1];
685 /* TODO: support .init module functions */
686 ret = split_symbol_offset(symbol, &offset);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400687 if (ret) {
688 pr_info("Failed to parse symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400689 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400690 }
691 if (offset && is_return) {
692 pr_info("Return probe must be used without offset.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400693 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400694 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400695 }
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400696 argc -= 2; argv += 2;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400697
698 /* setup a probe */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400699 if (!event) {
700 /* Make a new event name */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400701 if (symbol)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500702 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400703 is_return ? 'r' : 'p', symbol, offset);
704 else
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500705 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400706 is_return ? 'r' : 'p', addr);
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200707 event = buf;
708 }
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400709 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
710 is_return);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400711 if (IS_ERR(tp)) {
712 pr_info("Failed to allocate trace_probe.(%d)\n",
713 (int)PTR_ERR(tp));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400714 return PTR_ERR(tp);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400715 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400716
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400717 /* parse arguments */
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400718 ret = 0;
719 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400720 /* Parse argument name */
721 arg = strchr(argv[i], '=');
722 if (arg)
723 *arg++ = '\0';
724 else
725 arg = argv[i];
Masami Hiramatsua703d942009-10-07 18:28:07 -0400726
727 if (conflict_field_name(argv[i], tp->args, i)) {
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400728 pr_info("Argument%d name '%s' conflicts with "
729 "another field.\n", i, argv[i]);
Masami Hiramatsua703d942009-10-07 18:28:07 -0400730 ret = -EINVAL;
731 goto error;
732 }
733
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400734 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500735 if (!tp->args[i].name) {
736 pr_info("Failed to allocate argument%d name '%s'.\n",
737 i, argv[i]);
738 ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400739 goto error;
740 }
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500741
742 /* Parse fetch argument */
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400743 ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400744 if (ret) {
745 pr_info("Parse error at argument%d. (%d)\n", i, ret);
Lai Jiangshanabab9d32009-11-25 16:32:21 +0800746 kfree(tp->args[i].name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400747 goto error;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400748 }
Lai Jiangshanabab9d32009-11-25 16:32:21 +0800749
750 tp->nr_args++;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400751 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400752
753 ret = register_trace_probe(tp);
754 if (ret)
755 goto error;
756 return 0;
757
758error:
759 free_trace_probe(tp);
760 return ret;
761}
762
763static void cleanup_all_probes(void)
764{
765 struct trace_probe *tp;
766
767 mutex_lock(&probe_lock);
768 /* TODO: Use batch unregistration */
769 while (!list_empty(&probe_list)) {
770 tp = list_entry(probe_list.next, struct trace_probe, list);
771 unregister_trace_probe(tp);
772 free_trace_probe(tp);
773 }
774 mutex_unlock(&probe_lock);
775}
776
777
778/* Probes listing interfaces */
779static void *probes_seq_start(struct seq_file *m, loff_t *pos)
780{
781 mutex_lock(&probe_lock);
782 return seq_list_start(&probe_list, *pos);
783}
784
785static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
786{
787 return seq_list_next(v, &probe_list, pos);
788}
789
790static void probes_seq_stop(struct seq_file *m, void *v)
791{
792 mutex_unlock(&probe_lock);
793}
794
795static int probes_seq_show(struct seq_file *m, void *v)
796{
797 struct trace_probe *tp = v;
798 int i, ret;
799 char buf[MAX_ARGSTR_LEN + 1];
800
801 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
Steven Rostedt8f082012010-04-20 10:47:33 -0400802 seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400803
Lai Jiangshan52a11f32009-11-25 16:33:15 +0800804 if (!tp->symbol)
805 seq_printf(m, " 0x%p", tp->rp.kp.addr);
806 else if (tp->rp.kp.offset)
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200807 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400808 else
Lai Jiangshan52a11f32009-11-25 16:33:15 +0800809 seq_printf(m, " %s", probe_symbol(tp));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400810
811 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400812 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400813 if (ret < 0) {
814 pr_warning("Argument%d decoding error(%d).\n", i, ret);
815 return ret;
816 }
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400817 seq_printf(m, " %s=%s", tp->args[i].name, buf);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400818 }
819 seq_printf(m, "\n");
820 return 0;
821}
822
823static const struct seq_operations probes_seq_op = {
824 .start = probes_seq_start,
825 .next = probes_seq_next,
826 .stop = probes_seq_stop,
827 .show = probes_seq_show
828};
829
830static int probes_open(struct inode *inode, struct file *file)
831{
832 if ((file->f_mode & FMODE_WRITE) &&
833 (file->f_flags & O_TRUNC))
834 cleanup_all_probes();
835
836 return seq_open(file, &probes_seq_op);
837}
838
839static int command_trace_probe(const char *buf)
840{
841 char **argv;
842 int argc = 0, ret = 0;
843
844 argv = argv_split(GFP_KERNEL, buf, &argc);
845 if (!argv)
846 return -ENOMEM;
847
848 if (argc)
849 ret = create_trace_probe(argc, argv);
850
851 argv_free(argv);
852 return ret;
853}
854
855#define WRITE_BUFSIZE 128
856
857static ssize_t probes_write(struct file *file, const char __user *buffer,
858 size_t count, loff_t *ppos)
859{
860 char *kbuf, *tmp;
861 int ret;
862 size_t done;
863 size_t size;
864
865 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
866 if (!kbuf)
867 return -ENOMEM;
868
869 ret = done = 0;
870 while (done < count) {
871 size = count - done;
872 if (size >= WRITE_BUFSIZE)
873 size = WRITE_BUFSIZE - 1;
874 if (copy_from_user(kbuf, buffer + done, size)) {
875 ret = -EFAULT;
876 goto out;
877 }
878 kbuf[size] = '\0';
879 tmp = strchr(kbuf, '\n');
880 if (tmp) {
881 *tmp = '\0';
882 size = tmp - kbuf + 1;
883 } else if (done + size < count) {
884 pr_warning("Line length is too long: "
885 "Should be less than %d.", WRITE_BUFSIZE);
886 ret = -EINVAL;
887 goto out;
888 }
889 done += size;
890 /* Remove comments */
891 tmp = strchr(kbuf, '#');
892 if (tmp)
893 *tmp = '\0';
894
895 ret = command_trace_probe(kbuf);
896 if (ret)
897 goto out;
898 }
899 ret = done;
900out:
901 kfree(kbuf);
902 return ret;
903}
904
905static const struct file_operations kprobe_events_ops = {
906 .owner = THIS_MODULE,
907 .open = probes_open,
908 .read = seq_read,
909 .llseek = seq_lseek,
910 .release = seq_release,
911 .write = probes_write,
912};
913
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400914/* Probes profiling interfaces */
915static int probes_profile_seq_show(struct seq_file *m, void *v)
916{
917 struct trace_probe *tp = v;
918
919 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200920 tp->rp.kp.nmissed);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400921
922 return 0;
923}
924
925static const struct seq_operations profile_seq_op = {
926 .start = probes_seq_start,
927 .next = probes_seq_next,
928 .stop = probes_seq_stop,
929 .show = probes_profile_seq_show
930};
931
932static int profile_open(struct inode *inode, struct file *file)
933{
934 return seq_open(file, &profile_seq_op);
935}
936
937static const struct file_operations kprobe_profile_ops = {
938 .owner = THIS_MODULE,
939 .open = profile_open,
940 .read = seq_read,
941 .llseek = seq_lseek,
942 .release = seq_release,
943};
944
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400945/* Kprobe handler */
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +0800946static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400947{
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200948 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400949 struct kprobe_trace_entry *entry;
950 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200951 struct ring_buffer *buffer;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400952 int size, i, pc;
953 unsigned long irq_flags;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400954 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400955
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400956 tp->nhit++;
957
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400958 local_save_flags(irq_flags);
959 pc = preempt_count();
960
961 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
962
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400963 event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
964 size, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400965 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +0800966 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400967
968 entry = ring_buffer_event_data(event);
969 entry->nargs = tp->nr_args;
970 entry->ip = (unsigned long)kp->addr;
971 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400972 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400973
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200974 if (!filter_current_check_discard(buffer, call, entry, event))
975 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400976}
977
978/* Kretprobe handler */
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +0800979static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400980 struct pt_regs *regs)
981{
982 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
983 struct kretprobe_trace_entry *entry;
984 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200985 struct ring_buffer *buffer;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400986 int size, i, pc;
987 unsigned long irq_flags;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400988 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400989
990 local_save_flags(irq_flags);
991 pc = preempt_count();
992
993 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
994
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400995 event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
996 size, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400997 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +0800998 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400999
1000 entry = ring_buffer_event_data(event);
1001 entry->nargs = tp->nr_args;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +02001002 entry->func = (unsigned long)tp->rp.kp.addr;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001003 entry->ret_ip = (unsigned long)ri->ret_addr;
1004 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001005 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001006
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001007 if (!filter_current_check_discard(buffer, call, entry, event))
1008 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001009}
1010
1011/* Event entry printers */
1012enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001013print_kprobe_event(struct trace_iterator *iter, int flags,
1014 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001015{
1016 struct kprobe_trace_entry *field;
1017 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001018 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001019 int i;
1020
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001021 field = (struct kprobe_trace_entry *)iter->ent;
Steven Rostedt80decc72010-04-23 10:00:22 -04001022 tp = container_of(event, struct trace_probe, call.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
Steven Rostedta9a57762010-04-22 18:46:14 -04001047print_kretprobe_event(struct trace_iterator *iter, int flags,
1048 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001049{
1050 struct kretprobe_trace_entry *field;
1051 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001052 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;
Steven Rostedt80decc72010-04-23 10:00:22 -04001056 tp = container_of(event, struct trace_probe, call.event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001057
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001058 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1059 goto partial;
1060
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001061 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1062 goto partial;
1063
1064 if (!trace_seq_puts(s, " <- "))
1065 goto partial;
1066
1067 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1068 goto partial;
1069
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001070 if (!trace_seq_puts(s, ")"))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001071 goto partial;
1072
1073 for (i = 0; i < field->nargs; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001074 if (!trace_seq_printf(s, " %s=%lx",
1075 tp->args[i].name, field->args[i]))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001076 goto partial;
1077
1078 if (!trace_seq_puts(s, "\n"))
1079 goto partial;
1080
1081 return TRACE_TYPE_HANDLED;
1082partial:
1083 return TRACE_TYPE_PARTIAL_LINE;
1084}
1085
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001086static int probe_event_enable(struct ftrace_event_call *call)
1087{
1088 struct trace_probe *tp = (struct trace_probe *)call->data;
1089
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001090 tp->flags |= TP_FLAG_TRACE;
1091 if (probe_is_return(tp))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001092 return enable_kretprobe(&tp->rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001093 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +02001094 return enable_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001095}
1096
1097static void probe_event_disable(struct ftrace_event_call *call)
1098{
1099 struct trace_probe *tp = (struct trace_probe *)call->data;
1100
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001101 tp->flags &= ~TP_FLAG_TRACE;
1102 if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
1103 if (probe_is_return(tp))
1104 disable_kretprobe(&tp->rp);
1105 else
1106 disable_kprobe(&tp->rp.kp);
1107 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001108}
1109
1110static int probe_event_raw_init(struct ftrace_event_call *event_call)
1111{
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001112 return 0;
1113}
1114
1115#undef DEFINE_FIELD
1116#define DEFINE_FIELD(type, item, name, is_signed) \
1117 do { \
1118 ret = trace_define_field(event_call, #type, name, \
1119 offsetof(typeof(field), item), \
1120 sizeof(field.item), is_signed, \
1121 FILTER_OTHER); \
1122 if (ret) \
1123 return ret; \
1124 } while (0)
1125
1126static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1127{
1128 int ret, i;
1129 struct kprobe_trace_entry field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001130 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1131
Masami Hiramatsua703d942009-10-07 18:28:07 -04001132 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1133 DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001134 /* Set argument names as fields */
1135 for (i = 0; i < tp->nr_args; i++)
1136 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001137 return 0;
1138}
1139
1140static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1141{
1142 int ret, i;
1143 struct kretprobe_trace_entry field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001144 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1145
Masami Hiramatsua703d942009-10-07 18:28:07 -04001146 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1147 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1148 DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001149 /* Set argument names as fields */
1150 for (i = 0; i < tp->nr_args; i++)
1151 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001152 return 0;
1153}
1154
Lai Jiangshana342a0282009-12-15 15:39:49 +08001155static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
1156{
1157 int i;
1158 int pos = 0;
1159
1160 const char *fmt, *arg;
1161
1162 if (!probe_is_return(tp)) {
1163 fmt = "(%lx)";
1164 arg = "REC->" FIELD_STRING_IP;
1165 } else {
1166 fmt = "(%lx <- %lx)";
1167 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1168 }
1169
1170 /* When len=0, we just calculate the needed length */
1171#define LEN_OR_ZERO (len ? len - pos : 0)
1172
1173 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
1174
1175 for (i = 0; i < tp->nr_args; i++) {
1176 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%%lx",
1177 tp->args[i].name);
1178 }
1179
1180 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
1181
1182 for (i = 0; i < tp->nr_args; i++) {
1183 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
1184 tp->args[i].name);
1185 }
1186
1187#undef LEN_OR_ZERO
1188
1189 /* return the length of print_fmt */
1190 return pos;
1191}
1192
1193static int set_print_fmt(struct trace_probe *tp)
1194{
1195 int len;
1196 char *print_fmt;
1197
1198 /* First: called with 0 length to calculate the needed length */
1199 len = __set_print_fmt(tp, NULL, 0);
1200 print_fmt = kmalloc(len + 1, GFP_KERNEL);
1201 if (!print_fmt)
1202 return -ENOMEM;
1203
1204 /* Second: actually write the @print_fmt */
1205 __set_print_fmt(tp, print_fmt, len + 1);
1206 tp->call.print_fmt = print_fmt;
1207
1208 return 0;
1209}
1210
Li Zefan07b139c2009-12-21 14:27:35 +08001211#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001212
1213/* Kprobe profile handler */
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001214static __kprobes void kprobe_perf_func(struct kprobe *kp,
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001215 struct pt_regs *regs)
1216{
1217 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1218 struct ftrace_event_call *call = &tp->call;
1219 struct kprobe_trace_entry *entry;
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001220 int size, __size, i;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001221 unsigned long irq_flags;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001222 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001223
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001224 __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1225 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1226 size -= sizeof(u32);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001227 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001228 "profile buffer not large enough"))
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001229 return;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001230
Steven Rostedt32c0eda2010-04-23 10:38:03 -04001231 entry = perf_trace_buf_prepare(size, call->event.type,
1232 &rctx, &irq_flags);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001233 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001234 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001235
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001236 entry->nargs = tp->nr_args;
1237 entry->ip = (unsigned long)kp->addr;
1238 for (i = 0; i < tp->nr_args; i++)
1239 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001240
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001241 perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, irq_flags, regs);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001242}
1243
1244/* Kretprobe profile handler */
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001245static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001246 struct pt_regs *regs)
1247{
1248 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1249 struct ftrace_event_call *call = &tp->call;
1250 struct kretprobe_trace_entry *entry;
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001251 int size, __size, i;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001252 unsigned long irq_flags;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001253 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001254
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001255 __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1256 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1257 size -= sizeof(u32);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001258 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001259 "profile buffer not large enough"))
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001260 return;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001261
Steven Rostedt32c0eda2010-04-23 10:38:03 -04001262 entry = perf_trace_buf_prepare(size, call->event.type,
1263 &rctx, &irq_flags);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001264 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001265 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001266
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001267 entry->nargs = tp->nr_args;
1268 entry->func = (unsigned long)tp->rp.kp.addr;
1269 entry->ret_ip = (unsigned long)ri->ret_addr;
1270 for (i = 0; i < tp->nr_args; i++)
1271 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001272
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001273 perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1,
Frederic Weisbeckerc5306652010-03-03 07:16:16 +01001274 irq_flags, regs);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001275}
1276
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001277static int probe_perf_enable(struct ftrace_event_call *call)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001278{
1279 struct trace_probe *tp = (struct trace_probe *)call->data;
1280
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001281 tp->flags |= TP_FLAG_PROFILE;
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001282
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001283 if (probe_is_return(tp))
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001284 return enable_kretprobe(&tp->rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001285 else
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001286 return enable_kprobe(&tp->rp.kp);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001287}
1288
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001289static void probe_perf_disable(struct ftrace_event_call *call)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001290{
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001291 struct trace_probe *tp = (struct trace_probe *)call->data;
1292
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001293 tp->flags &= ~TP_FLAG_PROFILE;
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001294
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001295 if (!(tp->flags & TP_FLAG_TRACE)) {
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001296 if (probe_is_return(tp))
1297 disable_kretprobe(&tp->rp);
1298 else
1299 disable_kprobe(&tp->rp.kp);
1300 }
1301}
Li Zefan07b139c2009-12-21 14:27:35 +08001302#endif /* CONFIG_PERF_EVENTS */
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001303
Steven Rostedt22392912010-04-21 12:27:06 -04001304static __kprobes
1305int kprobe_register(struct ftrace_event_call *event, enum trace_reg type)
1306{
1307 switch (type) {
1308 case TRACE_REG_REGISTER:
1309 return probe_event_enable(event);
1310 case TRACE_REG_UNREGISTER:
1311 probe_event_disable(event);
1312 return 0;
1313
1314#ifdef CONFIG_PERF_EVENTS
1315 case TRACE_REG_PERF_REGISTER:
1316 return probe_perf_enable(event);
1317 case TRACE_REG_PERF_UNREGISTER:
1318 probe_perf_disable(event);
1319 return 0;
1320#endif
1321 }
1322 return 0;
1323}
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001324
1325static __kprobes
1326int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1327{
1328 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1329
1330 if (tp->flags & TP_FLAG_TRACE)
1331 kprobe_trace_func(kp, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001332#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001333 if (tp->flags & TP_FLAG_PROFILE)
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001334 kprobe_perf_func(kp, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001335#endif
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001336 return 0; /* We don't tweek kernel, so just return 0 */
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001337}
1338
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001339static __kprobes
1340int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1341{
1342 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1343
1344 if (tp->flags & TP_FLAG_TRACE)
1345 kretprobe_trace_func(ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001346#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001347 if (tp->flags & TP_FLAG_PROFILE)
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001348 kretprobe_perf_func(ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001349#endif
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001350 return 0; /* We don't tweek kernel, so just return 0 */
1351}
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001352
Steven Rostedta9a57762010-04-22 18:46:14 -04001353static struct trace_event_functions kretprobe_funcs = {
1354 .trace = print_kretprobe_event
1355};
1356
1357static struct trace_event_functions kprobe_funcs = {
1358 .trace = print_kprobe_event
1359};
1360
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001361static int register_probe_event(struct trace_probe *tp)
1362{
1363 struct ftrace_event_call *call = &tp->call;
1364 int ret;
1365
1366 /* Initialize ftrace_event_call */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001367 if (probe_is_return(tp)) {
Steven Rostedt2e33af02010-04-22 10:35:55 -04001368 INIT_LIST_HEAD(&call->class->fields);
Steven Rostedt80decc72010-04-23 10:00:22 -04001369 call->event.funcs = &kretprobe_funcs;
Steven Rostedt0405ab82010-04-22 11:46:44 -04001370 call->class->raw_init = probe_event_raw_init;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001371 call->class->define_fields = kretprobe_event_define_fields;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001372 } else {
Steven Rostedt2e33af02010-04-22 10:35:55 -04001373 INIT_LIST_HEAD(&call->class->fields);
Steven Rostedt80decc72010-04-23 10:00:22 -04001374 call->event.funcs = &kprobe_funcs;
Steven Rostedt0405ab82010-04-22 11:46:44 -04001375 call->class->raw_init = probe_event_raw_init;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001376 call->class->define_fields = kprobe_event_define_fields;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001377 }
Lai Jiangshana342a0282009-12-15 15:39:49 +08001378 if (set_print_fmt(tp) < 0)
1379 return -ENOMEM;
Steven Rostedt32c0eda2010-04-23 10:38:03 -04001380 ret = register_ftrace_event(&call->event);
1381 if (!ret) {
Lai Jiangshana342a0282009-12-15 15:39:49 +08001382 kfree(call->print_fmt);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001383 return -ENODEV;
Lai Jiangshana342a0282009-12-15 15:39:49 +08001384 }
Masami Hiramatsu5a0d9052009-09-14 16:49:37 -04001385 call->enabled = 0;
Steven Rostedt22392912010-04-21 12:27:06 -04001386 call->class->reg = kprobe_register;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001387 call->data = tp;
1388 ret = trace_add_event_call(call);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001389 if (ret) {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001390 pr_info("Failed to register kprobe event: %s\n", call->name);
Lai Jiangshana342a0282009-12-15 15:39:49 +08001391 kfree(call->print_fmt);
Steven Rostedt80decc72010-04-23 10:00:22 -04001392 unregister_ftrace_event(&call->event);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001393 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001394 return ret;
1395}
1396
1397static void unregister_probe_event(struct trace_probe *tp)
1398{
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001399 /* tp->event is unregistered in trace_remove_event_call() */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001400 trace_remove_event_call(&tp->call);
Lai Jiangshana342a0282009-12-15 15:39:49 +08001401 kfree(tp->call.print_fmt);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001402}
1403
1404/* Make a debugfs interface for controling probe points */
1405static __init int init_kprobe_trace(void)
1406{
1407 struct dentry *d_tracer;
1408 struct dentry *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001409
1410 d_tracer = tracing_init_dentry();
1411 if (!d_tracer)
1412 return 0;
1413
1414 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1415 NULL, &kprobe_events_ops);
1416
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001417 /* Event list interface */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001418 if (!entry)
1419 pr_warning("Could not create debugfs "
1420 "'kprobe_events' entry\n");
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001421
1422 /* Profile interface */
1423 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1424 NULL, &kprobe_profile_ops);
1425
1426 if (!entry)
1427 pr_warning("Could not create debugfs "
1428 "'kprobe_profile' entry\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001429 return 0;
1430}
1431fs_initcall(init_kprobe_trace);
1432
1433
1434#ifdef CONFIG_FTRACE_STARTUP_TEST
1435
1436static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1437 int a4, int a5, int a6)
1438{
1439 return a1 + a2 + a3 + a4 + a5 + a6;
1440}
1441
1442static __init int kprobe_trace_self_tests_init(void)
1443{
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001444 int ret, warn = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001445 int (*target)(int, int, int, int, int, int);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001446 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001447
1448 target = kprobe_trace_selftest_target;
1449
1450 pr_info("Testing kprobe tracing: ");
1451
1452 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001453 "$stack $stack0 +0($stack)");
1454 if (WARN_ON_ONCE(ret)) {
1455 pr_warning("error on probing function entry.\n");
1456 warn++;
1457 } else {
1458 /* Enable trace point */
1459 tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
1460 if (WARN_ON_ONCE(tp == NULL)) {
1461 pr_warning("error on getting new probe.\n");
1462 warn++;
1463 } else
1464 probe_event_enable(&tp->call);
1465 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001466
1467 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
Masami Hiramatsuf397af02009-10-16 20:07:20 -04001468 "$retval");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001469 if (WARN_ON_ONCE(ret)) {
1470 pr_warning("error on probing function return.\n");
1471 warn++;
1472 } else {
1473 /* Enable trace point */
1474 tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
1475 if (WARN_ON_ONCE(tp == NULL)) {
1476 pr_warning("error on getting new probe.\n");
1477 warn++;
1478 } else
1479 probe_event_enable(&tp->call);
1480 }
1481
1482 if (warn)
1483 goto end;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001484
1485 ret = target(1, 2, 3, 4, 5, 6);
1486
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001487 ret = command_trace_probe("-:testprobe");
1488 if (WARN_ON_ONCE(ret)) {
1489 pr_warning("error on deleting a probe.\n");
1490 warn++;
1491 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001492
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001493 ret = command_trace_probe("-:testprobe2");
1494 if (WARN_ON_ONCE(ret)) {
1495 pr_warning("error on deleting a probe.\n");
1496 warn++;
1497 }
1498
1499end:
1500 cleanup_all_probes();
1501 if (warn)
1502 pr_cont("NG: Some tests are failed. Please check them.\n");
1503 else
1504 pr_cont("OK\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001505 return 0;
1506}
1507
1508late_initcall(kprobe_trace_self_tests_init);
1509
1510#endif