1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Builtin probe command: Set up probe events by C expression
7 * Written by Masami Hiramatsu <mhiramat@redhat.com>
9 #include <sys/utsname.h>
10 #include <sys/types.h>
21 #include "namespaces.h"
22 #include "util/strlist.h"
23 #include "util/strfilter.h"
24 #include "util/symbol.h"
25 #include "util/debug.h"
26 #include <subcmd/parse-options.h>
27 #include "util/probe-finder.h"
28 #include "util/probe-event.h"
29 #include "util/probe-file.h"
30 #include <linux/zalloc.h>
32 #define DEFAULT_VAR_FILTER "!__k???tab_* & !__crc_*"
33 #define DEFAULT_FUNC_FILTER "!_*"
34 #define DEFAULT_LIST_FILTER "*"
36 /* Session management structure */
38 int command
; /* Command short_name */
44 struct perf_probe_event events
[MAX_PROBES
];
45 struct line_range line_range
;
47 struct strfilter
*filter
;
51 /* Parse an event definition. Note that any error must die. */
52 static int parse_probe_event(const char *str
)
54 struct perf_probe_event
*pev
= ¶ms
.events
[params
.nevents
];
57 pr_debug("probe-definition(%d): %s\n", params
.nevents
, str
);
58 if (++params
.nevents
== MAX_PROBES
) {
59 pr_err("Too many probes (> %d) were specified.", MAX_PROBES
);
63 pev
->uprobes
= params
.uprobes
;
65 pev
->target
= strdup(params
.target
);
68 params
.target_used
= true;
71 pev
->nsi
= nsinfo__get(params
.nsi
);
73 /* Parse a perf-probe command into event */
74 ret
= parse_perf_probe_command(str
, pev
);
75 pr_debug("%d arguments\n", pev
->nargs
);
80 static int params_add_filter(const char *str
)
82 const char *err
= NULL
;
85 pr_debug2("Add filter: %s\n", str
);
87 params
.filter
= strfilter__new(str
, &err
);
89 ret
= err
? -EINVAL
: -ENOMEM
;
91 ret
= strfilter__or(params
.filter
, str
, &err
);
94 pr_err("Filter parse error at %td.\n", err
- str
+ 1);
95 pr_err("Source: \"%s\"\n", str
);
96 pr_err(" %*c\n", (int)(err
- str
+ 1), '^');
102 static int set_target(const char *ptr
)
108 * The first argument after options can be an absolute path
109 * to an executable / library or kernel module.
111 * TODO: Support relative path, and $PATH, $LD_LIBRARY_PATH,
114 if (!params
.target
&& ptr
&& *ptr
== '/') {
115 params
.target
= strdup(ptr
);
118 params
.target_used
= false;
121 buf
= ptr
+ (strlen(ptr
) - 3);
123 if (strcmp(buf
, ".ko"))
124 params
.uprobes
= true;
131 static int parse_probe_event_argv(int argc
, const char **argv
)
133 int i
, len
, ret
, found_target
;
136 found_target
= set_target(argv
[0]);
137 if (found_target
< 0)
140 if (found_target
&& argc
== 1)
143 /* Bind up rest arguments */
145 for (i
= 0; i
< argc
; i
++) {
146 if (i
== 0 && found_target
)
149 len
+= strlen(argv
[i
]) + 1;
151 buf
= zalloc(len
+ 1);
155 for (i
= 0; i
< argc
; i
++) {
156 if (i
== 0 && found_target
)
159 len
+= sprintf(&buf
[len
], "%s ", argv
[i
]);
161 ret
= parse_probe_event(buf
);
166 static int opt_set_target(const struct option
*opt
, const char *str
,
167 int unset __maybe_unused
)
173 if (!strcmp(opt
->long_name
, "exec"))
174 params
.uprobes
= true;
175 else if (!strcmp(opt
->long_name
, "module"))
176 params
.uprobes
= false;
180 /* Expand given path to absolute path, except for modulename */
181 if (params
.uprobes
|| strchr(str
, '/')) {
182 tmp
= nsinfo__realpath(str
, params
.nsi
);
184 pr_warning("Failed to get the absolute path of %s: %m\n", str
);
194 params
.target_used
= false;
201 static int opt_set_target_ns(const struct option
*opt __maybe_unused
,
202 const char *str
, int unset __maybe_unused
)
210 ns_pid
= (pid_t
)strtol(str
, NULL
, 10);
213 pr_warning("Failed to parse %s as a pid: %s\n", str
,
217 nsip
= nsinfo__new(ns_pid
);
218 if (nsip
&& nsip
->need_setns
)
219 params
.nsi
= nsinfo__get(nsip
);
229 /* Command option callbacks */
231 #ifdef HAVE_DWARF_SUPPORT
232 static int opt_show_lines(const struct option
*opt
,
233 const char *str
, int unset __maybe_unused
)
240 if (params
.command
== 'L') {
241 pr_warning("Warning: more than one --line options are"
242 " detected. Only the first one is valid.\n");
246 params
.command
= opt
->short_name
;
247 ret
= parse_line_range_desc(str
, ¶ms
.line_range
);
252 static int opt_show_vars(const struct option
*opt
,
253 const char *str
, int unset __maybe_unused
)
255 struct perf_probe_event
*pev
= ¶ms
.events
[params
.nevents
];
261 ret
= parse_probe_event(str
);
262 if (!ret
&& pev
->nargs
!= 0) {
263 pr_err(" Error: '--vars' doesn't accept arguments.\n");
266 params
.command
= opt
->short_name
;
271 # define opt_show_lines NULL
272 # define opt_show_vars NULL
274 static int opt_add_probe_event(const struct option
*opt
,
275 const char *str
, int unset __maybe_unused
)
278 params
.command
= opt
->short_name
;
279 return parse_probe_event(str
);
285 static int opt_set_filter_with_command(const struct option
*opt
,
286 const char *str
, int unset
)
289 params
.command
= opt
->short_name
;
292 return params_add_filter(str
);
297 static int opt_set_filter(const struct option
*opt __maybe_unused
,
298 const char *str
, int unset __maybe_unused
)
301 return params_add_filter(str
);
306 static int init_params(void)
308 return line_range__init(¶ms
.line_range
);
311 static void cleanup_params(void)
315 for (i
= 0; i
< params
.nevents
; i
++)
316 clear_perf_probe_event(params
.events
+ i
);
317 line_range__clear(¶ms
.line_range
);
319 strfilter__delete(params
.filter
);
320 nsinfo__put(params
.nsi
);
321 memset(¶ms
, 0, sizeof(params
));
324 static void pr_err_with_code(const char *msg
, int err
)
326 char sbuf
[STRERR_BUFSIZE
];
329 pr_debug(" Reason: %s (Code: %d)",
330 str_error_r(-err
, sbuf
, sizeof(sbuf
)), err
);
334 static int perf_add_probe_events(struct perf_probe_event
*pevs
, int npevs
)
338 const char *event
= NULL
, *group
= NULL
;
340 ret
= init_probe_symbol_maps(pevs
->uprobes
);
344 ret
= convert_perf_probe_events(pevs
, npevs
);
348 if (params
.command
== 'D') { /* it shows definition */
349 ret
= show_probe_trace_events(pevs
, npevs
);
353 ret
= apply_perf_probe_events(pevs
, npevs
);
357 for (i
= k
= 0; i
< npevs
; i
++)
360 pr_info("Added new event%s\n", (k
> 1) ? "s:" : ":");
361 for (i
= 0; i
< npevs
; i
++) {
362 struct perf_probe_event
*pev
= &pevs
[i
];
364 for (k
= 0; k
< pev
->ntevs
; k
++) {
365 struct probe_trace_event
*tev
= &pev
->tevs
[k
];
367 /* We use tev's name for showing new events */
368 show_perf_probe_event(tev
->group
, tev
->event
, pev
,
369 tev
->point
.module
, false);
371 /* Save the last valid name */
377 /* Note that it is possible to skip all events because of blacklist */
379 /* Show how to use the event. */
380 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
381 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group
, event
);
385 cleanup_perf_probe_events(pevs
, npevs
);
386 exit_probe_symbol_maps();
390 static int del_perf_probe_caches(struct strfilter
*filter
)
392 struct probe_cache
*cache
;
393 struct strlist
*bidlist
;
397 bidlist
= build_id_cache__list_all(false);
400 pr_debug("Failed to get buildids: %d\n", ret
);
401 return ret
?: -ENOMEM
;
404 strlist__for_each_entry(nd
, bidlist
) {
405 cache
= probe_cache__new(nd
->s
, NULL
);
408 if (probe_cache__filter_purge(cache
, filter
) < 0 ||
409 probe_cache__commit(cache
) < 0)
410 pr_warning("Failed to remove entries for %s\n", nd
->s
);
411 probe_cache__delete(cache
);
416 static int perf_del_probe_events(struct strfilter
*filter
)
418 int ret
, ret2
, ufd
= -1, kfd
= -1;
419 char *str
= strfilter__string(filter
);
420 struct strlist
*klist
= NULL
, *ulist
= NULL
;
421 struct str_node
*ent
;
426 pr_debug("Delete filter: \'%s\'\n", str
);
428 if (probe_conf
.cache
)
429 return del_perf_probe_caches(filter
);
431 /* Get current event names */
432 ret
= probe_file__open_both(&kfd
, &ufd
, PF_FL_RW
);
436 klist
= strlist__new(NULL
, NULL
);
437 ulist
= strlist__new(NULL
, NULL
);
438 if (!klist
|| !ulist
) {
443 ret
= probe_file__get_events(kfd
, filter
, klist
);
445 strlist__for_each_entry(ent
, klist
)
446 pr_info("Removed event: %s\n", ent
->s
);
448 ret
= probe_file__del_strlist(kfd
, klist
);
453 ret2
= probe_file__get_events(ufd
, filter
, ulist
);
455 strlist__for_each_entry(ent
, ulist
)
456 pr_info("Removed event: %s\n", ent
->s
);
458 ret2
= probe_file__del_strlist(ufd
, ulist
);
463 if (ret
== -ENOENT
&& ret2
== -ENOENT
)
464 pr_warning("\"%s\" does not hit any event.\n", str
);
474 strlist__delete(klist
);
475 strlist__delete(ulist
);
481 #ifdef HAVE_DWARF_SUPPORT
482 #define PROBEDEF_STR \
483 "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT [[NAME=]ARG ...]"
485 #define PROBEDEF_STR "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]"
490 __cmd_probe(int argc
, const char **argv
)
492 const char * const probe_usage
[] = {
493 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
494 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
495 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
496 "perf probe --list [GROUP:]EVENT ...",
497 #ifdef HAVE_DWARF_SUPPORT
498 "perf probe [<options>] --line 'LINEDESC'",
499 "perf probe [<options>] --vars 'PROBEPOINT'",
501 "perf probe [<options>] --funcs",
504 struct option options
[] = {
505 OPT_INCR('v', "verbose", &verbose
,
506 "be more verbose (show parsed arguments, etc)"),
507 OPT_BOOLEAN('q', "quiet", ¶ms
.quiet
,
508 "be quiet (do not show any messages)"),
509 OPT_CALLBACK_DEFAULT('l', "list", NULL
, "[GROUP:]EVENT",
510 "list up probe events",
511 opt_set_filter_with_command
, DEFAULT_LIST_FILTER
),
512 OPT_CALLBACK('d', "del", NULL
, "[GROUP:]EVENT", "delete a probe event.",
513 opt_set_filter_with_command
),
514 OPT_CALLBACK('a', "add", NULL
, PROBEDEF_STR
,
515 "probe point definition, where\n"
516 "\t\tGROUP:\tGroup name (optional)\n"
517 "\t\tEVENT:\tEvent name\n"
518 "\t\tFUNC:\tFunction name\n"
519 "\t\tOFF:\tOffset from function entry (in byte)\n"
520 "\t\t%return:\tPut the probe at function return\n"
521 #ifdef HAVE_DWARF_SUPPORT
522 "\t\tSRC:\tSource code path\n"
523 "\t\tRL:\tRelative line number from function entry.\n"
524 "\t\tAL:\tAbsolute line number in file.\n"
525 "\t\tPT:\tLazy expression of line code.\n"
526 "\t\tARG:\tProbe argument (local variable name or\n"
527 "\t\t\tkprobe-tracer argument format.)\n",
529 "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
531 opt_add_probe_event
),
532 OPT_CALLBACK('D', "definition", NULL
, PROBEDEF_STR
,
533 "Show trace event definition of given traceevent for k/uprobe_events.",
534 opt_add_probe_event
),
535 OPT_BOOLEAN('f', "force", &probe_conf
.force_add
, "forcibly add events"
536 " with existing name"),
537 OPT_CALLBACK('L', "line", NULL
,
538 "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
539 "Show source code lines.", opt_show_lines
),
540 OPT_CALLBACK('V', "vars", NULL
,
541 "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT",
542 "Show accessible variables on PROBEDEF", opt_show_vars
),
543 OPT_BOOLEAN('\0', "externs", &probe_conf
.show_ext_vars
,
544 "Show external variables too (with --vars only)"),
545 OPT_BOOLEAN('\0', "range", &probe_conf
.show_location_range
,
546 "Show variables location range in scope (with --vars only)"),
547 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
548 "file", "vmlinux pathname"),
549 OPT_STRING('s', "source", &symbol_conf
.source_prefix
,
550 "directory", "path to kernel source"),
551 OPT_BOOLEAN('\0', "no-inlines", &probe_conf
.no_inlines
,
552 "Don't search inlined functions"),
553 OPT__DRY_RUN(&probe_event_dry_run
),
554 OPT_INTEGER('\0', "max-probes", &probe_conf
.max_probes
,
555 "Set how many probe points can be found for a probe."),
556 OPT_CALLBACK_DEFAULT('F', "funcs", NULL
, "[FILTER]",
557 "Show potential probe-able functions.",
558 opt_set_filter_with_command
, DEFAULT_FUNC_FILTER
),
559 OPT_CALLBACK('\0', "filter", NULL
,
560 "[!]FILTER", "Set a filter (with --vars/funcs only)\n"
561 "\t\t\t(default: \"" DEFAULT_VAR_FILTER
"\" for --vars,\n"
562 "\t\t\t \"" DEFAULT_FUNC_FILTER
"\" for --funcs)",
564 OPT_CALLBACK('x', "exec", NULL
, "executable|path",
565 "target executable name or path", opt_set_target
),
566 OPT_CALLBACK('m', "module", NULL
, "modname|path",
567 "target module name (for online) or path (for offline)",
569 OPT_BOOLEAN(0, "demangle", &symbol_conf
.demangle
,
570 "Enable symbol demangling"),
571 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf
.demangle_kernel
,
572 "Enable kernel symbol demangling"),
573 OPT_BOOLEAN(0, "cache", &probe_conf
.cache
, "Manipulate probe cache"),
574 OPT_STRING(0, "symfs", &symbol_conf
.symfs
, "directory",
575 "Look for files with symbols relative to this directory"),
576 OPT_CALLBACK(0, "target-ns", NULL
, "pid",
577 "target pid for namespace contexts", opt_set_target_ns
),
582 set_option_flag(options
, 'a', "add", PARSE_OPT_EXCLUSIVE
);
583 set_option_flag(options
, 'd', "del", PARSE_OPT_EXCLUSIVE
);
584 set_option_flag(options
, 'D', "definition", PARSE_OPT_EXCLUSIVE
);
585 set_option_flag(options
, 'l', "list", PARSE_OPT_EXCLUSIVE
);
586 #ifdef HAVE_DWARF_SUPPORT
587 set_option_flag(options
, 'L', "line", PARSE_OPT_EXCLUSIVE
);
588 set_option_flag(options
, 'V', "vars", PARSE_OPT_EXCLUSIVE
);
590 # define set_nobuild(s, l, c) set_option_nobuild(options, s, l, "NO_DWARF=1", c)
591 set_nobuild('L', "line", false);
592 set_nobuild('V', "vars", false);
593 set_nobuild('\0', "externs", false);
594 set_nobuild('\0', "range", false);
595 set_nobuild('k', "vmlinux", true);
596 set_nobuild('s', "source", true);
597 set_nobuild('\0', "no-inlines", true);
600 set_option_flag(options
, 'F', "funcs", PARSE_OPT_EXCLUSIVE
);
602 argc
= parse_options(argc
, argv
, options
, probe_usage
,
603 PARSE_OPT_STOP_AT_NON_OPTION
);
605 if (strcmp(argv
[0], "-") == 0) {
606 usage_with_options_msg(probe_usage
, options
,
607 "'-' is not supported.\n");
609 if (params
.command
&& params
.command
!= 'a') {
610 usage_with_options_msg(probe_usage
, options
,
611 "another command except --add is set.\n");
613 ret
= parse_probe_event_argv(argc
, argv
);
615 pr_err_with_code(" Error: Command Parse Error.", ret
);
618 params
.command
= 'a';
623 pr_err(" Error: -v and -q are exclusive.\n");
629 if (probe_conf
.max_probes
== 0)
630 probe_conf
.max_probes
= MAX_PROBES
;
633 * Only consider the user's kernel image path if given.
635 symbol_conf
.try_vmlinux_path
= (symbol_conf
.vmlinux_name
== NULL
);
638 * Except for --list, --del and --add, other command doesn't depend
639 * nor change running kernel. So if user gives offline vmlinux,
640 * ignore its buildid.
642 if (!strchr("lda", params
.command
) && symbol_conf
.vmlinux_name
)
643 symbol_conf
.ignore_vmlinux_buildid
= true;
645 switch (params
.command
) {
647 if (params
.uprobes
) {
648 pr_err(" Error: Don't use --list with --exec.\n");
649 parse_options_usage(probe_usage
, options
, "l", true);
650 parse_options_usage(NULL
, options
, "x", true);
653 ret
= show_perf_probe_events(params
.filter
);
655 pr_err_with_code(" Error: Failed to show event list.", ret
);
658 ret
= show_available_funcs(params
.target
, params
.nsi
,
659 params
.filter
, params
.uprobes
);
661 pr_err_with_code(" Error: Failed to show functions.", ret
);
663 #ifdef HAVE_DWARF_SUPPORT
665 ret
= show_line_range(¶ms
.line_range
, params
.target
,
666 params
.nsi
, params
.uprobes
);
668 pr_err_with_code(" Error: Failed to show lines.", ret
);
672 params
.filter
= strfilter__new(DEFAULT_VAR_FILTER
,
675 ret
= show_available_vars(params
.events
, params
.nevents
,
678 pr_err_with_code(" Error: Failed to show vars.", ret
);
682 ret
= perf_del_probe_events(params
.filter
);
684 pr_err_with_code(" Error: Failed to delete events.", ret
);
691 /* Ensure the last given target is used */
692 if (params
.target
&& !params
.target_used
) {
693 pr_err(" Error: -x/-m must follow the probe definitions.\n");
694 parse_options_usage(probe_usage
, options
, "m", true);
695 parse_options_usage(NULL
, options
, "x", true);
699 ret
= perf_add_probe_events(params
.events
, params
.nevents
);
703 * When perf_add_probe_events() fails it calls
704 * cleanup_perf_probe_events(pevs, npevs), i.e.
705 * cleanup_perf_probe_events(params.events, params.nevents), which
706 * will call clear_perf_probe_event(), so set nevents to zero
707 * to avoid cleanup_params() to call clear_perf_probe_event() again
711 pr_err_with_code(" Error: Failed to add events.", ret
);
716 usage_with_options(probe_usage
, options
);
721 int cmd_probe(int argc
, const char **argv
)
727 ret
= __cmd_probe(argc
, argv
);
731 return ret
< 0 ? ret
: 0;