blob: 6ea90c0e2c96a89835216314f3b9f792161c49de [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
94static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
95{
96 return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
97}
98
99static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
100 void *dummy)
101{
102 return regs_return_value(regs);
103}
104
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400105static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
106 void *dummy)
107{
108 return kernel_stack_pointer(regs);
109}
110
111/* Memory fetching by symbol */
112struct symbol_cache {
113 char *symbol;
114 long offset;
115 unsigned long addr;
116};
117
118static unsigned long update_symbol_cache(struct symbol_cache *sc)
119{
120 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
121 if (sc->addr)
122 sc->addr += sc->offset;
123 return sc->addr;
124}
125
126static void free_symbol_cache(struct symbol_cache *sc)
127{
128 kfree(sc->symbol);
129 kfree(sc);
130}
131
132static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
133{
134 struct symbol_cache *sc;
135
136 if (!sym || strlen(sym) == 0)
137 return NULL;
138 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
139 if (!sc)
140 return NULL;
141
142 sc->symbol = kstrdup(sym, GFP_KERNEL);
143 if (!sc->symbol) {
144 kfree(sc);
145 return NULL;
146 }
147 sc->offset = offset;
148
149 update_symbol_cache(sc);
150 return sc;
151}
152
153static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
154{
155 struct symbol_cache *sc = data;
156
157 if (sc->addr)
158 return fetch_memory(regs, (void *)sc->addr);
159 else
160 return 0;
161}
162
163/* Special indirect memory access interface */
164struct indirect_fetch_data {
165 struct fetch_func orig;
166 long offset;
167};
168
169static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
170{
171 struct indirect_fetch_data *ind = data;
172 unsigned long addr;
173
174 addr = call_fetch(&ind->orig, regs);
175 if (addr) {
176 addr += ind->offset;
177 return fetch_memory(regs, (void *)addr);
178 } else
179 return 0;
180}
181
182static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
183{
184 if (data->orig.func == fetch_indirect)
185 free_indirect_fetch_data(data->orig.data);
186 else if (data->orig.func == fetch_symbol)
187 free_symbol_cache(data->orig.data);
188 kfree(data);
189}
190
191/**
Masami Hiramatsu77b44d12009-11-03 19:12:47 -0500192 * Kprobe event core functions
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400193 */
194
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400195struct probe_arg {
196 struct fetch_func fetch;
197 const char *name;
198};
199
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400200/* Flags for trace_probe */
201#define TP_FLAG_TRACE 1
202#define TP_FLAG_PROFILE 2
203
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400204struct trace_probe {
205 struct list_head list;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200206 struct kretprobe rp; /* Use rp.kp for kprobe use */
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400207 unsigned long nhit;
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400208 unsigned int flags; /* For TP_FLAG_* */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400209 const char *symbol; /* symbol name */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400210 struct ftrace_event_call call;
Masami Hiramatsuff50d992009-08-13 16:35:34 -0400211 struct trace_event event;
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400212 unsigned int nr_args;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400213 struct probe_arg args[];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400214};
215
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400216#define SIZEOF_TRACE_PROBE(n) \
217 (offsetof(struct trace_probe, args) + \
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400218 (sizeof(struct probe_arg) * (n)))
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400219
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400220static __kprobes int probe_is_return(struct trace_probe *tp)
221{
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200222 return tp->rp.handler != NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400223}
224
225static __kprobes const char *probe_symbol(struct trace_probe *tp)
226{
227 return tp->symbol ? tp->symbol : "unknown";
228}
229
Masami Hiramatsu30a7e072009-08-21 15:43:51 -0400230static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400231{
232 int ret = -EINVAL;
233
234 if (ff->func == fetch_argument)
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400235 ret = snprintf(buf, n, "$arg%lu", (unsigned long)ff->data);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400236 else if (ff->func == fetch_register) {
237 const char *name;
238 name = regs_query_register_name((unsigned int)((long)ff->data));
239 ret = snprintf(buf, n, "%%%s", name);
240 } else if (ff->func == fetch_stack)
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400241 ret = snprintf(buf, n, "$stack%lu", (unsigned long)ff->data);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400242 else if (ff->func == fetch_memory)
243 ret = snprintf(buf, n, "@0x%p", ff->data);
244 else if (ff->func == fetch_symbol) {
245 struct symbol_cache *sc = ff->data;
Lai Jiangshan52a11f32009-11-25 16:33:15 +0800246 if (sc->offset)
247 ret = snprintf(buf, n, "@%s%+ld", sc->symbol,
248 sc->offset);
249 else
250 ret = snprintf(buf, n, "@%s", sc->symbol);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400251 } else if (ff->func == fetch_retvalue)
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400252 ret = snprintf(buf, n, "$retval");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400253 else if (ff->func == fetch_stack_address)
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400254 ret = snprintf(buf, n, "$stack");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400255 else if (ff->func == fetch_indirect) {
256 struct indirect_fetch_data *id = ff->data;
257 size_t l = 0;
258 ret = snprintf(buf, n, "%+ld(", id->offset);
259 if (ret >= n)
260 goto end;
261 l += ret;
Masami Hiramatsu30a7e072009-08-21 15:43:51 -0400262 ret = probe_arg_string(buf + l, n - l, &id->orig);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400263 if (ret < 0)
264 goto end;
265 l += ret;
266 ret = snprintf(buf + l, n - l, ")");
267 ret += l;
268 }
269end:
270 if (ret >= n)
271 return -ENOSPC;
272 return ret;
273}
274
275static int register_probe_event(struct trace_probe *tp);
276static void unregister_probe_event(struct trace_probe *tp);
277
278static DEFINE_MUTEX(probe_lock);
279static LIST_HEAD(probe_list);
280
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400281static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
282static int kretprobe_dispatcher(struct kretprobe_instance *ri,
283 struct pt_regs *regs);
284
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500285/* Check the name is good for event/group */
286static int check_event_name(const char *name)
287{
288 if (!isalpha(*name) && *name != '_')
289 return 0;
290 while (*++name != '\0') {
291 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
292 return 0;
293 }
294 return 1;
295}
296
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200297/*
298 * Allocate new trace_probe and initialize it (including kprobes).
299 */
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400300static struct trace_probe *alloc_trace_probe(const char *group,
301 const char *event,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200302 void *addr,
303 const char *symbol,
304 unsigned long offs,
305 int nargs, int is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400306{
307 struct trace_probe *tp;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500308 int ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400309
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400310 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400311 if (!tp)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500312 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400313
314 if (symbol) {
315 tp->symbol = kstrdup(symbol, GFP_KERNEL);
316 if (!tp->symbol)
317 goto error;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200318 tp->rp.kp.symbol_name = tp->symbol;
319 tp->rp.kp.offset = offs;
320 } else
321 tp->rp.kp.addr = addr;
322
323 if (is_return)
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400324 tp->rp.handler = kretprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200325 else
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400326 tp->rp.kp.pre_handler = kprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200327
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500328 if (!event || !check_event_name(event)) {
329 ret = -EINVAL;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400330 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500331 }
332
Masami Hiramatsu42635652009-08-13 16:35:26 -0400333 tp->call.name = kstrdup(event, GFP_KERNEL);
334 if (!tp->call.name)
335 goto error;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400336
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500337 if (!group || !check_event_name(group)) {
338 ret = -EINVAL;
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400339 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500340 }
341
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400342 tp->call.system = kstrdup(group, GFP_KERNEL);
343 if (!tp->call.system)
344 goto error;
345
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400346 INIT_LIST_HEAD(&tp->list);
347 return tp;
348error:
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400349 kfree(tp->call.name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400350 kfree(tp->symbol);
351 kfree(tp);
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500352 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400353}
354
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400355static void free_probe_arg(struct probe_arg *arg)
356{
357 if (arg->fetch.func == fetch_symbol)
358 free_symbol_cache(arg->fetch.data);
359 else if (arg->fetch.func == fetch_indirect)
360 free_indirect_fetch_data(arg->fetch.data);
361 kfree(arg->name);
362}
363
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400364static void free_trace_probe(struct trace_probe *tp)
365{
366 int i;
367
368 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400369 free_probe_arg(&tp->args[i]);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400370
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400371 kfree(tp->call.system);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400372 kfree(tp->call.name);
373 kfree(tp->symbol);
374 kfree(tp);
375}
376
Masami Hiramatsudd004c42009-10-27 16:42:44 -0400377static struct trace_probe *find_probe_event(const char *event,
378 const char *group)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400379{
380 struct trace_probe *tp;
381
382 list_for_each_entry(tp, &probe_list, list)
Masami Hiramatsudd004c42009-10-27 16:42:44 -0400383 if (strcmp(tp->call.name, event) == 0 &&
384 strcmp(tp->call.system, group) == 0)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400385 return tp;
386 return NULL;
387}
388
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400389/* Unregister a trace_probe and probe_event: call with locking probe_lock */
390static void unregister_trace_probe(struct trace_probe *tp)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400391{
392 if (probe_is_return(tp))
393 unregister_kretprobe(&tp->rp);
394 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200395 unregister_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400396 list_del(&tp->list);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400397 unregister_probe_event(tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400398}
399
400/* Register a trace_probe and probe_event */
401static int register_trace_probe(struct trace_probe *tp)
402{
403 struct trace_probe *old_tp;
404 int ret;
405
406 mutex_lock(&probe_lock);
407
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400408 /* register as an event */
Masami Hiramatsudd004c42009-10-27 16:42:44 -0400409 old_tp = find_probe_event(tp->call.name, tp->call.system);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400410 if (old_tp) {
411 /* delete old event */
412 unregister_trace_probe(old_tp);
413 free_trace_probe(old_tp);
414 }
415 ret = register_probe_event(tp);
416 if (ret) {
417 pr_warning("Faild to register probe event(%d)\n", ret);
418 goto end;
419 }
420
Masami Hiramatsu5a0d9052009-09-14 16:49:37 -0400421 tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400422 if (probe_is_return(tp))
423 ret = register_kretprobe(&tp->rp);
424 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200425 ret = register_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400426
427 if (ret) {
428 pr_warning("Could not insert probe(%d)\n", ret);
429 if (ret == -EILSEQ) {
430 pr_warning("Probing address(0x%p) is not an "
431 "instruction boundary.\n",
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200432 tp->rp.kp.addr);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400433 ret = -EINVAL;
434 }
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400435 unregister_probe_event(tp);
436 } else
437 list_add_tail(&tp->list, &probe_list);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400438end:
439 mutex_unlock(&probe_lock);
440 return ret;
441}
442
443/* Split symbol and offset. */
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400444static int split_symbol_offset(char *symbol, unsigned long *offset)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400445{
446 char *tmp;
447 int ret;
448
449 if (!offset)
450 return -EINVAL;
451
452 tmp = strchr(symbol, '+');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400453 if (tmp) {
454 /* skip sign because strict_strtol doesn't accept '+' */
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400455 ret = strict_strtoul(tmp + 1, 0, offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400456 if (ret)
457 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400458 *tmp = '\0';
459 } else
460 *offset = 0;
461 return 0;
462}
463
464#define PARAM_MAX_ARGS 16
465#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
466
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400467static int parse_probe_vars(char *arg, struct fetch_func *ff, int is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400468{
469 int ret = 0;
470 unsigned long param;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400471
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400472 if (strcmp(arg, "retval") == 0) {
473 if (is_return) {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400474 ff->func = fetch_retvalue;
475 ff->data = NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400476 } else
477 ret = -EINVAL;
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400478 } else if (strncmp(arg, "stack", 5) == 0) {
479 if (arg[5] == '\0') {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400480 ff->func = fetch_stack_address;
481 ff->data = NULL;
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400482 } else if (isdigit(arg[5])) {
483 ret = strict_strtoul(arg + 5, 10, &param);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400484 if (ret || param > PARAM_MAX_STACK)
485 ret = -EINVAL;
486 else {
487 ff->func = fetch_stack;
488 ff->data = (void *)param;
489 }
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400490 } else
491 ret = -EINVAL;
492 } else if (strncmp(arg, "arg", 3) == 0 && isdigit(arg[3])) {
493 ret = strict_strtoul(arg + 3, 10, &param);
494 if (ret || param > PARAM_MAX_ARGS)
495 ret = -EINVAL;
496 else {
497 ff->func = fetch_argument;
498 ff->data = (void *)param;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400499 }
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400500 } else
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400501 ret = -EINVAL;
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400502 return ret;
503}
504
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500505/* Recursive argument parser */
506static int __parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400507{
508 int ret = 0;
509 unsigned long param;
510 long offset;
511 char *tmp;
512
513 switch (arg[0]) {
514 case '$':
515 ret = parse_probe_vars(arg + 1, ff, is_return);
516 break;
517 case '%': /* named register */
518 ret = regs_query_register_offset(arg + 1);
519 if (ret >= 0) {
520 ff->func = fetch_register;
521 ff->data = (void *)(unsigned long)ret;
522 ret = 0;
523 }
524 break;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400525 case '@': /* memory or symbol */
526 if (isdigit(arg[1])) {
527 ret = strict_strtoul(arg + 1, 0, &param);
528 if (ret)
529 break;
530 ff->func = fetch_memory;
531 ff->data = (void *)param;
532 } else {
533 ret = split_symbol_offset(arg + 1, &offset);
534 if (ret)
535 break;
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400536 ff->data = alloc_symbol_cache(arg + 1, offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400537 if (ff->data)
538 ff->func = fetch_symbol;
539 else
540 ret = -EINVAL;
541 }
542 break;
543 case '+': /* indirect memory */
544 case '-':
545 tmp = strchr(arg, '(');
546 if (!tmp) {
547 ret = -EINVAL;
548 break;
549 }
550 *tmp = '\0';
551 ret = strict_strtol(arg + 1, 0, &offset);
552 if (ret)
553 break;
554 if (arg[0] == '-')
555 offset = -offset;
556 arg = tmp + 1;
557 tmp = strrchr(arg, ')');
558 if (tmp) {
559 struct indirect_fetch_data *id;
560 *tmp = '\0';
561 id = kzalloc(sizeof(struct indirect_fetch_data),
562 GFP_KERNEL);
563 if (!id)
564 return -ENOMEM;
565 id->offset = offset;
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500566 ret = __parse_probe_arg(arg, &id->orig, is_return);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400567 if (ret)
568 kfree(id);
569 else {
570 ff->func = fetch_indirect;
571 ff->data = (void *)id;
572 }
573 } else
574 ret = -EINVAL;
575 break;
576 default:
577 /* TODO: support custom handler */
578 ret = -EINVAL;
579 }
580 return ret;
581}
582
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500583/* String length checking wrapper */
584static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
585{
586 if (strlen(arg) > MAX_ARGSTR_LEN) {
587 pr_info("Argument is too long.: %s\n", arg);
588 return -ENOSPC;
589 }
590 return __parse_probe_arg(arg, ff, is_return);
591}
592
Masami Hiramatsua703d942009-10-07 18:28:07 -0400593/* Return 1 if name is reserved or already used by another argument */
594static int conflict_field_name(const char *name,
595 struct probe_arg *args, int narg)
596{
597 int i;
598 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
599 if (strcmp(reserved_field_names[i], name) == 0)
600 return 1;
601 for (i = 0; i < narg; i++)
602 if (strcmp(args[i].name, name) == 0)
603 return 1;
604 return 0;
605}
606
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400607static int create_trace_probe(int argc, char **argv)
608{
609 /*
610 * Argument syntax:
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400611 * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
612 * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400613 * Fetch args:
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400614 * $argN : fetch Nth of function argument. (N:0-)
615 * $retval : fetch return value
616 * $stack : fetch stack address
617 * $stackN : fetch Nth of stack (N:0-)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400618 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
619 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
620 * %REG : fetch register REG
621 * Indirect memory fetch:
622 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400623 * Alias name of args:
624 * NAME=FETCHARG : set NAME as alias of FETCHARG.
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400625 */
626 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400627 int i, ret = 0;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500628 int is_return = 0, is_delete = 0;
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400629 char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400630 unsigned long offset = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400631 void *addr = NULL;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200632 char buf[MAX_EVENT_NAME_LEN];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400633
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500634 /* argc must be >= 1 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400635 if (argv[0][0] == 'p')
636 is_return = 0;
637 else if (argv[0][0] == 'r')
638 is_return = 1;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500639 else if (argv[0][0] == '-')
640 is_delete = 1;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400641 else {
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500642 pr_info("Probe definition must be started with 'p', 'r' or"
643 " '-'.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400644 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400645 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400646
647 if (argv[0][1] == ':') {
648 event = &argv[0][2];
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400649 if (strchr(event, '/')) {
650 group = event;
651 event = strchr(group, '/') + 1;
652 event[-1] = '\0';
653 if (strlen(group) == 0) {
654 pr_info("Group name is not specifiled\n");
655 return -EINVAL;
656 }
657 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400658 if (strlen(event) == 0) {
659 pr_info("Event name is not specifiled\n");
660 return -EINVAL;
661 }
662 }
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500663 if (!group)
664 group = KPROBE_EVENT_SYSTEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400665
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500666 if (is_delete) {
667 if (!event) {
668 pr_info("Delete command needs an event name.\n");
669 return -EINVAL;
670 }
671 tp = find_probe_event(event, group);
672 if (!tp) {
673 pr_info("Event %s/%s doesn't exist.\n", group, event);
674 return -ENOENT;
675 }
676 /* delete an event */
677 unregister_trace_probe(tp);
678 free_trace_probe(tp);
679 return 0;
680 }
681
682 if (argc < 2) {
683 pr_info("Probe point is not specified.\n");
684 return -EINVAL;
685 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400686 if (isdigit(argv[1][0])) {
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400687 if (is_return) {
688 pr_info("Return probe point must be a symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400689 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400690 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400691 /* an address specified */
692 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400693 if (ret) {
694 pr_info("Failed to parse address.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400695 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400696 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400697 } else {
698 /* a symbol specified */
699 symbol = argv[1];
700 /* TODO: support .init module functions */
701 ret = split_symbol_offset(symbol, &offset);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400702 if (ret) {
703 pr_info("Failed to parse symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400704 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400705 }
706 if (offset && is_return) {
707 pr_info("Return probe must be used without offset.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400708 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400709 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400710 }
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400711 argc -= 2; argv += 2;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400712
713 /* setup a probe */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400714 if (!event) {
715 /* Make a new event name */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400716 if (symbol)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500717 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400718 is_return ? 'r' : 'p', symbol, offset);
719 else
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500720 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400721 is_return ? 'r' : 'p', addr);
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200722 event = buf;
723 }
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400724 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
725 is_return);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400726 if (IS_ERR(tp)) {
727 pr_info("Failed to allocate trace_probe.(%d)\n",
728 (int)PTR_ERR(tp));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400729 return PTR_ERR(tp);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400730 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400731
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400732 /* parse arguments */
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400733 ret = 0;
734 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400735 /* Parse argument name */
736 arg = strchr(argv[i], '=');
737 if (arg)
738 *arg++ = '\0';
739 else
740 arg = argv[i];
Masami Hiramatsua703d942009-10-07 18:28:07 -0400741
742 if (conflict_field_name(argv[i], tp->args, i)) {
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400743 pr_info("Argument%d name '%s' conflicts with "
744 "another field.\n", i, argv[i]);
Masami Hiramatsua703d942009-10-07 18:28:07 -0400745 ret = -EINVAL;
746 goto error;
747 }
748
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400749 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500750 if (!tp->args[i].name) {
751 pr_info("Failed to allocate argument%d name '%s'.\n",
752 i, argv[i]);
753 ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400754 goto error;
755 }
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500756
757 /* Parse fetch argument */
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400758 ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400759 if (ret) {
760 pr_info("Parse error at argument%d. (%d)\n", i, ret);
Lai Jiangshanabab9d32009-11-25 16:32:21 +0800761 kfree(tp->args[i].name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400762 goto error;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400763 }
Lai Jiangshanabab9d32009-11-25 16:32:21 +0800764
765 tp->nr_args++;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400766 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400767
768 ret = register_trace_probe(tp);
769 if (ret)
770 goto error;
771 return 0;
772
773error:
774 free_trace_probe(tp);
775 return ret;
776}
777
778static void cleanup_all_probes(void)
779{
780 struct trace_probe *tp;
781
782 mutex_lock(&probe_lock);
783 /* TODO: Use batch unregistration */
784 while (!list_empty(&probe_list)) {
785 tp = list_entry(probe_list.next, struct trace_probe, list);
786 unregister_trace_probe(tp);
787 free_trace_probe(tp);
788 }
789 mutex_unlock(&probe_lock);
790}
791
792
793/* Probes listing interfaces */
794static void *probes_seq_start(struct seq_file *m, loff_t *pos)
795{
796 mutex_lock(&probe_lock);
797 return seq_list_start(&probe_list, *pos);
798}
799
800static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
801{
802 return seq_list_next(v, &probe_list, pos);
803}
804
805static void probes_seq_stop(struct seq_file *m, void *v)
806{
807 mutex_unlock(&probe_lock);
808}
809
810static int probes_seq_show(struct seq_file *m, void *v)
811{
812 struct trace_probe *tp = v;
813 int i, ret;
814 char buf[MAX_ARGSTR_LEN + 1];
815
816 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
Lai Jiangshan3d9b2e12009-11-25 16:32:47 +0800817 seq_printf(m, ":%s/%s", tp->call.system, tp->call.name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400818
Lai Jiangshan52a11f32009-11-25 16:33:15 +0800819 if (!tp->symbol)
820 seq_printf(m, " 0x%p", tp->rp.kp.addr);
821 else if (tp->rp.kp.offset)
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200822 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400823 else
Lai Jiangshan52a11f32009-11-25 16:33:15 +0800824 seq_printf(m, " %s", probe_symbol(tp));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400825
826 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400827 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400828 if (ret < 0) {
829 pr_warning("Argument%d decoding error(%d).\n", i, ret);
830 return ret;
831 }
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400832 seq_printf(m, " %s=%s", tp->args[i].name, buf);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400833 }
834 seq_printf(m, "\n");
835 return 0;
836}
837
838static const struct seq_operations probes_seq_op = {
839 .start = probes_seq_start,
840 .next = probes_seq_next,
841 .stop = probes_seq_stop,
842 .show = probes_seq_show
843};
844
845static int probes_open(struct inode *inode, struct file *file)
846{
847 if ((file->f_mode & FMODE_WRITE) &&
848 (file->f_flags & O_TRUNC))
849 cleanup_all_probes();
850
851 return seq_open(file, &probes_seq_op);
852}
853
854static int command_trace_probe(const char *buf)
855{
856 char **argv;
857 int argc = 0, ret = 0;
858
859 argv = argv_split(GFP_KERNEL, buf, &argc);
860 if (!argv)
861 return -ENOMEM;
862
863 if (argc)
864 ret = create_trace_probe(argc, argv);
865
866 argv_free(argv);
867 return ret;
868}
869
870#define WRITE_BUFSIZE 128
871
872static ssize_t probes_write(struct file *file, const char __user *buffer,
873 size_t count, loff_t *ppos)
874{
875 char *kbuf, *tmp;
876 int ret;
877 size_t done;
878 size_t size;
879
880 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
881 if (!kbuf)
882 return -ENOMEM;
883
884 ret = done = 0;
885 while (done < count) {
886 size = count - done;
887 if (size >= WRITE_BUFSIZE)
888 size = WRITE_BUFSIZE - 1;
889 if (copy_from_user(kbuf, buffer + done, size)) {
890 ret = -EFAULT;
891 goto out;
892 }
893 kbuf[size] = '\0';
894 tmp = strchr(kbuf, '\n');
895 if (tmp) {
896 *tmp = '\0';
897 size = tmp - kbuf + 1;
898 } else if (done + size < count) {
899 pr_warning("Line length is too long: "
900 "Should be less than %d.", WRITE_BUFSIZE);
901 ret = -EINVAL;
902 goto out;
903 }
904 done += size;
905 /* Remove comments */
906 tmp = strchr(kbuf, '#');
907 if (tmp)
908 *tmp = '\0';
909
910 ret = command_trace_probe(kbuf);
911 if (ret)
912 goto out;
913 }
914 ret = done;
915out:
916 kfree(kbuf);
917 return ret;
918}
919
920static const struct file_operations kprobe_events_ops = {
921 .owner = THIS_MODULE,
922 .open = probes_open,
923 .read = seq_read,
924 .llseek = seq_lseek,
925 .release = seq_release,
926 .write = probes_write,
927};
928
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400929/* Probes profiling interfaces */
930static int probes_profile_seq_show(struct seq_file *m, void *v)
931{
932 struct trace_probe *tp = v;
933
934 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200935 tp->rp.kp.nmissed);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400936
937 return 0;
938}
939
940static const struct seq_operations profile_seq_op = {
941 .start = probes_seq_start,
942 .next = probes_seq_next,
943 .stop = probes_seq_stop,
944 .show = probes_profile_seq_show
945};
946
947static int profile_open(struct inode *inode, struct file *file)
948{
949 return seq_open(file, &profile_seq_op);
950}
951
952static const struct file_operations kprobe_profile_ops = {
953 .owner = THIS_MODULE,
954 .open = profile_open,
955 .read = seq_read,
956 .llseek = seq_lseek,
957 .release = seq_release,
958};
959
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400960/* Kprobe handler */
961static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
962{
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200963 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400964 struct kprobe_trace_entry *entry;
965 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200966 struct ring_buffer *buffer;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400967 int size, i, pc;
968 unsigned long irq_flags;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400969 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400970
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400971 tp->nhit++;
972
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400973 local_save_flags(irq_flags);
974 pc = preempt_count();
975
976 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
977
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200978 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400979 irq_flags, pc);
980 if (!event)
981 return 0;
982
983 entry = ring_buffer_event_data(event);
984 entry->nargs = tp->nr_args;
985 entry->ip = (unsigned long)kp->addr;
986 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400987 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400988
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200989 if (!filter_current_check_discard(buffer, call, entry, event))
990 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400991 return 0;
992}
993
994/* Kretprobe handler */
995static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
996 struct pt_regs *regs)
997{
998 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
999 struct kretprobe_trace_entry *entry;
1000 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001001 struct ring_buffer *buffer;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001002 int size, i, pc;
1003 unsigned long irq_flags;
Masami Hiramatsu42635652009-08-13 16:35:26 -04001004 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001005
1006 local_save_flags(irq_flags);
1007 pc = preempt_count();
1008
1009 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1010
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001011 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001012 irq_flags, pc);
1013 if (!event)
1014 return 0;
1015
1016 entry = ring_buffer_event_data(event);
1017 entry->nargs = tp->nr_args;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +02001018 entry->func = (unsigned long)tp->rp.kp.addr;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001019 entry->ret_ip = (unsigned long)ri->ret_addr;
1020 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001021 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001022
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001023 if (!filter_current_check_discard(buffer, call, entry, event))
1024 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001025
1026 return 0;
1027}
1028
1029/* Event entry printers */
1030enum print_line_t
1031print_kprobe_event(struct trace_iterator *iter, int flags)
1032{
1033 struct kprobe_trace_entry *field;
1034 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001035 struct trace_event *event;
1036 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001037 int i;
1038
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001039 field = (struct kprobe_trace_entry *)iter->ent;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001040 event = ftrace_find_event(field->ent.type);
1041 tp = container_of(event, struct trace_probe, event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001042
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001043 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1044 goto partial;
1045
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001046 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1047 goto partial;
1048
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001049 if (!trace_seq_puts(s, ")"))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001050 goto partial;
1051
1052 for (i = 0; i < field->nargs; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001053 if (!trace_seq_printf(s, " %s=%lx",
1054 tp->args[i].name, field->args[i]))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001055 goto partial;
1056
1057 if (!trace_seq_puts(s, "\n"))
1058 goto partial;
1059
1060 return TRACE_TYPE_HANDLED;
1061partial:
1062 return TRACE_TYPE_PARTIAL_LINE;
1063}
1064
1065enum print_line_t
1066print_kretprobe_event(struct trace_iterator *iter, int flags)
1067{
1068 struct kretprobe_trace_entry *field;
1069 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001070 struct trace_event *event;
1071 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001072 int i;
1073
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001074 field = (struct kretprobe_trace_entry *)iter->ent;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001075 event = ftrace_find_event(field->ent.type);
1076 tp = container_of(event, struct trace_probe, event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001077
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001078 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1079 goto partial;
1080
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001081 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1082 goto partial;
1083
1084 if (!trace_seq_puts(s, " <- "))
1085 goto partial;
1086
1087 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1088 goto partial;
1089
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001090 if (!trace_seq_puts(s, ")"))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001091 goto partial;
1092
1093 for (i = 0; i < field->nargs; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001094 if (!trace_seq_printf(s, " %s=%lx",
1095 tp->args[i].name, field->args[i]))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001096 goto partial;
1097
1098 if (!trace_seq_puts(s, "\n"))
1099 goto partial;
1100
1101 return TRACE_TYPE_HANDLED;
1102partial:
1103 return TRACE_TYPE_PARTIAL_LINE;
1104}
1105
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001106static int probe_event_enable(struct ftrace_event_call *call)
1107{
1108 struct trace_probe *tp = (struct trace_probe *)call->data;
1109
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001110 tp->flags |= TP_FLAG_TRACE;
1111 if (probe_is_return(tp))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001112 return enable_kretprobe(&tp->rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001113 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +02001114 return enable_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001115}
1116
1117static void probe_event_disable(struct ftrace_event_call *call)
1118{
1119 struct trace_probe *tp = (struct trace_probe *)call->data;
1120
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001121 tp->flags &= ~TP_FLAG_TRACE;
1122 if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
1123 if (probe_is_return(tp))
1124 disable_kretprobe(&tp->rp);
1125 else
1126 disable_kprobe(&tp->rp.kp);
1127 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001128}
1129
1130static int probe_event_raw_init(struct ftrace_event_call *event_call)
1131{
1132 INIT_LIST_HEAD(&event_call->fields);
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001133
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001134 return 0;
1135}
1136
1137#undef DEFINE_FIELD
1138#define DEFINE_FIELD(type, item, name, is_signed) \
1139 do { \
1140 ret = trace_define_field(event_call, #type, name, \
1141 offsetof(typeof(field), item), \
1142 sizeof(field.item), is_signed, \
1143 FILTER_OTHER); \
1144 if (ret) \
1145 return ret; \
1146 } while (0)
1147
1148static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1149{
1150 int ret, i;
1151 struct kprobe_trace_entry field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001152 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1153
Masami Hiramatsua703d942009-10-07 18:28:07 -04001154 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1155 DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001156 /* Set argument names as fields */
1157 for (i = 0; i < tp->nr_args; i++)
1158 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001159 return 0;
1160}
1161
1162static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1163{
1164 int ret, i;
1165 struct kretprobe_trace_entry field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001166 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1167
Masami Hiramatsua703d942009-10-07 18:28:07 -04001168 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1169 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1170 DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001171 /* Set argument names as fields */
1172 for (i = 0; i < tp->nr_args; i++)
1173 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001174 return 0;
1175}
1176
1177static int __probe_event_show_format(struct trace_seq *s,
1178 struct trace_probe *tp, const char *fmt,
1179 const char *arg)
1180{
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001181 int i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001182
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001183 /* Show format */
1184 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1185 return 0;
1186
1187 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001188 if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001189 return 0;
1190
1191 if (!trace_seq_printf(s, "\", %s", arg))
1192 return 0;
1193
1194 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001195 if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001196 return 0;
1197
1198 return trace_seq_puts(s, "\n");
1199}
1200
1201#undef SHOW_FIELD
1202#define SHOW_FIELD(type, item, name) \
1203 do { \
Lai Jiangshan79b40822009-12-15 15:39:19 +08001204 ret = trace_seq_printf(s, "\tfield:" #type " %s;\t" \
1205 "offset:%u;\tsize:%u;\tsigned:%d;\n", name,\
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001206 (unsigned int)offsetof(typeof(field), item),\
Lai Jiangshan79b40822009-12-15 15:39:19 +08001207 (unsigned int)sizeof(type), \
1208 is_signed_type(type)); \
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001209 if (!ret) \
1210 return 0; \
1211 } while (0)
1212
1213static int kprobe_event_show_format(struct ftrace_event_call *call,
1214 struct trace_seq *s)
1215{
1216 struct kprobe_trace_entry field __attribute__((unused));
1217 int ret, i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001218 struct trace_probe *tp = (struct trace_probe *)call->data;
1219
Masami Hiramatsua703d942009-10-07 18:28:07 -04001220 SHOW_FIELD(unsigned long, ip, FIELD_STRING_IP);
1221 SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001222
1223 /* Show fields */
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001224 for (i = 0; i < tp->nr_args; i++)
1225 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001226 trace_seq_puts(s, "\n");
1227
Masami Hiramatsua703d942009-10-07 18:28:07 -04001228 return __probe_event_show_format(s, tp, "(%lx)",
1229 "REC->" FIELD_STRING_IP);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001230}
1231
1232static int kretprobe_event_show_format(struct ftrace_event_call *call,
1233 struct trace_seq *s)
1234{
1235 struct kretprobe_trace_entry field __attribute__((unused));
1236 int ret, i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001237 struct trace_probe *tp = (struct trace_probe *)call->data;
1238
Masami Hiramatsua703d942009-10-07 18:28:07 -04001239 SHOW_FIELD(unsigned long, func, FIELD_STRING_FUNC);
1240 SHOW_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP);
1241 SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001242
1243 /* Show fields */
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001244 for (i = 0; i < tp->nr_args; i++)
1245 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001246 trace_seq_puts(s, "\n");
1247
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001248 return __probe_event_show_format(s, tp, "(%lx <- %lx)",
Masami Hiramatsua703d942009-10-07 18:28:07 -04001249 "REC->" FIELD_STRING_FUNC
1250 ", REC->" FIELD_STRING_RETIP);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001251}
1252
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001253#ifdef CONFIG_EVENT_PROFILE
1254
1255/* Kprobe profile handler */
1256static __kprobes int kprobe_profile_func(struct kprobe *kp,
1257 struct pt_regs *regs)
1258{
1259 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1260 struct ftrace_event_call *call = &tp->call;
1261 struct kprobe_trace_entry *entry;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001262 struct trace_entry *ent;
1263 int size, __size, i, pc, __cpu;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001264 unsigned long irq_flags;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001265 char *trace_buf;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001266 char *raw_data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001267 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001268
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001269 pc = preempt_count();
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001270 __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1271 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1272 size -= sizeof(u32);
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001273 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1274 "profile buffer not large enough"))
1275 return 0;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001276
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001277 /*
1278 * Protect the non nmi buffer
1279 * This also protects the rcu read side
1280 */
1281 local_irq_save(irq_flags);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001282
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001283 rctx = perf_swevent_get_recursion_context();
1284 if (rctx < 0)
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001285 goto end_recursion;
1286
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001287 __cpu = smp_processor_id();
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001288
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001289 if (in_nmi())
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001290 trace_buf = rcu_dereference(perf_trace_buf_nmi);
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001291 else
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001292 trace_buf = rcu_dereference(perf_trace_buf);
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001293
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001294 if (!trace_buf)
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001295 goto end;
1296
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001297 raw_data = per_cpu_ptr(trace_buf, __cpu);
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001298
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001299 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1300 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1301 entry = (struct kprobe_trace_entry *)raw_data;
1302 ent = &entry->ent;
1303
1304 tracing_generic_entry_update(ent, irq_flags, pc);
1305 ent->type = call->id;
1306 entry->nargs = tp->nr_args;
1307 entry->ip = (unsigned long)kp->addr;
1308 for (i = 0; i < tp->nr_args; i++)
1309 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1310 perf_tp_event(call->id, entry->ip, 1, entry, size);
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001311
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001312end:
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001313 perf_swevent_put_recursion_context(rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001314end_recursion:
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001315 local_irq_restore(irq_flags);
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001316
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001317 return 0;
1318}
1319
1320/* Kretprobe profile handler */
1321static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
1322 struct pt_regs *regs)
1323{
1324 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1325 struct ftrace_event_call *call = &tp->call;
1326 struct kretprobe_trace_entry *entry;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001327 struct trace_entry *ent;
1328 int size, __size, i, pc, __cpu;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001329 unsigned long irq_flags;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001330 char *trace_buf;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001331 char *raw_data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001332 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001333
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001334 pc = preempt_count();
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001335 __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1336 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1337 size -= sizeof(u32);
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001338 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1339 "profile buffer not large enough"))
1340 return 0;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001341
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001342 /*
1343 * Protect the non nmi buffer
1344 * This also protects the rcu read side
1345 */
1346 local_irq_save(irq_flags);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001347
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001348 rctx = perf_swevent_get_recursion_context();
1349 if (rctx < 0)
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001350 goto end_recursion;
1351
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001352 __cpu = smp_processor_id();
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001353
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001354 if (in_nmi())
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001355 trace_buf = rcu_dereference(perf_trace_buf_nmi);
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001356 else
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001357 trace_buf = rcu_dereference(perf_trace_buf);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001358
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001359 if (!trace_buf)
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001360 goto end;
1361
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001362 raw_data = per_cpu_ptr(trace_buf, __cpu);
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001363
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001364 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1365 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1366 entry = (struct kretprobe_trace_entry *)raw_data;
1367 ent = &entry->ent;
1368
1369 tracing_generic_entry_update(ent, irq_flags, pc);
1370 ent->type = call->id;
1371 entry->nargs = tp->nr_args;
1372 entry->func = (unsigned long)tp->rp.kp.addr;
1373 entry->ret_ip = (unsigned long)ri->ret_addr;
1374 for (i = 0; i < tp->nr_args; i++)
1375 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1376 perf_tp_event(call->id, entry->ret_ip, 1, entry, size);
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001377
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001378end:
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001379 perf_swevent_put_recursion_context(rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001380end_recursion:
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001381 local_irq_restore(irq_flags);
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001382
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001383 return 0;
1384}
1385
1386static int probe_profile_enable(struct ftrace_event_call *call)
1387{
1388 struct trace_probe *tp = (struct trace_probe *)call->data;
1389
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001390 tp->flags |= TP_FLAG_PROFILE;
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001391
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001392 if (probe_is_return(tp))
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001393 return enable_kretprobe(&tp->rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001394 else
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001395 return enable_kprobe(&tp->rp.kp);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001396}
1397
1398static void probe_profile_disable(struct ftrace_event_call *call)
1399{
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001400 struct trace_probe *tp = (struct trace_probe *)call->data;
1401
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001402 tp->flags &= ~TP_FLAG_PROFILE;
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001403
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001404 if (!(tp->flags & TP_FLAG_TRACE)) {
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001405 if (probe_is_return(tp))
1406 disable_kretprobe(&tp->rp);
1407 else
1408 disable_kprobe(&tp->rp.kp);
1409 }
1410}
1411#endif /* CONFIG_EVENT_PROFILE */
1412
1413
1414static __kprobes
1415int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1416{
1417 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1418
1419 if (tp->flags & TP_FLAG_TRACE)
1420 kprobe_trace_func(kp, regs);
1421#ifdef CONFIG_EVENT_PROFILE
1422 if (tp->flags & TP_FLAG_PROFILE)
1423 kprobe_profile_func(kp, regs);
1424#endif /* CONFIG_EVENT_PROFILE */
1425 return 0; /* We don't tweek kernel, so just return 0 */
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001426}
1427
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001428static __kprobes
1429int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1430{
1431 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1432
1433 if (tp->flags & TP_FLAG_TRACE)
1434 kretprobe_trace_func(ri, regs);
1435#ifdef CONFIG_EVENT_PROFILE
1436 if (tp->flags & TP_FLAG_PROFILE)
1437 kretprobe_profile_func(ri, regs);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001438#endif /* CONFIG_EVENT_PROFILE */
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001439 return 0; /* We don't tweek kernel, so just return 0 */
1440}
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001441
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001442static int register_probe_event(struct trace_probe *tp)
1443{
1444 struct ftrace_event_call *call = &tp->call;
1445 int ret;
1446
1447 /* Initialize ftrace_event_call */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001448 if (probe_is_return(tp)) {
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001449 tp->event.trace = print_kretprobe_event;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001450 call->raw_init = probe_event_raw_init;
1451 call->show_format = kretprobe_event_show_format;
1452 call->define_fields = kretprobe_event_define_fields;
1453 } else {
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001454 tp->event.trace = print_kprobe_event;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001455 call->raw_init = probe_event_raw_init;
1456 call->show_format = kprobe_event_show_format;
1457 call->define_fields = kprobe_event_define_fields;
1458 }
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001459 call->event = &tp->event;
1460 call->id = register_ftrace_event(&tp->event);
1461 if (!call->id)
1462 return -ENODEV;
Masami Hiramatsu5a0d9052009-09-14 16:49:37 -04001463 call->enabled = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001464 call->regfunc = probe_event_enable;
1465 call->unregfunc = probe_event_disable;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001466
1467#ifdef CONFIG_EVENT_PROFILE
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001468 call->profile_enable = probe_profile_enable;
1469 call->profile_disable = probe_profile_disable;
1470#endif
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001471 call->data = tp;
1472 ret = trace_add_event_call(call);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001473 if (ret) {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001474 pr_info("Failed to register kprobe event: %s\n", call->name);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001475 unregister_ftrace_event(&tp->event);
1476 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001477 return ret;
1478}
1479
1480static void unregister_probe_event(struct trace_probe *tp)
1481{
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001482 /* tp->event is unregistered in trace_remove_event_call() */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001483 trace_remove_event_call(&tp->call);
1484}
1485
1486/* Make a debugfs interface for controling probe points */
1487static __init int init_kprobe_trace(void)
1488{
1489 struct dentry *d_tracer;
1490 struct dentry *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001491
1492 d_tracer = tracing_init_dentry();
1493 if (!d_tracer)
1494 return 0;
1495
1496 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1497 NULL, &kprobe_events_ops);
1498
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001499 /* Event list interface */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001500 if (!entry)
1501 pr_warning("Could not create debugfs "
1502 "'kprobe_events' entry\n");
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001503
1504 /* Profile interface */
1505 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1506 NULL, &kprobe_profile_ops);
1507
1508 if (!entry)
1509 pr_warning("Could not create debugfs "
1510 "'kprobe_profile' entry\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001511 return 0;
1512}
1513fs_initcall(init_kprobe_trace);
1514
1515
1516#ifdef CONFIG_FTRACE_STARTUP_TEST
1517
1518static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1519 int a4, int a5, int a6)
1520{
1521 return a1 + a2 + a3 + a4 + a5 + a6;
1522}
1523
1524static __init int kprobe_trace_self_tests_init(void)
1525{
1526 int ret;
1527 int (*target)(int, int, int, int, int, int);
1528
1529 target = kprobe_trace_selftest_target;
1530
1531 pr_info("Testing kprobe tracing: ");
1532
1533 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
Masami Hiramatsuf397af02009-10-16 20:07:20 -04001534 "$arg1 $arg2 $arg3 $arg4 $stack $stack0");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001535 if (WARN_ON_ONCE(ret))
1536 pr_warning("error enabling function entry\n");
1537
1538 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
Masami Hiramatsuf397af02009-10-16 20:07:20 -04001539 "$retval");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001540 if (WARN_ON_ONCE(ret))
1541 pr_warning("error enabling function return\n");
1542
1543 ret = target(1, 2, 3, 4, 5, 6);
1544
1545 cleanup_all_probes();
1546
1547 pr_cont("OK\n");
1548 return 0;
1549}
1550
1551late_initcall(kprobe_trace_self_tests_init);
1552
1553#endif