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 /* Recalculate 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 * appears 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 slash = strchr(event, '.');
240 if (slash == event) {
241 trace_probe_log_err(offset, NO_GROUP_NAME);
244 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
245 trace_probe_log_err(offset, GROUP_TOO_LONG);
248 strlcpy(buf, event, slash - event + 1);
249 if (!is_good_name(buf)) {
250 trace_probe_log_err(offset, BAD_GROUP_NAME);
255 offset += slash - event + 1;
260 trace_probe_log_err(offset, NO_EVENT_NAME);
262 } else if (len > MAX_EVENT_NAME_LEN) {
263 trace_probe_log_err(offset, EVENT_TOO_LONG);
266 if (!is_good_name(event)) {
267 trace_probe_log_err(offset, BAD_EVENT_NAME);
273 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
275 static int parse_probe_vars(char *arg, const struct fetch_type *t,
276 struct fetch_insn *code, unsigned int flags, int offs)
282 if (strcmp(arg, "retval") == 0) {
283 if (flags & TPARG_FL_RETURN) {
284 code->op = FETCH_OP_RETVAL;
286 trace_probe_log_err(offs, RETVAL_ON_PROBE);
289 } else if ((len = str_has_prefix(arg, "stack"))) {
290 if (arg[len] == '\0') {
291 code->op = FETCH_OP_STACKP;
292 } else if (isdigit(arg[len])) {
293 ret = kstrtoul(arg + len, 10, ¶m);
296 } else if ((flags & TPARG_FL_KERNEL) &&
297 param > PARAM_MAX_STACK) {
298 trace_probe_log_err(offs, BAD_STACK_NUM);
301 code->op = FETCH_OP_STACK;
302 code->param = (unsigned int)param;
306 } else if (strcmp(arg, "comm") == 0) {
307 code->op = FETCH_OP_COMM;
308 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
309 } else if (((flags & TPARG_FL_MASK) ==
310 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
311 (len = str_has_prefix(arg, "arg"))) {
312 ret = kstrtoul(arg + len, 10, ¶m);
315 } else if (!param || param > PARAM_MAX_STACK) {
316 trace_probe_log_err(offs, BAD_ARG_NUM);
319 code->op = FETCH_OP_ARG;
320 code->param = (unsigned int)param - 1;
328 trace_probe_log_err(offs, BAD_VAR);
332 static int str_to_immediate(char *str, unsigned long *imm)
335 return kstrtoul(str, 0, imm);
336 else if (str[0] == '-')
337 return kstrtol(str, 0, (long *)imm);
338 else if (str[0] == '+')
339 return kstrtol(str + 1, 0, (long *)imm);
343 static int __parse_imm_string(char *str, char **pbuf, int offs)
345 size_t len = strlen(str);
347 if (str[len - 1] != '"') {
348 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
351 *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
355 /* Recursive argument parser */
357 parse_probe_arg(char *arg, const struct fetch_type *type,
358 struct fetch_insn **pcode, struct fetch_insn *end,
359 unsigned int flags, int offs)
361 struct fetch_insn *code = *pcode;
363 int deref = FETCH_OP_DEREF;
370 ret = parse_probe_vars(arg + 1, type, code, flags, offs);
373 case '%': /* named register */
374 ret = regs_query_register_offset(arg + 1);
376 code->op = FETCH_OP_REG;
377 code->param = (unsigned int)ret;
380 trace_probe_log_err(offs, BAD_REG_NAME);
383 case '@': /* memory, file-offset or symbol */
384 if (isdigit(arg[1])) {
385 ret = kstrtoul(arg + 1, 0, ¶m);
387 trace_probe_log_err(offs, BAD_MEM_ADDR);
391 code->op = FETCH_OP_IMM;
392 code->immediate = param;
393 } else if (arg[1] == '+') {
394 /* kprobes don't support file offsets */
395 if (flags & TPARG_FL_KERNEL) {
396 trace_probe_log_err(offs, FILE_ON_KPROBE);
399 ret = kstrtol(arg + 2, 0, &offset);
401 trace_probe_log_err(offs, BAD_FILE_OFFS);
405 code->op = FETCH_OP_FOFFS;
406 code->immediate = (unsigned long)offset; // imm64?
408 /* uprobes don't support symbols */
409 if (!(flags & TPARG_FL_KERNEL)) {
410 trace_probe_log_err(offs, SYM_ON_UPROBE);
413 /* Preserve symbol for updating */
414 code->op = FETCH_NOP_SYMBOL;
415 code->data = kstrdup(arg + 1, GFP_KERNEL);
419 trace_probe_log_err(offs, TOO_MANY_OPS);
422 code->op = FETCH_OP_IMM;
425 /* These are fetching from memory */
427 trace_probe_log_err(offs, TOO_MANY_OPS);
431 code->op = FETCH_OP_DEREF;
432 code->offset = offset;
435 case '+': /* deref memory */
438 deref = FETCH_OP_UDEREF;
443 arg++; /* Skip '+', because kstrtol() rejects it. */
444 tmp = strchr(arg, '(');
446 trace_probe_log_err(offs, DEREF_NEED_BRACE);
450 ret = kstrtol(arg, 0, &offset);
452 trace_probe_log_err(offs, BAD_DEREF_OFFS);
455 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
457 tmp = strrchr(arg, ')');
459 trace_probe_log_err(offs + strlen(arg),
463 const struct fetch_type *t2 = find_fetch_type(NULL);
466 ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
469 if (code->op == FETCH_OP_COMM ||
470 code->op == FETCH_OP_DATA) {
471 trace_probe_log_err(offs, COMM_CANT_DEREF);
475 trace_probe_log_err(offs, TOO_MANY_OPS);
481 code->offset = offset;
484 case '\\': /* Immediate value */
485 if (arg[1] == '"') { /* Immediate string */
486 ret = __parse_imm_string(arg + 2, &tmp, offs + 2);
489 code->op = FETCH_OP_DATA;
492 ret = str_to_immediate(arg + 1, &code->immediate);
494 trace_probe_log_err(offs + 1, BAD_IMM);
496 code->op = FETCH_OP_IMM;
500 if (!ret && code->op == FETCH_OP_NOP) {
501 /* Parsed, but do not find fetch method */
502 trace_probe_log_err(offs, BAD_FETCH_ARG);
508 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
510 /* Bitfield type needs to be parsed into a fetch function */
511 static int __parse_bitfield_probe_arg(const char *bf,
512 const struct fetch_type *t,
513 struct fetch_insn **pcode)
515 struct fetch_insn *code = *pcode;
516 unsigned long bw, bo;
522 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
524 if (bw == 0 || *tail != '@')
528 bo = simple_strtoul(bf, &tail, 0);
530 if (tail == bf || *tail != '/')
533 if (code->op != FETCH_OP_NOP)
537 code->op = FETCH_OP_MOD_BF;
538 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
539 code->rshift = BYTES_TO_BITS(t->size) - bw;
540 code->basesize = t->size;
542 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
545 /* String length checking wrapper */
546 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
547 struct probe_arg *parg, unsigned int flags, int offset)
549 struct fetch_insn *code, *scode, *tmp = NULL;
554 arg = kstrdup(argv, GFP_KERNEL);
560 if (len > MAX_ARGSTR_LEN) {
561 trace_probe_log_err(offset, ARG_TOO_LONG);
563 } else if (len == 0) {
564 trace_probe_log_err(offset, NO_ARG_BODY);
569 parg->comm = kstrdup(arg, GFP_KERNEL);
574 t = strchr(arg, ':');
577 t2 = strchr(++t, '[');
580 t3 = strchr(t2, ']');
582 offset += t2 + strlen(t2) - arg;
583 trace_probe_log_err(offset,
586 } else if (t3[1] != '\0') {
587 trace_probe_log_err(offset + t3 + 1 - arg,
592 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
593 trace_probe_log_err(offset + t2 - arg,
597 if (parg->count > MAX_ARRAY_LEN) {
598 trace_probe_log_err(offset + t2 - arg,
606 * Since $comm and immediate string can not be dereferenced,
607 * we can find those by strcmp.
609 if (strcmp(arg, "$comm") == 0 || strncmp(arg, "\\\"", 2) == 0) {
610 /* The type of $comm must be "string", and not an array. */
611 if (parg->count || (t && strcmp(t, "string")))
613 parg->type = find_fetch_type("string");
615 parg->type = find_fetch_type(t);
617 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
620 parg->offset = *size;
621 *size += parg->type->size * (parg->count ?: 1);
625 len = strlen(parg->type->fmttype) + 6;
626 parg->fmt = kmalloc(len, GFP_KERNEL);
629 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
633 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
636 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
638 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
644 /* Store operation */
645 if (!strcmp(parg->type->name, "string") ||
646 !strcmp(parg->type->name, "ustring")) {
647 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
648 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
649 code->op != FETCH_OP_DATA) {
650 trace_probe_log_err(offset + (t ? (t - arg) : 0),
654 if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
655 code->op == FETCH_OP_DATA) || parg->count) {
657 * IMM, DATA and COMM is pointing actual address, those
658 * must be kept, and if parg->count != 0, this is an
659 * array of string pointers instead of string address
663 if (code->op != FETCH_OP_NOP) {
664 trace_probe_log_err(offset, TOO_MANY_OPS);
668 /* If op == DEREF, replace it with STRING */
669 if (!strcmp(parg->type->name, "ustring") ||
670 code->op == FETCH_OP_UDEREF)
671 code->op = FETCH_OP_ST_USTRING;
673 code->op = FETCH_OP_ST_STRING;
674 code->size = parg->type->size;
675 parg->dynamic = true;
676 } else if (code->op == FETCH_OP_DEREF) {
677 code->op = FETCH_OP_ST_MEM;
678 code->size = parg->type->size;
679 } else if (code->op == FETCH_OP_UDEREF) {
680 code->op = FETCH_OP_ST_UMEM;
681 code->size = parg->type->size;
684 if (code->op != FETCH_OP_NOP) {
685 trace_probe_log_err(offset, TOO_MANY_OPS);
688 code->op = FETCH_OP_ST_RAW;
689 code->size = parg->type->size;
692 /* Modify operation */
694 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
696 trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
701 /* Loop(Array) operation */
703 if (scode->op != FETCH_OP_ST_MEM &&
704 scode->op != FETCH_OP_ST_STRING &&
705 scode->op != FETCH_OP_ST_USTRING) {
706 trace_probe_log_err(offset + (t ? (t - arg) : 0),
711 if (code->op != FETCH_OP_NOP) {
712 trace_probe_log_err(offset, TOO_MANY_OPS);
715 code->op = FETCH_OP_LP_ARRAY;
716 code->param = parg->count;
719 code->op = FETCH_OP_END;
722 /* Shrink down the code buffer */
723 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
727 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
731 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
732 if (code->op == FETCH_NOP_SYMBOL ||
733 code->op == FETCH_OP_DATA)
743 /* Return 1 if name is reserved or already used by another argument */
744 static int traceprobe_conflict_field_name(const char *name,
745 struct probe_arg *args, int narg)
749 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
750 if (strcmp(reserved_field_names[i], name) == 0)
753 for (i = 0; i < narg; i++)
754 if (strcmp(args[i].name, name) == 0)
760 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
763 struct probe_arg *parg = &tp->args[i];
766 /* Increment count for freeing args in error case */
769 body = strchr(arg, '=');
771 if (body - arg > MAX_ARG_NAME_LEN) {
772 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
774 } else if (body == arg) {
775 trace_probe_log_err(0, NO_ARG_NAME);
778 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
781 /* If argument name is omitted, set "argN" */
782 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
788 if (!is_good_name(parg->name)) {
789 trace_probe_log_err(0, BAD_ARG_NAME);
792 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
793 trace_probe_log_err(0, USED_ARG_NAME);
796 /* Parse fetch argument */
797 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
801 void traceprobe_free_probe_arg(struct probe_arg *arg)
803 struct fetch_insn *code = arg->code;
805 while (code && code->op != FETCH_OP_END) {
806 if (code->op == FETCH_NOP_SYMBOL ||
807 code->op == FETCH_OP_DATA)
817 int traceprobe_update_arg(struct probe_arg *arg)
819 struct fetch_insn *code = arg->code;
825 while (code && code->op != FETCH_OP_END) {
826 if (code->op == FETCH_NOP_SYMBOL) {
827 if (code[1].op != FETCH_OP_IMM)
830 tmp = strpbrk(code->data, "+-");
833 ret = traceprobe_split_symbol_offset(code->data,
839 (unsigned long)kallsyms_lookup_name(code->data);
842 if (!code[1].immediate)
844 code[1].immediate += offset;
851 /* When len=0, we just calculate the needed length */
852 #define LEN_OR_ZERO (len ? len - pos : 0)
853 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
854 enum probe_print_type ptype)
856 struct probe_arg *parg;
859 const char *fmt, *arg;
862 case PROBE_PRINT_NORMAL:
864 arg = "REC->" FIELD_STRING_IP;
866 case PROBE_PRINT_RETURN:
867 fmt = "(%lx <- %lx)";
868 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
875 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
877 for (i = 0; i < tp->nr_args; i++) {
879 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
881 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
883 for (j = 1; j < parg->count; j++)
884 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
886 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
888 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
892 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
894 for (i = 0; i < tp->nr_args; i++) {
897 if ((strcmp(parg->type->name, "string") == 0) ||
898 (strcmp(parg->type->name, "ustring") == 0))
899 fmt = ", __get_str(%s[%d])";
901 fmt = ", REC->%s[%d]";
902 for (j = 0; j < parg->count; j++)
903 pos += snprintf(buf + pos, LEN_OR_ZERO,
906 if ((strcmp(parg->type->name, "string") == 0) ||
907 (strcmp(parg->type->name, "ustring") == 0))
908 fmt = ", __get_str(%s)";
911 pos += snprintf(buf + pos, LEN_OR_ZERO,
916 /* return the length of print_fmt */
921 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
923 struct trace_event_call *call = trace_probe_event_call(tp);
927 /* First: called with 0 length to calculate the needed length */
928 len = __set_print_fmt(tp, NULL, 0, ptype);
929 print_fmt = kmalloc(len + 1, GFP_KERNEL);
933 /* Second: actually write the @print_fmt */
934 __set_print_fmt(tp, print_fmt, len + 1, ptype);
935 call->print_fmt = print_fmt;
940 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
941 size_t offset, struct trace_probe *tp)
945 /* Set argument names as fields */
946 for (i = 0; i < tp->nr_args; i++) {
947 struct probe_arg *parg = &tp->args[i];
948 const char *fmt = parg->type->fmttype;
949 int size = parg->type->size;
955 ret = trace_define_field(event_call, fmt, parg->name,
956 offset + parg->offset, size,
957 parg->type->is_signed,
965 static void trace_probe_event_free(struct trace_probe_event *tpe)
967 kfree(tpe->class.system);
968 kfree(tpe->call.name);
969 kfree(tpe->call.print_fmt);
973 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
975 if (trace_probe_has_sibling(tp))
978 list_del_init(&tp->list);
979 trace_probe_event_free(tp->event);
981 tp->event = to->event;
982 list_add_tail(&tp->list, trace_probe_probe_list(to));
987 void trace_probe_unlink(struct trace_probe *tp)
989 list_del_init(&tp->list);
990 if (list_empty(trace_probe_probe_list(tp)))
991 trace_probe_event_free(tp->event);
995 void trace_probe_cleanup(struct trace_probe *tp)
999 for (i = 0; i < tp->nr_args; i++)
1000 traceprobe_free_probe_arg(&tp->args[i]);
1003 trace_probe_unlink(tp);
1006 int trace_probe_init(struct trace_probe *tp, const char *event,
1007 const char *group, bool alloc_filter)
1009 struct trace_event_call *call;
1010 size_t size = sizeof(struct trace_probe_event);
1013 if (!event || !group)
1017 size += sizeof(struct trace_uprobe_filter);
1019 tp->event = kzalloc(size, GFP_KERNEL);
1023 INIT_LIST_HEAD(&tp->event->files);
1024 INIT_LIST_HEAD(&tp->event->class.fields);
1025 INIT_LIST_HEAD(&tp->event->probes);
1026 INIT_LIST_HEAD(&tp->list);
1027 list_add(&tp->list, &tp->event->probes);
1029 call = trace_probe_event_call(tp);
1030 call->class = &tp->event->class;
1031 call->name = kstrdup(event, GFP_KERNEL);
1037 tp->event->class.system = kstrdup(group, GFP_KERNEL);
1038 if (!tp->event->class.system) {
1046 trace_probe_cleanup(tp);
1050 int trace_probe_register_event_call(struct trace_probe *tp)
1052 struct trace_event_call *call = trace_probe_event_call(tp);
1055 ret = register_trace_event(&call->event);
1059 ret = trace_add_event_call(call);
1061 unregister_trace_event(&call->event);
1066 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1068 struct event_file_link *link;
1070 link = kmalloc(sizeof(*link), GFP_KERNEL);
1075 INIT_LIST_HEAD(&link->list);
1076 list_add_tail_rcu(&link->list, &tp->event->files);
1077 trace_probe_set_flag(tp, TP_FLAG_TRACE);
1081 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1082 struct trace_event_file *file)
1084 struct event_file_link *link;
1086 trace_probe_for_each_link(link, tp) {
1087 if (link->file == file)
1094 int trace_probe_remove_file(struct trace_probe *tp,
1095 struct trace_event_file *file)
1097 struct event_file_link *link;
1099 link = trace_probe_get_file_link(tp, file);
1103 list_del_rcu(&link->list);
1107 if (list_empty(&tp->event->files))
1108 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
1114 * Return the smallest index of different type argument (start from 1).
1115 * If all argument types and name are same, return 0.
1117 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1121 /* In case of more arguments */
1122 if (a->nr_args < b->nr_args)
1123 return a->nr_args + 1;
1124 if (a->nr_args > b->nr_args)
1125 return b->nr_args + 1;
1127 for (i = 0; i < a->nr_args; i++) {
1128 if ((b->nr_args <= i) ||
1129 ((a->args[i].type != b->args[i].type) ||
1130 (a->args[i].count != b->args[i].count) ||
1131 strcmp(a->args[i].name, b->args[i].name)))
1138 bool trace_probe_match_command_args(struct trace_probe *tp,
1139 int argc, const char **argv)
1141 char buf[MAX_ARGSTR_LEN + 1];
1144 if (tp->nr_args < argc)
1147 for (i = 0; i < argc; i++) {
1148 snprintf(buf, sizeof(buf), "%s=%s",
1149 tp->args[i].name, tp->args[i].comm);
1150 if (strcmp(buf, argv[i]))
1156 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
1158 int argc = 0, ret = 0;
1161 argv = argv_split(GFP_KERNEL, raw_command, &argc);
1166 ret = createfn(argc, (const char **)argv);