builtin-probe.c 12 KB

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