builtin-probe.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. pr_err("%s", msg);
  252. pr_debug(" Reason: %s (Code: %d)", strerror(-err), err);
  253. pr_err("\n");
  254. }
  255. static int
  256. __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
  257. {
  258. const char * const probe_usage[] = {
  259. "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
  260. "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
  261. "perf probe [<options>] --del '[GROUP:]EVENT' ...",
  262. "perf probe --list",
  263. #ifdef HAVE_DWARF_SUPPORT
  264. "perf probe [<options>] --line 'LINEDESC'",
  265. "perf probe [<options>] --vars 'PROBEPOINT'",
  266. #endif
  267. NULL
  268. };
  269. const struct option options[] = {
  270. OPT_INCR('v', "verbose", &verbose,
  271. "be more verbose (show parsed arguments, etc)"),
  272. OPT_BOOLEAN('l', "list", &params.list_events,
  273. "list up current probe events"),
  274. OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
  275. opt_del_probe_event),
  276. OPT_CALLBACK('a', "add", NULL,
  277. #ifdef HAVE_DWARF_SUPPORT
  278. "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
  279. " [[NAME=]ARG ...]",
  280. #else
  281. "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]",
  282. #endif
  283. "probe point definition, where\n"
  284. "\t\tGROUP:\tGroup name (optional)\n"
  285. "\t\tEVENT:\tEvent name\n"
  286. "\t\tFUNC:\tFunction name\n"
  287. "\t\tOFF:\tOffset from function entry (in byte)\n"
  288. "\t\t%return:\tPut the probe at function return\n"
  289. #ifdef HAVE_DWARF_SUPPORT
  290. "\t\tSRC:\tSource code path\n"
  291. "\t\tRL:\tRelative line number from function entry.\n"
  292. "\t\tAL:\tAbsolute line number in file.\n"
  293. "\t\tPT:\tLazy expression of line code.\n"
  294. "\t\tARG:\tProbe argument (local variable name or\n"
  295. "\t\t\tkprobe-tracer argument format.)\n",
  296. #else
  297. "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
  298. #endif
  299. opt_add_probe_event),
  300. OPT_BOOLEAN('f', "force", &params.force_add, "forcibly add events"
  301. " with existing name"),
  302. #ifdef HAVE_DWARF_SUPPORT
  303. OPT_CALLBACK('L', "line", NULL,
  304. "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
  305. "Show source code lines.", opt_show_lines),
  306. OPT_CALLBACK('V', "vars", NULL,
  307. "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT",
  308. "Show accessible variables on PROBEDEF", opt_show_vars),
  309. OPT_BOOLEAN('\0', "externs", &params.show_ext_vars,
  310. "Show external variables too (with --vars only)"),
  311. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  312. "file", "vmlinux pathname"),
  313. OPT_STRING('s', "source", &symbol_conf.source_prefix,
  314. "directory", "path to kernel source"),
  315. OPT_CALLBACK('m', "module", NULL, "modname|path",
  316. "target module name (for online) or path (for offline)",
  317. opt_set_target),
  318. #endif
  319. OPT__DRY_RUN(&probe_event_dry_run),
  320. OPT_INTEGER('\0', "max-probes", &params.max_probe_points,
  321. "Set how many probe points can be found for a probe."),
  322. OPT_BOOLEAN('F', "funcs", &params.show_funcs,
  323. "Show potential probe-able functions."),
  324. OPT_CALLBACK('\0', "filter", NULL,
  325. "[!]FILTER", "Set a filter (with --vars/funcs only)\n"
  326. "\t\t\t(default: \"" DEFAULT_VAR_FILTER "\" for --vars,\n"
  327. "\t\t\t \"" DEFAULT_FUNC_FILTER "\" for --funcs)",
  328. opt_set_filter),
  329. OPT_CALLBACK('x', "exec", NULL, "executable|path",
  330. "target executable name or path", opt_set_target),
  331. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  332. "Disable symbol demangling"),
  333. OPT_END()
  334. };
  335. int ret;
  336. argc = parse_options(argc, argv, options, probe_usage,
  337. PARSE_OPT_STOP_AT_NON_OPTION);
  338. if (argc > 0) {
  339. if (strcmp(argv[0], "-") == 0) {
  340. pr_warning(" Error: '-' is not supported.\n");
  341. usage_with_options(probe_usage, options);
  342. }
  343. ret = parse_probe_event_argv(argc, argv);
  344. if (ret < 0) {
  345. pr_err_with_code(" Error: Command Parse Error.", ret);
  346. return ret;
  347. }
  348. }
  349. if (params.max_probe_points == 0)
  350. params.max_probe_points = MAX_PROBES;
  351. if ((!params.nevents && !params.dellist && !params.list_events &&
  352. !params.show_lines && !params.show_funcs))
  353. usage_with_options(probe_usage, options);
  354. /*
  355. * Only consider the user's kernel image path if given.
  356. */
  357. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  358. if (params.list_events) {
  359. if (params.mod_events) {
  360. pr_err(" Error: Don't use --list with --add/--del.\n");
  361. usage_with_options(probe_usage, options);
  362. }
  363. if (params.show_lines) {
  364. pr_err(" Error: Don't use --list with --line.\n");
  365. usage_with_options(probe_usage, options);
  366. }
  367. if (params.show_vars) {
  368. pr_err(" Error: Don't use --list with --vars.\n");
  369. usage_with_options(probe_usage, options);
  370. }
  371. if (params.show_funcs) {
  372. pr_err(" Error: Don't use --list with --funcs.\n");
  373. usage_with_options(probe_usage, options);
  374. }
  375. if (params.uprobes) {
  376. pr_warning(" Error: Don't use --list with --exec.\n");
  377. usage_with_options(probe_usage, options);
  378. }
  379. ret = show_perf_probe_events();
  380. if (ret < 0)
  381. pr_err_with_code(" Error: Failed to show event list.", ret);
  382. return ret;
  383. }
  384. if (params.show_funcs) {
  385. if (params.nevents != 0 || params.dellist) {
  386. pr_err(" Error: Don't use --funcs with"
  387. " --add/--del.\n");
  388. usage_with_options(probe_usage, options);
  389. }
  390. if (params.show_lines) {
  391. pr_err(" Error: Don't use --funcs with --line.\n");
  392. usage_with_options(probe_usage, options);
  393. }
  394. if (params.show_vars) {
  395. pr_err(" Error: Don't use --funcs with --vars.\n");
  396. usage_with_options(probe_usage, options);
  397. }
  398. if (!params.filter)
  399. params.filter = strfilter__new(DEFAULT_FUNC_FILTER,
  400. NULL);
  401. ret = show_available_funcs(params.target, params.filter,
  402. params.uprobes);
  403. strfilter__delete(params.filter);
  404. params.filter = NULL;
  405. if (ret < 0)
  406. pr_err_with_code(" Error: Failed to show functions.", ret);
  407. return ret;
  408. }
  409. #ifdef HAVE_DWARF_SUPPORT
  410. if (params.show_lines) {
  411. if (params.mod_events) {
  412. pr_err(" Error: Don't use --line with"
  413. " --add/--del.\n");
  414. usage_with_options(probe_usage, options);
  415. }
  416. if (params.show_vars) {
  417. pr_err(" Error: Don't use --line with --vars.\n");
  418. usage_with_options(probe_usage, options);
  419. }
  420. ret = show_line_range(&params.line_range, params.target);
  421. if (ret < 0)
  422. pr_err_with_code(" Error: Failed to show lines.", ret);
  423. return ret;
  424. }
  425. if (params.show_vars) {
  426. if (params.mod_events) {
  427. pr_err(" Error: Don't use --vars with"
  428. " --add/--del.\n");
  429. usage_with_options(probe_usage, options);
  430. }
  431. if (!params.filter)
  432. params.filter = strfilter__new(DEFAULT_VAR_FILTER,
  433. NULL);
  434. ret = show_available_vars(params.events, params.nevents,
  435. params.max_probe_points,
  436. params.target,
  437. params.filter,
  438. params.show_ext_vars);
  439. strfilter__delete(params.filter);
  440. params.filter = NULL;
  441. if (ret < 0)
  442. pr_err_with_code(" Error: Failed to show vars.", ret);
  443. return ret;
  444. }
  445. #endif
  446. if (params.dellist) {
  447. ret = del_perf_probe_events(params.dellist);
  448. if (ret < 0) {
  449. pr_err_with_code(" Error: Failed to delete events.", ret);
  450. return ret;
  451. }
  452. }
  453. if (params.nevents) {
  454. ret = add_perf_probe_events(params.events, params.nevents,
  455. params.max_probe_points,
  456. params.target,
  457. params.force_add);
  458. if (ret < 0) {
  459. pr_err_with_code(" Error: Failed to add events.", ret);
  460. return ret;
  461. }
  462. }
  463. return 0;
  464. }
  465. int cmd_probe(int argc, const char **argv, const char *prefix)
  466. {
  467. int ret;
  468. ret = init_params();
  469. if (!ret) {
  470. ret = __cmd_probe(argc, argv, prefix);
  471. cleanup_params();
  472. }
  473. return ret;
  474. }