2 * Common code for probe-based Dynamic events.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * This code was copied from kernel/trace/trace_kprobe.c written by
18 * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
20 * Updates to make this generic:
21 * Copyright (C) IBM Corporation, 2010-2011
22 * Author: Srikar Dronamraju
25 #include "trace_probe.h"
27 const char *reserved_field_names[] = {
30 "common_preempt_count",
38 /* Printing in basic type function template */
39 #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt) \
40 __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \
42 void *data, void *ent) \
44 return trace_seq_printf(s, " %s=" fmt, name, *(type *)data); \
46 const char PRINT_TYPE_FMT_NAME(type)[] = fmt;
48 DEFINE_BASIC_PRINT_TYPE_FUNC(u8 , "0x%x")
49 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "0x%x")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "0x%x")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "0x%Lx")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d")
53 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d")
54 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%d")
55 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%Ld")
57 /* For defining macros, define string/string_size types */
59 typedef u32 string_size;
61 /* Print type function for string type */
62 __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s,
64 void *data, void *ent)
66 int len = *(u32 *)data >> 16;
69 return trace_seq_printf(s, " %s=(fault)", name);
71 return trace_seq_printf(s, " %s=\"%s\"", name,
72 (const char *)get_loc_data(data, ent));
75 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
77 #define FETCH_FUNC_NAME(method, type) fetch_##method##_##type
79 * Define macro for basic types - we don't need to define s* types, because
80 * we have to care only about bitwidth at recording time.
82 #define DEFINE_BASIC_FETCH_FUNCS(method) \
83 DEFINE_FETCH_##method(u8) \
84 DEFINE_FETCH_##method(u16) \
85 DEFINE_FETCH_##method(u32) \
86 DEFINE_FETCH_##method(u64)
88 #define CHECK_FETCH_FUNCS(method, fn) \
89 (((FETCH_FUNC_NAME(method, u8) == fn) || \
90 (FETCH_FUNC_NAME(method, u16) == fn) || \
91 (FETCH_FUNC_NAME(method, u32) == fn) || \
92 (FETCH_FUNC_NAME(method, u64) == fn) || \
93 (FETCH_FUNC_NAME(method, string) == fn) || \
94 (FETCH_FUNC_NAME(method, string_size) == fn)) \
97 /* Data fetch function templates */
98 #define DEFINE_FETCH_reg(type) \
99 __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \
100 void *offset, void *dest) \
102 *(type *)dest = (type)regs_get_register(regs, \
103 (unsigned int)((unsigned long)offset)); \
105 DEFINE_BASIC_FETCH_FUNCS(reg)
106 /* No string on the register */
107 #define fetch_reg_string NULL
108 #define fetch_reg_string_size NULL
110 #define DEFINE_FETCH_stack(type) \
111 __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
112 void *offset, void *dest) \
114 *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
115 (unsigned int)((unsigned long)offset)); \
117 DEFINE_BASIC_FETCH_FUNCS(stack)
118 /* No string on the stack entry */
119 #define fetch_stack_string NULL
120 #define fetch_stack_string_size NULL
122 #define DEFINE_FETCH_retval(type) \
123 __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs, \
124 void *dummy, void *dest) \
126 *(type *)dest = (type)regs_return_value(regs); \
128 DEFINE_BASIC_FETCH_FUNCS(retval)
129 /* No string on the retval */
130 #define fetch_retval_string NULL
131 #define fetch_retval_string_size NULL
133 #define DEFINE_FETCH_memory(type) \
134 __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
135 void *addr, void *dest) \
138 if (probe_kernel_address(addr, retval)) \
141 *(type *)dest = retval; \
143 DEFINE_BASIC_FETCH_FUNCS(memory)
145 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
146 * length and relative data location.
148 __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
149 void *addr, void *dest)
152 int maxlen = get_rloc_len(*(u32 *)dest);
153 u8 *dst = get_rloc_data(dest);
155 mm_segment_t old_fs = get_fs();
161 * Try to get string again, since the string can be changed while
168 ret = __copy_from_user_inatomic(dst++, src++, 1);
169 while (dst[-1] && ret == 0 && src - (u8 *)addr < maxlen);
175 if (ret < 0) { /* Failed to fetch string */
176 ((u8 *)get_rloc_data(dest))[0] = '\0';
177 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
179 *(u32 *)dest = make_data_rloc(src - (u8 *)addr,
180 get_rloc_offs(*(u32 *)dest));
184 /* Return the length of string -- including null terminal byte */
185 __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
186 void *addr, void *dest)
197 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
199 } while (c && ret == 0 && len < MAX_STRING_SIZE);
204 if (ret < 0) /* Failed to check the length */
210 /* Memory fetching by symbol */
211 struct symbol_cache {
217 static unsigned long update_symbol_cache(struct symbol_cache *sc)
219 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
222 sc->addr += sc->offset;
227 static void free_symbol_cache(struct symbol_cache *sc)
233 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
235 struct symbol_cache *sc;
237 if (!sym || strlen(sym) == 0)
240 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
244 sc->symbol = kstrdup(sym, GFP_KERNEL);
250 update_symbol_cache(sc);
255 #define DEFINE_FETCH_symbol(type) \
256 __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, \
257 void *data, void *dest) \
259 struct symbol_cache *sc = data; \
261 fetch_memory_##type(regs, (void *)sc->addr, dest); \
265 DEFINE_BASIC_FETCH_FUNCS(symbol)
266 DEFINE_FETCH_symbol(string)
267 DEFINE_FETCH_symbol(string_size)
269 /* Dereference memory access function */
270 struct deref_fetch_param {
271 struct fetch_param orig;
275 #define DEFINE_FETCH_deref(type) \
276 __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs, \
277 void *data, void *dest) \
279 struct deref_fetch_param *dprm = data; \
280 unsigned long addr; \
281 call_fetch(&dprm->orig, regs, &addr); \
283 addr += dprm->offset; \
284 fetch_memory_##type(regs, (void *)addr, dest); \
288 DEFINE_BASIC_FETCH_FUNCS(deref)
289 DEFINE_FETCH_deref(string)
290 DEFINE_FETCH_deref(string_size)
292 static __kprobes void update_deref_fetch_param(struct deref_fetch_param *data)
294 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
295 update_deref_fetch_param(data->orig.data);
296 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
297 update_symbol_cache(data->orig.data);
300 static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
302 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
303 free_deref_fetch_param(data->orig.data);
304 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
305 free_symbol_cache(data->orig.data);
309 /* Bitfield fetch function */
310 struct bitfield_fetch_param {
311 struct fetch_param orig;
312 unsigned char hi_shift;
313 unsigned char low_shift;
316 #define DEFINE_FETCH_bitfield(type) \
317 __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs, \
318 void *data, void *dest) \
320 struct bitfield_fetch_param *bprm = data; \
322 call_fetch(&bprm->orig, regs, &buf); \
324 buf <<= bprm->hi_shift; \
325 buf >>= bprm->low_shift; \
327 *(type *)dest = buf; \
330 DEFINE_BASIC_FETCH_FUNCS(bitfield)
331 #define fetch_bitfield_string NULL
332 #define fetch_bitfield_string_size NULL
334 static __kprobes void
335 update_bitfield_fetch_param(struct bitfield_fetch_param *data)
338 * Don't check the bitfield itself, because this must be the
339 * last fetch function.
341 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
342 update_deref_fetch_param(data->orig.data);
343 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
344 update_symbol_cache(data->orig.data);
347 static __kprobes void
348 free_bitfield_fetch_param(struct bitfield_fetch_param *data)
351 * Don't check the bitfield itself, because this must be the
352 * last fetch function.
354 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
355 free_deref_fetch_param(data->orig.data);
356 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
357 free_symbol_cache(data->orig.data);
362 /* Fetch type information table */
363 static const struct fetch_type fetch_type_table[] = {
365 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
366 sizeof(u32), 1, "__data_loc char[]"),
367 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
368 string_size, sizeof(u32), 0, "u32"),
370 ASSIGN_FETCH_TYPE(u8, u8, 0),
371 ASSIGN_FETCH_TYPE(u16, u16, 0),
372 ASSIGN_FETCH_TYPE(u32, u32, 0),
373 ASSIGN_FETCH_TYPE(u64, u64, 0),
374 ASSIGN_FETCH_TYPE(s8, u8, 1),
375 ASSIGN_FETCH_TYPE(s16, u16, 1),
376 ASSIGN_FETCH_TYPE(s32, u32, 1),
377 ASSIGN_FETCH_TYPE(s64, u64, 1),
380 static const struct fetch_type *find_fetch_type(const char *type)
385 type = DEFAULT_FETCH_TYPE_STR;
387 /* Special case: bitfield */
391 type = strchr(type, '/');
396 if (kstrtoul(type, 0, &bs))
401 return find_fetch_type("u8");
403 return find_fetch_type("u16");
405 return find_fetch_type("u32");
407 return find_fetch_type("u64");
413 for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++)
414 if (strcmp(type, fetch_type_table[i].name) == 0)
415 return &fetch_type_table[i];
421 /* Special function : only accept unsigned long */
422 static __kprobes void fetch_stack_address(struct pt_regs *regs,
423 void *dummy, void *dest)
425 *(unsigned long *)dest = kernel_stack_pointer(regs);
428 static fetch_func_t get_fetch_size_function(const struct fetch_type *type,
429 fetch_func_t orig_fn)
433 if (type != &fetch_type_table[FETCH_TYPE_STRING])
434 return NULL; /* Only string type needs size function */
436 for (i = 0; i < FETCH_MTD_END; i++)
437 if (type->fetch[i] == orig_fn)
438 return fetch_type_table[FETCH_TYPE_STRSIZE].fetch[i];
440 WARN_ON(1); /* This should not happen */
445 /* Split symbol and offset. */
446 int traceprobe_split_symbol_offset(char *symbol, unsigned long *offset)
454 tmp = strchr(symbol, '+');
456 /* skip sign because kstrtoul doesn't accept '+' */
457 ret = kstrtoul(tmp + 1, 0, offset);
468 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
470 static int parse_probe_vars(char *arg, const struct fetch_type *t,
471 struct fetch_param *f, bool is_return)
476 if (strcmp(arg, "retval") == 0) {
478 f->fn = t->fetch[FETCH_MTD_retval];
481 } else if (strncmp(arg, "stack", 5) == 0) {
482 if (arg[5] == '\0') {
483 if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
484 f->fn = fetch_stack_address;
487 } else if (isdigit(arg[5])) {
488 ret = kstrtoul(arg + 5, 10, ¶m);
489 if (ret || param > PARAM_MAX_STACK)
492 f->fn = t->fetch[FETCH_MTD_stack];
493 f->data = (void *)param;
503 /* Recursive argument parser */
504 static int parse_probe_arg(char *arg, const struct fetch_type *t,
505 struct fetch_param *f, bool is_return, bool is_kprobe)
514 /* Until uprobe_events supports only reg arguments */
515 if (!is_kprobe && arg[0] != '%')
520 ret = parse_probe_vars(arg + 1, t, f, is_return);
523 case '%': /* named register */
524 ret = regs_query_register_offset(arg + 1);
526 f->fn = t->fetch[FETCH_MTD_reg];
527 f->data = (void *)(unsigned long)ret;
532 case '@': /* memory or symbol */
533 if (isdigit(arg[1])) {
534 ret = kstrtoul(arg + 1, 0, ¶m);
538 f->fn = t->fetch[FETCH_MTD_memory];
539 f->data = (void *)param;
541 ret = traceprobe_split_symbol_offset(arg + 1, &offset);
545 f->data = alloc_symbol_cache(arg + 1, offset);
547 f->fn = t->fetch[FETCH_MTD_symbol];
551 case '+': /* deref memory */
552 arg++; /* Skip '+', because kstrtol() rejects it. */
554 tmp = strchr(arg, '(');
559 ret = kstrtol(arg, 0, &offset);
565 tmp = strrchr(arg, ')');
568 struct deref_fetch_param *dprm;
569 const struct fetch_type *t2;
571 t2 = find_fetch_type(NULL);
573 dprm = kzalloc(sizeof(struct deref_fetch_param), GFP_KERNEL);
578 dprm->offset = offset;
579 ret = parse_probe_arg(arg, t2, &dprm->orig, is_return,
584 f->fn = t->fetch[FETCH_MTD_deref];
585 f->data = (void *)dprm;
590 if (!ret && !f->fn) { /* Parsed, but do not find fetch method */
591 pr_info("%s type has no corresponding fetch method.\n", t->name);
598 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
600 /* Bitfield type needs to be parsed into a fetch function */
601 static int __parse_bitfield_probe_arg(const char *bf,
602 const struct fetch_type *t,
603 struct fetch_param *f)
605 struct bitfield_fetch_param *bprm;
606 unsigned long bw, bo;
612 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
617 f->fn = t->fetch[FETCH_MTD_bitfield];
618 f->data = (void *)bprm;
619 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
621 if (bw == 0 || *tail != '@')
625 bo = simple_strtoul(bf, &tail, 0);
627 if (tail == bf || *tail != '/')
630 bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
631 bprm->low_shift = bprm->hi_shift + bo;
633 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
636 /* String length checking wrapper */
637 int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
638 struct probe_arg *parg, bool is_return, bool is_kprobe)
643 if (strlen(arg) > MAX_ARGSTR_LEN) {
644 pr_info("Argument is too long.: %s\n", arg);
647 parg->comm = kstrdup(arg, GFP_KERNEL);
649 pr_info("Failed to allocate memory for command '%s'.\n", arg);
652 t = strchr(parg->comm, ':');
654 arg[t - parg->comm] = '\0';
657 parg->type = find_fetch_type(t);
659 pr_info("Unsupported type: %s\n", t);
662 parg->offset = *size;
663 *size += parg->type->size;
664 ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return, is_kprobe);
666 if (ret >= 0 && t != NULL)
667 ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
670 parg->fetch_size.fn = get_fetch_size_function(parg->type,
672 parg->fetch_size.data = parg->fetch.data;
678 /* Return 1 if name is reserved or already used by another argument */
679 int traceprobe_conflict_field_name(const char *name,
680 struct probe_arg *args, int narg)
684 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
685 if (strcmp(reserved_field_names[i], name) == 0)
688 for (i = 0; i < narg; i++)
689 if (strcmp(args[i].name, name) == 0)
695 void traceprobe_update_arg(struct probe_arg *arg)
697 if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
698 update_bitfield_fetch_param(arg->fetch.data);
699 else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
700 update_deref_fetch_param(arg->fetch.data);
701 else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
702 update_symbol_cache(arg->fetch.data);
705 void traceprobe_free_probe_arg(struct probe_arg *arg)
707 if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
708 free_bitfield_fetch_param(arg->fetch.data);
709 else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
710 free_deref_fetch_param(arg->fetch.data);
711 else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
712 free_symbol_cache(arg->fetch.data);
718 int traceprobe_command(const char *buf, int (*createfn)(int, char **))
725 argv = argv_split(GFP_KERNEL, buf, &argc);
730 ret = createfn(argc, argv);
737 #define WRITE_BUFSIZE 4096
739 ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,
740 size_t count, loff_t *ppos,
741 int (*createfn)(int, char **))
748 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
752 while (done < count) {
755 if (size >= WRITE_BUFSIZE)
756 size = WRITE_BUFSIZE - 1;
758 if (copy_from_user(kbuf, buffer + done, size)) {
763 tmp = strchr(kbuf, '\n');
767 size = tmp - kbuf + 1;
768 } else if (done + size < count) {
769 pr_warning("Line length is too long: "
770 "Should be less than %d.", WRITE_BUFSIZE);
775 /* Remove comments */
776 tmp = strchr(kbuf, '#');
781 ret = traceprobe_command(kbuf, createfn);
793 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
799 const char *fmt, *arg;
803 arg = "REC->" FIELD_STRING_IP;
805 fmt = "(%lx <- %lx)";
806 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
809 /* When len=0, we just calculate the needed length */
810 #define LEN_OR_ZERO (len ? len - pos : 0)
812 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
814 for (i = 0; i < tp->nr_args; i++) {
815 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
816 tp->args[i].name, tp->args[i].type->fmt);
819 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
821 for (i = 0; i < tp->nr_args; i++) {
822 if (strcmp(tp->args[i].type->name, "string") == 0)
823 pos += snprintf(buf + pos, LEN_OR_ZERO,
827 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
833 /* return the length of print_fmt */
837 int set_print_fmt(struct trace_probe *tp, bool is_return)
842 /* First: called with 0 length to calculate the needed length */
843 len = __set_print_fmt(tp, NULL, 0, is_return);
844 print_fmt = kmalloc(len + 1, GFP_KERNEL);
848 /* Second: actually write the @print_fmt */
849 __set_print_fmt(tp, print_fmt, len + 1, is_return);
850 tp->call.print_fmt = print_fmt;