builtin-probe.c 13 KB

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