blob: b5d15cf254710e62c606e38731775c22077fc942 [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 Hiramatsu253977b2009-10-27 16:43:10 -040082static int opt_add_probe_event(const struct option *opt __used,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -040083 const char *str, int unset __used)
84{
85 if (str)
Masami Hiramatsu253977b2009-10-27 16:43:10 -040086 parse_probe_event(str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040087 return 0;
88}
89
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040090#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040091static int open_default_vmlinux(void)
92{
93 struct utsname uts;
94 char fname[MAX_PATH_LEN];
95 int fd, ret, i;
96
97 ret = uname(&uts);
98 if (ret) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020099 pr_debug("uname() failed.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400100 return -errno;
101 }
102 session.release = uts.release;
103 for (i = 0; i < NR_SEARCH_PATH; i++) {
104 ret = snprintf(fname, MAX_PATH_LEN,
105 default_search_path[i], session.release);
106 if (ret >= MAX_PATH_LEN || ret < 0) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200107 pr_debug("Filename(%d,%s) is too long.\n", i,
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400108 uts.release);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400109 errno = E2BIG;
110 return -E2BIG;
111 }
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200112 pr_debug("try to open %s\n", fname);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400113 fd = open(fname, O_RDONLY);
114 if (fd >= 0)
115 break;
116 }
117 return fd;
118}
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400119#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400120
121static const char * const probe_usage[] = {
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400122 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
123 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500124 "perf probe --list",
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400125 NULL
126};
127
128static const struct option options[] = {
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400129 OPT_BOOLEAN('v', "verbose", &verbose,
130 "be more verbose (show parsed arguments, etc)"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400131#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400132 OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
133 "vmlinux/module pathname"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400134#endif
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500135 OPT_BOOLEAN('l', "list", &listing, "list up current probes"),
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400136 OPT_CALLBACK('a', "add", NULL,
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400137#ifdef NO_LIBDWARF
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400138 "FUNC[+OFFS|%return] [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400139#else
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400140 "FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400141#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400142 "probe point definition, where\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400143 "\t\tGRP:\tGroup name (optional)\n"
144 "\t\tNAME:\tEvent name\n"
145 "\t\tFUNC:\tFunction name\n"
146 "\t\tOFFS:\tOffset from function entry (in byte)\n"
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400147 "\t\t%return:\tPut the probe at function return\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400148#ifdef NO_LIBDWARF
149 "\t\tARG:\tProbe argument (only \n"
150#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400151 "\t\tSRC:\tSource code path\n"
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400152 "\t\tRLN:\tRelative line number from function entry.\n"
153 "\t\tALN:\tAbsolute line number in file.\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400154 "\t\tARG:\tProbe argument (local variable name or\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400155#endif
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500156 "\t\t\tkprobe-tracer argument format.)\n",
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400157 opt_add_probe_event),
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400158 OPT_END()
159};
160
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400161int cmd_probe(int argc, const char **argv, const char *prefix __used)
162{
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400163 int i, j, fd, ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400164 struct probe_point *pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400165
166 argc = parse_options(argc, argv, options, probe_usage,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400167 PARSE_OPT_STOP_AT_NON_OPTION);
168 for (i = 0; i < argc; i++)
169 parse_probe_event(argv[i]);
170
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500171 if ((session.nr_probe == 0 && !listing) ||
172 (session.nr_probe != 0 && listing))
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400173 usage_with_options(probe_usage, options);
174
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500175 if (listing) {
176 show_perf_probe_events();
177 return 0;
178 }
179
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400180 if (session.need_dwarf)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500181#ifdef NO_LIBDWARF
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500182 die("Debuginfo-analysis is not supported");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500183#else /* !NO_LIBDWARF */
Masami Hiramatsuf41b1e42009-11-30 19:19:27 -0500184 pr_debug("Some probes require debuginfo.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400185
186 if (session.vmlinux)
187 fd = open(session.vmlinux, O_RDONLY);
188 else
189 fd = open_default_vmlinux();
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500190 if (fd < 0) {
191 if (session.need_dwarf)
192 die("Could not open vmlinux/module file.");
193
194 pr_warning("Could not open vmlinux/module file."
195 " Try to use symbols.\n");
196 goto end_dwarf;
197 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400198
199 /* Searching probe points */
200 for (j = 0; j < session.nr_probe; j++) {
201 pp = &session.probes[j];
202 if (pp->found)
203 continue;
204
205 lseek(fd, SEEK_SET, 0);
206 ret = find_probepoint(fd, pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500207 if (ret < 0) {
208 if (session.need_dwarf)
209 die("Could not analyze debuginfo.");
210
211 pr_warning("An error occurred in debuginfo analysis. Try to use symbols.\n");
212 break;
213 }
214 if (ret == 0) /* No error but failed to find probe point. */
215 die("No probe point found.");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400216 }
217 close(fd);
218
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500219end_dwarf:
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400220#endif /* !NO_LIBDWARF */
221
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500222 /* Synthesize probes without dwarf */
223 for (j = 0; j < session.nr_probe; j++) {
224 pp = &session.probes[j];
225 if (pp->found) /* This probe is already found. */
226 continue;
227
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500228 ret = synthesize_trace_kprobe_event(pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500229 if (ret == -E2BIG)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500230 die("probe point definition becomes too long.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500231 else if (ret < 0)
232 die("Failed to synthesize a probe point.");
233 }
234
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400235 /* Settng up probe points */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500236 add_trace_kprobe_events(session.probes, session.nr_probe);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400237 return 0;
238}
239