builtin-probe.c 19 KB

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