builtin-probe.c 13 KB

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