builtin-probe.c 11 KB

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