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