blob: 6b0e4cf322d89502669029ceff45ae9863cab817 [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 Hiramatsua1281682009-12-15 10:32:33 -050041#include "util/symbol.h"
42#include "util/thread.h"
43#include "util/session.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040044#include "util/parse-options.h"
45#include "util/parse-events.h" /* For debugfs_path */
46#include "util/probe-finder.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050047#include "util/probe-event.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040048
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040049#define MAX_PATH_LEN 256
50#define MAX_PROBES 128
51
52/* Session management structure */
53static struct {
Masami Hiramatsufac13fd2009-12-15 10:31:14 -050054 bool need_dwarf;
55 bool list_events;
Masami Hiramatsud761b082009-12-15 10:32:25 -050056 bool force_add;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040057 int nr_probe;
58 struct probe_point probes[MAX_PROBES];
Masami Hiramatsufa282442009-12-08 17:03:23 -050059 struct strlist *dellist;
Masami Hiramatsua1281682009-12-15 10:32:33 -050060 struct symbol_conf conf;
61 struct perf_session *psession;
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -050062 struct map *kmap;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040063} session;
64
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050065
Masami Hiramatsu253977b2009-10-27 16:43:10 -040066/* Parse an event definition. Note that any error must die. */
67static void parse_probe_event(const char *str)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040068{
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040069 struct probe_point *pp = &session.probes[session.nr_probe];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040070
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020071 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040072 if (++session.nr_probe == MAX_PROBES)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050073 die("Too many probes (> %d) are specified.", MAX_PROBES);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040074
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050075 /* Parse perf-probe event into probe_point */
Masami Hiramatsufac13fd2009-12-15 10:31:14 -050076 parse_perf_probe_event(str, pp, &session.need_dwarf);
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040077
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020078 pr_debug("%d arguments\n", pp->nr_args);
Masami Hiramatsu46ab4922009-10-27 16:43:02 -040079}
80
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -050081static void parse_probe_event_argv(int argc, const char **argv)
82{
83 int i, len;
84 char *buf;
85
86 /* Bind up rest arguments */
87 len = 0;
88 for (i = 0; i < argc; i++)
89 len += strlen(argv[i]) + 1;
90 buf = zalloc(len + 1);
91 if (!buf)
92 die("Failed to allocate memory for binding arguments.");
93 len = 0;
94 for (i = 0; i < argc; i++)
95 len += sprintf(&buf[len], "%s ", argv[i]);
96 parse_probe_event(buf);
97 free(buf);
98}
99
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400100static int opt_add_probe_event(const struct option *opt __used,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400101 const char *str, int unset __used)
102{
103 if (str)
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400104 parse_probe_event(str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400105 return 0;
106}
107
Masami Hiramatsufa282442009-12-08 17:03:23 -0500108static int opt_del_probe_event(const struct option *opt __used,
109 const char *str, int unset __used)
110{
111 if (str) {
112 if (!session.dellist)
113 session.dellist = strlist__new(true, NULL);
114 strlist__add(session.dellist, str);
115 }
116 return 0;
117}
118
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500119/* Currently just checking function name from symbol map */
120static void evaluate_probe_point(struct probe_point *pp)
121{
122 struct symbol *sym;
123 sym = map__find_symbol_by_name(session.kmap, pp->function,
124 session.psession, NULL);
125 if (!sym)
126 die("Kernel symbol \'%s\' not found - probe not added.",
127 pp->function);
128}
129
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400130#ifndef NO_LIBDWARF
Masami Hiramatsua1281682009-12-15 10:32:33 -0500131static int open_vmlinux(void)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400132{
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500133 if (map__load(session.kmap, session.psession, NULL) < 0) {
Masami Hiramatsua1281682009-12-15 10:32:33 -0500134 pr_debug("Failed to load kernel map.\n");
135 return -EINVAL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400136 }
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500137 pr_debug("Try to open %s\n", session.kmap->dso->long_name);
138 return open(session.kmap->dso->long_name, O_RDONLY);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400139}
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400140#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400141
142static const char * const probe_usage[] = {
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400143 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
144 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
Masami Hiramatsufa282442009-12-08 17:03:23 -0500145 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500146 "perf probe --list",
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400147 NULL
148};
149
150static const struct option options[] = {
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400151 OPT_BOOLEAN('v', "verbose", &verbose,
152 "be more verbose (show parsed arguments, etc)"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400153#ifndef NO_LIBDWARF
Masami Hiramatsua1281682009-12-15 10:32:33 -0500154 OPT_STRING('k', "vmlinux", &session.conf.vmlinux_name,
155 "file", "vmlinux pathname"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400156#endif
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500157 OPT_BOOLEAN('l', "list", &session.list_events,
158 "list up current probe events"),
Masami Hiramatsufa282442009-12-08 17:03:23 -0500159 OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
160 opt_del_probe_event),
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400161 OPT_CALLBACK('a', "add", NULL,
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400162#ifdef NO_LIBDWARF
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500163 "[EVENT=]FUNC[+OFFS|%return] [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400164#else
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500165 "[EVENT=]FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400166#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400167 "probe point definition, where\n"
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500168 "\t\tGROUP:\tGroup name (optional)\n"
169 "\t\tEVENT:\tEvent name\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400170 "\t\tFUNC:\tFunction name\n"
171 "\t\tOFFS:\tOffset from function entry (in byte)\n"
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400172 "\t\t%return:\tPut the probe at function return\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400173#ifdef NO_LIBDWARF
174 "\t\tARG:\tProbe argument (only \n"
175#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400176 "\t\tSRC:\tSource code path\n"
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400177 "\t\tRLN:\tRelative line number from function entry.\n"
178 "\t\tALN:\tAbsolute line number in file.\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400179 "\t\tARG:\tProbe argument (local variable name or\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400180#endif
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500181 "\t\t\tkprobe-tracer argument format.)\n",
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400182 opt_add_probe_event),
Masami Hiramatsud761b082009-12-15 10:32:25 -0500183 OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
184 " with existing name"),
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400185 OPT_END()
186};
187
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400188int cmd_probe(int argc, const char **argv, const char *prefix __used)
189{
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500190 int i, ret;
Xiao Guangrongbdad0db2009-12-02 16:08:41 +0800191#ifndef NO_LIBDWARF
192 int fd;
193#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400194 struct probe_point *pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400195
196 argc = parse_options(argc, argv, options, probe_usage,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400197 PARSE_OPT_STOP_AT_NON_OPTION);
Masami Hiramatsuce11a602009-12-15 10:31:28 -0500198 if (argc > 0) {
199 if (strcmp(argv[0], "-") == 0) {
200 pr_warning(" Error: '-' is not supported.\n");
201 usage_with_options(probe_usage, options);
202 }
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500203 parse_probe_event_argv(argc, argv);
Masami Hiramatsuce11a602009-12-15 10:31:28 -0500204 }
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400205
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500206 if ((!session.nr_probe && !session.dellist && !session.list_events))
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400207 usage_with_options(probe_usage, options);
208
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500209 if (session.list_events) {
Masami Hiramatsufa282442009-12-08 17:03:23 -0500210 if (session.nr_probe != 0 || session.dellist) {
211 pr_warning(" Error: Don't use --list with"
212 " --add/--del.\n");
213 usage_with_options(probe_usage, options);
214 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500215 show_perf_probe_events();
216 return 0;
217 }
218
Masami Hiramatsufa282442009-12-08 17:03:23 -0500219 if (session.dellist) {
220 del_trace_kprobe_events(session.dellist);
221 strlist__delete(session.dellist);
222 if (session.nr_probe == 0)
223 return 0;
224 }
225
Masami Hiramatsua1281682009-12-15 10:32:33 -0500226 /* Initialize symbol maps for vmlinux */
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500227 session.conf.sort_by_name = true;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500228 if (session.conf.vmlinux_name == NULL)
229 session.conf.try_vmlinux_path = true;
230 if (symbol__init(&session.conf) < 0)
231 die("Failed to init symbol map.");
232 session.psession = perf_session__new(NULL, O_WRONLY, false,
233 &session.conf);
234 if (session.psession == NULL)
235 die("Failed to init perf_session.");
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500236 session.kmap = map_groups__find_by_name(&session.psession->kmaps,
237 MAP__FUNCTION,
238 "[kernel.kallsyms]");
239 if (!session.kmap)
240 die("Could not find kernel map.\n");
Masami Hiramatsua1281682009-12-15 10:32:33 -0500241
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400242 if (session.need_dwarf)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500243#ifdef NO_LIBDWARF
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500244 die("Debuginfo-analysis is not supported");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500245#else /* !NO_LIBDWARF */
Masami Hiramatsuf41b1e42009-11-30 19:19:27 -0500246 pr_debug("Some probes require debuginfo.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400247
Masami Hiramatsua1281682009-12-15 10:32:33 -0500248 fd = open_vmlinux();
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500249 if (fd < 0) {
250 if (session.need_dwarf)
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500251 die("Could not open debuginfo file.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500252
Masami Hiramatsud3a2dbf2009-12-07 12:00:59 -0500253 pr_debug("Could not open vmlinux/module file."
254 " Try to use symbols.\n");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500255 goto end_dwarf;
256 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400257
258 /* Searching probe points */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500259 for (i = 0; i < session.nr_probe; i++) {
260 pp = &session.probes[i];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400261 if (pp->found)
262 continue;
263
264 lseek(fd, SEEK_SET, 0);
265 ret = find_probepoint(fd, pp);
Masami Hiramatsu411edfe2009-12-15 10:31:35 -0500266 if (ret > 0)
267 continue;
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500268 if (ret == 0) /* No error but failed to find probe point. */
269 die("No probe point found.");
Masami Hiramatsu411edfe2009-12-15 10:31:35 -0500270 /* Error path */
271 if (session.need_dwarf) {
272 if (ret == -ENOENT)
273 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
274 die("Could not analyze debuginfo.");
275 }
276 pr_debug("An error occurred in debuginfo analysis."
277 " Try to use symbols.\n");
278 break;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400279 }
280 close(fd);
281
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500282end_dwarf:
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400283#endif /* !NO_LIBDWARF */
284
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500285 /* Synthesize probes without dwarf */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500286 for (i = 0; i < session.nr_probe; i++) {
287 pp = &session.probes[i];
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500288 if (pp->found) /* This probe is already found. */
289 continue;
290
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500291 evaluate_probe_point(pp);
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