builtin-probe.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. #define DEFAULT_LIST_FILTER "*:*"
  46. /* Session management structure */
  47. static struct {
  48. int command; /* Command short_name */
  49. bool list_events;
  50. bool force_add;
  51. bool show_ext_vars;
  52. bool uprobes;
  53. bool quiet;
  54. bool target_used;
  55. int nevents;
  56. struct perf_probe_event events[MAX_PROBES];
  57. struct line_range line_range;
  58. char *target;
  59. int max_probe_points;
  60. struct strfilter *filter;
  61. } params;
  62. /* Parse an event definition. Note that any error must die. */
  63. static int parse_probe_event(const char *str)
  64. {
  65. struct perf_probe_event *pev = &params.events[params.nevents];
  66. int ret;
  67. pr_debug("probe-definition(%d): %s\n", params.nevents, str);
  68. if (++params.nevents == MAX_PROBES) {
  69. pr_err("Too many probes (> %d) were specified.", MAX_PROBES);
  70. return -1;
  71. }
  72. pev->uprobes = params.uprobes;
  73. if (params.target) {
  74. pev->target = strdup(params.target);
  75. if (!pev->target)
  76. return -ENOMEM;
  77. params.target_used = true;
  78. }
  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. #ifdef HAVE_DWARF_SUPPORT
  163. else if (!strcmp(opt->long_name, "module"))
  164. params.uprobes = false;
  165. #endif
  166. else
  167. return ret;
  168. /* Expand given path to absolute path, except for modulename */
  169. if (params.uprobes || strchr(str, '/')) {
  170. tmp = realpath(str, NULL);
  171. if (!tmp) {
  172. pr_warning("Failed to get the absolute path of %s: %m\n", str);
  173. return ret;
  174. }
  175. } else {
  176. tmp = strdup(str);
  177. if (!tmp)
  178. return -ENOMEM;
  179. }
  180. free(params.target);
  181. params.target = tmp;
  182. params.target_used = false;
  183. ret = 0;
  184. }
  185. return ret;
  186. }
  187. /* Command option callbacks */
  188. #ifdef HAVE_DWARF_SUPPORT
  189. static int opt_show_lines(const struct option *opt,
  190. const char *str, int unset __maybe_unused)
  191. {
  192. int ret = 0;
  193. if (!str)
  194. return 0;
  195. if (params.command == 'L') {
  196. pr_warning("Warning: more than one --line options are"
  197. " detected. Only the first one is valid.\n");
  198. return 0;
  199. }
  200. params.command = opt->short_name;
  201. ret = parse_line_range_desc(str, &params.line_range);
  202. return ret;
  203. }
  204. static int opt_show_vars(const struct option *opt,
  205. const char *str, int unset __maybe_unused)
  206. {
  207. struct perf_probe_event *pev = &params.events[params.nevents];
  208. int ret;
  209. if (!str)
  210. return 0;
  211. ret = parse_probe_event(str);
  212. if (!ret && pev->nargs != 0) {
  213. pr_err(" Error: '--vars' doesn't accept arguments.\n");
  214. return -EINVAL;
  215. }
  216. params.command = opt->short_name;
  217. return ret;
  218. }
  219. #endif
  220. static int opt_add_probe_event(const struct option *opt,
  221. const char *str, int unset __maybe_unused)
  222. {
  223. if (str) {
  224. params.command = opt->short_name;
  225. return parse_probe_event(str);
  226. }
  227. return 0;
  228. }
  229. static int opt_set_filter_with_command(const struct option *opt,
  230. const char *str, int unset)
  231. {
  232. if (!unset)
  233. params.command = opt->short_name;
  234. if (str)
  235. return params_add_filter(str);
  236. return 0;
  237. }
  238. static int opt_set_filter(const struct option *opt __maybe_unused,
  239. const char *str, int unset __maybe_unused)
  240. {
  241. if (str)
  242. return params_add_filter(str);
  243. return 0;
  244. }
  245. static int init_params(void)
  246. {
  247. return line_range__init(&params.line_range);
  248. }
  249. static void cleanup_params(void)
  250. {
  251. int i;
  252. for (i = 0; i < params.nevents; i++)
  253. clear_perf_probe_event(params.events + i);
  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 [GROUP:]EVENT ...",
  276. #ifdef HAVE_DWARF_SUPPORT
  277. "perf probe [<options>] --line 'LINEDESC'",
  278. "perf probe [<options>] --vars 'PROBEPOINT'",
  279. #endif
  280. "perf probe [<options>] --funcs",
  281. NULL
  282. };
  283. struct option options[] = {
  284. OPT_INCR('v', "verbose", &verbose,
  285. "be more verbose (show parsed arguments, etc)"),
  286. OPT_BOOLEAN('q', "quiet", &params.quiet,
  287. "be quiet (do not show any mesages)"),
  288. OPT_CALLBACK_DEFAULT('l', "list", NULL, "[GROUP:]EVENT",
  289. "list up probe events",
  290. opt_set_filter_with_command, DEFAULT_LIST_FILTER),
  291. OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
  292. opt_set_filter_with_command),
  293. OPT_CALLBACK('a', "add", NULL,
  294. #ifdef HAVE_DWARF_SUPPORT
  295. "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
  296. " [[NAME=]ARG ...]",
  297. #else
  298. "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]",
  299. #endif
  300. "probe point definition, where\n"
  301. "\t\tGROUP:\tGroup name (optional)\n"
  302. "\t\tEVENT:\tEvent name\n"
  303. "\t\tFUNC:\tFunction name\n"
  304. "\t\tOFF:\tOffset from function entry (in byte)\n"
  305. "\t\t%return:\tPut the probe at function return\n"
  306. #ifdef HAVE_DWARF_SUPPORT
  307. "\t\tSRC:\tSource code path\n"
  308. "\t\tRL:\tRelative line number from function entry.\n"
  309. "\t\tAL:\tAbsolute line number in file.\n"
  310. "\t\tPT:\tLazy expression of line code.\n"
  311. "\t\tARG:\tProbe argument (local variable name or\n"
  312. "\t\t\tkprobe-tracer argument format.)\n",
  313. #else
  314. "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
  315. #endif
  316. opt_add_probe_event),
  317. OPT_BOOLEAN('f', "force", &params.force_add, "forcibly add events"
  318. " with existing name"),
  319. #ifdef HAVE_DWARF_SUPPORT
  320. OPT_CALLBACK('L', "line", NULL,
  321. "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
  322. "Show source code lines.", opt_show_lines),
  323. OPT_CALLBACK('V', "vars", NULL,
  324. "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT",
  325. "Show accessible variables on PROBEDEF", opt_show_vars),
  326. OPT_BOOLEAN('\0', "externs", &params.show_ext_vars,
  327. "Show external variables too (with --vars only)"),
  328. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  329. "file", "vmlinux pathname"),
  330. OPT_STRING('s', "source", &symbol_conf.source_prefix,
  331. "directory", "path to kernel source"),
  332. OPT_CALLBACK('m', "module", NULL, "modname|path",
  333. "target module name (for online) or path (for offline)",
  334. opt_set_target),
  335. #endif
  336. OPT__DRY_RUN(&probe_event_dry_run),
  337. OPT_INTEGER('\0', "max-probes", &params.max_probe_points,
  338. "Set how many probe points can be found for a probe."),
  339. OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]",
  340. "Show potential probe-able functions.",
  341. opt_set_filter_with_command, DEFAULT_FUNC_FILTER),
  342. OPT_CALLBACK('\0', "filter", NULL,
  343. "[!]FILTER", "Set a filter (with --vars/funcs only)\n"
  344. "\t\t\t(default: \"" DEFAULT_VAR_FILTER "\" for --vars,\n"
  345. "\t\t\t \"" DEFAULT_FUNC_FILTER "\" for --funcs)",
  346. opt_set_filter),
  347. OPT_CALLBACK('x', "exec", NULL, "executable|path",
  348. "target executable name or path", opt_set_target),
  349. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  350. "Enable symbol demangling"),
  351. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  352. "Enable kernel symbol demangling"),
  353. OPT_END()
  354. };
  355. int ret;
  356. set_option_flag(options, 'a', "add", PARSE_OPT_EXCLUSIVE);
  357. set_option_flag(options, 'd', "del", PARSE_OPT_EXCLUSIVE);
  358. set_option_flag(options, 'l', "list", PARSE_OPT_EXCLUSIVE);
  359. #ifdef HAVE_DWARF_SUPPORT
  360. set_option_flag(options, 'L', "line", PARSE_OPT_EXCLUSIVE);
  361. set_option_flag(options, 'V', "vars", PARSE_OPT_EXCLUSIVE);
  362. #endif
  363. set_option_flag(options, 'F', "funcs", PARSE_OPT_EXCLUSIVE);
  364. argc = parse_options(argc, argv, options, probe_usage,
  365. PARSE_OPT_STOP_AT_NON_OPTION);
  366. if (argc > 0) {
  367. if (strcmp(argv[0], "-") == 0) {
  368. pr_warning(" Error: '-' is not supported.\n");
  369. usage_with_options(probe_usage, options);
  370. }
  371. if (params.command && params.command != 'a') {
  372. pr_warning(" Error: another command except --add is set.\n");
  373. usage_with_options(probe_usage, options);
  374. }
  375. ret = parse_probe_event_argv(argc, argv);
  376. if (ret < 0) {
  377. pr_err_with_code(" Error: Command Parse Error.", ret);
  378. return ret;
  379. }
  380. params.command = 'a';
  381. }
  382. if (params.quiet) {
  383. if (verbose != 0) {
  384. pr_err(" Error: -v and -q are exclusive.\n");
  385. return -EINVAL;
  386. }
  387. verbose = -1;
  388. }
  389. if (params.max_probe_points == 0)
  390. params.max_probe_points = MAX_PROBES;
  391. /*
  392. * Only consider the user's kernel image path if given.
  393. */
  394. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  395. switch (params.command) {
  396. case 'l':
  397. if (params.uprobes) {
  398. pr_warning(" Error: Don't use --list with --exec.\n");
  399. usage_with_options(probe_usage, options);
  400. }
  401. ret = show_perf_probe_events(params.filter);
  402. if (ret < 0)
  403. pr_err_with_code(" Error: Failed to show event list.", ret);
  404. return ret;
  405. case 'F':
  406. ret = show_available_funcs(params.target, params.filter,
  407. params.uprobes);
  408. if (ret < 0)
  409. pr_err_with_code(" Error: Failed to show functions.", ret);
  410. return ret;
  411. #ifdef HAVE_DWARF_SUPPORT
  412. case 'L':
  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. case 'V':
  419. if (!params.filter)
  420. params.filter = strfilter__new(DEFAULT_VAR_FILTER,
  421. NULL);
  422. ret = show_available_vars(params.events, params.nevents,
  423. params.max_probe_points,
  424. params.target,
  425. params.filter,
  426. params.show_ext_vars);
  427. if (ret < 0)
  428. pr_err_with_code(" Error: Failed to show vars.", ret);
  429. return ret;
  430. #endif
  431. case 'd':
  432. ret = del_perf_probe_events(params.filter);
  433. if (ret < 0) {
  434. pr_err_with_code(" Error: Failed to delete events.", ret);
  435. return ret;
  436. }
  437. break;
  438. case 'a':
  439. /* Ensure the last given target is used */
  440. if (params.target && !params.target_used) {
  441. pr_warning(" Error: -x/-m must follow the probe definitions.\n");
  442. usage_with_options(probe_usage, options);
  443. }
  444. ret = add_perf_probe_events(params.events, params.nevents,
  445. params.max_probe_points,
  446. params.force_add);
  447. if (ret < 0) {
  448. pr_err_with_code(" Error: Failed to add events.", ret);
  449. return ret;
  450. }
  451. break;
  452. default:
  453. usage_with_options(probe_usage, options);
  454. }
  455. return 0;
  456. }
  457. int cmd_probe(int argc, const char **argv, const char *prefix)
  458. {
  459. int ret;
  460. ret = init_params();
  461. if (!ret) {
  462. ret = __cmd_probe(argc, argv, prefix);
  463. cleanup_params();
  464. }
  465. return ret < 0 ? ret : 0;
  466. }