1 // SPDX-License-Identifier: GPL-2.0
3 * Common code for probe-based Dynamic events.
5 * This code was copied from kernel/trace/trace_kprobe.c written by
6 * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
8 * Updates to make this generic:
9 * Copyright (C) IBM Corporation, 2010-2011
10 * Author: Srikar Dronamraju
12 #define pr_fmt(fmt) "trace_probe: " fmt
14 #include "trace_probe.h"
16 const char *reserved_field_names[] = {
19 "common_preempt_count",
27 /* Printing in basic type function template */
28 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
29 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
31 trace_seq_printf(s, fmt, *(type *)data); \
32 return !trace_seq_has_overflowed(s); \
34 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
36 DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u")
37 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
38 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
39 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
40 DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d")
41 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
42 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
43 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
44 DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x")
45 DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
49 int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
51 trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
52 return !trace_seq_has_overflowed(s);
54 const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
56 /* Print type function for string type */
57 int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
59 int len = *(u32 *)data >> 16;
62 trace_seq_puts(s, "(fault)");
64 trace_seq_printf(s, "\"%s\"",
65 (const char *)get_loc_data(data, ent));
66 return !trace_seq_has_overflowed(s);
69 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
71 /* Fetch type information table */
72 static const struct fetch_type probe_fetch_types[] = {
74 __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1,
77 ASSIGN_FETCH_TYPE(u8, u8, 0),
78 ASSIGN_FETCH_TYPE(u16, u16, 0),
79 ASSIGN_FETCH_TYPE(u32, u32, 0),
80 ASSIGN_FETCH_TYPE(u64, u64, 0),
81 ASSIGN_FETCH_TYPE(s8, u8, 1),
82 ASSIGN_FETCH_TYPE(s16, u16, 1),
83 ASSIGN_FETCH_TYPE(s32, u32, 1),
84 ASSIGN_FETCH_TYPE(s64, u64, 1),
85 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
86 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
87 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
88 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
89 ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
94 static const struct fetch_type *find_fetch_type(const char *type)
99 type = DEFAULT_FETCH_TYPE_STR;
101 /* Special case: bitfield */
105 type = strchr(type, '/');
110 if (kstrtoul(type, 0, &bs))
115 return find_fetch_type("u8");
117 return find_fetch_type("u16");
119 return find_fetch_type("u32");
121 return find_fetch_type("u64");
127 for (i = 0; probe_fetch_types[i].name; i++) {
128 if (strcmp(type, probe_fetch_types[i].name) == 0)
129 return &probe_fetch_types[i];
136 /* Split symbol and offset. */
137 int traceprobe_split_symbol_offset(char *symbol, long *offset)
145 tmp = strpbrk(symbol, "+-");
147 ret = kstrtol(tmp, 0, offset);
157 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
159 static int parse_probe_vars(char *arg, const struct fetch_type *t,
160 struct fetch_insn *code, unsigned int flags)
165 if (strcmp(arg, "retval") == 0) {
166 if (flags & TPARG_FL_RETURN)
167 code->op = FETCH_OP_RETVAL;
170 } else if (strncmp(arg, "stack", 5) == 0) {
171 if (arg[5] == '\0') {
172 code->op = FETCH_OP_STACKP;
173 } else if (isdigit(arg[5])) {
174 ret = kstrtoul(arg + 5, 10, ¶m);
175 if (ret || ((flags & TPARG_FL_KERNEL) &&
176 param > PARAM_MAX_STACK))
179 code->op = FETCH_OP_STACK;
180 code->param = (unsigned int)param;
184 } else if (strcmp(arg, "comm") == 0) {
185 code->op = FETCH_OP_COMM;
186 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
187 } else if (((flags & TPARG_FL_MASK) ==
188 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
189 strncmp(arg, "arg", 3) == 0) {
190 if (!isdigit(arg[3]))
192 ret = kstrtoul(arg + 3, 10, ¶m);
193 if (ret || !param || param > PARAM_MAX_STACK)
195 code->op = FETCH_OP_ARG;
196 code->param = (unsigned int)param - 1;
204 /* Recursive argument parser */
206 parse_probe_arg(char *arg, const struct fetch_type *type,
207 struct fetch_insn **pcode, struct fetch_insn *end,
210 struct fetch_insn *code = *pcode;
218 ret = parse_probe_vars(arg + 1, type, code, flags);
221 case '%': /* named register */
222 ret = regs_query_register_offset(arg + 1);
224 code->op = FETCH_OP_REG;
225 code->param = (unsigned int)ret;
230 case '@': /* memory, file-offset or symbol */
231 if (isdigit(arg[1])) {
232 ret = kstrtoul(arg + 1, 0, ¶m);
236 code->op = FETCH_OP_IMM;
237 code->immediate = param;
238 } else if (arg[1] == '+') {
239 /* kprobes don't support file offsets */
240 if (flags & TPARG_FL_KERNEL)
243 ret = kstrtol(arg + 2, 0, &offset);
247 code->op = FETCH_OP_FOFFS;
248 code->immediate = (unsigned long)offset; // imm64?
250 /* uprobes don't support symbols */
251 if (!(flags & TPARG_FL_KERNEL))
254 /* Preserve symbol for updating */
255 code->op = FETCH_NOP_SYMBOL;
256 code->data = kstrdup(arg + 1, GFP_KERNEL);
262 code->op = FETCH_OP_IMM;
265 /* These are fetching from memory */
269 code->op = FETCH_OP_DEREF;
270 code->offset = offset;
273 case '+': /* deref memory */
274 arg++; /* Skip '+', because kstrtol() rejects it. */
276 tmp = strchr(arg, '(');
281 ret = kstrtol(arg, 0, &offset);
286 tmp = strrchr(arg, ')');
289 const struct fetch_type *t2 = find_fetch_type(NULL);
292 ret = parse_probe_arg(arg, t2, &code, end, flags);
295 if (code->op == FETCH_OP_COMM)
301 code->op = FETCH_OP_DEREF;
302 code->offset = offset;
306 if (!ret && code->op == FETCH_OP_NOP) {
307 /* Parsed, but do not find fetch method */
313 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
315 /* Bitfield type needs to be parsed into a fetch function */
316 static int __parse_bitfield_probe_arg(const char *bf,
317 const struct fetch_type *t,
318 struct fetch_insn **pcode)
320 struct fetch_insn *code = *pcode;
321 unsigned long bw, bo;
327 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
329 if (bw == 0 || *tail != '@')
333 bo = simple_strtoul(bf, &tail, 0);
335 if (tail == bf || *tail != '/')
338 if (code->op != FETCH_OP_NOP)
342 code->op = FETCH_OP_MOD_BF;
343 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
344 code->rshift = BYTES_TO_BITS(t->size) - bw;
345 code->basesize = t->size;
347 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
350 /* String length checking wrapper */
351 int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
352 struct probe_arg *parg, unsigned int flags)
354 struct fetch_insn *code, *scode, *tmp = NULL;
358 if (strlen(arg) > MAX_ARGSTR_LEN) {
359 pr_info("Argument is too long.: %s\n", arg);
362 parg->comm = kstrdup(arg, GFP_KERNEL);
364 pr_info("Failed to allocate memory for command '%s'.\n", arg);
367 t = strchr(arg, ':');
370 t2 = strchr(++t, '[');
373 parg->count = simple_strtoul(t2 + 1, &t2, 0);
374 if (strcmp(t2, "]") || parg->count == 0)
376 if (parg->count > MAX_ARRAY_LEN)
381 * The default type of $comm should be "string", and it can't be
384 if (!t && strcmp(arg, "$comm") == 0)
385 parg->type = find_fetch_type("string");
387 parg->type = find_fetch_type(t);
389 pr_info("Unsupported type: %s\n", t);
392 parg->offset = *size;
393 *size += parg->type->size * (parg->count ?: 1);
396 len = strlen(parg->type->fmttype) + 6;
397 parg->fmt = kmalloc(len, GFP_KERNEL);
400 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
404 code = tmp = kzalloc(sizeof(*code) * FETCH_INSN_MAX, GFP_KERNEL);
407 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
409 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
414 /* Store operation */
415 if (!strcmp(parg->type->name, "string")) {
416 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_IMM &&
417 code->op != FETCH_OP_COMM) {
418 pr_info("string only accepts memory or address.\n");
422 if (code->op != FETCH_OP_DEREF || parg->count) {
424 * IMM and COMM is pointing actual address, those must
425 * be kept, and if parg->count != 0, this is an array
426 * of string pointers instead of string address itself.
429 if (code->op != FETCH_OP_NOP) {
434 code->op = FETCH_OP_ST_STRING; /* In DEREF case, replace it */
435 code->size = parg->type->size;
436 parg->dynamic = true;
437 } else if (code->op == FETCH_OP_DEREF) {
438 code->op = FETCH_OP_ST_MEM;
439 code->size = parg->type->size;
442 if (code->op != FETCH_OP_NOP) {
446 code->op = FETCH_OP_ST_RAW;
447 code->size = parg->type->size;
450 /* Modify operation */
452 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
456 /* Loop(Array) operation */
458 if (scode->op != FETCH_OP_ST_MEM &&
459 scode->op != FETCH_OP_ST_STRING) {
460 pr_info("array only accepts memory or address\n");
465 if (code->op != FETCH_OP_NOP) {
469 code->op = FETCH_OP_LP_ARRAY;
470 code->param = parg->count;
473 code->op = FETCH_OP_END;
475 /* Shrink down the code buffer */
476 parg->code = kzalloc(sizeof(*code) * (code - tmp + 1), GFP_KERNEL);
480 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
484 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
485 if (code->op == FETCH_NOP_SYMBOL)
493 /* Return 1 if name is reserved or already used by another argument */
494 int traceprobe_conflict_field_name(const char *name,
495 struct probe_arg *args, int narg)
499 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
500 if (strcmp(reserved_field_names[i], name) == 0)
503 for (i = 0; i < narg; i++)
504 if (strcmp(args[i].name, name) == 0)
510 void traceprobe_free_probe_arg(struct probe_arg *arg)
512 struct fetch_insn *code = arg->code;
514 while (code && code->op != FETCH_OP_END) {
515 if (code->op == FETCH_NOP_SYMBOL)
525 int traceprobe_update_arg(struct probe_arg *arg)
527 struct fetch_insn *code = arg->code;
533 while (code && code->op != FETCH_OP_END) {
534 if (code->op == FETCH_NOP_SYMBOL) {
535 if (code[1].op != FETCH_OP_IMM)
538 tmp = strpbrk(code->data, "+-");
541 ret = traceprobe_split_symbol_offset(code->data,
547 (unsigned long)kallsyms_lookup_name(code->data);
550 if (!code[1].immediate)
552 code[1].immediate += offset;
559 /* When len=0, we just calculate the needed length */
560 #define LEN_OR_ZERO (len ? len - pos : 0)
561 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
564 struct probe_arg *parg;
567 const char *fmt, *arg;
571 arg = "REC->" FIELD_STRING_IP;
573 fmt = "(%lx <- %lx)";
574 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
577 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
579 for (i = 0; i < tp->nr_args; i++) {
581 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
583 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
585 for (j = 1; j < parg->count; j++)
586 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
588 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
590 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
594 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
596 for (i = 0; i < tp->nr_args; i++) {
599 if (strcmp(parg->type->name, "string") == 0)
600 fmt = ", __get_str(%s[%d])";
602 fmt = ", REC->%s[%d]";
603 for (j = 0; j < parg->count; j++)
604 pos += snprintf(buf + pos, LEN_OR_ZERO,
607 if (strcmp(parg->type->name, "string") == 0)
608 fmt = ", __get_str(%s)";
611 pos += snprintf(buf + pos, LEN_OR_ZERO,
616 /* return the length of print_fmt */
621 int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return)
626 /* First: called with 0 length to calculate the needed length */
627 len = __set_print_fmt(tp, NULL, 0, is_return);
628 print_fmt = kmalloc(len + 1, GFP_KERNEL);
632 /* Second: actually write the @print_fmt */
633 __set_print_fmt(tp, print_fmt, len + 1, is_return);
634 tp->call.print_fmt = print_fmt;
639 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
640 size_t offset, struct trace_probe *tp)
644 /* Set argument names as fields */
645 for (i = 0; i < tp->nr_args; i++) {
646 struct probe_arg *parg = &tp->args[i];
647 const char *fmt = parg->type->fmttype;
648 int size = parg->type->size;
654 ret = trace_define_field(event_call, fmt, parg->name,
655 offset + parg->offset, size,
656 parg->type->is_signed,