blob: b6afe7b344dd62e0d2dc4bc31789126da0209947 [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
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040050
51/* Session management structure */
52static struct {
Masami Hiramatsufac13fd2009-12-15 10:31:14 -050053 bool need_dwarf;
54 bool list_events;
Masami Hiramatsud761b082009-12-15 10:32:25 -050055 bool force_add;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050056 bool show_lines;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040057 int nr_probe;
58 struct probe_point probes[MAX_PROBES];
Masami Hiramatsufa282442009-12-08 17:03:23 -050059 struct strlist *dellist;
Arnaldo Carvalho de Melo8ad94c62010-02-03 16:52:03 -020060 struct map_groups kmap_groups;
61 struct map *kmaps[MAP__NR_TYPES];
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050062 struct line_range line_range;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040063} session;
64
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050065
Masami Hiramatsu253977b2009-10-27 16:43:10 -040066/* Parse an event definition. Note that any error must die. */
67static void parse_probe_event(const char *str)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040068{
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040069 struct probe_point *pp = &session.probes[session.nr_probe];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040070
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020071 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040072 if (++session.nr_probe == MAX_PROBES)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050073 die("Too many probes (> %d) are specified.", MAX_PROBES);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040074
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050075 /* Parse perf-probe event into probe_point */
Masami Hiramatsufac13fd2009-12-15 10:31:14 -050076 parse_perf_probe_event(str, pp, &session.need_dwarf);
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040077
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020078 pr_debug("%d arguments\n", pp->nr_args);
Masami Hiramatsu46ab4922009-10-27 16:43:02 -040079}
80
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -050081static void parse_probe_event_argv(int argc, const char **argv)
82{
83 int i, len;
84 char *buf;
85
86 /* Bind up rest arguments */
87 len = 0;
88 for (i = 0; i < argc; i++)
89 len += strlen(argv[i]) + 1;
Masami Hiramatsu31facc52010-03-16 18:05:30 -040090 buf = xzalloc(len + 1);
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -050091 len = 0;
92 for (i = 0; i < argc; i++)
93 len += sprintf(&buf[len], "%s ", argv[i]);
94 parse_probe_event(buf);
95 free(buf);
96}
97
Masami Hiramatsu253977b2009-10-27 16:43:10 -040098static int opt_add_probe_event(const struct option *opt __used,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -040099 const char *str, int unset __used)
100{
101 if (str)
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400102 parse_probe_event(str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400103 return 0;
104}
105
Masami Hiramatsufa282442009-12-08 17:03:23 -0500106static int opt_del_probe_event(const struct option *opt __used,
107 const char *str, int unset __used)
108{
109 if (str) {
110 if (!session.dellist)
111 session.dellist = strlist__new(true, NULL);
112 strlist__add(session.dellist, str);
113 }
114 return 0;
115}
116
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500117/* Currently just checking function name from symbol map */
118static void evaluate_probe_point(struct probe_point *pp)
119{
120 struct symbol *sym;
Arnaldo Carvalho de Melo8ad94c62010-02-03 16:52:03 -0200121 sym = map__find_symbol_by_name(session.kmaps[MAP__FUNCTION],
122 pp->function, NULL);
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500123 if (!sym)
124 die("Kernel symbol \'%s\' not found - probe not added.",
125 pp->function);
126}
127
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500128#ifndef NO_DWARF_SUPPORT
Masami Hiramatsua1281682009-12-15 10:32:33 -0500129static int open_vmlinux(void)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400130{
Arnaldo Carvalho de Melo8ad94c62010-02-03 16:52:03 -0200131 if (map__load(session.kmaps[MAP__FUNCTION], NULL) < 0) {
Masami Hiramatsua1281682009-12-15 10:32:33 -0500132 pr_debug("Failed to load kernel map.\n");
133 return -EINVAL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400134 }
Arnaldo Carvalho de Melo8ad94c62010-02-03 16:52:03 -0200135 pr_debug("Try to open %s\n",
136 session.kmaps[MAP__FUNCTION]->dso->long_name);
137 return open(session.kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400138}
Hitoshi Mitake0eda7382010-01-16 21:31:16 +0900139
140static int opt_show_lines(const struct option *opt __used,
141 const char *str, int unset __used)
142{
143 if (str)
144 parse_line_range_desc(str, &session.line_range);
145 INIT_LIST_HEAD(&session.line_range.line_list);
146 session.show_lines = true;
147 return 0;
148}
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400149#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400150
151static const char * const probe_usage[] = {
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400152 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
153 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
Masami Hiramatsufa282442009-12-08 17:03:23 -0500154 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500155 "perf probe --list",
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500156#ifndef NO_DWARF_SUPPORT
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500157 "perf probe --line 'LINEDESC'",
Masami Hiramatsuf3ab4812010-02-25 08:35:12 -0500158#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400159 NULL
160};
161
162static const struct option options[] = {
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400163 OPT_BOOLEAN('v', "verbose", &verbose,
164 "be more verbose (show parsed arguments, etc)"),
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500165#ifndef NO_DWARF_SUPPORT
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200166 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
Masami Hiramatsua1281682009-12-15 10:32:33 -0500167 "file", "vmlinux pathname"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400168#endif
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500169 OPT_BOOLEAN('l', "list", &session.list_events,
170 "list up current probe events"),
Masami Hiramatsufa282442009-12-08 17:03:23 -0500171 OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
172 opt_del_probe_event),
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400173 OPT_CALLBACK('a', "add", NULL,
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500174#ifdef NO_DWARF_SUPPORT
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500175 "[EVENT=]FUNC[+OFF|%return] [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400176#else
Masami Hiramatsu32cb0dd2010-03-03 22:38:43 -0500177 "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500178 " [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400179#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400180 "probe point definition, where\n"
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500181 "\t\tGROUP:\tGroup name (optional)\n"
182 "\t\tEVENT:\tEvent name\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400183 "\t\tFUNC:\tFunction name\n"
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500184 "\t\tOFF:\tOffset from function entry (in byte)\n"
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400185 "\t\t%return:\tPut the probe at function return\n"
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500186#ifdef NO_DWARF_SUPPORT
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400187 "\t\tARG:\tProbe argument (only \n"
188#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400189 "\t\tSRC:\tSource code path\n"
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500190 "\t\tRL:\tRelative line number from function entry.\n"
191 "\t\tAL:\tAbsolute line number in file.\n"
192 "\t\tPT:\tLazy expression of line code.\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400193 "\t\tARG:\tProbe argument (local variable name or\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400194#endif
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500195 "\t\t\tkprobe-tracer argument format.)\n",
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400196 opt_add_probe_event),
Masami Hiramatsud761b082009-12-15 10:32:25 -0500197 OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
198 " with existing name"),
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500199#ifndef NO_DWARF_SUPPORT
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500200 OPT_CALLBACK('L', "line", NULL,
201 "FUNC[:RLN[+NUM|:RLN2]]|SRC:ALN[+NUM|:ALN2]",
202 "Show source code lines.", opt_show_lines),
203#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400204 OPT_END()
205};
206
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500207/* Initialize symbol maps for vmlinux */
208static void init_vmlinux(void)
209{
210 symbol_conf.sort_by_name = true;
211 if (symbol_conf.vmlinux_name == NULL)
212 symbol_conf.try_vmlinux_path = true;
213 else
214 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
215 if (symbol__init() < 0)
216 die("Failed to init symbol map.");
Arnaldo Carvalho de Melo8ad94c62010-02-03 16:52:03 -0200217
218 map_groups__init(&session.kmap_groups);
219 if (map_groups__create_kernel_maps(&session.kmap_groups,
220 session.kmaps) < 0)
221 die("Failed to create kernel maps.");
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500222}
223
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400224int cmd_probe(int argc, const char **argv, const char *prefix __used)
225{
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500226 int i, ret;
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500227#ifndef NO_DWARF_SUPPORT
Xiao Guangrongbdad0db2009-12-02 16:08:41 +0800228 int fd;
229#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400230 struct probe_point *pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400231
232 argc = parse_options(argc, argv, options, probe_usage,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400233 PARSE_OPT_STOP_AT_NON_OPTION);
Masami Hiramatsuce11a602009-12-15 10:31:28 -0500234 if (argc > 0) {
235 if (strcmp(argv[0], "-") == 0) {
236 pr_warning(" Error: '-' is not supported.\n");
237 usage_with_options(probe_usage, options);
238 }
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500239 parse_probe_event_argv(argc, argv);
Masami Hiramatsuce11a602009-12-15 10:31:28 -0500240 }
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400241
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500242 if ((!session.nr_probe && !session.dellist && !session.list_events &&
243 !session.show_lines))
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400244 usage_with_options(probe_usage, options);
245
Masami Hiramatsu96c96612009-12-16 17:24:00 -0500246 if (debugfs_valid_mountpoint(debugfs_path) < 0)
247 die("Failed to find debugfs path.");
248
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500249 if (session.list_events) {
Masami Hiramatsufa282442009-12-08 17:03:23 -0500250 if (session.nr_probe != 0 || session.dellist) {
251 pr_warning(" Error: Don't use --list with"
252 " --add/--del.\n");
253 usage_with_options(probe_usage, options);
254 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500255 if (session.show_lines) {
256 pr_warning(" Error: Don't use --list with --line.\n");
257 usage_with_options(probe_usage, options);
258 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500259 show_perf_probe_events();
260 return 0;
261 }
262
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500263#ifndef NO_DWARF_SUPPORT
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500264 if (session.show_lines) {
265 if (session.nr_probe != 0 || session.dellist) {
266 pr_warning(" Error: Don't use --line with"
267 " --add/--del.\n");
268 usage_with_options(probe_usage, options);
269 }
270 init_vmlinux();
271 fd = open_vmlinux();
272 if (fd < 0)
273 die("Could not open debuginfo file.");
274 ret = find_line_range(fd, &session.line_range);
275 if (ret <= 0)
276 die("Source line is not found.\n");
277 close(fd);
278 show_line_range(&session.line_range);
279 return 0;
280 }
281#endif
282
Masami Hiramatsufa282442009-12-08 17:03:23 -0500283 if (session.dellist) {
284 del_trace_kprobe_events(session.dellist);
285 strlist__delete(session.dellist);
286 if (session.nr_probe == 0)
287 return 0;
288 }
289
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500290 /* Add probes */
291 init_vmlinux();
Masami Hiramatsua1281682009-12-15 10:32:33 -0500292
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400293 if (session.need_dwarf)
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500294#ifdef NO_DWARF_SUPPORT
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500295 die("Debuginfo-analysis is not supported");
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500296#else /* !NO_DWARF_SUPPORT */
Masami Hiramatsuf41b1e42009-11-30 19:19:27 -0500297 pr_debug("Some probes require debuginfo.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400298
Masami Hiramatsua1281682009-12-15 10:32:33 -0500299 fd = open_vmlinux();
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500300 if (fd < 0) {
301 if (session.need_dwarf)
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500302 die("Could not open debuginfo file.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500303
Masami Hiramatsud3a2dbf2009-12-07 12:00:59 -0500304 pr_debug("Could not open vmlinux/module file."
305 " Try to use symbols.\n");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500306 goto end_dwarf;
307 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400308
309 /* Searching probe points */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500310 for (i = 0; i < session.nr_probe; i++) {
311 pp = &session.probes[i];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400312 if (pp->found)
313 continue;
314
315 lseek(fd, SEEK_SET, 0);
Masami Hiramatsu81cb8aa2010-02-25 08:35:34 -0500316 ret = find_probe_point(fd, pp);
Masami Hiramatsu411edfe2009-12-15 10:31:35 -0500317 if (ret > 0)
318 continue;
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500319 if (ret == 0) { /* No error but failed to find probe point. */
320 synthesize_perf_probe_point(pp);
321 die("Probe point '%s' not found. - probe not added.",
322 pp->probes[0]);
323 }
Masami Hiramatsu411edfe2009-12-15 10:31:35 -0500324 /* Error path */
325 if (session.need_dwarf) {
326 if (ret == -ENOENT)
327 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
328 die("Could not analyze debuginfo.");
329 }
330 pr_debug("An error occurred in debuginfo analysis."
331 " Try to use symbols.\n");
332 break;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400333 }
334 close(fd);
335
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500336end_dwarf:
Masami Hiramatsu804b3602010-02-25 08:35:42 -0500337#endif /* !NO_DWARF_SUPPORT */
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400338
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500339 /* Synthesize probes without dwarf */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500340 for (i = 0; i < session.nr_probe; i++) {
341 pp = &session.probes[i];
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500342 if (pp->found) /* This probe is already found. */
343 continue;
344
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500345 evaluate_probe_point(pp);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500346 ret = synthesize_trace_kprobe_event(pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500347 if (ret == -E2BIG)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500348 die("probe point definition becomes too long.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500349 else if (ret < 0)
350 die("Failed to synthesize a probe point.");
351 }
352
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400353 /* Settng up probe points */
Masami Hiramatsud761b082009-12-15 10:32:25 -0500354 add_trace_kprobe_events(session.probes, session.nr_probe,
355 session.force_add);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400356 return 0;
357}
358