xref: /linux/tools/perf/builtin-probe.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * builtin-probe.c
4  *
5  * Builtin probe command: Set up probe events by C expression
6  *
7  * Written by Masami Hiramatsu <mhiramat@redhat.com>
8  */
9 #include <sys/utsname.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <errno.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include "builtin.h"
20 #include "namespaces.h"
21 #include "util/build-id.h"
22 #include "util/strlist.h"
23 #include "util/strfilter.h"
24 #include "util/symbol.h"
25 #include "util/symbol_conf.h"
26 #include "util/debug.h"
27 #include <subcmd/parse-options.h>
28 #include "util/probe-finder.h"
29 #include "util/probe-event.h"
30 #include "util/probe-file.h"
31 #include <linux/string.h>
32 #include <linux/zalloc.h>
33 
34 #define DEFAULT_VAR_FILTER "!__k???tab_* & !__crc_*"
35 #define DEFAULT_FUNC_FILTER "!_* & !*@plt"
36 #define DEFAULT_LIST_FILTER "*"
37 
38 /* Session management structure */
39 static struct {
40 	int command;	/* Command short_name */
41 	bool list_events;
42 	bool uprobes;
43 	bool target_used;
44 	int nevents;
45 	struct perf_probe_event events[MAX_PROBES];
46 	struct line_range line_range;
47 	char *target;
48 	struct strfilter *filter;
49 	struct nsinfo *nsi;
50 } *params;
51 
52 /* Parse an event definition. Note that any error must die. */
53 static int parse_probe_event(const char *str)
54 {
55 	struct perf_probe_event *pev = &params->events[params->nevents];
56 	int ret;
57 
58 	pr_debug("probe-definition(%d): %s\n", params->nevents, str);
59 	if (++params->nevents == MAX_PROBES) {
60 		pr_err("Too many probes (> %d) were specified.", MAX_PROBES);
61 		return -1;
62 	}
63 
64 	pev->uprobes = params->uprobes;
65 	if (params->target) {
66 		pev->target = strdup(params->target);
67 		if (!pev->target)
68 			return -ENOMEM;
69 		params->target_used = true;
70 	}
71 
72 	pev->nsi = nsinfo__get(params->nsi);
73 
74 	/* Parse a perf-probe command into event */
75 	ret = parse_perf_probe_command(str, pev);
76 	pr_debug("%d arguments\n", pev->nargs);
77 
78 	return ret;
79 }
80 
81 static int params_add_filter(const char *str)
82 {
83 	const char *err = NULL;
84 	int ret = 0;
85 
86 	pr_debug2("Add filter: %s\n", str);
87 	if (!params->filter) {
88 		params->filter = strfilter__new(str, &err);
89 		if (!params->filter)
90 			ret = err ? -EINVAL : -ENOMEM;
91 	} else
92 		ret = strfilter__or(params->filter, str, &err);
93 
94 	if (ret == -EINVAL) {
95 		pr_err("Filter parse error at %td.\n", err - str + 1);
96 		pr_err("Source: \"%s\"\n", str);
97 		pr_err("         %*c\n", (int)(err - str + 1), '^');
98 	}
99 
100 	return ret;
101 }
102 
103 static int set_target(const char *ptr)
104 {
105 	int found = 0;
106 	const char *buf;
107 
108 	/*
109 	 * The first argument after options can be an absolute path
110 	 * to an executable / library or kernel module.
111 	 *
112 	 * TODO: Support relative path, and $PATH, $LD_LIBRARY_PATH,
113 	 * short module name.
114 	 */
115 	if (!params->target && ptr && *ptr == '/') {
116 		params->target = strdup(ptr);
117 		if (!params->target)
118 			return -ENOMEM;
119 		params->target_used = false;
120 
121 		found = 1;
122 		buf = ptr + (strlen(ptr) - 3);
123 
124 		if (strcmp(buf, ".ko"))
125 			params->uprobes = true;
126 
127 	}
128 
129 	return found;
130 }
131 
132 static int parse_probe_event_argv(int argc, const char **argv)
133 {
134 	int i, len, ret, found_target;
135 	char *buf;
136 
137 	found_target = set_target(argv[0]);
138 	if (found_target < 0)
139 		return found_target;
140 
141 	if (found_target && argc == 1)
142 		return 0;
143 
144 	/* Bind up rest arguments */
145 	len = 0;
146 	for (i = 0; i < argc; i++) {
147 		if (i == 0 && found_target)
148 			continue;
149 
150 		len += strlen(argv[i]) + 1;
151 	}
152 	buf = zalloc(len + 1);
153 	if (buf == NULL)
154 		return -ENOMEM;
155 	len = 0;
156 	for (i = 0; i < argc; i++) {
157 		if (i == 0 && found_target)
158 			continue;
159 
160 		len += sprintf(&buf[len], "%s ", argv[i]);
161 	}
162 	ret = parse_probe_event(buf);
163 	free(buf);
164 	return ret;
165 }
166 
167 static int opt_set_target(const struct option *opt, const char *str,
168 			int unset __maybe_unused)
169 {
170 	int ret = -ENOENT;
171 	char *tmp;
172 
173 	if  (str) {
174 		if (!strcmp(opt->long_name, "exec"))
175 			params->uprobes = true;
176 		else if (!strcmp(opt->long_name, "module"))
177 			params->uprobes = false;
178 		else
179 			return ret;
180 
181 		/* Expand given path to absolute path, except for modulename */
182 		if (params->uprobes || strchr(str, '/')) {
183 			tmp = nsinfo__realpath(str, params->nsi);
184 			if (!tmp) {
185 				pr_warning("Failed to get the absolute path of %s: %m\n", str);
186 				return ret;
187 			}
188 		} else {
189 			tmp = strdup(str);
190 			if (!tmp)
191 				return -ENOMEM;
192 		}
193 		free(params->target);
194 		params->target = tmp;
195 		params->target_used = false;
196 		ret = 0;
197 	}
198 
199 	return ret;
200 }
201 
202 static int opt_set_target_ns(const struct option *opt __maybe_unused,
203 			     const char *str, int unset __maybe_unused)
204 {
205 	int ret = -ENOENT;
206 	pid_t ns_pid;
207 	struct nsinfo *nsip;
208 
209 	if (str) {
210 		errno = 0;
211 		ns_pid = (pid_t)strtol(str, NULL, 10);
212 		if (errno != 0) {
213 			ret = -errno;
214 			pr_warning("Failed to parse %s as a pid: %m\n", str);
215 			return ret;
216 		}
217 		nsip = nsinfo__new(ns_pid);
218 		if (nsip && nsinfo__need_setns(nsip))
219 			params->nsi = nsinfo__get(nsip);
220 		nsinfo__put(nsip);
221 
222 		ret = 0;
223 	}
224 
225 	return ret;
226 }
227 
228 
229 /* Command option callbacks */
230 
231 #ifdef HAVE_LIBDW_SUPPORT
232 static int opt_show_lines(const struct option *opt,
233 			  const char *str, int unset __maybe_unused)
234 {
235 	int ret = 0;
236 
237 	if (!str)
238 		return 0;
239 
240 	if (params->command == 'L') {
241 		pr_warning("Warning: more than one --line options are"
242 			   " detected. Only the first one is valid.\n");
243 		return 0;
244 	}
245 
246 	params->command = opt->short_name;
247 	ret = parse_line_range_desc(str, &params->line_range);
248 
249 	return ret;
250 }
251 
252 static int opt_show_vars(const struct option *opt,
253 			 const char *str, int unset __maybe_unused)
254 {
255 	struct perf_probe_event *pev = &params->events[params->nevents];
256 	int ret;
257 
258 	if (!str)
259 		return 0;
260 
261 	ret = parse_probe_event(str);
262 	if (!ret && pev->nargs != 0) {
263 		pr_err("  Error: '--vars' doesn't accept arguments.\n");
264 		return -EINVAL;
265 	}
266 	params->command = opt->short_name;
267 
268 	return ret;
269 }
270 #else
271 # define opt_show_lines NULL
272 # define opt_show_vars NULL
273 #endif
274 static int opt_add_probe_event(const struct option *opt,
275 			      const char *str, int unset __maybe_unused)
276 {
277 	if (str) {
278 		params->command = opt->short_name;
279 		return parse_probe_event(str);
280 	}
281 
282 	return 0;
283 }
284 
285 static int opt_set_filter_with_command(const struct option *opt,
286 				       const char *str, int unset)
287 {
288 	if (!unset)
289 		params->command = opt->short_name;
290 
291 	if (str)
292 		return params_add_filter(str);
293 
294 	return 0;
295 }
296 
297 static int opt_set_filter(const struct option *opt __maybe_unused,
298 			  const char *str, int unset __maybe_unused)
299 {
300 	if (str)
301 		return params_add_filter(str);
302 
303 	return 0;
304 }
305 
306 static int init_params(void)
307 {
308 	int ret;
309 
310 	params = calloc(1, sizeof(*params));
311 	if (!params)
312 		return -ENOMEM;
313 
314 	ret = line_range__init(&params->line_range);
315 	if (ret)
316 		zfree(&params);
317 	return ret;
318 }
319 
320 static void cleanup_params(void)
321 {
322 	int i;
323 
324 	for (i = 0; i < params->nevents; i++)
325 		clear_perf_probe_event(params->events + i);
326 	line_range__clear(&params->line_range);
327 	zfree(&params->target);
328 	strfilter__delete(params->filter);
329 	nsinfo__put(params->nsi);
330 	zfree(&params);
331 }
332 
333 static void pr_err_with_code(const char *msg, int err)
334 {
335 	char sbuf[STRERR_BUFSIZE];
336 
337 	pr_err("%s", msg);
338 	pr_debug(" Reason: %s (Code: %d)",
339 		 str_error_r(-err, sbuf, sizeof(sbuf)), err);
340 	pr_err("\n");
341 }
342 
343 static int perf_add_probe_events(struct perf_probe_event *pevs, int npevs)
344 {
345 	int ret;
346 	int i, k;
347 	const char *event = NULL, *group = NULL;
348 
349 	ret = init_probe_symbol_maps(pevs->uprobes);
350 	if (ret < 0)
351 		return ret;
352 
353 	ret = convert_perf_probe_events(pevs, npevs);
354 	if (ret < 0)
355 		goto out_cleanup;
356 
357 	if (params->command == 'D') {	/* it shows definition */
358 		if (probe_conf.bootconfig)
359 			ret = show_bootconfig_events(pevs, npevs);
360 		else
361 			ret = show_probe_trace_events(pevs, npevs);
362 		goto out_cleanup;
363 	}
364 
365 	ret = apply_perf_probe_events(pevs, npevs);
366 	if (ret < 0)
367 		goto out_cleanup;
368 
369 	for (i = k = 0; i < npevs; i++)
370 		k += pevs[i].ntevs;
371 
372 	pr_info("Added new event%s\n", (k > 1) ? "s:" : ":");
373 	for (i = 0; i < npevs; i++) {
374 		struct perf_probe_event *pev = &pevs[i];
375 
376 		for (k = 0; k < pev->ntevs; k++) {
377 			struct probe_trace_event *tev = &pev->tevs[k];
378 			/* Skipped events have no event name */
379 			if (!tev->event)
380 				continue;
381 
382 			/* We use tev's name for showing new events */
383 			show_perf_probe_event(tev->group, tev->event, pev,
384 					      tev->point.module, false);
385 
386 			/* Save the last valid name */
387 			event = tev->event;
388 			group = tev->group;
389 		}
390 	}
391 
392 	/* Note that it is possible to skip all events because of blacklist */
393 	if (event) {
394 #ifndef HAVE_LIBTRACEEVENT
395 		pr_info("\nperf is not linked with libtraceevent, to use the new probe you can use tracefs:\n\n");
396 		pr_info("\tcd /sys/kernel/tracing/\n");
397 		pr_info("\techo 1 > events/%s/%s/enable\n", group, event);
398 		pr_info("\techo 1 > tracing_on\n");
399 		pr_info("\tcat trace_pipe\n");
400 		pr_info("\tBefore removing the probe, echo 0 > events/%s/%s/enable\n", group, event);
401 #else
402 		/* Show how to use the event. */
403 		pr_info("\nYou can now use it in all perf tools, such as:\n\n");
404 		pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group, event);
405 #endif
406 	}
407 
408 out_cleanup:
409 	cleanup_perf_probe_events(pevs, npevs);
410 	exit_probe_symbol_maps();
411 	return ret;
412 }
413 
414 static int del_perf_probe_caches(struct strfilter *filter)
415 {
416 	struct probe_cache *cache;
417 	struct strlist *bidlist;
418 	struct str_node *nd;
419 	int ret;
420 
421 	bidlist = build_id_cache__list_all(false);
422 	if (!bidlist) {
423 		ret = -errno;
424 		pr_debug("Failed to get buildids: %d\n", ret);
425 		return ret ?: -ENOMEM;
426 	}
427 
428 	strlist__for_each_entry(nd, bidlist) {
429 		cache = probe_cache__new(nd->s, NULL);
430 		if (!cache)
431 			continue;
432 		if (probe_cache__filter_purge(cache, filter) < 0 ||
433 		    probe_cache__commit(cache) < 0)
434 			pr_warning("Failed to remove entries for %s\n", nd->s);
435 		probe_cache__delete(cache);
436 	}
437 	return 0;
438 }
439 
440 static int perf_del_probe_events(struct strfilter *filter)
441 {
442 	int ret, ret2, ufd = -1, kfd = -1;
443 	char *str = strfilter__string(filter);
444 	struct strlist *klist = NULL, *ulist = NULL;
445 	struct str_node *ent;
446 
447 	if (!str)
448 		return -EINVAL;
449 
450 	pr_debug("Delete filter: \'%s\'\n", str);
451 
452 	if (probe_conf.cache)
453 		return del_perf_probe_caches(filter);
454 
455 	/* Get current event names */
456 	ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
457 	if (ret < 0)
458 		goto out;
459 
460 	klist = strlist__new(NULL, NULL);
461 	ulist = strlist__new(NULL, NULL);
462 	if (!klist || !ulist) {
463 		ret = -ENOMEM;
464 		goto out;
465 	}
466 
467 	ret = probe_file__get_events(kfd, filter, klist);
468 	if (ret == 0) {
469 		strlist__for_each_entry(ent, klist)
470 			pr_info("Removed event: %s\n", ent->s);
471 
472 		ret = probe_file__del_strlist(kfd, klist);
473 		if (ret < 0)
474 			goto error;
475 	} else if (ret == -ENOMEM)
476 		goto error;
477 
478 	ret2 = probe_file__get_events(ufd, filter, ulist);
479 	if (ret2 == 0) {
480 		strlist__for_each_entry(ent, ulist)
481 			pr_info("Removed event: %s\n", ent->s);
482 
483 		ret2 = probe_file__del_strlist(ufd, ulist);
484 		if (ret2 < 0)
485 			goto error;
486 	} else if (ret2 == -ENOMEM)
487 		goto error;
488 
489 	if (ret == -ENOENT && ret2 == -ENOENT)
490 		pr_warning("\"%s\" does not hit any event.\n", str);
491 	else
492 		ret = 0;
493 
494 error:
495 	if (kfd >= 0)
496 		close(kfd);
497 	if (ufd >= 0)
498 		close(ufd);
499 out:
500 	strlist__delete(klist);
501 	strlist__delete(ulist);
502 	free(str);
503 
504 	return ret;
505 }
506 
507 #ifdef HAVE_LIBDW_SUPPORT
508 #define PROBEDEF_STR	\
509 	"[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT [[NAME=]ARG ...]"
510 #else
511 #define PROBEDEF_STR	"[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]"
512 #endif
513 
514 
515 static int
516 __cmd_probe(int argc, const char **argv)
517 {
518 	const char * const probe_usage[] = {
519 		"perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
520 		"perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
521 		"perf probe [<options>] --del '[GROUP:]EVENT' ...",
522 		"perf probe --list [GROUP:]EVENT ...",
523 #ifdef HAVE_LIBDW_SUPPORT
524 		"perf probe [<options>] --line 'LINEDESC'",
525 		"perf probe [<options>] --vars 'PROBEPOINT'",
526 #endif
527 		"perf probe [<options>] --funcs",
528 		NULL
529 	};
530 	struct option options[] = {
531 	OPT_INCR('v', "verbose", &verbose,
532 		    "be more verbose (show parsed arguments, etc)"),
533 	OPT_BOOLEAN('q', "quiet", &quiet,
534 		    "be quiet (do not show any warnings or messages)"),
535 	OPT_CALLBACK_DEFAULT('l', "list", NULL, "[GROUP:]EVENT",
536 			     "list up probe events",
537 			     opt_set_filter_with_command, DEFAULT_LIST_FILTER),
538 	OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
539 		     opt_set_filter_with_command),
540 	OPT_CALLBACK('a', "add", NULL, PROBEDEF_STR,
541 		"probe point definition, where\n"
542 		"\t\tGROUP:\tGroup name (optional)\n"
543 		"\t\tEVENT:\tEvent name\n"
544 		"\t\tFUNC:\tFunction name\n"
545 		"\t\tOFF:\tOffset from function entry (in byte)\n"
546 		"\t\t%return:\tPut the probe at function return\n"
547 #ifdef HAVE_LIBDW_SUPPORT
548 		"\t\tSRC:\tSource code path\n"
549 		"\t\tRL:\tRelative line number from function entry.\n"
550 		"\t\tAL:\tAbsolute line number in file.\n"
551 		"\t\tPT:\tLazy expression of line code.\n"
552 		"\t\tARG:\tProbe argument (local variable name or\n"
553 		"\t\t\tkprobe-tracer argument format.)\n",
554 #else
555 		"\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
556 #endif
557 		opt_add_probe_event),
558 	OPT_CALLBACK('D', "definition", NULL, PROBEDEF_STR,
559 		"Show trace event definition of given traceevent for k/uprobe_events.",
560 		opt_add_probe_event),
561 	OPT_BOOLEAN('f', "force", &probe_conf.force_add, "forcibly add events"
562 		    " with existing name"),
563 	OPT_CALLBACK('L', "line", NULL,
564 		     "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
565 		     "Show source code lines.", opt_show_lines),
566 	OPT_CALLBACK('V', "vars", NULL,
567 		     "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT",
568 		     "Show accessible variables on PROBEDEF", opt_show_vars),
569 	OPT_BOOLEAN('\0', "externs", &probe_conf.show_ext_vars,
570 		    "Show external variables too (with --vars only)"),
571 	OPT_BOOLEAN('\0', "range", &probe_conf.show_location_range,
572 		"Show variables location range in scope (with --vars only)"),
573 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
574 		   "file", "vmlinux pathname"),
575 	OPT_STRING('s', "source", &symbol_conf.source_prefix,
576 		   "directory", "path to kernel source"),
577 	OPT_BOOLEAN('\0', "no-inlines", &probe_conf.no_inlines,
578 		"Don't search inlined functions"),
579 	OPT__DRY_RUN(&probe_event_dry_run),
580 	OPT_INTEGER('\0', "max-probes", &probe_conf.max_probes,
581 		 "Set how many probe points can be found for a probe."),
582 	OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]",
583 			     "Show potential probe-able functions.",
584 			     opt_set_filter_with_command, DEFAULT_FUNC_FILTER),
585 	OPT_CALLBACK('\0', "filter", NULL,
586 		     "[!]FILTER", "Set a filter (with --vars/funcs only)\n"
587 		     "\t\t\t(default: \"" DEFAULT_VAR_FILTER "\" for --vars,\n"
588 		     "\t\t\t \"" DEFAULT_FUNC_FILTER "\" for --funcs)",
589 		     opt_set_filter),
590 	OPT_CALLBACK('x', "exec", NULL, "executable|path",
591 			"target executable name or path", opt_set_target),
592 	OPT_CALLBACK('m', "module", NULL, "modname|path",
593 		"target module name (for online) or path (for offline)",
594 		opt_set_target),
595 	OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
596 		    "Enable symbol demangling"),
597 	OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
598 		    "Enable kernel symbol demangling"),
599 	OPT_BOOLEAN(0, "cache", &probe_conf.cache, "Manipulate probe cache"),
600 	OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
601 		   "Look for files with symbols relative to this directory"),
602 	OPT_CALLBACK(0, "target-ns", NULL, "pid",
603 		     "target pid for namespace contexts", opt_set_target_ns),
604 	OPT_BOOLEAN(0, "bootconfig", &probe_conf.bootconfig,
605 		    "Output probe definition with bootconfig format"),
606 	OPT_END()
607 	};
608 	int ret;
609 
610 	set_option_flag(options, 'a', "add", PARSE_OPT_EXCLUSIVE);
611 	set_option_flag(options, 'd', "del", PARSE_OPT_EXCLUSIVE);
612 	set_option_flag(options, 'D', "definition", PARSE_OPT_EXCLUSIVE);
613 	set_option_flag(options, 'l', "list", PARSE_OPT_EXCLUSIVE);
614 #ifdef HAVE_LIBDW_SUPPORT
615 	set_option_flag(options, 'L', "line", PARSE_OPT_EXCLUSIVE);
616 	set_option_flag(options, 'V', "vars", PARSE_OPT_EXCLUSIVE);
617 #else
618 # define set_nobuild(s, l, c) set_option_nobuild(options, s, l, "NO_LIBDW=1", c)
619 	set_nobuild('L', "line", false);
620 	set_nobuild('V', "vars", false);
621 	set_nobuild('\0', "externs", false);
622 	set_nobuild('\0', "range", false);
623 	set_nobuild('k', "vmlinux", true);
624 	set_nobuild('s', "source", true);
625 	set_nobuild('\0', "no-inlines", true);
626 # undef set_nobuild
627 #endif
628 	set_option_flag(options, 'F', "funcs", PARSE_OPT_EXCLUSIVE);
629 
630 	argc = parse_options(argc, argv, options, probe_usage,
631 			     PARSE_OPT_STOP_AT_NON_OPTION);
632 
633 	if (quiet) {
634 		if (verbose != 0) {
635 			pr_err("  Error: -v and -q are exclusive.\n");
636 			return -EINVAL;
637 		}
638 		verbose = -1;
639 	}
640 
641 	if (argc > 0) {
642 		if (strcmp(argv[0], "-") == 0) {
643 			usage_with_options_msg(probe_usage, options,
644 				"'-' is not supported.\n");
645 		}
646 		if (params->command && params->command != 'a') {
647 			usage_with_options_msg(probe_usage, options,
648 				"another command except --add is set.\n");
649 		}
650 		ret = parse_probe_event_argv(argc, argv);
651 		if (ret < 0) {
652 			pr_err_with_code("  Error: Command Parse Error.", ret);
653 			return ret;
654 		}
655 		params->command = 'a';
656 	}
657 
658 	ret = symbol__validate_sym_arguments();
659 	if (ret)
660 		return ret;
661 
662 	if (probe_conf.max_probes == 0)
663 		probe_conf.max_probes = MAX_PROBES;
664 
665 	/*
666 	 * Only consider the user's kernel image path if given.
667 	 */
668 	symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
669 
670 	/*
671 	 * Except for --list, --del and --add, other command doesn't depend
672 	 * nor change running kernel. So if user gives offline vmlinux,
673 	 * ignore its buildid.
674 	 */
675 	if (!strchr("lda", params->command) && symbol_conf.vmlinux_name)
676 		symbol_conf.ignore_vmlinux_buildid = true;
677 
678 	switch (params->command) {
679 	case 'l':
680 		if (params->uprobes) {
681 			pr_err("  Error: Don't use --list with --exec.\n");
682 			parse_options_usage(probe_usage, options, "l", true);
683 			parse_options_usage(NULL, options, "x", true);
684 			return -EINVAL;
685 		}
686 		ret = show_perf_probe_events(params->filter);
687 		if (ret < 0)
688 			pr_err_with_code("  Error: Failed to show event list.", ret);
689 		return ret;
690 	case 'F':
691 		ret = show_available_funcs(params->target, params->nsi,
692 					   params->filter, params->uprobes);
693 		if (ret < 0)
694 			pr_err_with_code("  Error: Failed to show functions.", ret);
695 		return ret;
696 #ifdef HAVE_LIBDW_SUPPORT
697 	case 'L':
698 		ret = show_line_range(&params->line_range, params->target,
699 				      params->nsi, params->uprobes);
700 		if (ret < 0)
701 			pr_err_with_code("  Error: Failed to show lines.", ret);
702 		return ret;
703 	case 'V':
704 		if (!params->filter)
705 			params->filter = strfilter__new(DEFAULT_VAR_FILTER,
706 						       NULL);
707 
708 		ret = show_available_vars(params->events, params->nevents,
709 					  params->filter);
710 		if (ret < 0)
711 			pr_err_with_code("  Error: Failed to show vars.", ret);
712 		return ret;
713 #endif
714 	case 'd':
715 		ret = perf_del_probe_events(params->filter);
716 		if (ret < 0) {
717 			pr_err_with_code("  Error: Failed to delete events.", ret);
718 			return ret;
719 		}
720 		break;
721 	case 'D':
722 		if (probe_conf.bootconfig && params->uprobes) {
723 			pr_err("  Error: --bootconfig doesn't support uprobes.\n");
724 			return -EINVAL;
725 		}
726 		fallthrough;
727 	case 'a':
728 
729 		/* Ensure the last given target is used */
730 		if (params->target && !params->target_used) {
731 			pr_err("  Error: -x/-m must follow the probe definitions.\n");
732 			parse_options_usage(probe_usage, options, "m", true);
733 			parse_options_usage(NULL, options, "x", true);
734 			return -EINVAL;
735 		}
736 
737 		ret = perf_add_probe_events(params->events, params->nevents);
738 		if (ret < 0) {
739 
740 			/*
741 			 * When perf_add_probe_events() fails it calls
742 			 * cleanup_perf_probe_events(pevs, npevs), i.e.
743 			 * cleanup_perf_probe_events(params->events, params->nevents), which
744 			 * will call clear_perf_probe_event(), so set nevents to zero
745 			 * to avoid cleanup_params() to call clear_perf_probe_event() again
746 			 * on the same pevs.
747 			 */
748 			params->nevents = 0;
749 			pr_err_with_code("  Error: Failed to add events.", ret);
750 			return ret;
751 		}
752 		break;
753 	default:
754 		usage_with_options(probe_usage, options);
755 	}
756 	return 0;
757 }
758 
759 int cmd_probe(int argc, const char **argv)
760 {
761 	int ret;
762 
763 	ret = init_params();
764 	if (!ret) {
765 		ret = __cmd_probe(argc, argv);
766 		cleanup_params();
767 	}
768 
769 	return ret < 0 ? ret : 0;
770 }
771