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, 1,
81 __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, 1,
83 __ASSIGN_FETCH_TYPE("symstr", string, string, sizeof(u32), 1, 1,
86 ASSIGN_FETCH_TYPE(u8, u8, 0),
87 ASSIGN_FETCH_TYPE(u16, u16, 0),
88 ASSIGN_FETCH_TYPE(u32, u32, 0),
89 ASSIGN_FETCH_TYPE(u64, u64, 0),
90 ASSIGN_FETCH_TYPE(s8, u8, 1),
91 ASSIGN_FETCH_TYPE(s16, u16, 1),
92 ASSIGN_FETCH_TYPE(s32, u32, 1),
93 ASSIGN_FETCH_TYPE(s64, u64, 1),
94 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
95 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
96 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
97 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
98 ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
100 ASSIGN_FETCH_TYPE_END
103 static const struct fetch_type *find_fetch_type(const char *type)
108 type = DEFAULT_FETCH_TYPE_STR;
110 /* Special case: bitfield */
114 type = strchr(type, '/');
119 if (kstrtoul(type, 0, &bs))
124 return find_fetch_type("u8");
126 return find_fetch_type("u16");
128 return find_fetch_type("u32");
130 return find_fetch_type("u64");
136 for (i = 0; probe_fetch_types[i].name; i++) {
137 if (strcmp(type, probe_fetch_types[i].name) == 0)
138 return &probe_fetch_types[i];
145 static struct trace_probe_log trace_probe_log;
147 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
149 trace_probe_log.subsystem = subsystem;
150 trace_probe_log.argc = argc;
151 trace_probe_log.argv = argv;
152 trace_probe_log.index = 0;
155 void trace_probe_log_clear(void)
157 memset(&trace_probe_log, 0, sizeof(trace_probe_log));
160 void trace_probe_log_set_index(int index)
162 trace_probe_log.index = index;
165 void __trace_probe_log_err(int offset, int err_type)
168 int i, len = 0, pos = 0;
170 if (!trace_probe_log.argv)
173 /* Recalculate the length and allocate buffer */
174 for (i = 0; i < trace_probe_log.argc; i++) {
175 if (i == trace_probe_log.index)
177 len += strlen(trace_probe_log.argv[i]) + 1;
179 command = kzalloc(len, GFP_KERNEL);
183 if (trace_probe_log.index >= trace_probe_log.argc) {
185 * Set the error position is next to the last arg + space.
186 * Note that len includes the terminal null and the cursor
187 * appears at pos + 1.
193 /* And make a command string from argv array */
195 for (i = 0; i < trace_probe_log.argc; i++) {
196 len = strlen(trace_probe_log.argv[i]);
197 strcpy(p, trace_probe_log.argv[i]);
203 tracing_log_err(NULL, trace_probe_log.subsystem, command,
204 trace_probe_err_text, err_type, pos + offset);
209 /* Split symbol and offset. */
210 int traceprobe_split_symbol_offset(char *symbol, long *offset)
218 tmp = strpbrk(symbol, "+-");
220 ret = kstrtol(tmp, 0, offset);
230 /* @buf must has MAX_EVENT_NAME_LEN size */
231 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
232 char *buf, int offset)
234 const char *slash, *event = *pevent;
237 slash = strchr(event, '/');
239 slash = strchr(event, '.');
242 if (slash == event) {
243 trace_probe_log_err(offset, NO_GROUP_NAME);
246 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
247 trace_probe_log_err(offset, GROUP_TOO_LONG);
250 strlcpy(buf, event, slash - event + 1);
251 if (!is_good_name(buf)) {
252 trace_probe_log_err(offset, BAD_GROUP_NAME);
257 offset += slash - event + 1;
266 trace_probe_log_err(offset, NO_EVENT_NAME);
268 } else if (len > MAX_EVENT_NAME_LEN) {
269 trace_probe_log_err(offset, EVENT_TOO_LONG);
272 if (!is_good_name(event)) {
273 trace_probe_log_err(offset, BAD_EVENT_NAME);
279 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
281 static int parse_probe_vars(char *arg, const struct fetch_type *t,
282 struct fetch_insn *code, unsigned int flags, int offs)
288 if (flags & TPARG_FL_TPOINT) {
291 code->data = kstrdup(arg, GFP_KERNEL);
294 code->op = FETCH_OP_TP_ARG;
295 } else if (strcmp(arg, "retval") == 0) {
296 if (flags & TPARG_FL_RETURN) {
297 code->op = FETCH_OP_RETVAL;
299 trace_probe_log_err(offs, RETVAL_ON_PROBE);
302 } else if ((len = str_has_prefix(arg, "stack"))) {
303 if (arg[len] == '\0') {
304 code->op = FETCH_OP_STACKP;
305 } else if (isdigit(arg[len])) {
306 ret = kstrtoul(arg + len, 10, ¶m);
309 } else if ((flags & TPARG_FL_KERNEL) &&
310 param > PARAM_MAX_STACK) {
311 trace_probe_log_err(offs, BAD_STACK_NUM);
314 code->op = FETCH_OP_STACK;
315 code->param = (unsigned int)param;
319 } else if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
320 code->op = FETCH_OP_COMM;
321 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
322 } else if (((flags & TPARG_FL_MASK) ==
323 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
324 (len = str_has_prefix(arg, "arg"))) {
325 ret = kstrtoul(arg + len, 10, ¶m);
328 } else if (!param || param > PARAM_MAX_STACK) {
329 trace_probe_log_err(offs, BAD_ARG_NUM);
332 code->op = FETCH_OP_ARG;
333 code->param = (unsigned int)param - 1;
341 trace_probe_log_err(offs, BAD_VAR);
345 static int str_to_immediate(char *str, unsigned long *imm)
348 return kstrtoul(str, 0, imm);
349 else if (str[0] == '-')
350 return kstrtol(str, 0, (long *)imm);
351 else if (str[0] == '+')
352 return kstrtol(str + 1, 0, (long *)imm);
356 static int __parse_imm_string(char *str, char **pbuf, int offs)
358 size_t len = strlen(str);
360 if (str[len - 1] != '"') {
361 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
364 *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
370 /* Recursive argument parser */
372 parse_probe_arg(char *arg, const struct fetch_type *type,
373 struct fetch_insn **pcode, struct fetch_insn *end,
374 unsigned int flags, int offs)
376 struct fetch_insn *code = *pcode;
378 int deref = FETCH_OP_DEREF;
385 ret = parse_probe_vars(arg + 1, type, code, flags, offs);
388 case '%': /* named register */
389 if (flags & TPARG_FL_TPOINT) {
390 /* eprobes do not handle registers */
391 trace_probe_log_err(offs, BAD_VAR);
394 ret = regs_query_register_offset(arg + 1);
396 code->op = FETCH_OP_REG;
397 code->param = (unsigned int)ret;
400 trace_probe_log_err(offs, BAD_REG_NAME);
403 case '@': /* memory, file-offset or symbol */
404 if (isdigit(arg[1])) {
405 ret = kstrtoul(arg + 1, 0, ¶m);
407 trace_probe_log_err(offs, BAD_MEM_ADDR);
411 code->op = FETCH_OP_IMM;
412 code->immediate = param;
413 } else if (arg[1] == '+') {
414 /* kprobes don't support file offsets */
415 if (flags & TPARG_FL_KERNEL) {
416 trace_probe_log_err(offs, FILE_ON_KPROBE);
419 ret = kstrtol(arg + 2, 0, &offset);
421 trace_probe_log_err(offs, BAD_FILE_OFFS);
425 code->op = FETCH_OP_FOFFS;
426 code->immediate = (unsigned long)offset; // imm64?
428 /* uprobes don't support symbols */
429 if (!(flags & TPARG_FL_KERNEL)) {
430 trace_probe_log_err(offs, SYM_ON_UPROBE);
433 /* Preserve symbol for updating */
434 code->op = FETCH_NOP_SYMBOL;
435 code->data = kstrdup(arg + 1, GFP_KERNEL);
439 trace_probe_log_err(offs, TOO_MANY_OPS);
442 code->op = FETCH_OP_IMM;
445 /* These are fetching from memory */
447 trace_probe_log_err(offs, TOO_MANY_OPS);
451 code->op = FETCH_OP_DEREF;
452 code->offset = offset;
455 case '+': /* deref memory */
458 deref = FETCH_OP_UDEREF;
463 arg++; /* Skip '+', because kstrtol() rejects it. */
464 tmp = strchr(arg, '(');
466 trace_probe_log_err(offs, DEREF_NEED_BRACE);
470 ret = kstrtol(arg, 0, &offset);
472 trace_probe_log_err(offs, BAD_DEREF_OFFS);
475 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
477 tmp = strrchr(arg, ')');
479 trace_probe_log_err(offs + strlen(arg),
483 const struct fetch_type *t2 = find_fetch_type(NULL);
486 ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
489 if (code->op == FETCH_OP_COMM ||
490 code->op == FETCH_OP_DATA) {
491 trace_probe_log_err(offs, COMM_CANT_DEREF);
495 trace_probe_log_err(offs, TOO_MANY_OPS);
501 code->offset = offset;
504 case '\\': /* Immediate value */
505 if (arg[1] == '"') { /* Immediate string */
506 ret = __parse_imm_string(arg + 2, &tmp, offs + 2);
509 code->op = FETCH_OP_DATA;
512 ret = str_to_immediate(arg + 1, &code->immediate);
514 trace_probe_log_err(offs + 1, BAD_IMM);
516 code->op = FETCH_OP_IMM;
520 if (!ret && code->op == FETCH_OP_NOP) {
521 /* Parsed, but do not find fetch method */
522 trace_probe_log_err(offs, BAD_FETCH_ARG);
528 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
530 /* Bitfield type needs to be parsed into a fetch function */
531 static int __parse_bitfield_probe_arg(const char *bf,
532 const struct fetch_type *t,
533 struct fetch_insn **pcode)
535 struct fetch_insn *code = *pcode;
536 unsigned long bw, bo;
542 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
544 if (bw == 0 || *tail != '@')
548 bo = simple_strtoul(bf, &tail, 0);
550 if (tail == bf || *tail != '/')
553 if (code->op != FETCH_OP_NOP)
557 code->op = FETCH_OP_MOD_BF;
558 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
559 code->rshift = BYTES_TO_BITS(t->size) - bw;
560 code->basesize = t->size;
562 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
565 /* String length checking wrapper */
566 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
567 struct probe_arg *parg, unsigned int flags, int offset)
569 struct fetch_insn *code, *scode, *tmp = NULL;
574 arg = kstrdup(argv, GFP_KERNEL);
580 if (len > MAX_ARGSTR_LEN) {
581 trace_probe_log_err(offset, ARG_TOO_LONG);
583 } else if (len == 0) {
584 trace_probe_log_err(offset, NO_ARG_BODY);
589 parg->comm = kstrdup(arg, GFP_KERNEL);
594 t = strchr(arg, ':');
597 t2 = strchr(++t, '[');
600 t3 = strchr(t2, ']');
602 offset += t2 + strlen(t2) - arg;
603 trace_probe_log_err(offset,
606 } else if (t3[1] != '\0') {
607 trace_probe_log_err(offset + t3 + 1 - arg,
612 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
613 trace_probe_log_err(offset + t2 - arg,
617 if (parg->count > MAX_ARRAY_LEN) {
618 trace_probe_log_err(offset + t2 - arg,
626 * Since $comm and immediate string can not be dereferenced,
627 * we can find those by strcmp. But ignore for eprobes.
629 if (!(flags & TPARG_FL_TPOINT) &&
630 (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
631 strncmp(arg, "\\\"", 2) == 0)) {
632 /* The type of $comm must be "string", and not an array. */
633 if (parg->count || (t && strcmp(t, "string")))
635 parg->type = find_fetch_type("string");
637 parg->type = find_fetch_type(t);
639 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
642 parg->offset = *size;
643 *size += parg->type->size * (parg->count ?: 1);
647 len = strlen(parg->type->fmttype) + 6;
648 parg->fmt = kmalloc(len, GFP_KERNEL);
651 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
655 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
658 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
660 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
666 /* Store operation */
667 if (parg->type->is_string) {
668 if (!strcmp(parg->type->name, "symstr")) {
669 if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
670 code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
671 code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
672 trace_probe_log_err(offset + (t ? (t - arg) : 0),
677 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
678 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
679 code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
680 trace_probe_log_err(offset + (t ? (t - arg) : 0),
685 if (!strcmp(parg->type->name, "symstr") ||
686 (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
687 code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
690 * IMM, DATA and COMM is pointing actual address, those
691 * must be kept, and if parg->count != 0, this is an
692 * array of string pointers instead of string address
694 * For the symstr, it doesn't need to dereference, thus
695 * it just get the value.
698 if (code->op != FETCH_OP_NOP) {
699 trace_probe_log_err(offset, TOO_MANY_OPS);
703 /* If op == DEREF, replace it with STRING */
704 if (!strcmp(parg->type->name, "ustring") ||
705 code->op == FETCH_OP_UDEREF)
706 code->op = FETCH_OP_ST_USTRING;
707 else if (!strcmp(parg->type->name, "symstr"))
708 code->op = FETCH_OP_ST_SYMSTR;
710 code->op = FETCH_OP_ST_STRING;
711 code->size = parg->type->size;
712 parg->dynamic = true;
713 } else if (code->op == FETCH_OP_DEREF) {
714 code->op = FETCH_OP_ST_MEM;
715 code->size = parg->type->size;
716 } else if (code->op == FETCH_OP_UDEREF) {
717 code->op = FETCH_OP_ST_UMEM;
718 code->size = parg->type->size;
721 if (code->op != FETCH_OP_NOP) {
722 trace_probe_log_err(offset, TOO_MANY_OPS);
725 code->op = FETCH_OP_ST_RAW;
726 code->size = parg->type->size;
729 /* Modify operation */
731 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
733 trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
738 /* Loop(Array) operation */
740 if (scode->op != FETCH_OP_ST_MEM &&
741 scode->op != FETCH_OP_ST_STRING &&
742 scode->op != FETCH_OP_ST_USTRING) {
743 trace_probe_log_err(offset + (t ? (t - arg) : 0),
748 if (code->op != FETCH_OP_NOP) {
749 trace_probe_log_err(offset, TOO_MANY_OPS);
752 code->op = FETCH_OP_LP_ARRAY;
753 code->param = parg->count;
756 code->op = FETCH_OP_END;
759 /* Shrink down the code buffer */
760 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
764 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
768 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
769 if (code->op == FETCH_NOP_SYMBOL ||
770 code->op == FETCH_OP_DATA)
780 /* Return 1 if name is reserved or already used by another argument */
781 static int traceprobe_conflict_field_name(const char *name,
782 struct probe_arg *args, int narg)
786 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
787 if (strcmp(reserved_field_names[i], name) == 0)
790 for (i = 0; i < narg; i++)
791 if (strcmp(args[i].name, name) == 0)
797 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
800 struct probe_arg *parg = &tp->args[i];
803 /* Increment count for freeing args in error case */
806 body = strchr(arg, '=');
808 if (body - arg > MAX_ARG_NAME_LEN) {
809 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
811 } else if (body == arg) {
812 trace_probe_log_err(0, NO_ARG_NAME);
815 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
818 /* If argument name is omitted, set "argN" */
819 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
825 if (!is_good_name(parg->name)) {
826 trace_probe_log_err(0, BAD_ARG_NAME);
829 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
830 trace_probe_log_err(0, USED_ARG_NAME);
833 /* Parse fetch argument */
834 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
838 void traceprobe_free_probe_arg(struct probe_arg *arg)
840 struct fetch_insn *code = arg->code;
842 while (code && code->op != FETCH_OP_END) {
843 if (code->op == FETCH_NOP_SYMBOL ||
844 code->op == FETCH_OP_DATA)
854 int traceprobe_update_arg(struct probe_arg *arg)
856 struct fetch_insn *code = arg->code;
862 while (code && code->op != FETCH_OP_END) {
863 if (code->op == FETCH_NOP_SYMBOL) {
864 if (code[1].op != FETCH_OP_IMM)
867 tmp = strpbrk(code->data, "+-");
870 ret = traceprobe_split_symbol_offset(code->data,
876 (unsigned long)kallsyms_lookup_name(code->data);
879 if (!code[1].immediate)
881 code[1].immediate += offset;
888 /* When len=0, we just calculate the needed length */
889 #define LEN_OR_ZERO (len ? len - pos : 0)
890 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
891 enum probe_print_type ptype)
893 struct probe_arg *parg;
896 const char *fmt, *arg;
899 case PROBE_PRINT_NORMAL:
901 arg = ", REC->" FIELD_STRING_IP;
903 case PROBE_PRINT_RETURN:
904 fmt = "(%lx <- %lx)";
905 arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
907 case PROBE_PRINT_EVENT:
916 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
918 for (i = 0; i < tp->nr_args; i++) {
920 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
922 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
924 for (j = 1; j < parg->count; j++)
925 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
927 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
929 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
933 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg);
935 for (i = 0; i < tp->nr_args; i++) {
938 if (parg->type->is_string)
939 fmt = ", __get_str(%s[%d])";
941 fmt = ", REC->%s[%d]";
942 for (j = 0; j < parg->count; j++)
943 pos += snprintf(buf + pos, LEN_OR_ZERO,
946 if (parg->type->is_string)
947 fmt = ", __get_str(%s)";
950 pos += snprintf(buf + pos, LEN_OR_ZERO,
955 /* return the length of print_fmt */
960 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
962 struct trace_event_call *call = trace_probe_event_call(tp);
966 /* First: called with 0 length to calculate the needed length */
967 len = __set_print_fmt(tp, NULL, 0, ptype);
968 print_fmt = kmalloc(len + 1, GFP_KERNEL);
972 /* Second: actually write the @print_fmt */
973 __set_print_fmt(tp, print_fmt, len + 1, ptype);
974 call->print_fmt = print_fmt;
979 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
980 size_t offset, struct trace_probe *tp)
984 /* Set argument names as fields */
985 for (i = 0; i < tp->nr_args; i++) {
986 struct probe_arg *parg = &tp->args[i];
987 const char *fmt = parg->type->fmttype;
988 int size = parg->type->size;
994 ret = trace_define_field(event_call, fmt, parg->name,
995 offset + parg->offset, size,
996 parg->type->is_signed,
1004 static void trace_probe_event_free(struct trace_probe_event *tpe)
1006 kfree(tpe->class.system);
1007 kfree(tpe->call.name);
1008 kfree(tpe->call.print_fmt);
1012 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
1014 if (trace_probe_has_sibling(tp))
1017 list_del_init(&tp->list);
1018 trace_probe_event_free(tp->event);
1020 tp->event = to->event;
1021 list_add_tail(&tp->list, trace_probe_probe_list(to));
1026 void trace_probe_unlink(struct trace_probe *tp)
1028 list_del_init(&tp->list);
1029 if (list_empty(trace_probe_probe_list(tp)))
1030 trace_probe_event_free(tp->event);
1034 void trace_probe_cleanup(struct trace_probe *tp)
1038 for (i = 0; i < tp->nr_args; i++)
1039 traceprobe_free_probe_arg(&tp->args[i]);
1042 trace_probe_unlink(tp);
1045 int trace_probe_init(struct trace_probe *tp, const char *event,
1046 const char *group, bool alloc_filter)
1048 struct trace_event_call *call;
1049 size_t size = sizeof(struct trace_probe_event);
1052 if (!event || !group)
1056 size += sizeof(struct trace_uprobe_filter);
1058 tp->event = kzalloc(size, GFP_KERNEL);
1062 INIT_LIST_HEAD(&tp->event->files);
1063 INIT_LIST_HEAD(&tp->event->class.fields);
1064 INIT_LIST_HEAD(&tp->event->probes);
1065 INIT_LIST_HEAD(&tp->list);
1066 list_add(&tp->list, &tp->event->probes);
1068 call = trace_probe_event_call(tp);
1069 call->class = &tp->event->class;
1070 call->name = kstrdup(event, GFP_KERNEL);
1076 tp->event->class.system = kstrdup(group, GFP_KERNEL);
1077 if (!tp->event->class.system) {
1085 trace_probe_cleanup(tp);
1089 static struct trace_event_call *
1090 find_trace_event_call(const char *system, const char *event_name)
1092 struct trace_event_call *tp_event;
1095 list_for_each_entry(tp_event, &ftrace_events, list) {
1096 if (!tp_event->class->system ||
1097 strcmp(system, tp_event->class->system))
1099 name = trace_event_name(tp_event);
1100 if (!name || strcmp(event_name, name))
1108 int trace_probe_register_event_call(struct trace_probe *tp)
1110 struct trace_event_call *call = trace_probe_event_call(tp);
1113 lockdep_assert_held(&event_mutex);
1115 if (find_trace_event_call(trace_probe_group_name(tp),
1116 trace_probe_name(tp)))
1119 ret = register_trace_event(&call->event);
1123 ret = trace_add_event_call(call);
1125 unregister_trace_event(&call->event);
1130 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1132 struct event_file_link *link;
1134 link = kmalloc(sizeof(*link), GFP_KERNEL);
1139 INIT_LIST_HEAD(&link->list);
1140 list_add_tail_rcu(&link->list, &tp->event->files);
1141 trace_probe_set_flag(tp, TP_FLAG_TRACE);
1145 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1146 struct trace_event_file *file)
1148 struct event_file_link *link;
1150 trace_probe_for_each_link(link, tp) {
1151 if (link->file == file)
1158 int trace_probe_remove_file(struct trace_probe *tp,
1159 struct trace_event_file *file)
1161 struct event_file_link *link;
1163 link = trace_probe_get_file_link(tp, file);
1167 list_del_rcu(&link->list);
1170 if (list_empty(&tp->event->files))
1171 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
1177 * Return the smallest index of different type argument (start from 1).
1178 * If all argument types and name are same, return 0.
1180 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1184 /* In case of more arguments */
1185 if (a->nr_args < b->nr_args)
1186 return a->nr_args + 1;
1187 if (a->nr_args > b->nr_args)
1188 return b->nr_args + 1;
1190 for (i = 0; i < a->nr_args; i++) {
1191 if ((b->nr_args <= i) ||
1192 ((a->args[i].type != b->args[i].type) ||
1193 (a->args[i].count != b->args[i].count) ||
1194 strcmp(a->args[i].name, b->args[i].name)))
1201 bool trace_probe_match_command_args(struct trace_probe *tp,
1202 int argc, const char **argv)
1204 char buf[MAX_ARGSTR_LEN + 1];
1207 if (tp->nr_args < argc)
1210 for (i = 0; i < argc; i++) {
1211 snprintf(buf, sizeof(buf), "%s=%s",
1212 tp->args[i].name, tp->args[i].comm);
1213 if (strcmp(buf, argv[i]))
1219 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
1221 int argc = 0, ret = 0;
1224 argv = argv_split(GFP_KERNEL, raw_command, &argc);
1229 ret = createfn(argc, (const char **)argv);