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.
24 #include <sys/utsname.h>
25 #include <sys/types.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"
49 #define MAX_PATH_LEN 256
50 #define MAX_PROBES 128
52 /* Session management structure */
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
;
67 /* Parse an event definition. Note that any error must die. */
68 static void parse_probe_event(const char *str
)
70 struct probe_point
*pp
= &session
.probes
[session
.nr_probe
];
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
);
76 /* Parse perf-probe event into probe_point */
77 parse_perf_probe_event(str
, pp
, &session
.need_dwarf
);
79 pr_debug("%d arguments\n", pp
->nr_args
);
82 static void parse_probe_event_argv(int argc
, const char **argv
)
87 /* Bind up rest arguments */
89 for (i
= 0; i
< argc
; i
++)
90 len
+= strlen(argv
[i
]) + 1;
91 buf
= zalloc(len
+ 1);
93 die("Failed to allocate memory for binding arguments.");
95 for (i
= 0; i
< argc
; i
++)
96 len
+= sprintf(&buf
[len
], "%s ", argv
[i
]);
97 parse_probe_event(buf
);
101 static int opt_add_probe_event(const struct option
*opt __used
,
102 const char *str
, int unset __used
)
105 parse_probe_event(str
);
109 static int opt_del_probe_event(const struct option
*opt __used
,
110 const char *str
, int unset __used
)
113 if (!session
.dellist
)
114 session
.dellist
= strlist__new(true, NULL
);
115 strlist__add(session
.dellist
, str
);
120 /* Currently just checking function name from symbol map */
121 static void evaluate_probe_point(struct probe_point
*pp
)
124 sym
= map__find_symbol_by_name(session
.kmaps
[MAP__FUNCTION
],
127 die("Kernel symbol \'%s\' not found - probe not added.",
131 #ifndef NO_DWARF_SUPPORT
132 static int open_vmlinux(void)
134 if (map__load(session
.kmaps
[MAP__FUNCTION
], NULL
) < 0) {
135 pr_debug("Failed to load kernel map.\n");
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
);
143 static int opt_show_lines(const struct option
*opt __used
,
144 const char *str
, int unset __used
)
147 parse_line_range_desc(str
, &session
.line_range
);
148 INIT_LIST_HEAD(&session
.line_range
.line_list
);
149 session
.show_lines
= true;
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' ...",
159 #ifndef NO_DWARF_SUPPORT
160 "perf probe --line 'LINEDESC'",
165 static const struct option options
[] = {
166 OPT_BOOLEAN('v', "verbose", &verbose
,
167 "be more verbose (show parsed arguments, etc)"),
168 #ifndef NO_DWARF_SUPPORT
169 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
170 "file", "vmlinux pathname"),
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_DWARF_SUPPORT
178 "[EVENT=]FUNC[+OFF|%return] [ARG ...]",
180 "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
183 "probe point definition, where\n"
184 "\t\tGROUP:\tGroup name (optional)\n"
185 "\t\tEVENT:\tEvent name\n"
186 "\t\tFUNC:\tFunction name\n"
187 "\t\tOFF:\tOffset from function entry (in byte)\n"
188 "\t\t%return:\tPut the probe at function return\n"
189 #ifdef NO_DWARF_SUPPORT
190 "\t\tARG:\tProbe argument (only \n"
192 "\t\tSRC:\tSource code path\n"
193 "\t\tRL:\tRelative line number from function entry.\n"
194 "\t\tAL:\tAbsolute line number in file.\n"
195 "\t\tPT:\tLazy expression of line code.\n"
196 "\t\tARG:\tProbe argument (local variable name or\n"
198 "\t\t\tkprobe-tracer argument format.)\n",
199 opt_add_probe_event
),
200 OPT_BOOLEAN('f', "force", &session
.force_add
, "forcibly add events"
201 " with existing name"),
202 #ifndef NO_DWARF_SUPPORT
203 OPT_CALLBACK('L', "line", NULL
,
204 "FUNC[:RLN[+NUM|:RLN2]]|SRC:ALN[+NUM|:ALN2]",
205 "Show source code lines.", opt_show_lines
),
210 /* Initialize symbol maps for vmlinux */
211 static void init_vmlinux(void)
213 symbol_conf
.sort_by_name
= true;
214 if (symbol_conf
.vmlinux_name
== NULL
)
215 symbol_conf
.try_vmlinux_path
= true;
217 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
218 if (symbol__init() < 0)
219 die("Failed to init symbol map.");
221 map_groups__init(&session
.kmap_groups
);
222 if (map_groups__create_kernel_maps(&session
.kmap_groups
,
224 die("Failed to create kernel maps.");
227 int cmd_probe(int argc
, const char **argv
, const char *prefix __used
)
230 #ifndef NO_DWARF_SUPPORT
233 struct probe_point
*pp
;
235 argc
= parse_options(argc
, argv
, options
, probe_usage
,
236 PARSE_OPT_STOP_AT_NON_OPTION
);
238 if (strcmp(argv
[0], "-") == 0) {
239 pr_warning(" Error: '-' is not supported.\n");
240 usage_with_options(probe_usage
, options
);
242 parse_probe_event_argv(argc
, argv
);
245 if ((!session
.nr_probe
&& !session
.dellist
&& !session
.list_events
&&
246 !session
.show_lines
))
247 usage_with_options(probe_usage
, options
);
249 if (debugfs_valid_mountpoint(debugfs_path
) < 0)
250 die("Failed to find debugfs path.");
252 if (session
.list_events
) {
253 if (session
.nr_probe
!= 0 || session
.dellist
) {
254 pr_warning(" Error: Don't use --list with"
256 usage_with_options(probe_usage
, options
);
258 if (session
.show_lines
) {
259 pr_warning(" Error: Don't use --list with --line.\n");
260 usage_with_options(probe_usage
, options
);
262 show_perf_probe_events();
266 #ifndef NO_DWARF_SUPPORT
267 if (session
.show_lines
) {
268 if (session
.nr_probe
!= 0 || session
.dellist
) {
269 pr_warning(" Error: Don't use --line with"
271 usage_with_options(probe_usage
, options
);
276 die("Could not open debuginfo file.");
277 ret
= find_line_range(fd
, &session
.line_range
);
279 die("Source line is not found.\n");
281 show_line_range(&session
.line_range
);
286 if (session
.dellist
) {
287 del_trace_kprobe_events(session
.dellist
);
288 strlist__delete(session
.dellist
);
289 if (session
.nr_probe
== 0)
296 if (session
.need_dwarf
)
297 #ifdef NO_DWARF_SUPPORT
298 die("Debuginfo-analysis is not supported");
299 #else /* !NO_DWARF_SUPPORT */
300 pr_debug("Some probes require debuginfo.\n");
304 if (session
.need_dwarf
)
305 die("Could not open debuginfo file.");
307 pr_debug("Could not open vmlinux/module file."
308 " Try to use symbols.\n");
312 /* Searching probe points */
313 for (i
= 0; i
< session
.nr_probe
; i
++) {
314 pp
= &session
.probes
[i
];
318 lseek(fd
, SEEK_SET
, 0);
319 ret
= find_probe_point(fd
, pp
);
322 if (ret
== 0) { /* No error but failed to find probe point. */
323 synthesize_perf_probe_point(pp
);
324 die("Probe point '%s' not found. - probe not added.",
328 if (session
.need_dwarf
) {
330 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
331 die("Could not analyze debuginfo.");
333 pr_debug("An error occurred in debuginfo analysis."
334 " Try to use symbols.\n");
340 #endif /* !NO_DWARF_SUPPORT */
342 /* Synthesize probes without dwarf */
343 for (i
= 0; i
< session
.nr_probe
; i
++) {
344 pp
= &session
.probes
[i
];
345 if (pp
->found
) /* This probe is already found. */
348 evaluate_probe_point(pp
);
349 ret
= synthesize_trace_kprobe_event(pp
);
351 die("probe point definition becomes too long.");
353 die("Failed to synthesize a probe point.");
356 /* Settng up probe points */
357 add_trace_kprobe_events(session
.probes
, session
.nr_probe
,