builtin-probe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. }
  378. ret2 = probe_file__get_events(ufd, filter, ulist);
  379. if (ret2 == 0) {
  380. strlist__for_each_entry(ent, ulist)
  381. pr_info("Removed event: %s\n", ent->s);
  382. ret2 = probe_file__del_strlist(ufd, ulist);
  383. if (ret2 < 0)
  384. goto error;
  385. }
  386. if (ret == -ENOENT && ret2 == -ENOENT)
  387. pr_warning("\"%s\" does not hit any event.\n", str);
  388. else
  389. ret = 0;
  390. error:
  391. if (kfd >= 0)
  392. close(kfd);
  393. if (ufd >= 0)
  394. close(ufd);
  395. out:
  396. strlist__delete(klist);
  397. strlist__delete(ulist);
  398. free(str);
  399. return ret;
  400. }
  401. #ifdef HAVE_DWARF_SUPPORT
  402. #define PROBEDEF_STR \
  403. "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT [[NAME=]ARG ...]"
  404. #else
  405. #define PROBEDEF_STR "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]"
  406. #endif
  407. static int
  408. __cmd_probe(int argc, const char **argv)
  409. {
  410. const char * const probe_usage[] = {
  411. "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
  412. "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
  413. "perf probe [<options>] --del '[GROUP:]EVENT' ...",
  414. "perf probe --list [GROUP:]EVENT ...",
  415. #ifdef HAVE_DWARF_SUPPORT
  416. "perf probe [<options>] --line 'LINEDESC'",
  417. "perf probe [<options>] --vars 'PROBEPOINT'",
  418. #endif
  419. "perf probe [<options>] --funcs",
  420. NULL
  421. };
  422. struct option options[] = {
  423. OPT_INCR('v', "verbose", &verbose,
  424. "be more verbose (show parsed arguments, etc)"),
  425. OPT_BOOLEAN('q', "quiet", &params.quiet,
  426. "be quiet (do not show any messages)"),
  427. OPT_CALLBACK_DEFAULT('l', "list", NULL, "[GROUP:]EVENT",
  428. "list up probe events",
  429. opt_set_filter_with_command, DEFAULT_LIST_FILTER),
  430. OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
  431. opt_set_filter_with_command),
  432. OPT_CALLBACK('a', "add", NULL, PROBEDEF_STR,
  433. "probe point definition, where\n"
  434. "\t\tGROUP:\tGroup name (optional)\n"
  435. "\t\tEVENT:\tEvent name\n"
  436. "\t\tFUNC:\tFunction name\n"
  437. "\t\tOFF:\tOffset from function entry (in byte)\n"
  438. "\t\t%return:\tPut the probe at function return\n"
  439. #ifdef HAVE_DWARF_SUPPORT
  440. "\t\tSRC:\tSource code path\n"
  441. "\t\tRL:\tRelative line number from function entry.\n"
  442. "\t\tAL:\tAbsolute line number in file.\n"
  443. "\t\tPT:\tLazy expression of line code.\n"
  444. "\t\tARG:\tProbe argument (local variable name or\n"
  445. "\t\t\tkprobe-tracer argument format.)\n",
  446. #else
  447. "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
  448. #endif
  449. opt_add_probe_event),
  450. OPT_CALLBACK('D', "definition", NULL, PROBEDEF_STR,
  451. "Show trace event definition of given traceevent for k/uprobe_events.",
  452. opt_add_probe_event),
  453. OPT_BOOLEAN('f', "force", &probe_conf.force_add, "forcibly add events"
  454. " with existing name"),
  455. OPT_CALLBACK('L', "line", NULL,
  456. "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
  457. "Show source code lines.", opt_show_lines),
  458. OPT_CALLBACK('V', "vars", NULL,
  459. "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT",
  460. "Show accessible variables on PROBEDEF", opt_show_vars),
  461. OPT_BOOLEAN('\0', "externs", &probe_conf.show_ext_vars,
  462. "Show external variables too (with --vars only)"),
  463. OPT_BOOLEAN('\0', "range", &probe_conf.show_location_range,
  464. "Show variables location range in scope (with --vars only)"),
  465. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  466. "file", "vmlinux pathname"),
  467. OPT_STRING('s', "source", &symbol_conf.source_prefix,
  468. "directory", "path to kernel source"),
  469. OPT_BOOLEAN('\0', "no-inlines", &probe_conf.no_inlines,
  470. "Don't search inlined functions"),
  471. OPT__DRY_RUN(&probe_event_dry_run),
  472. OPT_INTEGER('\0', "max-probes", &probe_conf.max_probes,
  473. "Set how many probe points can be found for a probe."),
  474. OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]",
  475. "Show potential probe-able functions.",
  476. opt_set_filter_with_command, DEFAULT_FUNC_FILTER),
  477. OPT_CALLBACK('\0', "filter", NULL,
  478. "[!]FILTER", "Set a filter (with --vars/funcs only)\n"
  479. "\t\t\t(default: \"" DEFAULT_VAR_FILTER "\" for --vars,\n"
  480. "\t\t\t \"" DEFAULT_FUNC_FILTER "\" for --funcs)",
  481. opt_set_filter),
  482. OPT_CALLBACK('x', "exec", NULL, "executable|path",
  483. "target executable name or path", opt_set_target),
  484. OPT_CALLBACK('m', "module", NULL, "modname|path",
  485. "target module name (for online) or path (for offline)",
  486. opt_set_target),
  487. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  488. "Enable symbol demangling"),
  489. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  490. "Enable kernel symbol demangling"),
  491. OPT_BOOLEAN(0, "cache", &probe_conf.cache, "Manipulate probe cache"),
  492. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  493. "Look for files with symbols relative to this directory"),
  494. OPT_CALLBACK(0, "target-ns", NULL, "pid",
  495. "target pid for namespace contexts", opt_set_target_ns),
  496. OPT_END()
  497. };
  498. int ret;
  499. set_option_flag(options, 'a', "add", PARSE_OPT_EXCLUSIVE);
  500. set_option_flag(options, 'd', "del", PARSE_OPT_EXCLUSIVE);
  501. set_option_flag(options, 'D', "definition", PARSE_OPT_EXCLUSIVE);
  502. set_option_flag(options, 'l', "list", PARSE_OPT_EXCLUSIVE);
  503. #ifdef HAVE_DWARF_SUPPORT
  504. set_option_flag(options, 'L', "line", PARSE_OPT_EXCLUSIVE);
  505. set_option_flag(options, 'V', "vars", PARSE_OPT_EXCLUSIVE);
  506. #else
  507. # define set_nobuild(s, l, c) set_option_nobuild(options, s, l, "NO_DWARF=1", c)
  508. set_nobuild('L', "line", false);
  509. set_nobuild('V', "vars", false);
  510. set_nobuild('\0', "externs", false);
  511. set_nobuild('\0', "range", false);
  512. set_nobuild('k', "vmlinux", true);
  513. set_nobuild('s', "source", true);
  514. set_nobuild('\0', "no-inlines", true);
  515. # undef set_nobuild
  516. #endif
  517. set_option_flag(options, 'F', "funcs", PARSE_OPT_EXCLUSIVE);
  518. argc = parse_options(argc, argv, options, probe_usage,
  519. PARSE_OPT_STOP_AT_NON_OPTION);
  520. if (argc > 0) {
  521. if (strcmp(argv[0], "-") == 0) {
  522. usage_with_options_msg(probe_usage, options,
  523. "'-' is not supported.\n");
  524. }
  525. if (params.command && params.command != 'a') {
  526. usage_with_options_msg(probe_usage, options,
  527. "another command except --add is set.\n");
  528. }
  529. ret = parse_probe_event_argv(argc, argv);
  530. if (ret < 0) {
  531. pr_err_with_code(" Error: Command Parse Error.", ret);
  532. return ret;
  533. }
  534. params.command = 'a';
  535. }
  536. if (params.quiet) {
  537. if (verbose != 0) {
  538. pr_err(" Error: -v and -q are exclusive.\n");
  539. return -EINVAL;
  540. }
  541. verbose = -1;
  542. }
  543. if (probe_conf.max_probes == 0)
  544. probe_conf.max_probes = MAX_PROBES;
  545. /*
  546. * Only consider the user's kernel image path if given.
  547. */
  548. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  549. /*
  550. * Except for --list, --del and --add, other command doesn't depend
  551. * nor change running kernel. So if user gives offline vmlinux,
  552. * ignore its buildid.
  553. */
  554. if (!strchr("lda", params.command) && symbol_conf.vmlinux_name)
  555. symbol_conf.ignore_vmlinux_buildid = true;
  556. switch (params.command) {
  557. case 'l':
  558. if (params.uprobes) {
  559. pr_err(" Error: Don't use --list with --exec.\n");
  560. parse_options_usage(probe_usage, options, "l", true);
  561. parse_options_usage(NULL, options, "x", true);
  562. return -EINVAL;
  563. }
  564. ret = show_perf_probe_events(params.filter);
  565. if (ret < 0)
  566. pr_err_with_code(" Error: Failed to show event list.", ret);
  567. return ret;
  568. case 'F':
  569. ret = show_available_funcs(params.target, params.nsi,
  570. params.filter, params.uprobes);
  571. if (ret < 0)
  572. pr_err_with_code(" Error: Failed to show functions.", ret);
  573. return ret;
  574. #ifdef HAVE_DWARF_SUPPORT
  575. case 'L':
  576. ret = show_line_range(&params.line_range, params.target,
  577. params.nsi, params.uprobes);
  578. if (ret < 0)
  579. pr_err_with_code(" Error: Failed to show lines.", ret);
  580. return ret;
  581. case 'V':
  582. if (!params.filter)
  583. params.filter = strfilter__new(DEFAULT_VAR_FILTER,
  584. NULL);
  585. ret = show_available_vars(params.events, params.nevents,
  586. params.filter);
  587. if (ret < 0)
  588. pr_err_with_code(" Error: Failed to show vars.", ret);
  589. return ret;
  590. #endif
  591. case 'd':
  592. ret = perf_del_probe_events(params.filter);
  593. if (ret < 0) {
  594. pr_err_with_code(" Error: Failed to delete events.", ret);
  595. return ret;
  596. }
  597. break;
  598. case 'D':
  599. case 'a':
  600. /* Ensure the last given target is used */
  601. if (params.target && !params.target_used) {
  602. pr_err(" Error: -x/-m must follow the probe definitions.\n");
  603. parse_options_usage(probe_usage, options, "m", true);
  604. parse_options_usage(NULL, options, "x", true);
  605. return -EINVAL;
  606. }
  607. ret = perf_add_probe_events(params.events, params.nevents);
  608. if (ret < 0) {
  609. /*
  610. * When perf_add_probe_events() fails it calls
  611. * cleanup_perf_probe_events(pevs, npevs), i.e.
  612. * cleanup_perf_probe_events(params.events, params.nevents), which
  613. * will call clear_perf_probe_event(), so set nevents to zero
  614. * to avoid cleanup_params() to call clear_perf_probe_event() again
  615. * on the same pevs.
  616. */
  617. params.nevents = 0;
  618. pr_err_with_code(" Error: Failed to add events.", ret);
  619. return ret;
  620. }
  621. break;
  622. default:
  623. usage_with_options(probe_usage, options);
  624. }
  625. return 0;
  626. }
  627. int cmd_probe(int argc, const char **argv)
  628. {
  629. int ret;
  630. ret = init_params();
  631. if (!ret) {
  632. ret = __cmd_probe(argc, argv);
  633. cleanup_params();
  634. }
  635. return ret < 0 ? ret : 0;
  636. }