builtin-probe.c 19 KB

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