blob: 3313fa74ce5f9c310be81eadaad4175deb56186f [file] [log] [blame]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001/*
2 * kprobe based kernel tracer
3 *
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
41/* currently, trace_kprobe only supports X86. */
42
43struct fetch_func {
44 unsigned long (*func)(struct pt_regs *, void *);
45 void *data;
46};
47
48static __kprobes unsigned long call_fetch(struct fetch_func *f,
49 struct pt_regs *regs)
50{
51 return f->func(regs, f->data);
52}
53
54/* fetch handlers */
55static __kprobes unsigned long fetch_register(struct pt_regs *regs,
56 void *offset)
57{
58 return regs_get_register(regs, (unsigned int)((unsigned long)offset));
59}
60
61static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
62 void *num)
63{
64 return regs_get_kernel_stack_nth(regs,
65 (unsigned int)((unsigned long)num));
66}
67
68static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
69{
70 unsigned long retval;
71
72 if (probe_kernel_address(addr, retval))
73 return 0;
74 return retval;
75}
76
77static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
78{
79 return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
80}
81
82static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
83 void *dummy)
84{
85 return regs_return_value(regs);
86}
87
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040088static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
89 void *dummy)
90{
91 return kernel_stack_pointer(regs);
92}
93
94/* Memory fetching by symbol */
95struct symbol_cache {
96 char *symbol;
97 long offset;
98 unsigned long addr;
99};
100
101static unsigned long update_symbol_cache(struct symbol_cache *sc)
102{
103 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
104 if (sc->addr)
105 sc->addr += sc->offset;
106 return sc->addr;
107}
108
109static void free_symbol_cache(struct symbol_cache *sc)
110{
111 kfree(sc->symbol);
112 kfree(sc);
113}
114
115static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
116{
117 struct symbol_cache *sc;
118
119 if (!sym || strlen(sym) == 0)
120 return NULL;
121 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
122 if (!sc)
123 return NULL;
124
125 sc->symbol = kstrdup(sym, GFP_KERNEL);
126 if (!sc->symbol) {
127 kfree(sc);
128 return NULL;
129 }
130 sc->offset = offset;
131
132 update_symbol_cache(sc);
133 return sc;
134}
135
136static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
137{
138 struct symbol_cache *sc = data;
139
140 if (sc->addr)
141 return fetch_memory(regs, (void *)sc->addr);
142 else
143 return 0;
144}
145
146/* Special indirect memory access interface */
147struct indirect_fetch_data {
148 struct fetch_func orig;
149 long offset;
150};
151
152static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
153{
154 struct indirect_fetch_data *ind = data;
155 unsigned long addr;
156
157 addr = call_fetch(&ind->orig, regs);
158 if (addr) {
159 addr += ind->offset;
160 return fetch_memory(regs, (void *)addr);
161 } else
162 return 0;
163}
164
165static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
166{
167 if (data->orig.func == fetch_indirect)
168 free_indirect_fetch_data(data->orig.data);
169 else if (data->orig.func == fetch_symbol)
170 free_symbol_cache(data->orig.data);
171 kfree(data);
172}
173
174/**
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400175 * Kprobe tracer core functions
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400176 */
177
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400178struct probe_arg {
179 struct fetch_func fetch;
180 const char *name;
181};
182
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400183/* Flags for trace_probe */
184#define TP_FLAG_TRACE 1
185#define TP_FLAG_PROFILE 2
186
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400187struct trace_probe {
188 struct list_head list;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200189 struct kretprobe rp; /* Use rp.kp for kprobe use */
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400190 unsigned long nhit;
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400191 unsigned int flags; /* For TP_FLAG_* */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400192 const char *symbol; /* symbol name */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400193 struct ftrace_event_call call;
Masami Hiramatsuff50d992009-08-13 16:35:34 -0400194 struct trace_event event;
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400195 unsigned int nr_args;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400196 struct probe_arg args[];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400197};
198
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400199#define SIZEOF_TRACE_PROBE(n) \
200 (offsetof(struct trace_probe, args) + \
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400201 (sizeof(struct probe_arg) * (n)))
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400202
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400203static __kprobes int probe_is_return(struct trace_probe *tp)
204{
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200205 return tp->rp.handler != NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400206}
207
208static __kprobes const char *probe_symbol(struct trace_probe *tp)
209{
210 return tp->symbol ? tp->symbol : "unknown";
211}
212
Masami Hiramatsu30a7e072009-08-21 15:43:51 -0400213static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400214{
215 int ret = -EINVAL;
216
217 if (ff->func == fetch_argument)
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400218 ret = snprintf(buf, n, "$arg%lu", (unsigned long)ff->data);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400219 else if (ff->func == fetch_register) {
220 const char *name;
221 name = regs_query_register_name((unsigned int)((long)ff->data));
222 ret = snprintf(buf, n, "%%%s", name);
223 } else if (ff->func == fetch_stack)
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400224 ret = snprintf(buf, n, "$stack%lu", (unsigned long)ff->data);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400225 else if (ff->func == fetch_memory)
226 ret = snprintf(buf, n, "@0x%p", ff->data);
227 else if (ff->func == fetch_symbol) {
228 struct symbol_cache *sc = ff->data;
229 ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
230 } else if (ff->func == fetch_retvalue)
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400231 ret = snprintf(buf, n, "$retval");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400232 else if (ff->func == fetch_stack_address)
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400233 ret = snprintf(buf, n, "$stack");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400234 else if (ff->func == fetch_indirect) {
235 struct indirect_fetch_data *id = ff->data;
236 size_t l = 0;
237 ret = snprintf(buf, n, "%+ld(", id->offset);
238 if (ret >= n)
239 goto end;
240 l += ret;
Masami Hiramatsu30a7e072009-08-21 15:43:51 -0400241 ret = probe_arg_string(buf + l, n - l, &id->orig);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400242 if (ret < 0)
243 goto end;
244 l += ret;
245 ret = snprintf(buf + l, n - l, ")");
246 ret += l;
247 }
248end:
249 if (ret >= n)
250 return -ENOSPC;
251 return ret;
252}
253
254static int register_probe_event(struct trace_probe *tp);
255static void unregister_probe_event(struct trace_probe *tp);
256
257static DEFINE_MUTEX(probe_lock);
258static LIST_HEAD(probe_list);
259
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400260static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
261static int kretprobe_dispatcher(struct kretprobe_instance *ri,
262 struct pt_regs *regs);
263
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200264/*
265 * Allocate new trace_probe and initialize it (including kprobes).
266 */
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400267static struct trace_probe *alloc_trace_probe(const char *group,
268 const char *event,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200269 void *addr,
270 const char *symbol,
271 unsigned long offs,
272 int nargs, int is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400273{
274 struct trace_probe *tp;
275
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400276 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400277 if (!tp)
278 return ERR_PTR(-ENOMEM);
279
280 if (symbol) {
281 tp->symbol = kstrdup(symbol, GFP_KERNEL);
282 if (!tp->symbol)
283 goto error;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200284 tp->rp.kp.symbol_name = tp->symbol;
285 tp->rp.kp.offset = offs;
286 } else
287 tp->rp.kp.addr = addr;
288
289 if (is_return)
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400290 tp->rp.handler = kretprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200291 else
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400292 tp->rp.kp.pre_handler = kprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200293
Masami Hiramatsu42635652009-08-13 16:35:26 -0400294 if (!event)
295 goto error;
296 tp->call.name = kstrdup(event, GFP_KERNEL);
297 if (!tp->call.name)
298 goto error;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400299
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400300 if (!group)
301 goto error;
302 tp->call.system = kstrdup(group, GFP_KERNEL);
303 if (!tp->call.system)
304 goto error;
305
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400306 INIT_LIST_HEAD(&tp->list);
307 return tp;
308error:
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400309 kfree(tp->call.name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400310 kfree(tp->symbol);
311 kfree(tp);
312 return ERR_PTR(-ENOMEM);
313}
314
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400315static void free_probe_arg(struct probe_arg *arg)
316{
317 if (arg->fetch.func == fetch_symbol)
318 free_symbol_cache(arg->fetch.data);
319 else if (arg->fetch.func == fetch_indirect)
320 free_indirect_fetch_data(arg->fetch.data);
321 kfree(arg->name);
322}
323
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400324static void free_trace_probe(struct trace_probe *tp)
325{
326 int i;
327
328 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400329 free_probe_arg(&tp->args[i]);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400330
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400331 kfree(tp->call.system);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400332 kfree(tp->call.name);
333 kfree(tp->symbol);
334 kfree(tp);
335}
336
337static struct trace_probe *find_probe_event(const char *event)
338{
339 struct trace_probe *tp;
340
341 list_for_each_entry(tp, &probe_list, list)
Masami Hiramatsu42635652009-08-13 16:35:26 -0400342 if (!strcmp(tp->call.name, event))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400343 return tp;
344 return NULL;
345}
346
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400347/* Unregister a trace_probe and probe_event: call with locking probe_lock */
348static void unregister_trace_probe(struct trace_probe *tp)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400349{
350 if (probe_is_return(tp))
351 unregister_kretprobe(&tp->rp);
352 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200353 unregister_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400354 list_del(&tp->list);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400355 unregister_probe_event(tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400356}
357
358/* Register a trace_probe and probe_event */
359static int register_trace_probe(struct trace_probe *tp)
360{
361 struct trace_probe *old_tp;
362 int ret;
363
364 mutex_lock(&probe_lock);
365
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400366 /* register as an event */
367 old_tp = find_probe_event(tp->call.name);
368 if (old_tp) {
369 /* delete old event */
370 unregister_trace_probe(old_tp);
371 free_trace_probe(old_tp);
372 }
373 ret = register_probe_event(tp);
374 if (ret) {
375 pr_warning("Faild to register probe event(%d)\n", ret);
376 goto end;
377 }
378
Masami Hiramatsu5a0d9052009-09-14 16:49:37 -0400379 tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400380 if (probe_is_return(tp))
381 ret = register_kretprobe(&tp->rp);
382 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200383 ret = register_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400384
385 if (ret) {
386 pr_warning("Could not insert probe(%d)\n", ret);
387 if (ret == -EILSEQ) {
388 pr_warning("Probing address(0x%p) is not an "
389 "instruction boundary.\n",
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200390 tp->rp.kp.addr);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400391 ret = -EINVAL;
392 }
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400393 unregister_probe_event(tp);
394 } else
395 list_add_tail(&tp->list, &probe_list);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400396end:
397 mutex_unlock(&probe_lock);
398 return ret;
399}
400
401/* Split symbol and offset. */
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400402static int split_symbol_offset(char *symbol, unsigned long *offset)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400403{
404 char *tmp;
405 int ret;
406
407 if (!offset)
408 return -EINVAL;
409
410 tmp = strchr(symbol, '+');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400411 if (tmp) {
412 /* skip sign because strict_strtol doesn't accept '+' */
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400413 ret = strict_strtoul(tmp + 1, 0, offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400414 if (ret)
415 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400416 *tmp = '\0';
417 } else
418 *offset = 0;
419 return 0;
420}
421
422#define PARAM_MAX_ARGS 16
423#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
424
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400425static int parse_probe_vars(char *arg, struct fetch_func *ff, int is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400426{
427 int ret = 0;
428 unsigned long param;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400429
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400430 if (strcmp(arg, "retval") == 0) {
431 if (is_return) {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400432 ff->func = fetch_retvalue;
433 ff->data = NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400434 } else
435 ret = -EINVAL;
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400436 } else if (strncmp(arg, "stack", 5) == 0) {
437 if (arg[5] == '\0') {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400438 ff->func = fetch_stack_address;
439 ff->data = NULL;
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400440 } else if (isdigit(arg[5])) {
441 ret = strict_strtoul(arg + 5, 10, &param);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400442 if (ret || param > PARAM_MAX_STACK)
443 ret = -EINVAL;
444 else {
445 ff->func = fetch_stack;
446 ff->data = (void *)param;
447 }
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400448 } else
449 ret = -EINVAL;
450 } else if (strncmp(arg, "arg", 3) == 0 && isdigit(arg[3])) {
451 ret = strict_strtoul(arg + 3, 10, &param);
452 if (ret || param > PARAM_MAX_ARGS)
453 ret = -EINVAL;
454 else {
455 ff->func = fetch_argument;
456 ff->data = (void *)param;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400457 }
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400458 } else
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400459 ret = -EINVAL;
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400460 return ret;
461}
462
463static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
464{
465 int ret = 0;
466 unsigned long param;
467 long offset;
468 char *tmp;
469
470 switch (arg[0]) {
471 case '$':
472 ret = parse_probe_vars(arg + 1, ff, is_return);
473 break;
474 case '%': /* named register */
475 ret = regs_query_register_offset(arg + 1);
476 if (ret >= 0) {
477 ff->func = fetch_register;
478 ff->data = (void *)(unsigned long)ret;
479 ret = 0;
480 }
481 break;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400482 case '@': /* memory or symbol */
483 if (isdigit(arg[1])) {
484 ret = strict_strtoul(arg + 1, 0, &param);
485 if (ret)
486 break;
487 ff->func = fetch_memory;
488 ff->data = (void *)param;
489 } else {
490 ret = split_symbol_offset(arg + 1, &offset);
491 if (ret)
492 break;
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400493 ff->data = alloc_symbol_cache(arg + 1, offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400494 if (ff->data)
495 ff->func = fetch_symbol;
496 else
497 ret = -EINVAL;
498 }
499 break;
500 case '+': /* indirect memory */
501 case '-':
502 tmp = strchr(arg, '(');
503 if (!tmp) {
504 ret = -EINVAL;
505 break;
506 }
507 *tmp = '\0';
508 ret = strict_strtol(arg + 1, 0, &offset);
509 if (ret)
510 break;
511 if (arg[0] == '-')
512 offset = -offset;
513 arg = tmp + 1;
514 tmp = strrchr(arg, ')');
515 if (tmp) {
516 struct indirect_fetch_data *id;
517 *tmp = '\0';
518 id = kzalloc(sizeof(struct indirect_fetch_data),
519 GFP_KERNEL);
520 if (!id)
521 return -ENOMEM;
522 id->offset = offset;
Masami Hiramatsu30a7e072009-08-21 15:43:51 -0400523 ret = parse_probe_arg(arg, &id->orig, is_return);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400524 if (ret)
525 kfree(id);
526 else {
527 ff->func = fetch_indirect;
528 ff->data = (void *)id;
529 }
530 } else
531 ret = -EINVAL;
532 break;
533 default:
534 /* TODO: support custom handler */
535 ret = -EINVAL;
536 }
537 return ret;
538}
539
540static int create_trace_probe(int argc, char **argv)
541{
542 /*
543 * Argument syntax:
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400544 * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
545 * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400546 * Fetch args:
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400547 * $argN : fetch Nth of function argument. (N:0-)
548 * $retval : fetch return value
549 * $stack : fetch stack address
550 * $stackN : fetch Nth of stack (N:0-)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400551 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
552 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
553 * %REG : fetch register REG
554 * Indirect memory fetch:
555 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400556 * Alias name of args:
557 * NAME=FETCHARG : set NAME as alias of FETCHARG.
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400558 */
559 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400560 int i, ret = 0;
561 int is_return = 0;
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400562 char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400563 unsigned long offset = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400564 void *addr = NULL;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200565 char buf[MAX_EVENT_NAME_LEN];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400566
567 if (argc < 2)
568 return -EINVAL;
569
570 if (argv[0][0] == 'p')
571 is_return = 0;
572 else if (argv[0][0] == 'r')
573 is_return = 1;
574 else
575 return -EINVAL;
576
577 if (argv[0][1] == ':') {
578 event = &argv[0][2];
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400579 if (strchr(event, '/')) {
580 group = event;
581 event = strchr(group, '/') + 1;
582 event[-1] = '\0';
583 if (strlen(group) == 0) {
584 pr_info("Group name is not specifiled\n");
585 return -EINVAL;
586 }
587 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400588 if (strlen(event) == 0) {
589 pr_info("Event name is not specifiled\n");
590 return -EINVAL;
591 }
592 }
593
594 if (isdigit(argv[1][0])) {
595 if (is_return)
596 return -EINVAL;
597 /* an address specified */
598 ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
599 if (ret)
600 return ret;
601 } else {
602 /* a symbol specified */
603 symbol = argv[1];
604 /* TODO: support .init module functions */
605 ret = split_symbol_offset(symbol, &offset);
606 if (ret)
607 return ret;
608 if (offset && is_return)
609 return -EINVAL;
610 }
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400611 argc -= 2; argv += 2;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400612
613 /* setup a probe */
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400614 if (!group)
615 group = KPROBE_EVENT_SYSTEM;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400616 if (!event) {
617 /* Make a new event name */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400618 if (symbol)
619 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
620 is_return ? 'r' : 'p', symbol, offset);
621 else
622 snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
623 is_return ? 'r' : 'p', addr);
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200624 event = buf;
625 }
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400626 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
627 is_return);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400628 if (IS_ERR(tp))
629 return PTR_ERR(tp);
630
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400631 /* parse arguments */
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400632 ret = 0;
633 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400634 /* Parse argument name */
635 arg = strchr(argv[i], '=');
636 if (arg)
637 *arg++ = '\0';
638 else
639 arg = argv[i];
640 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
641
642 /* Parse fetch argument */
643 if (strlen(arg) > MAX_ARGSTR_LEN) {
644 pr_info("Argument%d(%s) is too long.\n", i, arg);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400645 ret = -ENOSPC;
646 goto error;
647 }
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400648 ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400649 if (ret)
650 goto error;
651 }
652 tp->nr_args = i;
653
654 ret = register_trace_probe(tp);
655 if (ret)
656 goto error;
657 return 0;
658
659error:
660 free_trace_probe(tp);
661 return ret;
662}
663
664static void cleanup_all_probes(void)
665{
666 struct trace_probe *tp;
667
668 mutex_lock(&probe_lock);
669 /* TODO: Use batch unregistration */
670 while (!list_empty(&probe_list)) {
671 tp = list_entry(probe_list.next, struct trace_probe, list);
672 unregister_trace_probe(tp);
673 free_trace_probe(tp);
674 }
675 mutex_unlock(&probe_lock);
676}
677
678
679/* Probes listing interfaces */
680static void *probes_seq_start(struct seq_file *m, loff_t *pos)
681{
682 mutex_lock(&probe_lock);
683 return seq_list_start(&probe_list, *pos);
684}
685
686static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
687{
688 return seq_list_next(v, &probe_list, pos);
689}
690
691static void probes_seq_stop(struct seq_file *m, void *v)
692{
693 mutex_unlock(&probe_lock);
694}
695
696static int probes_seq_show(struct seq_file *m, void *v)
697{
698 struct trace_probe *tp = v;
699 int i, ret;
700 char buf[MAX_ARGSTR_LEN + 1];
701
702 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
Masami Hiramatsu42635652009-08-13 16:35:26 -0400703 seq_printf(m, ":%s", tp->call.name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400704
705 if (tp->symbol)
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200706 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400707 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200708 seq_printf(m, " 0x%p", tp->rp.kp.addr);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400709
710 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400711 ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400712 if (ret < 0) {
713 pr_warning("Argument%d decoding error(%d).\n", i, ret);
714 return ret;
715 }
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400716 seq_printf(m, " %s=%s", tp->args[i].name, buf);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400717 }
718 seq_printf(m, "\n");
719 return 0;
720}
721
722static const struct seq_operations probes_seq_op = {
723 .start = probes_seq_start,
724 .next = probes_seq_next,
725 .stop = probes_seq_stop,
726 .show = probes_seq_show
727};
728
729static int probes_open(struct inode *inode, struct file *file)
730{
731 if ((file->f_mode & FMODE_WRITE) &&
732 (file->f_flags & O_TRUNC))
733 cleanup_all_probes();
734
735 return seq_open(file, &probes_seq_op);
736}
737
738static int command_trace_probe(const char *buf)
739{
740 char **argv;
741 int argc = 0, ret = 0;
742
743 argv = argv_split(GFP_KERNEL, buf, &argc);
744 if (!argv)
745 return -ENOMEM;
746
747 if (argc)
748 ret = create_trace_probe(argc, argv);
749
750 argv_free(argv);
751 return ret;
752}
753
754#define WRITE_BUFSIZE 128
755
756static ssize_t probes_write(struct file *file, const char __user *buffer,
757 size_t count, loff_t *ppos)
758{
759 char *kbuf, *tmp;
760 int ret;
761 size_t done;
762 size_t size;
763
764 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
765 if (!kbuf)
766 return -ENOMEM;
767
768 ret = done = 0;
769 while (done < count) {
770 size = count - done;
771 if (size >= WRITE_BUFSIZE)
772 size = WRITE_BUFSIZE - 1;
773 if (copy_from_user(kbuf, buffer + done, size)) {
774 ret = -EFAULT;
775 goto out;
776 }
777 kbuf[size] = '\0';
778 tmp = strchr(kbuf, '\n');
779 if (tmp) {
780 *tmp = '\0';
781 size = tmp - kbuf + 1;
782 } else if (done + size < count) {
783 pr_warning("Line length is too long: "
784 "Should be less than %d.", WRITE_BUFSIZE);
785 ret = -EINVAL;
786 goto out;
787 }
788 done += size;
789 /* Remove comments */
790 tmp = strchr(kbuf, '#');
791 if (tmp)
792 *tmp = '\0';
793
794 ret = command_trace_probe(kbuf);
795 if (ret)
796 goto out;
797 }
798 ret = done;
799out:
800 kfree(kbuf);
801 return ret;
802}
803
804static const struct file_operations kprobe_events_ops = {
805 .owner = THIS_MODULE,
806 .open = probes_open,
807 .read = seq_read,
808 .llseek = seq_lseek,
809 .release = seq_release,
810 .write = probes_write,
811};
812
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400813/* Probes profiling interfaces */
814static int probes_profile_seq_show(struct seq_file *m, void *v)
815{
816 struct trace_probe *tp = v;
817
818 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200819 tp->rp.kp.nmissed);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400820
821 return 0;
822}
823
824static const struct seq_operations profile_seq_op = {
825 .start = probes_seq_start,
826 .next = probes_seq_next,
827 .stop = probes_seq_stop,
828 .show = probes_profile_seq_show
829};
830
831static int profile_open(struct inode *inode, struct file *file)
832{
833 return seq_open(file, &profile_seq_op);
834}
835
836static const struct file_operations kprobe_profile_ops = {
837 .owner = THIS_MODULE,
838 .open = profile_open,
839 .read = seq_read,
840 .llseek = seq_lseek,
841 .release = seq_release,
842};
843
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400844/* Kprobe handler */
845static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
846{
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200847 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400848 struct kprobe_trace_entry *entry;
849 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200850 struct ring_buffer *buffer;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400851 int size, i, pc;
852 unsigned long irq_flags;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400853 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400854
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400855 tp->nhit++;
856
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400857 local_save_flags(irq_flags);
858 pc = preempt_count();
859
860 size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
861
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200862 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400863 irq_flags, pc);
864 if (!event)
865 return 0;
866
867 entry = ring_buffer_event_data(event);
868 entry->nargs = tp->nr_args;
869 entry->ip = (unsigned long)kp->addr;
870 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400871 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400872
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200873 if (!filter_current_check_discard(buffer, call, entry, event))
874 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400875 return 0;
876}
877
878/* Kretprobe handler */
879static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
880 struct pt_regs *regs)
881{
882 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
883 struct kretprobe_trace_entry *entry;
884 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200885 struct ring_buffer *buffer;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400886 int size, i, pc;
887 unsigned long irq_flags;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400888 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400889
890 local_save_flags(irq_flags);
891 pc = preempt_count();
892
893 size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
894
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200895 event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400896 irq_flags, pc);
897 if (!event)
898 return 0;
899
900 entry = ring_buffer_event_data(event);
901 entry->nargs = tp->nr_args;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200902 entry->func = (unsigned long)tp->rp.kp.addr;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400903 entry->ret_ip = (unsigned long)ri->ret_addr;
904 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400905 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400906
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +0200907 if (!filter_current_check_discard(buffer, call, entry, event))
908 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400909
910 return 0;
911}
912
913/* Event entry printers */
914enum print_line_t
915print_kprobe_event(struct trace_iterator *iter, int flags)
916{
917 struct kprobe_trace_entry *field;
918 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400919 struct trace_event *event;
920 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400921 int i;
922
Masami Hiramatsuff50d992009-08-13 16:35:34 -0400923 field = (struct kprobe_trace_entry *)iter->ent;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400924 event = ftrace_find_event(field->ent.type);
925 tp = container_of(event, struct trace_probe, event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400926
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -0400927 if (!trace_seq_printf(s, "%s: (", tp->call.name))
928 goto partial;
929
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400930 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
931 goto partial;
932
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -0400933 if (!trace_seq_puts(s, ")"))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400934 goto partial;
935
936 for (i = 0; i < field->nargs; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400937 if (!trace_seq_printf(s, " %s=%lx",
938 tp->args[i].name, field->args[i]))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400939 goto partial;
940
941 if (!trace_seq_puts(s, "\n"))
942 goto partial;
943
944 return TRACE_TYPE_HANDLED;
945partial:
946 return TRACE_TYPE_PARTIAL_LINE;
947}
948
949enum print_line_t
950print_kretprobe_event(struct trace_iterator *iter, int flags)
951{
952 struct kretprobe_trace_entry *field;
953 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400954 struct trace_event *event;
955 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400956 int i;
957
Masami Hiramatsuff50d992009-08-13 16:35:34 -0400958 field = (struct kretprobe_trace_entry *)iter->ent;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400959 event = ftrace_find_event(field->ent.type);
960 tp = container_of(event, struct trace_probe, event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400961
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -0400962 if (!trace_seq_printf(s, "%s: (", tp->call.name))
963 goto partial;
964
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400965 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
966 goto partial;
967
968 if (!trace_seq_puts(s, " <- "))
969 goto partial;
970
971 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
972 goto partial;
973
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -0400974 if (!trace_seq_puts(s, ")"))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400975 goto partial;
976
977 for (i = 0; i < field->nargs; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400978 if (!trace_seq_printf(s, " %s=%lx",
979 tp->args[i].name, field->args[i]))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400980 goto partial;
981
982 if (!trace_seq_puts(s, "\n"))
983 goto partial;
984
985 return TRACE_TYPE_HANDLED;
986partial:
987 return TRACE_TYPE_PARTIAL_LINE;
988}
989
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400990static int probe_event_enable(struct ftrace_event_call *call)
991{
992 struct trace_probe *tp = (struct trace_probe *)call->data;
993
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400994 tp->flags |= TP_FLAG_TRACE;
995 if (probe_is_return(tp))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400996 return enable_kretprobe(&tp->rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400997 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200998 return enable_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400999}
1000
1001static void probe_event_disable(struct ftrace_event_call *call)
1002{
1003 struct trace_probe *tp = (struct trace_probe *)call->data;
1004
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001005 tp->flags &= ~TP_FLAG_TRACE;
1006 if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
1007 if (probe_is_return(tp))
1008 disable_kretprobe(&tp->rp);
1009 else
1010 disable_kprobe(&tp->rp.kp);
1011 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001012}
1013
1014static int probe_event_raw_init(struct ftrace_event_call *event_call)
1015{
1016 INIT_LIST_HEAD(&event_call->fields);
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001017
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001018 return 0;
1019}
1020
1021#undef DEFINE_FIELD
1022#define DEFINE_FIELD(type, item, name, is_signed) \
1023 do { \
1024 ret = trace_define_field(event_call, #type, name, \
1025 offsetof(typeof(field), item), \
1026 sizeof(field.item), is_signed, \
1027 FILTER_OTHER); \
1028 if (ret) \
1029 return ret; \
1030 } while (0)
1031
1032static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1033{
1034 int ret, i;
1035 struct kprobe_trace_entry field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001036 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1037
1038 ret = trace_define_common_fields(event_call);
1039 if (!ret)
1040 return ret;
1041
1042 DEFINE_FIELD(unsigned long, ip, "ip", 0);
1043 DEFINE_FIELD(int, nargs, "nargs", 1);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001044 /* Set argument names as fields */
1045 for (i = 0; i < tp->nr_args; i++)
1046 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001047 return 0;
1048}
1049
1050static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1051{
1052 int ret, i;
1053 struct kretprobe_trace_entry field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001054 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1055
1056 ret = trace_define_common_fields(event_call);
1057 if (!ret)
1058 return ret;
1059
1060 DEFINE_FIELD(unsigned long, func, "func", 0);
1061 DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
1062 DEFINE_FIELD(int, nargs, "nargs", 1);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001063 /* Set argument names as fields */
1064 for (i = 0; i < tp->nr_args; i++)
1065 DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001066 return 0;
1067}
1068
1069static int __probe_event_show_format(struct trace_seq *s,
1070 struct trace_probe *tp, const char *fmt,
1071 const char *arg)
1072{
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001073 int i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001074
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001075 /* Show format */
1076 if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
1077 return 0;
1078
1079 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001080 if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001081 return 0;
1082
1083 if (!trace_seq_printf(s, "\", %s", arg))
1084 return 0;
1085
1086 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001087 if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001088 return 0;
1089
1090 return trace_seq_puts(s, "\n");
1091}
1092
1093#undef SHOW_FIELD
1094#define SHOW_FIELD(type, item, name) \
1095 do { \
1096 ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
Masami Hiramatsu38a47492009-08-21 15:43:43 -04001097 "offset:%u;\tsize:%u;\n", name, \
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001098 (unsigned int)offsetof(typeof(field), item),\
1099 (unsigned int)sizeof(type)); \
1100 if (!ret) \
1101 return 0; \
1102 } while (0)
1103
1104static int kprobe_event_show_format(struct ftrace_event_call *call,
1105 struct trace_seq *s)
1106{
1107 struct kprobe_trace_entry field __attribute__((unused));
1108 int ret, i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001109 struct trace_probe *tp = (struct trace_probe *)call->data;
1110
1111 SHOW_FIELD(unsigned long, ip, "ip");
1112 SHOW_FIELD(int, nargs, "nargs");
1113
1114 /* Show fields */
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001115 for (i = 0; i < tp->nr_args; i++)
1116 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001117 trace_seq_puts(s, "\n");
1118
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001119 return __probe_event_show_format(s, tp, "(%lx)", "REC->ip");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001120}
1121
1122static int kretprobe_event_show_format(struct ftrace_event_call *call,
1123 struct trace_seq *s)
1124{
1125 struct kretprobe_trace_entry field __attribute__((unused));
1126 int ret, i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001127 struct trace_probe *tp = (struct trace_probe *)call->data;
1128
1129 SHOW_FIELD(unsigned long, func, "func");
1130 SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
1131 SHOW_FIELD(int, nargs, "nargs");
1132
1133 /* Show fields */
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001134 for (i = 0; i < tp->nr_args; i++)
1135 SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001136 trace_seq_puts(s, "\n");
1137
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001138 return __probe_event_show_format(s, tp, "(%lx <- %lx)",
1139 "REC->func, REC->ret_ip");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001140}
1141
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001142#ifdef CONFIG_EVENT_PROFILE
1143
1144/* Kprobe profile handler */
1145static __kprobes int kprobe_profile_func(struct kprobe *kp,
1146 struct pt_regs *regs)
1147{
1148 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1149 struct ftrace_event_call *call = &tp->call;
1150 struct kprobe_trace_entry *entry;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001151 struct trace_entry *ent;
1152 int size, __size, i, pc, __cpu;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001153 unsigned long irq_flags;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001154 char *raw_data;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001155
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001156 pc = preempt_count();
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001157 __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
1158 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1159 size -= sizeof(u32);
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001160 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1161 "profile buffer not large enough"))
1162 return 0;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001163
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001164 /*
1165 * Protect the non nmi buffer
1166 * This also protects the rcu read side
1167 */
1168 local_irq_save(irq_flags);
1169 __cpu = smp_processor_id();
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001170
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001171 if (in_nmi())
1172 raw_data = rcu_dereference(trace_profile_buf_nmi);
1173 else
1174 raw_data = rcu_dereference(trace_profile_buf);
1175
1176 if (!raw_data)
1177 goto end;
1178
1179 raw_data = per_cpu_ptr(raw_data, __cpu);
1180 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1181 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1182 entry = (struct kprobe_trace_entry *)raw_data;
1183 ent = &entry->ent;
1184
1185 tracing_generic_entry_update(ent, irq_flags, pc);
1186 ent->type = call->id;
1187 entry->nargs = tp->nr_args;
1188 entry->ip = (unsigned long)kp->addr;
1189 for (i = 0; i < tp->nr_args; i++)
1190 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1191 perf_tp_event(call->id, entry->ip, 1, entry, size);
1192end:
1193 local_irq_restore(irq_flags);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001194 return 0;
1195}
1196
1197/* Kretprobe profile handler */
1198static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
1199 struct pt_regs *regs)
1200{
1201 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1202 struct ftrace_event_call *call = &tp->call;
1203 struct kretprobe_trace_entry *entry;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001204 struct trace_entry *ent;
1205 int size, __size, i, pc, __cpu;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001206 unsigned long irq_flags;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001207 char *raw_data;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001208
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001209 pc = preempt_count();
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001210 __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
1211 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1212 size -= sizeof(u32);
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001213 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
1214 "profile buffer not large enough"))
1215 return 0;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001216
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001217 /*
1218 * Protect the non nmi buffer
1219 * This also protects the rcu read side
1220 */
1221 local_irq_save(irq_flags);
1222 __cpu = smp_processor_id();
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001223
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001224 if (in_nmi())
1225 raw_data = rcu_dereference(trace_profile_buf_nmi);
1226 else
1227 raw_data = rcu_dereference(trace_profile_buf);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001228
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001229 if (!raw_data)
1230 goto end;
1231
1232 raw_data = per_cpu_ptr(raw_data, __cpu);
1233 /* Zero dead bytes from alignment to avoid buffer leak to userspace */
1234 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
1235 entry = (struct kretprobe_trace_entry *)raw_data;
1236 ent = &entry->ent;
1237
1238 tracing_generic_entry_update(ent, irq_flags, pc);
1239 ent->type = call->id;
1240 entry->nargs = tp->nr_args;
1241 entry->func = (unsigned long)tp->rp.kp.addr;
1242 entry->ret_ip = (unsigned long)ri->ret_addr;
1243 for (i = 0; i < tp->nr_args; i++)
1244 entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
1245 perf_tp_event(call->id, entry->ret_ip, 1, entry, size);
1246end:
1247 local_irq_restore(irq_flags);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001248 return 0;
1249}
1250
1251static int probe_profile_enable(struct ftrace_event_call *call)
1252{
1253 struct trace_probe *tp = (struct trace_probe *)call->data;
1254
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001255 tp->flags |= TP_FLAG_PROFILE;
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001256
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001257 if (probe_is_return(tp))
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001258 return enable_kretprobe(&tp->rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001259 else
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001260 return enable_kprobe(&tp->rp.kp);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001261}
1262
1263static void probe_profile_disable(struct ftrace_event_call *call)
1264{
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001265 struct trace_probe *tp = (struct trace_probe *)call->data;
1266
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001267 tp->flags &= ~TP_FLAG_PROFILE;
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001268
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001269 if (!(tp->flags & TP_FLAG_TRACE)) {
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001270 if (probe_is_return(tp))
1271 disable_kretprobe(&tp->rp);
1272 else
1273 disable_kprobe(&tp->rp.kp);
1274 }
1275}
1276#endif /* CONFIG_EVENT_PROFILE */
1277
1278
1279static __kprobes
1280int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1281{
1282 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1283
1284 if (tp->flags & TP_FLAG_TRACE)
1285 kprobe_trace_func(kp, regs);
1286#ifdef CONFIG_EVENT_PROFILE
1287 if (tp->flags & TP_FLAG_PROFILE)
1288 kprobe_profile_func(kp, regs);
1289#endif /* CONFIG_EVENT_PROFILE */
1290 return 0; /* We don't tweek kernel, so just return 0 */
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001291}
1292
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001293static __kprobes
1294int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1295{
1296 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1297
1298 if (tp->flags & TP_FLAG_TRACE)
1299 kretprobe_trace_func(ri, regs);
1300#ifdef CONFIG_EVENT_PROFILE
1301 if (tp->flags & TP_FLAG_PROFILE)
1302 kretprobe_profile_func(ri, regs);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001303#endif /* CONFIG_EVENT_PROFILE */
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001304 return 0; /* We don't tweek kernel, so just return 0 */
1305}
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001306
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001307static int register_probe_event(struct trace_probe *tp)
1308{
1309 struct ftrace_event_call *call = &tp->call;
1310 int ret;
1311
1312 /* Initialize ftrace_event_call */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001313 if (probe_is_return(tp)) {
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001314 tp->event.trace = print_kretprobe_event;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001315 call->raw_init = probe_event_raw_init;
1316 call->show_format = kretprobe_event_show_format;
1317 call->define_fields = kretprobe_event_define_fields;
1318 } else {
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001319 tp->event.trace = print_kprobe_event;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001320 call->raw_init = probe_event_raw_init;
1321 call->show_format = kprobe_event_show_format;
1322 call->define_fields = kprobe_event_define_fields;
1323 }
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001324 call->event = &tp->event;
1325 call->id = register_ftrace_event(&tp->event);
1326 if (!call->id)
1327 return -ENODEV;
Masami Hiramatsu5a0d9052009-09-14 16:49:37 -04001328 call->enabled = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001329 call->regfunc = probe_event_enable;
1330 call->unregfunc = probe_event_disable;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001331
1332#ifdef CONFIG_EVENT_PROFILE
1333 atomic_set(&call->profile_count, -1);
1334 call->profile_enable = probe_profile_enable;
1335 call->profile_disable = probe_profile_disable;
1336#endif
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001337 call->data = tp;
1338 ret = trace_add_event_call(call);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001339 if (ret) {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001340 pr_info("Failed to register kprobe event: %s\n", call->name);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001341 unregister_ftrace_event(&tp->event);
1342 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001343 return ret;
1344}
1345
1346static void unregister_probe_event(struct trace_probe *tp)
1347{
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001348 /* tp->event is unregistered in trace_remove_event_call() */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001349 trace_remove_event_call(&tp->call);
1350}
1351
1352/* Make a debugfs interface for controling probe points */
1353static __init int init_kprobe_trace(void)
1354{
1355 struct dentry *d_tracer;
1356 struct dentry *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001357
1358 d_tracer = tracing_init_dentry();
1359 if (!d_tracer)
1360 return 0;
1361
1362 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1363 NULL, &kprobe_events_ops);
1364
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001365 /* Event list interface */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001366 if (!entry)
1367 pr_warning("Could not create debugfs "
1368 "'kprobe_events' entry\n");
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001369
1370 /* Profile interface */
1371 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1372 NULL, &kprobe_profile_ops);
1373
1374 if (!entry)
1375 pr_warning("Could not create debugfs "
1376 "'kprobe_profile' entry\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001377 return 0;
1378}
1379fs_initcall(init_kprobe_trace);
1380
1381
1382#ifdef CONFIG_FTRACE_STARTUP_TEST
1383
1384static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1385 int a4, int a5, int a6)
1386{
1387 return a1 + a2 + a3 + a4 + a5 + a6;
1388}
1389
1390static __init int kprobe_trace_self_tests_init(void)
1391{
1392 int ret;
1393 int (*target)(int, int, int, int, int, int);
1394
1395 target = kprobe_trace_selftest_target;
1396
1397 pr_info("Testing kprobe tracing: ");
1398
1399 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
1400 "a1 a2 a3 a4 a5 a6");
1401 if (WARN_ON_ONCE(ret))
1402 pr_warning("error enabling function entry\n");
1403
1404 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
1405 "ra rv");
1406 if (WARN_ON_ONCE(ret))
1407 pr_warning("error enabling function return\n");
1408
1409 ret = target(1, 2, 3, 4, 5, 6);
1410
1411 cleanup_all_probes();
1412
1413 pr_cont("OK\n");
1414 return 0;
1415}
1416
1417late_initcall(kprobe_trace_self_tests_init);
1418
1419#endif