mmc: sdhci: Check mrq->cmd in sdhci_tasklet_finish
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / builtin-probe.c
blobfa626eb9c878569f6df8cb15ae0aeb11977c1559
1 /*
2 * builtin-probe.c
4 * Builtin probe command: Set up probe events by C expression
6 * Written by Masami Hiramatsu <mhiramat@redhat.com>
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.
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.
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.
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>
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/session.h"
45 #include "util/parse-options.h"
46 #include "util/parse-events.h" /* For debugfs_path */
47 #include "util/probe-finder.h"
48 #include "util/probe-event.h"
50 #define MAX_PATH_LEN 256
52 /* Session management structure */
53 static struct {
54 bool need_dwarf;
55 bool list_events;
56 bool force_add;
57 int nr_probe;
58 struct probe_point probes[MAX_PROBES];
59 struct strlist *dellist;
60 struct perf_session *psession;
61 struct map *kmap;
62 } session;
65 /* Parse an event definition. Note that any error must die. */
66 static void parse_probe_event(const char *str)
68 struct probe_point *pp = &session.probes[session.nr_probe];
70 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
71 if (++session.nr_probe == MAX_PROBES)
72 die("Too many probes (> %d) are specified.", MAX_PROBES);
74 /* Parse perf-probe event into probe_point */
75 parse_perf_probe_event(str, pp, &session.need_dwarf);
77 pr_debug("%d arguments\n", pp->nr_args);
80 static void parse_probe_event_argv(int argc, const char **argv)
82 int i, len;
83 char *buf;
85 /* Bind up rest arguments */
86 len = 0;
87 for (i = 0; i < argc; i++)
88 len += strlen(argv[i]) + 1;
89 buf = zalloc(len + 1);
90 if (!buf)
91 die("Failed to allocate memory for binding arguments.");
92 len = 0;
93 for (i = 0; i < argc; i++)
94 len += sprintf(&buf[len], "%s ", argv[i]);
95 parse_probe_event(buf);
96 free(buf);
99 static int opt_add_probe_event(const struct option *opt __used,
100 const char *str, int unset __used)
102 if (str)
103 parse_probe_event(str);
104 return 0;
107 static int opt_del_probe_event(const struct option *opt __used,
108 const char *str, int unset __used)
110 if (str) {
111 if (!session.dellist)
112 session.dellist = strlist__new(true, NULL);
113 strlist__add(session.dellist, str);
115 return 0;
118 /* Currently just checking function name from symbol map */
119 static void evaluate_probe_point(struct probe_point *pp)
121 struct symbol *sym;
122 sym = map__find_symbol_by_name(session.kmap, pp->function,
123 session.psession, NULL);
124 if (!sym)
125 die("Kernel symbol \'%s\' not found - probe not added.",
126 pp->function);
129 #ifndef NO_LIBDWARF
130 static int open_vmlinux(void)
132 if (map__load(session.kmap, session.psession, NULL) < 0) {
133 pr_debug("Failed to load kernel map.\n");
134 return -EINVAL;
136 pr_debug("Try to open %s\n", session.kmap->dso->long_name);
137 return open(session.kmap->dso->long_name, O_RDONLY);
139 #endif
141 static const char * const probe_usage[] = {
142 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
143 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
144 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
145 "perf probe --list",
146 NULL
149 static const struct option options[] = {
150 OPT_BOOLEAN('v', "verbose", &verbose,
151 "be more verbose (show parsed arguments, etc)"),
152 #ifndef NO_LIBDWARF
153 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
154 "file", "vmlinux pathname"),
155 #endif
156 OPT_BOOLEAN('l', "list", &session.list_events,
157 "list up current probe events"),
158 OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
159 opt_del_probe_event),
160 OPT_CALLBACK('a', "add", NULL,
161 #ifdef NO_LIBDWARF
162 "[EVENT=]FUNC[+OFFS|%return] [ARG ...]",
163 #else
164 "[EVENT=]FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
165 #endif
166 "probe point definition, where\n"
167 "\t\tGROUP:\tGroup name (optional)\n"
168 "\t\tEVENT:\tEvent name\n"
169 "\t\tFUNC:\tFunction name\n"
170 "\t\tOFFS:\tOffset from function entry (in byte)\n"
171 "\t\t%return:\tPut the probe at function return\n"
172 #ifdef NO_LIBDWARF
173 "\t\tARG:\tProbe argument (only \n"
174 #else
175 "\t\tSRC:\tSource code path\n"
176 "\t\tRLN:\tRelative line number from function entry.\n"
177 "\t\tALN:\tAbsolute line number in file.\n"
178 "\t\tARG:\tProbe argument (local variable name or\n"
179 #endif
180 "\t\t\tkprobe-tracer argument format.)\n",
181 opt_add_probe_event),
182 OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
183 " with existing name"),
184 OPT_END()
187 int cmd_probe(int argc, const char **argv, const char *prefix __used)
189 int i, ret;
190 #ifndef NO_LIBDWARF
191 int fd;
192 #endif
193 struct probe_point *pp;
195 argc = parse_options(argc, argv, options, probe_usage,
196 PARSE_OPT_STOP_AT_NON_OPTION);
197 if (argc > 0) {
198 if (strcmp(argv[0], "-") == 0) {
199 pr_warning(" Error: '-' is not supported.\n");
200 usage_with_options(probe_usage, options);
202 parse_probe_event_argv(argc, argv);
205 if ((!session.nr_probe && !session.dellist && !session.list_events))
206 usage_with_options(probe_usage, options);
208 if (debugfs_valid_mountpoint(debugfs_path) < 0)
209 die("Failed to find debugfs path.");
211 if (session.list_events) {
212 if (session.nr_probe != 0 || session.dellist) {
213 pr_warning(" Error: Don't use --list with"
214 " --add/--del.\n");
215 usage_with_options(probe_usage, options);
217 show_perf_probe_events();
218 return 0;
221 if (session.dellist) {
222 del_trace_kprobe_events(session.dellist);
223 strlist__delete(session.dellist);
224 if (session.nr_probe == 0)
225 return 0;
228 /* Initialize symbol maps for vmlinux */
229 symbol_conf.sort_by_name = true;
230 if (symbol_conf.vmlinux_name == NULL)
231 symbol_conf.try_vmlinux_path = true;
232 if (symbol__init() < 0)
233 die("Failed to init symbol map.");
234 session.psession = perf_session__new(NULL, O_WRONLY, false);
235 if (session.psession == NULL)
236 die("Failed to init perf_session.");
237 session.kmap = map_groups__find_by_name(&session.psession->kmaps,
238 MAP__FUNCTION,
239 "[kernel.kallsyms]");
240 if (!session.kmap)
241 die("Could not find kernel map.\n");
243 if (session.need_dwarf)
244 #ifdef NO_LIBDWARF
245 die("Debuginfo-analysis is not supported");
246 #else /* !NO_LIBDWARF */
247 pr_debug("Some probes require debuginfo.\n");
249 fd = open_vmlinux();
250 if (fd < 0) {
251 if (session.need_dwarf)
252 die("Could not open debuginfo file.");
254 pr_debug("Could not open vmlinux/module file."
255 " Try to use symbols.\n");
256 goto end_dwarf;
259 /* Searching probe points */
260 for (i = 0; i < session.nr_probe; i++) {
261 pp = &session.probes[i];
262 if (pp->found)
263 continue;
265 lseek(fd, SEEK_SET, 0);
266 ret = find_probepoint(fd, pp);
267 if (ret > 0)
268 continue;
269 if (ret == 0) { /* No error but failed to find probe point. */
270 synthesize_perf_probe_point(pp);
271 die("Probe point '%s' not found. - probe not added.",
272 pp->probes[0]);
274 /* Error path */
275 if (session.need_dwarf) {
276 if (ret == -ENOENT)
277 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
278 die("Could not analyze debuginfo.");
280 pr_debug("An error occurred in debuginfo analysis."
281 " Try to use symbols.\n");
282 break;
284 close(fd);
286 end_dwarf:
287 #endif /* !NO_LIBDWARF */
289 /* Synthesize probes without dwarf */
290 for (i = 0; i < session.nr_probe; i++) {
291 pp = &session.probes[i];
292 if (pp->found) /* This probe is already found. */
293 continue;
295 evaluate_probe_point(pp);
296 ret = synthesize_trace_kprobe_event(pp);
297 if (ret == -E2BIG)
298 die("probe point definition becomes too long.");
299 else if (ret < 0)
300 die("Failed to synthesize a probe point.");
303 /* Settng up probe points */
304 add_trace_kprobe_events(session.probes, session.nr_probe,
305 session.force_add);
306 return 0;