builtin-probe.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 <api/fs/debugfs.h>
  40. #include "util/parse-options.h"
  41. #include "util/probe-finder.h"
  42. #include "util/probe-event.h"
  43. #define DEFAULT_VAR_FILTER "!__k???tab_* & !__crc_*"
  44. #define DEFAULT_FUNC_FILTER "!_*"
  45. /* Session management structure */
  46. static struct {
  47. bool list_events;
  48. bool force_add;
  49. bool show_lines;
  50. bool show_vars;
  51. bool show_ext_vars;
  52. bool show_funcs;
  53. bool mod_events;
  54. bool uprobes;
  55. int nevents;
  56. struct perf_probe_event events[MAX_PROBES];
  57. struct strlist *dellist;
  58. struct line_range line_range;
  59. char *target;
  60. int max_probe_points;
  61. struct strfilter *filter;
  62. } params;
  63. /* Parse an event definition. Note that any error must die. */
  64. static int parse_probe_event(const char *str)
  65. {
  66. struct perf_probe_event *pev = &params.events[params.nevents];
  67. int ret;
  68. pr_debug("probe-definition(%d): %s\n", params.nevents, str);
  69. if (++params.nevents == MAX_PROBES) {
  70. pr_err("Too many probes (> %d) were specified.", MAX_PROBES);
  71. return -1;
  72. }
  73. pev->uprobes = params.uprobes;
  74. /* Parse a perf-probe command into event */
  75. ret = parse_perf_probe_command(str, pev);
  76. pr_debug("%d arguments\n", pev->nargs);
  77. return ret;
  78. }
  79. static int set_target(const char *ptr)
  80. {
  81. int found = 0;
  82. const char *buf;
  83. /*
  84. * The first argument after options can be an absolute path
  85. * to an executable / library or kernel module.
  86. *
  87. * TODO: Support relative path, and $PATH, $LD_LIBRARY_PATH,
  88. * short module name.
  89. */
  90. if (!params.target && ptr && *ptr == '/') {
  91. params.target = strdup(ptr);
  92. if (!params.target)
  93. return -ENOMEM;
  94. found = 1;
  95. buf = ptr + (strlen(ptr) - 3);
  96. if (strcmp(buf, ".ko"))
  97. params.uprobes = true;
  98. }
  99. return found;
  100. }
  101. static int parse_probe_event_argv(int argc, const char **argv)
  102. {
  103. int i, len, ret, found_target;
  104. char *buf;
  105. found_target = set_target(argv[0]);
  106. if (found_target < 0)
  107. return found_target;
  108. if (found_target && argc == 1)
  109. return 0;
  110. /* Bind up rest arguments */
  111. len = 0;
  112. for (i = 0; i < argc; i++) {
  113. if (i == 0 && found_target)
  114. continue;
  115. len += strlen(argv[i]) + 1;
  116. }
  117. buf = zalloc(len + 1);
  118. if (buf == NULL)
  119. return -ENOMEM;
  120. len = 0;
  121. for (i = 0; i < argc; i++) {
  122. if (i == 0 && found_target)
  123. continue;
  124. len += sprintf(&buf[len], "%s ", argv[i]);
  125. }
  126. params.mod_events = true;
  127. ret = parse_probe_event(buf);
  128. free(buf);
  129. return ret;
  130. }
  131. static int opt_add_probe_event(const struct option *opt __maybe_unused,
  132. const char *str, int unset __maybe_unused)
  133. {
  134. if (str) {
  135. params.mod_events = true;
  136. return parse_probe_event(str);
  137. } else
  138. return 0;
  139. }
  140. static int opt_del_probe_event(const struct option *opt __maybe_unused,
  141. const char *str, int unset __maybe_unused)
  142. {
  143. if (str) {
  144. params.mod_events = true;
  145. if (!params.dellist)
  146. params.dellist = strlist__new(true, NULL);
  147. strlist__add(params.dellist, str);
  148. }
  149. return 0;
  150. }
  151. static int opt_set_target(const struct option *opt, const char *str,
  152. int unset __maybe_unused)
  153. {
  154. int ret = -ENOENT;
  155. char *tmp;
  156. if (str && !params.target) {
  157. if (!strcmp(opt->long_name, "exec"))
  158. params.uprobes = true;
  159. #ifdef HAVE_DWARF_SUPPORT
  160. else if (!strcmp(opt->long_name, "module"))
  161. params.uprobes = false;
  162. #endif
  163. else
  164. return ret;
  165. /* Expand given path to absolute path, except for modulename */
  166. if (params.uprobes || strchr(str, '/')) {
  167. tmp = realpath(str, NULL);
  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. params.target = tmp;
  178. ret = 0;
  179. }
  180. return ret;
  181. }
  182. #ifdef HAVE_DWARF_SUPPORT
  183. static int opt_show_lines(const struct option *opt __maybe_unused,
  184. const char *str, int unset __maybe_unused)
  185. {
  186. int ret = 0;
  187. if (!str)
  188. return 0;
  189. if (params.show_lines) {
  190. pr_warning("Warning: more than one --line options are"
  191. " detected. Only the first one is valid.\n");
  192. return 0;
  193. }
  194. params.show_lines = true;
  195. ret = parse_line_range_desc(str, &params.line_range);
  196. return ret;
  197. }
  198. static int opt_show_vars(const struct option *opt __maybe_unused,
  199. const char *str, int unset __maybe_unused)
  200. {
  201. struct perf_probe_event *pev = &params.events[params.nevents];
  202. int ret;
  203. if (!str)
  204. return 0;
  205. ret = parse_probe_event(str);
  206. if (!ret && pev->nargs != 0) {
  207. pr_err(" Error: '--vars' doesn't accept arguments.\n");
  208. return -EINVAL;
  209. }
  210. params.show_vars = true;
  211. return ret;
  212. }
  213. #endif
  214. static int opt_set_filter(const struct option *opt __maybe_unused,
  215. const char *str, int unset __maybe_unused)
  216. {
  217. const char *err;
  218. if (str) {
  219. pr_debug2("Set filter: %s\n", str);
  220. if (params.filter)
  221. strfilter__delete(params.filter);
  222. params.filter = strfilter__new(str, &err);
  223. if (!params.filter) {
  224. pr_err("Filter parse error at %td.\n", err - str + 1);
  225. pr_err("Source: \"%s\"\n", str);
  226. pr_err(" %*c\n", (int)(err - str + 1), '^');
  227. return -EINVAL;
  228. }
  229. }
  230. return 0;
  231. }
  232. static int init_params(void)
  233. {
  234. return line_range__init(&params.line_range);
  235. }
  236. static void cleanup_params(void)
  237. {
  238. int i;
  239. for (i = 0; i < params.nevents; i++)
  240. clear_perf_probe_event(params.events + i);
  241. if (params.dellist)
  242. strlist__delete(params.dellist);
  243. line_range__clear(&params.line_range);
  244. free(params.target);
  245. if (params.filter)
  246. strfilter__delete(params.filter);
  247. memset(&params, 0, sizeof(params));
  248. }
  249. static void pr_err_with_code(const char *msg, int err)
  250. {
  251. char sbuf[STRERR_BUFSIZE];
  252. pr_err("%s", msg);
  253. pr_debug(" Reason: %s (Code: %d)",
  254. strerror_r(-err, sbuf, sizeof(sbuf)), err);
  255. pr_err("\n");
  256. }
  257. static int
  258. __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
  259. {
  260. const char * const probe_usage[] = {
  261. "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
  262. "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
  263. "perf probe [<options>] --del '[GROUP:]EVENT' ...",
  264. "perf probe --list",
  265. #ifdef HAVE_DWARF_SUPPORT
  266. "perf probe [<options>] --line 'LINEDESC'",
  267. "perf probe [<options>] --vars 'PROBEPOINT'",
  268. #endif
  269. NULL
  270. };
  271. const struct option options[] = {
  272. OPT_INCR('v', "verbose", &verbose,
  273. "be more verbose (show parsed arguments, etc)"),
  274. OPT_BOOLEAN('l', "list", &params.list_events,
  275. "list up current probe events"),
  276. OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
  277. opt_del_probe_event),
  278. OPT_CALLBACK('a', "add", NULL,
  279. #ifdef HAVE_DWARF_SUPPORT
  280. "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
  281. " [[NAME=]ARG ...]",
  282. #else
  283. "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]",
  284. #endif
  285. "probe point definition, where\n"
  286. "\t\tGROUP:\tGroup name (optional)\n"
  287. "\t\tEVENT:\tEvent name\n"
  288. "\t\tFUNC:\tFunction name\n"
  289. "\t\tOFF:\tOffset from function entry (in byte)\n"
  290. "\t\t%return:\tPut the probe at function return\n"
  291. #ifdef HAVE_DWARF_SUPPORT
  292. "\t\tSRC:\tSource code path\n"
  293. "\t\tRL:\tRelative line number from function entry.\n"
  294. "\t\tAL:\tAbsolute line number in file.\n"
  295. "\t\tPT:\tLazy expression of line code.\n"
  296. "\t\tARG:\tProbe argument (local variable name or\n"
  297. "\t\t\tkprobe-tracer argument format.)\n",
  298. #else
  299. "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
  300. #endif
  301. opt_add_probe_event),
  302. OPT_BOOLEAN('f', "force", &params.force_add, "forcibly add events"
  303. " with existing name"),
  304. #ifdef HAVE_DWARF_SUPPORT
  305. OPT_CALLBACK('L', "line", NULL,
  306. "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
  307. "Show source code lines.", opt_show_lines),
  308. OPT_CALLBACK('V', "vars", NULL,
  309. "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT",
  310. "Show accessible variables on PROBEDEF", opt_show_vars),
  311. OPT_BOOLEAN('\0', "externs", &params.show_ext_vars,
  312. "Show external variables too (with --vars only)"),
  313. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  314. "file", "vmlinux pathname"),
  315. OPT_STRING('s', "source", &symbol_conf.source_prefix,
  316. "directory", "path to kernel source"),
  317. OPT_CALLBACK('m', "module", NULL, "modname|path",
  318. "target module name (for online) or path (for offline)",
  319. opt_set_target),
  320. #endif
  321. OPT__DRY_RUN(&probe_event_dry_run),
  322. OPT_INTEGER('\0', "max-probes", &params.max_probe_points,
  323. "Set how many probe points can be found for a probe."),
  324. OPT_BOOLEAN('F', "funcs", &params.show_funcs,
  325. "Show potential probe-able functions."),
  326. OPT_CALLBACK('\0', "filter", NULL,
  327. "[!]FILTER", "Set a filter (with --vars/funcs only)\n"
  328. "\t\t\t(default: \"" DEFAULT_VAR_FILTER "\" for --vars,\n"
  329. "\t\t\t \"" DEFAULT_FUNC_FILTER "\" for --funcs)",
  330. opt_set_filter),
  331. OPT_CALLBACK('x', "exec", NULL, "executable|path",
  332. "target executable name or path", opt_set_target),
  333. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  334. "Disable symbol demangling"),
  335. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  336. "Enable kernel symbol demangling"),
  337. OPT_END()
  338. };
  339. int ret;
  340. argc = parse_options(argc, argv, options, probe_usage,
  341. PARSE_OPT_STOP_AT_NON_OPTION);
  342. if (argc > 0) {
  343. if (strcmp(argv[0], "-") == 0) {
  344. pr_warning(" Error: '-' is not supported.\n");
  345. usage_with_options(probe_usage, options);
  346. }
  347. ret = parse_probe_event_argv(argc, argv);
  348. if (ret < 0) {
  349. pr_err_with_code(" Error: Command Parse Error.", ret);
  350. return ret;
  351. }
  352. }
  353. if (params.max_probe_points == 0)
  354. params.max_probe_points = MAX_PROBES;
  355. if ((!params.nevents && !params.dellist && !params.list_events &&
  356. !params.show_lines && !params.show_funcs))
  357. usage_with_options(probe_usage, options);
  358. /*
  359. * Only consider the user's kernel image path if given.
  360. */
  361. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  362. if (params.list_events) {
  363. if (params.mod_events) {
  364. pr_err(" Error: Don't use --list with --add/--del.\n");
  365. usage_with_options(probe_usage, options);
  366. }
  367. if (params.show_lines) {
  368. pr_err(" Error: Don't use --list with --line.\n");
  369. usage_with_options(probe_usage, options);
  370. }
  371. if (params.show_vars) {
  372. pr_err(" Error: Don't use --list with --vars.\n");
  373. usage_with_options(probe_usage, options);
  374. }
  375. if (params.show_funcs) {
  376. pr_err(" Error: Don't use --list with --funcs.\n");
  377. usage_with_options(probe_usage, options);
  378. }
  379. if (params.uprobes) {
  380. pr_warning(" Error: Don't use --list with --exec.\n");
  381. usage_with_options(probe_usage, options);
  382. }
  383. ret = show_perf_probe_events();
  384. if (ret < 0)
  385. pr_err_with_code(" Error: Failed to show event list.", ret);
  386. return ret;
  387. }
  388. if (params.show_funcs) {
  389. if (params.nevents != 0 || params.dellist) {
  390. pr_err(" Error: Don't use --funcs with"
  391. " --add/--del.\n");
  392. usage_with_options(probe_usage, options);
  393. }
  394. if (params.show_lines) {
  395. pr_err(" Error: Don't use --funcs with --line.\n");
  396. usage_with_options(probe_usage, options);
  397. }
  398. if (params.show_vars) {
  399. pr_err(" Error: Don't use --funcs with --vars.\n");
  400. usage_with_options(probe_usage, options);
  401. }
  402. if (!params.filter)
  403. params.filter = strfilter__new(DEFAULT_FUNC_FILTER,
  404. NULL);
  405. ret = show_available_funcs(params.target, params.filter,
  406. params.uprobes);
  407. strfilter__delete(params.filter);
  408. params.filter = NULL;
  409. if (ret < 0)
  410. pr_err_with_code(" Error: Failed to show functions.", ret);
  411. return ret;
  412. }
  413. #ifdef HAVE_DWARF_SUPPORT
  414. if (params.show_lines) {
  415. if (params.mod_events) {
  416. pr_err(" Error: Don't use --line with"
  417. " --add/--del.\n");
  418. usage_with_options(probe_usage, options);
  419. }
  420. if (params.show_vars) {
  421. pr_err(" Error: Don't use --line with --vars.\n");
  422. usage_with_options(probe_usage, options);
  423. }
  424. ret = show_line_range(&params.line_range, params.target,
  425. params.uprobes);
  426. if (ret < 0)
  427. pr_err_with_code(" Error: Failed to show lines.", ret);
  428. return ret;
  429. }
  430. if (params.show_vars) {
  431. if (params.mod_events) {
  432. pr_err(" Error: Don't use --vars with"
  433. " --add/--del.\n");
  434. usage_with_options(probe_usage, options);
  435. }
  436. if (!params.filter)
  437. params.filter = strfilter__new(DEFAULT_VAR_FILTER,
  438. NULL);
  439. ret = show_available_vars(params.events, params.nevents,
  440. params.max_probe_points,
  441. params.target,
  442. params.filter,
  443. params.show_ext_vars);
  444. strfilter__delete(params.filter);
  445. params.filter = NULL;
  446. if (ret < 0)
  447. pr_err_with_code(" Error: Failed to show vars.", ret);
  448. return ret;
  449. }
  450. #endif
  451. if (params.dellist) {
  452. ret = del_perf_probe_events(params.dellist);
  453. if (ret < 0) {
  454. pr_err_with_code(" Error: Failed to delete events.", ret);
  455. return ret;
  456. }
  457. }
  458. if (params.nevents) {
  459. ret = add_perf_probe_events(params.events, params.nevents,
  460. params.max_probe_points,
  461. params.target,
  462. params.force_add);
  463. if (ret < 0) {
  464. pr_err_with_code(" Error: Failed to add events.", ret);
  465. return ret;
  466. }
  467. }
  468. return 0;
  469. }
  470. int cmd_probe(int argc, const char **argv, const char *prefix)
  471. {
  472. int ret;
  473. ret = init_params();
  474. if (!ret) {
  475. ret = __cmd_probe(argc, argv, prefix);
  476. cleanup_params();
  477. }
  478. return ret;
  479. }