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 /* Print type function for string type */
50 int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
52 int len = *(u32 *)data >> 16;
55 trace_seq_puts(s, "(fault)");
57 trace_seq_printf(s, "\"%s\"",
58 (const char *)get_loc_data(data, ent));
59 return !trace_seq_has_overflowed(s);
62 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
64 static const struct fetch_type *find_fetch_type(const char *type,
65 const struct fetch_type *ftbl)
70 type = DEFAULT_FETCH_TYPE_STR;
72 /* Special case: bitfield */
76 type = strchr(type, '/');
81 if (kstrtoul(type, 0, &bs))
86 return find_fetch_type("u8", ftbl);
88 return find_fetch_type("u16", ftbl);
90 return find_fetch_type("u32", ftbl);
92 return find_fetch_type("u64", ftbl);
98 for (i = 0; ftbl[i].name; i++) {
99 if (strcmp(type, ftbl[i].name) == 0)
107 /* Split symbol and offset. */
108 int traceprobe_split_symbol_offset(char *symbol, long *offset)
116 tmp = strpbrk(symbol, "+-");
118 ret = kstrtol(tmp, 0, offset);
128 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
130 static int parse_probe_vars(char *arg, const struct fetch_type *t,
131 struct fetch_insn *code, bool is_return,
137 if (strcmp(arg, "retval") == 0) {
139 code->op = FETCH_OP_RETVAL;
142 } else if (strncmp(arg, "stack", 5) == 0) {
143 if (arg[5] == '\0') {
144 code->op = FETCH_OP_STACKP;
145 } else if (isdigit(arg[5])) {
146 ret = kstrtoul(arg + 5, 10, ¶m);
147 if (ret || (is_kprobe && param > PARAM_MAX_STACK))
150 code->op = FETCH_OP_STACK;
151 code->param = (unsigned int)param;
155 } else if (strcmp(arg, "comm") == 0) {
156 code->op = FETCH_OP_COMM;
163 /* Recursive argument parser */
165 parse_probe_arg(char *arg, const struct fetch_type *type,
166 struct fetch_insn **pcode, struct fetch_insn *end,
167 bool is_return, bool is_kprobe,
168 const struct fetch_type *ftbl)
170 struct fetch_insn *code = *pcode;
178 ret = parse_probe_vars(arg + 1, type, code,
179 is_return, is_kprobe);
182 case '%': /* named register */
183 ret = regs_query_register_offset(arg + 1);
185 code->op = FETCH_OP_REG;
186 code->param = (unsigned int)ret;
191 case '@': /* memory, file-offset or symbol */
192 if (isdigit(arg[1])) {
193 ret = kstrtoul(arg + 1, 0, ¶m);
197 code->op = FETCH_OP_IMM;
198 code->immediate = param;
199 } else if (arg[1] == '+') {
200 /* kprobes don't support file offsets */
204 ret = kstrtol(arg + 2, 0, &offset);
208 code->op = FETCH_OP_FOFFS;
209 code->immediate = (unsigned long)offset; // imm64?
211 /* uprobes don't support symbols */
215 ret = traceprobe_split_symbol_offset(arg + 1, &offset);
219 code->op = FETCH_OP_IMM;
221 (unsigned long)kallsyms_lookup_name(arg + 1);
222 if (!code->immediate)
224 code->immediate += offset;
226 /* These are fetching from memory */
230 code->op = FETCH_OP_DEREF;
231 code->offset = offset;
234 case '+': /* deref memory */
235 arg++; /* Skip '+', because kstrtol() rejects it. */
237 tmp = strchr(arg, '(');
242 ret = kstrtol(arg, 0, &offset);
247 tmp = strrchr(arg, ')');
250 const struct fetch_type *t2;
252 t2 = find_fetch_type(NULL, ftbl);
254 ret = parse_probe_arg(arg, t2, &code, end, is_return,
258 if (code->op == FETCH_OP_COMM)
264 code->op = FETCH_OP_DEREF;
265 code->offset = offset;
269 if (!ret && code->op == FETCH_OP_NOP) {
270 /* Parsed, but do not find fetch method */
276 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
278 /* Bitfield type needs to be parsed into a fetch function */
279 static int __parse_bitfield_probe_arg(const char *bf,
280 const struct fetch_type *t,
281 struct fetch_insn **pcode)
283 struct fetch_insn *code = *pcode;
284 unsigned long bw, bo;
290 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
292 if (bw == 0 || *tail != '@')
296 bo = simple_strtoul(bf, &tail, 0);
298 if (tail == bf || *tail != '/')
301 if (code->op != FETCH_OP_NOP)
305 code->op = FETCH_OP_MOD_BF;
306 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
307 code->rshift = BYTES_TO_BITS(t->size) - bw;
308 code->basesize = t->size;
310 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
313 /* String length checking wrapper */
314 int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
315 struct probe_arg *parg, bool is_return, bool is_kprobe,
316 const struct fetch_type *ftbl)
318 struct fetch_insn *code, *tmp = NULL;
322 if (strlen(arg) > MAX_ARGSTR_LEN) {
323 pr_info("Argument is too long.: %s\n", arg);
326 parg->comm = kstrdup(arg, GFP_KERNEL);
328 pr_info("Failed to allocate memory for command '%s'.\n", arg);
331 t = strchr(parg->comm, ':');
333 arg[t - parg->comm] = '\0';
337 * The default type of $comm should be "string", and it can't be
340 if (!t && strcmp(arg, "$comm") == 0)
342 parg->type = find_fetch_type(t, ftbl);
344 pr_info("Unsupported type: %s\n", t);
347 parg->offset = *size;
348 *size += parg->type->size;
350 code = tmp = kzalloc(sizeof(*code) * FETCH_INSN_MAX, GFP_KERNEL);
353 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
355 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
356 is_return, is_kprobe, ftbl);
360 /* Store operation */
361 if (!strcmp(parg->type->name, "string")) {
362 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_IMM &&
363 code->op != FETCH_OP_COMM) {
364 pr_info("string only accepts memory or address.\n");
368 /* Since IMM or COMM must be the 1st insn, this is safe */
369 if (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM)
371 code->op = FETCH_OP_ST_STRING; /* In DEREF case, replace it */
372 parg->dynamic = true;
373 } else if (code->op == FETCH_OP_DEREF) {
374 code->op = FETCH_OP_ST_MEM;
375 code->size = parg->type->size;
378 if (code->op != FETCH_OP_NOP) {
382 code->op = FETCH_OP_ST_RAW;
383 code->size = parg->type->size;
385 /* Modify operation */
387 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
392 code->op = FETCH_OP_END;
394 /* Shrink down the code buffer */
395 parg->code = kzalloc(sizeof(*code) * (code - tmp + 1), GFP_KERNEL);
399 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
407 /* Return 1 if name is reserved or already used by another argument */
408 int traceprobe_conflict_field_name(const char *name,
409 struct probe_arg *args, int narg)
413 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
414 if (strcmp(reserved_field_names[i], name) == 0)
417 for (i = 0; i < narg; i++)
418 if (strcmp(args[i].name, name) == 0)
424 void traceprobe_free_probe_arg(struct probe_arg *arg)
431 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
437 const char *fmt, *arg;
441 arg = "REC->" FIELD_STRING_IP;
443 fmt = "(%lx <- %lx)";
444 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
447 /* When len=0, we just calculate the needed length */
448 #define LEN_OR_ZERO (len ? len - pos : 0)
450 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
452 for (i = 0; i < tp->nr_args; i++) {
453 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
454 tp->args[i].name, tp->args[i].type->fmt);
457 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
459 for (i = 0; i < tp->nr_args; i++) {
460 if (strcmp(tp->args[i].type->name, "string") == 0)
461 pos += snprintf(buf + pos, LEN_OR_ZERO,
465 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
471 /* return the length of print_fmt */
475 int set_print_fmt(struct trace_probe *tp, bool is_return)
480 /* First: called with 0 length to calculate the needed length */
481 len = __set_print_fmt(tp, NULL, 0, is_return);
482 print_fmt = kmalloc(len + 1, GFP_KERNEL);
486 /* Second: actually write the @print_fmt */
487 __set_print_fmt(tp, print_fmt, len + 1, is_return);
488 tp->call.print_fmt = print_fmt;
493 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
494 size_t offset, struct trace_probe *tp)
498 /* Set argument names as fields */
499 for (i = 0; i < tp->nr_args; i++) {
500 struct probe_arg *parg = &tp->args[i];
502 ret = trace_define_field(event_call, parg->type->fmttype,
504 offset + parg->offset,
506 parg->type->is_signed,