blob: 80cc0bc284fd0bfdf50ae4a993eef6cd98d34c1c [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 Hiramatsu50656ee2009-11-30 19:19:58 -050034
35#undef _GNU_SOURCE
Masami Hiramatsu31facc52010-03-16 18:05:30 -040036#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050037#include "event.h"
Masami Hiramatsue1c01d62009-11-30 19:20:05 -050038#include "string.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050039#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050040#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050041#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050042#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040043#include "symbol.h"
44#include "thread.h"
Masami Hiramatsu7ca59892010-04-14 18:39:28 -040045#include "debugfs.h"
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030046#include "trace-event.h" /* For __unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050047#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040048#include "probe-finder.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050049
50#define MAX_CMDLEN 256
51#define MAX_PROBE_ARGS 128
52#define PERFPROBE_GROUP "probe"
53
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040054bool probe_event_dry_run; /* Dry run flag */
55
Masami Hiramatsu146a1432010-04-12 13:17:42 -040056#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050057
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050058/* If there is no space to write, returns -E2BIG. */
59static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050060 __attribute__((format(printf, 3, 4)));
61
62static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050063{
64 int ret;
65 va_list ap;
66 va_start(ap, format);
67 ret = vsnprintf(str, size, format, ap);
68 va_end(ap);
69 if (ret >= (int)size)
70 ret = -E2BIG;
71 return ret;
72}
73
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030074static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030075static struct machine machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040076
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090077/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsu146a1432010-04-12 13:17:42 -040078static int init_vmlinux(void)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040079{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040080 int ret;
81
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040082 symbol_conf.sort_by_name = true;
83 if (symbol_conf.vmlinux_name == NULL)
84 symbol_conf.try_vmlinux_path = true;
85 else
86 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040087 ret = symbol__init();
88 if (ret < 0) {
89 pr_debug("Failed to init symbol map.\n");
90 goto out;
91 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040092
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090093 ret = machine__init(&machine, "", HOST_KERNEL_ID);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030094 if (ret < 0)
95 goto out;
96
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090097 if (machine__create_kernel_maps(&machine) < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +090098 pr_debug("machine__create_kernel_maps() failed.\n");
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090099 goto out;
100 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400101out:
102 if (ret < 0)
103 pr_warning("Failed to init vmlinux path.\n");
104 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400105}
106
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900107static struct symbol *__find_kernel_function_by_name(const char *name,
108 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400109{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900110 return machine__find_kernel_function_by_name(&machine, name, mapp,
111 NULL);
112}
113
114const char *kernel_get_module_path(const char *module)
115{
116 struct dso *dso;
117
118 if (module) {
119 list_for_each_entry(dso, &machine.kernel_dsos, node) {
120 if (strncmp(dso->short_name + 1, module,
121 dso->short_name_len - 2) == 0)
122 goto found;
123 }
124 pr_debug("Failed to find module %s.\n", module);
125 return NULL;
126 } else {
127 dso = machine.vmlinux_maps[MAP__FUNCTION]->dso;
128 if (dso__load_vmlinux_path(dso,
129 machine.vmlinux_maps[MAP__FUNCTION], NULL) < 0) {
130 pr_debug("Failed to load kernel map.\n");
131 return NULL;
132 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400133 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900134found:
135 return dso->long_name;
136}
137
138#ifdef DWARF_SUPPORT
139static int open_vmlinux(const char *module)
140{
141 const char *path = kernel_get_module_path(module);
142 if (!path) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900143 pr_err("Failed to find path of %s module.\n",
144 module ?: "kernel");
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900145 return -ENOENT;
146 }
147 pr_debug("Try to open %s\n", path);
148 return open(path, O_RDONLY);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400149}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300150
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530151/*
152 * Convert trace point to probe point with debuginfo
153 * Currently only handles kprobes.
154 */
155static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900156 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300157{
158 struct symbol *sym;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900159 struct map *map;
160 u64 addr;
161 int ret = -ENOENT;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300162
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900163 sym = __find_kernel_function_by_name(tp->symbol, &map);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300164 if (sym) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900165 addr = map->unmap_ip(map, sym->start + tp->offset);
166 pr_debug("try to find %s+%ld@%llx\n", tp->symbol,
167 tp->offset, addr);
168 ret = find_perf_probe_point((unsigned long)addr, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300169 }
170 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400171 pr_debug("Failed to find corresponding probes from "
172 "debuginfo. Use kprobe event information.\n");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400173 pp->function = strdup(tp->symbol);
174 if (pp->function == NULL)
175 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300176 pp->offset = tp->offset;
177 }
178 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400179
180 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300181}
182
183/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530184static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
185 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900186 int max_tevs, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300187{
188 bool need_dwarf = perf_probe_event_need_dwarf(pev);
189 int fd, ntevs;
190
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900191 fd = open_vmlinux(module);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300192 if (fd < 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400193 if (need_dwarf) {
194 pr_warning("Failed to open debuginfo file.\n");
195 return fd;
196 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300197 pr_debug("Could not open vmlinux. Try to use symbols.\n");
198 return 0;
199 }
200
201 /* Searching trace events corresponding to probe event */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530202 ntevs = find_probe_trace_events(fd, pev, tevs, max_tevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300203 close(fd);
204
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400205 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530206 pr_debug("find %d probe_trace_events.\n", ntevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300207 return ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400208 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300209
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400210 if (ntevs == 0) { /* No error but failed to find probe point. */
211 pr_warning("Probe point '%s' not found.\n",
212 synthesize_perf_probe_point(&pev->point));
213 return -ENOENT;
214 }
215 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400216 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
217 if (ntevs == -EBADF) {
218 pr_warning("Warning: No dwarf info found in the vmlinux - "
219 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
220 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900221 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400222 return 0;
223 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300224 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400225 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300226}
227
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900228/*
229 * Find a src file from a DWARF tag path. Prepend optional source path prefix
230 * and chop off leading directories that do not exist. Result is passed back as
231 * a newly allocated path on success.
232 * Return 0 if file was found and readable, -errno otherwise.
233 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900234static int get_real_path(const char *raw_path, const char *comp_dir,
235 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900236{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900237 const char *prefix = symbol_conf.source_prefix;
238
239 if (!prefix) {
240 if (raw_path[0] != '/' && comp_dir)
241 /* If not an absolute path, try to use comp_dir */
242 prefix = comp_dir;
243 else {
244 if (access(raw_path, R_OK) == 0) {
245 *new_path = strdup(raw_path);
246 return 0;
247 } else
248 return -errno;
249 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900250 }
251
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900252 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900253 if (!*new_path)
254 return -ENOMEM;
255
256 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900257 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900258
259 if (access(*new_path, R_OK) == 0)
260 return 0;
261
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900262 if (!symbol_conf.source_prefix)
263 /* In case of searching comp_dir, don't retry */
264 return -errno;
265
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900266 switch (errno) {
267 case ENAMETOOLONG:
268 case ENOENT:
269 case EROFS:
270 case EFAULT:
271 raw_path = strchr(++raw_path, '/');
272 if (!raw_path) {
273 free(*new_path);
274 *new_path = NULL;
275 return -ENOENT;
276 }
277 continue;
278
279 default:
280 free(*new_path);
281 *new_path = NULL;
282 return -errno;
283 }
284 }
285}
286
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300287#define LINEBUF_SIZE 256
288#define NR_ADDITIONAL_LINES 2
289
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100290static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300291{
292 char buf[LINEBUF_SIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100293 const char *color = show_num ? "" : PERF_COLOR_BLUE;
294 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300295
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100296 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300297 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
298 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100299 if (skip)
300 continue;
301 if (!prefix) {
302 prefix = show_num ? "%7d " : " ";
303 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300304 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100305 color_fprintf(stdout, color, "%s", buf);
306
307 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400308
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100309 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300310error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100311 if (ferror(fp)) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400312 pr_warning("Source file is shorter than expected.\n");
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100313 return -1;
314 }
315 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300316}
317
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100318static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
319{
320 int rv = __show_one_line(fp, l, skip, show_num);
321 if (rv == 0) {
322 pr_warning("Source file is shorter than expected.\n");
323 rv = -1;
324 }
325 return rv;
326}
327
328#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
329#define show_one_line(f,l) _show_one_line(f,l,false,false)
330#define skip_one_line(f,l) _show_one_line(f,l,true,false)
331#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
332
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300333/*
334 * Show line-range always requires debuginfo to find source file and
335 * line number.
336 */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900337int show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300338{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400339 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300340 struct line_node *ln;
341 FILE *fp;
342 int fd, ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900343 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300344
345 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400346 ret = init_vmlinux();
347 if (ret < 0)
348 return ret;
349
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900350 fd = open_vmlinux(module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400351 if (fd < 0) {
352 pr_warning("Failed to open debuginfo file.\n");
353 return fd;
354 }
355
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300356 ret = find_line_range(fd, lr);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300357 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400358 if (ret == 0) {
359 pr_warning("Specified source line is not found.\n");
360 return -ENOENT;
361 } else if (ret < 0) {
362 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
363 return ret;
364 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300365
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900366 /* Convert source file path */
367 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900368 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900369 free(tmp); /* Free old path */
370 if (ret < 0) {
371 pr_warning("Failed to find source file. (%d)\n", ret);
372 return ret;
373 }
374
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300375 setup_pager();
376
377 if (lr->function)
378 fprintf(stdout, "<%s:%d>\n", lr->function,
379 lr->start - lr->offset);
380 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100381 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300382
383 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400384 if (fp == NULL) {
385 pr_warning("Failed to open %s: %s\n", lr->path,
386 strerror(errno));
387 return -errno;
388 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300389 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100390 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100391 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100392 if (ret < 0)
393 goto end;
394 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300395
396 list_for_each_entry(ln, &lr->line_list, list) {
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100397 for (; ln->line > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100398 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100399 if (ret < 0)
400 goto end;
401 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100402 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400403 if (ret < 0)
404 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300405 }
406
407 if (lr->end == INT_MAX)
408 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100409 while (l <= lr->end) {
410 ret = show_one_line_or_eof(fp, l++ - lr->offset);
411 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100412 break;
413 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400414end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300415 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400416 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300417}
418
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900419static int show_available_vars_at(int fd, struct perf_probe_event *pev,
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900420 int max_vls, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900421{
422 char *buf;
423 int ret, i;
424 struct str_node *node;
425 struct variable_list *vls = NULL, *vl;
426
427 buf = synthesize_perf_probe_point(&pev->point);
428 if (!buf)
429 return -EINVAL;
430 pr_debug("Searching variables at %s\n", buf);
431
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900432 ret = find_available_vars_at(fd, pev, &vls, max_vls, externs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900433 if (ret > 0) {
434 /* Some variables were found */
435 fprintf(stdout, "Available variables at %s\n", buf);
436 for (i = 0; i < ret; i++) {
437 vl = &vls[i];
438 /*
439 * A probe point might be converted to
440 * several trace points.
441 */
442 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
443 vl->point.offset);
444 free(vl->point.symbol);
445 if (vl->vars) {
446 strlist__for_each(node, vl->vars)
447 fprintf(stdout, "\t\t%s\n", node->s);
448 strlist__delete(vl->vars);
449 } else
450 fprintf(stdout, "(No variables)\n");
451 }
452 free(vls);
453 } else
454 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
455
456 free(buf);
457 return ret;
458}
459
460/* Show available variables on given probe point */
461int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900462 int max_vls, const char *module, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900463{
464 int i, fd, ret = 0;
465
466 ret = init_vmlinux();
467 if (ret < 0)
468 return ret;
469
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900470 fd = open_vmlinux(module);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900471 if (fd < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900472 pr_warning("Failed to open debug information file.\n");
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900473 return fd;
474 }
475
476 setup_pager();
477
478 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900479 ret = show_available_vars_at(fd, &pevs[i], max_vls, externs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900480
481 close(fd);
482 return ret;
483}
484
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300485#else /* !DWARF_SUPPORT */
486
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530487static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900488 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300489{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900490 struct symbol *sym;
491
492 sym = __find_kernel_function_by_name(tp->symbol, NULL);
493 if (!sym) {
494 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
495 return -ENOENT;
496 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400497 pp->function = strdup(tp->symbol);
498 if (pp->function == NULL)
499 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300500 pp->offset = tp->offset;
501 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400502
503 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300504}
505
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530506static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
507 struct probe_trace_event **tevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900508 int max_tevs __unused, const char *mod __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300509{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400510 if (perf_probe_event_need_dwarf(pev)) {
511 pr_warning("Debuginfo-analysis is not supported.\n");
512 return -ENOSYS;
513 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300514 return 0;
515}
516
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900517int show_line_range(struct line_range *lr __unused, const char *module __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300518{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400519 pr_warning("Debuginfo-analysis is not supported.\n");
520 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300521}
522
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900523int show_available_vars(struct perf_probe_event *pevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900524 int npevs __unused, int max_vls __unused,
525 const char *module __unused, bool externs __unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900526{
527 pr_warning("Debuginfo-analysis is not supported.\n");
528 return -ENOSYS;
529}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400530#endif
531
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100532/*
533 * Stuff 'lr' according to the line range described by 'arg'.
534 * The line range syntax is described by:
535 *
536 * SRC[:SLN[+NUM|-ELN]]
537 * FNC[:SLN[+NUM|-ELN]]
538 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400539int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500540{
541 const char *ptr;
542 char *tmp;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100543
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500544 ptr = strchr(arg, ':');
545 if (ptr) {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400546 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400547 if (*tmp == '+') {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400548 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400549 lr->end--; /*
550 * Adjust the number of lines here.
551 * If the number of lines == 1, the
552 * the end of line should be equal to
553 * the start of line.
554 */
555 } else if (*tmp == '-')
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400556 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500557 else
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400558 lr->end = INT_MAX;
559 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
560 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500561 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400562 " than end line.\n");
563 return -EINVAL;
564 }
565 if (*tmp != '\0') {
566 semantic_error("Tailing with invalid character '%d'.\n",
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500567 *tmp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400568 return -EINVAL;
569 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400570 tmp = strndup(arg, (ptr - arg));
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400571 } else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400572 tmp = strdup(arg);
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400573 lr->end = INT_MAX;
574 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400575
576 if (tmp == NULL)
577 return -ENOMEM;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500578
579 if (strchr(tmp, '.'))
580 lr->file = tmp;
581 else
582 lr->function = tmp;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400583
584 return 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500585}
586
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500587/* Check the name is good for event/group */
588static bool check_event_name(const char *name)
589{
590 if (!isalpha(*name) && *name != '_')
591 return false;
592 while (*++name != '\0') {
593 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
594 return false;
595 }
596 return true;
597}
598
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500599/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400600static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500601{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400602 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500603 char *ptr, *tmp;
604 char c, nc = 0;
605 /*
606 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500607 * perf probe [EVENT=]SRC[:LN|;PTN]
608 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500609 *
610 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500611 */
612
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500613 ptr = strpbrk(arg, ";=@+%");
614 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500615 *ptr = '\0';
616 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400617 if (strchr(arg, ':')) {
618 semantic_error("Group name is not supported yet.\n");
619 return -ENOTSUP;
620 }
621 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500622 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400623 "follow C symbol-naming rule.\n", arg);
624 return -EINVAL;
625 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400626 pev->event = strdup(arg);
627 if (pev->event == NULL)
628 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400629 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500630 arg = tmp;
631 }
632
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500633 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500634 if (ptr) {
635 nc = *ptr;
636 *ptr++ = '\0';
637 }
638
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400639 tmp = strdup(arg);
640 if (tmp == NULL)
641 return -ENOMEM;
642
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500643 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400644 if (strchr(tmp, '.')) /* File */
645 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500646 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400647 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500648
649 /* Parse other options */
650 while (ptr) {
651 arg = ptr;
652 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500653 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400654 pp->lazy_line = strdup(arg);
655 if (pp->lazy_line == NULL)
656 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500657 break;
658 }
659 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500660 if (ptr) {
661 nc = *ptr;
662 *ptr++ = '\0';
663 }
664 switch (c) {
665 case ':': /* Line number */
666 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400667 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500668 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400669 " in line number.\n");
670 return -EINVAL;
671 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500672 break;
673 case '+': /* Byte offset from a symbol */
674 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400675 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500676 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400677 " in offset.\n");
678 return -EINVAL;
679 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500680 break;
681 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400682 if (pp->file) {
683 semantic_error("SRC@SRC is not allowed.\n");
684 return -EINVAL;
685 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400686 pp->file = strdup(arg);
687 if (pp->file == NULL)
688 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500689 break;
690 case '%': /* Probe places */
691 if (strcmp(arg, "return") == 0) {
692 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400693 } else { /* Others not supported yet */
694 semantic_error("%%%s is not supported.\n", arg);
695 return -ENOTSUP;
696 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500697 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400698 default: /* Buggy case */
699 pr_err("This program has a bug at %s:%d.\n",
700 __FILE__, __LINE__);
701 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500702 break;
703 }
704 }
705
706 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400707 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900708 semantic_error("Lazy pattern can't be used with"
709 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400710 return -EINVAL;
711 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500712
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400713 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900714 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400715 return -EINVAL;
716 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500717
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400718 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900719 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400720 return -EINVAL;
721 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500722
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400723 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500724 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900725 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400726 return -EINVAL;
727 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500728
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400729 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900730 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400731 return -EINVAL;
732 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500733
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400734 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900735 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400736 return -EINVAL;
737 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500738
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400739 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500740 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900741 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400742 return -EINVAL;
743 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500744
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400745 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500746 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
747 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400748 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500749}
750
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400751/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400752static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400753{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400754 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400755 struct perf_probe_arg_field **fieldp;
756
757 pr_debug("parsing arg: %s into ", str);
758
Masami Hiramatsu48481932010-04-12 13:16:53 -0400759 tmp = strchr(str, '=');
760 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400761 arg->name = strndup(str, tmp - str);
762 if (arg->name == NULL)
763 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400764 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400765 str = tmp + 1;
766 }
767
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400768 tmp = strchr(str, ':');
769 if (tmp) { /* Type setting */
770 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400771 arg->type = strdup(tmp + 1);
772 if (arg->type == NULL)
773 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400774 pr_debug("type:%s ", arg->type);
775 }
776
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400777 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400778 if (!is_c_varname(str) || !tmp) {
779 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400780 arg->var = strdup(str);
781 if (arg->var == NULL)
782 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400783 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400784 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400785 }
786
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400787 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400788 arg->var = strndup(str, tmp - str);
789 if (arg->var == NULL)
790 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400791 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400792 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400793 fieldp = &arg->field;
794
795 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400796 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
797 if (*fieldp == NULL)
798 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400799 if (*tmp == '[') { /* Array */
800 str = tmp;
801 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400802 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400803 if (*tmp != ']' || tmp == str + 1) {
804 semantic_error("Array index must be a"
805 " number.\n");
806 return -EINVAL;
807 }
808 tmp++;
809 if (*tmp == '\0')
810 tmp = NULL;
811 } else { /* Structure */
812 if (*tmp == '.') {
813 str = tmp + 1;
814 (*fieldp)->ref = false;
815 } else if (tmp[1] == '>') {
816 str = tmp + 2;
817 (*fieldp)->ref = true;
818 } else {
819 semantic_error("Argument parse error: %s\n",
820 str);
821 return -EINVAL;
822 }
823 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400824 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400825 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400826 (*fieldp)->name = strndup(str, tmp - str);
827 if ((*fieldp)->name == NULL)
828 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400829 if (*str != '[')
830 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400831 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
832 fieldp = &(*fieldp)->next;
833 }
834 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400835 (*fieldp)->name = strdup(str);
836 if ((*fieldp)->name == NULL)
837 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400838 if (*str != '[')
839 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400840 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400841
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400842 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400843 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400844 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400845 if (arg->name == NULL)
846 return -ENOMEM;
847 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400848 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400849}
850
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400851/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400852int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500853{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500854 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400855 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500856
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400857 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400858 if (!argv) {
859 pr_debug("Failed to split arguments.\n");
860 return -ENOMEM;
861 }
862 if (argc - 1 > MAX_PROBE_ARGS) {
863 semantic_error("Too many probe arguments (%d).\n", argc - 1);
864 ret = -ERANGE;
865 goto out;
866 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500867 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400868 ret = parse_perf_probe_point(argv[0], pev);
869 if (ret < 0)
870 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500871
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500872 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400873 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400874 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
875 if (pev->args == NULL) {
876 ret = -ENOMEM;
877 goto out;
878 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400879 for (i = 0; i < pev->nargs && ret >= 0; i++) {
880 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
881 if (ret >= 0 &&
882 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400883 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400884 " kretprobe.\n");
885 ret = -EINVAL;
886 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500887 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400888out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500889 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400890
891 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500892}
893
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400894/* Return true if this perf_probe_event requires debuginfo */
895bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500896{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400897 int i;
898
899 if (pev->point.file || pev->point.line || pev->point.lazy_line)
900 return true;
901
902 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400903 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400904 return true;
905
906 return false;
907}
908
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530909/* Parse probe_events event into struct probe_point */
910static int parse_probe_trace_command(const char *cmd,
911 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400912{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530913 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500914 char pr;
915 char *p;
916 int ret, i, argc;
917 char **argv;
918
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530919 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400920 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400921 if (!argv) {
922 pr_debug("Failed to split arguments.\n");
923 return -ENOMEM;
924 }
925 if (argc < 2) {
926 semantic_error("Too few probe arguments.\n");
927 ret = -ERANGE;
928 goto out;
929 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500930
931 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800932 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400933 &pr, (float *)(void *)&tev->group,
934 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400935 if (ret != 3) {
936 semantic_error("Failed to parse event name: %s\n", argv[0]);
937 ret = -EINVAL;
938 goto out;
939 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400940 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500941
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400942 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500943
944 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400945 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
946 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500947 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400948 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500949
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400950 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530951 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400952 if (tev->args == NULL) {
953 ret = -ENOMEM;
954 goto out;
955 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400956 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500957 p = strchr(argv[i + 2], '=');
958 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400959 *p++ = '\0';
960 else
961 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400962 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400963 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400964 tev->args[i].value = strdup(p);
965 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
966 ret = -ENOMEM;
967 goto out;
968 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500969 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400970 ret = 0;
971out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500972 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400973 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500974}
975
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400976/* Compose only probe arg */
977int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
978{
979 struct perf_probe_arg_field *field = pa->field;
980 int ret;
981 char *tmp = buf;
982
Masami Hiramatsu48481932010-04-12 13:16:53 -0400983 if (pa->name && pa->var)
984 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
985 else
986 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400987 if (ret <= 0)
988 goto error;
989 tmp += ret;
990 len -= ret;
991
992 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400993 if (field->name[0] == '[')
994 ret = e_snprintf(tmp, len, "%s", field->name);
995 else
996 ret = e_snprintf(tmp, len, "%s%s",
997 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400998 if (ret <= 0)
999 goto error;
1000 tmp += ret;
1001 len -= ret;
1002 field = field->next;
1003 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001004
1005 if (pa->type) {
1006 ret = e_snprintf(tmp, len, ":%s", pa->type);
1007 if (ret <= 0)
1008 goto error;
1009 tmp += ret;
1010 len -= ret;
1011 }
1012
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001013 return tmp - buf;
1014error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001015 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001016 strerror(-ret));
1017 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001018}
1019
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001020/* Compose only probe point (not argument) */
1021static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001022{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001023 char *buf, *tmp;
1024 char offs[32] = "", line[32] = "", file[32] = "";
1025 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001026
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001027 buf = zalloc(MAX_CMDLEN);
1028 if (buf == NULL) {
1029 ret = -ENOMEM;
1030 goto error;
1031 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001032 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001033 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001034 if (ret <= 0)
1035 goto error;
1036 }
1037 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001038 ret = e_snprintf(line, 32, ":%d", pp->line);
1039 if (ret <= 0)
1040 goto error;
1041 }
1042 if (pp->file) {
Masami Hiramatsudd259c52010-04-14 18:39:35 -04001043 len = strlen(pp->file) - 31;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001044 if (len < 0)
1045 len = 0;
1046 tmp = strchr(pp->file + len, '/');
1047 if (!tmp)
Masami Hiramatsudd259c52010-04-14 18:39:35 -04001048 tmp = pp->file + len;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001049 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001050 if (ret <= 0)
1051 goto error;
1052 }
1053
1054 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001055 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1056 offs, pp->retprobe ? "%return" : "", line,
1057 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001058 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001059 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001060 if (ret <= 0)
1061 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001062
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001063 return buf;
1064error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001065 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001066 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001067 if (buf)
1068 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001069 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001070}
1071
1072#if 0
1073char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1074{
1075 char *buf;
1076 int i, len, ret;
1077
1078 buf = synthesize_perf_probe_point(&pev->point);
1079 if (!buf)
1080 return NULL;
1081
1082 len = strlen(buf);
1083 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001084 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001085 pev->args[i].name);
1086 if (ret <= 0) {
1087 free(buf);
1088 return NULL;
1089 }
1090 len += ret;
1091 }
1092
1093 return buf;
1094}
1095#endif
1096
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301097static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001098 char **buf, size_t *buflen,
1099 int depth)
1100{
1101 int ret;
1102 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301103 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001104 buflen, depth + 1);
1105 if (depth < 0)
1106 goto out;
1107 }
1108
1109 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1110 if (ret < 0)
1111 depth = ret;
1112 else {
1113 *buf += ret;
1114 *buflen -= ret;
1115 }
1116out:
1117 return depth;
1118
1119}
1120
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301121static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001122 char *buf, size_t buflen)
1123{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301124 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001125 int ret, depth = 0;
1126 char *tmp = buf;
1127
1128 /* Argument name or separator */
1129 if (arg->name)
1130 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1131 else
1132 ret = e_snprintf(buf, buflen, " ");
1133 if (ret < 0)
1134 return ret;
1135 buf += ret;
1136 buflen -= ret;
1137
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001138 /* Special case: @XXX */
1139 if (arg->value[0] == '@' && arg->ref)
1140 ref = ref->next;
1141
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001142 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001143 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301144 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001145 &buflen, 1);
1146 if (depth < 0)
1147 return depth;
1148 }
1149
1150 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001151 if (arg->value[0] == '@' && arg->ref)
1152 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1153 arg->ref->offset);
1154 else
1155 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001156 if (ret < 0)
1157 return ret;
1158 buf += ret;
1159 buflen -= ret;
1160
1161 /* Closing */
1162 while (depth--) {
1163 ret = e_snprintf(buf, buflen, ")");
1164 if (ret < 0)
1165 return ret;
1166 buf += ret;
1167 buflen -= ret;
1168 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001169 /* Print argument type */
1170 if (arg->type) {
1171 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1172 if (ret <= 0)
1173 return ret;
1174 buf += ret;
1175 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001176
1177 return buf - tmp;
1178}
1179
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301180char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001181{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301182 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001183 char *buf;
1184 int i, len, ret;
1185
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001186 buf = zalloc(MAX_CMDLEN);
1187 if (buf == NULL)
1188 return NULL;
1189
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001190 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1191 tp->retprobe ? 'r' : 'p',
1192 tev->group, tev->event,
1193 tp->symbol, tp->offset);
1194 if (len <= 0)
1195 goto error;
1196
1197 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301198 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001199 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001200 if (ret <= 0)
1201 goto error;
1202 len += ret;
1203 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001204
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001205 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001206error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001207 free(buf);
1208 return NULL;
1209}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001210
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301211static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001212 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001213{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001214 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001215 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001216
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001217 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001218 pev->event = strdup(tev->event);
1219 pev->group = strdup(tev->group);
1220 if (pev->event == NULL || pev->group == NULL)
1221 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001222
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001223 /* Convert trace_point to probe_point */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301224 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001225 if (ret < 0)
1226 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001227
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001228 /* Convert trace_arg to probe_arg */
1229 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001230 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1231 if (pev->args == NULL)
1232 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001233 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001234 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001235 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001236 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301237 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001238 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001239 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001240 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001241 if (pev->args[i].name == NULL && ret >= 0)
1242 ret = -ENOMEM;
1243 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001244
1245 if (ret < 0)
1246 clear_perf_probe_event(pev);
1247
1248 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001249}
1250
1251void clear_perf_probe_event(struct perf_probe_event *pev)
1252{
1253 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001254 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001255 int i;
1256
1257 if (pev->event)
1258 free(pev->event);
1259 if (pev->group)
1260 free(pev->group);
1261 if (pp->file)
1262 free(pp->file);
1263 if (pp->function)
1264 free(pp->function);
1265 if (pp->lazy_line)
1266 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001267 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001268 if (pev->args[i].name)
1269 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001270 if (pev->args[i].var)
1271 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001272 if (pev->args[i].type)
1273 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001274 field = pev->args[i].field;
1275 while (field) {
1276 next = field->next;
1277 if (field->name)
1278 free(field->name);
1279 free(field);
1280 field = next;
1281 }
1282 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001283 if (pev->args)
1284 free(pev->args);
1285 memset(pev, 0, sizeof(*pev));
1286}
1287
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301288static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001289{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301290 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001291 int i;
1292
1293 if (tev->event)
1294 free(tev->event);
1295 if (tev->group)
1296 free(tev->group);
1297 if (tev->point.symbol)
1298 free(tev->point.symbol);
1299 for (i = 0; i < tev->nargs; i++) {
1300 if (tev->args[i].name)
1301 free(tev->args[i].name);
1302 if (tev->args[i].value)
1303 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001304 if (tev->args[i].type)
1305 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001306 ref = tev->args[i].ref;
1307 while (ref) {
1308 next = ref->next;
1309 free(ref);
1310 ref = next;
1311 }
1312 }
1313 if (tev->args)
1314 free(tev->args);
1315 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001316}
1317
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001318static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001319{
1320 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001321 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001322 int ret;
1323
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001324 __debugfs = debugfs_find_mountpoint();
1325 if (__debugfs == NULL) {
1326 pr_warning("Debugfs is not mounted.\n");
1327 return -ENOENT;
1328 }
1329
1330 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001331 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001332 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001333 if (readwrite && !probe_event_dry_run)
1334 ret = open(buf, O_RDWR, O_APPEND);
1335 else
1336 ret = open(buf, O_RDONLY, 0);
1337 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001338
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001339 if (ret < 0) {
1340 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001341 pr_warning("kprobe_events file does not exist - please"
1342 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001343 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001344 pr_warning("Failed to open kprobe_events file: %s\n",
1345 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001346 }
1347 return ret;
1348}
1349
1350/* Get raw string list of current kprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301351static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001352{
1353 int ret, idx;
1354 FILE *fp;
1355 char buf[MAX_CMDLEN];
1356 char *p;
1357 struct strlist *sl;
1358
1359 sl = strlist__new(true, NULL);
1360
1361 fp = fdopen(dup(fd), "r");
1362 while (!feof(fp)) {
1363 p = fgets(buf, MAX_CMDLEN, fp);
1364 if (!p)
1365 break;
1366
1367 idx = strlen(p) - 1;
1368 if (p[idx] == '\n')
1369 p[idx] = '\0';
1370 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001371 if (ret < 0) {
1372 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1373 strlist__delete(sl);
1374 return NULL;
1375 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001376 }
1377 fclose(fp);
1378
1379 return sl;
1380}
1381
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001382/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001383static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001384{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001385 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001386 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001387 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001388
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001389 /* Synthesize only event probe point */
1390 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001391 if (!place)
1392 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001393
1394 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001395 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001396 return ret;
1397
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001398 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001399
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001400 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001401 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001402 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001403 ret = synthesize_perf_probe_arg(&pev->args[i],
1404 buf, 128);
1405 if (ret < 0)
1406 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001407 printf(" %s", buf);
1408 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001409 }
1410 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001411 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001412 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001413}
1414
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001415/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001416int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001417{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001418 int fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301419 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001420 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001421 struct strlist *rawlist;
1422 struct str_node *ent;
1423
Masami Hiramatsu72041332010-01-05 17:47:10 -05001424 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001425 ret = init_vmlinux();
1426 if (ret < 0)
1427 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001428
1429 memset(&tev, 0, sizeof(tev));
1430 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001431
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001432 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001433 if (fd < 0)
1434 return fd;
1435
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301436 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001437 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001438 if (!rawlist)
1439 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001440
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001441 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301442 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001443 if (ret >= 0) {
1444 ret = convert_to_perf_probe_event(&tev, &pev);
1445 if (ret >= 0)
1446 ret = show_perf_probe_event(&pev);
1447 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001448 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301449 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001450 if (ret < 0)
1451 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001452 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001453 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001454
1455 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001456}
1457
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001458/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301459static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001460{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001461 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001462 struct strlist *sl, *rawlist;
1463 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301464 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001465 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001466
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001467 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301468 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001469 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001470 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301471 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001472 if (ret < 0)
1473 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001474 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001475 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1476 tev.event);
1477 if (ret >= 0)
1478 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001479 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001480 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301481 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001482 if (ret < 0)
1483 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001484 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001485 strlist__delete(rawlist);
1486
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001487 if (ret < 0) {
1488 strlist__delete(sl);
1489 return NULL;
1490 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001491 return sl;
1492}
1493
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301494static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001495{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001496 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301497 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001498
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001499 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301500 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001501 return -EINVAL;
1502 }
1503
Masami Hiramatsufa282442009-12-08 17:03:23 -05001504 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001505 if (!probe_event_dry_run) {
1506 ret = write(fd, buf, strlen(buf));
1507 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001508 pr_warning("Failed to write event: %s\n",
1509 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001510 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001511 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001512 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001513}
1514
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001515static int get_new_event_name(char *buf, size_t len, const char *base,
1516 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001517{
1518 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001519
1520 /* Try no suffix */
1521 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001522 if (ret < 0) {
1523 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1524 return ret;
1525 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001526 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001527 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001528
Masami Hiramatsud761b082009-12-15 10:32:25 -05001529 if (!allow_suffix) {
1530 pr_warning("Error: event \"%s\" already exists. "
1531 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001532 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001533 }
1534
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001535 /* Try to add suffix */
1536 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001537 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001538 if (ret < 0) {
1539 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1540 return ret;
1541 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001542 if (!strlist__has_entry(namelist, buf))
1543 break;
1544 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001545 if (i == MAX_EVENT_INDEX) {
1546 pr_warning("Too many events are on the same function.\n");
1547 ret = -ERANGE;
1548 }
1549
1550 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001551}
1552
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301553static int __add_probe_trace_events(struct perf_probe_event *pev,
1554 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001555 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001556{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001557 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301558 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001559 char buf[64];
1560 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001561 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001562
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001563 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001564 if (fd < 0)
1565 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001566 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301567 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001568 if (!namelist) {
1569 pr_debug("Failed to get current event list.\n");
1570 return -EIO;
1571 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001572
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001573 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001574 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001575 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001576 tev = &tevs[i];
1577 if (pev->event)
1578 event = pev->event;
1579 else
1580 if (pev->point.function)
1581 event = pev->point.function;
1582 else
1583 event = tev->point.symbol;
1584 if (pev->group)
1585 group = pev->group;
1586 else
1587 group = PERFPROBE_GROUP;
1588
1589 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001590 ret = get_new_event_name(buf, 64, event,
1591 namelist, allow_suffix);
1592 if (ret < 0)
1593 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001594 event = buf;
1595
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001596 tev->event = strdup(event);
1597 tev->group = strdup(group);
1598 if (tev->event == NULL || tev->group == NULL) {
1599 ret = -ENOMEM;
1600 break;
1601 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301602 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001603 if (ret < 0)
1604 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001605 /* Add added event name to namelist */
1606 strlist__add(namelist, event);
1607
1608 /* Trick here - save current event/group */
1609 event = pev->event;
1610 group = pev->group;
1611 pev->event = tev->event;
1612 pev->group = tev->group;
1613 show_perf_probe_event(pev);
1614 /* Trick here - restore current event/group */
1615 pev->event = (char *)event;
1616 pev->group = (char *)group;
1617
1618 /*
1619 * Probes after the first probe which comes from same
1620 * user input are always allowed to add suffix, because
1621 * there might be several addresses corresponding to
1622 * one code line.
1623 */
1624 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001625 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001626
1627 if (ret >= 0) {
1628 /* Show how to use the event. */
1629 printf("\nYou can now use it on all perf tools, such as:\n\n");
1630 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1631 tev->event);
1632 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001633
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001634 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001635 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001636 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001637}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001638
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301639static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1640 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001641 int max_tevs, const char *module)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001642{
1643 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001644 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301645 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001646
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001647 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001648 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001649 if (ret != 0)
1650 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001651
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001652 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301653 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001654 if (tev == NULL)
1655 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001656
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001657 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001658 tev->point.symbol = strdup(pev->point.function);
1659 if (tev->point.symbol == NULL) {
1660 ret = -ENOMEM;
1661 goto error;
1662 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001663 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001664 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001665 tev->nargs = pev->nargs;
1666 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301667 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001668 * tev->nargs);
1669 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001670 ret = -ENOMEM;
1671 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001672 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001673 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001674 if (pev->args[i].name) {
1675 tev->args[i].name = strdup(pev->args[i].name);
1676 if (tev->args[i].name == NULL) {
1677 ret = -ENOMEM;
1678 goto error;
1679 }
1680 }
1681 tev->args[i].value = strdup(pev->args[i].var);
1682 if (tev->args[i].value == NULL) {
1683 ret = -ENOMEM;
1684 goto error;
1685 }
1686 if (pev->args[i].type) {
1687 tev->args[i].type = strdup(pev->args[i].type);
1688 if (tev->args[i].type == NULL) {
1689 ret = -ENOMEM;
1690 goto error;
1691 }
1692 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001693 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001694 }
1695
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001696 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001697 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001698 if (!sym) {
1699 pr_warning("Kernel symbol \'%s\' not found.\n",
1700 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001701 ret = -ENOENT;
1702 goto error;
1703 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001704
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001705 return 1;
1706error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301707 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001708 free(tev);
1709 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001710 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001711}
1712
1713struct __event_package {
1714 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301715 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001716 int ntevs;
1717};
1718
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001719int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001720 int max_tevs, const char *module, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001721{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001722 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001723 struct __event_package *pkgs;
1724
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001725 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1726 if (pkgs == NULL)
1727 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001728
1729 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001730 ret = init_vmlinux();
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001731 if (ret < 0) {
1732 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001733 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001734 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001735
1736 /* Loop 1: convert all events */
1737 for (i = 0; i < npevs; i++) {
1738 pkgs[i].pev = &pevs[i];
1739 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301740 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001741 &pkgs[i].tevs,
1742 max_tevs,
1743 module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001744 if (ret < 0)
1745 goto end;
1746 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001747 }
1748
1749 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001750 for (i = 0; i < npevs && ret >= 0; i++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301751 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001752 pkgs[i].ntevs, force_add);
1753end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001754 /* Loop 3: cleanup and free trace events */
1755 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001756 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301757 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001758 free(pkgs[i].tevs);
1759 }
1760 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001761
1762 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001763}
1764
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301765static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001766{
1767 char *p;
1768 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001769 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001770
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301771 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001772 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1773 if (ret < 0)
1774 goto error;
1775
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001776 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001777 if (!p) {
1778 pr_debug("Internal error: %s should have ':' but not.\n",
1779 ent->s);
1780 ret = -ENOTSUP;
1781 goto error;
1782 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001783 *p = '/';
1784
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001785 pr_debug("Writing event: %s\n", buf);
1786 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001787 if (ret < 0)
1788 goto error;
1789
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001790 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001791 return 0;
1792error:
1793 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1794 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001795}
1796
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301797static int del_trace_probe_event(int fd, const char *group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001798 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001799{
1800 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001801 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001802 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001803
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001804 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1805 if (ret < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001806 pr_err("Failed to copy event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001807 return ret;
1808 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001809
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001810 if (strpbrk(buf, "*?")) { /* Glob-exp */
1811 strlist__for_each_safe(ent, n, namelist)
1812 if (strglobmatch(ent->s, buf)) {
1813 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301814 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001815 if (ret < 0)
1816 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001817 strlist__remove(namelist, ent);
1818 }
1819 } else {
1820 ent = strlist__find(namelist, buf);
1821 if (ent) {
1822 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301823 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001824 if (ret >= 0)
1825 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001826 }
1827 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001828 if (found == 0 && ret >= 0)
1829 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1830
1831 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001832}
1833
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001834int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001835{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001836 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001837 const char *group, *event;
1838 char *p, *str;
1839 struct str_node *ent;
1840 struct strlist *namelist;
1841
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001842 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001843 if (fd < 0)
1844 return fd;
1845
Masami Hiramatsufa282442009-12-08 17:03:23 -05001846 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301847 namelist = get_probe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001848 if (namelist == NULL)
1849 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001850
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001851 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001852 str = strdup(ent->s);
1853 if (str == NULL) {
1854 ret = -ENOMEM;
1855 break;
1856 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001857 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001858 p = strchr(str, ':');
1859 if (p) {
1860 group = str;
1861 *p = '\0';
1862 event = p + 1;
1863 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001864 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001865 event = str;
1866 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001867 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301868 ret = del_trace_probe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001869 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001870 if (ret < 0)
1871 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001872 }
1873 strlist__delete(namelist);
1874 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001875
1876 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001877}
1878