builtin-probe.c 19 KB

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