builtin-probe.c 11 KB

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