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_STRING);
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_system_name(buf)) {
252 trace_probe_log_err(offset, BAD_GROUP_NAME);
257 offset += slash - event + 1;
262 trace_probe_log_err(offset, NO_EVENT_NAME);
264 } else if (len > MAX_EVENT_NAME_LEN) {
265 trace_probe_log_err(offset, EVENT_TOO_LONG);
268 if (!is_good_name(event)) {
269 trace_probe_log_err(offset, BAD_EVENT_NAME);
275 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
277 static int parse_probe_vars(char *arg, const struct fetch_type *t,
278 struct fetch_insn *code, unsigned int flags, int offs)
284 if (flags & TPARG_FL_TPOINT) {
287 code->data = kstrdup(arg, GFP_KERNEL);
290 code->op = FETCH_OP_TP_ARG;
291 } else if (strcmp(arg, "retval") == 0) {
292 if (flags & TPARG_FL_RETURN) {
293 code->op = FETCH_OP_RETVAL;
295 trace_probe_log_err(offs, RETVAL_ON_PROBE);
298 } else if ((len = str_has_prefix(arg, "stack"))) {
299 if (arg[len] == '\0') {
300 code->op = FETCH_OP_STACKP;
301 } else if (isdigit(arg[len])) {
302 ret = kstrtoul(arg + len, 10, ¶m);
305 } else if ((flags & TPARG_FL_KERNEL) &&
306 param > PARAM_MAX_STACK) {
307 trace_probe_log_err(offs, BAD_STACK_NUM);
310 code->op = FETCH_OP_STACK;
311 code->param = (unsigned int)param;
315 } else if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
316 code->op = FETCH_OP_COMM;
317 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
318 } else if (((flags & TPARG_FL_MASK) ==
319 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
320 (len = str_has_prefix(arg, "arg"))) {
321 ret = kstrtoul(arg + len, 10, ¶m);
324 } else if (!param || param > PARAM_MAX_STACK) {
325 trace_probe_log_err(offs, BAD_ARG_NUM);
328 code->op = FETCH_OP_ARG;
329 code->param = (unsigned int)param - 1;
337 trace_probe_log_err(offs, BAD_VAR);
341 static int str_to_immediate(char *str, unsigned long *imm)
344 return kstrtoul(str, 0, imm);
345 else if (str[0] == '-')
346 return kstrtol(str, 0, (long *)imm);
347 else if (str[0] == '+')
348 return kstrtol(str + 1, 0, (long *)imm);
352 static int __parse_imm_string(char *str, char **pbuf, int offs)
354 size_t len = strlen(str);
356 if (str[len - 1] != '"') {
357 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
360 *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
366 /* Recursive argument parser */
368 parse_probe_arg(char *arg, const struct fetch_type *type,
369 struct fetch_insn **pcode, struct fetch_insn *end,
370 unsigned int flags, int offs)
372 struct fetch_insn *code = *pcode;
374 int deref = FETCH_OP_DEREF;
381 ret = parse_probe_vars(arg + 1, type, code, flags, offs);
384 case '%': /* named register */
385 if (flags & TPARG_FL_TPOINT) {
386 /* eprobes do not handle registers */
387 trace_probe_log_err(offs, BAD_VAR);
390 ret = regs_query_register_offset(arg + 1);
392 code->op = FETCH_OP_REG;
393 code->param = (unsigned int)ret;
396 trace_probe_log_err(offs, BAD_REG_NAME);
399 case '@': /* memory, file-offset or symbol */
400 if (isdigit(arg[1])) {
401 ret = kstrtoul(arg + 1, 0, ¶m);
403 trace_probe_log_err(offs, BAD_MEM_ADDR);
407 code->op = FETCH_OP_IMM;
408 code->immediate = param;
409 } else if (arg[1] == '+') {
410 /* kprobes don't support file offsets */
411 if (flags & TPARG_FL_KERNEL) {
412 trace_probe_log_err(offs, FILE_ON_KPROBE);
415 ret = kstrtol(arg + 2, 0, &offset);
417 trace_probe_log_err(offs, BAD_FILE_OFFS);
421 code->op = FETCH_OP_FOFFS;
422 code->immediate = (unsigned long)offset; // imm64?
424 /* uprobes don't support symbols */
425 if (!(flags & TPARG_FL_KERNEL)) {
426 trace_probe_log_err(offs, SYM_ON_UPROBE);
429 /* Preserve symbol for updating */
430 code->op = FETCH_NOP_SYMBOL;
431 code->data = kstrdup(arg + 1, GFP_KERNEL);
435 trace_probe_log_err(offs, TOO_MANY_OPS);
438 code->op = FETCH_OP_IMM;
441 /* These are fetching from memory */
443 trace_probe_log_err(offs, TOO_MANY_OPS);
447 code->op = FETCH_OP_DEREF;
448 code->offset = offset;
451 case '+': /* deref memory */
454 deref = FETCH_OP_UDEREF;
459 arg++; /* Skip '+', because kstrtol() rejects it. */
460 tmp = strchr(arg, '(');
462 trace_probe_log_err(offs, DEREF_NEED_BRACE);
466 ret = kstrtol(arg, 0, &offset);
468 trace_probe_log_err(offs, BAD_DEREF_OFFS);
471 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
473 tmp = strrchr(arg, ')');
475 trace_probe_log_err(offs + strlen(arg),
479 const struct fetch_type *t2 = find_fetch_type(NULL);
482 ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
485 if (code->op == FETCH_OP_COMM ||
486 code->op == FETCH_OP_DATA) {
487 trace_probe_log_err(offs, COMM_CANT_DEREF);
491 trace_probe_log_err(offs, TOO_MANY_OPS);
497 code->offset = offset;
500 case '\\': /* Immediate value */
501 if (arg[1] == '"') { /* Immediate string */
502 ret = __parse_imm_string(arg + 2, &tmp, offs + 2);
505 code->op = FETCH_OP_DATA;
508 ret = str_to_immediate(arg + 1, &code->immediate);
510 trace_probe_log_err(offs + 1, BAD_IMM);
512 code->op = FETCH_OP_IMM;
516 if (!ret && code->op == FETCH_OP_NOP) {
517 /* Parsed, but do not find fetch method */
518 trace_probe_log_err(offs, BAD_FETCH_ARG);
524 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
526 /* Bitfield type needs to be parsed into a fetch function */
527 static int __parse_bitfield_probe_arg(const char *bf,
528 const struct fetch_type *t,
529 struct fetch_insn **pcode)
531 struct fetch_insn *code = *pcode;
532 unsigned long bw, bo;
538 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
540 if (bw == 0 || *tail != '@')
544 bo = simple_strtoul(bf, &tail, 0);
546 if (tail == bf || *tail != '/')
549 if (code->op != FETCH_OP_NOP)
553 code->op = FETCH_OP_MOD_BF;
554 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
555 code->rshift = BYTES_TO_BITS(t->size) - bw;
556 code->basesize = t->size;
558 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
561 /* String length checking wrapper */
562 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
563 struct probe_arg *parg, unsigned int flags, int offset)
565 struct fetch_insn *code, *scode, *tmp = NULL;
570 arg = kstrdup(argv, GFP_KERNEL);
576 if (len > MAX_ARGSTR_LEN) {
577 trace_probe_log_err(offset, ARG_TOO_LONG);
579 } else if (len == 0) {
580 trace_probe_log_err(offset, NO_ARG_BODY);
585 parg->comm = kstrdup(arg, GFP_KERNEL);
590 t = strchr(arg, ':');
593 t2 = strchr(++t, '[');
596 t3 = strchr(t2, ']');
598 offset += t2 + strlen(t2) - arg;
599 trace_probe_log_err(offset,
602 } else if (t3[1] != '\0') {
603 trace_probe_log_err(offset + t3 + 1 - arg,
608 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
609 trace_probe_log_err(offset + t2 - arg,
613 if (parg->count > MAX_ARRAY_LEN) {
614 trace_probe_log_err(offset + t2 - arg,
622 * Since $comm and immediate string can not be dereferenced,
623 * we can find those by strcmp. But ignore for eprobes.
625 if (!(flags & TPARG_FL_TPOINT) &&
626 (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
627 strncmp(arg, "\\\"", 2) == 0)) {
628 /* The type of $comm must be "string", and not an array. */
629 if (parg->count || (t && strcmp(t, "string")))
631 parg->type = find_fetch_type("string");
633 parg->type = find_fetch_type(t);
635 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
638 parg->offset = *size;
639 *size += parg->type->size * (parg->count ?: 1);
643 len = strlen(parg->type->fmttype) + 6;
644 parg->fmt = kmalloc(len, GFP_KERNEL);
647 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
651 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
654 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
656 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
662 /* Store operation */
663 if (parg->type->is_string) {
664 if (!strcmp(parg->type->name, "symstr")) {
665 if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
666 code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
667 code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
668 trace_probe_log_err(offset + (t ? (t - arg) : 0),
673 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
674 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
675 code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
676 trace_probe_log_err(offset + (t ? (t - arg) : 0),
681 if (!strcmp(parg->type->name, "symstr") ||
682 (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
683 code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
686 * IMM, DATA and COMM is pointing actual address, those
687 * must be kept, and if parg->count != 0, this is an
688 * array of string pointers instead of string address
690 * For the symstr, it doesn't need to dereference, thus
691 * it just get the value.
694 if (code->op != FETCH_OP_NOP) {
695 trace_probe_log_err(offset, TOO_MANY_OPS);
699 /* If op == DEREF, replace it with STRING */
700 if (!strcmp(parg->type->name, "ustring") ||
701 code->op == FETCH_OP_UDEREF)
702 code->op = FETCH_OP_ST_USTRING;
703 else if (!strcmp(parg->type->name, "symstr"))
704 code->op = FETCH_OP_ST_SYMSTR;
706 code->op = FETCH_OP_ST_STRING;
707 code->size = parg->type->size;
708 parg->dynamic = true;
709 } else if (code->op == FETCH_OP_DEREF) {
710 code->op = FETCH_OP_ST_MEM;
711 code->size = parg->type->size;
712 } else if (code->op == FETCH_OP_UDEREF) {
713 code->op = FETCH_OP_ST_UMEM;
714 code->size = parg->type->size;
717 if (code->op != FETCH_OP_NOP) {
718 trace_probe_log_err(offset, TOO_MANY_OPS);
721 code->op = FETCH_OP_ST_RAW;
722 code->size = parg->type->size;
725 /* Modify operation */
727 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
729 trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
734 /* Loop(Array) operation */
736 if (scode->op != FETCH_OP_ST_MEM &&
737 scode->op != FETCH_OP_ST_STRING &&
738 scode->op != FETCH_OP_ST_USTRING) {
739 trace_probe_log_err(offset + (t ? (t - arg) : 0),
744 if (code->op != FETCH_OP_NOP) {
745 trace_probe_log_err(offset, TOO_MANY_OPS);
748 code->op = FETCH_OP_LP_ARRAY;
749 code->param = parg->count;
752 code->op = FETCH_OP_END;
755 /* Shrink down the code buffer */
756 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
760 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
764 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
765 if (code->op == FETCH_NOP_SYMBOL ||
766 code->op == FETCH_OP_DATA)
776 /* Return 1 if name is reserved or already used by another argument */
777 static int traceprobe_conflict_field_name(const char *name,
778 struct probe_arg *args, int narg)
782 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
783 if (strcmp(reserved_field_names[i], name) == 0)
786 for (i = 0; i < narg; i++)
787 if (strcmp(args[i].name, name) == 0)
793 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
796 struct probe_arg *parg = &tp->args[i];
799 /* Increment count for freeing args in error case */
802 body = strchr(arg, '=');
804 if (body - arg > MAX_ARG_NAME_LEN) {
805 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
807 } else if (body == arg) {
808 trace_probe_log_err(0, NO_ARG_NAME);
811 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
814 /* If argument name is omitted, set "argN" */
815 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
821 if (!is_good_name(parg->name)) {
822 trace_probe_log_err(0, BAD_ARG_NAME);
825 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
826 trace_probe_log_err(0, USED_ARG_NAME);
829 /* Parse fetch argument */
830 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
834 void traceprobe_free_probe_arg(struct probe_arg *arg)
836 struct fetch_insn *code = arg->code;
838 while (code && code->op != FETCH_OP_END) {
839 if (code->op == FETCH_NOP_SYMBOL ||
840 code->op == FETCH_OP_DATA)
850 int traceprobe_update_arg(struct probe_arg *arg)
852 struct fetch_insn *code = arg->code;
858 while (code && code->op != FETCH_OP_END) {
859 if (code->op == FETCH_NOP_SYMBOL) {
860 if (code[1].op != FETCH_OP_IMM)
863 tmp = strpbrk(code->data, "+-");
866 ret = traceprobe_split_symbol_offset(code->data,
872 (unsigned long)kallsyms_lookup_name(code->data);
875 if (!code[1].immediate)
877 code[1].immediate += offset;
884 /* When len=0, we just calculate the needed length */
885 #define LEN_OR_ZERO (len ? len - pos : 0)
886 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
887 enum probe_print_type ptype)
889 struct probe_arg *parg;
892 const char *fmt, *arg;
895 case PROBE_PRINT_NORMAL:
897 arg = "REC->" FIELD_STRING_IP;
899 case PROBE_PRINT_RETURN:
900 fmt = "(%lx <- %lx)";
901 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
903 case PROBE_PRINT_EVENT:
905 arg = "REC->" FIELD_STRING_TYPE;
912 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
914 for (i = 0; i < tp->nr_args; i++) {
916 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
918 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
920 for (j = 1; j < parg->count; j++)
921 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
923 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
925 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
929 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
931 for (i = 0; i < tp->nr_args; i++) {
934 if (parg->type->is_string)
935 fmt = ", __get_str(%s[%d])";
937 fmt = ", REC->%s[%d]";
938 for (j = 0; j < parg->count; j++)
939 pos += snprintf(buf + pos, LEN_OR_ZERO,
942 if (parg->type->is_string)
943 fmt = ", __get_str(%s)";
946 pos += snprintf(buf + pos, LEN_OR_ZERO,
951 /* return the length of print_fmt */
956 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
958 struct trace_event_call *call = trace_probe_event_call(tp);
962 /* First: called with 0 length to calculate the needed length */
963 len = __set_print_fmt(tp, NULL, 0, ptype);
964 print_fmt = kmalloc(len + 1, GFP_KERNEL);
968 /* Second: actually write the @print_fmt */
969 __set_print_fmt(tp, print_fmt, len + 1, ptype);
970 call->print_fmt = print_fmt;
975 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
976 size_t offset, struct trace_probe *tp)
980 /* Set argument names as fields */
981 for (i = 0; i < tp->nr_args; i++) {
982 struct probe_arg *parg = &tp->args[i];
983 const char *fmt = parg->type->fmttype;
984 int size = parg->type->size;
990 ret = trace_define_field(event_call, fmt, parg->name,
991 offset + parg->offset, size,
992 parg->type->is_signed,
1000 static void trace_probe_event_free(struct trace_probe_event *tpe)
1002 kfree(tpe->class.system);
1003 kfree(tpe->call.name);
1004 kfree(tpe->call.print_fmt);
1008 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
1010 if (trace_probe_has_sibling(tp))
1013 list_del_init(&tp->list);
1014 trace_probe_event_free(tp->event);
1016 tp->event = to->event;
1017 list_add_tail(&tp->list, trace_probe_probe_list(to));
1022 void trace_probe_unlink(struct trace_probe *tp)
1024 list_del_init(&tp->list);
1025 if (list_empty(trace_probe_probe_list(tp)))
1026 trace_probe_event_free(tp->event);
1030 void trace_probe_cleanup(struct trace_probe *tp)
1034 for (i = 0; i < tp->nr_args; i++)
1035 traceprobe_free_probe_arg(&tp->args[i]);
1038 trace_probe_unlink(tp);
1041 int trace_probe_init(struct trace_probe *tp, const char *event,
1042 const char *group, bool alloc_filter)
1044 struct trace_event_call *call;
1045 size_t size = sizeof(struct trace_probe_event);
1048 if (!event || !group)
1052 size += sizeof(struct trace_uprobe_filter);
1054 tp->event = kzalloc(size, GFP_KERNEL);
1058 INIT_LIST_HEAD(&tp->event->files);
1059 INIT_LIST_HEAD(&tp->event->class.fields);
1060 INIT_LIST_HEAD(&tp->event->probes);
1061 INIT_LIST_HEAD(&tp->list);
1062 list_add(&tp->list, &tp->event->probes);
1064 call = trace_probe_event_call(tp);
1065 call->class = &tp->event->class;
1066 call->name = kstrdup(event, GFP_KERNEL);
1072 tp->event->class.system = kstrdup(group, GFP_KERNEL);
1073 if (!tp->event->class.system) {
1081 trace_probe_cleanup(tp);
1085 static struct trace_event_call *
1086 find_trace_event_call(const char *system, const char *event_name)
1088 struct trace_event_call *tp_event;
1091 list_for_each_entry(tp_event, &ftrace_events, list) {
1092 if (!tp_event->class->system ||
1093 strcmp(system, tp_event->class->system))
1095 name = trace_event_name(tp_event);
1096 if (!name || strcmp(event_name, name))
1104 int trace_probe_register_event_call(struct trace_probe *tp)
1106 struct trace_event_call *call = trace_probe_event_call(tp);
1109 lockdep_assert_held(&event_mutex);
1111 if (find_trace_event_call(trace_probe_group_name(tp),
1112 trace_probe_name(tp)))
1115 ret = register_trace_event(&call->event);
1119 ret = trace_add_event_call(call);
1121 unregister_trace_event(&call->event);
1126 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1128 struct event_file_link *link;
1130 link = kmalloc(sizeof(*link), GFP_KERNEL);
1135 INIT_LIST_HEAD(&link->list);
1136 list_add_tail_rcu(&link->list, &tp->event->files);
1137 trace_probe_set_flag(tp, TP_FLAG_TRACE);
1141 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1142 struct trace_event_file *file)
1144 struct event_file_link *link;
1146 trace_probe_for_each_link(link, tp) {
1147 if (link->file == file)
1154 int trace_probe_remove_file(struct trace_probe *tp,
1155 struct trace_event_file *file)
1157 struct event_file_link *link;
1159 link = trace_probe_get_file_link(tp, file);
1163 list_del_rcu(&link->list);
1167 if (list_empty(&tp->event->files))
1168 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
1174 * Return the smallest index of different type argument (start from 1).
1175 * If all argument types and name are same, return 0.
1177 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1181 /* In case of more arguments */
1182 if (a->nr_args < b->nr_args)
1183 return a->nr_args + 1;
1184 if (a->nr_args > b->nr_args)
1185 return b->nr_args + 1;
1187 for (i = 0; i < a->nr_args; i++) {
1188 if ((b->nr_args <= i) ||
1189 ((a->args[i].type != b->args[i].type) ||
1190 (a->args[i].count != b->args[i].count) ||
1191 strcmp(a->args[i].name, b->args[i].name)))
1198 bool trace_probe_match_command_args(struct trace_probe *tp,
1199 int argc, const char **argv)
1201 char buf[MAX_ARGSTR_LEN + 1];
1204 if (tp->nr_args < argc)
1207 for (i = 0; i < argc; i++) {
1208 snprintf(buf, sizeof(buf), "%s=%s",
1209 tp->args[i].name, tp->args[i].comm);
1210 if (strcmp(buf, argv[i]))
1216 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
1218 int argc = 0, ret = 0;
1221 argv = argv_split(GFP_KERNEL, raw_command, &argc);
1226 ret = createfn(argc, (const char **)argv);