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 /* And make a command string from argv array */
183 for (i = 0; i < trace_probe_log.argc; i++) {
184 len = strlen(trace_probe_log.argv[i]);
185 strcpy(p, trace_probe_log.argv[i]);
191 tracing_log_err(NULL, trace_probe_log.subsystem, command,
192 trace_probe_err_text, err_type, pos + offset);
197 /* Split symbol and offset. */
198 int traceprobe_split_symbol_offset(char *symbol, long *offset)
206 tmp = strpbrk(symbol, "+-");
208 ret = kstrtol(tmp, 0, offset);
218 /* @buf must has MAX_EVENT_NAME_LEN size */
219 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
220 char *buf, int offset)
222 const char *slash, *event = *pevent;
225 slash = strchr(event, '/');
227 if (slash == event) {
228 trace_probe_log_err(offset, NO_GROUP_NAME);
231 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
232 trace_probe_log_err(offset, GROUP_TOO_LONG);
235 strlcpy(buf, event, slash - event + 1);
236 if (!is_good_name(buf)) {
237 trace_probe_log_err(offset, BAD_GROUP_NAME);
242 offset += slash - event + 1;
247 trace_probe_log_err(offset, NO_EVENT_NAME);
249 } else if (len > MAX_EVENT_NAME_LEN) {
250 trace_probe_log_err(offset, EVENT_TOO_LONG);
253 if (!is_good_name(event)) {
254 trace_probe_log_err(offset, BAD_EVENT_NAME);
260 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
262 static int parse_probe_vars(char *arg, const struct fetch_type *t,
263 struct fetch_insn *code, unsigned int flags, int offs)
269 if (strcmp(arg, "retval") == 0) {
270 if (flags & TPARG_FL_RETURN) {
271 code->op = FETCH_OP_RETVAL;
273 trace_probe_log_err(offs, RETVAL_ON_PROBE);
276 } else if ((len = str_has_prefix(arg, "stack"))) {
277 if (arg[len] == '\0') {
278 code->op = FETCH_OP_STACKP;
279 } else if (isdigit(arg[len])) {
280 ret = kstrtoul(arg + len, 10, ¶m);
283 } else if ((flags & TPARG_FL_KERNEL) &&
284 param > PARAM_MAX_STACK) {
285 trace_probe_log_err(offs, BAD_STACK_NUM);
288 code->op = FETCH_OP_STACK;
289 code->param = (unsigned int)param;
293 } else if (strcmp(arg, "comm") == 0) {
294 code->op = FETCH_OP_COMM;
295 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
296 } else if (((flags & TPARG_FL_MASK) ==
297 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
298 (len = str_has_prefix(arg, "arg"))) {
299 ret = kstrtoul(arg + len, 10, ¶m);
302 } else if (!param || param > PARAM_MAX_STACK) {
303 trace_probe_log_err(offs, BAD_ARG_NUM);
306 code->op = FETCH_OP_ARG;
307 code->param = (unsigned int)param - 1;
315 trace_probe_log_err(offs, BAD_VAR);
319 static int str_to_immediate(char *str, unsigned long *imm)
322 return kstrtoul(str, 0, imm);
323 else if (str[0] == '-')
324 return kstrtol(str, 0, (long *)imm);
325 else if (str[0] == '+')
326 return kstrtol(str + 1, 0, (long *)imm);
330 static int __parse_imm_string(char *str, char **pbuf, int offs)
332 size_t len = strlen(str);
334 if (str[len - 1] != '"') {
335 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
338 *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
342 /* Recursive argument parser */
344 parse_probe_arg(char *arg, const struct fetch_type *type,
345 struct fetch_insn **pcode, struct fetch_insn *end,
346 unsigned int flags, int offs)
348 struct fetch_insn *code = *pcode;
350 int deref = FETCH_OP_DEREF;
357 ret = parse_probe_vars(arg + 1, type, code, flags, offs);
360 case '%': /* named register */
361 ret = regs_query_register_offset(arg + 1);
363 code->op = FETCH_OP_REG;
364 code->param = (unsigned int)ret;
367 trace_probe_log_err(offs, BAD_REG_NAME);
370 case '@': /* memory, file-offset or symbol */
371 if (isdigit(arg[1])) {
372 ret = kstrtoul(arg + 1, 0, ¶m);
374 trace_probe_log_err(offs, BAD_MEM_ADDR);
378 code->op = FETCH_OP_IMM;
379 code->immediate = param;
380 } else if (arg[1] == '+') {
381 /* kprobes don't support file offsets */
382 if (flags & TPARG_FL_KERNEL) {
383 trace_probe_log_err(offs, FILE_ON_KPROBE);
386 ret = kstrtol(arg + 2, 0, &offset);
388 trace_probe_log_err(offs, BAD_FILE_OFFS);
392 code->op = FETCH_OP_FOFFS;
393 code->immediate = (unsigned long)offset; // imm64?
395 /* uprobes don't support symbols */
396 if (!(flags & TPARG_FL_KERNEL)) {
397 trace_probe_log_err(offs, SYM_ON_UPROBE);
400 /* Preserve symbol for updating */
401 code->op = FETCH_NOP_SYMBOL;
402 code->data = kstrdup(arg + 1, GFP_KERNEL);
406 trace_probe_log_err(offs, TOO_MANY_OPS);
409 code->op = FETCH_OP_IMM;
412 /* These are fetching from memory */
414 trace_probe_log_err(offs, TOO_MANY_OPS);
418 code->op = FETCH_OP_DEREF;
419 code->offset = offset;
422 case '+': /* deref memory */
425 deref = FETCH_OP_UDEREF;
430 arg++; /* Skip '+', because kstrtol() rejects it. */
431 tmp = strchr(arg, '(');
433 trace_probe_log_err(offs, DEREF_NEED_BRACE);
437 ret = kstrtol(arg, 0, &offset);
439 trace_probe_log_err(offs, BAD_DEREF_OFFS);
442 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
444 tmp = strrchr(arg, ')');
446 trace_probe_log_err(offs + strlen(arg),
450 const struct fetch_type *t2 = find_fetch_type(NULL);
453 ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
456 if (code->op == FETCH_OP_COMM ||
457 code->op == FETCH_OP_DATA) {
458 trace_probe_log_err(offs, COMM_CANT_DEREF);
462 trace_probe_log_err(offs, TOO_MANY_OPS);
468 code->offset = offset;
471 case '\\': /* Immediate value */
472 if (arg[1] == '"') { /* Immediate string */
473 ret = __parse_imm_string(arg + 2, &tmp, offs + 2);
476 code->op = FETCH_OP_DATA;
479 ret = str_to_immediate(arg + 1, &code->immediate);
481 trace_probe_log_err(offs + 1, BAD_IMM);
483 code->op = FETCH_OP_IMM;
487 if (!ret && code->op == FETCH_OP_NOP) {
488 /* Parsed, but do not find fetch method */
489 trace_probe_log_err(offs, BAD_FETCH_ARG);
495 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
497 /* Bitfield type needs to be parsed into a fetch function */
498 static int __parse_bitfield_probe_arg(const char *bf,
499 const struct fetch_type *t,
500 struct fetch_insn **pcode)
502 struct fetch_insn *code = *pcode;
503 unsigned long bw, bo;
509 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
511 if (bw == 0 || *tail != '@')
515 bo = simple_strtoul(bf, &tail, 0);
517 if (tail == bf || *tail != '/')
520 if (code->op != FETCH_OP_NOP)
524 code->op = FETCH_OP_MOD_BF;
525 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
526 code->rshift = BYTES_TO_BITS(t->size) - bw;
527 code->basesize = t->size;
529 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
532 /* String length checking wrapper */
533 static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size,
534 struct probe_arg *parg, unsigned int flags, int offset)
536 struct fetch_insn *code, *scode, *tmp = NULL;
541 if (len > MAX_ARGSTR_LEN) {
542 trace_probe_log_err(offset, ARG_TOO_LONG);
544 } else if (len == 0) {
545 trace_probe_log_err(offset, NO_ARG_BODY);
549 parg->comm = kstrdup(arg, GFP_KERNEL);
553 t = strchr(arg, ':');
556 t2 = strchr(++t, '[');
559 t3 = strchr(t2, ']');
561 offset += t2 + strlen(t2) - arg;
562 trace_probe_log_err(offset,
565 } else if (t3[1] != '\0') {
566 trace_probe_log_err(offset + t3 + 1 - arg,
571 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
572 trace_probe_log_err(offset + t2 - arg,
576 if (parg->count > MAX_ARRAY_LEN) {
577 trace_probe_log_err(offset + t2 - arg,
585 * Since $comm and immediate string can not be dereferred,
586 * we can find those by strcmp.
588 if (strcmp(arg, "$comm") == 0 || strncmp(arg, "\\\"", 2) == 0) {
589 /* The type of $comm must be "string", and not an array. */
590 if (parg->count || (t && strcmp(t, "string")))
592 parg->type = find_fetch_type("string");
594 parg->type = find_fetch_type(t);
596 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
599 parg->offset = *size;
600 *size += parg->type->size * (parg->count ?: 1);
603 len = strlen(parg->type->fmttype) + 6;
604 parg->fmt = kmalloc(len, GFP_KERNEL);
607 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
611 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
614 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
616 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
621 /* Store operation */
622 if (!strcmp(parg->type->name, "string") ||
623 !strcmp(parg->type->name, "ustring")) {
624 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
625 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
626 code->op != FETCH_OP_DATA) {
627 trace_probe_log_err(offset + (t ? (t - arg) : 0),
632 if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM) ||
635 * IMM, DATA and COMM is pointing actual address, those
636 * must be kept, and if parg->count != 0, this is an
637 * array of string pointers instead of string address
641 if (code->op != FETCH_OP_NOP) {
642 trace_probe_log_err(offset, TOO_MANY_OPS);
647 /* If op == DEREF, replace it with STRING */
648 if (!strcmp(parg->type->name, "ustring") ||
649 code->op == FETCH_OP_UDEREF)
650 code->op = FETCH_OP_ST_USTRING;
652 code->op = FETCH_OP_ST_STRING;
653 code->size = parg->type->size;
654 parg->dynamic = true;
655 } else if (code->op == FETCH_OP_DEREF) {
656 code->op = FETCH_OP_ST_MEM;
657 code->size = parg->type->size;
658 } else if (code->op == FETCH_OP_UDEREF) {
659 code->op = FETCH_OP_ST_UMEM;
660 code->size = parg->type->size;
663 if (code->op != FETCH_OP_NOP) {
664 trace_probe_log_err(offset, TOO_MANY_OPS);
668 code->op = FETCH_OP_ST_RAW;
669 code->size = parg->type->size;
672 /* Modify operation */
674 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
676 trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
680 /* Loop(Array) operation */
682 if (scode->op != FETCH_OP_ST_MEM &&
683 scode->op != FETCH_OP_ST_STRING &&
684 scode->op != FETCH_OP_ST_USTRING) {
685 trace_probe_log_err(offset + (t ? (t - arg) : 0),
691 if (code->op != FETCH_OP_NOP) {
692 trace_probe_log_err(offset, TOO_MANY_OPS);
696 code->op = FETCH_OP_LP_ARRAY;
697 code->param = parg->count;
700 code->op = FETCH_OP_END;
702 /* Shrink down the code buffer */
703 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
707 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
711 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
712 if (code->op == FETCH_NOP_SYMBOL ||
713 code->op == FETCH_OP_DATA)
721 /* Return 1 if name is reserved or already used by another argument */
722 static int traceprobe_conflict_field_name(const char *name,
723 struct probe_arg *args, int narg)
727 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
728 if (strcmp(reserved_field_names[i], name) == 0)
731 for (i = 0; i < narg; i++)
732 if (strcmp(args[i].name, name) == 0)
738 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, char *arg,
741 struct probe_arg *parg = &tp->args[i];
744 /* Increment count for freeing args in error case */
747 body = strchr(arg, '=');
749 if (body - arg > MAX_ARG_NAME_LEN) {
750 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
752 } else if (body == arg) {
753 trace_probe_log_err(0, NO_ARG_NAME);
756 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
759 /* If argument name is omitted, set "argN" */
760 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
766 if (!is_good_name(parg->name)) {
767 trace_probe_log_err(0, BAD_ARG_NAME);
770 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
771 trace_probe_log_err(0, USED_ARG_NAME);
774 /* Parse fetch argument */
775 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
779 void traceprobe_free_probe_arg(struct probe_arg *arg)
781 struct fetch_insn *code = arg->code;
783 while (code && code->op != FETCH_OP_END) {
784 if (code->op == FETCH_NOP_SYMBOL ||
785 code->op == FETCH_OP_DATA)
795 int traceprobe_update_arg(struct probe_arg *arg)
797 struct fetch_insn *code = arg->code;
803 while (code && code->op != FETCH_OP_END) {
804 if (code->op == FETCH_NOP_SYMBOL) {
805 if (code[1].op != FETCH_OP_IMM)
808 tmp = strpbrk(code->data, "+-");
811 ret = traceprobe_split_symbol_offset(code->data,
817 (unsigned long)kallsyms_lookup_name(code->data);
820 if (!code[1].immediate)
822 code[1].immediate += offset;
829 /* When len=0, we just calculate the needed length */
830 #define LEN_OR_ZERO (len ? len - pos : 0)
831 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
834 struct probe_arg *parg;
837 const char *fmt, *arg;
841 arg = "REC->" FIELD_STRING_IP;
843 fmt = "(%lx <- %lx)";
844 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
847 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
849 for (i = 0; i < tp->nr_args; i++) {
851 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
853 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
855 for (j = 1; j < parg->count; j++)
856 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
858 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
860 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
864 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
866 for (i = 0; i < tp->nr_args; i++) {
869 if (strcmp(parg->type->name, "string") == 0)
870 fmt = ", __get_str(%s[%d])";
872 fmt = ", REC->%s[%d]";
873 for (j = 0; j < parg->count; j++)
874 pos += snprintf(buf + pos, LEN_OR_ZERO,
877 if (strcmp(parg->type->name, "string") == 0)
878 fmt = ", __get_str(%s)";
881 pos += snprintf(buf + pos, LEN_OR_ZERO,
886 /* return the length of print_fmt */
891 int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return)
893 struct trace_event_call *call = trace_probe_event_call(tp);
897 /* First: called with 0 length to calculate the needed length */
898 len = __set_print_fmt(tp, NULL, 0, is_return);
899 print_fmt = kmalloc(len + 1, GFP_KERNEL);
903 /* Second: actually write the @print_fmt */
904 __set_print_fmt(tp, print_fmt, len + 1, is_return);
905 call->print_fmt = print_fmt;
910 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
911 size_t offset, struct trace_probe *tp)
915 /* Set argument names as fields */
916 for (i = 0; i < tp->nr_args; i++) {
917 struct probe_arg *parg = &tp->args[i];
918 const char *fmt = parg->type->fmttype;
919 int size = parg->type->size;
925 ret = trace_define_field(event_call, fmt, parg->name,
926 offset + parg->offset, size,
927 parg->type->is_signed,
935 static void trace_probe_event_free(struct trace_probe_event *tpe)
937 kfree(tpe->class.system);
938 kfree(tpe->call.name);
939 kfree(tpe->call.print_fmt);
943 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
945 if (trace_probe_has_sibling(tp))
948 list_del_init(&tp->list);
949 trace_probe_event_free(tp->event);
951 tp->event = to->event;
952 list_add_tail(&tp->list, trace_probe_probe_list(to));
957 void trace_probe_unlink(struct trace_probe *tp)
959 list_del_init(&tp->list);
960 if (list_empty(trace_probe_probe_list(tp)))
961 trace_probe_event_free(tp->event);
965 void trace_probe_cleanup(struct trace_probe *tp)
969 for (i = 0; i < tp->nr_args; i++)
970 traceprobe_free_probe_arg(&tp->args[i]);
973 trace_probe_unlink(tp);
976 int trace_probe_init(struct trace_probe *tp, const char *event,
979 struct trace_event_call *call;
982 if (!event || !group)
985 tp->event = kzalloc(sizeof(struct trace_probe_event), GFP_KERNEL);
989 INIT_LIST_HEAD(&tp->event->files);
990 INIT_LIST_HEAD(&tp->event->class.fields);
991 INIT_LIST_HEAD(&tp->event->probes);
992 INIT_LIST_HEAD(&tp->list);
993 list_add(&tp->event->probes, &tp->list);
995 call = trace_probe_event_call(tp);
996 call->class = &tp->event->class;
997 call->name = kstrdup(event, GFP_KERNEL);
1003 tp->event->class.system = kstrdup(group, GFP_KERNEL);
1004 if (!tp->event->class.system) {
1012 trace_probe_cleanup(tp);
1016 int trace_probe_register_event_call(struct trace_probe *tp)
1018 struct trace_event_call *call = trace_probe_event_call(tp);
1021 ret = register_trace_event(&call->event);
1025 ret = trace_add_event_call(call);
1027 unregister_trace_event(&call->event);
1032 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1034 struct event_file_link *link;
1036 link = kmalloc(sizeof(*link), GFP_KERNEL);
1041 INIT_LIST_HEAD(&link->list);
1042 list_add_tail_rcu(&link->list, &tp->event->files);
1043 trace_probe_set_flag(tp, TP_FLAG_TRACE);
1047 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1048 struct trace_event_file *file)
1050 struct event_file_link *link;
1052 trace_probe_for_each_link(link, tp) {
1053 if (link->file == file)
1060 int trace_probe_remove_file(struct trace_probe *tp,
1061 struct trace_event_file *file)
1063 struct event_file_link *link;
1065 link = trace_probe_get_file_link(tp, file);
1069 list_del_rcu(&link->list);
1073 if (list_empty(&tp->event->files))
1074 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
1080 * Return the smallest index of different type argument (start from 1).
1081 * If all argument types and name are same, return 0.
1083 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1087 for (i = 0; i < a->nr_args; i++) {
1088 if ((b->nr_args <= i) ||
1089 ((a->args[i].type != b->args[i].type) ||
1090 (a->args[i].count != b->args[i].count) ||
1091 strcmp(a->args[i].name, b->args[i].name)))
1098 bool trace_probe_match_command_args(struct trace_probe *tp,
1099 int argc, const char **argv)
1101 char buf[MAX_ARGSTR_LEN + 1];
1104 if (tp->nr_args < argc)
1107 for (i = 0; i < argc; i++) {
1108 snprintf(buf, sizeof(buf), "%s=%s",
1109 tp->args[i].name, tp->args[i].comm);
1110 if (strcmp(buf, argv[i]))