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"
19 static const char *trace_probe_err_text[] = { ERRORS };
21 static const char *reserved_field_names[] = {
24 "common_preempt_count",
32 /* Printing in basic type function template */
33 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
34 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
36 trace_seq_printf(s, fmt, *(type *)data); \
37 return !trace_seq_has_overflowed(s); \
39 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
41 DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u")
42 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
43 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
44 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
45 DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
48 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
49 DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
53 DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8, "'%c'")
55 int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
57 trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
58 return !trace_seq_has_overflowed(s);
60 const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
62 /* Print type function for string type */
63 int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
65 int len = *(u32 *)data >> 16;
68 trace_seq_puts(s, "(fault)");
70 trace_seq_printf(s, "\"%s\"",
71 (const char *)get_loc_data(data, ent));
72 return !trace_seq_has_overflowed(s);
75 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
77 /* Fetch type information table */
78 static const struct fetch_type probe_fetch_types[] = {
80 __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, 1,
82 __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, 1,
84 __ASSIGN_FETCH_TYPE("symstr", string, string, sizeof(u32), 1, 1,
87 ASSIGN_FETCH_TYPE(u8, u8, 0),
88 ASSIGN_FETCH_TYPE(u16, u16, 0),
89 ASSIGN_FETCH_TYPE(u32, u32, 0),
90 ASSIGN_FETCH_TYPE(u64, u64, 0),
91 ASSIGN_FETCH_TYPE(s8, u8, 1),
92 ASSIGN_FETCH_TYPE(s16, u16, 1),
93 ASSIGN_FETCH_TYPE(s32, u32, 1),
94 ASSIGN_FETCH_TYPE(s64, u64, 1),
95 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
96 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
97 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
98 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
99 ASSIGN_FETCH_TYPE_ALIAS(char, u8, u8, 0),
100 ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
102 ASSIGN_FETCH_TYPE_END
105 static const struct fetch_type *find_fetch_type(const char *type, unsigned long flags)
109 /* Reject the symbol/symstr for uprobes */
110 if (type && (flags & TPARG_FL_USER) &&
111 (!strcmp(type, "symbol") || !strcmp(type, "symstr")))
115 type = DEFAULT_FETCH_TYPE_STR;
117 /* Special case: bitfield */
121 type = strchr(type, '/');
126 if (kstrtoul(type, 0, &bs))
131 return find_fetch_type("u8", flags);
133 return find_fetch_type("u16", flags);
135 return find_fetch_type("u32", flags);
137 return find_fetch_type("u64", flags);
143 for (i = 0; probe_fetch_types[i].name; i++) {
144 if (strcmp(type, probe_fetch_types[i].name) == 0)
145 return &probe_fetch_types[i];
152 static struct trace_probe_log trace_probe_log;
154 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
156 trace_probe_log.subsystem = subsystem;
157 trace_probe_log.argc = argc;
158 trace_probe_log.argv = argv;
159 trace_probe_log.index = 0;
162 void trace_probe_log_clear(void)
164 memset(&trace_probe_log, 0, sizeof(trace_probe_log));
167 void trace_probe_log_set_index(int index)
169 trace_probe_log.index = index;
172 void __trace_probe_log_err(int offset, int err_type)
175 int i, len = 0, pos = 0;
177 if (!trace_probe_log.argv)
180 /* Recalculate the length and allocate buffer */
181 for (i = 0; i < trace_probe_log.argc; i++) {
182 if (i == trace_probe_log.index)
184 len += strlen(trace_probe_log.argv[i]) + 1;
186 command = kzalloc(len, GFP_KERNEL);
190 if (trace_probe_log.index >= trace_probe_log.argc) {
192 * Set the error position is next to the last arg + space.
193 * Note that len includes the terminal null and the cursor
194 * appears at pos + 1.
200 /* And make a command string from argv array */
202 for (i = 0; i < trace_probe_log.argc; i++) {
203 len = strlen(trace_probe_log.argv[i]);
204 strcpy(p, trace_probe_log.argv[i]);
210 tracing_log_err(NULL, trace_probe_log.subsystem, command,
211 trace_probe_err_text, err_type, pos + offset);
216 /* Split symbol and offset. */
217 int traceprobe_split_symbol_offset(char *symbol, long *offset)
225 tmp = strpbrk(symbol, "+-");
227 ret = kstrtol(tmp, 0, offset);
237 /* @buf must has MAX_EVENT_NAME_LEN size */
238 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
239 char *buf, int offset)
241 const char *slash, *event = *pevent;
244 slash = strchr(event, '/');
246 slash = strchr(event, '.');
249 if (slash == event) {
250 trace_probe_log_err(offset, NO_GROUP_NAME);
253 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
254 trace_probe_log_err(offset, GROUP_TOO_LONG);
257 strlcpy(buf, event, slash - event + 1);
258 if (!is_good_system_name(buf)) {
259 trace_probe_log_err(offset, BAD_GROUP_NAME);
264 offset += slash - event + 1;
273 trace_probe_log_err(offset, NO_EVENT_NAME);
275 } else if (len > MAX_EVENT_NAME_LEN) {
276 trace_probe_log_err(offset, EVENT_TOO_LONG);
279 if (!is_good_name(event)) {
280 trace_probe_log_err(offset, BAD_EVENT_NAME);
286 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
288 static int parse_probe_vars(char *arg, const struct fetch_type *t,
289 struct fetch_insn *code, unsigned int flags, int offs)
295 if (flags & TPARG_FL_TPOINT) {
298 code->data = kstrdup(arg, GFP_KERNEL);
301 code->op = FETCH_OP_TP_ARG;
302 } else if (strcmp(arg, "retval") == 0) {
303 if (flags & TPARG_FL_RETURN) {
304 code->op = FETCH_OP_RETVAL;
306 trace_probe_log_err(offs, RETVAL_ON_PROBE);
309 } else if ((len = str_has_prefix(arg, "stack"))) {
310 if (arg[len] == '\0') {
311 code->op = FETCH_OP_STACKP;
312 } else if (isdigit(arg[len])) {
313 ret = kstrtoul(arg + len, 10, ¶m);
316 } else if ((flags & TPARG_FL_KERNEL) &&
317 param > PARAM_MAX_STACK) {
318 trace_probe_log_err(offs, BAD_STACK_NUM);
321 code->op = FETCH_OP_STACK;
322 code->param = (unsigned int)param;
326 } else if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
327 code->op = FETCH_OP_COMM;
328 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
329 } else if (((flags & TPARG_FL_MASK) ==
330 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
331 (len = str_has_prefix(arg, "arg"))) {
332 ret = kstrtoul(arg + len, 10, ¶m);
335 } else if (!param || param > PARAM_MAX_STACK) {
336 trace_probe_log_err(offs, BAD_ARG_NUM);
339 code->op = FETCH_OP_ARG;
340 code->param = (unsigned int)param - 1;
348 trace_probe_log_err(offs, BAD_VAR);
352 static int str_to_immediate(char *str, unsigned long *imm)
355 return kstrtoul(str, 0, imm);
356 else if (str[0] == '-')
357 return kstrtol(str, 0, (long *)imm);
358 else if (str[0] == '+')
359 return kstrtol(str + 1, 0, (long *)imm);
363 static int __parse_imm_string(char *str, char **pbuf, int offs)
365 size_t len = strlen(str);
367 if (str[len - 1] != '"') {
368 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
371 *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
377 /* Recursive argument parser */
379 parse_probe_arg(char *arg, const struct fetch_type *type,
380 struct fetch_insn **pcode, struct fetch_insn *end,
381 unsigned int flags, int offs)
383 struct fetch_insn *code = *pcode;
385 int deref = FETCH_OP_DEREF;
392 ret = parse_probe_vars(arg + 1, type, code, flags, offs);
395 case '%': /* named register */
396 if (flags & TPARG_FL_TPOINT) {
397 /* eprobes do not handle registers */
398 trace_probe_log_err(offs, BAD_VAR);
401 ret = regs_query_register_offset(arg + 1);
403 code->op = FETCH_OP_REG;
404 code->param = (unsigned int)ret;
407 trace_probe_log_err(offs, BAD_REG_NAME);
410 case '@': /* memory, file-offset or symbol */
411 if (isdigit(arg[1])) {
412 ret = kstrtoul(arg + 1, 0, ¶m);
414 trace_probe_log_err(offs, BAD_MEM_ADDR);
418 code->op = FETCH_OP_IMM;
419 code->immediate = param;
420 } else if (arg[1] == '+') {
421 /* kprobes don't support file offsets */
422 if (flags & TPARG_FL_KERNEL) {
423 trace_probe_log_err(offs, FILE_ON_KPROBE);
426 ret = kstrtol(arg + 2, 0, &offset);
428 trace_probe_log_err(offs, BAD_FILE_OFFS);
432 code->op = FETCH_OP_FOFFS;
433 code->immediate = (unsigned long)offset; // imm64?
435 /* uprobes don't support symbols */
436 if (!(flags & TPARG_FL_KERNEL)) {
437 trace_probe_log_err(offs, SYM_ON_UPROBE);
440 /* Preserve symbol for updating */
441 code->op = FETCH_NOP_SYMBOL;
442 code->data = kstrdup(arg + 1, GFP_KERNEL);
446 trace_probe_log_err(offs, TOO_MANY_OPS);
449 code->op = FETCH_OP_IMM;
452 /* These are fetching from memory */
454 trace_probe_log_err(offs, TOO_MANY_OPS);
458 code->op = FETCH_OP_DEREF;
459 code->offset = offset;
462 case '+': /* deref memory */
465 deref = FETCH_OP_UDEREF;
470 arg++; /* Skip '+', because kstrtol() rejects it. */
471 tmp = strchr(arg, '(');
473 trace_probe_log_err(offs, DEREF_NEED_BRACE);
477 ret = kstrtol(arg, 0, &offset);
479 trace_probe_log_err(offs, BAD_DEREF_OFFS);
482 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
484 tmp = strrchr(arg, ')');
486 trace_probe_log_err(offs + strlen(arg),
490 const struct fetch_type *t2 = find_fetch_type(NULL, flags);
493 ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
496 if (code->op == FETCH_OP_COMM ||
497 code->op == FETCH_OP_DATA) {
498 trace_probe_log_err(offs, COMM_CANT_DEREF);
502 trace_probe_log_err(offs, TOO_MANY_OPS);
508 code->offset = offset;
511 case '\\': /* Immediate value */
512 if (arg[1] == '"') { /* Immediate string */
513 ret = __parse_imm_string(arg + 2, &tmp, offs + 2);
516 code->op = FETCH_OP_DATA;
519 ret = str_to_immediate(arg + 1, &code->immediate);
521 trace_probe_log_err(offs + 1, BAD_IMM);
523 code->op = FETCH_OP_IMM;
527 if (!ret && code->op == FETCH_OP_NOP) {
528 /* Parsed, but do not find fetch method */
529 trace_probe_log_err(offs, BAD_FETCH_ARG);
535 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
537 /* Bitfield type needs to be parsed into a fetch function */
538 static int __parse_bitfield_probe_arg(const char *bf,
539 const struct fetch_type *t,
540 struct fetch_insn **pcode)
542 struct fetch_insn *code = *pcode;
543 unsigned long bw, bo;
549 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
551 if (bw == 0 || *tail != '@')
555 bo = simple_strtoul(bf, &tail, 0);
557 if (tail == bf || *tail != '/')
560 if (code->op != FETCH_OP_NOP)
564 code->op = FETCH_OP_MOD_BF;
565 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
566 code->rshift = BYTES_TO_BITS(t->size) - bw;
567 code->basesize = t->size;
569 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
572 /* String length checking wrapper */
573 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
574 struct probe_arg *parg, unsigned int flags, int offset)
576 struct fetch_insn *code, *scode, *tmp = NULL;
581 arg = kstrdup(argv, GFP_KERNEL);
587 if (len > MAX_ARGSTR_LEN) {
588 trace_probe_log_err(offset, ARG_TOO_LONG);
590 } else if (len == 0) {
591 trace_probe_log_err(offset, NO_ARG_BODY);
596 parg->comm = kstrdup(arg, GFP_KERNEL);
601 t = strchr(arg, ':');
604 t2 = strchr(++t, '[');
607 t3 = strchr(t2, ']');
609 offset += t2 + strlen(t2) - arg;
610 trace_probe_log_err(offset,
613 } else if (t3[1] != '\0') {
614 trace_probe_log_err(offset + t3 + 1 - arg,
619 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
620 trace_probe_log_err(offset + t2 - arg,
624 if (parg->count > MAX_ARRAY_LEN) {
625 trace_probe_log_err(offset + t2 - arg,
633 * Since $comm and immediate string can not be dereferenced,
634 * we can find those by strcmp. But ignore for eprobes.
636 if (!(flags & TPARG_FL_TPOINT) &&
637 (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
638 strncmp(arg, "\\\"", 2) == 0)) {
639 /* The type of $comm must be "string", and not an array. */
640 if (parg->count || (t && strcmp(t, "string")))
642 parg->type = find_fetch_type("string", flags);
644 parg->type = find_fetch_type(t, flags);
646 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
649 parg->offset = *size;
650 *size += parg->type->size * (parg->count ?: 1);
654 len = strlen(parg->type->fmttype) + 6;
655 parg->fmt = kmalloc(len, GFP_KERNEL);
658 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
662 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
665 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
667 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
673 /* Store operation */
674 if (parg->type->is_string) {
675 if (!strcmp(parg->type->name, "symstr")) {
676 if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
677 code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
678 code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
679 trace_probe_log_err(offset + (t ? (t - arg) : 0),
684 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
685 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
686 code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
687 trace_probe_log_err(offset + (t ? (t - arg) : 0),
692 if (!strcmp(parg->type->name, "symstr") ||
693 (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
694 code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
697 * IMM, DATA and COMM is pointing actual address, those
698 * must be kept, and if parg->count != 0, this is an
699 * array of string pointers instead of string address
701 * For the symstr, it doesn't need to dereference, thus
702 * it just get the value.
705 if (code->op != FETCH_OP_NOP) {
706 trace_probe_log_err(offset, TOO_MANY_OPS);
710 /* If op == DEREF, replace it with STRING */
711 if (!strcmp(parg->type->name, "ustring") ||
712 code->op == FETCH_OP_UDEREF)
713 code->op = FETCH_OP_ST_USTRING;
714 else if (!strcmp(parg->type->name, "symstr"))
715 code->op = FETCH_OP_ST_SYMSTR;
717 code->op = FETCH_OP_ST_STRING;
718 code->size = parg->type->size;
719 parg->dynamic = true;
720 } else if (code->op == FETCH_OP_DEREF) {
721 code->op = FETCH_OP_ST_MEM;
722 code->size = parg->type->size;
723 } else if (code->op == FETCH_OP_UDEREF) {
724 code->op = FETCH_OP_ST_UMEM;
725 code->size = parg->type->size;
728 if (code->op != FETCH_OP_NOP) {
729 trace_probe_log_err(offset, TOO_MANY_OPS);
732 code->op = FETCH_OP_ST_RAW;
733 code->size = parg->type->size;
736 /* Modify operation */
738 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
740 trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
745 /* Loop(Array) operation */
747 if (scode->op != FETCH_OP_ST_MEM &&
748 scode->op != FETCH_OP_ST_STRING &&
749 scode->op != FETCH_OP_ST_USTRING) {
750 trace_probe_log_err(offset + (t ? (t - arg) : 0),
755 if (code->op != FETCH_OP_NOP) {
756 trace_probe_log_err(offset, TOO_MANY_OPS);
759 code->op = FETCH_OP_LP_ARRAY;
760 code->param = parg->count;
763 code->op = FETCH_OP_END;
766 /* Shrink down the code buffer */
767 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
771 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
775 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
776 if (code->op == FETCH_NOP_SYMBOL ||
777 code->op == FETCH_OP_DATA)
787 /* Return 1 if name is reserved or already used by another argument */
788 static int traceprobe_conflict_field_name(const char *name,
789 struct probe_arg *args, int narg)
793 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
794 if (strcmp(reserved_field_names[i], name) == 0)
797 for (i = 0; i < narg; i++)
798 if (strcmp(args[i].name, name) == 0)
804 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
807 struct probe_arg *parg = &tp->args[i];
810 /* Increment count for freeing args in error case */
813 body = strchr(arg, '=');
815 if (body - arg > MAX_ARG_NAME_LEN) {
816 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
818 } else if (body == arg) {
819 trace_probe_log_err(0, NO_ARG_NAME);
822 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
825 /* If argument name is omitted, set "argN" */
826 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
832 if (!is_good_name(parg->name)) {
833 trace_probe_log_err(0, BAD_ARG_NAME);
836 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
837 trace_probe_log_err(0, USED_ARG_NAME);
840 /* Parse fetch argument */
841 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
845 void traceprobe_free_probe_arg(struct probe_arg *arg)
847 struct fetch_insn *code = arg->code;
849 while (code && code->op != FETCH_OP_END) {
850 if (code->op == FETCH_NOP_SYMBOL ||
851 code->op == FETCH_OP_DATA)
861 int traceprobe_update_arg(struct probe_arg *arg)
863 struct fetch_insn *code = arg->code;
869 while (code && code->op != FETCH_OP_END) {
870 if (code->op == FETCH_NOP_SYMBOL) {
871 if (code[1].op != FETCH_OP_IMM)
874 tmp = strpbrk(code->data, "+-");
877 ret = traceprobe_split_symbol_offset(code->data,
883 (unsigned long)kallsyms_lookup_name(code->data);
886 if (!code[1].immediate)
888 code[1].immediate += offset;
895 /* When len=0, we just calculate the needed length */
896 #define LEN_OR_ZERO (len ? len - pos : 0)
897 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
898 enum probe_print_type ptype)
900 struct probe_arg *parg;
903 const char *fmt, *arg;
906 case PROBE_PRINT_NORMAL:
908 arg = ", REC->" FIELD_STRING_IP;
910 case PROBE_PRINT_RETURN:
911 fmt = "(%lx <- %lx)";
912 arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
914 case PROBE_PRINT_EVENT:
923 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
925 for (i = 0; i < tp->nr_args; i++) {
927 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
929 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
931 for (j = 1; j < parg->count; j++)
932 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
934 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
936 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
940 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg);
942 for (i = 0; i < tp->nr_args; i++) {
945 if (parg->type->is_string)
946 fmt = ", __get_str(%s[%d])";
948 fmt = ", REC->%s[%d]";
949 for (j = 0; j < parg->count; j++)
950 pos += snprintf(buf + pos, LEN_OR_ZERO,
953 if (parg->type->is_string)
954 fmt = ", __get_str(%s)";
957 pos += snprintf(buf + pos, LEN_OR_ZERO,
962 /* return the length of print_fmt */
967 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
969 struct trace_event_call *call = trace_probe_event_call(tp);
973 /* First: called with 0 length to calculate the needed length */
974 len = __set_print_fmt(tp, NULL, 0, ptype);
975 print_fmt = kmalloc(len + 1, GFP_KERNEL);
979 /* Second: actually write the @print_fmt */
980 __set_print_fmt(tp, print_fmt, len + 1, ptype);
981 call->print_fmt = print_fmt;
986 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
987 size_t offset, struct trace_probe *tp)
991 /* Set argument names as fields */
992 for (i = 0; i < tp->nr_args; i++) {
993 struct probe_arg *parg = &tp->args[i];
994 const char *fmt = parg->type->fmttype;
995 int size = parg->type->size;
1000 size *= parg->count;
1001 ret = trace_define_field(event_call, fmt, parg->name,
1002 offset + parg->offset, size,
1003 parg->type->is_signed,
1011 static void trace_probe_event_free(struct trace_probe_event *tpe)
1013 kfree(tpe->class.system);
1014 kfree(tpe->call.name);
1015 kfree(tpe->call.print_fmt);
1019 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
1021 if (trace_probe_has_sibling(tp))
1024 list_del_init(&tp->list);
1025 trace_probe_event_free(tp->event);
1027 tp->event = to->event;
1028 list_add_tail(&tp->list, trace_probe_probe_list(to));
1033 void trace_probe_unlink(struct trace_probe *tp)
1035 list_del_init(&tp->list);
1036 if (list_empty(trace_probe_probe_list(tp)))
1037 trace_probe_event_free(tp->event);
1041 void trace_probe_cleanup(struct trace_probe *tp)
1045 for (i = 0; i < tp->nr_args; i++)
1046 traceprobe_free_probe_arg(&tp->args[i]);
1049 trace_probe_unlink(tp);
1052 int trace_probe_init(struct trace_probe *tp, const char *event,
1053 const char *group, bool alloc_filter)
1055 struct trace_event_call *call;
1056 size_t size = sizeof(struct trace_probe_event);
1059 if (!event || !group)
1063 size += sizeof(struct trace_uprobe_filter);
1065 tp->event = kzalloc(size, GFP_KERNEL);
1069 INIT_LIST_HEAD(&tp->event->files);
1070 INIT_LIST_HEAD(&tp->event->class.fields);
1071 INIT_LIST_HEAD(&tp->event->probes);
1072 INIT_LIST_HEAD(&tp->list);
1073 list_add(&tp->list, &tp->event->probes);
1075 call = trace_probe_event_call(tp);
1076 call->class = &tp->event->class;
1077 call->name = kstrdup(event, GFP_KERNEL);
1083 tp->event->class.system = kstrdup(group, GFP_KERNEL);
1084 if (!tp->event->class.system) {
1092 trace_probe_cleanup(tp);
1096 static struct trace_event_call *
1097 find_trace_event_call(const char *system, const char *event_name)
1099 struct trace_event_call *tp_event;
1102 list_for_each_entry(tp_event, &ftrace_events, list) {
1103 if (!tp_event->class->system ||
1104 strcmp(system, tp_event->class->system))
1106 name = trace_event_name(tp_event);
1107 if (!name || strcmp(event_name, name))
1115 int trace_probe_register_event_call(struct trace_probe *tp)
1117 struct trace_event_call *call = trace_probe_event_call(tp);
1120 lockdep_assert_held(&event_mutex);
1122 if (find_trace_event_call(trace_probe_group_name(tp),
1123 trace_probe_name(tp)))
1126 ret = register_trace_event(&call->event);
1130 ret = trace_add_event_call(call);
1132 unregister_trace_event(&call->event);
1137 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1139 struct event_file_link *link;
1141 link = kmalloc(sizeof(*link), GFP_KERNEL);
1146 INIT_LIST_HEAD(&link->list);
1147 list_add_tail_rcu(&link->list, &tp->event->files);
1148 trace_probe_set_flag(tp, TP_FLAG_TRACE);
1152 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1153 struct trace_event_file *file)
1155 struct event_file_link *link;
1157 trace_probe_for_each_link(link, tp) {
1158 if (link->file == file)
1165 int trace_probe_remove_file(struct trace_probe *tp,
1166 struct trace_event_file *file)
1168 struct event_file_link *link;
1170 link = trace_probe_get_file_link(tp, file);
1174 list_del_rcu(&link->list);
1175 kvfree_rcu_mightsleep(link);
1177 if (list_empty(&tp->event->files))
1178 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
1184 * Return the smallest index of different type argument (start from 1).
1185 * If all argument types and name are same, return 0.
1187 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1191 /* In case of more arguments */
1192 if (a->nr_args < b->nr_args)
1193 return a->nr_args + 1;
1194 if (a->nr_args > b->nr_args)
1195 return b->nr_args + 1;
1197 for (i = 0; i < a->nr_args; i++) {
1198 if ((b->nr_args <= i) ||
1199 ((a->args[i].type != b->args[i].type) ||
1200 (a->args[i].count != b->args[i].count) ||
1201 strcmp(a->args[i].name, b->args[i].name)))
1208 bool trace_probe_match_command_args(struct trace_probe *tp,
1209 int argc, const char **argv)
1211 char buf[MAX_ARGSTR_LEN + 1];
1214 if (tp->nr_args < argc)
1217 for (i = 0; i < argc; i++) {
1218 snprintf(buf, sizeof(buf), "%s=%s",
1219 tp->args[i].name, tp->args[i].comm);
1220 if (strcmp(buf, argv[i]))
1226 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
1228 int argc = 0, ret = 0;
1231 argv = argv_split(GFP_KERNEL, raw_command, &argc);
1236 ret = createfn(argc, (const char **)argv);
1243 int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_args,
1244 u8 *data, void *field)
1249 for (i = 0; i < nr_args; i++) {
1250 struct probe_arg *a = args + i;
1252 trace_seq_printf(s, " %s=", a->name);
1253 if (likely(!a->count)) {
1254 if (!a->type->print(s, data + a->offset, field))
1258 trace_seq_putc(s, '{');
1259 p = data + a->offset;
1260 for (j = 0; j < a->count; j++) {
1261 if (!a->type->print(s, p, field))
1263 trace_seq_putc(s, j == a->count - 1 ? '}' : ',');