builtin-probe.c 8.9 KB

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