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, unsigned long flags)
107 /* Reject the symbol/symstr for uprobes */
108 if (type && (flags & TPARG_FL_USER) &&
109 (!strcmp(type, "symbol") || !strcmp(type, "symstr")))
113 type = DEFAULT_FETCH_TYPE_STR;
115 /* Special case: bitfield */
119 type = strchr(type, '/');
124 if (kstrtoul(type, 0, &bs))
129 return find_fetch_type("u8", flags);
131 return find_fetch_type("u16", flags);
133 return find_fetch_type("u32", flags);
135 return find_fetch_type("u64", flags);
141 for (i = 0; probe_fetch_types[i].name; i++) {
142 if (strcmp(type, probe_fetch_types[i].name) == 0)
143 return &probe_fetch_types[i];
150 static struct trace_probe_log trace_probe_log;
152 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
154 trace_probe_log.subsystem = subsystem;
155 trace_probe_log.argc = argc;
156 trace_probe_log.argv = argv;
157 trace_probe_log.index = 0;
160 void trace_probe_log_clear(void)
162 memset(&trace_probe_log, 0, sizeof(trace_probe_log));
165 void trace_probe_log_set_index(int index)
167 trace_probe_log.index = index;
170 void __trace_probe_log_err(int offset, int err_type)
173 int i, len = 0, pos = 0;
175 if (!trace_probe_log.argv)
178 /* Recalculate the length and allocate buffer */
179 for (i = 0; i < trace_probe_log.argc; i++) {
180 if (i == trace_probe_log.index)
182 len += strlen(trace_probe_log.argv[i]) + 1;
184 command = kzalloc(len, GFP_KERNEL);
188 if (trace_probe_log.index >= trace_probe_log.argc) {
190 * Set the error position is next to the last arg + space.
191 * Note that len includes the terminal null and the cursor
192 * appears at pos + 1.
198 /* And make a command string from argv array */
200 for (i = 0; i < trace_probe_log.argc; i++) {
201 len = strlen(trace_probe_log.argv[i]);
202 strcpy(p, trace_probe_log.argv[i]);
208 tracing_log_err(NULL, trace_probe_log.subsystem, command,
209 trace_probe_err_text, err_type, pos + offset);
214 /* Split symbol and offset. */
215 int traceprobe_split_symbol_offset(char *symbol, long *offset)
223 tmp = strpbrk(symbol, "+-");
225 ret = kstrtol(tmp, 0, offset);
235 /* @buf must has MAX_EVENT_NAME_LEN size */
236 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
237 char *buf, int offset)
239 const char *slash, *event = *pevent;
242 slash = strchr(event, '/');
244 slash = strchr(event, '.');
247 if (slash == event) {
248 trace_probe_log_err(offset, NO_GROUP_NAME);
251 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
252 trace_probe_log_err(offset, GROUP_TOO_LONG);
255 strlcpy(buf, event, slash - event + 1);
256 if (!is_good_system_name(buf)) {
257 trace_probe_log_err(offset, BAD_GROUP_NAME);
262 offset += slash - event + 1;
271 trace_probe_log_err(offset, NO_EVENT_NAME);
273 } else if (len > MAX_EVENT_NAME_LEN) {
274 trace_probe_log_err(offset, EVENT_TOO_LONG);
277 if (!is_good_name(event)) {
278 trace_probe_log_err(offset, BAD_EVENT_NAME);
284 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
286 static int parse_probe_vars(char *arg, const struct fetch_type *t,
287 struct fetch_insn *code, unsigned int flags, int offs)
293 if (flags & TPARG_FL_TPOINT) {
296 code->data = kstrdup(arg, GFP_KERNEL);
299 code->op = FETCH_OP_TP_ARG;
300 } else if (strcmp(arg, "retval") == 0) {
301 if (flags & TPARG_FL_RETURN) {
302 code->op = FETCH_OP_RETVAL;
304 trace_probe_log_err(offs, RETVAL_ON_PROBE);
307 } else if ((len = str_has_prefix(arg, "stack"))) {
308 if (arg[len] == '\0') {
309 code->op = FETCH_OP_STACKP;
310 } else if (isdigit(arg[len])) {
311 ret = kstrtoul(arg + len, 10, ¶m);
314 } else if ((flags & TPARG_FL_KERNEL) &&
315 param > PARAM_MAX_STACK) {
316 trace_probe_log_err(offs, BAD_STACK_NUM);
319 code->op = FETCH_OP_STACK;
320 code->param = (unsigned int)param;
324 } else if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
325 code->op = FETCH_OP_COMM;
326 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
327 } else if (((flags & TPARG_FL_MASK) ==
328 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
329 (len = str_has_prefix(arg, "arg"))) {
330 ret = kstrtoul(arg + len, 10, ¶m);
333 } else if (!param || param > PARAM_MAX_STACK) {
334 trace_probe_log_err(offs, BAD_ARG_NUM);
337 code->op = FETCH_OP_ARG;
338 code->param = (unsigned int)param - 1;
346 trace_probe_log_err(offs, BAD_VAR);
350 static int str_to_immediate(char *str, unsigned long *imm)
353 return kstrtoul(str, 0, imm);
354 else if (str[0] == '-')
355 return kstrtol(str, 0, (long *)imm);
356 else if (str[0] == '+')
357 return kstrtol(str + 1, 0, (long *)imm);
361 static int __parse_imm_string(char *str, char **pbuf, int offs)
363 size_t len = strlen(str);
365 if (str[len - 1] != '"') {
366 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
369 *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
375 /* Recursive argument parser */
377 parse_probe_arg(char *arg, const struct fetch_type *type,
378 struct fetch_insn **pcode, struct fetch_insn *end,
379 unsigned int flags, int offs)
381 struct fetch_insn *code = *pcode;
383 int deref = FETCH_OP_DEREF;
390 ret = parse_probe_vars(arg + 1, type, code, flags, offs);
393 case '%': /* named register */
394 if (flags & TPARG_FL_TPOINT) {
395 /* eprobes do not handle registers */
396 trace_probe_log_err(offs, BAD_VAR);
399 ret = regs_query_register_offset(arg + 1);
401 code->op = FETCH_OP_REG;
402 code->param = (unsigned int)ret;
405 trace_probe_log_err(offs, BAD_REG_NAME);
408 case '@': /* memory, file-offset or symbol */
409 if (isdigit(arg[1])) {
410 ret = kstrtoul(arg + 1, 0, ¶m);
412 trace_probe_log_err(offs, BAD_MEM_ADDR);
416 code->op = FETCH_OP_IMM;
417 code->immediate = param;
418 } else if (arg[1] == '+') {
419 /* kprobes don't support file offsets */
420 if (flags & TPARG_FL_KERNEL) {
421 trace_probe_log_err(offs, FILE_ON_KPROBE);
424 ret = kstrtol(arg + 2, 0, &offset);
426 trace_probe_log_err(offs, BAD_FILE_OFFS);
430 code->op = FETCH_OP_FOFFS;
431 code->immediate = (unsigned long)offset; // imm64?
433 /* uprobes don't support symbols */
434 if (!(flags & TPARG_FL_KERNEL)) {
435 trace_probe_log_err(offs, SYM_ON_UPROBE);
438 /* Preserve symbol for updating */
439 code->op = FETCH_NOP_SYMBOL;
440 code->data = kstrdup(arg + 1, GFP_KERNEL);
444 trace_probe_log_err(offs, TOO_MANY_OPS);
447 code->op = FETCH_OP_IMM;
450 /* These are fetching from memory */
452 trace_probe_log_err(offs, TOO_MANY_OPS);
456 code->op = FETCH_OP_DEREF;
457 code->offset = offset;
460 case '+': /* deref memory */
463 deref = FETCH_OP_UDEREF;
468 arg++; /* Skip '+', because kstrtol() rejects it. */
469 tmp = strchr(arg, '(');
471 trace_probe_log_err(offs, DEREF_NEED_BRACE);
475 ret = kstrtol(arg, 0, &offset);
477 trace_probe_log_err(offs, BAD_DEREF_OFFS);
480 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
482 tmp = strrchr(arg, ')');
484 trace_probe_log_err(offs + strlen(arg),
488 const struct fetch_type *t2 = find_fetch_type(NULL, flags);
491 ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
494 if (code->op == FETCH_OP_COMM ||
495 code->op == FETCH_OP_DATA) {
496 trace_probe_log_err(offs, COMM_CANT_DEREF);
500 trace_probe_log_err(offs, TOO_MANY_OPS);
506 code->offset = offset;
509 case '\\': /* Immediate value */
510 if (arg[1] == '"') { /* Immediate string */
511 ret = __parse_imm_string(arg + 2, &tmp, offs + 2);
514 code->op = FETCH_OP_DATA;
517 ret = str_to_immediate(arg + 1, &code->immediate);
519 trace_probe_log_err(offs + 1, BAD_IMM);
521 code->op = FETCH_OP_IMM;
525 if (!ret && code->op == FETCH_OP_NOP) {
526 /* Parsed, but do not find fetch method */
527 trace_probe_log_err(offs, BAD_FETCH_ARG);
533 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
535 /* Bitfield type needs to be parsed into a fetch function */
536 static int __parse_bitfield_probe_arg(const char *bf,
537 const struct fetch_type *t,
538 struct fetch_insn **pcode)
540 struct fetch_insn *code = *pcode;
541 unsigned long bw, bo;
547 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
549 if (bw == 0 || *tail != '@')
553 bo = simple_strtoul(bf, &tail, 0);
555 if (tail == bf || *tail != '/')
558 if (code->op != FETCH_OP_NOP)
562 code->op = FETCH_OP_MOD_BF;
563 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
564 code->rshift = BYTES_TO_BITS(t->size) - bw;
565 code->basesize = t->size;
567 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
570 /* String length checking wrapper */
571 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
572 struct probe_arg *parg, unsigned int flags, int offset)
574 struct fetch_insn *code, *scode, *tmp = NULL;
579 arg = kstrdup(argv, GFP_KERNEL);
585 if (len > MAX_ARGSTR_LEN) {
586 trace_probe_log_err(offset, ARG_TOO_LONG);
588 } else if (len == 0) {
589 trace_probe_log_err(offset, NO_ARG_BODY);
594 parg->comm = kstrdup(arg, GFP_KERNEL);
599 t = strchr(arg, ':');
602 t2 = strchr(++t, '[');
605 t3 = strchr(t2, ']');
607 offset += t2 + strlen(t2) - arg;
608 trace_probe_log_err(offset,
611 } else if (t3[1] != '\0') {
612 trace_probe_log_err(offset + t3 + 1 - arg,
617 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
618 trace_probe_log_err(offset + t2 - arg,
622 if (parg->count > MAX_ARRAY_LEN) {
623 trace_probe_log_err(offset + t2 - arg,
631 * Since $comm and immediate string can not be dereferenced,
632 * we can find those by strcmp. But ignore for eprobes.
634 if (!(flags & TPARG_FL_TPOINT) &&
635 (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
636 strncmp(arg, "\\\"", 2) == 0)) {
637 /* The type of $comm must be "string", and not an array. */
638 if (parg->count || (t && strcmp(t, "string")))
640 parg->type = find_fetch_type("string", flags);
642 parg->type = find_fetch_type(t, flags);
644 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
647 parg->offset = *size;
648 *size += parg->type->size * (parg->count ?: 1);
652 len = strlen(parg->type->fmttype) + 6;
653 parg->fmt = kmalloc(len, GFP_KERNEL);
656 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
660 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
663 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
665 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
671 /* Store operation */
672 if (parg->type->is_string) {
673 if (!strcmp(parg->type->name, "symstr")) {
674 if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
675 code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
676 code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
677 trace_probe_log_err(offset + (t ? (t - arg) : 0),
682 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
683 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
684 code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
685 trace_probe_log_err(offset + (t ? (t - arg) : 0),
690 if (!strcmp(parg->type->name, "symstr") ||
691 (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
692 code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
695 * IMM, DATA and COMM is pointing actual address, those
696 * must be kept, and if parg->count != 0, this is an
697 * array of string pointers instead of string address
699 * For the symstr, it doesn't need to dereference, thus
700 * it just get the value.
703 if (code->op != FETCH_OP_NOP) {
704 trace_probe_log_err(offset, TOO_MANY_OPS);
708 /* If op == DEREF, replace it with STRING */
709 if (!strcmp(parg->type->name, "ustring") ||
710 code->op == FETCH_OP_UDEREF)
711 code->op = FETCH_OP_ST_USTRING;
712 else if (!strcmp(parg->type->name, "symstr"))
713 code->op = FETCH_OP_ST_SYMSTR;
715 code->op = FETCH_OP_ST_STRING;
716 code->size = parg->type->size;
717 parg->dynamic = true;
718 } else if (code->op == FETCH_OP_DEREF) {
719 code->op = FETCH_OP_ST_MEM;
720 code->size = parg->type->size;
721 } else if (code->op == FETCH_OP_UDEREF) {
722 code->op = FETCH_OP_ST_UMEM;
723 code->size = parg->type->size;
726 if (code->op != FETCH_OP_NOP) {
727 trace_probe_log_err(offset, TOO_MANY_OPS);
730 code->op = FETCH_OP_ST_RAW;
731 code->size = parg->type->size;
734 /* Modify operation */
736 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
738 trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
743 /* Loop(Array) operation */
745 if (scode->op != FETCH_OP_ST_MEM &&
746 scode->op != FETCH_OP_ST_STRING &&
747 scode->op != FETCH_OP_ST_USTRING) {
748 trace_probe_log_err(offset + (t ? (t - arg) : 0),
753 if (code->op != FETCH_OP_NOP) {
754 trace_probe_log_err(offset, TOO_MANY_OPS);
757 code->op = FETCH_OP_LP_ARRAY;
758 code->param = parg->count;
761 code->op = FETCH_OP_END;
764 /* Shrink down the code buffer */
765 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
769 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
773 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
774 if (code->op == FETCH_NOP_SYMBOL ||
775 code->op == FETCH_OP_DATA)
785 /* Return 1 if name is reserved or already used by another argument */
786 static int traceprobe_conflict_field_name(const char *name,
787 struct probe_arg *args, int narg)
791 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
792 if (strcmp(reserved_field_names[i], name) == 0)
795 for (i = 0; i < narg; i++)
796 if (strcmp(args[i].name, name) == 0)
802 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
805 struct probe_arg *parg = &tp->args[i];
808 /* Increment count for freeing args in error case */
811 body = strchr(arg, '=');
813 if (body - arg > MAX_ARG_NAME_LEN) {
814 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
816 } else if (body == arg) {
817 trace_probe_log_err(0, NO_ARG_NAME);
820 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
823 /* If argument name is omitted, set "argN" */
824 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
830 if (!is_good_name(parg->name)) {
831 trace_probe_log_err(0, BAD_ARG_NAME);
834 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
835 trace_probe_log_err(0, USED_ARG_NAME);
838 /* Parse fetch argument */
839 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
843 void traceprobe_free_probe_arg(struct probe_arg *arg)
845 struct fetch_insn *code = arg->code;
847 while (code && code->op != FETCH_OP_END) {
848 if (code->op == FETCH_NOP_SYMBOL ||
849 code->op == FETCH_OP_DATA)
859 int traceprobe_update_arg(struct probe_arg *arg)
861 struct fetch_insn *code = arg->code;
867 while (code && code->op != FETCH_OP_END) {
868 if (code->op == FETCH_NOP_SYMBOL) {
869 if (code[1].op != FETCH_OP_IMM)
872 tmp = strpbrk(code->data, "+-");
875 ret = traceprobe_split_symbol_offset(code->data,
881 (unsigned long)kallsyms_lookup_name(code->data);
884 if (!code[1].immediate)
886 code[1].immediate += offset;
893 /* When len=0, we just calculate the needed length */
894 #define LEN_OR_ZERO (len ? len - pos : 0)
895 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
896 enum probe_print_type ptype)
898 struct probe_arg *parg;
901 const char *fmt, *arg;
904 case PROBE_PRINT_NORMAL:
906 arg = ", REC->" FIELD_STRING_IP;
908 case PROBE_PRINT_RETURN:
909 fmt = "(%lx <- %lx)";
910 arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
912 case PROBE_PRINT_EVENT:
921 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
923 for (i = 0; i < tp->nr_args; i++) {
925 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
927 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
929 for (j = 1; j < parg->count; j++)
930 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
932 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
934 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
938 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg);
940 for (i = 0; i < tp->nr_args; i++) {
943 if (parg->type->is_string)
944 fmt = ", __get_str(%s[%d])";
946 fmt = ", REC->%s[%d]";
947 for (j = 0; j < parg->count; j++)
948 pos += snprintf(buf + pos, LEN_OR_ZERO,
951 if (parg->type->is_string)
952 fmt = ", __get_str(%s)";
955 pos += snprintf(buf + pos, LEN_OR_ZERO,
960 /* return the length of print_fmt */
965 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
967 struct trace_event_call *call = trace_probe_event_call(tp);
971 /* First: called with 0 length to calculate the needed length */
972 len = __set_print_fmt(tp, NULL, 0, ptype);
973 print_fmt = kmalloc(len + 1, GFP_KERNEL);
977 /* Second: actually write the @print_fmt */
978 __set_print_fmt(tp, print_fmt, len + 1, ptype);
979 call->print_fmt = print_fmt;
984 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
985 size_t offset, struct trace_probe *tp)
989 /* Set argument names as fields */
990 for (i = 0; i < tp->nr_args; i++) {
991 struct probe_arg *parg = &tp->args[i];
992 const char *fmt = parg->type->fmttype;
993 int size = parg->type->size;
999 ret = trace_define_field(event_call, fmt, parg->name,
1000 offset + parg->offset, size,
1001 parg->type->is_signed,
1009 static void trace_probe_event_free(struct trace_probe_event *tpe)
1011 kfree(tpe->class.system);
1012 kfree(tpe->call.name);
1013 kfree(tpe->call.print_fmt);
1017 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
1019 if (trace_probe_has_sibling(tp))
1022 list_del_init(&tp->list);
1023 trace_probe_event_free(tp->event);
1025 tp->event = to->event;
1026 list_add_tail(&tp->list, trace_probe_probe_list(to));
1031 void trace_probe_unlink(struct trace_probe *tp)
1033 list_del_init(&tp->list);
1034 if (list_empty(trace_probe_probe_list(tp)))
1035 trace_probe_event_free(tp->event);
1039 void trace_probe_cleanup(struct trace_probe *tp)
1043 for (i = 0; i < tp->nr_args; i++)
1044 traceprobe_free_probe_arg(&tp->args[i]);
1047 trace_probe_unlink(tp);
1050 int trace_probe_init(struct trace_probe *tp, const char *event,
1051 const char *group, bool alloc_filter)
1053 struct trace_event_call *call;
1054 size_t size = sizeof(struct trace_probe_event);
1057 if (!event || !group)
1061 size += sizeof(struct trace_uprobe_filter);
1063 tp->event = kzalloc(size, GFP_KERNEL);
1067 INIT_LIST_HEAD(&tp->event->files);
1068 INIT_LIST_HEAD(&tp->event->class.fields);
1069 INIT_LIST_HEAD(&tp->event->probes);
1070 INIT_LIST_HEAD(&tp->list);
1071 list_add(&tp->list, &tp->event->probes);
1073 call = trace_probe_event_call(tp);
1074 call->class = &tp->event->class;
1075 call->name = kstrdup(event, GFP_KERNEL);
1081 tp->event->class.system = kstrdup(group, GFP_KERNEL);
1082 if (!tp->event->class.system) {
1090 trace_probe_cleanup(tp);
1094 static struct trace_event_call *
1095 find_trace_event_call(const char *system, const char *event_name)
1097 struct trace_event_call *tp_event;
1100 list_for_each_entry(tp_event, &ftrace_events, list) {
1101 if (!tp_event->class->system ||
1102 strcmp(system, tp_event->class->system))
1104 name = trace_event_name(tp_event);
1105 if (!name || strcmp(event_name, name))
1113 int trace_probe_register_event_call(struct trace_probe *tp)
1115 struct trace_event_call *call = trace_probe_event_call(tp);
1118 lockdep_assert_held(&event_mutex);
1120 if (find_trace_event_call(trace_probe_group_name(tp),
1121 trace_probe_name(tp)))
1124 ret = register_trace_event(&call->event);
1128 ret = trace_add_event_call(call);
1130 unregister_trace_event(&call->event);
1135 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1137 struct event_file_link *link;
1139 link = kmalloc(sizeof(*link), GFP_KERNEL);
1144 INIT_LIST_HEAD(&link->list);
1145 list_add_tail_rcu(&link->list, &tp->event->files);
1146 trace_probe_set_flag(tp, TP_FLAG_TRACE);
1150 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1151 struct trace_event_file *file)
1153 struct event_file_link *link;
1155 trace_probe_for_each_link(link, tp) {
1156 if (link->file == file)
1163 int trace_probe_remove_file(struct trace_probe *tp,
1164 struct trace_event_file *file)
1166 struct event_file_link *link;
1168 link = trace_probe_get_file_link(tp, file);
1172 list_del_rcu(&link->list);
1175 if (list_empty(&tp->event->files))
1176 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
1182 * Return the smallest index of different type argument (start from 1).
1183 * If all argument types and name are same, return 0.
1185 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1189 /* In case of more arguments */
1190 if (a->nr_args < b->nr_args)
1191 return a->nr_args + 1;
1192 if (a->nr_args > b->nr_args)
1193 return b->nr_args + 1;
1195 for (i = 0; i < a->nr_args; i++) {
1196 if ((b->nr_args <= i) ||
1197 ((a->args[i].type != b->args[i].type) ||
1198 (a->args[i].count != b->args[i].count) ||
1199 strcmp(a->args[i].name, b->args[i].name)))
1206 bool trace_probe_match_command_args(struct trace_probe *tp,
1207 int argc, const char **argv)
1209 char buf[MAX_ARGSTR_LEN + 1];
1212 if (tp->nr_args < argc)
1215 for (i = 0; i < argc; i++) {
1216 snprintf(buf, sizeof(buf), "%s=%s",
1217 tp->args[i].name, tp->args[i].comm);
1218 if (strcmp(buf, argv[i]))
1224 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
1226 int argc = 0, ret = 0;
1229 argv = argv_split(GFP_KERNEL, raw_command, &argc);
1234 ret = createfn(argc, (const char **)argv);