perf probe: Show accessible local variables
[linux-2.6.git] / 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/symbol.h"
40 #include "util/debug.h"
41 #include "util/debugfs.h"
42 #include "util/parse-options.h"
43 #include "util/probe-finder.h"
44 #include "util/probe-event.h"
45
46 #define MAX_PATH_LEN 256
47
48 /* Session management structure */
49 static struct {
50         bool list_events;
51         bool force_add;
52         bool show_lines;
53         bool show_vars;
54         bool mod_events;
55         int nevents;
56         struct perf_probe_event events[MAX_PROBES];
57         struct strlist *dellist;
58         struct line_range line_range;
59         int max_probe_points;
60 } params;
61
62 /* Parse an event definition. Note that any error must die. */
63 static int parse_probe_event(const char *str)
64 {
65         struct perf_probe_event *pev = &params.events[params.nevents];
66         int ret;
67
68         pr_debug("probe-definition(%d): %s\n", params.nevents, str);
69         if (++params.nevents == MAX_PROBES) {
70                 pr_err("Too many probes (> %d) were specified.", MAX_PROBES);
71                 return -1;
72         }
73
74         /* Parse a perf-probe command into event */
75         ret = parse_perf_probe_command(str, pev);
76         pr_debug("%d arguments\n", pev->nargs);
77
78         return ret;
79 }
80
81 static int parse_probe_event_argv(int argc, const char **argv)
82 {
83         int i, len, ret;
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 == NULL)
92                 return -ENOMEM;
93         len = 0;
94         for (i = 0; i < argc; i++)
95                 len += sprintf(&buf[len], "%s ", argv[i]);
96         params.mod_events = true;
97         ret = parse_probe_event(buf);
98         free(buf);
99         return ret;
100 }
101
102 static int opt_add_probe_event(const struct option *opt __used,
103                               const char *str, int unset __used)
104 {
105         if (str) {
106                 params.mod_events = true;
107                 return parse_probe_event(str);
108         } else
109                 return 0;
110 }
111
112 static int opt_del_probe_event(const struct option *opt __used,
113                                const char *str, int unset __used)
114 {
115         if (str) {
116                 params.mod_events = true;
117                 if (!params.dellist)
118                         params.dellist = strlist__new(true, NULL);
119                 strlist__add(params.dellist, str);
120         }
121         return 0;
122 }
123
124 #ifdef DWARF_SUPPORT
125 static int opt_show_lines(const struct option *opt __used,
126                           const char *str, int unset __used)
127 {
128         int ret = 0;
129
130         if (str)
131                 ret = parse_line_range_desc(str, &params.line_range);
132         INIT_LIST_HEAD(&params.line_range.line_list);
133         params.show_lines = true;
134
135         return ret;
136 }
137
138 static int opt_show_vars(const struct option *opt __used,
139                          const char *str, int unset __used)
140 {
141         struct perf_probe_event *pev = &params.events[params.nevents];
142         int ret;
143
144         if (!str)
145                 return 0;
146
147         ret = parse_probe_event(str);
148         if (!ret && pev->nargs != 0) {
149                 pr_err("  Error: '--vars' doesn't accept arguments.\n");
150                 return -EINVAL;
151         }
152         params.show_vars = true;
153
154         return ret;
155 }
156 #endif
157
158 static const char * const probe_usage[] = {
159         "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
160         "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
161         "perf probe [<options>] --del '[GROUP:]EVENT' ...",
162         "perf probe --list",
163 #ifdef DWARF_SUPPORT
164         "perf probe --line 'LINEDESC'",
165         "perf probe --vars 'PROBEPOINT'",
166 #endif
167         NULL
168 };
169
170 static const struct option options[] = {
171         OPT_INCR('v', "verbose", &verbose,
172                     "be more verbose (show parsed arguments, etc)"),
173         OPT_BOOLEAN('l', "list", &params.list_events,
174                     "list up current probe events"),
175         OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
176                 opt_del_probe_event),
177         OPT_CALLBACK('a', "add", NULL,
178 #ifdef DWARF_SUPPORT
179                 "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
180                 " [[NAME=]ARG ...]",
181 #else
182                 "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]",
183 #endif
184                 "probe point definition, where\n"
185                 "\t\tGROUP:\tGroup name (optional)\n"
186                 "\t\tEVENT:\tEvent name\n"
187                 "\t\tFUNC:\tFunction name\n"
188                 "\t\tOFF:\tOffset from function entry (in byte)\n"
189                 "\t\t%return:\tPut the probe at function return\n"
190 #ifdef DWARF_SUPPORT
191                 "\t\tSRC:\tSource code path\n"
192                 "\t\tRL:\tRelative line number from function entry.\n"
193                 "\t\tAL:\tAbsolute line number in file.\n"
194                 "\t\tPT:\tLazy expression of line code.\n"
195                 "\t\tARG:\tProbe argument (local variable name or\n"
196                 "\t\t\tkprobe-tracer argument format.)\n",
197 #else
198                 "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
199 #endif
200                 opt_add_probe_event),
201         OPT_BOOLEAN('f', "force", &params.force_add, "forcibly add events"
202                     " with existing name"),
203 #ifdef DWARF_SUPPORT
204         OPT_CALLBACK('L', "line", NULL,
205                      "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
206                      "Show source code lines.", opt_show_lines),
207         OPT_CALLBACK('V', "vars", NULL,
208                      "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT",
209                      "Show accessible variables on PROBEDEF", opt_show_vars),
210         OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
211                    "file", "vmlinux pathname"),
212         OPT_STRING('s', "source", &symbol_conf.source_prefix,
213                    "directory", "path to kernel source"),
214 #endif
215         OPT__DRY_RUN(&probe_event_dry_run),
216         OPT_INTEGER('\0', "max-probes", &params.max_probe_points,
217                  "Set how many probe points can be found for a probe."),
218         OPT_END()
219 };
220
221 int cmd_probe(int argc, const char **argv, const char *prefix __used)
222 {
223         int ret;
224
225         argc = parse_options(argc, argv, options, probe_usage,
226                              PARSE_OPT_STOP_AT_NON_OPTION);
227         if (argc > 0) {
228                 if (strcmp(argv[0], "-") == 0) {
229                         pr_warning("  Error: '-' is not supported.\n");
230                         usage_with_options(probe_usage, options);
231                 }
232                 ret = parse_probe_event_argv(argc, argv);
233                 if (ret < 0) {
234                         pr_err("  Error: Parse Error.  (%d)\n", ret);
235                         return ret;
236                 }
237         }
238
239         if (params.max_probe_points == 0)
240                 params.max_probe_points = MAX_PROBES;
241
242         if ((!params.nevents && !params.dellist && !params.list_events &&
243              !params.show_lines))
244                 usage_with_options(probe_usage, options);
245
246         if (params.list_events) {
247                 if (params.mod_events) {
248                         pr_err("  Error: Don't use --list with --add/--del.\n");
249                         usage_with_options(probe_usage, options);
250                 }
251                 if (params.show_lines) {
252                         pr_err("  Error: Don't use --list with --line.\n");
253                         usage_with_options(probe_usage, options);
254                 }
255                 if (params.show_vars) {
256                         pr_err(" Error: Don't use --list with --vars.\n");
257                         usage_with_options(probe_usage, options);
258                 }
259                 ret = show_perf_probe_events();
260                 if (ret < 0)
261                         pr_err("  Error: Failed to show event list. (%d)\n",
262                                ret);
263                 return ret;
264         }
265
266 #ifdef DWARF_SUPPORT
267         if (params.show_lines) {
268                 if (params.mod_events) {
269                         pr_err("  Error: Don't use --line with"
270                                " --add/--del.\n");
271                         usage_with_options(probe_usage, options);
272                 }
273                 if (params.show_vars) {
274                         pr_err(" Error: Don't use --line with --vars.\n");
275                         usage_with_options(probe_usage, options);
276                 }
277
278                 ret = show_line_range(&params.line_range);
279                 if (ret < 0)
280                         pr_err("  Error: Failed to show lines. (%d)\n", ret);
281                 return ret;
282         }
283         if (params.show_vars) {
284                 if (params.mod_events) {
285                         pr_err("  Error: Don't use --vars with"
286                                " --add/--del.\n");
287                         usage_with_options(probe_usage, options);
288                 }
289                 ret = show_available_vars(params.events, params.nevents,
290                                           params.max_probe_points);
291                 if (ret < 0)
292                         pr_err("  Error: Failed to show vars. (%d)\n", ret);
293                 return ret;
294         }
295 #endif
296
297         if (params.dellist) {
298                 ret = del_perf_probe_events(params.dellist);
299                 strlist__delete(params.dellist);
300                 if (ret < 0) {
301                         pr_err("  Error: Failed to delete events. (%d)\n", ret);
302                         return ret;
303                 }
304         }
305
306         if (params.nevents) {
307                 ret = add_perf_probe_events(params.events, params.nevents,
308                                             params.force_add,
309                                             params.max_probe_points);
310                 if (ret < 0) {
311                         pr_err("  Error: Failed to add events. (%d)\n", ret);
312                         return ret;
313                 }
314         }
315         return 0;
316 }