perf probe: Use wrapper functions
[safe/jmp/linux-2.6] / tools / perf / builtin-probe.c
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"
38 #include "util/strlist.h"
39 #include "util/event.h"
40 #include "util/debug.h"
41 #include "util/debugfs.h"
42 #include "util/symbol.h"
43 #include "util/thread.h"
44 #include "util/parse-options.h"
45 #include "util/parse-events.h"  /* For debugfs_path */
46 #include "util/probe-finder.h"
47 #include "util/probe-event.h"
48
49 #define MAX_PATH_LEN 256
50
51 /* Session management structure */
52 static struct {
53         bool need_dwarf;
54         bool list_events;
55         bool force_add;
56         bool show_lines;
57         int nr_probe;
58         struct probe_point probes[MAX_PROBES];
59         struct strlist *dellist;
60         struct map_groups kmap_groups;
61         struct map *kmaps[MAP__NR_TYPES];
62         struct line_range line_range;
63 } session;
64
65
66 /* Parse an event definition. Note that any error must die. */
67 static void parse_probe_event(const char *str)
68 {
69         struct probe_point *pp = &session.probes[session.nr_probe];
70
71         pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
72         if (++session.nr_probe == MAX_PROBES)
73                 die("Too many probes (> %d) are specified.", MAX_PROBES);
74
75         /* Parse perf-probe event into probe_point */
76         parse_perf_probe_event(str, pp, &session.need_dwarf);
77
78         pr_debug("%d arguments\n", pp->nr_args);
79 }
80
81 static 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 = xzalloc(len + 1);
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
98 static int opt_add_probe_event(const struct option *opt __used,
99                               const char *str, int unset __used)
100 {
101         if (str)
102                 parse_probe_event(str);
103         return 0;
104 }
105
106 static 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
117 /* Currently just checking function name from symbol map */
118 static void evaluate_probe_point(struct probe_point *pp)
119 {
120         struct symbol *sym;
121         sym = map__find_symbol_by_name(session.kmaps[MAP__FUNCTION],
122                                        pp->function, NULL);
123         if (!sym)
124                 die("Kernel symbol \'%s\' not found - probe not added.",
125                     pp->function);
126 }
127
128 #ifndef NO_DWARF_SUPPORT
129 static int open_vmlinux(void)
130 {
131         if (map__load(session.kmaps[MAP__FUNCTION], NULL) < 0) {
132                 pr_debug("Failed to load kernel map.\n");
133                 return -EINVAL;
134         }
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);
138 }
139
140 static 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 }
149 #endif
150
151 static const char * const probe_usage[] = {
152         "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
153         "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
154         "perf probe [<options>] --del '[GROUP:]EVENT' ...",
155         "perf probe --list",
156 #ifndef NO_DWARF_SUPPORT
157         "perf probe --line 'LINEDESC'",
158 #endif
159         NULL
160 };
161
162 static const struct option options[] = {
163         OPT_BOOLEAN('v', "verbose", &verbose,
164                     "be more verbose (show parsed arguments, etc)"),
165 #ifndef NO_DWARF_SUPPORT
166         OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
167                    "file", "vmlinux pathname"),
168 #endif
169         OPT_BOOLEAN('l', "list", &session.list_events,
170                     "list up current probe events"),
171         OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
172                 opt_del_probe_event),
173         OPT_CALLBACK('a', "add", NULL,
174 #ifdef NO_DWARF_SUPPORT
175                 "[EVENT=]FUNC[+OFF|%return] [ARG ...]",
176 #else
177                 "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
178                 " [ARG ...]",
179 #endif
180                 "probe point definition, where\n"
181                 "\t\tGROUP:\tGroup name (optional)\n"
182                 "\t\tEVENT:\tEvent name\n"
183                 "\t\tFUNC:\tFunction name\n"
184                 "\t\tOFF:\tOffset from function entry (in byte)\n"
185                 "\t\t%return:\tPut the probe at function return\n"
186 #ifdef NO_DWARF_SUPPORT
187                 "\t\tARG:\tProbe argument (only \n"
188 #else
189                 "\t\tSRC:\tSource code path\n"
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"
193                 "\t\tARG:\tProbe argument (local variable name or\n"
194 #endif
195                 "\t\t\tkprobe-tracer argument format.)\n",
196                 opt_add_probe_event),
197         OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
198                     " with existing name"),
199 #ifndef NO_DWARF_SUPPORT
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
204         OPT_END()
205 };
206
207 /* Initialize symbol maps for vmlinux */
208 static 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.");
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.");
222 }
223
224 int cmd_probe(int argc, const char **argv, const char *prefix __used)
225 {
226         int i, ret;
227 #ifndef NO_DWARF_SUPPORT
228         int fd;
229 #endif
230         struct probe_point *pp;
231
232         argc = parse_options(argc, argv, options, probe_usage,
233                              PARSE_OPT_STOP_AT_NON_OPTION);
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                 }
239                 parse_probe_event_argv(argc, argv);
240         }
241
242         if ((!session.nr_probe && !session.dellist && !session.list_events &&
243              !session.show_lines))
244                 usage_with_options(probe_usage, options);
245
246         if (debugfs_valid_mountpoint(debugfs_path) < 0)
247                 die("Failed to find debugfs path.");
248
249         if (session.list_events) {
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                 }
255                 if (session.show_lines) {
256                         pr_warning("  Error: Don't use --list with --line.\n");
257                         usage_with_options(probe_usage, options);
258                 }
259                 show_perf_probe_events();
260                 return 0;
261         }
262
263 #ifndef NO_DWARF_SUPPORT
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
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
290         /* Add probes */
291         init_vmlinux();
292
293         if (session.need_dwarf)
294 #ifdef NO_DWARF_SUPPORT
295                 die("Debuginfo-analysis is not supported");
296 #else   /* !NO_DWARF_SUPPORT */
297                 pr_debug("Some probes require debuginfo.\n");
298
299         fd = open_vmlinux();
300         if (fd < 0) {
301                 if (session.need_dwarf)
302                         die("Could not open debuginfo file.");
303
304                 pr_debug("Could not open vmlinux/module file."
305                          " Try to use symbols.\n");
306                 goto end_dwarf;
307         }
308
309         /* Searching probe points */
310         for (i = 0; i < session.nr_probe; i++) {
311                 pp = &session.probes[i];
312                 if (pp->found)
313                         continue;
314
315                 lseek(fd, SEEK_SET, 0);
316                 ret = find_probe_point(fd, pp);
317                 if (ret > 0)
318                         continue;
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                 }
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;
333         }
334         close(fd);
335
336 end_dwarf:
337 #endif /* !NO_DWARF_SUPPORT */
338
339         /* Synthesize probes without dwarf */
340         for (i = 0; i < session.nr_probe; i++) {
341                 pp = &session.probes[i];
342                 if (pp->found)  /* This probe is already found. */
343                         continue;
344
345                 evaluate_probe_point(pp);
346                 ret = synthesize_trace_kprobe_event(pp);
347                 if (ret == -E2BIG)
348                         die("probe point definition becomes too long.");
349                 else if (ret < 0)
350                         die("Failed to synthesize a probe point.");
351         }
352
353         /* Settng up probe points */
354         add_trace_kprobe_events(session.probes, session.nr_probe,
355                                 session.force_add);
356         return 0;
357 }
358