blob: 8b4fdaeefa29b4f445b0b37112393b9e3e15115b [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 Hiramatsufac13fd2009-12-15 10:31:14 -050062 bool need_dwarf;
63 bool list_events;
Masami Hiramatsud761b082009-12-15 10:32:25 -050064 bool force_add;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040065 int nr_probe;
66 struct probe_point probes[MAX_PROBES];
Masami Hiramatsufa282442009-12-08 17:03:23 -050067 struct strlist *dellist;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040068} session;
69
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050070
Masami Hiramatsu253977b2009-10-27 16:43:10 -040071/* Parse an event definition. Note that any error must die. */
72static void parse_probe_event(const char *str)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040073{
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040074 struct probe_point *pp = &session.probes[session.nr_probe];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040075
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020076 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040077 if (++session.nr_probe == MAX_PROBES)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050078 die("Too many probes (> %d) are specified.", MAX_PROBES);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040079
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050080 /* Parse perf-probe event into probe_point */
Masami Hiramatsufac13fd2009-12-15 10:31:14 -050081 parse_perf_probe_event(str, pp, &session.need_dwarf);
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040082
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020083 pr_debug("%d arguments\n", pp->nr_args);
Masami Hiramatsu46ab4922009-10-27 16:43:02 -040084}
85
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -050086static void parse_probe_event_argv(int argc, const char **argv)
87{
88 int i, len;
89 char *buf;
90
91 /* Bind up rest arguments */
92 len = 0;
93 for (i = 0; i < argc; i++)
94 len += strlen(argv[i]) + 1;
95 buf = zalloc(len + 1);
96 if (!buf)
97 die("Failed to allocate memory for binding arguments.");
98 len = 0;
99 for (i = 0; i < argc; i++)
100 len += sprintf(&buf[len], "%s ", argv[i]);
101 parse_probe_event(buf);
102 free(buf);
103}
104
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400105static int opt_add_probe_event(const struct option *opt __used,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400106 const char *str, int unset __used)
107{
108 if (str)
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400109 parse_probe_event(str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400110 return 0;
111}
112
Masami Hiramatsufa282442009-12-08 17:03:23 -0500113static int opt_del_probe_event(const struct option *opt __used,
114 const char *str, int unset __used)
115{
116 if (str) {
117 if (!session.dellist)
118 session.dellist = strlist__new(true, NULL);
119 strlist__add(session.dellist, str);
120 }
121 return 0;
122}
123
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400124#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400125static int open_default_vmlinux(void)
126{
127 struct utsname uts;
128 char fname[MAX_PATH_LEN];
129 int fd, ret, i;
130
131 ret = uname(&uts);
132 if (ret) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200133 pr_debug("uname() failed.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400134 return -errno;
135 }
136 session.release = uts.release;
137 for (i = 0; i < NR_SEARCH_PATH; i++) {
138 ret = snprintf(fname, MAX_PATH_LEN,
139 default_search_path[i], session.release);
140 if (ret >= MAX_PATH_LEN || ret < 0) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200141 pr_debug("Filename(%d,%s) is too long.\n", i,
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400142 uts.release);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400143 errno = E2BIG;
144 return -E2BIG;
145 }
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200146 pr_debug("try to open %s\n", fname);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400147 fd = open(fname, O_RDONLY);
148 if (fd >= 0)
149 break;
150 }
151 return fd;
152}
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400153#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400154
155static const char * const probe_usage[] = {
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400156 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
157 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
Masami Hiramatsufa282442009-12-08 17:03:23 -0500158 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500159 "perf probe --list",
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400160 NULL
161};
162
163static const struct option options[] = {
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400164 OPT_BOOLEAN('v', "verbose", &verbose,
165 "be more verbose (show parsed arguments, etc)"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400166#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400167 OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
168 "vmlinux/module pathname"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400169#endif
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500170 OPT_BOOLEAN('l', "list", &session.list_events,
171 "list up current probe events"),
Masami Hiramatsufa282442009-12-08 17:03:23 -0500172 OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
173 opt_del_probe_event),
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400174 OPT_CALLBACK('a', "add", NULL,
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400175#ifdef NO_LIBDWARF
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500176 "[EVENT=]FUNC[+OFFS|%return] [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400177#else
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500178 "[EVENT=]FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [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"
184 "\t\tOFFS:\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 Hiramatsu23e8ec02009-10-07 18:28:30 -0400186#ifdef NO_LIBDWARF
187 "\t\tARG:\tProbe argument (only \n"
188#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400189 "\t\tSRC:\tSource code path\n"
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400190 "\t\tRLN:\tRelative line number from function entry.\n"
191 "\t\tALN:\tAbsolute line number in file.\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400192 "\t\tARG:\tProbe argument (local variable name or\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400193#endif
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500194 "\t\t\tkprobe-tracer argument format.)\n",
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400195 opt_add_probe_event),
Masami Hiramatsud761b082009-12-15 10:32:25 -0500196 OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
197 " with existing name"),
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400198 OPT_END()
199};
200
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400201int cmd_probe(int argc, const char **argv, const char *prefix __used)
202{
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500203 int i, ret;
Xiao Guangrongbdad0db2009-12-02 16:08:41 +0800204#ifndef NO_LIBDWARF
205 int fd;
206#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400207 struct probe_point *pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400208
209 argc = parse_options(argc, argv, options, probe_usage,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400210 PARSE_OPT_STOP_AT_NON_OPTION);
Masami Hiramatsuce11a602009-12-15 10:31:28 -0500211 if (argc > 0) {
212 if (strcmp(argv[0], "-") == 0) {
213 pr_warning(" Error: '-' is not supported.\n");
214 usage_with_options(probe_usage, options);
215 }
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500216 parse_probe_event_argv(argc, argv);
Masami Hiramatsuce11a602009-12-15 10:31:28 -0500217 }
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400218
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500219 if ((!session.nr_probe && !session.dellist && !session.list_events))
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400220 usage_with_options(probe_usage, options);
221
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500222 if (session.list_events) {
Masami Hiramatsufa282442009-12-08 17:03:23 -0500223 if (session.nr_probe != 0 || session.dellist) {
224 pr_warning(" Error: Don't use --list with"
225 " --add/--del.\n");
226 usage_with_options(probe_usage, options);
227 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500228 show_perf_probe_events();
229 return 0;
230 }
231
Masami Hiramatsufa282442009-12-08 17:03:23 -0500232 if (session.dellist) {
233 del_trace_kprobe_events(session.dellist);
234 strlist__delete(session.dellist);
235 if (session.nr_probe == 0)
236 return 0;
237 }
238
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400239 if (session.need_dwarf)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500240#ifdef NO_LIBDWARF
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500241 die("Debuginfo-analysis is not supported");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500242#else /* !NO_LIBDWARF */
Masami Hiramatsuf41b1e42009-11-30 19:19:27 -0500243 pr_debug("Some probes require debuginfo.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400244
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500245 if (session.vmlinux) {
246 pr_debug("Try to open %s.", session.vmlinux);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400247 fd = open(session.vmlinux, O_RDONLY);
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500248 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400249 fd = open_default_vmlinux();
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500250 if (fd < 0) {
251 if (session.need_dwarf)
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500252 die("Could not open debuginfo file.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500253
Masami Hiramatsud3a2dbf2009-12-07 12:00:59 -0500254 pr_debug("Could not open vmlinux/module file."
255 " Try to use symbols.\n");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500256 goto end_dwarf;
257 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400258
259 /* Searching probe points */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500260 for (i = 0; i < session.nr_probe; i++) {
261 pp = &session.probes[i];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400262 if (pp->found)
263 continue;
264
265 lseek(fd, SEEK_SET, 0);
266 ret = find_probepoint(fd, pp);
Masami Hiramatsu411edfe2009-12-15 10:31:35 -0500267 if (ret > 0)
268 continue;
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500269 if (ret == 0) /* No error but failed to find probe point. */
270 die("No probe point found.");
Masami Hiramatsu411edfe2009-12-15 10:31:35 -0500271 /* Error path */
272 if (session.need_dwarf) {
273 if (ret == -ENOENT)
274 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
275 die("Could not analyze debuginfo.");
276 }
277 pr_debug("An error occurred in debuginfo analysis."
278 " Try to use symbols.\n");
279 break;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400280 }
281 close(fd);
282
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500283end_dwarf:
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400284#endif /* !NO_LIBDWARF */
285
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500286 /* Synthesize probes without dwarf */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500287 for (i = 0; i < session.nr_probe; i++) {
288 pp = &session.probes[i];
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500289 if (pp->found) /* This probe is already found. */
290 continue;
291
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500292 ret = synthesize_trace_kprobe_event(pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500293 if (ret == -E2BIG)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500294 die("probe point definition becomes too long.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500295 else if (ret < 0)
296 die("Failed to synthesize a probe point.");
297 }
298
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400299 /* Settng up probe points */
Masami Hiramatsud761b082009-12-15 10:32:25 -0500300 add_trace_kprobe_events(session.probes, session.nr_probe,
301 session.force_add);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400302 return 0;
303}
304