blob: 1c97e133a3f420066cb307c28f451093668d0204 [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 */
Masami Hiramatsuf984f032009-12-08 17:03:09 -050046#define NR_SEARCH_PATH 4
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040047const 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 */
Masami Hiramatsuf984f032009-12-08 17:03:09 -050051"./vmlinux", /* CWD */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040052};
53
54#define MAX_PATH_LEN 256
55#define MAX_PROBES 128
56
57/* Session management structure */
58static struct {
59 char *vmlinux;
60 char *release;
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040061 int need_dwarf;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040062 int nr_probe;
63 struct probe_point probes[MAX_PROBES];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040064} session;
65
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050066static bool listing;
67
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 */
78 session.need_dwarf = parse_perf_probe_event(str, pp);
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 Hiramatsu23e8ec02009-10-07 18:28:30 -0400110#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400111static int open_default_vmlinux(void)
112{
113 struct utsname uts;
114 char fname[MAX_PATH_LEN];
115 int fd, ret, i;
116
117 ret = uname(&uts);
118 if (ret) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200119 pr_debug("uname() failed.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400120 return -errno;
121 }
122 session.release = uts.release;
123 for (i = 0; i < NR_SEARCH_PATH; i++) {
124 ret = snprintf(fname, MAX_PATH_LEN,
125 default_search_path[i], session.release);
126 if (ret >= MAX_PATH_LEN || ret < 0) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200127 pr_debug("Filename(%d,%s) is too long.\n", i,
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400128 uts.release);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400129 errno = E2BIG;
130 return -E2BIG;
131 }
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200132 pr_debug("try to open %s\n", fname);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400133 fd = open(fname, O_RDONLY);
134 if (fd >= 0)
135 break;
136 }
137 return fd;
138}
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400139#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400140
141static const char * const probe_usage[] = {
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400142 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
143 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500144 "perf probe --list",
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400145 NULL
146};
147
148static const struct option options[] = {
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400149 OPT_BOOLEAN('v', "verbose", &verbose,
150 "be more verbose (show parsed arguments, etc)"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400151#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400152 OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
153 "vmlinux/module pathname"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400154#endif
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500155 OPT_BOOLEAN('l', "list", &listing, "list up current probes"),
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400156 OPT_CALLBACK('a', "add", NULL,
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400157#ifdef NO_LIBDWARF
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400158 "FUNC[+OFFS|%return] [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400159#else
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400160 "FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400161#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400162 "probe point definition, where\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400163 "\t\tGRP:\tGroup name (optional)\n"
164 "\t\tNAME:\tEvent name\n"
165 "\t\tFUNC:\tFunction name\n"
166 "\t\tOFFS:\tOffset from function entry (in byte)\n"
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400167 "\t\t%return:\tPut the probe at function return\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400168#ifdef NO_LIBDWARF
169 "\t\tARG:\tProbe argument (only \n"
170#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400171 "\t\tSRC:\tSource code path\n"
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400172 "\t\tRLN:\tRelative line number from function entry.\n"
173 "\t\tALN:\tAbsolute line number in file.\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400174 "\t\tARG:\tProbe argument (local variable name or\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400175#endif
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500176 "\t\t\tkprobe-tracer argument format.)\n",
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400177 opt_add_probe_event),
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400178 OPT_END()
179};
180
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400181int cmd_probe(int argc, const char **argv, const char *prefix __used)
182{
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500183 int i, ret;
Xiao Guangrongbdad0db2009-12-02 16:08:41 +0800184#ifndef NO_LIBDWARF
185 int fd;
186#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400187 struct probe_point *pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400188
189 argc = parse_options(argc, argv, options, probe_usage,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400190 PARSE_OPT_STOP_AT_NON_OPTION);
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500191 if (argc > 0)
192 parse_probe_event_argv(argc, argv);
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400193
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500194 if ((session.nr_probe == 0 && !listing) ||
195 (session.nr_probe != 0 && listing))
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400196 usage_with_options(probe_usage, options);
197
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500198 if (listing) {
199 show_perf_probe_events();
200 return 0;
201 }
202
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400203 if (session.need_dwarf)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500204#ifdef NO_LIBDWARF
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500205 die("Debuginfo-analysis is not supported");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500206#else /* !NO_LIBDWARF */
Masami Hiramatsuf41b1e42009-11-30 19:19:27 -0500207 pr_debug("Some probes require debuginfo.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400208
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500209 if (session.vmlinux) {
210 pr_debug("Try to open %s.", session.vmlinux);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400211 fd = open(session.vmlinux, O_RDONLY);
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500212 } else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400213 fd = open_default_vmlinux();
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500214 if (fd < 0) {
215 if (session.need_dwarf)
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500216 die("Could not open debuginfo file.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500217
Masami Hiramatsud3a2dbf2009-12-07 12:00:59 -0500218 pr_debug("Could not open vmlinux/module file."
219 " Try to use symbols.\n");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500220 goto end_dwarf;
221 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400222
223 /* Searching probe points */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500224 for (i = 0; i < session.nr_probe; i++) {
225 pp = &session.probes[i];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400226 if (pp->found)
227 continue;
228
229 lseek(fd, SEEK_SET, 0);
230 ret = find_probepoint(fd, pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500231 if (ret < 0) {
232 if (session.need_dwarf)
233 die("Could not analyze debuginfo.");
234
235 pr_warning("An error occurred in debuginfo analysis. Try to use symbols.\n");
236 break;
237 }
238 if (ret == 0) /* No error but failed to find probe point. */
239 die("No probe point found.");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400240 }
241 close(fd);
242
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500243end_dwarf:
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400244#endif /* !NO_LIBDWARF */
245
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500246 /* Synthesize probes without dwarf */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500247 for (i = 0; i < session.nr_probe; i++) {
248 pp = &session.probes[i];
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500249 if (pp->found) /* This probe is already found. */
250 continue;
251
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500252 ret = synthesize_trace_kprobe_event(pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500253 if (ret == -E2BIG)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500254 die("probe point definition becomes too long.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500255 else if (ret < 0)
256 die("Failed to synthesize a probe point.");
257 }
258
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400259 /* Settng up probe points */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500260 add_trace_kprobe_events(session.probes, session.nr_probe);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400261 return 0;
262}
263