blob: 1347fdf5337ece65de8d23e36883a3e3a68472a6 [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 Hiramatsu89c69c02009-10-16 20:08:10 -040038#include "util/event.h"
39#include "util/debug.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040040#include "util/parse-options.h"
41#include "util/parse-events.h" /* For debugfs_path */
42#include "util/probe-finder.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050043#include "util/probe-event.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040044
45/* Default vmlinux search paths */
46#define NR_SEARCH_PATH 3
47const char *default_search_path[NR_SEARCH_PATH] = {
48"/lib/modules/%s/build/vmlinux", /* Custom build kernel */
49"/usr/lib/debug/lib/modules/%s/vmlinux", /* Red Hat debuginfo */
50"/boot/vmlinux-debug-%s", /* Ubuntu */
51};
52
53#define MAX_PATH_LEN 256
54#define MAX_PROBES 128
55
56/* Session management structure */
57static struct {
58 char *vmlinux;
59 char *release;
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040060 int need_dwarf;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040061 int nr_probe;
62 struct probe_point probes[MAX_PROBES];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040063} session;
64
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050065static bool listing;
66
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 */
77 session.need_dwarf = parse_perf_probe_event(str, pp);
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 Hiramatsu23e8ec02009-10-07 18:28:30 -0400109#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400110static int open_default_vmlinux(void)
111{
112 struct utsname uts;
113 char fname[MAX_PATH_LEN];
114 int fd, ret, i;
115
116 ret = uname(&uts);
117 if (ret) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200118 pr_debug("uname() failed.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400119 return -errno;
120 }
121 session.release = uts.release;
122 for (i = 0; i < NR_SEARCH_PATH; i++) {
123 ret = snprintf(fname, MAX_PATH_LEN,
124 default_search_path[i], session.release);
125 if (ret >= MAX_PATH_LEN || ret < 0) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200126 pr_debug("Filename(%d,%s) is too long.\n", i,
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400127 uts.release);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400128 errno = E2BIG;
129 return -E2BIG;
130 }
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200131 pr_debug("try to open %s\n", fname);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400132 fd = open(fname, O_RDONLY);
133 if (fd >= 0)
134 break;
135 }
136 return fd;
137}
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400138#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400139
140static const char * const probe_usage[] = {
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400141 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
142 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500143 "perf probe --list",
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400144 NULL
145};
146
147static const struct option options[] = {
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400148 OPT_BOOLEAN('v', "verbose", &verbose,
149 "be more verbose (show parsed arguments, etc)"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400150#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400151 OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
152 "vmlinux/module pathname"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400153#endif
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500154 OPT_BOOLEAN('l', "list", &listing, "list up current probes"),
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400155 OPT_CALLBACK('a', "add", NULL,
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400156#ifdef NO_LIBDWARF
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400157 "FUNC[+OFFS|%return] [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400158#else
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400159 "FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400160#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400161 "probe point definition, where\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400162 "\t\tGRP:\tGroup name (optional)\n"
163 "\t\tNAME:\tEvent name\n"
164 "\t\tFUNC:\tFunction name\n"
165 "\t\tOFFS:\tOffset from function entry (in byte)\n"
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400166 "\t\t%return:\tPut the probe at function return\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400167#ifdef NO_LIBDWARF
168 "\t\tARG:\tProbe argument (only \n"
169#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400170 "\t\tSRC:\tSource code path\n"
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400171 "\t\tRLN:\tRelative line number from function entry.\n"
172 "\t\tALN:\tAbsolute line number in file.\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400173 "\t\tARG:\tProbe argument (local variable name or\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400174#endif
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500175 "\t\t\tkprobe-tracer argument format.)\n",
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400176 opt_add_probe_event),
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400177 OPT_END()
178};
179
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400180int cmd_probe(int argc, const char **argv, const char *prefix __used)
181{
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500182 int i, ret;
Xiao Guangrongbdad0db2009-12-02 16:08:41 +0800183#ifndef NO_LIBDWARF
184 int fd;
185#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400186 struct probe_point *pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400187
188 argc = parse_options(argc, argv, options, probe_usage,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400189 PARSE_OPT_STOP_AT_NON_OPTION);
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500190 if (argc > 0)
191 parse_probe_event_argv(argc, argv);
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400192
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500193 if ((session.nr_probe == 0 && !listing) ||
194 (session.nr_probe != 0 && listing))
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400195 usage_with_options(probe_usage, options);
196
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500197 if (listing) {
198 show_perf_probe_events();
199 return 0;
200 }
201
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400202 if (session.need_dwarf)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500203#ifdef NO_LIBDWARF
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500204 die("Debuginfo-analysis is not supported");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500205#else /* !NO_LIBDWARF */
Masami Hiramatsuf41b1e42009-11-30 19:19:27 -0500206 pr_debug("Some probes require debuginfo.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400207
208 if (session.vmlinux)
209 fd = open(session.vmlinux, O_RDONLY);
210 else
211 fd = open_default_vmlinux();
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500212 if (fd < 0) {
213 if (session.need_dwarf)
214 die("Could not open vmlinux/module file.");
215
Masami Hiramatsud3a2dbf2009-12-07 12:00:59 -0500216 pr_debug("Could not open vmlinux/module file."
217 " Try to use symbols.\n");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500218 goto end_dwarf;
219 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400220
221 /* Searching probe points */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500222 for (i = 0; i < session.nr_probe; i++) {
223 pp = &session.probes[i];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400224 if (pp->found)
225 continue;
226
227 lseek(fd, SEEK_SET, 0);
228 ret = find_probepoint(fd, pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500229 if (ret < 0) {
230 if (session.need_dwarf)
231 die("Could not analyze debuginfo.");
232
233 pr_warning("An error occurred in debuginfo analysis. Try to use symbols.\n");
234 break;
235 }
236 if (ret == 0) /* No error but failed to find probe point. */
237 die("No probe point found.");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400238 }
239 close(fd);
240
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500241end_dwarf:
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400242#endif /* !NO_LIBDWARF */
243
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500244 /* Synthesize probes without dwarf */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500245 for (i = 0; i < session.nr_probe; i++) {
246 pp = &session.probes[i];
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500247 if (pp->found) /* This probe is already found. */
248 continue;
249
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500250 ret = synthesize_trace_kprobe_event(pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500251 if (ret == -E2BIG)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500252 die("probe point definition becomes too long.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500253 else if (ret < 0)
254 die("Failed to synthesize a probe point.");
255 }
256
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400257 /* Settng up probe points */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500258 add_trace_kprobe_events(session.probes, session.nr_probe);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400259 return 0;
260}
261