perf probe: Use wrapper functions
[deliverable/linux.git] / tools / perf / builtin-probe.c
CommitLineData
4ea42b18
MH
1/*
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"
fa28244d 38#include "util/strlist.h"
89c69c0e
MH
39#include "util/event.h"
40#include "util/debug.h"
96c96612 41#include "util/debugfs.h"
a128168d
MH
42#include "util/symbol.h"
43#include "util/thread.h"
4ea42b18
MH
44#include "util/parse-options.h"
45#include "util/parse-events.h" /* For debugfs_path */
46#include "util/probe-finder.h"
50656eec 47#include "util/probe-event.h"
4ea42b18 48
4ea42b18 49#define MAX_PATH_LEN 256
4ea42b18
MH
50
51/* Session management structure */
52static struct {
fac13fd5
MH
53 bool need_dwarf;
54 bool list_events;
d761b08b 55 bool force_add;
631c9def 56 bool show_lines;
4ea42b18
MH
57 int nr_probe;
58 struct probe_point probes[MAX_PROBES];
fa28244d 59 struct strlist *dellist;
8ad94c60
ACM
60 struct map_groups kmap_groups;
61 struct map *kmaps[MAP__NR_TYPES];
631c9def 62 struct line_range line_range;
4ea42b18
MH
63} session;
64
4de189fe 65
253977b0
MH
66/* Parse an event definition. Note that any error must die. */
67static void parse_probe_event(const char *str)
4ea42b18 68{
4ea42b18 69 struct probe_point *pp = &session.probes[session.nr_probe];
4ea42b18 70
b7cb10e7 71 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
4ea42b18 72 if (++session.nr_probe == MAX_PROBES)
50656eec 73 die("Too many probes (> %d) are specified.", MAX_PROBES);
4ea42b18 74
50656eec 75 /* Parse perf-probe event into probe_point */
fac13fd5 76 parse_perf_probe_event(str, pp, &session.need_dwarf);
23e8ec0d 77
b7cb10e7 78 pr_debug("%d arguments\n", pp->nr_args);
46ab4926
MH
79}
80
d1bde3f7
MH
81static 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;
31facc5f 90 buf = xzalloc(len + 1);
d1bde3f7
MH
91 len = 0;
92 for (i = 0; i < argc; i++)
93 len += sprintf(&buf[len], "%s ", argv[i]);
94 parse_probe_event(buf);
95 free(buf);
96}
97
253977b0 98static int opt_add_probe_event(const struct option *opt __used,
46ab4926
MH
99 const char *str, int unset __used)
100{
101 if (str)
253977b0 102 parse_probe_event(str);
4ea42b18
MH
103 return 0;
104}
105
fa28244d
MH
106static int opt_del_probe_event(const struct option *opt __used,
107 const char *str, int unset __used)
108{
109 if (str) {
110 if (!session.dellist)
111 session.dellist = strlist__new(true, NULL);
112 strlist__add(session.dellist, str);
113 }
114 return 0;
115}
116
62bdc1b3
MH
117/* Currently just checking function name from symbol map */
118static void evaluate_probe_point(struct probe_point *pp)
119{
120 struct symbol *sym;
8ad94c60
ACM
121 sym = map__find_symbol_by_name(session.kmaps[MAP__FUNCTION],
122 pp->function, NULL);
62bdc1b3
MH
123 if (!sym)
124 die("Kernel symbol \'%s\' not found - probe not added.",
125 pp->function);
126}
127
804b3606 128#ifndef NO_DWARF_SUPPORT
a128168d 129static int open_vmlinux(void)
4ea42b18 130{
8ad94c60 131 if (map__load(session.kmaps[MAP__FUNCTION], NULL) < 0) {
a128168d
MH
132 pr_debug("Failed to load kernel map.\n");
133 return -EINVAL;
4ea42b18 134 }
8ad94c60
ACM
135 pr_debug("Try to open %s\n",
136 session.kmaps[MAP__FUNCTION]->dso->long_name);
137 return open(session.kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
4ea42b18 138}
0eda7385
HM
139
140static int opt_show_lines(const struct option *opt __used,
141 const char *str, int unset __used)
142{
143 if (str)
144 parse_line_range_desc(str, &session.line_range);
145 INIT_LIST_HEAD(&session.line_range.line_list);
146 session.show_lines = true;
147 return 0;
148}
23e8ec0d 149#endif
4ea42b18
MH
150
151static const char * const probe_usage[] = {
46ab4926
MH
152 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
153 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
fa28244d 154 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
4de189fe 155 "perf probe --list",
804b3606 156#ifndef NO_DWARF_SUPPORT
631c9def 157 "perf probe --line 'LINEDESC'",
f3ab481c 158#endif
4ea42b18
MH
159 NULL
160};
161
162static const struct option options[] = {
89c69c0e
MH
163 OPT_BOOLEAN('v', "verbose", &verbose,
164 "be more verbose (show parsed arguments, etc)"),
804b3606 165#ifndef NO_DWARF_SUPPORT
75be6cf4 166 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
a128168d 167 "file", "vmlinux pathname"),
23e8ec0d 168#endif
fac13fd5
MH
169 OPT_BOOLEAN('l', "list", &session.list_events,
170 "list up current probe events"),
fa28244d
MH
171 OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
172 opt_del_probe_event),
46ab4926 173 OPT_CALLBACK('a', "add", NULL,
804b3606 174#ifdef NO_DWARF_SUPPORT
2a9c8c36 175 "[EVENT=]FUNC[+OFF|%return] [ARG ...]",
23e8ec0d 176#else
32cb0dd5 177 "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
2a9c8c36 178 " [ARG ...]",
23e8ec0d 179#endif
4ea42b18 180 "probe point definition, where\n"
af663d75
MH
181 "\t\tGROUP:\tGroup name (optional)\n"
182 "\t\tEVENT:\tEvent name\n"
4ea42b18 183 "\t\tFUNC:\tFunction name\n"
2a9c8c36 184 "\t\tOFF:\tOffset from function entry (in byte)\n"
253977b0 185 "\t\t%return:\tPut the probe at function return\n"
804b3606 186#ifdef NO_DWARF_SUPPORT
23e8ec0d
MH
187 "\t\tARG:\tProbe argument (only \n"
188#else
4ea42b18 189 "\t\tSRC:\tSource code path\n"
2a9c8c36
MH
190 "\t\tRL:\tRelative line number from function entry.\n"
191 "\t\tAL:\tAbsolute line number in file.\n"
192 "\t\tPT:\tLazy expression of line code.\n"
4ea42b18 193 "\t\tARG:\tProbe argument (local variable name or\n"
23e8ec0d 194#endif
50656eec 195 "\t\t\tkprobe-tracer argument format.)\n",
253977b0 196 opt_add_probe_event),
d761b08b
MH
197 OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
198 " with existing name"),
804b3606 199#ifndef NO_DWARF_SUPPORT
631c9def
MH
200 OPT_CALLBACK('L', "line", NULL,
201 "FUNC[:RLN[+NUM|:RLN2]]|SRC:ALN[+NUM|:ALN2]",
202 "Show source code lines.", opt_show_lines),
203#endif
4ea42b18
MH
204 OPT_END()
205};
206
631c9def
MH
207/* Initialize symbol maps for vmlinux */
208static void init_vmlinux(void)
209{
210 symbol_conf.sort_by_name = true;
211 if (symbol_conf.vmlinux_name == NULL)
212 symbol_conf.try_vmlinux_path = true;
213 else
214 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
215 if (symbol__init() < 0)
216 die("Failed to init symbol map.");
8ad94c60
ACM
217
218 map_groups__init(&session.kmap_groups);
219 if (map_groups__create_kernel_maps(&session.kmap_groups,
220 session.kmaps) < 0)
221 die("Failed to create kernel maps.");
631c9def
MH
222}
223
4ea42b18
MH
224int cmd_probe(int argc, const char **argv, const char *prefix __used)
225{
d1bde3f7 226 int i, ret;
804b3606 227#ifndef NO_DWARF_SUPPORT
bdad0db7
XG
228 int fd;
229#endif
4ea42b18 230 struct probe_point *pp;
4ea42b18
MH
231
232 argc = parse_options(argc, argv, options, probe_usage,
46ab4926 233 PARSE_OPT_STOP_AT_NON_OPTION);
ce11a603
MH
234 if (argc > 0) {
235 if (strcmp(argv[0], "-") == 0) {
236 pr_warning(" Error: '-' is not supported.\n");
237 usage_with_options(probe_usage, options);
238 }
d1bde3f7 239 parse_probe_event_argv(argc, argv);
ce11a603 240 }
46ab4926 241
631c9def
MH
242 if ((!session.nr_probe && !session.dellist && !session.list_events &&
243 !session.show_lines))
4ea42b18
MH
244 usage_with_options(probe_usage, options);
245
96c96612
MH
246 if (debugfs_valid_mountpoint(debugfs_path) < 0)
247 die("Failed to find debugfs path.");
248
fac13fd5 249 if (session.list_events) {
fa28244d
MH
250 if (session.nr_probe != 0 || session.dellist) {
251 pr_warning(" Error: Don't use --list with"
252 " --add/--del.\n");
253 usage_with_options(probe_usage, options);
254 }
631c9def
MH
255 if (session.show_lines) {
256 pr_warning(" Error: Don't use --list with --line.\n");
257 usage_with_options(probe_usage, options);
258 }
4de189fe
MH
259 show_perf_probe_events();
260 return 0;
261 }
262
804b3606 263#ifndef NO_DWARF_SUPPORT
631c9def
MH
264 if (session.show_lines) {
265 if (session.nr_probe != 0 || session.dellist) {
266 pr_warning(" Error: Don't use --line with"
267 " --add/--del.\n");
268 usage_with_options(probe_usage, options);
269 }
270 init_vmlinux();
271 fd = open_vmlinux();
272 if (fd < 0)
273 die("Could not open debuginfo file.");
274 ret = find_line_range(fd, &session.line_range);
275 if (ret <= 0)
276 die("Source line is not found.\n");
277 close(fd);
278 show_line_range(&session.line_range);
279 return 0;
280 }
281#endif
282
fa28244d
MH
283 if (session.dellist) {
284 del_trace_kprobe_events(session.dellist);
285 strlist__delete(session.dellist);
286 if (session.nr_probe == 0)
287 return 0;
288 }
289
631c9def
MH
290 /* Add probes */
291 init_vmlinux();
a128168d 292
23e8ec0d 293 if (session.need_dwarf)
804b3606 294#ifdef NO_DWARF_SUPPORT
50656eec 295 die("Debuginfo-analysis is not supported");
804b3606 296#else /* !NO_DWARF_SUPPORT */
f41b1e43 297 pr_debug("Some probes require debuginfo.\n");
4ea42b18 298
a128168d 299 fd = open_vmlinux();
a225a1d9
MH
300 if (fd < 0) {
301 if (session.need_dwarf)
f984f03d 302 die("Could not open debuginfo file.");
a225a1d9 303
d3a2dbf8
MH
304 pr_debug("Could not open vmlinux/module file."
305 " Try to use symbols.\n");
a225a1d9
MH
306 goto end_dwarf;
307 }
4ea42b18
MH
308
309 /* Searching probe points */
d1bde3f7
MH
310 for (i = 0; i < session.nr_probe; i++) {
311 pp = &session.probes[i];
4ea42b18
MH
312 if (pp->found)
313 continue;
314
315 lseek(fd, SEEK_SET, 0);
81cb8aa3 316 ret = find_probe_point(fd, pp);
411edfe5
MH
317 if (ret > 0)
318 continue;
7ef17aaf
MH
319 if (ret == 0) { /* No error but failed to find probe point. */
320 synthesize_perf_probe_point(pp);
321 die("Probe point '%s' not found. - probe not added.",
322 pp->probes[0]);
323 }
411edfe5
MH
324 /* Error path */
325 if (session.need_dwarf) {
326 if (ret == -ENOENT)
327 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
328 die("Could not analyze debuginfo.");
329 }
330 pr_debug("An error occurred in debuginfo analysis."
331 " Try to use symbols.\n");
332 break;
4ea42b18
MH
333 }
334 close(fd);
335
a225a1d9 336end_dwarf:
804b3606 337#endif /* !NO_DWARF_SUPPORT */
23e8ec0d 338
a225a1d9 339 /* Synthesize probes without dwarf */
d1bde3f7
MH
340 for (i = 0; i < session.nr_probe; i++) {
341 pp = &session.probes[i];
a225a1d9
MH
342 if (pp->found) /* This probe is already found. */
343 continue;
344
62bdc1b3 345 evaluate_probe_point(pp);
50656eec 346 ret = synthesize_trace_kprobe_event(pp);
a225a1d9 347 if (ret == -E2BIG)
50656eec 348 die("probe point definition becomes too long.");
a225a1d9
MH
349 else if (ret < 0)
350 die("Failed to synthesize a probe point.");
351 }
352
4ea42b18 353 /* Settng up probe points */
d761b08b
MH
354 add_trace_kprobe_events(session.probes, session.nr_probe,
355 session.force_add);
4ea42b18
MH
356 return 0;
357}
358
This page took 0.058698 seconds and 5 git commands to generate.