builtin-probe.c 9.7 KB

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