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")
54 int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
56 trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
57 return !trace_seq_has_overflowed(s);
59 const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
61 /* Print type function for string type */
62 int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
64 int len = *(u32 *)data >> 16;
67 trace_seq_puts(s, "(fault)");
69 trace_seq_printf(s, "\"%s\"",
70 (const char *)get_loc_data(data, ent));
71 return !trace_seq_has_overflowed(s);
74 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
76 /* Fetch type information table */
77 static const struct fetch_type probe_fetch_types[] = {
79 __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1,
81 __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1,
84 ASSIGN_FETCH_TYPE(u8, u8, 0),
85 ASSIGN_FETCH_TYPE(u16, u16, 0),
86 ASSIGN_FETCH_TYPE(u32, u32, 0),
87 ASSIGN_FETCH_TYPE(u64, u64, 0),
88 ASSIGN_FETCH_TYPE(s8, u8, 1),
89 ASSIGN_FETCH_TYPE(s16, u16, 1),
90 ASSIGN_FETCH_TYPE(s32, u32, 1),
91 ASSIGN_FETCH_TYPE(s64, u64, 1),
92 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
93 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
94 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
95 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
96 ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
101 static const struct fetch_type *find_fetch_type(const char *type)
106 type = DEFAULT_FETCH_TYPE_STR;
108 /* Special case: bitfield */
112 type = strchr(type, '/');
117 if (kstrtoul(type, 0, &bs))
122 return find_fetch_type("u8");
124 return find_fetch_type("u16");
126 return find_fetch_type("u32");
128 return find_fetch_type("u64");
134 for (i = 0; probe_fetch_types[i].name; i++) {
135 if (strcmp(type, probe_fetch_types[i].name) == 0)
136 return &probe_fetch_types[i];
143 static struct trace_probe_log trace_probe_log;
145 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
147 trace_probe_log.subsystem = subsystem;
148 trace_probe_log.argc = argc;
149 trace_probe_log.argv = argv;
150 trace_probe_log.index = 0;
153 void trace_probe_log_clear(void)
155 memset(&trace_probe_log, 0, sizeof(trace_probe_log));
158 void trace_probe_log_set_index(int index)
160 trace_probe_log.index = index;
163 void __trace_probe_log_err(int offset, int err_type)
166 int i, len = 0, pos = 0;
168 if (!trace_probe_log.argv)
171 /* Recalcurate the length and allocate buffer */
172 for (i = 0; i < trace_probe_log.argc; i++) {
173 if (i == trace_probe_log.index)
175 len += strlen(trace_probe_log.argv[i]) + 1;
177 command = kzalloc(len, GFP_KERNEL);
181 if (trace_probe_log.index >= trace_probe_log.argc) {
183 * Set the error position is next to the last arg + space.
184 * Note that len includes the terminal null and the cursor
185 * appaers at pos + 1.
191 /* And make a command string from argv array */
193 for (i = 0; i < trace_probe_log.argc; i++) {
194 len = strlen(trace_probe_log.argv[i]);
195 strcpy(p, trace_probe_log.argv[i]);
201 tracing_log_err(NULL, trace_probe_log.subsystem, command,
202 trace_probe_err_text, err_type, pos + offset);
207 /* Split symbol and offset. */
208 int traceprobe_split_symbol_offset(char *symbol, long *offset)
216 tmp = strpbrk(symbol, "+-");
218 ret = kstrtol(tmp, 0, offset);
228 /* @buf must has MAX_EVENT_NAME_LEN size */
229 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
230 char *buf, int offset)
232 const char *slash, *event = *pevent;
235 slash = strchr(event, '/');
237 if (slash == event) {
238 trace_probe_log_err(offset, NO_GROUP_NAME);
241 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
242 trace_probe_log_err(offset, GROUP_TOO_LONG);
245 strlcpy(buf, event, slash - event + 1);
246 if (!is_good_name(buf)) {
247 trace_probe_log_err(offset, BAD_GROUP_NAME);
252 offset += slash - event + 1;
257 trace_probe_log_err(offset, NO_EVENT_NAME);
259 } else if (len > MAX_EVENT_NAME_LEN) {
260 trace_probe_log_err(offset, EVENT_TOO_LONG);
263 if (!is_good_name(event)) {
264 trace_probe_log_err(offset, BAD_EVENT_NAME);
270 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
272 static int parse_probe_vars(char *arg, const struct fetch_type *t,
273 struct fetch_insn *code, unsigned int flags, int offs)
279 if (strcmp(arg, "retval") == 0) {
280 if (flags & TPARG_FL_RETURN) {
281 code->op = FETCH_OP_RETVAL;
283 trace_probe_log_err(offs, RETVAL_ON_PROBE);
286 } else if ((len = str_has_prefix(arg, "stack"))) {
287 if (arg[len] == '\0') {
288 code->op = FETCH_OP_STACKP;
289 } else if (isdigit(arg[len])) {
290 ret = kstrtoul(arg + len, 10, ¶m);
293 } else if ((flags & TPARG_FL_KERNEL) &&
294 param > PARAM_MAX_STACK) {
295 trace_probe_log_err(offs, BAD_STACK_NUM);
298 code->op = FETCH_OP_STACK;
299 code->param = (unsigned int)param;
303 } else if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
304 code->op = FETCH_OP_COMM;
305 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
306 } else if (((flags & TPARG_FL_MASK) ==
307 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
308 (len = str_has_prefix(arg, "arg"))) {
309 ret = kstrtoul(arg + len, 10, ¶m);
312 } else if (!param || param > PARAM_MAX_STACK) {
313 trace_probe_log_err(offs, BAD_ARG_NUM);
316 code->op = FETCH_OP_ARG;
317 code->param = (unsigned int)param - 1;
325 trace_probe_log_err(offs, BAD_VAR);
329 static int str_to_immediate(char *str, unsigned long *imm)
332 return kstrtoul(str, 0, imm);
333 else if (str[0] == '-')
334 return kstrtol(str, 0, (long *)imm);
335 else if (str[0] == '+')
336 return kstrtol(str + 1, 0, (long *)imm);
340 static int __parse_imm_string(char *str, char **pbuf, int offs)
342 size_t len = strlen(str);
344 if (str[len - 1] != '"') {
345 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
348 *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
352 /* Recursive argument parser */
354 parse_probe_arg(char *arg, const struct fetch_type *type,
355 struct fetch_insn **pcode, struct fetch_insn *end,
356 unsigned int flags, int offs)
358 struct fetch_insn *code = *pcode;
360 int deref = FETCH_OP_DEREF;
367 ret = parse_probe_vars(arg + 1, type, code, flags, offs);
370 case '%': /* named register */
371 ret = regs_query_register_offset(arg + 1);
373 code->op = FETCH_OP_REG;
374 code->param = (unsigned int)ret;
377 trace_probe_log_err(offs, BAD_REG_NAME);
380 case '@': /* memory, file-offset or symbol */
381 if (isdigit(arg[1])) {
382 ret = kstrtoul(arg + 1, 0, ¶m);
384 trace_probe_log_err(offs, BAD_MEM_ADDR);
388 code->op = FETCH_OP_IMM;
389 code->immediate = param;
390 } else if (arg[1] == '+') {
391 /* kprobes don't support file offsets */
392 if (flags & TPARG_FL_KERNEL) {
393 trace_probe_log_err(offs, FILE_ON_KPROBE);
396 ret = kstrtol(arg + 2, 0, &offset);
398 trace_probe_log_err(offs, BAD_FILE_OFFS);
402 code->op = FETCH_OP_FOFFS;
403 code->immediate = (unsigned long)offset; // imm64?
405 /* uprobes don't support symbols */
406 if (!(flags & TPARG_FL_KERNEL)) {
407 trace_probe_log_err(offs, SYM_ON_UPROBE);
410 /* Preserve symbol for updating */
411 code->op = FETCH_NOP_SYMBOL;
412 code->data = kstrdup(arg + 1, GFP_KERNEL);
416 trace_probe_log_err(offs, TOO_MANY_OPS);
419 code->op = FETCH_OP_IMM;
422 /* These are fetching from memory */
424 trace_probe_log_err(offs, TOO_MANY_OPS);
428 code->op = FETCH_OP_DEREF;
429 code->offset = offset;
432 case '+': /* deref memory */
435 deref = FETCH_OP_UDEREF;
440 arg++; /* Skip '+', because kstrtol() rejects it. */
441 tmp = strchr(arg, '(');
443 trace_probe_log_err(offs, DEREF_NEED_BRACE);
447 ret = kstrtol(arg, 0, &offset);
449 trace_probe_log_err(offs, BAD_DEREF_OFFS);
452 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
454 tmp = strrchr(arg, ')');
456 trace_probe_log_err(offs + strlen(arg),
460 const struct fetch_type *t2 = find_fetch_type(NULL);
463 ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
466 if (code->op == FETCH_OP_COMM ||
467 code->op == FETCH_OP_DATA) {
468 trace_probe_log_err(offs, COMM_CANT_DEREF);
472 trace_probe_log_err(offs, TOO_MANY_OPS);
478 code->offset = offset;
481 case '\\': /* Immediate value */
482 if (arg[1] == '"') { /* Immediate string */
483 ret = __parse_imm_string(arg + 2, &tmp, offs + 2);
486 code->op = FETCH_OP_DATA;
489 ret = str_to_immediate(arg + 1, &code->immediate);
491 trace_probe_log_err(offs + 1, BAD_IMM);
493 code->op = FETCH_OP_IMM;
497 if (!ret && code->op == FETCH_OP_NOP) {
498 /* Parsed, but do not find fetch method */
499 trace_probe_log_err(offs, BAD_FETCH_ARG);
505 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
507 /* Bitfield type needs to be parsed into a fetch function */
508 static int __parse_bitfield_probe_arg(const char *bf,
509 const struct fetch_type *t,
510 struct fetch_insn **pcode)
512 struct fetch_insn *code = *pcode;
513 unsigned long bw, bo;
519 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
521 if (bw == 0 || *tail != '@')
525 bo = simple_strtoul(bf, &tail, 0);
527 if (tail == bf || *tail != '/')
530 if (code->op != FETCH_OP_NOP)
534 code->op = FETCH_OP_MOD_BF;
535 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
536 code->rshift = BYTES_TO_BITS(t->size) - bw;
537 code->basesize = t->size;
539 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
542 /* String length checking wrapper */
543 static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size,
544 struct probe_arg *parg, unsigned int flags, int offset)
546 struct fetch_insn *code, *scode, *tmp = NULL;
551 if (len > MAX_ARGSTR_LEN) {
552 trace_probe_log_err(offset, ARG_TOO_LONG);
554 } else if (len == 0) {
555 trace_probe_log_err(offset, NO_ARG_BODY);
559 parg->comm = kstrdup(arg, GFP_KERNEL);
563 t = strchr(arg, ':');
566 t2 = strchr(++t, '[');
569 t3 = strchr(t2, ']');
571 offset += t2 + strlen(t2) - arg;
572 trace_probe_log_err(offset,
575 } else if (t3[1] != '\0') {
576 trace_probe_log_err(offset + t3 + 1 - arg,
581 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
582 trace_probe_log_err(offset + t2 - arg,
586 if (parg->count > MAX_ARRAY_LEN) {
587 trace_probe_log_err(offset + t2 - arg,
595 * Since $comm and immediate string can not be dereferred,
596 * we can find those by strcmp.
598 if (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
599 strncmp(arg, "\\\"", 2) == 0) {
600 /* The type of $comm must be "string", and not an array. */
601 if (parg->count || (t && strcmp(t, "string")))
603 parg->type = find_fetch_type("string");
605 parg->type = find_fetch_type(t);
607 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
610 parg->offset = *size;
611 *size += parg->type->size * (parg->count ?: 1);
614 len = strlen(parg->type->fmttype) + 6;
615 parg->fmt = kmalloc(len, GFP_KERNEL);
618 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
622 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
625 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
627 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
632 /* Store operation */
633 if (!strcmp(parg->type->name, "string") ||
634 !strcmp(parg->type->name, "ustring")) {
635 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
636 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
637 code->op != FETCH_OP_DATA) {
638 trace_probe_log_err(offset + (t ? (t - arg) : 0),
643 if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
644 code->op == FETCH_OP_DATA) || parg->count) {
646 * IMM, DATA and COMM is pointing actual address, those
647 * must be kept, and if parg->count != 0, this is an
648 * array of string pointers instead of string address
652 if (code->op != FETCH_OP_NOP) {
653 trace_probe_log_err(offset, TOO_MANY_OPS);
658 /* If op == DEREF, replace it with STRING */
659 if (!strcmp(parg->type->name, "ustring") ||
660 code->op == FETCH_OP_UDEREF)
661 code->op = FETCH_OP_ST_USTRING;
663 code->op = FETCH_OP_ST_STRING;
664 code->size = parg->type->size;
665 parg->dynamic = true;
666 } else if (code->op == FETCH_OP_DEREF) {
667 code->op = FETCH_OP_ST_MEM;
668 code->size = parg->type->size;
669 } else if (code->op == FETCH_OP_UDEREF) {
670 code->op = FETCH_OP_ST_UMEM;
671 code->size = parg->type->size;
674 if (code->op != FETCH_OP_NOP) {
675 trace_probe_log_err(offset, TOO_MANY_OPS);
679 code->op = FETCH_OP_ST_RAW;
680 code->size = parg->type->size;
683 /* Modify operation */
685 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
687 trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
691 /* Loop(Array) operation */
693 if (scode->op != FETCH_OP_ST_MEM &&
694 scode->op != FETCH_OP_ST_STRING &&
695 scode->op != FETCH_OP_ST_USTRING) {
696 trace_probe_log_err(offset + (t ? (t - arg) : 0),
702 if (code->op != FETCH_OP_NOP) {
703 trace_probe_log_err(offset, TOO_MANY_OPS);
707 code->op = FETCH_OP_LP_ARRAY;
708 code->param = parg->count;
711 code->op = FETCH_OP_END;
713 /* Shrink down the code buffer */
714 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
718 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
722 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
723 if (code->op == FETCH_NOP_SYMBOL ||
724 code->op == FETCH_OP_DATA)
732 /* Return 1 if name is reserved or already used by another argument */
733 static int traceprobe_conflict_field_name(const char *name,
734 struct probe_arg *args, int narg)
738 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
739 if (strcmp(reserved_field_names[i], name) == 0)
742 for (i = 0; i < narg; i++)
743 if (strcmp(args[i].name, name) == 0)
749 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, char *arg,
752 struct probe_arg *parg = &tp->args[i];
755 /* Increment count for freeing args in error case */
758 body = strchr(arg, '=');
760 if (body - arg > MAX_ARG_NAME_LEN) {
761 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
763 } else if (body == arg) {
764 trace_probe_log_err(0, NO_ARG_NAME);
767 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
770 /* If argument name is omitted, set "argN" */
771 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
777 if (!is_good_name(parg->name)) {
778 trace_probe_log_err(0, BAD_ARG_NAME);
781 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
782 trace_probe_log_err(0, USED_ARG_NAME);
785 /* Parse fetch argument */
786 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
790 void traceprobe_free_probe_arg(struct probe_arg *arg)
792 struct fetch_insn *code = arg->code;
794 while (code && code->op != FETCH_OP_END) {
795 if (code->op == FETCH_NOP_SYMBOL ||
796 code->op == FETCH_OP_DATA)
806 int traceprobe_update_arg(struct probe_arg *arg)
808 struct fetch_insn *code = arg->code;
814 while (code && code->op != FETCH_OP_END) {
815 if (code->op == FETCH_NOP_SYMBOL) {
816 if (code[1].op != FETCH_OP_IMM)
819 tmp = strpbrk(code->data, "+-");
822 ret = traceprobe_split_symbol_offset(code->data,
828 (unsigned long)kallsyms_lookup_name(code->data);
831 if (!code[1].immediate)
833 code[1].immediate += offset;
840 /* When len=0, we just calculate the needed length */
841 #define LEN_OR_ZERO (len ? len - pos : 0)
842 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
845 struct probe_arg *parg;
848 const char *fmt, *arg;
852 arg = "REC->" FIELD_STRING_IP;
854 fmt = "(%lx <- %lx)";
855 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
858 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
860 for (i = 0; i < tp->nr_args; i++) {
862 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
864 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
866 for (j = 1; j < parg->count; j++)
867 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
869 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
871 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
875 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
877 for (i = 0; i < tp->nr_args; i++) {
880 if ((strcmp(parg->type->name, "string") == 0) ||
881 (strcmp(parg->type->name, "ustring") == 0))
882 fmt = ", __get_str(%s[%d])";
884 fmt = ", REC->%s[%d]";
885 for (j = 0; j < parg->count; j++)
886 pos += snprintf(buf + pos, LEN_OR_ZERO,
889 if ((strcmp(parg->type->name, "string") == 0) ||
890 (strcmp(parg->type->name, "ustring") == 0))
891 fmt = ", __get_str(%s)";
894 pos += snprintf(buf + pos, LEN_OR_ZERO,
899 /* return the length of print_fmt */
904 int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return)
906 struct trace_event_call *call = trace_probe_event_call(tp);
910 /* First: called with 0 length to calculate the needed length */
911 len = __set_print_fmt(tp, NULL, 0, is_return);
912 print_fmt = kmalloc(len + 1, GFP_KERNEL);
916 /* Second: actually write the @print_fmt */
917 __set_print_fmt(tp, print_fmt, len + 1, is_return);
918 call->print_fmt = print_fmt;
923 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
924 size_t offset, struct trace_probe *tp)
928 /* Set argument names as fields */
929 for (i = 0; i < tp->nr_args; i++) {
930 struct probe_arg *parg = &tp->args[i];
931 const char *fmt = parg->type->fmttype;
932 int size = parg->type->size;
938 ret = trace_define_field(event_call, fmt, parg->name,
939 offset + parg->offset, size,
940 parg->type->is_signed,
948 static void trace_probe_event_free(struct trace_probe_event *tpe)
950 kfree(tpe->class.system);
951 kfree(tpe->call.name);
952 kfree(tpe->call.print_fmt);
956 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
958 if (trace_probe_has_sibling(tp))
961 list_del_init(&tp->list);
962 trace_probe_event_free(tp->event);
964 tp->event = to->event;
965 list_add_tail(&tp->list, trace_probe_probe_list(to));
970 void trace_probe_unlink(struct trace_probe *tp)
972 list_del_init(&tp->list);
973 if (list_empty(trace_probe_probe_list(tp)))
974 trace_probe_event_free(tp->event);
978 void trace_probe_cleanup(struct trace_probe *tp)
982 for (i = 0; i < tp->nr_args; i++)
983 traceprobe_free_probe_arg(&tp->args[i]);
986 trace_probe_unlink(tp);
989 int trace_probe_init(struct trace_probe *tp, const char *event,
990 const char *group, bool alloc_filter)
992 struct trace_event_call *call;
993 size_t size = sizeof(struct trace_probe_event);
996 if (!event || !group)
1000 size += sizeof(struct trace_uprobe_filter);
1002 tp->event = kzalloc(size, GFP_KERNEL);
1006 INIT_LIST_HEAD(&tp->event->files);
1007 INIT_LIST_HEAD(&tp->event->class.fields);
1008 INIT_LIST_HEAD(&tp->event->probes);
1009 INIT_LIST_HEAD(&tp->list);
1010 list_add(&tp->list, &tp->event->probes);
1012 call = trace_probe_event_call(tp);
1013 call->class = &tp->event->class;
1014 call->name = kstrdup(event, GFP_KERNEL);
1020 tp->event->class.system = kstrdup(group, GFP_KERNEL);
1021 if (!tp->event->class.system) {
1029 trace_probe_cleanup(tp);
1033 static struct trace_event_call *
1034 find_trace_event_call(const char *system, const char *event_name)
1036 struct trace_event_call *tp_event;
1039 list_for_each_entry(tp_event, &ftrace_events, list) {
1040 if (!tp_event->class->system ||
1041 strcmp(system, tp_event->class->system))
1043 name = trace_event_name(tp_event);
1044 if (!name || strcmp(event_name, name))
1052 int trace_probe_register_event_call(struct trace_probe *tp)
1054 struct trace_event_call *call = trace_probe_event_call(tp);
1057 lockdep_assert_held(&event_mutex);
1059 if (find_trace_event_call(trace_probe_group_name(tp),
1060 trace_probe_name(tp)))
1063 ret = register_trace_event(&call->event);
1067 ret = trace_add_event_call(call);
1069 unregister_trace_event(&call->event);
1074 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1076 struct event_file_link *link;
1078 link = kmalloc(sizeof(*link), GFP_KERNEL);
1083 INIT_LIST_HEAD(&link->list);
1084 list_add_tail_rcu(&link->list, &tp->event->files);
1085 trace_probe_set_flag(tp, TP_FLAG_TRACE);
1089 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1090 struct trace_event_file *file)
1092 struct event_file_link *link;
1094 trace_probe_for_each_link(link, tp) {
1095 if (link->file == file)
1102 int trace_probe_remove_file(struct trace_probe *tp,
1103 struct trace_event_file *file)
1105 struct event_file_link *link;
1107 link = trace_probe_get_file_link(tp, file);
1111 list_del_rcu(&link->list);
1115 if (list_empty(&tp->event->files))
1116 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
1122 * Return the smallest index of different type argument (start from 1).
1123 * If all argument types and name are same, return 0.
1125 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1129 /* In case of more arguments */
1130 if (a->nr_args < b->nr_args)
1131 return a->nr_args + 1;
1132 if (a->nr_args > b->nr_args)
1133 return b->nr_args + 1;
1135 for (i = 0; i < a->nr_args; i++) {
1136 if ((b->nr_args <= i) ||
1137 ((a->args[i].type != b->args[i].type) ||
1138 (a->args[i].count != b->args[i].count) ||
1139 strcmp(a->args[i].name, b->args[i].name)))
1146 bool trace_probe_match_command_args(struct trace_probe *tp,
1147 int argc, const char **argv)
1149 char buf[MAX_ARGSTR_LEN + 1];
1152 if (tp->nr_args < argc)
1155 for (i = 0; i < argc; i++) {
1156 snprintf(buf, sizeof(buf), "%s=%s",
1157 tp->args[i].name, tp->args[i].comm);
1158 if (strcmp(buf, argv[i]))