blob: 3b831d8e201e95697eab6aa181391c8d70502f24 [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 Hiramatsu93ccae72010-04-12 13:17:08 -040032#include <linux/stringify.h>
33#include <asm/bitsperlong.h>
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040034
35#include "trace.h"
36#include "trace_output.h"
37
Masami Hiramatsua82378d2009-08-13 16:35:18 -040038#define MAX_TRACE_ARGS 128
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040039#define MAX_ARGSTR_LEN 63
Masami Hiramatsu42635652009-08-13 16:35:26 -040040#define MAX_EVENT_NAME_LEN 64
Masami Hiramatsuf52487e2009-09-10 19:53:53 -040041#define KPROBE_EVENT_SYSTEM "kprobes"
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040042
Masami Hiramatsua703d942009-10-07 18:28:07 -040043/* Reserved field names */
Masami Hiramatsue93f4d82009-10-07 18:28:14 -040044#define FIELD_STRING_IP "__probe_ip"
Masami Hiramatsue93f4d82009-10-07 18:28:14 -040045#define FIELD_STRING_RETIP "__probe_ret_ip"
46#define FIELD_STRING_FUNC "__probe_func"
Masami Hiramatsua703d942009-10-07 18:28:07 -040047
48const char *reserved_field_names[] = {
49 "common_type",
50 "common_flags",
51 "common_preempt_count",
52 "common_pid",
53 "common_tgid",
54 "common_lock_depth",
55 FIELD_STRING_IP,
Masami Hiramatsua703d942009-10-07 18:28:07 -040056 FIELD_STRING_RETIP,
57 FIELD_STRING_FUNC,
58};
59
Masami Hiramatsu93ccae72010-04-12 13:17:08 -040060/* Printing function type */
61typedef int (*print_type_func_t)(struct trace_seq *, const char *, void *);
62#define PRINT_TYPE_FUNC_NAME(type) print_type_##type
63#define PRINT_TYPE_FMT_NAME(type) print_type_format_##type
64
65/* Printing in basic type function template */
66#define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt, cast) \
67static __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \
68 const char *name, void *data)\
69{ \
70 return trace_seq_printf(s, " %s=" fmt, name, (cast)*(type *)data);\
71} \
72static const char PRINT_TYPE_FMT_NAME(type)[] = fmt;
73
74DEFINE_BASIC_PRINT_TYPE_FUNC(u8, "%x", unsigned int)
75DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "%x", unsigned int)
76DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "%lx", unsigned long)
77DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "%llx", unsigned long long)
78DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d", int)
79DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d", int)
80DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%ld", long)
81DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%lld", long long)
82
83/* Data fetch function type */
84typedef void (*fetch_func_t)(struct pt_regs *, void *, void *);
85
86struct fetch_param {
87 fetch_func_t fn;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040088 void *data;
89};
90
Masami Hiramatsu93ccae72010-04-12 13:17:08 -040091static __kprobes void call_fetch(struct fetch_param *fprm,
92 struct pt_regs *regs, void *dest)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040093{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -040094 return fprm->fn(regs, fprm->data, dest);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040095}
96
Masami Hiramatsu93ccae72010-04-12 13:17:08 -040097#define FETCH_FUNC_NAME(kind, type) fetch_##kind##_##type
98/*
99 * Define macro for basic types - we don't need to define s* types, because
100 * we have to care only about bitwidth at recording time.
101 */
102#define DEFINE_BASIC_FETCH_FUNCS(kind) \
103DEFINE_FETCH_##kind(u8) \
104DEFINE_FETCH_##kind(u16) \
105DEFINE_FETCH_##kind(u32) \
106DEFINE_FETCH_##kind(u64)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400107
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400108#define CHECK_BASIC_FETCH_FUNCS(kind, fn) \
109 ((FETCH_FUNC_NAME(kind, u8) == fn) || \
110 (FETCH_FUNC_NAME(kind, u16) == fn) || \
111 (FETCH_FUNC_NAME(kind, u32) == fn) || \
112 (FETCH_FUNC_NAME(kind, u64) == fn))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400113
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400114/* Data fetch function templates */
115#define DEFINE_FETCH_reg(type) \
116static __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \
117 void *offset, void *dest) \
118{ \
119 *(type *)dest = (type)regs_get_register(regs, \
120 (unsigned int)((unsigned long)offset)); \
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400121}
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400122DEFINE_BASIC_FETCH_FUNCS(reg)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400123
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400124#define DEFINE_FETCH_stack(type) \
125static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
126 void *offset, void *dest) \
127{ \
128 *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
129 (unsigned int)((unsigned long)offset)); \
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400130}
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400131DEFINE_BASIC_FETCH_FUNCS(stack)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400132
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400133#define DEFINE_FETCH_retval(type) \
134static __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,\
135 void *dummy, void *dest) \
136{ \
137 *(type *)dest = (type)regs_return_value(regs); \
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400138}
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400139DEFINE_BASIC_FETCH_FUNCS(retval)
140
141#define DEFINE_FETCH_memory(type) \
142static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\
143 void *addr, void *dest) \
144{ \
145 type retval; \
146 if (probe_kernel_address(addr, retval)) \
147 *(type *)dest = 0; \
148 else \
149 *(type *)dest = retval; \
150}
151DEFINE_BASIC_FETCH_FUNCS(memory)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400152
153/* Memory fetching by symbol */
154struct symbol_cache {
155 char *symbol;
156 long offset;
157 unsigned long addr;
158};
159
160static unsigned long update_symbol_cache(struct symbol_cache *sc)
161{
162 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
163 if (sc->addr)
164 sc->addr += sc->offset;
165 return sc->addr;
166}
167
168static void free_symbol_cache(struct symbol_cache *sc)
169{
170 kfree(sc->symbol);
171 kfree(sc);
172}
173
174static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
175{
176 struct symbol_cache *sc;
177
178 if (!sym || strlen(sym) == 0)
179 return NULL;
180 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
181 if (!sc)
182 return NULL;
183
184 sc->symbol = kstrdup(sym, GFP_KERNEL);
185 if (!sc->symbol) {
186 kfree(sc);
187 return NULL;
188 }
189 sc->offset = offset;
190
191 update_symbol_cache(sc);
192 return sc;
193}
194
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400195#define DEFINE_FETCH_symbol(type) \
196static __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,\
197 void *data, void *dest) \
198{ \
199 struct symbol_cache *sc = data; \
200 if (sc->addr) \
201 fetch_memory_##type(regs, (void *)sc->addr, dest); \
202 else \
203 *(type *)dest = 0; \
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400204}
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400205DEFINE_BASIC_FETCH_FUNCS(symbol)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400206
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400207/* Dereference memory access function */
208struct deref_fetch_param {
209 struct fetch_param orig;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400210 long offset;
211};
212
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400213#define DEFINE_FETCH_deref(type) \
214static __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,\
215 void *data, void *dest) \
216{ \
217 struct deref_fetch_param *dprm = data; \
218 unsigned long addr; \
219 call_fetch(&dprm->orig, regs, &addr); \
220 if (addr) { \
221 addr += dprm->offset; \
222 fetch_memory_##type(regs, (void *)addr, dest); \
223 } else \
224 *(type *)dest = 0; \
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400225}
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400226DEFINE_BASIC_FETCH_FUNCS(deref)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400227
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400228static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400229{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400230 if (CHECK_BASIC_FETCH_FUNCS(deref, data->orig.fn))
231 free_deref_fetch_param(data->orig.data);
232 else if (CHECK_BASIC_FETCH_FUNCS(symbol, data->orig.fn))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400233 free_symbol_cache(data->orig.data);
234 kfree(data);
235}
236
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400237/* Default (unsigned long) fetch type */
238#define __DEFAULT_FETCH_TYPE(t) u##t
239#define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
240#define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
241#define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
242
243#define ASSIGN_FETCH_FUNC(kind, type) \
244 .kind = FETCH_FUNC_NAME(kind, type)
245
246#define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \
247 {.name = #ptype, \
248 .size = sizeof(ftype), \
249 .is_signed = sign, \
250 .print = PRINT_TYPE_FUNC_NAME(ptype), \
251 .fmt = PRINT_TYPE_FMT_NAME(ptype), \
252ASSIGN_FETCH_FUNC(reg, ftype), \
253ASSIGN_FETCH_FUNC(stack, ftype), \
254ASSIGN_FETCH_FUNC(retval, ftype), \
255ASSIGN_FETCH_FUNC(memory, ftype), \
256ASSIGN_FETCH_FUNC(symbol, ftype), \
257ASSIGN_FETCH_FUNC(deref, ftype), \
258 }
259
260/* Fetch type information table */
261static const struct fetch_type {
262 const char *name; /* Name of type */
263 size_t size; /* Byte size of type */
264 int is_signed; /* Signed flag */
265 print_type_func_t print; /* Print functions */
266 const char *fmt; /* Fromat string */
267 /* Fetch functions */
268 fetch_func_t reg;
269 fetch_func_t stack;
270 fetch_func_t retval;
271 fetch_func_t memory;
272 fetch_func_t symbol;
273 fetch_func_t deref;
274} fetch_type_table[] = {
275 ASSIGN_FETCH_TYPE(u8, u8, 0),
276 ASSIGN_FETCH_TYPE(u16, u16, 0),
277 ASSIGN_FETCH_TYPE(u32, u32, 0),
278 ASSIGN_FETCH_TYPE(u64, u64, 0),
279 ASSIGN_FETCH_TYPE(s8, u8, 1),
280 ASSIGN_FETCH_TYPE(s16, u16, 1),
281 ASSIGN_FETCH_TYPE(s32, u32, 1),
282 ASSIGN_FETCH_TYPE(s64, u64, 1),
283};
284
285static const struct fetch_type *find_fetch_type(const char *type)
286{
287 int i;
288
289 if (!type)
290 type = DEFAULT_FETCH_TYPE_STR;
291
292 for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++)
293 if (strcmp(type, fetch_type_table[i].name) == 0)
294 return &fetch_type_table[i];
295 return NULL;
296}
297
298/* Special function : only accept unsigned long */
299static __kprobes void fetch_stack_address(struct pt_regs *regs,
300 void *dummy, void *dest)
301{
302 *(unsigned long *)dest = kernel_stack_pointer(regs);
303}
304
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400305/**
Masami Hiramatsu77b44d12009-11-03 19:12:47 -0500306 * Kprobe event core functions
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400307 */
308
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400309struct probe_arg {
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400310 struct fetch_param fetch;
311 unsigned int offset; /* Offset from argument entry */
312 const char *name; /* Name of this argument */
313 const char *comm; /* Command of this argument */
314 const struct fetch_type *type; /* Type of this argument */
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400315};
316
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400317/* Flags for trace_probe */
318#define TP_FLAG_TRACE 1
319#define TP_FLAG_PROFILE 2
320
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400321struct trace_probe {
322 struct list_head list;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200323 struct kretprobe rp; /* Use rp.kp for kprobe use */
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -0400324 unsigned long nhit;
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400325 unsigned int flags; /* For TP_FLAG_* */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400326 const char *symbol; /* symbol name */
Steven Rostedt22392912010-04-21 12:27:06 -0400327 struct ftrace_event_class class;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400328 struct ftrace_event_call call;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400329 ssize_t size; /* trace entry size */
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400330 unsigned int nr_args;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400331 struct probe_arg args[];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400332};
333
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400334#define SIZEOF_TRACE_PROBE(n) \
335 (offsetof(struct trace_probe, args) + \
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400336 (sizeof(struct probe_arg) * (n)))
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400337
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400338
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400339static __kprobes int probe_is_return(struct trace_probe *tp)
340{
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200341 return tp->rp.handler != NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400342}
343
344static __kprobes const char *probe_symbol(struct trace_probe *tp)
345{
346 return tp->symbol ? tp->symbol : "unknown";
347}
348
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400349static int register_probe_event(struct trace_probe *tp);
350static void unregister_probe_event(struct trace_probe *tp);
351
352static DEFINE_MUTEX(probe_lock);
353static LIST_HEAD(probe_list);
354
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400355static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
356static int kretprobe_dispatcher(struct kretprobe_instance *ri,
357 struct pt_regs *regs);
358
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500359/* Check the name is good for event/group */
360static int check_event_name(const char *name)
361{
362 if (!isalpha(*name) && *name != '_')
363 return 0;
364 while (*++name != '\0') {
365 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
366 return 0;
367 }
368 return 1;
369}
370
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200371/*
372 * Allocate new trace_probe and initialize it (including kprobes).
373 */
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400374static struct trace_probe *alloc_trace_probe(const char *group,
375 const char *event,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200376 void *addr,
377 const char *symbol,
378 unsigned long offs,
379 int nargs, int is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400380{
381 struct trace_probe *tp;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500382 int ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400383
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400384 tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400385 if (!tp)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500386 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400387
388 if (symbol) {
389 tp->symbol = kstrdup(symbol, GFP_KERNEL);
390 if (!tp->symbol)
391 goto error;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200392 tp->rp.kp.symbol_name = tp->symbol;
393 tp->rp.kp.offset = offs;
394 } else
395 tp->rp.kp.addr = addr;
396
397 if (is_return)
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400398 tp->rp.handler = kretprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200399 else
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400400 tp->rp.kp.pre_handler = kprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200401
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500402 if (!event || !check_event_name(event)) {
403 ret = -EINVAL;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400404 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500405 }
406
Steven Rostedt22392912010-04-21 12:27:06 -0400407 tp->call.class = &tp->class;
Masami Hiramatsu42635652009-08-13 16:35:26 -0400408 tp->call.name = kstrdup(event, GFP_KERNEL);
409 if (!tp->call.name)
410 goto error;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400411
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500412 if (!group || !check_event_name(group)) {
413 ret = -EINVAL;
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400414 goto error;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500415 }
416
Steven Rostedt22392912010-04-21 12:27:06 -0400417 tp->class.system = kstrdup(group, GFP_KERNEL);
418 if (!tp->class.system)
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400419 goto error;
420
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400421 INIT_LIST_HEAD(&tp->list);
422 return tp;
423error:
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400424 kfree(tp->call.name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400425 kfree(tp->symbol);
426 kfree(tp);
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500427 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400428}
429
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400430static void free_probe_arg(struct probe_arg *arg)
431{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400432 if (CHECK_BASIC_FETCH_FUNCS(deref, arg->fetch.fn))
433 free_deref_fetch_param(arg->fetch.data);
434 else if (CHECK_BASIC_FETCH_FUNCS(symbol, arg->fetch.fn))
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400435 free_symbol_cache(arg->fetch.data);
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400436 kfree(arg->name);
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400437 kfree(arg->comm);
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400438}
439
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400440static void free_trace_probe(struct trace_probe *tp)
441{
442 int i;
443
444 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400445 free_probe_arg(&tp->args[i]);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400446
Steven Rostedt8f082012010-04-20 10:47:33 -0400447 kfree(tp->call.class->system);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400448 kfree(tp->call.name);
449 kfree(tp->symbol);
450 kfree(tp);
451}
452
Masami Hiramatsudd004c42009-10-27 16:42:44 -0400453static struct trace_probe *find_probe_event(const char *event,
454 const char *group)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400455{
456 struct trace_probe *tp;
457
458 list_for_each_entry(tp, &probe_list, list)
Masami Hiramatsudd004c42009-10-27 16:42:44 -0400459 if (strcmp(tp->call.name, event) == 0 &&
Steven Rostedt8f082012010-04-20 10:47:33 -0400460 strcmp(tp->call.class->system, group) == 0)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400461 return tp;
462 return NULL;
463}
464
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400465/* Unregister a trace_probe and probe_event: call with locking probe_lock */
466static void unregister_trace_probe(struct trace_probe *tp)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400467{
468 if (probe_is_return(tp))
469 unregister_kretprobe(&tp->rp);
470 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200471 unregister_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400472 list_del(&tp->list);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400473 unregister_probe_event(tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400474}
475
476/* Register a trace_probe and probe_event */
477static int register_trace_probe(struct trace_probe *tp)
478{
479 struct trace_probe *old_tp;
480 int ret;
481
482 mutex_lock(&probe_lock);
483
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400484 /* register as an event */
Steven Rostedt8f082012010-04-20 10:47:33 -0400485 old_tp = find_probe_event(tp->call.name, tp->call.class->system);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400486 if (old_tp) {
487 /* delete old event */
488 unregister_trace_probe(old_tp);
489 free_trace_probe(old_tp);
490 }
491 ret = register_probe_event(tp);
492 if (ret) {
493 pr_warning("Faild to register probe event(%d)\n", ret);
494 goto end;
495 }
496
Masami Hiramatsu5a0d9052009-09-14 16:49:37 -0400497 tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400498 if (probe_is_return(tp))
499 ret = register_kretprobe(&tp->rp);
500 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200501 ret = register_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400502
503 if (ret) {
504 pr_warning("Could not insert probe(%d)\n", ret);
505 if (ret == -EILSEQ) {
506 pr_warning("Probing address(0x%p) is not an "
507 "instruction boundary.\n",
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200508 tp->rp.kp.addr);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400509 ret = -EINVAL;
510 }
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400511 unregister_probe_event(tp);
512 } else
513 list_add_tail(&tp->list, &probe_list);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400514end:
515 mutex_unlock(&probe_lock);
516 return ret;
517}
518
519/* Split symbol and offset. */
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400520static int split_symbol_offset(char *symbol, unsigned long *offset)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400521{
522 char *tmp;
523 int ret;
524
525 if (!offset)
526 return -EINVAL;
527
528 tmp = strchr(symbol, '+');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400529 if (tmp) {
530 /* skip sign because strict_strtol doesn't accept '+' */
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400531 ret = strict_strtoul(tmp + 1, 0, offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400532 if (ret)
533 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400534 *tmp = '\0';
535 } else
536 *offset = 0;
537 return 0;
538}
539
540#define PARAM_MAX_ARGS 16
541#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
542
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400543static int parse_probe_vars(char *arg, const struct fetch_type *t,
544 struct fetch_param *f, int is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400545{
546 int ret = 0;
547 unsigned long param;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400548
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400549 if (strcmp(arg, "retval") == 0) {
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400550 if (is_return)
551 f->fn = t->retval;
552 else
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400553 ret = -EINVAL;
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400554 } else if (strncmp(arg, "stack", 5) == 0) {
555 if (arg[5] == '\0') {
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400556 if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
557 f->fn = fetch_stack_address;
558 else
559 ret = -EINVAL;
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400560 } else if (isdigit(arg[5])) {
561 ret = strict_strtoul(arg + 5, 10, &param);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400562 if (ret || param > PARAM_MAX_STACK)
563 ret = -EINVAL;
564 else {
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400565 f->fn = t->stack;
566 f->data = (void *)param;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400567 }
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400568 } else
569 ret = -EINVAL;
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400570 } else
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400571 ret = -EINVAL;
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400572 return ret;
573}
574
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500575/* Recursive argument parser */
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400576static int __parse_probe_arg(char *arg, const struct fetch_type *t,
577 struct fetch_param *f, int is_return)
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400578{
579 int ret = 0;
580 unsigned long param;
581 long offset;
582 char *tmp;
583
584 switch (arg[0]) {
585 case '$':
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400586 ret = parse_probe_vars(arg + 1, t, f, is_return);
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400587 break;
588 case '%': /* named register */
589 ret = regs_query_register_offset(arg + 1);
590 if (ret >= 0) {
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400591 f->fn = t->reg;
592 f->data = (void *)(unsigned long)ret;
Masami Hiramatsu405b2652009-10-07 18:27:40 -0400593 ret = 0;
594 }
595 break;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400596 case '@': /* memory or symbol */
597 if (isdigit(arg[1])) {
598 ret = strict_strtoul(arg + 1, 0, &param);
599 if (ret)
600 break;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400601 f->fn = t->memory;
602 f->data = (void *)param;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400603 } else {
604 ret = split_symbol_offset(arg + 1, &offset);
605 if (ret)
606 break;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400607 f->data = alloc_symbol_cache(arg + 1, offset);
608 if (f->data)
609 f->fn = t->symbol;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400610 }
611 break;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400612 case '+': /* deref memory */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400613 case '-':
614 tmp = strchr(arg, '(');
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400615 if (!tmp)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400616 break;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400617 *tmp = '\0';
618 ret = strict_strtol(arg + 1, 0, &offset);
619 if (ret)
620 break;
621 if (arg[0] == '-')
622 offset = -offset;
623 arg = tmp + 1;
624 tmp = strrchr(arg, ')');
625 if (tmp) {
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400626 struct deref_fetch_param *dprm;
627 const struct fetch_type *t2 = find_fetch_type(NULL);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400628 *tmp = '\0';
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400629 dprm = kzalloc(sizeof(struct deref_fetch_param),
630 GFP_KERNEL);
631 if (!dprm)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400632 return -ENOMEM;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400633 dprm->offset = offset;
634 ret = __parse_probe_arg(arg, t2, &dprm->orig,
635 is_return);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400636 if (ret)
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400637 kfree(dprm);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400638 else {
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400639 f->fn = t->deref;
640 f->data = (void *)dprm;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400641 }
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400642 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400643 break;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400644 }
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400645 if (!ret && !f->fn)
646 ret = -EINVAL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400647 return ret;
648}
649
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500650/* String length checking wrapper */
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400651static int parse_probe_arg(char *arg, struct trace_probe *tp,
652 struct probe_arg *parg, int is_return)
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500653{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400654 const char *t;
655
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500656 if (strlen(arg) > MAX_ARGSTR_LEN) {
657 pr_info("Argument is too long.: %s\n", arg);
658 return -ENOSPC;
659 }
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400660 parg->comm = kstrdup(arg, GFP_KERNEL);
661 if (!parg->comm) {
662 pr_info("Failed to allocate memory for command '%s'.\n", arg);
663 return -ENOMEM;
664 }
665 t = strchr(parg->comm, ':');
666 if (t) {
667 arg[t - parg->comm] = '\0';
668 t++;
669 }
670 parg->type = find_fetch_type(t);
671 if (!parg->type) {
672 pr_info("Unsupported type: %s\n", t);
673 return -EINVAL;
674 }
675 parg->offset = tp->size;
676 tp->size += parg->type->size;
677 return __parse_probe_arg(arg, parg->type, &parg->fetch, is_return);
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500678}
679
Masami Hiramatsua703d942009-10-07 18:28:07 -0400680/* Return 1 if name is reserved or already used by another argument */
681static int conflict_field_name(const char *name,
682 struct probe_arg *args, int narg)
683{
684 int i;
685 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
686 if (strcmp(reserved_field_names[i], name) == 0)
687 return 1;
688 for (i = 0; i < narg; i++)
689 if (strcmp(args[i].name, name) == 0)
690 return 1;
691 return 0;
692}
693
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400694static int create_trace_probe(int argc, char **argv)
695{
696 /*
697 * Argument syntax:
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400698 * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
699 * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400700 * Fetch args:
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400701 * $retval : fetch return value
702 * $stack : fetch stack address
703 * $stackN : fetch Nth of stack (N:0-)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400704 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
705 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
706 * %REG : fetch register REG
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400707 * Dereferencing memory fetch:
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400708 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400709 * Alias name of args:
710 * NAME=FETCHARG : set NAME as alias of FETCHARG.
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400711 * Type of args:
712 * FETCHARG:TYPE : use TYPE instead of unsigned long.
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400713 */
714 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400715 int i, ret = 0;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500716 int is_return = 0, is_delete = 0;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400717 char *symbol = NULL, *event = NULL, *group = NULL;
718 char *arg, *tmp;
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -0400719 unsigned long offset = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400720 void *addr = NULL;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200721 char buf[MAX_EVENT_NAME_LEN];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400722
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500723 /* argc must be >= 1 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400724 if (argv[0][0] == 'p')
725 is_return = 0;
726 else if (argv[0][0] == 'r')
727 is_return = 1;
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500728 else if (argv[0][0] == '-')
729 is_delete = 1;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400730 else {
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500731 pr_info("Probe definition must be started with 'p', 'r' or"
732 " '-'.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400733 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400734 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400735
736 if (argv[0][1] == ':') {
737 event = &argv[0][2];
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400738 if (strchr(event, '/')) {
739 group = event;
740 event = strchr(group, '/') + 1;
741 event[-1] = '\0';
742 if (strlen(group) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800743 pr_info("Group name is not specified\n");
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400744 return -EINVAL;
745 }
746 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400747 if (strlen(event) == 0) {
Wenji Huanga5efd922010-02-24 15:40:23 +0800748 pr_info("Event name is not specified\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400749 return -EINVAL;
750 }
751 }
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500752 if (!group)
753 group = KPROBE_EVENT_SYSTEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400754
Masami Hiramatsua7c312b2009-12-08 17:03:16 -0500755 if (is_delete) {
756 if (!event) {
757 pr_info("Delete command needs an event name.\n");
758 return -EINVAL;
759 }
760 tp = find_probe_event(event, group);
761 if (!tp) {
762 pr_info("Event %s/%s doesn't exist.\n", group, event);
763 return -ENOENT;
764 }
765 /* delete an event */
766 unregister_trace_probe(tp);
767 free_trace_probe(tp);
768 return 0;
769 }
770
771 if (argc < 2) {
772 pr_info("Probe point is not specified.\n");
773 return -EINVAL;
774 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400775 if (isdigit(argv[1][0])) {
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400776 if (is_return) {
777 pr_info("Return probe point must be a symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400778 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400779 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400780 /* an address specified */
Heiko Carstensa9bb18f2010-02-10 17:23:47 +0100781 ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400782 if (ret) {
783 pr_info("Failed to parse address.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400784 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400785 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400786 } else {
787 /* a symbol specified */
788 symbol = argv[1];
789 /* TODO: support .init module functions */
790 ret = split_symbol_offset(symbol, &offset);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400791 if (ret) {
792 pr_info("Failed to parse symbol.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400793 return ret;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400794 }
795 if (offset && is_return) {
796 pr_info("Return probe must be used without offset.\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400797 return -EINVAL;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400798 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400799 }
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400800 argc -= 2; argv += 2;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400801
802 /* setup a probe */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400803 if (!event) {
804 /* Make a new event name */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400805 if (symbol)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500806 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400807 is_return ? 'r' : 'p', symbol, offset);
808 else
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500809 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400810 is_return ? 'r' : 'p', addr);
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200811 event = buf;
812 }
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400813 tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
814 is_return);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400815 if (IS_ERR(tp)) {
816 pr_info("Failed to allocate trace_probe.(%d)\n",
817 (int)PTR_ERR(tp));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400818 return PTR_ERR(tp);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400819 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400820
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400821 /* parse arguments */
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400822 ret = 0;
823 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400824 /* Parse argument name */
825 arg = strchr(argv[i], '=');
826 if (arg)
827 *arg++ = '\0';
828 else
829 arg = argv[i];
Masami Hiramatsua703d942009-10-07 18:28:07 -0400830
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400831 tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500832 if (!tp->args[i].name) {
833 pr_info("Failed to allocate argument%d name '%s'.\n",
834 i, argv[i]);
835 ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400836 goto error;
837 }
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400838 tmp = strchr(tp->args[i].name, ':');
839 if (tmp)
840 *tmp = '_'; /* convert : to _ */
841
842 if (conflict_field_name(tp->args[i].name, tp->args, i)) {
843 pr_info("Argument%d name '%s' conflicts with "
844 "another field.\n", i, argv[i]);
845 ret = -EINVAL;
846 goto error;
847 }
Masami Hiramatsuba8665d2009-11-30 19:19:20 -0500848
849 /* Parse fetch argument */
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400850 ret = parse_probe_arg(arg, tp, &tp->args[i], is_return);
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400851 if (ret) {
852 pr_info("Parse error at argument%d. (%d)\n", i, ret);
Lai Jiangshanabab9d32009-11-25 16:32:21 +0800853 kfree(tp->args[i].name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400854 goto error;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400855 }
Lai Jiangshanabab9d32009-11-25 16:32:21 +0800856
857 tp->nr_args++;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400858 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400859
860 ret = register_trace_probe(tp);
861 if (ret)
862 goto error;
863 return 0;
864
865error:
866 free_trace_probe(tp);
867 return ret;
868}
869
870static void cleanup_all_probes(void)
871{
872 struct trace_probe *tp;
873
874 mutex_lock(&probe_lock);
875 /* TODO: Use batch unregistration */
876 while (!list_empty(&probe_list)) {
877 tp = list_entry(probe_list.next, struct trace_probe, list);
878 unregister_trace_probe(tp);
879 free_trace_probe(tp);
880 }
881 mutex_unlock(&probe_lock);
882}
883
884
885/* Probes listing interfaces */
886static void *probes_seq_start(struct seq_file *m, loff_t *pos)
887{
888 mutex_lock(&probe_lock);
889 return seq_list_start(&probe_list, *pos);
890}
891
892static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
893{
894 return seq_list_next(v, &probe_list, pos);
895}
896
897static void probes_seq_stop(struct seq_file *m, void *v)
898{
899 mutex_unlock(&probe_lock);
900}
901
902static int probes_seq_show(struct seq_file *m, void *v)
903{
904 struct trace_probe *tp = v;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400905 int i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400906
907 seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
Steven Rostedt8f082012010-04-20 10:47:33 -0400908 seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400909
Lai Jiangshan52a11f32009-11-25 16:33:15 +0800910 if (!tp->symbol)
911 seq_printf(m, " 0x%p", tp->rp.kp.addr);
912 else if (tp->rp.kp.offset)
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200913 seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400914 else
Lai Jiangshan52a11f32009-11-25 16:33:15 +0800915 seq_printf(m, " %s", probe_symbol(tp));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400916
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400917 for (i = 0; i < tp->nr_args; i++)
918 seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400919 seq_printf(m, "\n");
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400920
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400921 return 0;
922}
923
924static const struct seq_operations probes_seq_op = {
925 .start = probes_seq_start,
926 .next = probes_seq_next,
927 .stop = probes_seq_stop,
928 .show = probes_seq_show
929};
930
931static int probes_open(struct inode *inode, struct file *file)
932{
933 if ((file->f_mode & FMODE_WRITE) &&
934 (file->f_flags & O_TRUNC))
935 cleanup_all_probes();
936
937 return seq_open(file, &probes_seq_op);
938}
939
940static int command_trace_probe(const char *buf)
941{
942 char **argv;
943 int argc = 0, ret = 0;
944
945 argv = argv_split(GFP_KERNEL, buf, &argc);
946 if (!argv)
947 return -ENOMEM;
948
949 if (argc)
950 ret = create_trace_probe(argc, argv);
951
952 argv_free(argv);
953 return ret;
954}
955
956#define WRITE_BUFSIZE 128
957
958static ssize_t probes_write(struct file *file, const char __user *buffer,
959 size_t count, loff_t *ppos)
960{
961 char *kbuf, *tmp;
962 int ret;
963 size_t done;
964 size_t size;
965
966 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
967 if (!kbuf)
968 return -ENOMEM;
969
970 ret = done = 0;
971 while (done < count) {
972 size = count - done;
973 if (size >= WRITE_BUFSIZE)
974 size = WRITE_BUFSIZE - 1;
975 if (copy_from_user(kbuf, buffer + done, size)) {
976 ret = -EFAULT;
977 goto out;
978 }
979 kbuf[size] = '\0';
980 tmp = strchr(kbuf, '\n');
981 if (tmp) {
982 *tmp = '\0';
983 size = tmp - kbuf + 1;
984 } else if (done + size < count) {
985 pr_warning("Line length is too long: "
986 "Should be less than %d.", WRITE_BUFSIZE);
987 ret = -EINVAL;
988 goto out;
989 }
990 done += size;
991 /* Remove comments */
992 tmp = strchr(kbuf, '#');
993 if (tmp)
994 *tmp = '\0';
995
996 ret = command_trace_probe(kbuf);
997 if (ret)
998 goto out;
999 }
1000 ret = done;
1001out:
1002 kfree(kbuf);
1003 return ret;
1004}
1005
1006static const struct file_operations kprobe_events_ops = {
1007 .owner = THIS_MODULE,
1008 .open = probes_open,
1009 .read = seq_read,
1010 .llseek = seq_lseek,
1011 .release = seq_release,
1012 .write = probes_write,
1013};
1014
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001015/* Probes profiling interfaces */
1016static int probes_profile_seq_show(struct seq_file *m, void *v)
1017{
1018 struct trace_probe *tp = v;
1019
1020 seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +02001021 tp->rp.kp.nmissed);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001022
1023 return 0;
1024}
1025
1026static const struct seq_operations profile_seq_op = {
1027 .start = probes_seq_start,
1028 .next = probes_seq_next,
1029 .stop = probes_seq_stop,
1030 .show = probes_profile_seq_show
1031};
1032
1033static int profile_open(struct inode *inode, struct file *file)
1034{
1035 return seq_open(file, &profile_seq_op);
1036}
1037
1038static const struct file_operations kprobe_profile_ops = {
1039 .owner = THIS_MODULE,
1040 .open = profile_open,
1041 .read = seq_read,
1042 .llseek = seq_lseek,
1043 .release = seq_release,
1044};
1045
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001046/* Kprobe handler */
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001047static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001048{
Masami Hiramatsu4a846b42009-09-11 05:31:21 +02001049 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001050 struct kprobe_trace_entry_head *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001051 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001052 struct ring_buffer *buffer;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001053 u8 *data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001054 int size, i, pc;
1055 unsigned long irq_flags;
Masami Hiramatsu42635652009-08-13 16:35:26 -04001056 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001057
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001058 tp->nhit++;
1059
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001060 local_save_flags(irq_flags);
1061 pc = preempt_count();
1062
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001063 size = sizeof(*entry) + tp->size;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001064
Steven Rostedt32c0eda2010-04-23 10:38:03 -04001065 event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
1066 size, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001067 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001068 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001069
1070 entry = ring_buffer_event_data(event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001071 entry->ip = (unsigned long)kp->addr;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001072 data = (u8 *)&entry[1];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001073 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001074 call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001075
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001076 if (!filter_current_check_discard(buffer, call, entry, event))
1077 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001078}
1079
1080/* Kretprobe handler */
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001081static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001082 struct pt_regs *regs)
1083{
1084 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001085 struct kretprobe_trace_entry_head *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001086 struct ring_buffer_event *event;
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001087 struct ring_buffer *buffer;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001088 u8 *data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001089 int size, i, pc;
1090 unsigned long irq_flags;
Masami Hiramatsu42635652009-08-13 16:35:26 -04001091 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001092
1093 local_save_flags(irq_flags);
1094 pc = preempt_count();
1095
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001096 size = sizeof(*entry) + tp->size;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001097
Steven Rostedt32c0eda2010-04-23 10:38:03 -04001098 event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
1099 size, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001100 if (!event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001101 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001102
1103 entry = ring_buffer_event_data(event);
Masami Hiramatsu4a846b42009-09-11 05:31:21 +02001104 entry->func = (unsigned long)tp->rp.kp.addr;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001105 entry->ret_ip = (unsigned long)ri->ret_addr;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001106 data = (u8 *)&entry[1];
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001107 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001108 call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001109
Frederic Weisbecker8f8ffe22009-09-11 01:09:23 +02001110 if (!filter_current_check_discard(buffer, call, entry, event))
1111 trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001112}
1113
1114/* Event entry printers */
1115enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001116print_kprobe_event(struct trace_iterator *iter, int flags,
1117 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001118{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001119 struct kprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001120 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001121 struct trace_probe *tp;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001122 u8 *data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001123 int i;
1124
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001125 field = (struct kprobe_trace_entry_head *)iter->ent;
Steven Rostedt80decc72010-04-23 10:00:22 -04001126 tp = container_of(event, struct trace_probe, call.event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001127
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001128 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1129 goto partial;
1130
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001131 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1132 goto partial;
1133
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001134 if (!trace_seq_puts(s, ")"))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001135 goto partial;
1136
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001137 data = (u8 *)&field[1];
1138 for (i = 0; i < tp->nr_args; i++)
1139 if (!tp->args[i].type->print(s, tp->args[i].name,
1140 data + tp->args[i].offset))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001141 goto partial;
1142
1143 if (!trace_seq_puts(s, "\n"))
1144 goto partial;
1145
1146 return TRACE_TYPE_HANDLED;
1147partial:
1148 return TRACE_TYPE_PARTIAL_LINE;
1149}
1150
1151enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001152print_kretprobe_event(struct trace_iterator *iter, int flags,
1153 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001154{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001155 struct kretprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001156 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001157 struct trace_probe *tp;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001158 u8 *data;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001159 int i;
1160
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001161 field = (struct kretprobe_trace_entry_head *)iter->ent;
Steven Rostedt80decc72010-04-23 10:00:22 -04001162 tp = container_of(event, struct trace_probe, call.event);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001163
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001164 if (!trace_seq_printf(s, "%s: (", tp->call.name))
1165 goto partial;
1166
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001167 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1168 goto partial;
1169
1170 if (!trace_seq_puts(s, " <- "))
1171 goto partial;
1172
1173 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1174 goto partial;
1175
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001176 if (!trace_seq_puts(s, ")"))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001177 goto partial;
1178
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001179 data = (u8 *)&field[1];
1180 for (i = 0; i < tp->nr_args; i++)
1181 if (!tp->args[i].type->print(s, tp->args[i].name,
1182 data + tp->args[i].offset))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001183 goto partial;
1184
1185 if (!trace_seq_puts(s, "\n"))
1186 goto partial;
1187
1188 return TRACE_TYPE_HANDLED;
1189partial:
1190 return TRACE_TYPE_PARTIAL_LINE;
1191}
1192
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001193static int probe_event_enable(struct ftrace_event_call *call)
1194{
1195 struct trace_probe *tp = (struct trace_probe *)call->data;
1196
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001197 tp->flags |= TP_FLAG_TRACE;
1198 if (probe_is_return(tp))
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001199 return enable_kretprobe(&tp->rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001200 else
Masami Hiramatsu4a846b42009-09-11 05:31:21 +02001201 return enable_kprobe(&tp->rp.kp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001202}
1203
1204static void probe_event_disable(struct ftrace_event_call *call)
1205{
1206 struct trace_probe *tp = (struct trace_probe *)call->data;
1207
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001208 tp->flags &= ~TP_FLAG_TRACE;
1209 if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
1210 if (probe_is_return(tp))
1211 disable_kretprobe(&tp->rp);
1212 else
1213 disable_kprobe(&tp->rp.kp);
1214 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001215}
1216
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001217#undef DEFINE_FIELD
1218#define DEFINE_FIELD(type, item, name, is_signed) \
1219 do { \
1220 ret = trace_define_field(event_call, #type, name, \
1221 offsetof(typeof(field), item), \
1222 sizeof(field.item), is_signed, \
1223 FILTER_OTHER); \
1224 if (ret) \
1225 return ret; \
1226 } while (0)
1227
1228static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1229{
1230 int ret, i;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001231 struct kprobe_trace_entry_head field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001232 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1233
Masami Hiramatsua703d942009-10-07 18:28:07 -04001234 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001235 /* Set argument names as fields */
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001236 for (i = 0; i < tp->nr_args; i++) {
1237 ret = trace_define_field(event_call, tp->args[i].type->name,
1238 tp->args[i].name,
1239 sizeof(field) + tp->args[i].offset,
1240 tp->args[i].type->size,
1241 tp->args[i].type->is_signed,
1242 FILTER_OTHER);
1243 if (ret)
1244 return ret;
1245 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001246 return 0;
1247}
1248
1249static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1250{
1251 int ret, i;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001252 struct kretprobe_trace_entry_head field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001253 struct trace_probe *tp = (struct trace_probe *)event_call->data;
1254
Masami Hiramatsua703d942009-10-07 18:28:07 -04001255 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1256 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001257 /* Set argument names as fields */
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001258 for (i = 0; i < tp->nr_args; i++) {
1259 ret = trace_define_field(event_call, tp->args[i].type->name,
1260 tp->args[i].name,
1261 sizeof(field) + tp->args[i].offset,
1262 tp->args[i].type->size,
1263 tp->args[i].type->is_signed,
1264 FILTER_OTHER);
1265 if (ret)
1266 return ret;
1267 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001268 return 0;
1269}
1270
Lai Jiangshana342a0282009-12-15 15:39:49 +08001271static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
1272{
1273 int i;
1274 int pos = 0;
1275
1276 const char *fmt, *arg;
1277
1278 if (!probe_is_return(tp)) {
1279 fmt = "(%lx)";
1280 arg = "REC->" FIELD_STRING_IP;
1281 } else {
1282 fmt = "(%lx <- %lx)";
1283 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1284 }
1285
1286 /* When len=0, we just calculate the needed length */
1287#define LEN_OR_ZERO (len ? len - pos : 0)
1288
1289 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
1290
1291 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001292 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
1293 tp->args[i].name, tp->args[i].type->fmt);
Lai Jiangshana342a0282009-12-15 15:39:49 +08001294 }
1295
1296 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
1297
1298 for (i = 0; i < tp->nr_args; i++) {
1299 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
1300 tp->args[i].name);
1301 }
1302
1303#undef LEN_OR_ZERO
1304
1305 /* return the length of print_fmt */
1306 return pos;
1307}
1308
1309static int set_print_fmt(struct trace_probe *tp)
1310{
1311 int len;
1312 char *print_fmt;
1313
1314 /* First: called with 0 length to calculate the needed length */
1315 len = __set_print_fmt(tp, NULL, 0);
1316 print_fmt = kmalloc(len + 1, GFP_KERNEL);
1317 if (!print_fmt)
1318 return -ENOMEM;
1319
1320 /* Second: actually write the @print_fmt */
1321 __set_print_fmt(tp, print_fmt, len + 1);
1322 tp->call.print_fmt = print_fmt;
1323
1324 return 0;
1325}
1326
Li Zefan07b139c2009-12-21 14:27:35 +08001327#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001328
1329/* Kprobe profile handler */
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001330static __kprobes void kprobe_perf_func(struct kprobe *kp,
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001331 struct pt_regs *regs)
1332{
1333 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1334 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001335 struct kprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001336 struct hlist_head *head;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001337 u8 *data;
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001338 int size, __size, i;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001339 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001340
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001341 __size = sizeof(*entry) + tp->size;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001342 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1343 size -= sizeof(u32);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001344 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001345 "profile buffer not large enough"))
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001346 return;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001347
Steven Rostedtff5f1492010-05-21 11:49:57 -04001348 entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001349 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001350 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001351
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001352 entry->ip = (unsigned long)kp->addr;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001353 data = (u8 *)&entry[1];
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001354 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001355 call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001356
Peter Zijlstra3771f072010-05-21 12:31:09 +02001357 head = this_cpu_ptr(call->perf_events);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001358 perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001359}
1360
1361/* Kretprobe profile handler */
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001362static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001363 struct pt_regs *regs)
1364{
1365 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1366 struct ftrace_event_call *call = &tp->call;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001367 struct kretprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001368 struct hlist_head *head;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001369 u8 *data;
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001370 int size, __size, i;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001371 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001372
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001373 __size = sizeof(*entry) + tp->size;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001374 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1375 size -= sizeof(u32);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001376 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001377 "profile buffer not large enough"))
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001378 return;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001379
Steven Rostedtff5f1492010-05-21 11:49:57 -04001380 entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001381 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001382 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001383
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001384 entry->func = (unsigned long)tp->rp.kp.addr;
1385 entry->ret_ip = (unsigned long)ri->ret_addr;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001386 data = (u8 *)&entry[1];
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001387 for (i = 0; i < tp->nr_args; i++)
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001388 call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset);
Frederic Weisbecker444a2a32009-11-06 04:13:05 +01001389
Peter Zijlstra3771f072010-05-21 12:31:09 +02001390 head = this_cpu_ptr(call->perf_events);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001391 perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001392}
1393
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001394static int probe_perf_enable(struct ftrace_event_call *call)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001395{
1396 struct trace_probe *tp = (struct trace_probe *)call->data;
1397
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001398 tp->flags |= TP_FLAG_PROFILE;
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001399
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001400 if (probe_is_return(tp))
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001401 return enable_kretprobe(&tp->rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001402 else
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001403 return enable_kprobe(&tp->rp.kp);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001404}
1405
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001406static void probe_perf_disable(struct ftrace_event_call *call)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001407{
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001408 struct trace_probe *tp = (struct trace_probe *)call->data;
1409
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001410 tp->flags &= ~TP_FLAG_PROFILE;
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001411
Frederic Weisbeckerd7a4b412009-09-23 23:08:43 +02001412 if (!(tp->flags & TP_FLAG_TRACE)) {
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001413 if (probe_is_return(tp))
1414 disable_kretprobe(&tp->rp);
1415 else
1416 disable_kprobe(&tp->rp.kp);
1417 }
1418}
Li Zefan07b139c2009-12-21 14:27:35 +08001419#endif /* CONFIG_PERF_EVENTS */
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001420
Steven Rostedt22392912010-04-21 12:27:06 -04001421static __kprobes
1422int kprobe_register(struct ftrace_event_call *event, enum trace_reg type)
1423{
1424 switch (type) {
1425 case TRACE_REG_REGISTER:
1426 return probe_event_enable(event);
1427 case TRACE_REG_UNREGISTER:
1428 probe_event_disable(event);
1429 return 0;
1430
1431#ifdef CONFIG_PERF_EVENTS
1432 case TRACE_REG_PERF_REGISTER:
1433 return probe_perf_enable(event);
1434 case TRACE_REG_PERF_UNREGISTER:
1435 probe_perf_disable(event);
1436 return 0;
1437#endif
1438 }
1439 return 0;
1440}
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001441
1442static __kprobes
1443int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1444{
1445 struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1446
1447 if (tp->flags & TP_FLAG_TRACE)
1448 kprobe_trace_func(kp, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001449#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001450 if (tp->flags & TP_FLAG_PROFILE)
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001451 kprobe_perf_func(kp, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001452#endif
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001453 return 0; /* We don't tweek kernel, so just return 0 */
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001454}
1455
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001456static __kprobes
1457int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1458{
1459 struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1460
1461 if (tp->flags & TP_FLAG_TRACE)
1462 kretprobe_trace_func(ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001463#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001464 if (tp->flags & TP_FLAG_PROFILE)
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01001465 kretprobe_perf_func(ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001466#endif
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001467 return 0; /* We don't tweek kernel, so just return 0 */
1468}
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001469
Steven Rostedta9a57762010-04-22 18:46:14 -04001470static struct trace_event_functions kretprobe_funcs = {
1471 .trace = print_kretprobe_event
1472};
1473
1474static struct trace_event_functions kprobe_funcs = {
1475 .trace = print_kprobe_event
1476};
1477
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001478static int register_probe_event(struct trace_probe *tp)
1479{
1480 struct ftrace_event_call *call = &tp->call;
1481 int ret;
1482
1483 /* Initialize ftrace_event_call */
Li Zefanffb9f992010-05-24 16:24:52 +08001484 INIT_LIST_HEAD(&call->class->fields);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001485 if (probe_is_return(tp)) {
Steven Rostedt80decc72010-04-23 10:00:22 -04001486 call->event.funcs = &kretprobe_funcs;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001487 call->class->define_fields = kretprobe_event_define_fields;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001488 } else {
Steven Rostedt80decc72010-04-23 10:00:22 -04001489 call->event.funcs = &kprobe_funcs;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001490 call->class->define_fields = kprobe_event_define_fields;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001491 }
Lai Jiangshana342a0282009-12-15 15:39:49 +08001492 if (set_print_fmt(tp) < 0)
1493 return -ENOMEM;
Steven Rostedt32c0eda2010-04-23 10:38:03 -04001494 ret = register_ftrace_event(&call->event);
1495 if (!ret) {
Lai Jiangshana342a0282009-12-15 15:39:49 +08001496 kfree(call->print_fmt);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001497 return -ENODEV;
Lai Jiangshana342a0282009-12-15 15:39:49 +08001498 }
Steven Rostedt553552c2010-04-23 11:12:36 -04001499 call->flags = 0;
Steven Rostedt22392912010-04-21 12:27:06 -04001500 call->class->reg = kprobe_register;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001501 call->data = tp;
1502 ret = trace_add_event_call(call);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001503 if (ret) {
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001504 pr_info("Failed to register kprobe event: %s\n", call->name);
Lai Jiangshana342a0282009-12-15 15:39:49 +08001505 kfree(call->print_fmt);
Steven Rostedt80decc72010-04-23 10:00:22 -04001506 unregister_ftrace_event(&call->event);
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001507 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001508 return ret;
1509}
1510
1511static void unregister_probe_event(struct trace_probe *tp)
1512{
Masami Hiramatsuff50d992009-08-13 16:35:34 -04001513 /* tp->event is unregistered in trace_remove_event_call() */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001514 trace_remove_event_call(&tp->call);
Lai Jiangshana342a0282009-12-15 15:39:49 +08001515 kfree(tp->call.print_fmt);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001516}
1517
1518/* Make a debugfs interface for controling probe points */
1519static __init int init_kprobe_trace(void)
1520{
1521 struct dentry *d_tracer;
1522 struct dentry *entry;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001523
1524 d_tracer = tracing_init_dentry();
1525 if (!d_tracer)
1526 return 0;
1527
1528 entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1529 NULL, &kprobe_events_ops);
1530
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001531 /* Event list interface */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001532 if (!entry)
1533 pr_warning("Could not create debugfs "
1534 "'kprobe_events' entry\n");
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001535
1536 /* Profile interface */
1537 entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1538 NULL, &kprobe_profile_ops);
1539
1540 if (!entry)
1541 pr_warning("Could not create debugfs "
1542 "'kprobe_profile' entry\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001543 return 0;
1544}
1545fs_initcall(init_kprobe_trace);
1546
1547
1548#ifdef CONFIG_FTRACE_STARTUP_TEST
1549
1550static int kprobe_trace_selftest_target(int a1, int a2, int a3,
1551 int a4, int a5, int a6)
1552{
1553 return a1 + a2 + a3 + a4 + a5 + a6;
1554}
1555
1556static __init int kprobe_trace_self_tests_init(void)
1557{
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001558 int ret, warn = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001559 int (*target)(int, int, int, int, int, int);
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001560 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001561
1562 target = kprobe_trace_selftest_target;
1563
1564 pr_info("Testing kprobe tracing: ");
1565
1566 ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001567 "$stack $stack0 +0($stack)");
1568 if (WARN_ON_ONCE(ret)) {
1569 pr_warning("error on probing function entry.\n");
1570 warn++;
1571 } else {
1572 /* Enable trace point */
1573 tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
1574 if (WARN_ON_ONCE(tp == NULL)) {
1575 pr_warning("error on getting new probe.\n");
1576 warn++;
1577 } else
1578 probe_event_enable(&tp->call);
1579 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001580
1581 ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
Masami Hiramatsuf397af02009-10-16 20:07:20 -04001582 "$retval");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001583 if (WARN_ON_ONCE(ret)) {
1584 pr_warning("error on probing function return.\n");
1585 warn++;
1586 } else {
1587 /* Enable trace point */
1588 tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
1589 if (WARN_ON_ONCE(tp == NULL)) {
1590 pr_warning("error on getting new probe.\n");
1591 warn++;
1592 } else
1593 probe_event_enable(&tp->call);
1594 }
1595
1596 if (warn)
1597 goto end;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001598
1599 ret = target(1, 2, 3, 4, 5, 6);
1600
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001601 ret = command_trace_probe("-:testprobe");
1602 if (WARN_ON_ONCE(ret)) {
1603 pr_warning("error on deleting a probe.\n");
1604 warn++;
1605 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001606
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001607 ret = command_trace_probe("-:testprobe2");
1608 if (WARN_ON_ONCE(ret)) {
1609 pr_warning("error on deleting a probe.\n");
1610 warn++;
1611 }
1612
1613end:
1614 cleanup_all_probes();
1615 if (warn)
1616 pr_cont("NG: Some tests are failed. Please check them.\n");
1617 else
1618 pr_cont("OK\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001619 return 0;
1620}
1621
1622late_initcall(kprobe_trace_self_tests_init);
1623
1624#endif