builtin-probe.c 20 KB

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