blob: bf20df2e816da827b545370e4dfb53c437ae4bcf [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 Hiramatsu253977b2009-10-27 16:43:10 -040065/* Parse an event definition. Note that any error must die. */
66static void parse_probe_event(const char *str)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040067{
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040068 struct probe_point *pp = &session.probes[session.nr_probe];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040069
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020070 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040071 if (++session.nr_probe == MAX_PROBES)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050072 die("Too many probes (> %d) are specified.", MAX_PROBES);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040073
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050074 /* Parse perf-probe event into probe_point */
75 session.need_dwarf = parse_perf_probe_event(str, pp);
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040076
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020077 pr_debug("%d arguments\n", pp->nr_args);
Masami Hiramatsu46ab4922009-10-27 16:43:02 -040078}
79
Masami Hiramatsu253977b2009-10-27 16:43:10 -040080static int opt_add_probe_event(const struct option *opt __used,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -040081 const char *str, int unset __used)
82{
83 if (str)
Masami Hiramatsu253977b2009-10-27 16:43:10 -040084 parse_probe_event(str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040085 return 0;
86}
87
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040088#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040089static int open_default_vmlinux(void)
90{
91 struct utsname uts;
92 char fname[MAX_PATH_LEN];
93 int fd, ret, i;
94
95 ret = uname(&uts);
96 if (ret) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020097 pr_debug("uname() failed.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040098 return -errno;
99 }
100 session.release = uts.release;
101 for (i = 0; i < NR_SEARCH_PATH; i++) {
102 ret = snprintf(fname, MAX_PATH_LEN,
103 default_search_path[i], session.release);
104 if (ret >= MAX_PATH_LEN || ret < 0) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200105 pr_debug("Filename(%d,%s) is too long.\n", i,
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400106 uts.release);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400107 errno = E2BIG;
108 return -E2BIG;
109 }
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200110 pr_debug("try to open %s\n", fname);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400111 fd = open(fname, O_RDONLY);
112 if (fd >= 0)
113 break;
114 }
115 return fd;
116}
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400117#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400118
119static const char * const probe_usage[] = {
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400120 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
121 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400122 NULL
123};
124
125static const struct option options[] = {
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400126 OPT_BOOLEAN('v', "verbose", &verbose,
127 "be more verbose (show parsed arguments, etc)"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400128#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400129 OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
130 "vmlinux/module pathname"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400131#endif
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400132 OPT_CALLBACK('a', "add", NULL,
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400133#ifdef NO_LIBDWARF
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400134 "FUNC[+OFFS|%return] [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400135#else
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400136 "FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400137#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400138 "probe point definition, where\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400139 "\t\tGRP:\tGroup name (optional)\n"
140 "\t\tNAME:\tEvent name\n"
141 "\t\tFUNC:\tFunction name\n"
142 "\t\tOFFS:\tOffset from function entry (in byte)\n"
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400143 "\t\t%return:\tPut the probe at function return\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400144#ifdef NO_LIBDWARF
145 "\t\tARG:\tProbe argument (only \n"
146#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400147 "\t\tSRC:\tSource code path\n"
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400148 "\t\tRLN:\tRelative line number from function entry.\n"
149 "\t\tALN:\tAbsolute line number in file.\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400150 "\t\tARG:\tProbe argument (local variable name or\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400151#endif
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500152 "\t\t\tkprobe-tracer argument format.)\n",
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400153 opt_add_probe_event),
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400154 OPT_END()
155};
156
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400157int cmd_probe(int argc, const char **argv, const char *prefix __used)
158{
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400159 int i, j, fd, ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400160 struct probe_point *pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400161
162 argc = parse_options(argc, argv, options, probe_usage,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400163 PARSE_OPT_STOP_AT_NON_OPTION);
164 for (i = 0; i < argc; i++)
165 parse_probe_event(argv[i]);
166
167 if (session.nr_probe == 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400168 usage_with_options(probe_usage, options);
169
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400170 if (session.need_dwarf)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500171#ifdef NO_LIBDWARF
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500172 die("Debuginfo-analysis is not supported");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500173#else /* !NO_LIBDWARF */
Masami Hiramatsuf41b1e42009-11-30 19:19:27 -0500174 pr_debug("Some probes require debuginfo.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400175
176 if (session.vmlinux)
177 fd = open(session.vmlinux, O_RDONLY);
178 else
179 fd = open_default_vmlinux();
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500180 if (fd < 0) {
181 if (session.need_dwarf)
182 die("Could not open vmlinux/module file.");
183
184 pr_warning("Could not open vmlinux/module file."
185 " Try to use symbols.\n");
186 goto end_dwarf;
187 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400188
189 /* Searching probe points */
190 for (j = 0; j < session.nr_probe; j++) {
191 pp = &session.probes[j];
192 if (pp->found)
193 continue;
194
195 lseek(fd, SEEK_SET, 0);
196 ret = find_probepoint(fd, pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500197 if (ret < 0) {
198 if (session.need_dwarf)
199 die("Could not analyze debuginfo.");
200
201 pr_warning("An error occurred in debuginfo analysis. Try to use symbols.\n");
202 break;
203 }
204 if (ret == 0) /* No error but failed to find probe point. */
205 die("No probe point found.");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400206 }
207 close(fd);
208
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500209end_dwarf:
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400210#endif /* !NO_LIBDWARF */
211
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500212 /* Synthesize probes without dwarf */
213 for (j = 0; j < session.nr_probe; j++) {
214 pp = &session.probes[j];
215 if (pp->found) /* This probe is already found. */
216 continue;
217
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500218 ret = synthesize_trace_kprobe_event(pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500219 if (ret == -E2BIG)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500220 die("probe point definition becomes too long.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500221 else if (ret < 0)
222 die("Failed to synthesize a probe point.");
223 }
224
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400225 /* Settng up probe points */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500226 add_trace_kprobe_events(session.probes, session.nr_probe);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400227 return 0;
228}
229