blob: e3dfd0dcce242b1e6fe3271bdad54e3ff226ef04 [file] [log] [blame]
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001/*
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
34#undef _GNU_SOURCE
35#include "perf.h"
36#include "builtin.h"
37#include "util/util.h"
Masami Hiramatsufa282442009-12-08 17:03:23 -050038#include "util/strlist.h"
Masami Hiramatsu89c69c02009-10-16 20:08:10 -040039#include "util/event.h"
40#include "util/debug.h"
Masami Hiramatsu96c96612009-12-16 17:24:00 -050041#include "util/debugfs.h"
Masami Hiramatsua1281682009-12-15 10:32:33 -050042#include "util/symbol.h"
43#include "util/thread.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040044#include "util/parse-options.h"
45#include "util/parse-events.h" /* For debugfs_path */
46#include "util/probe-finder.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050047#include "util/probe-event.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040048
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040049#define MAX_PATH_LEN 256
50#define MAX_PROBES 128
51
52/* Session management structure */
53static struct {
Masami Hiramatsufac13fd2009-12-15 10:31:14 -050054 bool need_dwarf;
55 bool list_events;
Masami Hiramatsud761b082009-12-15 10:32:25 -050056 bool force_add;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050057 bool show_lines;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040058 int nr_probe;
59 struct probe_point probes[MAX_PROBES];
Masami Hiramatsufa282442009-12-08 17:03:23 -050060 struct strlist *dellist;
Arnaldo Carvalho de Melo8ad94c62010-02-03 16:52:03 -020061 struct map_groups kmap_groups;
62 struct map *kmaps[MAP__NR_TYPES];
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050063 struct line_range line_range;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040064} session;
65
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050066
Masami Hiramatsu253977b2009-10-27 16:43:10 -040067/* Parse an event definition. Note that any error must die. */
68static void parse_probe_event(const char *str)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040069{
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040070 struct probe_point *pp = &session.probes[session.nr_probe];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040071
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020072 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040073 if (++session.nr_probe == MAX_PROBES)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050074 die("Too many probes (> %d) are specified.", MAX_PROBES);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040075
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050076 /* Parse perf-probe event into probe_point */
Masami Hiramatsufac13fd2009-12-15 10:31:14 -050077 parse_perf_probe_event(str, pp, &session.need_dwarf);
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040078
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020079 pr_debug("%d arguments\n", pp->nr_args);
Masami Hiramatsu46ab4922009-10-27 16:43:02 -040080}
81
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -050082static void parse_probe_event_argv(int argc, const char **argv)
83{
84 int i, len;
85 char *buf;
86
87 /* Bind up rest arguments */
88 len = 0;
89 for (i = 0; i < argc; i++)
90 len += strlen(argv[i]) + 1;
91 buf = zalloc(len + 1);
92 if (!buf)
93 die("Failed to allocate memory for binding arguments.");
94 len = 0;
95 for (i = 0; i < argc; i++)
96 len += sprintf(&buf[len], "%s ", argv[i]);
97 parse_probe_event(buf);
98 free(buf);
99}
100
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400101static int opt_add_probe_event(const struct option *opt __used,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400102 const char *str, int unset __used)
103{
104 if (str)
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400105 parse_probe_event(str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400106 return 0;
107}
108
Masami Hiramatsufa282442009-12-08 17:03:23 -0500109static int opt_del_probe_event(const struct option *opt __used,
110 const char *str, int unset __used)
111{
112 if (str) {
113 if (!session.dellist)
114 session.dellist = strlist__new(true, NULL);
115 strlist__add(session.dellist, str);
116 }
117 return 0;
118}
119
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500120/* Currently just checking function name from symbol map */
121static void evaluate_probe_point(struct probe_point *pp)
122{
123 struct symbol *sym;
Arnaldo Carvalho de Melo8ad94c62010-02-03 16:52:03 -0200124 sym = map__find_symbol_by_name(session.kmaps[MAP__FUNCTION],
125 pp->function, NULL);
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500126 if (!sym)
127 die("Kernel symbol \'%s\' not found - probe not added.",
128 pp->function);
129}
130
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500131#ifndef NO_DWARF_SUPPORT
Masami Hiramatsua1281682009-12-15 10:32:33 -0500132static int open_vmlinux(void)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400133{
Arnaldo Carvalho de Melo8ad94c62010-02-03 16:52:03 -0200134 if (map__load(session.kmaps[MAP__FUNCTION], NULL) < 0) {
Masami Hiramatsua1281682009-12-15 10:32:33 -0500135 pr_debug("Failed to load kernel map.\n");
136 return -EINVAL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400137 }
Arnaldo Carvalho de Melo8ad94c62010-02-03 16:52:03 -0200138 pr_debug("Try to open %s\n",
139 session.kmaps[MAP__FUNCTION]->dso->long_name);
140 return open(session.kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400141}
Hitoshi Mitake0eda7382010-01-16 21:31:16 +0900142
143static int opt_show_lines(const struct option *opt __used,
144 const char *str, int unset __used)
145{
146 if (str)
147 parse_line_range_desc(str, &session.line_range);
148 INIT_LIST_HEAD(&session.line_range.line_list);
149 session.show_lines = true;
150 return 0;
151}
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400152#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400153
154static const char * const probe_usage[] = {
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400155 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
156 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
Masami Hiramatsufa282442009-12-08 17:03:23 -0500157 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500158 "perf probe --list",
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500159#ifndef NO_DWARF_SUPPORT
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500160 "perf probe --line 'LINEDESC'",
Masami Hiramatsuf3ab4812010-02-25 08:35:12 -0500161#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400162 NULL
163};
164
165static const struct option options[] = {
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400166 OPT_BOOLEAN('v', "verbose", &verbose,
167 "be more verbose (show parsed arguments, etc)"),
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500168#ifndef NO_DWARF_SUPPORT
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200169 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
Masami Hiramatsua1281682009-12-15 10:32:33 -0500170 "file", "vmlinux pathname"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400171#endif
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500172 OPT_BOOLEAN('l', "list", &session.list_events,
173 "list up current probe events"),
Masami Hiramatsufa282442009-12-08 17:03:23 -0500174 OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
175 opt_del_probe_event),
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400176 OPT_CALLBACK('a', "add", NULL,
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500177#ifdef NO_DWARF_SUPPORT
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500178 "[EVENT=]FUNC[+OFF|%return] [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400179#else
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500180 "[EVENT=]FUNC[+OFF|%return|:RL|;PT][@SRC]|SRC:AL|SRC;PT"
181 " [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400182#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400183 "probe point definition, where\n"
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500184 "\t\tGROUP:\tGroup name (optional)\n"
185 "\t\tEVENT:\tEvent name\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400186 "\t\tFUNC:\tFunction name\n"
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500187 "\t\tOFF:\tOffset from function entry (in byte)\n"
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400188 "\t\t%return:\tPut the probe at function return\n"
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500189#ifdef NO_DWARF_SUPPORT
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400190 "\t\tARG:\tProbe argument (only \n"
191#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400192 "\t\tSRC:\tSource code path\n"
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500193 "\t\tRL:\tRelative line number from function entry.\n"
194 "\t\tAL:\tAbsolute line number in file.\n"
195 "\t\tPT:\tLazy expression of line code.\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400196 "\t\tARG:\tProbe argument (local variable name or\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400197#endif
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500198 "\t\t\tkprobe-tracer argument format.)\n",
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400199 opt_add_probe_event),
Masami Hiramatsud761b082009-12-15 10:32:25 -0500200 OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
201 " with existing name"),
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500202#ifndef NO_DWARF_SUPPORT
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500203 OPT_CALLBACK('L', "line", NULL,
204 "FUNC[:RLN[+NUM|:RLN2]]|SRC:ALN[+NUM|:ALN2]",
205 "Show source code lines.", opt_show_lines),
206#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400207 OPT_END()
208};
209
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500210/* Initialize symbol maps for vmlinux */
211static void init_vmlinux(void)
212{
213 symbol_conf.sort_by_name = true;
214 if (symbol_conf.vmlinux_name == NULL)
215 symbol_conf.try_vmlinux_path = true;
216 else
217 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
218 if (symbol__init() < 0)
219 die("Failed to init symbol map.");
Arnaldo Carvalho de Melo8ad94c62010-02-03 16:52:03 -0200220
221 map_groups__init(&session.kmap_groups);
222 if (map_groups__create_kernel_maps(&session.kmap_groups,
223 session.kmaps) < 0)
224 die("Failed to create kernel maps.");
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500225}
226
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400227int cmd_probe(int argc, const char **argv, const char *prefix __used)
228{
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500229 int i, ret;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500230#ifndef NO_DWARF_SUPPORT
Xiao Guangrongbdad0db2009-12-02 16:08:41 +0800231 int fd;
232#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400233 struct probe_point *pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400234
235 argc = parse_options(argc, argv, options, probe_usage,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400236 PARSE_OPT_STOP_AT_NON_OPTION);
Masami Hiramatsuce11a602009-12-15 10:31:28 -0500237 if (argc > 0) {
238 if (strcmp(argv[0], "-") == 0) {
239 pr_warning(" Error: '-' is not supported.\n");
240 usage_with_options(probe_usage, options);
241 }
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500242 parse_probe_event_argv(argc, argv);
Masami Hiramatsuce11a602009-12-15 10:31:28 -0500243 }
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400244
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500245 if ((!session.nr_probe && !session.dellist && !session.list_events &&
246 !session.show_lines))
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400247 usage_with_options(probe_usage, options);
248
Masami Hiramatsu96c96612009-12-16 17:24:00 -0500249 if (debugfs_valid_mountpoint(debugfs_path) < 0)
250 die("Failed to find debugfs path.");
251
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500252 if (session.list_events) {
Masami Hiramatsufa282442009-12-08 17:03:23 -0500253 if (session.nr_probe != 0 || session.dellist) {
254 pr_warning(" Error: Don't use --list with"
255 " --add/--del.\n");
256 usage_with_options(probe_usage, options);
257 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500258 if (session.show_lines) {
259 pr_warning(" Error: Don't use --list with --line.\n");
260 usage_with_options(probe_usage, options);
261 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500262 show_perf_probe_events();
263 return 0;
264 }
265
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500266#ifndef NO_DWARF_SUPPORT
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500267 if (session.show_lines) {
268 if (session.nr_probe != 0 || session.dellist) {
269 pr_warning(" Error: Don't use --line with"
270 " --add/--del.\n");
271 usage_with_options(probe_usage, options);
272 }
273 init_vmlinux();
274 fd = open_vmlinux();
275 if (fd < 0)
276 die("Could not open debuginfo file.");
277 ret = find_line_range(fd, &session.line_range);
278 if (ret <= 0)
279 die("Source line is not found.\n");
280 close(fd);
281 show_line_range(&session.line_range);
282 return 0;
283 }
284#endif
285
Masami Hiramatsufa282442009-12-08 17:03:23 -0500286 if (session.dellist) {
287 del_trace_kprobe_events(session.dellist);
288 strlist__delete(session.dellist);
289 if (session.nr_probe == 0)
290 return 0;
291 }
292
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500293 /* Add probes */
294 init_vmlinux();
Masami Hiramatsua1281682009-12-15 10:32:33 -0500295
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400296 if (session.need_dwarf)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500297#ifdef NO_DWARF_SUPPORT
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500298 die("Debuginfo-analysis is not supported");
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500299#else /* !NO_DWARF_SUPPORT */
Masami Hiramatsuf41b1e42009-11-30 19:19:27 -0500300 pr_debug("Some probes require debuginfo.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400301
Masami Hiramatsua1281682009-12-15 10:32:33 -0500302 fd = open_vmlinux();
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500303 if (fd < 0) {
304 if (session.need_dwarf)
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500305 die("Could not open debuginfo file.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500306
Masami Hiramatsud3a2dbf2009-12-07 12:00:59 -0500307 pr_debug("Could not open vmlinux/module file."
308 " Try to use symbols.\n");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500309 goto end_dwarf;
310 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400311
312 /* Searching probe points */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500313 for (i = 0; i < session.nr_probe; i++) {
314 pp = &session.probes[i];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400315 if (pp->found)
316 continue;
317
318 lseek(fd, SEEK_SET, 0);
Masami Hiramatsu81cb8aa2010-02-25 08:35:34 -0500319 ret = find_probe_point(fd, pp);
Masami Hiramatsu411edfe2009-12-15 10:31:35 -0500320 if (ret > 0)
321 continue;
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500322 if (ret == 0) { /* No error but failed to find probe point. */
323 synthesize_perf_probe_point(pp);
324 die("Probe point '%s' not found. - probe not added.",
325 pp->probes[0]);
326 }
Masami Hiramatsu411edfe2009-12-15 10:31:35 -0500327 /* Error path */
328 if (session.need_dwarf) {
329 if (ret == -ENOENT)
330 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
331 die("Could not analyze debuginfo.");
332 }
333 pr_debug("An error occurred in debuginfo analysis."
334 " Try to use symbols.\n");
335 break;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400336 }
337 close(fd);
338
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500339end_dwarf:
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500340#endif /* !NO_DWARF_SUPPORT */
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400341
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500342 /* Synthesize probes without dwarf */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500343 for (i = 0; i < session.nr_probe; i++) {
344 pp = &session.probes[i];
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500345 if (pp->found) /* This probe is already found. */
346 continue;
347
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500348 evaluate_probe_point(pp);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500349 ret = synthesize_trace_kprobe_event(pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500350 if (ret == -E2BIG)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500351 die("probe point definition becomes too long.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500352 else if (ret < 0)
353 die("Failed to synthesize a probe point.");
354 }
355
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400356 /* Settng up probe points */
Masami Hiramatsud761b082009-12-15 10:32:25 -0500357 add_trace_kprobe_events(session.probes, session.nr_probe,
358 session.force_add);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400359 return 0;
360}
361