blob: c1e6774fd3ed316171118cb6598ac8e2dbf7a36c [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 Hiramatsu96c96612009-12-16 17:24:00 -050041#include "util/debugfs.h"
Masami Hiramatsua1281682009-12-15 10:32:33 -050042#include "util/symbol.h"
43#include "util/thread.h"
44#include "util/session.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040045#include "util/parse-options.h"
46#include "util/parse-events.h" /* For debugfs_path */
47#include "util/probe-finder.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050048#include "util/probe-event.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040049
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040050#define MAX_PATH_LEN 256
51#define MAX_PROBES 128
52
53/* Session management structure */
54static struct {
Masami Hiramatsufac13fd2009-12-15 10:31:14 -050055 bool need_dwarf;
56 bool list_events;
Masami Hiramatsud761b082009-12-15 10:32:25 -050057 bool force_add;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040058 int nr_probe;
59 struct probe_point probes[MAX_PROBES];
Masami Hiramatsufa282442009-12-08 17:03:23 -050060 struct strlist *dellist;
Masami Hiramatsua1281682009-12-15 10:32:33 -050061 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
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200154 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
Masami Hiramatsua1281682009-12-15 10:32:33 -0500155 "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 Hiramatsu96c96612009-12-16 17:24:00 -0500209 if (debugfs_valid_mountpoint(debugfs_path) < 0)
210 die("Failed to find debugfs path.");
211
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500212 if (session.list_events) {
Masami Hiramatsufa282442009-12-08 17:03:23 -0500213 if (session.nr_probe != 0 || session.dellist) {
214 pr_warning(" Error: Don't use --list with"
215 " --add/--del.\n");
216 usage_with_options(probe_usage, options);
217 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500218 show_perf_probe_events();
219 return 0;
220 }
221
Masami Hiramatsufa282442009-12-08 17:03:23 -0500222 if (session.dellist) {
223 del_trace_kprobe_events(session.dellist);
224 strlist__delete(session.dellist);
225 if (session.nr_probe == 0)
226 return 0;
227 }
228
Masami Hiramatsua1281682009-12-15 10:32:33 -0500229 /* Initialize symbol maps for vmlinux */
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200230 symbol_conf.sort_by_name = true;
231 if (symbol_conf.vmlinux_name == NULL)
232 symbol_conf.try_vmlinux_path = true;
233 if (symbol__init() < 0)
Masami Hiramatsua1281682009-12-15 10:32:33 -0500234 die("Failed to init symbol map.");
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200235 session.psession = perf_session__new(NULL, O_WRONLY, false);
Masami Hiramatsua1281682009-12-15 10:32:33 -0500236 if (session.psession == NULL)
237 die("Failed to init perf_session.");
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500238 session.kmap = map_groups__find_by_name(&session.psession->kmaps,
239 MAP__FUNCTION,
240 "[kernel.kallsyms]");
241 if (!session.kmap)
242 die("Could not find kernel map.\n");
Masami Hiramatsua1281682009-12-15 10:32:33 -0500243
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400244 if (session.need_dwarf)
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500245#ifdef NO_LIBDWARF
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500246 die("Debuginfo-analysis is not supported");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500247#else /* !NO_LIBDWARF */
Masami Hiramatsuf41b1e42009-11-30 19:19:27 -0500248 pr_debug("Some probes require debuginfo.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400249
Masami Hiramatsua1281682009-12-15 10:32:33 -0500250 fd = open_vmlinux();
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500251 if (fd < 0) {
252 if (session.need_dwarf)
Masami Hiramatsuf984f032009-12-08 17:03:09 -0500253 die("Could not open debuginfo file.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500254
Masami Hiramatsud3a2dbf2009-12-07 12:00:59 -0500255 pr_debug("Could not open vmlinux/module file."
256 " Try to use symbols.\n");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500257 goto end_dwarf;
258 }
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400259
260 /* Searching probe points */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500261 for (i = 0; i < session.nr_probe; i++) {
262 pp = &session.probes[i];
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400263 if (pp->found)
264 continue;
265
266 lseek(fd, SEEK_SET, 0);
267 ret = find_probepoint(fd, pp);
Masami Hiramatsu411edfe2009-12-15 10:31:35 -0500268 if (ret > 0)
269 continue;
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500270 if (ret == 0) { /* No error but failed to find probe point. */
271 synthesize_perf_probe_point(pp);
272 die("Probe point '%s' not found. - probe not added.",
273 pp->probes[0]);
274 }
Masami Hiramatsu411edfe2009-12-15 10:31:35 -0500275 /* Error path */
276 if (session.need_dwarf) {
277 if (ret == -ENOENT)
278 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
279 die("Could not analyze debuginfo.");
280 }
281 pr_debug("An error occurred in debuginfo analysis."
282 " Try to use symbols.\n");
283 break;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400284 }
285 close(fd);
286
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500287end_dwarf:
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400288#endif /* !NO_LIBDWARF */
289
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500290 /* Synthesize probes without dwarf */
Masami Hiramatsud1bde3f2009-12-08 17:02:54 -0500291 for (i = 0; i < session.nr_probe; i++) {
292 pp = &session.probes[i];
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500293 if (pp->found) /* This probe is already found. */
294 continue;
295
Masami Hiramatsu62bdc1b2009-12-15 10:32:40 -0500296 evaluate_probe_point(pp);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500297 ret = synthesize_trace_kprobe_event(pp);
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500298 if (ret == -E2BIG)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500299 die("probe point definition becomes too long.");
Masami Hiramatsua225a1d2009-11-03 19:12:30 -0500300 else if (ret < 0)
301 die("Failed to synthesize a probe point.");
302 }
303
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400304 /* Settng up probe points */
Masami Hiramatsud761b082009-12-15 10:32:25 -0500305 add_trace_kprobe_events(session.probes, session.nr_probe,
306 session.force_add);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400307 return 0;
308}
309