perf tools: make -C consistent across commands (for cpu list arg)
[deliverable/linux.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/strfilter.h"
40 #include "util/symbol.h"
41 #include "util/debug.h"
42 #include "util/debugfs.h"
43 #include "util/parse-options.h"
44 #include "util/probe-finder.h"
45 #include "util/probe-event.h"
46
47 #define DEFAULT_VAR_FILTER "!__k???tab_* & !__crc_*"
48 #define DEFAULT_FUNC_FILTER "!_*"
49
50 /* Session management structure */
51 static struct {
52 bool list_events;
53 bool force_add;
54 bool show_lines;
55 bool show_vars;
56 bool show_ext_vars;
57 bool show_funcs;
58 bool mod_events;
59 int nevents;
60 struct perf_probe_event events[MAX_PROBES];
61 struct strlist *dellist;
62 struct line_range line_range;
63 const char *target_module;
64 int max_probe_points;
65 struct strfilter *filter;
66 } params;
67
68 /* Parse an event definition. Note that any error must die. */
69 static int parse_probe_event(const char *str)
70 {
71 struct perf_probe_event *pev = &params.events[params.nevents];
72 int ret;
73
74 pr_debug("probe-definition(%d): %s\n", params.nevents, str);
75 if (++params.nevents == MAX_PROBES) {
76 pr_err("Too many probes (> %d) were specified.", MAX_PROBES);
77 return -1;
78 }
79
80 /* Parse a perf-probe command into event */
81 ret = parse_perf_probe_command(str, pev);
82 pr_debug("%d arguments\n", pev->nargs);
83
84 return ret;
85 }
86
87 static int parse_probe_event_argv(int argc, const char **argv)
88 {
89 int i, len, ret;
90 char *buf;
91
92 /* Bind up rest arguments */
93 len = 0;
94 for (i = 0; i < argc; i++)
95 len += strlen(argv[i]) + 1;
96 buf = zalloc(len + 1);
97 if (buf == NULL)
98 return -ENOMEM;
99 len = 0;
100 for (i = 0; i < argc; i++)
101 len += sprintf(&buf[len], "%s ", argv[i]);
102 params.mod_events = true;
103 ret = parse_probe_event(buf);
104 free(buf);
105 return ret;
106 }
107
108 static int opt_add_probe_event(const struct option *opt __used,
109 const char *str, int unset __used)
110 {
111 if (str) {
112 params.mod_events = true;
113 return parse_probe_event(str);
114 } else
115 return 0;
116 }
117
118 static int opt_del_probe_event(const struct option *opt __used,
119 const char *str, int unset __used)
120 {
121 if (str) {
122 params.mod_events = true;
123 if (!params.dellist)
124 params.dellist = strlist__new(true, NULL);
125 strlist__add(params.dellist, str);
126 }
127 return 0;
128 }
129
130 #ifdef DWARF_SUPPORT
131 static int opt_show_lines(const struct option *opt __used,
132 const char *str, int unset __used)
133 {
134 int ret = 0;
135
136 if (!str)
137 return 0;
138
139 if (params.show_lines) {
140 pr_warning("Warning: more than one --line options are"
141 " detected. Only the first one is valid.\n");
142 return 0;
143 }
144
145 params.show_lines = true;
146 ret = parse_line_range_desc(str, &params.line_range);
147 INIT_LIST_HEAD(&params.line_range.line_list);
148
149 return ret;
150 }
151
152 static int opt_show_vars(const struct option *opt __used,
153 const char *str, int unset __used)
154 {
155 struct perf_probe_event *pev = &params.events[params.nevents];
156 int ret;
157
158 if (!str)
159 return 0;
160
161 ret = parse_probe_event(str);
162 if (!ret && pev->nargs != 0) {
163 pr_err(" Error: '--vars' doesn't accept arguments.\n");
164 return -EINVAL;
165 }
166 params.show_vars = true;
167
168 return ret;
169 }
170 #endif
171
172 static int opt_set_filter(const struct option *opt __used,
173 const char *str, int unset __used)
174 {
175 const char *err;
176
177 if (str) {
178 pr_debug2("Set filter: %s\n", str);
179 if (params.filter)
180 strfilter__delete(params.filter);
181 params.filter = strfilter__new(str, &err);
182 if (!params.filter) {
183 pr_err("Filter parse error at %td.\n", err - str + 1);
184 pr_err("Source: \"%s\"\n", str);
185 pr_err(" %*c\n", (int)(err - str + 1), '^');
186 return -EINVAL;
187 }
188 }
189
190 return 0;
191 }
192
193 static const char * const probe_usage[] = {
194 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
195 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
196 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
197 "perf probe --list",
198 #ifdef DWARF_SUPPORT
199 "perf probe [<options>] --line 'LINEDESC'",
200 "perf probe [<options>] --vars 'PROBEPOINT'",
201 #endif
202 NULL
203 };
204
205 static const struct option options[] = {
206 OPT_INCR('v', "verbose", &verbose,
207 "be more verbose (show parsed arguments, etc)"),
208 OPT_BOOLEAN('l', "list", &params.list_events,
209 "list up current probe events"),
210 OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
211 opt_del_probe_event),
212 OPT_CALLBACK('a', "add", NULL,
213 #ifdef DWARF_SUPPORT
214 "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
215 " [[NAME=]ARG ...]",
216 #else
217 "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]",
218 #endif
219 "probe point definition, where\n"
220 "\t\tGROUP:\tGroup name (optional)\n"
221 "\t\tEVENT:\tEvent name\n"
222 "\t\tFUNC:\tFunction name\n"
223 "\t\tOFF:\tOffset from function entry (in byte)\n"
224 "\t\t%return:\tPut the probe at function return\n"
225 #ifdef DWARF_SUPPORT
226 "\t\tSRC:\tSource code path\n"
227 "\t\tRL:\tRelative line number from function entry.\n"
228 "\t\tAL:\tAbsolute line number in file.\n"
229 "\t\tPT:\tLazy expression of line code.\n"
230 "\t\tARG:\tProbe argument (local variable name or\n"
231 "\t\t\tkprobe-tracer argument format.)\n",
232 #else
233 "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
234 #endif
235 opt_add_probe_event),
236 OPT_BOOLEAN('f', "force", &params.force_add, "forcibly add events"
237 " with existing name"),
238 #ifdef DWARF_SUPPORT
239 OPT_CALLBACK('L', "line", NULL,
240 "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
241 "Show source code lines.", opt_show_lines),
242 OPT_CALLBACK('V', "vars", NULL,
243 "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT",
244 "Show accessible variables on PROBEDEF", opt_show_vars),
245 OPT_BOOLEAN('\0', "externs", &params.show_ext_vars,
246 "Show external variables too (with --vars only)"),
247 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
248 "file", "vmlinux pathname"),
249 OPT_STRING('s', "source", &symbol_conf.source_prefix,
250 "directory", "path to kernel source"),
251 OPT_STRING('m', "module", &params.target_module,
252 "modname|path",
253 "target module name (for online) or path (for offline)"),
254 #endif
255 OPT__DRY_RUN(&probe_event_dry_run),
256 OPT_INTEGER('\0', "max-probes", &params.max_probe_points,
257 "Set how many probe points can be found for a probe."),
258 OPT_BOOLEAN('F', "funcs", &params.show_funcs,
259 "Show potential probe-able functions."),
260 OPT_CALLBACK('\0', "filter", NULL,
261 "[!]FILTER", "Set a filter (with --vars/funcs only)\n"
262 "\t\t\t(default: \"" DEFAULT_VAR_FILTER "\" for --vars,\n"
263 "\t\t\t \"" DEFAULT_FUNC_FILTER "\" for --funcs)",
264 opt_set_filter),
265 OPT_END()
266 };
267
268 int cmd_probe(int argc, const char **argv, const char *prefix __used)
269 {
270 int ret;
271
272 argc = parse_options(argc, argv, options, probe_usage,
273 PARSE_OPT_STOP_AT_NON_OPTION);
274 if (argc > 0) {
275 if (strcmp(argv[0], "-") == 0) {
276 pr_warning(" Error: '-' is not supported.\n");
277 usage_with_options(probe_usage, options);
278 }
279 ret = parse_probe_event_argv(argc, argv);
280 if (ret < 0) {
281 pr_err(" Error: Parse Error. (%d)\n", ret);
282 return ret;
283 }
284 }
285
286 if (params.max_probe_points == 0)
287 params.max_probe_points = MAX_PROBES;
288
289 if ((!params.nevents && !params.dellist && !params.list_events &&
290 !params.show_lines && !params.show_funcs))
291 usage_with_options(probe_usage, options);
292
293 /*
294 * Only consider the user's kernel image path if given.
295 */
296 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
297
298 if (params.list_events) {
299 if (params.mod_events) {
300 pr_err(" Error: Don't use --list with --add/--del.\n");
301 usage_with_options(probe_usage, options);
302 }
303 if (params.show_lines) {
304 pr_err(" Error: Don't use --list with --line.\n");
305 usage_with_options(probe_usage, options);
306 }
307 if (params.show_vars) {
308 pr_err(" Error: Don't use --list with --vars.\n");
309 usage_with_options(probe_usage, options);
310 }
311 if (params.show_funcs) {
312 pr_err(" Error: Don't use --list with --funcs.\n");
313 usage_with_options(probe_usage, options);
314 }
315 ret = show_perf_probe_events();
316 if (ret < 0)
317 pr_err(" Error: Failed to show event list. (%d)\n",
318 ret);
319 return ret;
320 }
321 if (params.show_funcs) {
322 if (params.nevents != 0 || params.dellist) {
323 pr_err(" Error: Don't use --funcs with"
324 " --add/--del.\n");
325 usage_with_options(probe_usage, options);
326 }
327 if (params.show_lines) {
328 pr_err(" Error: Don't use --funcs with --line.\n");
329 usage_with_options(probe_usage, options);
330 }
331 if (params.show_vars) {
332 pr_err(" Error: Don't use --funcs with --vars.\n");
333 usage_with_options(probe_usage, options);
334 }
335 if (!params.filter)
336 params.filter = strfilter__new(DEFAULT_FUNC_FILTER,
337 NULL);
338 ret = show_available_funcs(params.target_module,
339 params.filter);
340 strfilter__delete(params.filter);
341 if (ret < 0)
342 pr_err(" Error: Failed to show functions."
343 " (%d)\n", ret);
344 return ret;
345 }
346
347 #ifdef DWARF_SUPPORT
348 if (params.show_lines) {
349 if (params.mod_events) {
350 pr_err(" Error: Don't use --line with"
351 " --add/--del.\n");
352 usage_with_options(probe_usage, options);
353 }
354 if (params.show_vars) {
355 pr_err(" Error: Don't use --line with --vars.\n");
356 usage_with_options(probe_usage, options);
357 }
358
359 ret = show_line_range(&params.line_range, params.target_module);
360 if (ret < 0)
361 pr_err(" Error: Failed to show lines. (%d)\n", ret);
362 return ret;
363 }
364 if (params.show_vars) {
365 if (params.mod_events) {
366 pr_err(" Error: Don't use --vars with"
367 " --add/--del.\n");
368 usage_with_options(probe_usage, options);
369 }
370 if (!params.filter)
371 params.filter = strfilter__new(DEFAULT_VAR_FILTER,
372 NULL);
373
374 ret = show_available_vars(params.events, params.nevents,
375 params.max_probe_points,
376 params.target_module,
377 params.filter,
378 params.show_ext_vars);
379 strfilter__delete(params.filter);
380 if (ret < 0)
381 pr_err(" Error: Failed to show vars. (%d)\n", ret);
382 return ret;
383 }
384 #endif
385
386 if (params.dellist) {
387 ret = del_perf_probe_events(params.dellist);
388 strlist__delete(params.dellist);
389 if (ret < 0) {
390 pr_err(" Error: Failed to delete events. (%d)\n", ret);
391 return ret;
392 }
393 }
394
395 if (params.nevents) {
396 ret = add_perf_probe_events(params.events, params.nevents,
397 params.max_probe_points,
398 params.target_module,
399 params.force_add);
400 if (ret < 0) {
401 pr_err(" Error: Failed to add events. (%d)\n", ret);
402 return ret;
403 }
404 }
405 return 0;
406 }
This page took 0.08702 seconds and 5 git commands to generate.