builtin-probe.c 11 KB

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