blob: 7e741f54d7986a15f72a97c9eb4f684f33f23e6f [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 perf_session *psession;
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -050061 struct map *kmap;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040062} session;
63
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050064
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 */
Masami Hiramatsufac13fd2009-12-15 10:31:14 -050075 parse_perf_probe_event(str, pp, &session.need_dwarf);
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 Hiramatsud1bde3f2009-12-08 17:02:54 -050080static void parse_probe_event_argv(int argc, const char **argv)
81{
82 int i, len;
83 char *buf;
84
85 /* Bind up rest arguments */
86 len = 0;
87 for (i = 0; i < argc; i++)
88 len += strlen(argv[i]) + 1;
89 buf = zalloc(len + 1);
90 if (!buf)
91 die("Failed to allocate memory for binding arguments.");
92 len = 0;
93 for (i = 0; i < argc; i++)
94 len += sprintf(&buf[len], "%s ", argv[i]);
95 parse_probe_event(buf);
96 free(buf);
97}
98
Masami Hiramatsu253977b2009-10-27 16:43:10 -040099static int opt_add_probe_event(const struct option *opt __used,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400100 const char *str, int unset __used)
101{
102 if (str)
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400103 parse_probe_event(str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400104 return 0;
105}
106
Masami Hiramatsufa282442009-12-08 17:03:23 -0500107static int opt_del_probe_event(const struct option *opt __used,
108 const char *str, int unset __used)
109{
110 if (str) {
111 if (!session.dellist)
112 session.dellist = strlist__new(true, NULL);
113 strlist__add(session.dellist, str);
114 }
115 return 0;
116}
117
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500118/* Currently just checking function name from symbol map */
119static void evaluate_probe_point(struct probe_point *pp)
120{
121 struct symbol *sym;
122 sym = map__find_symbol_by_name(session.kmap, pp->function,
123 session.psession, NULL);
124 if (!sym)
125 die("Kernel symbol \'%s\' not found - probe not added.",
126 pp->function);
127}
128
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400129#ifndef NO_LIBDWARF
Masami Hiramatsua1281682009-12-15 10:32:33 -0500130static int open_vmlinux(void)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400131{
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500132 if (map__load(session.kmap, session.psession, NULL) < 0) {
Masami Hiramatsua1281682009-12-15 10:32:33 -0500133 pr_debug("Failed to load kernel map.\n");
134 return -EINVAL;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400135 }
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500136 pr_debug("Try to open %s\n", session.kmap->dso->long_name);
137 return open(session.kmap->dso->long_name, O_RDONLY);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400138}
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 Hiramatsufa282442009-12-08 17:03:23 -0500144 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500145 "perf probe --list",
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400146 NULL
147};
148
149static const struct option options[] = {
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400150 OPT_BOOLEAN('v', "verbose", &verbose,
151 "be more verbose (show parsed arguments, etc)"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400152#ifndef NO_LIBDWARF
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200153 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
Masami Hiramatsua1281682009-12-15 10:32:33 -0500154 "file", "vmlinux pathname"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400155#endif
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500156 OPT_BOOLEAN('l', "list", &session.list_events,
157 "list up current probe events"),
Masami Hiramatsufa282442009-12-08 17:03:23 -0500158 OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
159 opt_del_probe_event),
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400160 OPT_CALLBACK('a', "add", NULL,
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400161#ifdef NO_LIBDWARF
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500162 "[EVENT=]FUNC[+OFFS|%return] [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400163#else
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500164 "[EVENT=]FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400165#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400166 "probe point definition, where\n"
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500167 "\t\tGROUP:\tGroup name (optional)\n"
168 "\t\tEVENT:\tEvent name\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400169 "\t\tFUNC:\tFunction name\n"
170 "\t\tOFFS:\tOffset from function entry (in byte)\n"
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400171 "\t\t%return:\tPut the probe at function return\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400172#ifdef NO_LIBDWARF
173 "\t\tARG:\tProbe argument (only \n"
174#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400175 "\t\tSRC:\tSource code path\n"
Masami Hiramatsub0ef0732009-10-27 16:43:19 -0400176 "\t\tRLN:\tRelative line number from function entry.\n"
177 "\t\tALN:\tAbsolute line number in file.\n"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400178 "\t\tARG:\tProbe argument (local variable name or\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400179#endif
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500180 "\t\t\tkprobe-tracer argument format.)\n",
Masami Hiramatsu253977b2009-10-27 16:43:10 -0400181 opt_add_probe_event),
Masami Hiramatsud761b082009-12-15 10:32:25 -0500182 OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
183 " with existing name"),
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400184 OPT_END()
185};
186
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400187int cmd_probe(int argc, const char **argv, const char *prefix __used)
188{
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500189 int i, ret;
Xiao Guangrongbdad0db2009-12-02 16:08:41 +0800190#ifndef NO_LIBDWARF
191 int fd;
192#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400193 struct probe_point *pp;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400194
195 argc = parse_options(argc, argv, options, probe_usage,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400196 PARSE_OPT_STOP_AT_NON_OPTION);
Masami Hiramatsuce11a602009-12-15 10:31:28 -0500197 if (argc > 0) {
198 if (strcmp(argv[0], "-") == 0) {
199 pr_warning(" Error: '-' is not supported.\n");
200 usage_with_options(probe_usage, options);
201 }
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500202 parse_probe_event_argv(argc, argv);
Masami Hiramatsuce11a602009-12-15 10:31:28 -0500203 }
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400204
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500205 if ((!session.nr_probe && !session.dellist && !session.list_events))
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400206 usage_with_options(probe_usage, options);
207
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500208 if (session.list_events) {
Masami Hiramatsufa282442009-12-08 17:03:23 -0500209 if (session.nr_probe != 0 || session.dellist) {
210 pr_warning(" Error: Don't use --list with"
211 " --add/--del.\n");
212 usage_with_options(probe_usage, options);
213 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500214 show_perf_probe_events();
215 return 0;
216 }
217
Masami Hiramatsufa282442009-12-08 17:03:23 -0500218 if (session.dellist) {
219 del_trace_kprobe_events(session.dellist);
220 strlist__delete(session.dellist);
221 if (session.nr_probe == 0)
222 return 0;
223 }
224
Masami Hiramatsua1281682009-12-15 10:32:33 -0500225 /* Initialize symbol maps for vmlinux */
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200226 symbol_conf.sort_by_name = true;
227 if (symbol_conf.vmlinux_name == NULL)
228 symbol_conf.try_vmlinux_path = true;
229 if (symbol__init() < 0)
Masami Hiramatsua1281682009-12-15 10:32:33 -0500230 die("Failed to init symbol map.");
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200231 session.psession = perf_session__new(NULL, O_WRONLY, false);
Masami Hiramatsua1281682009-12-15 10:32:33 -0500232 if (session.psession == NULL)
233 die("Failed to init perf_session.");
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500234 session.kmap = map_groups__find_by_name(&session.psession->kmaps,
235 MAP__FUNCTION,
236 "[kernel.kallsyms]");
237 if (!session.kmap)
238 die("Could not find kernel map.\n");
Masami Hiramatsua1281682009-12-15 10:32:33 -0500239
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400240 if (session.need_dwarf)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500241#ifdef NO_LIBDWARF
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500242 die("Debuginfo-analysis is not supported");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500243#else /* !NO_LIBDWARF */
Masami Hiramatsuf41b1e42009-11-30 19:19:27 -0500244 pr_debug("Some probes require debuginfo.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400245
Masami Hiramatsua1281682009-12-15 10:32:33 -0500246 fd = open_vmlinux();
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500247 if (fd < 0) {
248 if (session.need_dwarf)
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500249 die("Could not open debuginfo file.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500250
Masami Hiramatsud3a2dbf2009-12-07 12:00:59 -0500251 pr_debug("Could not open vmlinux/module file."
252 " Try to use symbols.\n");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500253 goto end_dwarf;
254 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400255
256 /* Searching probe points */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500257 for (i = 0; i < session.nr_probe; i++) {
258 pp = &session.probes[i];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400259 if (pp->found)
260 continue;
261
262 lseek(fd, SEEK_SET, 0);
263 ret = find_probepoint(fd, pp);
Masami Hiramatsu411edfe2009-12-15 10:31:35 -0500264 if (ret > 0)
265 continue;
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500266 if (ret == 0) { /* No error but failed to find probe point. */
267 synthesize_perf_probe_point(pp);
268 die("Probe point '%s' not found. - probe not added.",
269 pp->probes[0]);
270 }
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 Hiramatsu62bdc1b2009-12-15 10:32:40 -0500292 evaluate_probe_point(pp);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500293 ret = synthesize_trace_kprobe_event(pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500294 if (ret == -E2BIG)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500295 die("probe point definition becomes too long.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500296 else if (ret < 0)
297 die("Failed to synthesize a probe point.");
298 }
299
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400300 /* Settng up probe points */
Masami Hiramatsud761b082009-12-15 10:32:25 -0500301 add_trace_kprobe_events(session.probes, session.nr_probe,
302 session.force_add);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400303 return 0;
304}
305