blob: 5a47c1e11f77738b6f770f8e3b67f7a085a0362a [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 Hiramatsu4ea42b12009-10-08 17:17:38 -040041#include "util/parse-options.h"
42#include "util/parse-events.h" /* For debugfs_path */
43#include "util/probe-finder.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050044#include "util/probe-event.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040045
46/* Default vmlinux search paths */
Masami Hiramatsuf984f032009-12-08 17:03:09 -050047#define NR_SEARCH_PATH 4
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040048const char *default_search_path[NR_SEARCH_PATH] = {
49"/lib/modules/%s/build/vmlinux", /* Custom build kernel */
50"/usr/lib/debug/lib/modules/%s/vmlinux", /* Red Hat debuginfo */
51"/boot/vmlinux-debug-%s", /* Ubuntu */
Masami Hiramatsuf984f032009-12-08 17:03:09 -050052"./vmlinux", /* CWD */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040053};
54
55#define MAX_PATH_LEN 256
56#define MAX_PROBES 128
57
58/* Session management structure */
59static struct {
60 char *vmlinux;
61 char *release;
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040062 int need_dwarf;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040063 int nr_probe;
64 struct probe_point probes[MAX_PROBES];
Masami Hiramatsufa282442009-12-08 17:03:23 -050065 struct strlist *dellist;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040066} session;
67
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050068static bool listing;
69
Masami Hiramatsu253977b2009-10-27 16:43:10 -040070/* Parse an event definition. Note that any error must die. */
71static void parse_probe_event(const char *str)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040072{
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040073 struct probe_point *pp = &session.probes[session.nr_probe];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040074
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020075 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040076 if (++session.nr_probe == MAX_PROBES)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050077 die("Too many probes (> %d) are specified.", MAX_PROBES);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040078
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050079 /* Parse perf-probe event into probe_point */
80 session.need_dwarf = parse_perf_probe_event(str, pp);
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040081
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020082 pr_debug("%d arguments\n", pp->nr_args);
Masami Hiramatsu46ab4922009-10-27 16:43:02 -040083}
84
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -050085static void parse_probe_event_argv(int argc, const char **argv)
86{
87 int i, len;
88 char *buf;
89
90 /* Bind up rest arguments */
91 len = 0;
92 for (i = 0; i < argc; i++)
93 len += strlen(argv[i]) + 1;
94 buf = zalloc(len + 1);
95 if (!buf)
96 die("Failed to allocate memory for binding arguments.");
97 len = 0;
98 for (i = 0; i < argc; i++)
99 len += sprintf(&buf[len], "%s ", argv[i]);
100 parse_probe_event(buf);
101 free(buf);
102}
103
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400104static int opt_add_probe_event(const struct option *opt __used,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400105 const char *str, int unset __used)
106{
107 if (str)
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400108 parse_probe_event(str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400109 return 0;
110}
111
Masami Hiramatsufa282442009-12-08 17:03:23 -0500112static int opt_del_probe_event(const struct option *opt __used,
113 const char *str, int unset __used)
114{
115 if (str) {
116 if (!session.dellist)
117 session.dellist = strlist__new(true, NULL);
118 strlist__add(session.dellist, str);
119 }
120 return 0;
121}
122
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400123#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400124static int open_default_vmlinux(void)
125{
126 struct utsname uts;
127 char fname[MAX_PATH_LEN];
128 int fd, ret, i;
129
130 ret = uname(&uts);
131 if (ret) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200132 pr_debug("uname() failed.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400133 return -errno;
134 }
135 session.release = uts.release;
136 for (i = 0; i < NR_SEARCH_PATH; i++) {
137 ret = snprintf(fname, MAX_PATH_LEN,
138 default_search_path[i], session.release);
139 if (ret >= MAX_PATH_LEN || ret < 0) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200140 pr_debug("Filename(%d,%s) is too long.\n", i,
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400141 uts.release);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400142 errno = E2BIG;
143 return -E2BIG;
144 }
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200145 pr_debug("try to open %s\n", fname);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400146 fd = open(fname, O_RDONLY);
147 if (fd >= 0)
148 break;
149 }
150 return fd;
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 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
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400166 OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
167 "vmlinux/module pathname"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400168#endif
Masami Hiramatsufa282442009-12-08 17:03:23 -0500169 OPT_BOOLEAN('l', "list", &listing, "list up current probe events"),
170 OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
171 opt_del_probe_event),
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400172 OPT_CALLBACK('a', "add", NULL,
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400173#ifdef NO_LIBDWARF
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400174 "FUNC[+OFFS|%return] [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400175#else
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400176 "FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400177#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400178 "probe point definition, where\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400179 "\t\tGRP:\tGroup name (optional)\n"
180 "\t\tNAME:\tEvent name\n"
181 "\t\tFUNC:\tFunction name\n"
182 "\t\tOFFS:\tOffset from function entry (in byte)\n"
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400183 "\t\t%return:\tPut the probe at function return\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400184#ifdef NO_LIBDWARF
185 "\t\tARG:\tProbe argument (only \n"
186#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400187 "\t\tSRC:\tSource code path\n"
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400188 "\t\tRLN:\tRelative line number from function entry.\n"
189 "\t\tALN:\tAbsolute line number in file.\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400190 "\t\tARG:\tProbe argument (local variable name or\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400191#endif
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500192 "\t\t\tkprobe-tracer argument format.)\n",
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400193 opt_add_probe_event),
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400194 OPT_END()
195};
196
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400197int cmd_probe(int argc, const char **argv, const char *prefix __used)
198{
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500199 int i, ret;
Xiao Guangrongbdad0db2009-12-02 16:08:41 +0800200#ifndef NO_LIBDWARF
201 int fd;
202#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400203 struct probe_point *pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400204
205 argc = parse_options(argc, argv, options, probe_usage,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400206 PARSE_OPT_STOP_AT_NON_OPTION);
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500207 if (argc > 0)
208 parse_probe_event_argv(argc, argv);
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400209
Masami Hiramatsufa282442009-12-08 17:03:23 -0500210 if ((session.nr_probe == 0 && !session.dellist && !listing))
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400211 usage_with_options(probe_usage, options);
212
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500213 if (listing) {
Masami Hiramatsufa282442009-12-08 17:03:23 -0500214 if (session.nr_probe != 0 || session.dellist) {
215 pr_warning(" Error: Don't use --list with"
216 " --add/--del.\n");
217 usage_with_options(probe_usage, options);
218 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500219 show_perf_probe_events();
220 return 0;
221 }
222
Masami Hiramatsufa282442009-12-08 17:03:23 -0500223 if (session.dellist) {
224 del_trace_kprobe_events(session.dellist);
225 strlist__delete(session.dellist);
226 if (session.nr_probe == 0)
227 return 0;
228 }
229
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400230 if (session.need_dwarf)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500231#ifdef NO_LIBDWARF
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500232 die("Debuginfo-analysis is not supported");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500233#else /* !NO_LIBDWARF */
Masami Hiramatsuf41b1e42009-11-30 19:19:27 -0500234 pr_debug("Some probes require debuginfo.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400235
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500236 if (session.vmlinux) {
237 pr_debug("Try to open %s.", session.vmlinux);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400238 fd = open(session.vmlinux, O_RDONLY);
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500239 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400240 fd = open_default_vmlinux();
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500241 if (fd < 0) {
242 if (session.need_dwarf)
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500243 die("Could not open debuginfo file.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500244
Masami Hiramatsud3a2dbf2009-12-07 12:00:59 -0500245 pr_debug("Could not open vmlinux/module file."
246 " Try to use symbols.\n");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500247 goto end_dwarf;
248 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400249
250 /* Searching probe points */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500251 for (i = 0; i < session.nr_probe; i++) {
252 pp = &session.probes[i];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400253 if (pp->found)
254 continue;
255
256 lseek(fd, SEEK_SET, 0);
257 ret = find_probepoint(fd, pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500258 if (ret < 0) {
259 if (session.need_dwarf)
260 die("Could not analyze debuginfo.");
261
262 pr_warning("An error occurred in debuginfo analysis. Try to use symbols.\n");
263 break;
264 }
265 if (ret == 0) /* No error but failed to find probe point. */
266 die("No probe point found.");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400267 }
268 close(fd);
269
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500270end_dwarf:
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400271#endif /* !NO_LIBDWARF */
272
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500273 /* Synthesize probes without dwarf */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500274 for (i = 0; i < session.nr_probe; i++) {
275 pp = &session.probes[i];
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500276 if (pp->found) /* This probe is already found. */
277 continue;
278
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500279 ret = synthesize_trace_kprobe_event(pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500280 if (ret == -E2BIG)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500281 die("probe point definition becomes too long.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500282 else if (ret < 0)
283 die("Failed to synthesize a probe point.");
284 }
285
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400286 /* Settng up probe points */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500287 add_trace_kprobe_events(session.probes, session.nr_probe);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400288 return 0;
289}
290