builtin-probe.c 9.5 KB

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