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 /* Print type function for string type */
58 __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s,
60 void *data, void *ent)
62 int len = *(u32 *)data >> 16;
65 return trace_seq_printf(s, " %s=(fault)", name);
67 return trace_seq_printf(s, " %s=\"%s\"", name,
68 (const char *)get_loc_data(data, ent));
71 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
73 #define CHECK_FETCH_FUNCS(method, fn) \
74 (((FETCH_FUNC_NAME(method, u8) == fn) || \
75 (FETCH_FUNC_NAME(method, u16) == fn) || \
76 (FETCH_FUNC_NAME(method, u32) == fn) || \
77 (FETCH_FUNC_NAME(method, u64) == fn) || \
78 (FETCH_FUNC_NAME(method, string) == fn) || \
79 (FETCH_FUNC_NAME(method, string_size) == fn)) \
82 /* Data fetch function templates */
83 #define DEFINE_FETCH_reg(type) \
84 __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \
85 void *offset, void *dest) \
87 *(type *)dest = (type)regs_get_register(regs, \
88 (unsigned int)((unsigned long)offset)); \
90 DEFINE_BASIC_FETCH_FUNCS(reg)
91 /* No string on the register */
92 #define fetch_reg_string NULL
93 #define fetch_reg_string_size NULL
95 #define DEFINE_FETCH_retval(type) \
96 __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs, \
97 void *dummy, void *dest) \
99 *(type *)dest = (type)regs_return_value(regs); \
101 DEFINE_BASIC_FETCH_FUNCS(retval)
102 /* No string on the retval */
103 #define fetch_retval_string NULL
104 #define fetch_retval_string_size NULL
106 #define DEFINE_FETCH_memory(type) \
107 __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
108 void *addr, void *dest) \
111 if (probe_kernel_address(addr, retval)) \
114 *(type *)dest = retval; \
116 DEFINE_BASIC_FETCH_FUNCS(memory)
118 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
119 * length and relative data location.
121 __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
122 void *addr, void *dest)
125 int maxlen = get_rloc_len(*(u32 *)dest);
126 u8 *dst = get_rloc_data(dest);
128 mm_segment_t old_fs = get_fs();
134 * Try to get string again, since the string can be changed while
141 ret = __copy_from_user_inatomic(dst++, src++, 1);
142 while (dst[-1] && ret == 0 && src - (u8 *)addr < maxlen);
148 if (ret < 0) { /* Failed to fetch string */
149 ((u8 *)get_rloc_data(dest))[0] = '\0';
150 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
152 *(u32 *)dest = make_data_rloc(src - (u8 *)addr,
153 get_rloc_offs(*(u32 *)dest));
157 /* Return the length of string -- including null terminal byte */
158 __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
159 void *addr, void *dest)
170 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
172 } while (c && ret == 0 && len < MAX_STRING_SIZE);
177 if (ret < 0) /* Failed to check the length */
183 /* Memory fetching by symbol */
184 struct symbol_cache {
190 static unsigned long update_symbol_cache(struct symbol_cache *sc)
192 sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
195 sc->addr += sc->offset;
200 static void free_symbol_cache(struct symbol_cache *sc)
206 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
208 struct symbol_cache *sc;
210 if (!sym || strlen(sym) == 0)
213 sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
217 sc->symbol = kstrdup(sym, GFP_KERNEL);
223 update_symbol_cache(sc);
228 #define DEFINE_FETCH_symbol(type) \
229 __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, \
230 void *data, void *dest) \
232 struct symbol_cache *sc = data; \
234 fetch_memory_##type(regs, (void *)sc->addr, dest); \
238 DEFINE_BASIC_FETCH_FUNCS(symbol)
239 DEFINE_FETCH_symbol(string)
240 DEFINE_FETCH_symbol(string_size)
242 /* Dereference memory access function */
243 struct deref_fetch_param {
244 struct fetch_param orig;
248 #define DEFINE_FETCH_deref(type) \
249 __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs, \
250 void *data, void *dest) \
252 struct deref_fetch_param *dprm = data; \
253 unsigned long addr; \
254 call_fetch(&dprm->orig, regs, &addr); \
256 addr += dprm->offset; \
257 fetch_memory_##type(regs, (void *)addr, dest); \
261 DEFINE_BASIC_FETCH_FUNCS(deref)
262 DEFINE_FETCH_deref(string)
263 DEFINE_FETCH_deref(string_size)
265 static __kprobes void update_deref_fetch_param(struct deref_fetch_param *data)
267 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
268 update_deref_fetch_param(data->orig.data);
269 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
270 update_symbol_cache(data->orig.data);
273 static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
275 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
276 free_deref_fetch_param(data->orig.data);
277 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
278 free_symbol_cache(data->orig.data);
282 /* Bitfield fetch function */
283 struct bitfield_fetch_param {
284 struct fetch_param orig;
285 unsigned char hi_shift;
286 unsigned char low_shift;
289 #define DEFINE_FETCH_bitfield(type) \
290 __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs, \
291 void *data, void *dest) \
293 struct bitfield_fetch_param *bprm = data; \
295 call_fetch(&bprm->orig, regs, &buf); \
297 buf <<= bprm->hi_shift; \
298 buf >>= bprm->low_shift; \
300 *(type *)dest = buf; \
303 DEFINE_BASIC_FETCH_FUNCS(bitfield)
304 #define fetch_bitfield_string NULL
305 #define fetch_bitfield_string_size NULL
307 static __kprobes void
308 update_bitfield_fetch_param(struct bitfield_fetch_param *data)
311 * Don't check the bitfield itself, because this must be the
312 * last fetch function.
314 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
315 update_deref_fetch_param(data->orig.data);
316 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
317 update_symbol_cache(data->orig.data);
320 static __kprobes void
321 free_bitfield_fetch_param(struct bitfield_fetch_param *data)
324 * Don't check the bitfield itself, because this must be the
325 * last fetch function.
327 if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
328 free_deref_fetch_param(data->orig.data);
329 else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
330 free_symbol_cache(data->orig.data);
335 static const struct fetch_type *find_fetch_type(const char *type,
336 const struct fetch_type *ftbl)
341 type = DEFAULT_FETCH_TYPE_STR;
343 /* Special case: bitfield */
347 type = strchr(type, '/');
352 if (kstrtoul(type, 0, &bs))
357 return find_fetch_type("u8", ftbl);
359 return find_fetch_type("u16", ftbl);
361 return find_fetch_type("u32", ftbl);
363 return find_fetch_type("u64", ftbl);
369 for (i = 0; ftbl[i].name; i++) {
370 if (strcmp(type, ftbl[i].name) == 0)
378 /* Special function : only accept unsigned long */
379 static __kprobes void fetch_stack_address(struct pt_regs *regs,
380 void *dummy, void *dest)
382 *(unsigned long *)dest = kernel_stack_pointer(regs);
385 static fetch_func_t get_fetch_size_function(const struct fetch_type *type,
386 fetch_func_t orig_fn,
387 const struct fetch_type *ftbl)
391 if (type != &ftbl[FETCH_TYPE_STRING])
392 return NULL; /* Only string type needs size function */
394 for (i = 0; i < FETCH_MTD_END; i++)
395 if (type->fetch[i] == orig_fn)
396 return ftbl[FETCH_TYPE_STRSIZE].fetch[i];
398 WARN_ON(1); /* This should not happen */
403 /* Split symbol and offset. */
404 int traceprobe_split_symbol_offset(char *symbol, unsigned long *offset)
412 tmp = strchr(symbol, '+');
414 /* skip sign because kstrtoul doesn't accept '+' */
415 ret = kstrtoul(tmp + 1, 0, offset);
426 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
428 static int parse_probe_vars(char *arg, const struct fetch_type *t,
429 struct fetch_param *f, bool is_return)
434 if (strcmp(arg, "retval") == 0) {
436 f->fn = t->fetch[FETCH_MTD_retval];
439 } else if (strncmp(arg, "stack", 5) == 0) {
440 if (arg[5] == '\0') {
441 if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
442 f->fn = fetch_stack_address;
445 } else if (isdigit(arg[5])) {
446 ret = kstrtoul(arg + 5, 10, ¶m);
447 if (ret || param > PARAM_MAX_STACK)
450 f->fn = t->fetch[FETCH_MTD_stack];
451 f->data = (void *)param;
461 /* Recursive argument parser */
462 static int parse_probe_arg(char *arg, const struct fetch_type *t,
463 struct fetch_param *f, bool is_return, bool is_kprobe)
465 const struct fetch_type *ftbl;
471 ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table;
472 BUG_ON(ftbl == NULL);
474 /* Until uprobe_events supports only reg arguments */
475 if (!is_kprobe && arg[0] != '%')
480 ret = parse_probe_vars(arg + 1, t, f, is_return);
483 case '%': /* named register */
484 ret = regs_query_register_offset(arg + 1);
486 f->fn = t->fetch[FETCH_MTD_reg];
487 f->data = (void *)(unsigned long)ret;
492 case '@': /* memory or symbol */
493 if (isdigit(arg[1])) {
494 ret = kstrtoul(arg + 1, 0, ¶m);
498 f->fn = t->fetch[FETCH_MTD_memory];
499 f->data = (void *)param;
501 ret = traceprobe_split_symbol_offset(arg + 1, &offset);
505 f->data = alloc_symbol_cache(arg + 1, offset);
507 f->fn = t->fetch[FETCH_MTD_symbol];
511 case '+': /* deref memory */
512 arg++; /* Skip '+', because kstrtol() rejects it. */
514 tmp = strchr(arg, '(');
519 ret = kstrtol(arg, 0, &offset);
525 tmp = strrchr(arg, ')');
528 struct deref_fetch_param *dprm;
529 const struct fetch_type *t2;
531 t2 = find_fetch_type(NULL, ftbl);
533 dprm = kzalloc(sizeof(struct deref_fetch_param), GFP_KERNEL);
538 dprm->offset = offset;
539 ret = parse_probe_arg(arg, t2, &dprm->orig, is_return,
544 f->fn = t->fetch[FETCH_MTD_deref];
545 f->data = (void *)dprm;
550 if (!ret && !f->fn) { /* Parsed, but do not find fetch method */
551 pr_info("%s type has no corresponding fetch method.\n", t->name);
558 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
560 /* Bitfield type needs to be parsed into a fetch function */
561 static int __parse_bitfield_probe_arg(const char *bf,
562 const struct fetch_type *t,
563 struct fetch_param *f)
565 struct bitfield_fetch_param *bprm;
566 unsigned long bw, bo;
572 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
577 f->fn = t->fetch[FETCH_MTD_bitfield];
578 f->data = (void *)bprm;
579 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
581 if (bw == 0 || *tail != '@')
585 bo = simple_strtoul(bf, &tail, 0);
587 if (tail == bf || *tail != '/')
590 bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
591 bprm->low_shift = bprm->hi_shift + bo;
593 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
596 /* String length checking wrapper */
597 int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
598 struct probe_arg *parg, bool is_return, bool is_kprobe)
600 const struct fetch_type *ftbl;
604 ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table;
605 BUG_ON(ftbl == NULL);
607 if (strlen(arg) > MAX_ARGSTR_LEN) {
608 pr_info("Argument is too long.: %s\n", arg);
611 parg->comm = kstrdup(arg, GFP_KERNEL);
613 pr_info("Failed to allocate memory for command '%s'.\n", arg);
616 t = strchr(parg->comm, ':');
618 arg[t - parg->comm] = '\0';
621 parg->type = find_fetch_type(t, ftbl);
623 pr_info("Unsupported type: %s\n", t);
626 parg->offset = *size;
627 *size += parg->type->size;
628 ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return, is_kprobe);
630 if (ret >= 0 && t != NULL)
631 ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
634 parg->fetch_size.fn = get_fetch_size_function(parg->type,
637 parg->fetch_size.data = parg->fetch.data;
643 /* Return 1 if name is reserved or already used by another argument */
644 int traceprobe_conflict_field_name(const char *name,
645 struct probe_arg *args, int narg)
649 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
650 if (strcmp(reserved_field_names[i], name) == 0)
653 for (i = 0; i < narg; i++)
654 if (strcmp(args[i].name, name) == 0)
660 void traceprobe_update_arg(struct probe_arg *arg)
662 if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
663 update_bitfield_fetch_param(arg->fetch.data);
664 else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
665 update_deref_fetch_param(arg->fetch.data);
666 else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
667 update_symbol_cache(arg->fetch.data);
670 void traceprobe_free_probe_arg(struct probe_arg *arg)
672 if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
673 free_bitfield_fetch_param(arg->fetch.data);
674 else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
675 free_deref_fetch_param(arg->fetch.data);
676 else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
677 free_symbol_cache(arg->fetch.data);
683 int traceprobe_command(const char *buf, int (*createfn)(int, char **))
690 argv = argv_split(GFP_KERNEL, buf, &argc);
695 ret = createfn(argc, argv);
702 #define WRITE_BUFSIZE 4096
704 ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,
705 size_t count, loff_t *ppos,
706 int (*createfn)(int, char **))
713 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
717 while (done < count) {
720 if (size >= WRITE_BUFSIZE)
721 size = WRITE_BUFSIZE - 1;
723 if (copy_from_user(kbuf, buffer + done, size)) {
728 tmp = strchr(kbuf, '\n');
732 size = tmp - kbuf + 1;
733 } else if (done + size < count) {
734 pr_warning("Line length is too long: "
735 "Should be less than %d.", WRITE_BUFSIZE);
740 /* Remove comments */
741 tmp = strchr(kbuf, '#');
746 ret = traceprobe_command(kbuf, createfn);
758 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
764 const char *fmt, *arg;
768 arg = "REC->" FIELD_STRING_IP;
770 fmt = "(%lx <- %lx)";
771 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
774 /* When len=0, we just calculate the needed length */
775 #define LEN_OR_ZERO (len ? len - pos : 0)
777 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
779 for (i = 0; i < tp->nr_args; i++) {
780 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
781 tp->args[i].name, tp->args[i].type->fmt);
784 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
786 for (i = 0; i < tp->nr_args; i++) {
787 if (strcmp(tp->args[i].type->name, "string") == 0)
788 pos += snprintf(buf + pos, LEN_OR_ZERO,
792 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
798 /* return the length of print_fmt */
802 int set_print_fmt(struct trace_probe *tp, bool is_return)
807 /* First: called with 0 length to calculate the needed length */
808 len = __set_print_fmt(tp, NULL, 0, is_return);
809 print_fmt = kmalloc(len + 1, GFP_KERNEL);
813 /* Second: actually write the @print_fmt */
814 __set_print_fmt(tp, print_fmt, len + 1, is_return);
815 tp->call.print_fmt = print_fmt;