blob: 1d3a99ea5ce1a3433f9216401c12fd8fade19fbd [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"
44#include "util/session.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040045#include "util/parse-options.h"
46#include "util/parse-events.h" /* For debugfs_path */
47#include "util/probe-finder.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050048#include "util/probe-event.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040049
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040050#define MAX_PATH_LEN 256
51#define MAX_PROBES 128
52
53/* Session management structure */
54static struct {
Masami Hiramatsufac13fd2009-12-15 10:31:14 -050055 bool need_dwarf;
56 bool list_events;
Masami Hiramatsud761b082009-12-15 10:32:25 -050057 bool force_add;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050058 bool show_lines;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040059 int nr_probe;
60 struct probe_point probes[MAX_PROBES];
Masami Hiramatsufa282442009-12-08 17:03:23 -050061 struct strlist *dellist;
Masami Hiramatsua1281682009-12-15 10:32:33 -050062 struct perf_session *psession;
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -050063 struct map *kmap;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050064 struct line_range line_range;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040065} session;
66
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050067
Masami Hiramatsu253977b2009-10-27 16:43:10 -040068/* Parse an event definition. Note that any error must die. */
69static void parse_probe_event(const char *str)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040070{
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040071 struct probe_point *pp = &session.probes[session.nr_probe];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040072
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020073 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040074 if (++session.nr_probe == MAX_PROBES)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050075 die("Too many probes (> %d) are specified.", MAX_PROBES);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040076
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050077 /* Parse perf-probe event into probe_point */
Masami Hiramatsufac13fd2009-12-15 10:31:14 -050078 parse_perf_probe_event(str, pp, &session.need_dwarf);
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040079
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020080 pr_debug("%d arguments\n", pp->nr_args);
Masami Hiramatsu46ab4922009-10-27 16:43:02 -040081}
82
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -050083static void parse_probe_event_argv(int argc, const char **argv)
84{
85 int i, len;
86 char *buf;
87
88 /* Bind up rest arguments */
89 len = 0;
90 for (i = 0; i < argc; i++)
91 len += strlen(argv[i]) + 1;
92 buf = zalloc(len + 1);
93 if (!buf)
94 die("Failed to allocate memory for binding arguments.");
95 len = 0;
96 for (i = 0; i < argc; i++)
97 len += sprintf(&buf[len], "%s ", argv[i]);
98 parse_probe_event(buf);
99 free(buf);
100}
101
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400102static int opt_add_probe_event(const struct option *opt __used,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400103 const char *str, int unset __used)
104{
105 if (str)
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400106 parse_probe_event(str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400107 return 0;
108}
109
Masami Hiramatsufa282442009-12-08 17:03:23 -0500110static int opt_del_probe_event(const struct option *opt __used,
111 const char *str, int unset __used)
112{
113 if (str) {
114 if (!session.dellist)
115 session.dellist = strlist__new(true, NULL);
116 strlist__add(session.dellist, str);
117 }
118 return 0;
119}
120
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500121static int opt_show_lines(const struct option *opt __used,
122 const char *str, int unset __used)
123{
124 if (str)
125 parse_line_range_desc(str, &session.line_range);
126 INIT_LIST_HEAD(&session.line_range.line_list);
127 session.show_lines = true;
128 return 0;
129}
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500130/* Currently just checking function name from symbol map */
131static void evaluate_probe_point(struct probe_point *pp)
132{
133 struct symbol *sym;
134 sym = map__find_symbol_by_name(session.kmap, pp->function,
135 session.psession, NULL);
136 if (!sym)
137 die("Kernel symbol \'%s\' not found - probe not added.",
138 pp->function);
139}
140
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400141#ifndef NO_LIBDWARF
Masami Hiramatsua1281682009-12-15 10:32:33 -0500142static int open_vmlinux(void)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400143{
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500144 if (map__load(session.kmap, session.psession, NULL) < 0) {
Masami Hiramatsua1281682009-12-15 10:32:33 -0500145 pr_debug("Failed to load kernel map.\n");
146 return -EINVAL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400147 }
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500148 pr_debug("Try to open %s\n", session.kmap->dso->long_name);
149 return open(session.kmap->dso->long_name, O_RDONLY);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400150}
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400151#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400152
153static const char * const probe_usage[] = {
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400154 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
155 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
Masami Hiramatsufa282442009-12-08 17:03:23 -0500156 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500157 "perf probe --list",
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500158 "perf probe --line 'LINEDESC'",
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 Hiramatsu23e8ec02009-10-07 18:28:30 -0400165#ifndef NO_LIBDWARF
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 Hiramatsu23e8ec02009-10-07 18:28:30 -0400174#ifdef NO_LIBDWARF
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500175 "[EVENT=]FUNC[+OFFS|%return] [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400176#else
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500177 "[EVENT=]FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400178#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400179 "probe point definition, where\n"
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500180 "\t\tGROUP:\tGroup name (optional)\n"
181 "\t\tEVENT:\tEvent name\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400182 "\t\tFUNC:\tFunction name\n"
183 "\t\tOFFS:\tOffset from function entry (in byte)\n"
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400184 "\t\t%return:\tPut the probe at function return\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400185#ifdef NO_LIBDWARF
186 "\t\tARG:\tProbe argument (only \n"
187#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400188 "\t\tSRC:\tSource code path\n"
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400189 "\t\tRLN:\tRelative line number from function entry.\n"
190 "\t\tALN:\tAbsolute line number in file.\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400191 "\t\tARG:\tProbe argument (local variable name or\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400192#endif
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500193 "\t\t\tkprobe-tracer argument format.)\n",
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400194 opt_add_probe_event),
Masami Hiramatsud761b082009-12-15 10:32:25 -0500195 OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
196 " with existing name"),
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500197#ifndef NO_LIBDWARF
198 OPT_CALLBACK('L', "line", NULL,
199 "FUNC[:RLN[+NUM|:RLN2]]|SRC:ALN[+NUM|:ALN2]",
200 "Show source code lines.", opt_show_lines),
201#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400202 OPT_END()
203};
204
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500205/* Initialize symbol maps for vmlinux */
206static void init_vmlinux(void)
207{
208 symbol_conf.sort_by_name = true;
209 if (symbol_conf.vmlinux_name == NULL)
210 symbol_conf.try_vmlinux_path = true;
211 else
212 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
213 if (symbol__init() < 0)
214 die("Failed to init symbol map.");
215 session.psession = perf_session__new(NULL, O_WRONLY, false);
216 if (session.psession == NULL)
217 die("Failed to init perf_session.");
218 session.kmap = session.psession->vmlinux_maps[MAP__FUNCTION];
219 if (!session.kmap)
220 die("Could not find kernel map.\n");
221}
222
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400223int cmd_probe(int argc, const char **argv, const char *prefix __used)
224{
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500225 int i, ret;
Xiao Guangrongbdad0db2009-12-02 16:08:41 +0800226#ifndef NO_LIBDWARF
227 int fd;
228#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400229 struct probe_point *pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400230
231 argc = parse_options(argc, argv, options, probe_usage,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400232 PARSE_OPT_STOP_AT_NON_OPTION);
Masami Hiramatsuce11a602009-12-15 10:31:28 -0500233 if (argc > 0) {
234 if (strcmp(argv[0], "-") == 0) {
235 pr_warning(" Error: '-' is not supported.\n");
236 usage_with_options(probe_usage, options);
237 }
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500238 parse_probe_event_argv(argc, argv);
Masami Hiramatsuce11a602009-12-15 10:31:28 -0500239 }
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400240
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500241 if ((!session.nr_probe && !session.dellist && !session.list_events &&
242 !session.show_lines))
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400243 usage_with_options(probe_usage, options);
244
Masami Hiramatsu96c96612009-12-16 17:24:00 -0500245 if (debugfs_valid_mountpoint(debugfs_path) < 0)
246 die("Failed to find debugfs path.");
247
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500248 if (session.list_events) {
Masami Hiramatsufa282442009-12-08 17:03:23 -0500249 if (session.nr_probe != 0 || session.dellist) {
250 pr_warning(" Error: Don't use --list with"
251 " --add/--del.\n");
252 usage_with_options(probe_usage, options);
253 }
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500254 if (session.show_lines) {
255 pr_warning(" Error: Don't use --list with --line.\n");
256 usage_with_options(probe_usage, options);
257 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500258 show_perf_probe_events();
259 return 0;
260 }
261
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500262#ifndef NO_LIBDWARF
263 if (session.show_lines) {
264 if (session.nr_probe != 0 || session.dellist) {
265 pr_warning(" Error: Don't use --line with"
266 " --add/--del.\n");
267 usage_with_options(probe_usage, options);
268 }
269 init_vmlinux();
270 fd = open_vmlinux();
271 if (fd < 0)
272 die("Could not open debuginfo file.");
273 ret = find_line_range(fd, &session.line_range);
274 if (ret <= 0)
275 die("Source line is not found.\n");
276 close(fd);
277 show_line_range(&session.line_range);
278 return 0;
279 }
280#endif
281
Masami Hiramatsufa282442009-12-08 17:03:23 -0500282 if (session.dellist) {
283 del_trace_kprobe_events(session.dellist);
284 strlist__delete(session.dellist);
285 if (session.nr_probe == 0)
286 return 0;
287 }
288
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500289 /* Add probes */
290 init_vmlinux();
Masami Hiramatsua1281682009-12-15 10:32:33 -0500291
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400292 if (session.need_dwarf)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500293#ifdef NO_LIBDWARF
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500294 die("Debuginfo-analysis is not supported");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500295#else /* !NO_LIBDWARF */
Masami Hiramatsuf41b1e42009-11-30 19:19:27 -0500296 pr_debug("Some probes require debuginfo.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400297
Masami Hiramatsua1281682009-12-15 10:32:33 -0500298 fd = open_vmlinux();
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500299 if (fd < 0) {
300 if (session.need_dwarf)
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500301 die("Could not open debuginfo file.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500302
Masami Hiramatsud3a2dbf2009-12-07 12:00:59 -0500303 pr_debug("Could not open vmlinux/module file."
304 " Try to use symbols.\n");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500305 goto end_dwarf;
306 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400307
308 /* Searching probe points */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500309 for (i = 0; i < session.nr_probe; i++) {
310 pp = &session.probes[i];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400311 if (pp->found)
312 continue;
313
314 lseek(fd, SEEK_SET, 0);
315 ret = find_probepoint(fd, pp);
Masami Hiramatsu411edfe2009-12-15 10:31:35 -0500316 if (ret > 0)
317 continue;
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500318 if (ret == 0) { /* No error but failed to find probe point. */
319 synthesize_perf_probe_point(pp);
320 die("Probe point '%s' not found. - probe not added.",
321 pp->probes[0]);
322 }
Masami Hiramatsu411edfe2009-12-15 10:31:35 -0500323 /* Error path */
324 if (session.need_dwarf) {
325 if (ret == -ENOENT)
326 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
327 die("Could not analyze debuginfo.");
328 }
329 pr_debug("An error occurred in debuginfo analysis."
330 " Try to use symbols.\n");
331 break;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400332 }
333 close(fd);
334
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500335end_dwarf:
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400336#endif /* !NO_LIBDWARF */
337
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500338 /* Synthesize probes without dwarf */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500339 for (i = 0; i < session.nr_probe; i++) {
340 pp = &session.probes[i];
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500341 if (pp->found) /* This probe is already found. */
342 continue;
343
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500344 evaluate_probe_point(pp);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500345 ret = synthesize_trace_kprobe_event(pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500346 if (ret == -E2BIG)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500347 die("probe point definition becomes too long.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500348 else if (ret < 0)
349 die("Failed to synthesize a probe point.");
350 }
351
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400352 /* Settng up probe points */
Masami Hiramatsud761b082009-12-15 10:32:25 -0500353 add_trace_kprobe_events(session.probes, session.nr_probe,
354 session.force_add);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400355 return 0;
356}
357