builtin-probe.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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.h"
  24. #include "util/symbol_conf.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/string.h>
  31. #include <linux/zalloc.h>
  32. #define DEFAULT_VAR_FILTER "!__k???tab_* & !__crc_*"
  33. #define DEFAULT_FUNC_FILTER "!_* & !*@plt"
  34. #define DEFAULT_LIST_FILTER "*"
  35. /* Session management structure */
  36. static struct {
  37. int command; /* Command short_name */
  38. bool list_events;
  39. bool uprobes;
  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 && nsinfo__need_setns(nsip))
  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. int ret;
  259. params = calloc(1, sizeof(*params));
  260. if (!params)
  261. return -ENOMEM;
  262. ret = line_range__init(&params->line_range);
  263. if (ret)
  264. zfree(&params);
  265. return ret;
  266. }
  267. static void cleanup_params(void)
  268. {
  269. int i;
  270. for (i = 0; i < params->nevents; i++)
  271. clear_perf_probe_event(params->events + i);
  272. line_range__clear(&params->line_range);
  273. zfree(&params->target);
  274. strfilter__delete(params->filter);
  275. nsinfo__put(params->nsi);
  276. zfree(&params);
  277. }
  278. static void pr_err_with_code(const char *msg, int err)
  279. {
  280. char sbuf[STRERR_BUFSIZE];
  281. pr_err("%s", msg);
  282. pr_debug(" Reason: %s (Code: %d)",
  283. str_error_r(-err, sbuf, sizeof(sbuf)), err);
  284. pr_err("\n");
  285. }
  286. static int perf_add_probe_events(struct perf_probe_event *pevs, int npevs)
  287. {
  288. int ret;
  289. int i, k;
  290. const char *event = NULL, *group = NULL;
  291. ret = init_probe_symbol_maps(pevs->uprobes);
  292. if (ret < 0)
  293. return ret;
  294. ret = convert_perf_probe_events(pevs, npevs);
  295. if (ret < 0)
  296. goto out_cleanup;
  297. if (params->command == 'D') { /* it shows definition */
  298. if (probe_conf.bootconfig)
  299. ret = show_bootconfig_events(pevs, npevs);
  300. else
  301. ret = show_probe_trace_events(pevs, npevs);
  302. goto out_cleanup;
  303. }
  304. ret = apply_perf_probe_events(pevs, npevs);
  305. if (ret < 0)
  306. goto out_cleanup;
  307. for (i = k = 0; i < npevs; i++)
  308. k += pevs[i].ntevs;
  309. pr_info("Added new event%s\n", (k > 1) ? "s:" : ":");
  310. for (i = 0; i < npevs; i++) {
  311. struct perf_probe_event *pev = &pevs[i];
  312. for (k = 0; k < pev->ntevs; k++) {
  313. struct probe_trace_event *tev = &pev->tevs[k];
  314. /* Skipped events have no event name */
  315. if (!tev->event)
  316. continue;
  317. /* We use tev's name for showing new events */
  318. show_perf_probe_event(tev->group, tev->event, pev,
  319. tev->point.module, false);
  320. /* Save the last valid name */
  321. event = tev->event;
  322. group = tev->group;
  323. }
  324. }
  325. /* Note that it is possible to skip all events because of blacklist */
  326. if (event) {
  327. #ifndef HAVE_LIBTRACEEVENT
  328. pr_info("\nperf is not linked with libtraceevent, to use the new probe you can use tracefs:\n\n");
  329. pr_info("\tcd /sys/kernel/tracing/\n");
  330. pr_info("\techo 1 > events/%s/%s/enable\n", group, event);
  331. pr_info("\techo 1 > tracing_on\n");
  332. pr_info("\tcat trace_pipe\n");
  333. pr_info("\tBefore removing the probe, echo 0 > events/%s/%s/enable\n", group, event);
  334. #else
  335. /* Show how to use the event. */
  336. pr_info("\nYou can now use it in all perf tools, such as:\n\n");
  337. pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group, event);
  338. #endif
  339. }
  340. out_cleanup:
  341. cleanup_perf_probe_events(pevs, npevs);
  342. exit_probe_symbol_maps();
  343. return ret;
  344. }
  345. static int del_perf_probe_caches(struct strfilter *filter)
  346. {
  347. struct probe_cache *cache;
  348. struct strlist *bidlist;
  349. struct str_node *nd;
  350. int ret;
  351. bidlist = build_id_cache__list_all(false);
  352. if (!bidlist) {
  353. ret = -errno;
  354. pr_debug("Failed to get buildids: %d\n", ret);
  355. return ret ?: -ENOMEM;
  356. }
  357. strlist__for_each_entry(nd, bidlist) {
  358. cache = probe_cache__new(nd->s, NULL);
  359. if (!cache)
  360. continue;
  361. if (probe_cache__filter_purge(cache, filter) < 0 ||
  362. probe_cache__commit(cache) < 0)
  363. pr_warning("Failed to remove entries for %s\n", nd->s);
  364. probe_cache__delete(cache);
  365. }
  366. return 0;
  367. }
  368. static int perf_del_probe_events(struct strfilter *filter)
  369. {
  370. int ret, ret2, ufd = -1, kfd = -1;
  371. char *str = strfilter__string(filter);
  372. struct strlist *klist = NULL, *ulist = NULL;
  373. struct str_node *ent;
  374. if (!str)
  375. return -EINVAL;
  376. pr_debug("Delete filter: \'%s\'\n", str);
  377. if (probe_conf.cache)
  378. return del_perf_probe_caches(filter);
  379. /* Get current event names */
  380. ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
  381. if (ret < 0)
  382. goto out;
  383. klist = strlist__new(NULL, NULL);
  384. ulist = strlist__new(NULL, NULL);
  385. if (!klist || !ulist) {
  386. ret = -ENOMEM;
  387. goto out;
  388. }
  389. ret = probe_file__get_events(kfd, filter, klist);
  390. if (ret == 0) {
  391. strlist__for_each_entry(ent, klist)
  392. pr_info("Removed event: %s\n", ent->s);
  393. ret = probe_file__del_strlist(kfd, klist);
  394. if (ret < 0)
  395. goto error;
  396. } else if (ret == -ENOMEM)
  397. goto error;
  398. ret2 = probe_file__get_events(ufd, filter, ulist);
  399. if (ret2 == 0) {
  400. strlist__for_each_entry(ent, ulist)
  401. pr_info("Removed event: %s\n", ent->s);
  402. ret2 = probe_file__del_strlist(ufd, ulist);
  403. if (ret2 < 0)
  404. goto error;
  405. } else if (ret2 == -ENOMEM)
  406. goto error;
  407. if (ret == -ENOENT && ret2 == -ENOENT)
  408. pr_warning("\"%s\" does not hit any event.\n", str);
  409. else
  410. ret = 0;
  411. error:
  412. if (kfd >= 0)
  413. close(kfd);
  414. if (ufd >= 0)
  415. close(ufd);
  416. out:
  417. strlist__delete(klist);
  418. strlist__delete(ulist);
  419. free(str);
  420. return ret;
  421. }
  422. #ifdef HAVE_DWARF_SUPPORT
  423. #define PROBEDEF_STR \
  424. "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT [[NAME=]ARG ...]"
  425. #else
  426. #define PROBEDEF_STR "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]"
  427. #endif
  428. static int
  429. __cmd_probe(int argc, const char **argv)
  430. {
  431. const char * const probe_usage[] = {
  432. "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
  433. "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
  434. "perf probe [<options>] --del '[GROUP:]EVENT' ...",
  435. "perf probe --list [GROUP:]EVENT ...",
  436. #ifdef HAVE_DWARF_SUPPORT
  437. "perf probe [<options>] --line 'LINEDESC'",
  438. "perf probe [<options>] --vars 'PROBEPOINT'",
  439. #endif
  440. "perf probe [<options>] --funcs",
  441. NULL
  442. };
  443. struct option options[] = {
  444. OPT_INCR('v', "verbose", &verbose,
  445. "be more verbose (show parsed arguments, etc)"),
  446. OPT_BOOLEAN('q', "quiet", &quiet,
  447. "be quiet (do not show any warnings or messages)"),
  448. OPT_CALLBACK_DEFAULT('l', "list", NULL, "[GROUP:]EVENT",
  449. "list up probe events",
  450. opt_set_filter_with_command, DEFAULT_LIST_FILTER),
  451. OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
  452. opt_set_filter_with_command),
  453. OPT_CALLBACK('a', "add", NULL, PROBEDEF_STR,
  454. "probe point definition, where\n"
  455. "\t\tGROUP:\tGroup name (optional)\n"
  456. "\t\tEVENT:\tEvent name\n"
  457. "\t\tFUNC:\tFunction name\n"
  458. "\t\tOFF:\tOffset from function entry (in byte)\n"
  459. "\t\t%return:\tPut the probe at function return\n"
  460. #ifdef HAVE_DWARF_SUPPORT
  461. "\t\tSRC:\tSource code path\n"
  462. "\t\tRL:\tRelative line number from function entry.\n"
  463. "\t\tAL:\tAbsolute line number in file.\n"
  464. "\t\tPT:\tLazy expression of line code.\n"
  465. "\t\tARG:\tProbe argument (local variable name or\n"
  466. "\t\t\tkprobe-tracer argument format.)\n",
  467. #else
  468. "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
  469. #endif
  470. opt_add_probe_event),
  471. OPT_CALLBACK('D', "definition", NULL, PROBEDEF_STR,
  472. "Show trace event definition of given traceevent for k/uprobe_events.",
  473. opt_add_probe_event),
  474. OPT_BOOLEAN('f', "force", &probe_conf.force_add, "forcibly add events"
  475. " with existing name"),
  476. OPT_CALLBACK('L', "line", NULL,
  477. "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
  478. "Show source code lines.", opt_show_lines),
  479. OPT_CALLBACK('V', "vars", NULL,
  480. "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT",
  481. "Show accessible variables on PROBEDEF", opt_show_vars),
  482. OPT_BOOLEAN('\0', "externs", &probe_conf.show_ext_vars,
  483. "Show external variables too (with --vars only)"),
  484. OPT_BOOLEAN('\0', "range", &probe_conf.show_location_range,
  485. "Show variables location range in scope (with --vars only)"),
  486. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  487. "file", "vmlinux pathname"),
  488. OPT_STRING('s', "source", &symbol_conf.source_prefix,
  489. "directory", "path to kernel source"),
  490. OPT_BOOLEAN('\0', "no-inlines", &probe_conf.no_inlines,
  491. "Don't search inlined functions"),
  492. OPT__DRY_RUN(&probe_event_dry_run),
  493. OPT_INTEGER('\0', "max-probes", &probe_conf.max_probes,
  494. "Set how many probe points can be found for a probe."),
  495. OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]",
  496. "Show potential probe-able functions.",
  497. opt_set_filter_with_command, DEFAULT_FUNC_FILTER),
  498. OPT_CALLBACK('\0', "filter", NULL,
  499. "[!]FILTER", "Set a filter (with --vars/funcs only)\n"
  500. "\t\t\t(default: \"" DEFAULT_VAR_FILTER "\" for --vars,\n"
  501. "\t\t\t \"" DEFAULT_FUNC_FILTER "\" for --funcs)",
  502. opt_set_filter),
  503. OPT_CALLBACK('x', "exec", NULL, "executable|path",
  504. "target executable name or path", opt_set_target),
  505. OPT_CALLBACK('m', "module", NULL, "modname|path",
  506. "target module name (for online) or path (for offline)",
  507. opt_set_target),
  508. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  509. "Enable symbol demangling"),
  510. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  511. "Enable kernel symbol demangling"),
  512. OPT_BOOLEAN(0, "cache", &probe_conf.cache, "Manipulate probe cache"),
  513. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  514. "Look for files with symbols relative to this directory"),
  515. OPT_CALLBACK(0, "target-ns", NULL, "pid",
  516. "target pid for namespace contexts", opt_set_target_ns),
  517. OPT_BOOLEAN(0, "bootconfig", &probe_conf.bootconfig,
  518. "Output probe definition with bootconfig format"),
  519. OPT_END()
  520. };
  521. int ret;
  522. set_option_flag(options, 'a', "add", PARSE_OPT_EXCLUSIVE);
  523. set_option_flag(options, 'd', "del", PARSE_OPT_EXCLUSIVE);
  524. set_option_flag(options, 'D', "definition", PARSE_OPT_EXCLUSIVE);
  525. set_option_flag(options, 'l', "list", PARSE_OPT_EXCLUSIVE);
  526. #ifdef HAVE_DWARF_SUPPORT
  527. set_option_flag(options, 'L', "line", PARSE_OPT_EXCLUSIVE);
  528. set_option_flag(options, 'V', "vars", PARSE_OPT_EXCLUSIVE);
  529. #else
  530. # define set_nobuild(s, l, c) set_option_nobuild(options, s, l, "NO_DWARF=1", c)
  531. set_nobuild('L', "line", false);
  532. set_nobuild('V', "vars", false);
  533. set_nobuild('\0', "externs", false);
  534. set_nobuild('\0', "range", false);
  535. set_nobuild('k', "vmlinux", true);
  536. set_nobuild('s', "source", true);
  537. set_nobuild('\0', "no-inlines", true);
  538. # undef set_nobuild
  539. #endif
  540. set_option_flag(options, 'F', "funcs", PARSE_OPT_EXCLUSIVE);
  541. argc = parse_options(argc, argv, options, probe_usage,
  542. PARSE_OPT_STOP_AT_NON_OPTION);
  543. if (quiet) {
  544. if (verbose != 0) {
  545. pr_err(" Error: -v and -q are exclusive.\n");
  546. return -EINVAL;
  547. }
  548. verbose = -1;
  549. }
  550. if (argc > 0) {
  551. if (strcmp(argv[0], "-") == 0) {
  552. usage_with_options_msg(probe_usage, options,
  553. "'-' is not supported.\n");
  554. }
  555. if (params->command && params->command != 'a') {
  556. usage_with_options_msg(probe_usage, options,
  557. "another command except --add is set.\n");
  558. }
  559. ret = parse_probe_event_argv(argc, argv);
  560. if (ret < 0) {
  561. pr_err_with_code(" Error: Command Parse Error.", ret);
  562. return ret;
  563. }
  564. params->command = 'a';
  565. }
  566. ret = symbol__validate_sym_arguments();
  567. if (ret)
  568. return ret;
  569. if (probe_conf.max_probes == 0)
  570. probe_conf.max_probes = MAX_PROBES;
  571. /*
  572. * Only consider the user's kernel image path if given.
  573. */
  574. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  575. /*
  576. * Except for --list, --del and --add, other command doesn't depend
  577. * nor change running kernel. So if user gives offline vmlinux,
  578. * ignore its buildid.
  579. */
  580. if (!strchr("lda", params->command) && symbol_conf.vmlinux_name)
  581. symbol_conf.ignore_vmlinux_buildid = true;
  582. switch (params->command) {
  583. case 'l':
  584. if (params->uprobes) {
  585. pr_err(" Error: Don't use --list with --exec.\n");
  586. parse_options_usage(probe_usage, options, "l", true);
  587. parse_options_usage(NULL, options, "x", true);
  588. return -EINVAL;
  589. }
  590. ret = show_perf_probe_events(params->filter);
  591. if (ret < 0)
  592. pr_err_with_code(" Error: Failed to show event list.", ret);
  593. return ret;
  594. case 'F':
  595. ret = show_available_funcs(params->target, params->nsi,
  596. params->filter, params->uprobes);
  597. if (ret < 0)
  598. pr_err_with_code(" Error: Failed to show functions.", ret);
  599. return ret;
  600. #ifdef HAVE_DWARF_SUPPORT
  601. case 'L':
  602. ret = show_line_range(&params->line_range, params->target,
  603. params->nsi, params->uprobes);
  604. if (ret < 0)
  605. pr_err_with_code(" Error: Failed to show lines.", ret);
  606. return ret;
  607. case 'V':
  608. if (!params->filter)
  609. params->filter = strfilter__new(DEFAULT_VAR_FILTER,
  610. NULL);
  611. ret = show_available_vars(params->events, params->nevents,
  612. params->filter);
  613. if (ret < 0)
  614. pr_err_with_code(" Error: Failed to show vars.", ret);
  615. return ret;
  616. #endif
  617. case 'd':
  618. ret = perf_del_probe_events(params->filter);
  619. if (ret < 0) {
  620. pr_err_with_code(" Error: Failed to delete events.", ret);
  621. return ret;
  622. }
  623. break;
  624. case 'D':
  625. if (probe_conf.bootconfig && params->uprobes) {
  626. pr_err(" Error: --bootconfig doesn't support uprobes.\n");
  627. return -EINVAL;
  628. }
  629. fallthrough;
  630. case 'a':
  631. /* Ensure the last given target is used */
  632. if (params->target && !params->target_used) {
  633. pr_err(" Error: -x/-m must follow the probe definitions.\n");
  634. parse_options_usage(probe_usage, options, "m", true);
  635. parse_options_usage(NULL, options, "x", true);
  636. return -EINVAL;
  637. }
  638. ret = perf_add_probe_events(params->events, params->nevents);
  639. if (ret < 0) {
  640. /*
  641. * When perf_add_probe_events() fails it calls
  642. * cleanup_perf_probe_events(pevs, npevs), i.e.
  643. * cleanup_perf_probe_events(params->events, params->nevents), which
  644. * will call clear_perf_probe_event(), so set nevents to zero
  645. * to avoid cleanup_params() to call clear_perf_probe_event() again
  646. * on the same pevs.
  647. */
  648. params->nevents = 0;
  649. pr_err_with_code(" Error: Failed to add events.", ret);
  650. return ret;
  651. }
  652. break;
  653. default:
  654. usage_with_options(probe_usage, options);
  655. }
  656. return 0;
  657. }
  658. int cmd_probe(int argc, const char **argv)
  659. {
  660. int ret;
  661. ret = init_params();
  662. if (!ret) {
  663. ret = __cmd_probe(argc, argv);
  664. cleanup_params();
  665. }
  666. return ret < 0 ? ret : 0;
  667. }