blob: ee3f41eec5c117f83891a7b50ae746271fb20120 [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302 * probe-event.c : perf-probe definition to probe_events format converter
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05003 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
22#define _GNU_SOURCE
23#include <sys/utsname.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <errno.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <stdlib.h>
31#include <string.h>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050032#include <stdarg.h>
33#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090034#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050035
36#undef _GNU_SOURCE
Masami Hiramatsu31facc52010-03-16 18:05:30 -040037#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050038#include "event.h"
Masami Hiramatsue1c01d62009-11-30 19:20:05 -050039#include "string.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050040#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050041#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050042#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050043#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040044#include "symbol.h"
45#include "thread.h"
Masami Hiramatsu7ca59892010-04-14 18:39:28 -040046#include "debugfs.h"
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030047#include "trace-event.h" /* For __unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050048#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040049#include "probe-finder.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050050
51#define MAX_CMDLEN 256
52#define MAX_PROBE_ARGS 128
53#define PERFPROBE_GROUP "probe"
54
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040055bool probe_event_dry_run; /* Dry run flag */
56
Masami Hiramatsu146a1432010-04-12 13:17:42 -040057#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050058
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050059/* If there is no space to write, returns -E2BIG. */
60static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050061 __attribute__((format(printf, 3, 4)));
62
63static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050064{
65 int ret;
66 va_list ap;
67 va_start(ap, format);
68 ret = vsnprintf(str, size, format, ap);
69 va_end(ap);
70 if (ret >= (int)size)
71 ret = -E2BIG;
72 return ret;
73}
74
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030075static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030076static struct machine machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040077
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090078/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsu146a1432010-04-12 13:17:42 -040079static int init_vmlinux(void)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040080{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040081 int ret;
82
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040083 symbol_conf.sort_by_name = true;
84 if (symbol_conf.vmlinux_name == NULL)
85 symbol_conf.try_vmlinux_path = true;
86 else
87 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040088 ret = symbol__init();
89 if (ret < 0) {
90 pr_debug("Failed to init symbol map.\n");
91 goto out;
92 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040093
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090094 ret = machine__init(&machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030095 if (ret < 0)
96 goto out;
97
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090098 if (machine__create_kernel_maps(&machine) < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +090099 pr_debug("machine__create_kernel_maps() failed.\n");
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900100 goto out;
101 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400102out:
103 if (ret < 0)
104 pr_warning("Failed to init vmlinux path.\n");
105 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400106}
107
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900108static struct symbol *__find_kernel_function_by_name(const char *name,
109 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400110{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900111 return machine__find_kernel_function_by_name(&machine, name, mapp,
112 NULL);
113}
114
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900115static struct map *kernel_get_module_map(const char *module)
116{
117 struct rb_node *nd;
118 struct map_groups *grp = &machine.kmaps;
119
120 if (!module)
121 module = "kernel";
122
123 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
124 struct map *pos = rb_entry(nd, struct map, rb_node);
125 if (strncmp(pos->dso->short_name + 1, module,
126 pos->dso->short_name_len - 2) == 0) {
127 return pos;
128 }
129 }
130 return NULL;
131}
132
133static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900134{
135 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100136 struct map *map;
137 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900138
139 if (module) {
140 list_for_each_entry(dso, &machine.kernel_dsos, node) {
141 if (strncmp(dso->short_name + 1, module,
142 dso->short_name_len - 2) == 0)
143 goto found;
144 }
145 pr_debug("Failed to find module %s.\n", module);
146 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100147 }
148
149 map = machine.vmlinux_maps[MAP__FUNCTION];
150 dso = map->dso;
151
152 vmlinux_name = symbol_conf.vmlinux_name;
153 if (vmlinux_name) {
154 if (dso__load_vmlinux(dso, map, vmlinux_name, NULL) <= 0)
155 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900156 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100157 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900158 pr_debug("Failed to load kernel map.\n");
159 return NULL;
160 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400161 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900162found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900163 return dso;
164}
165
166const char *kernel_get_module_path(const char *module)
167{
168 struct dso *dso = kernel_get_module_dso(module);
169 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900170}
171
172#ifdef DWARF_SUPPORT
Masami Hiramatsuff741782011-06-27 16:27:39 +0900173/* Open new debuginfo of given module */
174static struct debuginfo *open_debuginfo(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900175{
176 const char *path = kernel_get_module_path(module);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900177
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900178 if (!path) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900179 pr_err("Failed to find path of %s module.\n",
180 module ?: "kernel");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900181 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900182 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900183 return debuginfo__new(path);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400184}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300185
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530186/*
187 * Convert trace point to probe point with debuginfo
188 * Currently only handles kprobes.
189 */
190static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900191 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300192{
193 struct symbol *sym;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900194 struct map *map;
195 u64 addr;
196 int ret = -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900197 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300198
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900199 sym = __find_kernel_function_by_name(tp->symbol, &map);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300200 if (sym) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900201 addr = map->unmap_ip(map, sym->start + tp->offset);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200202 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900203 tp->offset, addr);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900204
205 dinfo = debuginfo__new_online_kernel(addr);
206 if (dinfo) {
207 ret = debuginfo__find_probe_point(dinfo,
208 (unsigned long)addr, pp);
209 debuginfo__delete(dinfo);
210 } else {
211 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n",
212 addr);
213 ret = -ENOENT;
214 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300215 }
216 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400217 pr_debug("Failed to find corresponding probes from "
218 "debuginfo. Use kprobe event information.\n");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400219 pp->function = strdup(tp->symbol);
220 if (pp->function == NULL)
221 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300222 pp->offset = tp->offset;
223 }
224 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400225
226 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300227}
228
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900229static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
230 int ntevs, const char *module)
231{
232 int i;
233 for (i = 0; i < ntevs; i++) {
234 tevs[i].point.module = strdup(module);
235 if (!tevs[i].point.module)
236 return -ENOMEM;
237 }
238 return 0;
239}
240
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300241/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530242static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900243 struct probe_trace_event **tevs,
244 int max_tevs, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300245{
246 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900247 struct debuginfo *dinfo = open_debuginfo(module);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900248 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300249
Masami Hiramatsuff741782011-06-27 16:27:39 +0900250 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400251 if (need_dwarf) {
252 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900253 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400254 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900255 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300256 return 0;
257 }
258
Masami Hiramatsuff741782011-06-27 16:27:39 +0900259 /* Searching trace events corresponding to a probe event */
260 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
261
262 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300263
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400264 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530265 pr_debug("find %d probe_trace_events.\n", ntevs);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900266 if (module)
267 ret = add_module_to_probe_trace_events(*tevs, ntevs,
268 module);
269 return ret < 0 ? ret : ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400270 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300271
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400272 if (ntevs == 0) { /* No error but failed to find probe point. */
273 pr_warning("Probe point '%s' not found.\n",
274 synthesize_perf_probe_point(&pev->point));
275 return -ENOENT;
276 }
277 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400278 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
279 if (ntevs == -EBADF) {
280 pr_warning("Warning: No dwarf info found in the vmlinux - "
281 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
282 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900283 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400284 return 0;
285 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300286 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400287 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300288}
289
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900290/*
291 * Find a src file from a DWARF tag path. Prepend optional source path prefix
292 * and chop off leading directories that do not exist. Result is passed back as
293 * a newly allocated path on success.
294 * Return 0 if file was found and readable, -errno otherwise.
295 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900296static int get_real_path(const char *raw_path, const char *comp_dir,
297 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900298{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900299 const char *prefix = symbol_conf.source_prefix;
300
301 if (!prefix) {
302 if (raw_path[0] != '/' && comp_dir)
303 /* If not an absolute path, try to use comp_dir */
304 prefix = comp_dir;
305 else {
306 if (access(raw_path, R_OK) == 0) {
307 *new_path = strdup(raw_path);
308 return 0;
309 } else
310 return -errno;
311 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900312 }
313
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900314 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900315 if (!*new_path)
316 return -ENOMEM;
317
318 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900319 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900320
321 if (access(*new_path, R_OK) == 0)
322 return 0;
323
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900324 if (!symbol_conf.source_prefix)
325 /* In case of searching comp_dir, don't retry */
326 return -errno;
327
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900328 switch (errno) {
329 case ENAMETOOLONG:
330 case ENOENT:
331 case EROFS:
332 case EFAULT:
333 raw_path = strchr(++raw_path, '/');
334 if (!raw_path) {
335 free(*new_path);
336 *new_path = NULL;
337 return -ENOENT;
338 }
339 continue;
340
341 default:
342 free(*new_path);
343 *new_path = NULL;
344 return -errno;
345 }
346 }
347}
348
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300349#define LINEBUF_SIZE 256
350#define NR_ADDITIONAL_LINES 2
351
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100352static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300353{
354 char buf[LINEBUF_SIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100355 const char *color = show_num ? "" : PERF_COLOR_BLUE;
356 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300357
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100358 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300359 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
360 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100361 if (skip)
362 continue;
363 if (!prefix) {
364 prefix = show_num ? "%7d " : " ";
365 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300366 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100367 color_fprintf(stdout, color, "%s", buf);
368
369 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400370
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100371 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300372error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100373 if (ferror(fp)) {
Franck Bui-Huu32b2b6e2010-12-22 17:37:13 +0100374 pr_warning("File read error: %s\n", strerror(errno));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100375 return -1;
376 }
377 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300378}
379
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100380static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
381{
382 int rv = __show_one_line(fp, l, skip, show_num);
383 if (rv == 0) {
384 pr_warning("Source file is shorter than expected.\n");
385 rv = -1;
386 }
387 return rv;
388}
389
390#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
391#define show_one_line(f,l) _show_one_line(f,l,false,false)
392#define skip_one_line(f,l) _show_one_line(f,l,true,false)
393#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
394
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300395/*
396 * Show line-range always requires debuginfo to find source file and
397 * line number.
398 */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900399int show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300400{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400401 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300402 struct line_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900403 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300404 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900405 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900406 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300407
408 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400409 ret = init_vmlinux();
410 if (ret < 0)
411 return ret;
412
Masami Hiramatsuff741782011-06-27 16:27:39 +0900413 dinfo = open_debuginfo(module);
414 if (!dinfo) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400415 pr_warning("Failed to open debuginfo file.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900416 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400417 }
418
Masami Hiramatsuff741782011-06-27 16:27:39 +0900419 ret = debuginfo__find_line_range(dinfo, lr);
420 debuginfo__delete(dinfo);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400421 if (ret == 0) {
422 pr_warning("Specified source line is not found.\n");
423 return -ENOENT;
424 } else if (ret < 0) {
425 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
426 return ret;
427 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300428
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900429 /* Convert source file path */
430 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900431 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900432 free(tmp); /* Free old path */
433 if (ret < 0) {
434 pr_warning("Failed to find source file. (%d)\n", ret);
435 return ret;
436 }
437
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300438 setup_pager();
439
440 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900441 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300442 lr->start - lr->offset);
443 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100444 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300445
446 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400447 if (fp == NULL) {
448 pr_warning("Failed to open %s: %s\n", lr->path,
449 strerror(errno));
450 return -errno;
451 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300452 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100453 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100454 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100455 if (ret < 0)
456 goto end;
457 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300458
459 list_for_each_entry(ln, &lr->line_list, list) {
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100460 for (; ln->line > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100461 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100462 if (ret < 0)
463 goto end;
464 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100465 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400466 if (ret < 0)
467 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300468 }
469
470 if (lr->end == INT_MAX)
471 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100472 while (l <= lr->end) {
473 ret = show_one_line_or_eof(fp, l++ - lr->offset);
474 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100475 break;
476 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400477end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300478 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400479 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300480}
481
Masami Hiramatsuff741782011-06-27 16:27:39 +0900482static int show_available_vars_at(struct debuginfo *dinfo,
483 struct perf_probe_event *pev,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900484 int max_vls, struct strfilter *_filter,
485 bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900486{
487 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900488 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900489 struct str_node *node;
490 struct variable_list *vls = NULL, *vl;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900491 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900492
493 buf = synthesize_perf_probe_point(&pev->point);
494 if (!buf)
495 return -EINVAL;
496 pr_debug("Searching variables at %s\n", buf);
497
Masami Hiramatsuff741782011-06-27 16:27:39 +0900498 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
499 max_vls, externs);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900500 if (ret <= 0) {
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900501 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900502 goto end;
503 }
504 /* Some variables are found */
505 fprintf(stdout, "Available variables at %s\n", buf);
506 for (i = 0; i < ret; i++) {
507 vl = &vls[i];
508 /*
509 * A probe point might be converted to
510 * several trace points.
511 */
512 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
513 vl->point.offset);
514 free(vl->point.symbol);
515 nvars = 0;
516 if (vl->vars) {
517 strlist__for_each(node, vl->vars) {
518 var = strchr(node->s, '\t') + 1;
519 if (strfilter__compare(_filter, var)) {
520 fprintf(stdout, "\t\t%s\n", node->s);
521 nvars++;
522 }
523 }
524 strlist__delete(vl->vars);
525 }
526 if (nvars == 0)
527 fprintf(stdout, "\t\t(No matched variables)\n");
528 }
529 free(vls);
530end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900531 free(buf);
532 return ret;
533}
534
535/* Show available variables on given probe point */
536int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900537 int max_vls, const char *module,
538 struct strfilter *_filter, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900539{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900540 int i, ret = 0;
541 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900542
543 ret = init_vmlinux();
544 if (ret < 0)
545 return ret;
546
Masami Hiramatsuff741782011-06-27 16:27:39 +0900547 dinfo = open_debuginfo(module);
548 if (!dinfo) {
549 pr_warning("Failed to open debuginfo file.\n");
550 return -ENOENT;
551 }
552
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900553 setup_pager();
554
Masami Hiramatsuff741782011-06-27 16:27:39 +0900555 for (i = 0; i < npevs && ret >= 0; i++)
556 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900557 externs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900558
559 debuginfo__delete(dinfo);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900560 return ret;
561}
562
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300563#else /* !DWARF_SUPPORT */
564
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530565static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900566 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300567{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900568 struct symbol *sym;
569
570 sym = __find_kernel_function_by_name(tp->symbol, NULL);
571 if (!sym) {
572 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
573 return -ENOENT;
574 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400575 pp->function = strdup(tp->symbol);
576 if (pp->function == NULL)
577 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300578 pp->offset = tp->offset;
579 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400580
581 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300582}
583
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530584static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
585 struct probe_trace_event **tevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900586 int max_tevs __unused, const char *mod __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300587{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400588 if (perf_probe_event_need_dwarf(pev)) {
589 pr_warning("Debuginfo-analysis is not supported.\n");
590 return -ENOSYS;
591 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300592 return 0;
593}
594
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900595int show_line_range(struct line_range *lr __unused, const char *module __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300596{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400597 pr_warning("Debuginfo-analysis is not supported.\n");
598 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300599}
600
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900601int show_available_vars(struct perf_probe_event *pevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900602 int npevs __unused, int max_vls __unused,
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900603 const char *module __unused,
604 struct strfilter *filter __unused,
605 bool externs __unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900606{
607 pr_warning("Debuginfo-analysis is not supported.\n");
608 return -ENOSYS;
609}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400610#endif
611
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100612static int parse_line_num(char **ptr, int *val, const char *what)
613{
614 const char *start = *ptr;
615
616 errno = 0;
617 *val = strtol(*ptr, ptr, 0);
618 if (errno || *ptr == start) {
619 semantic_error("'%s' is not a valid number.\n", what);
620 return -EINVAL;
621 }
622 return 0;
623}
624
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100625/*
626 * Stuff 'lr' according to the line range described by 'arg'.
627 * The line range syntax is described by:
628 *
629 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900630 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100631 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400632int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500633{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900634 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100635 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100636
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100637 if (!name)
638 return -ENOMEM;
639
640 lr->start = 0;
641 lr->end = INT_MAX;
642
643 range = strchr(name, ':');
644 if (range) {
645 *range++ = '\0';
646
647 err = parse_line_num(&range, &lr->start, "start line");
648 if (err)
649 goto err;
650
651 if (*range == '+' || *range == '-') {
652 const char c = *range++;
653
654 err = parse_line_num(&range, &lr->end, "end line");
655 if (err)
656 goto err;
657
658 if (c == '+') {
659 lr->end += lr->start;
660 /*
661 * Adjust the number of lines here.
662 * If the number of lines == 1, the
663 * the end of line should be equal to
664 * the start of line.
665 */
666 lr->end--;
667 }
668 }
669
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400670 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100671
672 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400673 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500674 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400675 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100676 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400677 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100678 if (*range != '\0') {
679 semantic_error("Tailing with invalid str '%s'.\n", range);
680 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400681 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400682 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400683
Masami Hiramatsue116dfa2011-02-10 18:08:10 +0900684 file = strchr(name, '@');
685 if (file) {
686 *file = '\0';
687 lr->file = strdup(++file);
688 if (lr->file == NULL) {
689 err = -ENOMEM;
690 goto err;
691 }
692 lr->function = name;
693 } else if (strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100694 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500695 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100696 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400697
698 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100699err:
700 free(name);
701 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500702}
703
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500704/* Check the name is good for event/group */
705static bool check_event_name(const char *name)
706{
707 if (!isalpha(*name) && *name != '_')
708 return false;
709 while (*++name != '\0') {
710 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
711 return false;
712 }
713 return true;
714}
715
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500716/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400717static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500718{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400719 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500720 char *ptr, *tmp;
721 char c, nc = 0;
722 /*
723 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500724 * perf probe [EVENT=]SRC[:LN|;PTN]
725 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500726 *
727 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500728 */
729
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500730 ptr = strpbrk(arg, ";=@+%");
731 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500732 *ptr = '\0';
733 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400734 if (strchr(arg, ':')) {
735 semantic_error("Group name is not supported yet.\n");
736 return -ENOTSUP;
737 }
738 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500739 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400740 "follow C symbol-naming rule.\n", arg);
741 return -EINVAL;
742 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400743 pev->event = strdup(arg);
744 if (pev->event == NULL)
745 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400746 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500747 arg = tmp;
748 }
749
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500750 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500751 if (ptr) {
752 nc = *ptr;
753 *ptr++ = '\0';
754 }
755
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400756 tmp = strdup(arg);
757 if (tmp == NULL)
758 return -ENOMEM;
759
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500760 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400761 if (strchr(tmp, '.')) /* File */
762 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500763 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400764 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500765
766 /* Parse other options */
767 while (ptr) {
768 arg = ptr;
769 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500770 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400771 pp->lazy_line = strdup(arg);
772 if (pp->lazy_line == NULL)
773 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500774 break;
775 }
776 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500777 if (ptr) {
778 nc = *ptr;
779 *ptr++ = '\0';
780 }
781 switch (c) {
782 case ':': /* Line number */
783 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400784 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500785 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400786 " in line number.\n");
787 return -EINVAL;
788 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500789 break;
790 case '+': /* Byte offset from a symbol */
791 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400792 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500793 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400794 " in offset.\n");
795 return -EINVAL;
796 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500797 break;
798 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400799 if (pp->file) {
800 semantic_error("SRC@SRC is not allowed.\n");
801 return -EINVAL;
802 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400803 pp->file = strdup(arg);
804 if (pp->file == NULL)
805 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500806 break;
807 case '%': /* Probe places */
808 if (strcmp(arg, "return") == 0) {
809 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400810 } else { /* Others not supported yet */
811 semantic_error("%%%s is not supported.\n", arg);
812 return -ENOTSUP;
813 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500814 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400815 default: /* Buggy case */
816 pr_err("This program has a bug at %s:%d.\n",
817 __FILE__, __LINE__);
818 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500819 break;
820 }
821 }
822
823 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400824 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900825 semantic_error("Lazy pattern can't be used with"
826 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400827 return -EINVAL;
828 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500829
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400830 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900831 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400832 return -EINVAL;
833 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500834
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400835 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900836 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400837 return -EINVAL;
838 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500839
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400840 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500841 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900842 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400843 return -EINVAL;
844 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500845
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400846 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900847 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400848 return -EINVAL;
849 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500850
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400851 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900852 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400853 return -EINVAL;
854 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500855
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400856 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500857 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900858 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400859 return -EINVAL;
860 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500861
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400862 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500863 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
864 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400865 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500866}
867
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400868/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400869static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400870{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400871 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400872 struct perf_probe_arg_field **fieldp;
873
874 pr_debug("parsing arg: %s into ", str);
875
Masami Hiramatsu48481932010-04-12 13:16:53 -0400876 tmp = strchr(str, '=');
877 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400878 arg->name = strndup(str, tmp - str);
879 if (arg->name == NULL)
880 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400881 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400882 str = tmp + 1;
883 }
884
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400885 tmp = strchr(str, ':');
886 if (tmp) { /* Type setting */
887 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400888 arg->type = strdup(tmp + 1);
889 if (arg->type == NULL)
890 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400891 pr_debug("type:%s ", arg->type);
892 }
893
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400894 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400895 if (!is_c_varname(str) || !tmp) {
896 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400897 arg->var = strdup(str);
898 if (arg->var == NULL)
899 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400900 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400901 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400902 }
903
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400904 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400905 arg->var = strndup(str, tmp - str);
906 if (arg->var == NULL)
907 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400908 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400909 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400910 fieldp = &arg->field;
911
912 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400913 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
914 if (*fieldp == NULL)
915 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400916 if (*tmp == '[') { /* Array */
917 str = tmp;
918 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400919 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400920 if (*tmp != ']' || tmp == str + 1) {
921 semantic_error("Array index must be a"
922 " number.\n");
923 return -EINVAL;
924 }
925 tmp++;
926 if (*tmp == '\0')
927 tmp = NULL;
928 } else { /* Structure */
929 if (*tmp == '.') {
930 str = tmp + 1;
931 (*fieldp)->ref = false;
932 } else if (tmp[1] == '>') {
933 str = tmp + 2;
934 (*fieldp)->ref = true;
935 } else {
936 semantic_error("Argument parse error: %s\n",
937 str);
938 return -EINVAL;
939 }
940 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400941 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400942 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400943 (*fieldp)->name = strndup(str, tmp - str);
944 if ((*fieldp)->name == NULL)
945 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400946 if (*str != '[')
947 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400948 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
949 fieldp = &(*fieldp)->next;
950 }
951 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400952 (*fieldp)->name = strdup(str);
953 if ((*fieldp)->name == NULL)
954 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400955 if (*str != '[')
956 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400957 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400958
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400959 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400960 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400961 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400962 if (arg->name == NULL)
963 return -ENOMEM;
964 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400965 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400966}
967
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400968/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400969int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500970{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500971 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400972 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500973
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400974 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400975 if (!argv) {
976 pr_debug("Failed to split arguments.\n");
977 return -ENOMEM;
978 }
979 if (argc - 1 > MAX_PROBE_ARGS) {
980 semantic_error("Too many probe arguments (%d).\n", argc - 1);
981 ret = -ERANGE;
982 goto out;
983 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500984 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400985 ret = parse_perf_probe_point(argv[0], pev);
986 if (ret < 0)
987 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500988
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500989 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400990 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400991 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
992 if (pev->args == NULL) {
993 ret = -ENOMEM;
994 goto out;
995 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400996 for (i = 0; i < pev->nargs && ret >= 0; i++) {
997 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
998 if (ret >= 0 &&
999 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001000 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001001 " kretprobe.\n");
1002 ret = -EINVAL;
1003 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001004 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001005out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001006 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001007
1008 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001009}
1010
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001011/* Return true if this perf_probe_event requires debuginfo */
1012bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001013{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001014 int i;
1015
1016 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1017 return true;
1018
1019 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001020 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001021 return true;
1022
1023 return false;
1024}
1025
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301026/* Parse probe_events event into struct probe_point */
1027static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001028 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001029{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301030 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001031 char pr;
1032 char *p;
1033 int ret, i, argc;
1034 char **argv;
1035
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301036 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001037 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001038 if (!argv) {
1039 pr_debug("Failed to split arguments.\n");
1040 return -ENOMEM;
1041 }
1042 if (argc < 2) {
1043 semantic_error("Too few probe arguments.\n");
1044 ret = -ERANGE;
1045 goto out;
1046 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001047
1048 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +08001049 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001050 &pr, (float *)(void *)&tev->group,
1051 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001052 if (ret != 3) {
1053 semantic_error("Failed to parse event name: %s\n", argv[0]);
1054 ret = -EINVAL;
1055 goto out;
1056 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001057 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001058
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001059 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001060
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001061 /* Scan module name(if there), function name and offset */
1062 p = strchr(argv[1], ':');
1063 if (p) {
1064 tp->module = strndup(argv[1], p - argv[1]);
1065 p++;
1066 } else
1067 p = argv[1];
1068 ret = sscanf(p, "%a[^+]+%lu", (float *)(void *)&tp->symbol,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001069 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001070 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001071 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001072
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001073 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301074 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001075 if (tev->args == NULL) {
1076 ret = -ENOMEM;
1077 goto out;
1078 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001079 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001080 p = strchr(argv[i + 2], '=');
1081 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001082 *p++ = '\0';
1083 else
1084 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001085 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001086 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001087 tev->args[i].value = strdup(p);
1088 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1089 ret = -ENOMEM;
1090 goto out;
1091 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001092 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001093 ret = 0;
1094out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001095 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001096 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001097}
1098
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001099/* Compose only probe arg */
1100int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1101{
1102 struct perf_probe_arg_field *field = pa->field;
1103 int ret;
1104 char *tmp = buf;
1105
Masami Hiramatsu48481932010-04-12 13:16:53 -04001106 if (pa->name && pa->var)
1107 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1108 else
1109 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001110 if (ret <= 0)
1111 goto error;
1112 tmp += ret;
1113 len -= ret;
1114
1115 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001116 if (field->name[0] == '[')
1117 ret = e_snprintf(tmp, len, "%s", field->name);
1118 else
1119 ret = e_snprintf(tmp, len, "%s%s",
1120 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001121 if (ret <= 0)
1122 goto error;
1123 tmp += ret;
1124 len -= ret;
1125 field = field->next;
1126 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001127
1128 if (pa->type) {
1129 ret = e_snprintf(tmp, len, ":%s", pa->type);
1130 if (ret <= 0)
1131 goto error;
1132 tmp += ret;
1133 len -= ret;
1134 }
1135
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001136 return tmp - buf;
1137error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001138 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001139 strerror(-ret));
1140 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001141}
1142
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001143/* Compose only probe point (not argument) */
1144static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001145{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001146 char *buf, *tmp;
1147 char offs[32] = "", line[32] = "", file[32] = "";
1148 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001149
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001150 buf = zalloc(MAX_CMDLEN);
1151 if (buf == NULL) {
1152 ret = -ENOMEM;
1153 goto error;
1154 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001155 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001156 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001157 if (ret <= 0)
1158 goto error;
1159 }
1160 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001161 ret = e_snprintf(line, 32, ":%d", pp->line);
1162 if (ret <= 0)
1163 goto error;
1164 }
1165 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001166 tmp = pp->file;
1167 len = strlen(tmp);
1168 if (len > 30) {
1169 tmp = strchr(pp->file + len - 30, '/');
1170 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1171 }
1172 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001173 if (ret <= 0)
1174 goto error;
1175 }
1176
1177 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001178 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1179 offs, pp->retprobe ? "%return" : "", line,
1180 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001181 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001182 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001183 if (ret <= 0)
1184 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001185
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001186 return buf;
1187error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001188 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001189 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001190 if (buf)
1191 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001192 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001193}
1194
1195#if 0
1196char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1197{
1198 char *buf;
1199 int i, len, ret;
1200
1201 buf = synthesize_perf_probe_point(&pev->point);
1202 if (!buf)
1203 return NULL;
1204
1205 len = strlen(buf);
1206 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001207 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001208 pev->args[i].name);
1209 if (ret <= 0) {
1210 free(buf);
1211 return NULL;
1212 }
1213 len += ret;
1214 }
1215
1216 return buf;
1217}
1218#endif
1219
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301220static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001221 char **buf, size_t *buflen,
1222 int depth)
1223{
1224 int ret;
1225 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301226 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001227 buflen, depth + 1);
1228 if (depth < 0)
1229 goto out;
1230 }
1231
1232 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1233 if (ret < 0)
1234 depth = ret;
1235 else {
1236 *buf += ret;
1237 *buflen -= ret;
1238 }
1239out:
1240 return depth;
1241
1242}
1243
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301244static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001245 char *buf, size_t buflen)
1246{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301247 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001248 int ret, depth = 0;
1249 char *tmp = buf;
1250
1251 /* Argument name or separator */
1252 if (arg->name)
1253 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1254 else
1255 ret = e_snprintf(buf, buflen, " ");
1256 if (ret < 0)
1257 return ret;
1258 buf += ret;
1259 buflen -= ret;
1260
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001261 /* Special case: @XXX */
1262 if (arg->value[0] == '@' && arg->ref)
1263 ref = ref->next;
1264
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001265 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001266 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301267 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001268 &buflen, 1);
1269 if (depth < 0)
1270 return depth;
1271 }
1272
1273 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001274 if (arg->value[0] == '@' && arg->ref)
1275 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1276 arg->ref->offset);
1277 else
1278 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001279 if (ret < 0)
1280 return ret;
1281 buf += ret;
1282 buflen -= ret;
1283
1284 /* Closing */
1285 while (depth--) {
1286 ret = e_snprintf(buf, buflen, ")");
1287 if (ret < 0)
1288 return ret;
1289 buf += ret;
1290 buflen -= ret;
1291 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001292 /* Print argument type */
1293 if (arg->type) {
1294 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1295 if (ret <= 0)
1296 return ret;
1297 buf += ret;
1298 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001299
1300 return buf - tmp;
1301}
1302
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301303char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001304{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301305 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001306 char *buf;
1307 int i, len, ret;
1308
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001309 buf = zalloc(MAX_CMDLEN);
1310 if (buf == NULL)
1311 return NULL;
1312
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001313 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001314 tp->retprobe ? 'r' : 'p',
1315 tev->group, tev->event,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001316 tp->module ?: "", tp->module ? ":" : "",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001317 tp->symbol, tp->offset);
1318 if (len <= 0)
1319 goto error;
1320
1321 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301322 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001323 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001324 if (ret <= 0)
1325 goto error;
1326 len += ret;
1327 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001328
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001329 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001330error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001331 free(buf);
1332 return NULL;
1333}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001334
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301335static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001336 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001337{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001338 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001339 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001340
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001341 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001342 pev->event = strdup(tev->event);
1343 pev->group = strdup(tev->group);
1344 if (pev->event == NULL || pev->group == NULL)
1345 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001346
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001347 /* Convert trace_point to probe_point */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301348 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001349 if (ret < 0)
1350 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001351
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001352 /* Convert trace_arg to probe_arg */
1353 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001354 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1355 if (pev->args == NULL)
1356 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001357 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001358 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001359 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001360 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301361 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001362 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001363 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001364 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001365 if (pev->args[i].name == NULL && ret >= 0)
1366 ret = -ENOMEM;
1367 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001368
1369 if (ret < 0)
1370 clear_perf_probe_event(pev);
1371
1372 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001373}
1374
1375void clear_perf_probe_event(struct perf_probe_event *pev)
1376{
1377 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001378 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001379 int i;
1380
1381 if (pev->event)
1382 free(pev->event);
1383 if (pev->group)
1384 free(pev->group);
1385 if (pp->file)
1386 free(pp->file);
1387 if (pp->function)
1388 free(pp->function);
1389 if (pp->lazy_line)
1390 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001391 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001392 if (pev->args[i].name)
1393 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001394 if (pev->args[i].var)
1395 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001396 if (pev->args[i].type)
1397 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001398 field = pev->args[i].field;
1399 while (field) {
1400 next = field->next;
1401 if (field->name)
1402 free(field->name);
1403 free(field);
1404 field = next;
1405 }
1406 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001407 if (pev->args)
1408 free(pev->args);
1409 memset(pev, 0, sizeof(*pev));
1410}
1411
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301412static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001413{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301414 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001415 int i;
1416
1417 if (tev->event)
1418 free(tev->event);
1419 if (tev->group)
1420 free(tev->group);
1421 if (tev->point.symbol)
1422 free(tev->point.symbol);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001423 if (tev->point.module)
1424 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001425 for (i = 0; i < tev->nargs; i++) {
1426 if (tev->args[i].name)
1427 free(tev->args[i].name);
1428 if (tev->args[i].value)
1429 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001430 if (tev->args[i].type)
1431 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001432 ref = tev->args[i].ref;
1433 while (ref) {
1434 next = ref->next;
1435 free(ref);
1436 ref = next;
1437 }
1438 }
1439 if (tev->args)
1440 free(tev->args);
1441 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001442}
1443
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001444static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001445{
1446 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001447 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001448 int ret;
1449
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001450 __debugfs = debugfs_find_mountpoint();
1451 if (__debugfs == NULL) {
1452 pr_warning("Debugfs is not mounted.\n");
1453 return -ENOENT;
1454 }
1455
1456 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001457 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001458 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001459 if (readwrite && !probe_event_dry_run)
1460 ret = open(buf, O_RDWR, O_APPEND);
1461 else
1462 ret = open(buf, O_RDONLY, 0);
1463 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001464
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001465 if (ret < 0) {
1466 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001467 pr_warning("kprobe_events file does not exist - please"
1468 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001469 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001470 pr_warning("Failed to open kprobe_events file: %s\n",
1471 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001472 }
1473 return ret;
1474}
1475
1476/* Get raw string list of current kprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301477static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001478{
1479 int ret, idx;
1480 FILE *fp;
1481 char buf[MAX_CMDLEN];
1482 char *p;
1483 struct strlist *sl;
1484
1485 sl = strlist__new(true, NULL);
1486
1487 fp = fdopen(dup(fd), "r");
1488 while (!feof(fp)) {
1489 p = fgets(buf, MAX_CMDLEN, fp);
1490 if (!p)
1491 break;
1492
1493 idx = strlen(p) - 1;
1494 if (p[idx] == '\n')
1495 p[idx] = '\0';
1496 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001497 if (ret < 0) {
1498 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1499 strlist__delete(sl);
1500 return NULL;
1501 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001502 }
1503 fclose(fp);
1504
1505 return sl;
1506}
1507
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001508/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001509static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001510{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001511 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001512 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001513 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001514
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001515 /* Synthesize only event probe point */
1516 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001517 if (!place)
1518 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001519
1520 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001521 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001522 return ret;
1523
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001524 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001525
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001526 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001527 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001528 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001529 ret = synthesize_perf_probe_arg(&pev->args[i],
1530 buf, 128);
1531 if (ret < 0)
1532 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001533 printf(" %s", buf);
1534 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001535 }
1536 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001537 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001538 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001539}
1540
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001541/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001542int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001543{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001544 int fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301545 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001546 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001547 struct strlist *rawlist;
1548 struct str_node *ent;
1549
Masami Hiramatsu72041332010-01-05 17:47:10 -05001550 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001551 ret = init_vmlinux();
1552 if (ret < 0)
1553 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001554
1555 memset(&tev, 0, sizeof(tev));
1556 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001557
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001558 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001559 if (fd < 0)
1560 return fd;
1561
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301562 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001563 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001564 if (!rawlist)
1565 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001566
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001567 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301568 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001569 if (ret >= 0) {
1570 ret = convert_to_perf_probe_event(&tev, &pev);
1571 if (ret >= 0)
1572 ret = show_perf_probe_event(&pev);
1573 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001574 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301575 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001576 if (ret < 0)
1577 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001578 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001579 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001580
1581 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001582}
1583
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001584/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301585static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001586{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001587 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001588 struct strlist *sl, *rawlist;
1589 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301590 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001591 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001592
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001593 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301594 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001595 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001596 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301597 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001598 if (ret < 0)
1599 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001600 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001601 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1602 tev.event);
1603 if (ret >= 0)
1604 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001605 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001606 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301607 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001608 if (ret < 0)
1609 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001610 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001611 strlist__delete(rawlist);
1612
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001613 if (ret < 0) {
1614 strlist__delete(sl);
1615 return NULL;
1616 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001617 return sl;
1618}
1619
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301620static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001621{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001622 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301623 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001624
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001625 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301626 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001627 return -EINVAL;
1628 }
1629
Masami Hiramatsufa282442009-12-08 17:03:23 -05001630 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001631 if (!probe_event_dry_run) {
1632 ret = write(fd, buf, strlen(buf));
1633 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001634 pr_warning("Failed to write event: %s\n",
1635 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001636 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001637 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001638 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001639}
1640
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001641static int get_new_event_name(char *buf, size_t len, const char *base,
1642 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001643{
1644 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001645
1646 /* Try no suffix */
1647 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001648 if (ret < 0) {
1649 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1650 return ret;
1651 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001652 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001653 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001654
Masami Hiramatsud761b082009-12-15 10:32:25 -05001655 if (!allow_suffix) {
1656 pr_warning("Error: event \"%s\" already exists. "
1657 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001658 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001659 }
1660
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001661 /* Try to add suffix */
1662 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001663 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001664 if (ret < 0) {
1665 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1666 return ret;
1667 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001668 if (!strlist__has_entry(namelist, buf))
1669 break;
1670 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001671 if (i == MAX_EVENT_INDEX) {
1672 pr_warning("Too many events are on the same function.\n");
1673 ret = -ERANGE;
1674 }
1675
1676 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001677}
1678
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301679static int __add_probe_trace_events(struct perf_probe_event *pev,
1680 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001681 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001682{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001683 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301684 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001685 char buf[64];
1686 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001687 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001688
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001689 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001690 if (fd < 0)
1691 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001692 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301693 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001694 if (!namelist) {
1695 pr_debug("Failed to get current event list.\n");
1696 return -EIO;
1697 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001698
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001699 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001700 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001701 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001702 tev = &tevs[i];
1703 if (pev->event)
1704 event = pev->event;
1705 else
1706 if (pev->point.function)
1707 event = pev->point.function;
1708 else
1709 event = tev->point.symbol;
1710 if (pev->group)
1711 group = pev->group;
1712 else
1713 group = PERFPROBE_GROUP;
1714
1715 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001716 ret = get_new_event_name(buf, 64, event,
1717 namelist, allow_suffix);
1718 if (ret < 0)
1719 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001720 event = buf;
1721
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001722 tev->event = strdup(event);
1723 tev->group = strdup(group);
1724 if (tev->event == NULL || tev->group == NULL) {
1725 ret = -ENOMEM;
1726 break;
1727 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301728 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001729 if (ret < 0)
1730 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001731 /* Add added event name to namelist */
1732 strlist__add(namelist, event);
1733
1734 /* Trick here - save current event/group */
1735 event = pev->event;
1736 group = pev->group;
1737 pev->event = tev->event;
1738 pev->group = tev->group;
1739 show_perf_probe_event(pev);
1740 /* Trick here - restore current event/group */
1741 pev->event = (char *)event;
1742 pev->group = (char *)group;
1743
1744 /*
1745 * Probes after the first probe which comes from same
1746 * user input are always allowed to add suffix, because
1747 * there might be several addresses corresponding to
1748 * one code line.
1749 */
1750 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001751 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001752
1753 if (ret >= 0) {
1754 /* Show how to use the event. */
1755 printf("\nYou can now use it on all perf tools, such as:\n\n");
1756 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1757 tev->event);
1758 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001759
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001760 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001761 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001762 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001763}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001764
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301765static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1766 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001767 int max_tevs, const char *module)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001768{
1769 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001770 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301771 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001772
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001773 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001774 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001775 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001776 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001777
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001778 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301779 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001780 if (tev == NULL)
1781 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001782
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001783 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001784 tev->point.symbol = strdup(pev->point.function);
1785 if (tev->point.symbol == NULL) {
1786 ret = -ENOMEM;
1787 goto error;
1788 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001789 tev->point.module = strdup(module);
1790 if (tev->point.module == NULL) {
1791 ret = -ENOMEM;
1792 goto error;
1793 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001794 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001795 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001796 tev->nargs = pev->nargs;
1797 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301798 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001799 * tev->nargs);
1800 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001801 ret = -ENOMEM;
1802 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001803 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001804 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001805 if (pev->args[i].name) {
1806 tev->args[i].name = strdup(pev->args[i].name);
1807 if (tev->args[i].name == NULL) {
1808 ret = -ENOMEM;
1809 goto error;
1810 }
1811 }
1812 tev->args[i].value = strdup(pev->args[i].var);
1813 if (tev->args[i].value == NULL) {
1814 ret = -ENOMEM;
1815 goto error;
1816 }
1817 if (pev->args[i].type) {
1818 tev->args[i].type = strdup(pev->args[i].type);
1819 if (tev->args[i].type == NULL) {
1820 ret = -ENOMEM;
1821 goto error;
1822 }
1823 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001824 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001825 }
1826
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001827 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001828 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001829 if (!sym) {
1830 pr_warning("Kernel symbol \'%s\' not found.\n",
1831 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001832 ret = -ENOENT;
1833 goto error;
1834 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001835
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001836 return 1;
1837error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301838 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001839 free(tev);
1840 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001841 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001842}
1843
1844struct __event_package {
1845 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301846 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001847 int ntevs;
1848};
1849
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001850int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001851 int max_tevs, const char *module, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001852{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001853 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001854 struct __event_package *pkgs;
1855
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001856 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1857 if (pkgs == NULL)
1858 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001859
1860 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001861 ret = init_vmlinux();
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001862 if (ret < 0) {
1863 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001864 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001865 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001866
1867 /* Loop 1: convert all events */
1868 for (i = 0; i < npevs; i++) {
1869 pkgs[i].pev = &pevs[i];
1870 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301871 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001872 &pkgs[i].tevs,
1873 max_tevs,
1874 module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001875 if (ret < 0)
1876 goto end;
1877 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001878 }
1879
1880 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03001881 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301882 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001883 pkgs[i].ntevs, force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03001884 if (ret < 0)
1885 break;
1886 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001887end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001888 /* Loop 3: cleanup and free trace events */
1889 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001890 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301891 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001892 free(pkgs[i].tevs);
1893 }
1894 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001895
1896 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001897}
1898
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301899static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001900{
1901 char *p;
1902 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001903 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001904
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301905 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001906 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1907 if (ret < 0)
1908 goto error;
1909
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001910 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001911 if (!p) {
1912 pr_debug("Internal error: %s should have ':' but not.\n",
1913 ent->s);
1914 ret = -ENOTSUP;
1915 goto error;
1916 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001917 *p = '/';
1918
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001919 pr_debug("Writing event: %s\n", buf);
1920 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001921 if (ret < 0)
1922 goto error;
1923
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001924 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001925 return 0;
1926error:
1927 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1928 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001929}
1930
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301931static int del_trace_probe_event(int fd, const char *group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001932 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001933{
1934 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001935 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001936 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001937
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001938 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1939 if (ret < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001940 pr_err("Failed to copy event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001941 return ret;
1942 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001943
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001944 if (strpbrk(buf, "*?")) { /* Glob-exp */
1945 strlist__for_each_safe(ent, n, namelist)
1946 if (strglobmatch(ent->s, buf)) {
1947 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301948 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001949 if (ret < 0)
1950 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001951 strlist__remove(namelist, ent);
1952 }
1953 } else {
1954 ent = strlist__find(namelist, buf);
1955 if (ent) {
1956 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301957 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001958 if (ret >= 0)
1959 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001960 }
1961 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001962 if (found == 0 && ret >= 0)
1963 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1964
1965 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001966}
1967
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001968int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001969{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001970 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001971 const char *group, *event;
1972 char *p, *str;
1973 struct str_node *ent;
1974 struct strlist *namelist;
1975
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001976 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001977 if (fd < 0)
1978 return fd;
1979
Masami Hiramatsufa282442009-12-08 17:03:23 -05001980 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301981 namelist = get_probe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001982 if (namelist == NULL)
1983 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001984
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001985 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001986 str = strdup(ent->s);
1987 if (str == NULL) {
1988 ret = -ENOMEM;
1989 break;
1990 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001991 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001992 p = strchr(str, ':');
1993 if (p) {
1994 group = str;
1995 *p = '\0';
1996 event = p + 1;
1997 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001998 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001999 event = str;
2000 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002001 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302002 ret = del_trace_probe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002003 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002004 if (ret < 0)
2005 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002006 }
2007 strlist__delete(namelist);
2008 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002009
2010 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002011}
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002012/* TODO: don't use a global variable for filter ... */
2013static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002014
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002015/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002016 * If a symbol corresponds to a function with global binding and
2017 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002018 */
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002019static int filter_available_functions(struct map *map __unused,
2020 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002021{
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002022 if (sym->binding == STB_GLOBAL &&
2023 strfilter__compare(available_func_filter, sym->name))
2024 return 0;
2025 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002026}
2027
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002028int show_available_funcs(const char *module, struct strfilter *_filter)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002029{
2030 struct map *map;
2031 int ret;
2032
2033 setup_pager();
2034
2035 ret = init_vmlinux();
2036 if (ret < 0)
2037 return ret;
2038
2039 map = kernel_get_module_map(module);
2040 if (!map) {
2041 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
2042 return -EINVAL;
2043 }
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002044 available_func_filter = _filter;
2045 if (map__load(map, filter_available_functions)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002046 pr_err("Failed to load map.\n");
2047 return -EINVAL;
2048 }
2049 if (!dso__sorted_by_name(map->dso, map->type))
2050 dso__sort_by_name(map->dso, map->type);
2051
2052 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2053 return 0;
2054}