builtin-probe.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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/event.h"
  39. #include "util/debug.h"
  40. #include "util/debugfs.h"
  41. #include "util/symbol.h"
  42. #include "util/thread.h"
  43. #include "util/parse-options.h"
  44. #include "util/parse-events.h" /* For debugfs_path */
  45. #include "util/probe-finder.h"
  46. #include "util/probe-event.h"
  47. #define MAX_PATH_LEN 256
  48. /* Session management structure */
  49. static struct {
  50. bool need_dwarf;
  51. bool list_events;
  52. bool force_add;
  53. bool show_lines;
  54. int nr_probe;
  55. struct probe_point probes[MAX_PROBES];
  56. struct strlist *dellist;
  57. struct map_groups kmap_groups;
  58. struct map *kmaps[MAP__NR_TYPES];
  59. struct line_range line_range;
  60. } session;
  61. /* Parse an event definition. Note that any error must die. */
  62. static void parse_probe_event(const char *str)
  63. {
  64. struct probe_point *pp = &session.probes[session.nr_probe];
  65. pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
  66. if (++session.nr_probe == MAX_PROBES)
  67. die("Too many probes (> %d) are specified.", MAX_PROBES);
  68. /* Parse perf-probe event into probe_point */
  69. parse_perf_probe_event(str, pp, &session.need_dwarf);
  70. pr_debug("%d arguments\n", pp->nr_args);
  71. }
  72. static void parse_probe_event_argv(int argc, const char **argv)
  73. {
  74. int i, len;
  75. char *buf;
  76. /* Bind up rest arguments */
  77. len = 0;
  78. for (i = 0; i < argc; i++)
  79. len += strlen(argv[i]) + 1;
  80. buf = zalloc(len + 1);
  81. if (!buf)
  82. die("Failed to allocate memory for binding arguments.");
  83. len = 0;
  84. for (i = 0; i < argc; i++)
  85. len += sprintf(&buf[len], "%s ", argv[i]);
  86. parse_probe_event(buf);
  87. free(buf);
  88. }
  89. static int opt_add_probe_event(const struct option *opt __used,
  90. const char *str, int unset __used)
  91. {
  92. if (str)
  93. parse_probe_event(str);
  94. return 0;
  95. }
  96. static int opt_del_probe_event(const struct option *opt __used,
  97. const char *str, int unset __used)
  98. {
  99. if (str) {
  100. if (!session.dellist)
  101. session.dellist = strlist__new(true, NULL);
  102. strlist__add(session.dellist, str);
  103. }
  104. return 0;
  105. }
  106. /* Currently just checking function name from symbol map */
  107. static void evaluate_probe_point(struct probe_point *pp)
  108. {
  109. struct symbol *sym;
  110. sym = map__find_symbol_by_name(session.kmaps[MAP__FUNCTION],
  111. pp->function, NULL);
  112. if (!sym)
  113. die("Kernel symbol \'%s\' not found - probe not added.",
  114. pp->function);
  115. }
  116. #ifndef NO_DWARF_SUPPORT
  117. static int open_vmlinux(void)
  118. {
  119. if (map__load(session.kmaps[MAP__FUNCTION], NULL) < 0) {
  120. pr_debug("Failed to load kernel map.\n");
  121. return -EINVAL;
  122. }
  123. pr_debug("Try to open %s\n",
  124. session.kmaps[MAP__FUNCTION]->dso->long_name);
  125. return open(session.kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
  126. }
  127. static int opt_show_lines(const struct option *opt __used,
  128. const char *str, int unset __used)
  129. {
  130. if (str)
  131. parse_line_range_desc(str, &session.line_range);
  132. INIT_LIST_HEAD(&session.line_range.line_list);
  133. session.show_lines = true;
  134. return 0;
  135. }
  136. #endif
  137. static const char * const probe_usage[] = {
  138. "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
  139. "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
  140. "perf probe [<options>] --del '[GROUP:]EVENT' ...",
  141. "perf probe --list",
  142. #ifndef NO_DWARF_SUPPORT
  143. "perf probe --line 'LINEDESC'",
  144. #endif
  145. NULL
  146. };
  147. static const struct option options[] = {
  148. OPT_BOOLEAN('v', "verbose", &verbose,
  149. "be more verbose (show parsed arguments, etc)"),
  150. #ifndef NO_DWARF_SUPPORT
  151. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  152. "file", "vmlinux pathname"),
  153. #endif
  154. OPT_BOOLEAN('l', "list", &session.list_events,
  155. "list up current probe events"),
  156. OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
  157. opt_del_probe_event),
  158. OPT_CALLBACK('a', "add", NULL,
  159. #ifdef NO_DWARF_SUPPORT
  160. "[EVENT=]FUNC[+OFF|%return] [ARG ...]",
  161. #else
  162. "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
  163. " [ARG ...]",
  164. #endif
  165. "probe point definition, where\n"
  166. "\t\tGROUP:\tGroup name (optional)\n"
  167. "\t\tEVENT:\tEvent name\n"
  168. "\t\tFUNC:\tFunction name\n"
  169. "\t\tOFF:\tOffset from function entry (in byte)\n"
  170. "\t\t%return:\tPut the probe at function return\n"
  171. #ifdef NO_DWARF_SUPPORT
  172. "\t\tARG:\tProbe argument (only \n"
  173. #else
  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. #endif
  180. "\t\t\tkprobe-tracer argument format.)\n",
  181. opt_add_probe_event),
  182. OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
  183. " with existing name"),
  184. #ifndef NO_DWARF_SUPPORT
  185. OPT_CALLBACK('L', "line", NULL,
  186. "FUNC[:RLN[+NUM|:RLN2]]|SRC:ALN[+NUM|:ALN2]",
  187. "Show source code lines.", opt_show_lines),
  188. #endif
  189. OPT_END()
  190. };
  191. /* Initialize symbol maps for vmlinux */
  192. static void init_vmlinux(void)
  193. {
  194. symbol_conf.sort_by_name = true;
  195. if (symbol_conf.vmlinux_name == NULL)
  196. symbol_conf.try_vmlinux_path = true;
  197. else
  198. pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
  199. if (symbol__init() < 0)
  200. die("Failed to init symbol map.");
  201. map_groups__init(&session.kmap_groups);
  202. if (map_groups__create_kernel_maps(&session.kmap_groups,
  203. session.kmaps) < 0)
  204. die("Failed to create kernel maps.");
  205. }
  206. int cmd_probe(int argc, const char **argv, const char *prefix __used)
  207. {
  208. int i, ret;
  209. #ifndef NO_DWARF_SUPPORT
  210. int fd;
  211. #endif
  212. struct probe_point *pp;
  213. argc = parse_options(argc, argv, options, probe_usage,
  214. PARSE_OPT_STOP_AT_NON_OPTION);
  215. if (argc > 0) {
  216. if (strcmp(argv[0], "-") == 0) {
  217. pr_warning(" Error: '-' is not supported.\n");
  218. usage_with_options(probe_usage, options);
  219. }
  220. parse_probe_event_argv(argc, argv);
  221. }
  222. if ((!session.nr_probe && !session.dellist && !session.list_events &&
  223. !session.show_lines))
  224. usage_with_options(probe_usage, options);
  225. if (debugfs_valid_mountpoint(debugfs_path) < 0)
  226. die("Failed to find debugfs path.");
  227. if (session.list_events) {
  228. if (session.nr_probe != 0 || session.dellist) {
  229. pr_warning(" Error: Don't use --list with"
  230. " --add/--del.\n");
  231. usage_with_options(probe_usage, options);
  232. }
  233. if (session.show_lines) {
  234. pr_warning(" Error: Don't use --list with --line.\n");
  235. usage_with_options(probe_usage, options);
  236. }
  237. show_perf_probe_events();
  238. return 0;
  239. }
  240. #ifndef NO_DWARF_SUPPORT
  241. if (session.show_lines) {
  242. if (session.nr_probe != 0 || session.dellist) {
  243. pr_warning(" Error: Don't use --line with"
  244. " --add/--del.\n");
  245. usage_with_options(probe_usage, options);
  246. }
  247. init_vmlinux();
  248. fd = open_vmlinux();
  249. if (fd < 0)
  250. die("Could not open debuginfo file.");
  251. ret = find_line_range(fd, &session.line_range);
  252. if (ret <= 0)
  253. die("Source line is not found.\n");
  254. close(fd);
  255. show_line_range(&session.line_range);
  256. return 0;
  257. }
  258. #endif
  259. if (session.dellist) {
  260. del_trace_kprobe_events(session.dellist);
  261. strlist__delete(session.dellist);
  262. if (session.nr_probe == 0)
  263. return 0;
  264. }
  265. /* Add probes */
  266. init_vmlinux();
  267. if (session.need_dwarf)
  268. #ifdef NO_DWARF_SUPPORT
  269. die("Debuginfo-analysis is not supported");
  270. #else /* !NO_DWARF_SUPPORT */
  271. pr_debug("Some probes require debuginfo.\n");
  272. fd = open_vmlinux();
  273. if (fd < 0) {
  274. if (session.need_dwarf)
  275. die("Could not open debuginfo file.");
  276. pr_debug("Could not open vmlinux/module file."
  277. " Try to use symbols.\n");
  278. goto end_dwarf;
  279. }
  280. /* Searching probe points */
  281. for (i = 0; i < session.nr_probe; i++) {
  282. pp = &session.probes[i];
  283. if (pp->found)
  284. continue;
  285. lseek(fd, SEEK_SET, 0);
  286. ret = find_probe_point(fd, pp);
  287. if (ret > 0)
  288. continue;
  289. if (ret == 0) { /* No error but failed to find probe point. */
  290. synthesize_perf_probe_point(pp);
  291. die("Probe point '%s' not found. - probe not added.",
  292. pp->probes[0]);
  293. }
  294. /* Error path */
  295. if (session.need_dwarf) {
  296. if (ret == -ENOENT)
  297. pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
  298. die("Could not analyze debuginfo.");
  299. }
  300. pr_debug("An error occurred in debuginfo analysis."
  301. " Try to use symbols.\n");
  302. break;
  303. }
  304. close(fd);
  305. end_dwarf:
  306. #endif /* !NO_DWARF_SUPPORT */
  307. /* Synthesize probes without dwarf */
  308. for (i = 0; i < session.nr_probe; i++) {
  309. pp = &session.probes[i];
  310. if (pp->found) /* This probe is already found. */
  311. continue;
  312. evaluate_probe_point(pp);
  313. ret = synthesize_trace_kprobe_event(pp);
  314. if (ret == -E2BIG)
  315. die("probe point definition becomes too long.");
  316. else if (ret < 0)
  317. die("Failed to synthesize a probe point.");
  318. }
  319. /* Settng up probe points */
  320. add_trace_kprobe_events(session.probes, session.nr_probe,
  321. session.force_add);
  322. return 0;
  323. }