builtin-probe.c 11 KB

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