builtin-probe.c 19 KB

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