blob: 6e29d9c9dcccb50f63adac097bc512b56fb3e28c [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;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100117 struct map *map;
118 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900119
120 if (module) {
121 list_for_each_entry(dso, &machine.kernel_dsos, node) {
122 if (strncmp(dso->short_name + 1, module,
123 dso->short_name_len - 2) == 0)
124 goto found;
125 }
126 pr_debug("Failed to find module %s.\n", module);
127 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100128 }
129
130 map = machine.vmlinux_maps[MAP__FUNCTION];
131 dso = map->dso;
132
133 vmlinux_name = symbol_conf.vmlinux_name;
134 if (vmlinux_name) {
135 if (dso__load_vmlinux(dso, map, vmlinux_name, NULL) <= 0)
136 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900137 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100138 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900139 pr_debug("Failed to load kernel map.\n");
140 return NULL;
141 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400142 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900143found:
144 return dso->long_name;
145}
146
147#ifdef DWARF_SUPPORT
148static int open_vmlinux(const char *module)
149{
150 const char *path = kernel_get_module_path(module);
151 if (!path) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900152 pr_err("Failed to find path of %s module.\n",
153 module ?: "kernel");
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900154 return -ENOENT;
155 }
156 pr_debug("Try to open %s\n", path);
157 return open(path, O_RDONLY);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400158}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300159
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530160/*
161 * Convert trace point to probe point with debuginfo
162 * Currently only handles kprobes.
163 */
164static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900165 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300166{
167 struct symbol *sym;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900168 struct map *map;
169 u64 addr;
170 int ret = -ENOENT;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300171
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900172 sym = __find_kernel_function_by_name(tp->symbol, &map);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300173 if (sym) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900174 addr = map->unmap_ip(map, sym->start + tp->offset);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200175 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900176 tp->offset, addr);
177 ret = find_perf_probe_point((unsigned long)addr, pp);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300178 }
179 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400180 pr_debug("Failed to find corresponding probes from "
181 "debuginfo. Use kprobe event information.\n");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400182 pp->function = strdup(tp->symbol);
183 if (pp->function == NULL)
184 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300185 pp->offset = tp->offset;
186 }
187 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400188
189 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300190}
191
192/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530193static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
194 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900195 int max_tevs, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300196{
197 bool need_dwarf = perf_probe_event_need_dwarf(pev);
198 int fd, ntevs;
199
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900200 fd = open_vmlinux(module);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300201 if (fd < 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400202 if (need_dwarf) {
203 pr_warning("Failed to open debuginfo file.\n");
204 return fd;
205 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300206 pr_debug("Could not open vmlinux. Try to use symbols.\n");
207 return 0;
208 }
209
210 /* Searching trace events corresponding to probe event */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530211 ntevs = find_probe_trace_events(fd, pev, tevs, max_tevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300212 close(fd);
213
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400214 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530215 pr_debug("find %d probe_trace_events.\n", ntevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300216 return ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400217 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300218
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400219 if (ntevs == 0) { /* No error but failed to find probe point. */
220 pr_warning("Probe point '%s' not found.\n",
221 synthesize_perf_probe_point(&pev->point));
222 return -ENOENT;
223 }
224 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400225 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
226 if (ntevs == -EBADF) {
227 pr_warning("Warning: No dwarf info found in the vmlinux - "
228 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
229 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900230 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400231 return 0;
232 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300233 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400234 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300235}
236
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900237/*
238 * Find a src file from a DWARF tag path. Prepend optional source path prefix
239 * and chop off leading directories that do not exist. Result is passed back as
240 * a newly allocated path on success.
241 * Return 0 if file was found and readable, -errno otherwise.
242 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900243static int get_real_path(const char *raw_path, const char *comp_dir,
244 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900245{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900246 const char *prefix = symbol_conf.source_prefix;
247
248 if (!prefix) {
249 if (raw_path[0] != '/' && comp_dir)
250 /* If not an absolute path, try to use comp_dir */
251 prefix = comp_dir;
252 else {
253 if (access(raw_path, R_OK) == 0) {
254 *new_path = strdup(raw_path);
255 return 0;
256 } else
257 return -errno;
258 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900259 }
260
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900261 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900262 if (!*new_path)
263 return -ENOMEM;
264
265 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900266 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900267
268 if (access(*new_path, R_OK) == 0)
269 return 0;
270
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900271 if (!symbol_conf.source_prefix)
272 /* In case of searching comp_dir, don't retry */
273 return -errno;
274
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900275 switch (errno) {
276 case ENAMETOOLONG:
277 case ENOENT:
278 case EROFS:
279 case EFAULT:
280 raw_path = strchr(++raw_path, '/');
281 if (!raw_path) {
282 free(*new_path);
283 *new_path = NULL;
284 return -ENOENT;
285 }
286 continue;
287
288 default:
289 free(*new_path);
290 *new_path = NULL;
291 return -errno;
292 }
293 }
294}
295
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300296#define LINEBUF_SIZE 256
297#define NR_ADDITIONAL_LINES 2
298
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100299static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300300{
301 char buf[LINEBUF_SIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100302 const char *color = show_num ? "" : PERF_COLOR_BLUE;
303 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300304
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100305 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300306 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
307 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100308 if (skip)
309 continue;
310 if (!prefix) {
311 prefix = show_num ? "%7d " : " ";
312 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300313 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100314 color_fprintf(stdout, color, "%s", buf);
315
316 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400317
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100318 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300319error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100320 if (ferror(fp)) {
Franck Bui-Huu32b2b6e2010-12-22 17:37:13 +0100321 pr_warning("File read error: %s\n", strerror(errno));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100322 return -1;
323 }
324 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300325}
326
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100327static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
328{
329 int rv = __show_one_line(fp, l, skip, show_num);
330 if (rv == 0) {
331 pr_warning("Source file is shorter than expected.\n");
332 rv = -1;
333 }
334 return rv;
335}
336
337#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
338#define show_one_line(f,l) _show_one_line(f,l,false,false)
339#define skip_one_line(f,l) _show_one_line(f,l,true,false)
340#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
341
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300342/*
343 * Show line-range always requires debuginfo to find source file and
344 * line number.
345 */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900346int show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300347{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400348 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300349 struct line_node *ln;
350 FILE *fp;
351 int fd, ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900352 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300353
354 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400355 ret = init_vmlinux();
356 if (ret < 0)
357 return ret;
358
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900359 fd = open_vmlinux(module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400360 if (fd < 0) {
361 pr_warning("Failed to open debuginfo file.\n");
362 return fd;
363 }
364
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300365 ret = find_line_range(fd, lr);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300366 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400367 if (ret == 0) {
368 pr_warning("Specified source line is not found.\n");
369 return -ENOENT;
370 } else if (ret < 0) {
371 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
372 return ret;
373 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300374
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900375 /* Convert source file path */
376 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900377 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900378 free(tmp); /* Free old path */
379 if (ret < 0) {
380 pr_warning("Failed to find source file. (%d)\n", ret);
381 return ret;
382 }
383
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300384 setup_pager();
385
386 if (lr->function)
387 fprintf(stdout, "<%s:%d>\n", lr->function,
388 lr->start - lr->offset);
389 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100390 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300391
392 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400393 if (fp == NULL) {
394 pr_warning("Failed to open %s: %s\n", lr->path,
395 strerror(errno));
396 return -errno;
397 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300398 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100399 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100400 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100401 if (ret < 0)
402 goto end;
403 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300404
405 list_for_each_entry(ln, &lr->line_list, list) {
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100406 for (; ln->line > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100407 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100408 if (ret < 0)
409 goto end;
410 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100411 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400412 if (ret < 0)
413 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300414 }
415
416 if (lr->end == INT_MAX)
417 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100418 while (l <= lr->end) {
419 ret = show_one_line_or_eof(fp, l++ - lr->offset);
420 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100421 break;
422 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400423end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300424 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400425 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300426}
427
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900428static int show_available_vars_at(int fd, struct perf_probe_event *pev,
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900429 int max_vls, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900430{
431 char *buf;
432 int ret, i;
433 struct str_node *node;
434 struct variable_list *vls = NULL, *vl;
435
436 buf = synthesize_perf_probe_point(&pev->point);
437 if (!buf)
438 return -EINVAL;
439 pr_debug("Searching variables at %s\n", buf);
440
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900441 ret = find_available_vars_at(fd, pev, &vls, max_vls, externs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900442 if (ret > 0) {
443 /* Some variables were found */
444 fprintf(stdout, "Available variables at %s\n", buf);
445 for (i = 0; i < ret; i++) {
446 vl = &vls[i];
447 /*
448 * A probe point might be converted to
449 * several trace points.
450 */
451 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
452 vl->point.offset);
453 free(vl->point.symbol);
454 if (vl->vars) {
455 strlist__for_each(node, vl->vars)
456 fprintf(stdout, "\t\t%s\n", node->s);
457 strlist__delete(vl->vars);
458 } else
459 fprintf(stdout, "(No variables)\n");
460 }
461 free(vls);
462 } else
463 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
464
465 free(buf);
466 return ret;
467}
468
469/* Show available variables on given probe point */
470int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900471 int max_vls, const char *module, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900472{
473 int i, fd, ret = 0;
474
475 ret = init_vmlinux();
476 if (ret < 0)
477 return ret;
478
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900479 fd = open_vmlinux(module);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900480 if (fd < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900481 pr_warning("Failed to open debug information file.\n");
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900482 return fd;
483 }
484
485 setup_pager();
486
487 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900488 ret = show_available_vars_at(fd, &pevs[i], max_vls, externs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900489
490 close(fd);
491 return ret;
492}
493
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300494#else /* !DWARF_SUPPORT */
495
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530496static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900497 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300498{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900499 struct symbol *sym;
500
501 sym = __find_kernel_function_by_name(tp->symbol, NULL);
502 if (!sym) {
503 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
504 return -ENOENT;
505 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400506 pp->function = strdup(tp->symbol);
507 if (pp->function == NULL)
508 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300509 pp->offset = tp->offset;
510 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400511
512 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300513}
514
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530515static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
516 struct probe_trace_event **tevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900517 int max_tevs __unused, const char *mod __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300518{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400519 if (perf_probe_event_need_dwarf(pev)) {
520 pr_warning("Debuginfo-analysis is not supported.\n");
521 return -ENOSYS;
522 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300523 return 0;
524}
525
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900526int show_line_range(struct line_range *lr __unused, const char *module __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300527{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400528 pr_warning("Debuginfo-analysis is not supported.\n");
529 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300530}
531
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900532int show_available_vars(struct perf_probe_event *pevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900533 int npevs __unused, int max_vls __unused,
534 const char *module __unused, bool externs __unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900535{
536 pr_warning("Debuginfo-analysis is not supported.\n");
537 return -ENOSYS;
538}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400539#endif
540
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100541static int parse_line_num(char **ptr, int *val, const char *what)
542{
543 const char *start = *ptr;
544
545 errno = 0;
546 *val = strtol(*ptr, ptr, 0);
547 if (errno || *ptr == start) {
548 semantic_error("'%s' is not a valid number.\n", what);
549 return -EINVAL;
550 }
551 return 0;
552}
553
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100554/*
555 * Stuff 'lr' according to the line range described by 'arg'.
556 * The line range syntax is described by:
557 *
558 * SRC[:SLN[+NUM|-ELN]]
559 * FNC[:SLN[+NUM|-ELN]]
560 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400561int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500562{
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100563 char *range, *name = strdup(arg);
564 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100565
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100566 if (!name)
567 return -ENOMEM;
568
569 lr->start = 0;
570 lr->end = INT_MAX;
571
572 range = strchr(name, ':');
573 if (range) {
574 *range++ = '\0';
575
576 err = parse_line_num(&range, &lr->start, "start line");
577 if (err)
578 goto err;
579
580 if (*range == '+' || *range == '-') {
581 const char c = *range++;
582
583 err = parse_line_num(&range, &lr->end, "end line");
584 if (err)
585 goto err;
586
587 if (c == '+') {
588 lr->end += lr->start;
589 /*
590 * Adjust the number of lines here.
591 * If the number of lines == 1, the
592 * the end of line should be equal to
593 * the start of line.
594 */
595 lr->end--;
596 }
597 }
598
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400599 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100600
601 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400602 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500603 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400604 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100605 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400606 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100607 if (*range != '\0') {
608 semantic_error("Tailing with invalid str '%s'.\n", range);
609 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400610 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400611 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400612
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100613 if (strchr(name, '.'))
614 lr->file = name;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500615 else
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100616 lr->function = name;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400617
618 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100619err:
620 free(name);
621 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500622}
623
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500624/* Check the name is good for event/group */
625static bool check_event_name(const char *name)
626{
627 if (!isalpha(*name) && *name != '_')
628 return false;
629 while (*++name != '\0') {
630 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
631 return false;
632 }
633 return true;
634}
635
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500636/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400637static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500638{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400639 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500640 char *ptr, *tmp;
641 char c, nc = 0;
642 /*
643 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500644 * perf probe [EVENT=]SRC[:LN|;PTN]
645 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500646 *
647 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500648 */
649
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500650 ptr = strpbrk(arg, ";=@+%");
651 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500652 *ptr = '\0';
653 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400654 if (strchr(arg, ':')) {
655 semantic_error("Group name is not supported yet.\n");
656 return -ENOTSUP;
657 }
658 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500659 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400660 "follow C symbol-naming rule.\n", arg);
661 return -EINVAL;
662 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400663 pev->event = strdup(arg);
664 if (pev->event == NULL)
665 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400666 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500667 arg = tmp;
668 }
669
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500670 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500671 if (ptr) {
672 nc = *ptr;
673 *ptr++ = '\0';
674 }
675
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400676 tmp = strdup(arg);
677 if (tmp == NULL)
678 return -ENOMEM;
679
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500680 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400681 if (strchr(tmp, '.')) /* File */
682 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500683 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400684 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500685
686 /* Parse other options */
687 while (ptr) {
688 arg = ptr;
689 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500690 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400691 pp->lazy_line = strdup(arg);
692 if (pp->lazy_line == NULL)
693 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500694 break;
695 }
696 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500697 if (ptr) {
698 nc = *ptr;
699 *ptr++ = '\0';
700 }
701 switch (c) {
702 case ':': /* Line number */
703 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400704 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500705 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400706 " in line number.\n");
707 return -EINVAL;
708 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500709 break;
710 case '+': /* Byte offset from a symbol */
711 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400712 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500713 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400714 " in offset.\n");
715 return -EINVAL;
716 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500717 break;
718 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400719 if (pp->file) {
720 semantic_error("SRC@SRC is not allowed.\n");
721 return -EINVAL;
722 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400723 pp->file = strdup(arg);
724 if (pp->file == NULL)
725 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500726 break;
727 case '%': /* Probe places */
728 if (strcmp(arg, "return") == 0) {
729 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400730 } else { /* Others not supported yet */
731 semantic_error("%%%s is not supported.\n", arg);
732 return -ENOTSUP;
733 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500734 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400735 default: /* Buggy case */
736 pr_err("This program has a bug at %s:%d.\n",
737 __FILE__, __LINE__);
738 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500739 break;
740 }
741 }
742
743 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400744 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900745 semantic_error("Lazy pattern can't be used with"
746 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400747 return -EINVAL;
748 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500749
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400750 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900751 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400752 return -EINVAL;
753 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500754
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400755 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900756 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400757 return -EINVAL;
758 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500759
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400760 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500761 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900762 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400763 return -EINVAL;
764 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500765
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400766 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900767 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400768 return -EINVAL;
769 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500770
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400771 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900772 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400773 return -EINVAL;
774 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500775
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400776 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500777 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900778 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400779 return -EINVAL;
780 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500781
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400782 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500783 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
784 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400785 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500786}
787
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400788/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400789static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400790{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400791 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400792 struct perf_probe_arg_field **fieldp;
793
794 pr_debug("parsing arg: %s into ", str);
795
Masami Hiramatsu48481932010-04-12 13:16:53 -0400796 tmp = strchr(str, '=');
797 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400798 arg->name = strndup(str, tmp - str);
799 if (arg->name == NULL)
800 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400801 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400802 str = tmp + 1;
803 }
804
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400805 tmp = strchr(str, ':');
806 if (tmp) { /* Type setting */
807 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400808 arg->type = strdup(tmp + 1);
809 if (arg->type == NULL)
810 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400811 pr_debug("type:%s ", arg->type);
812 }
813
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400814 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400815 if (!is_c_varname(str) || !tmp) {
816 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400817 arg->var = strdup(str);
818 if (arg->var == NULL)
819 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400820 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400821 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400822 }
823
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400824 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400825 arg->var = strndup(str, tmp - str);
826 if (arg->var == NULL)
827 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400828 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400829 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400830 fieldp = &arg->field;
831
832 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400833 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
834 if (*fieldp == NULL)
835 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400836 if (*tmp == '[') { /* Array */
837 str = tmp;
838 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400839 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400840 if (*tmp != ']' || tmp == str + 1) {
841 semantic_error("Array index must be a"
842 " number.\n");
843 return -EINVAL;
844 }
845 tmp++;
846 if (*tmp == '\0')
847 tmp = NULL;
848 } else { /* Structure */
849 if (*tmp == '.') {
850 str = tmp + 1;
851 (*fieldp)->ref = false;
852 } else if (tmp[1] == '>') {
853 str = tmp + 2;
854 (*fieldp)->ref = true;
855 } else {
856 semantic_error("Argument parse error: %s\n",
857 str);
858 return -EINVAL;
859 }
860 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400861 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400862 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400863 (*fieldp)->name = strndup(str, tmp - str);
864 if ((*fieldp)->name == NULL)
865 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400866 if (*str != '[')
867 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400868 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
869 fieldp = &(*fieldp)->next;
870 }
871 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400872 (*fieldp)->name = strdup(str);
873 if ((*fieldp)->name == NULL)
874 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400875 if (*str != '[')
876 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400877 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400878
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400879 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400880 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400881 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400882 if (arg->name == NULL)
883 return -ENOMEM;
884 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400885 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400886}
887
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400888/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400889int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500890{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500891 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400892 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500893
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400894 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400895 if (!argv) {
896 pr_debug("Failed to split arguments.\n");
897 return -ENOMEM;
898 }
899 if (argc - 1 > MAX_PROBE_ARGS) {
900 semantic_error("Too many probe arguments (%d).\n", argc - 1);
901 ret = -ERANGE;
902 goto out;
903 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500904 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400905 ret = parse_perf_probe_point(argv[0], pev);
906 if (ret < 0)
907 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500908
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500909 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400910 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400911 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
912 if (pev->args == NULL) {
913 ret = -ENOMEM;
914 goto out;
915 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400916 for (i = 0; i < pev->nargs && ret >= 0; i++) {
917 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
918 if (ret >= 0 &&
919 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400920 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400921 " kretprobe.\n");
922 ret = -EINVAL;
923 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500924 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400925out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500926 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400927
928 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500929}
930
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400931/* Return true if this perf_probe_event requires debuginfo */
932bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500933{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400934 int i;
935
936 if (pev->point.file || pev->point.line || pev->point.lazy_line)
937 return true;
938
939 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400940 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400941 return true;
942
943 return false;
944}
945
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530946/* Parse probe_events event into struct probe_point */
947static int parse_probe_trace_command(const char *cmd,
948 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400949{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530950 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500951 char pr;
952 char *p;
953 int ret, i, argc;
954 char **argv;
955
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530956 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400957 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400958 if (!argv) {
959 pr_debug("Failed to split arguments.\n");
960 return -ENOMEM;
961 }
962 if (argc < 2) {
963 semantic_error("Too few probe arguments.\n");
964 ret = -ERANGE;
965 goto out;
966 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500967
968 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800969 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400970 &pr, (float *)(void *)&tev->group,
971 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400972 if (ret != 3) {
973 semantic_error("Failed to parse event name: %s\n", argv[0]);
974 ret = -EINVAL;
975 goto out;
976 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400977 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500978
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400979 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500980
981 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400982 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
983 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500984 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400985 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500986
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400987 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530988 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400989 if (tev->args == NULL) {
990 ret = -ENOMEM;
991 goto out;
992 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400993 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500994 p = strchr(argv[i + 2], '=');
995 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400996 *p++ = '\0';
997 else
998 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400999 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001000 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001001 tev->args[i].value = strdup(p);
1002 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1003 ret = -ENOMEM;
1004 goto out;
1005 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001006 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001007 ret = 0;
1008out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001009 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001010 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001011}
1012
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001013/* Compose only probe arg */
1014int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1015{
1016 struct perf_probe_arg_field *field = pa->field;
1017 int ret;
1018 char *tmp = buf;
1019
Masami Hiramatsu48481932010-04-12 13:16:53 -04001020 if (pa->name && pa->var)
1021 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1022 else
1023 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001024 if (ret <= 0)
1025 goto error;
1026 tmp += ret;
1027 len -= ret;
1028
1029 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001030 if (field->name[0] == '[')
1031 ret = e_snprintf(tmp, len, "%s", field->name);
1032 else
1033 ret = e_snprintf(tmp, len, "%s%s",
1034 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001035 if (ret <= 0)
1036 goto error;
1037 tmp += ret;
1038 len -= ret;
1039 field = field->next;
1040 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001041
1042 if (pa->type) {
1043 ret = e_snprintf(tmp, len, ":%s", pa->type);
1044 if (ret <= 0)
1045 goto error;
1046 tmp += ret;
1047 len -= ret;
1048 }
1049
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001050 return tmp - buf;
1051error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001052 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001053 strerror(-ret));
1054 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001055}
1056
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001057/* Compose only probe point (not argument) */
1058static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001059{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001060 char *buf, *tmp;
1061 char offs[32] = "", line[32] = "", file[32] = "";
1062 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001063
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001064 buf = zalloc(MAX_CMDLEN);
1065 if (buf == NULL) {
1066 ret = -ENOMEM;
1067 goto error;
1068 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001069 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001070 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001071 if (ret <= 0)
1072 goto error;
1073 }
1074 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001075 ret = e_snprintf(line, 32, ":%d", pp->line);
1076 if (ret <= 0)
1077 goto error;
1078 }
1079 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001080 tmp = pp->file;
1081 len = strlen(tmp);
1082 if (len > 30) {
1083 tmp = strchr(pp->file + len - 30, '/');
1084 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1085 }
1086 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001087 if (ret <= 0)
1088 goto error;
1089 }
1090
1091 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001092 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1093 offs, pp->retprobe ? "%return" : "", line,
1094 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001095 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001096 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001097 if (ret <= 0)
1098 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001099
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001100 return buf;
1101error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001102 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001103 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001104 if (buf)
1105 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001106 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001107}
1108
1109#if 0
1110char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1111{
1112 char *buf;
1113 int i, len, ret;
1114
1115 buf = synthesize_perf_probe_point(&pev->point);
1116 if (!buf)
1117 return NULL;
1118
1119 len = strlen(buf);
1120 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001121 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001122 pev->args[i].name);
1123 if (ret <= 0) {
1124 free(buf);
1125 return NULL;
1126 }
1127 len += ret;
1128 }
1129
1130 return buf;
1131}
1132#endif
1133
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301134static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001135 char **buf, size_t *buflen,
1136 int depth)
1137{
1138 int ret;
1139 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301140 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001141 buflen, depth + 1);
1142 if (depth < 0)
1143 goto out;
1144 }
1145
1146 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1147 if (ret < 0)
1148 depth = ret;
1149 else {
1150 *buf += ret;
1151 *buflen -= ret;
1152 }
1153out:
1154 return depth;
1155
1156}
1157
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301158static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001159 char *buf, size_t buflen)
1160{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301161 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001162 int ret, depth = 0;
1163 char *tmp = buf;
1164
1165 /* Argument name or separator */
1166 if (arg->name)
1167 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1168 else
1169 ret = e_snprintf(buf, buflen, " ");
1170 if (ret < 0)
1171 return ret;
1172 buf += ret;
1173 buflen -= ret;
1174
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001175 /* Special case: @XXX */
1176 if (arg->value[0] == '@' && arg->ref)
1177 ref = ref->next;
1178
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001179 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001180 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301181 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001182 &buflen, 1);
1183 if (depth < 0)
1184 return depth;
1185 }
1186
1187 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001188 if (arg->value[0] == '@' && arg->ref)
1189 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1190 arg->ref->offset);
1191 else
1192 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001193 if (ret < 0)
1194 return ret;
1195 buf += ret;
1196 buflen -= ret;
1197
1198 /* Closing */
1199 while (depth--) {
1200 ret = e_snprintf(buf, buflen, ")");
1201 if (ret < 0)
1202 return ret;
1203 buf += ret;
1204 buflen -= ret;
1205 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001206 /* Print argument type */
1207 if (arg->type) {
1208 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1209 if (ret <= 0)
1210 return ret;
1211 buf += ret;
1212 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001213
1214 return buf - tmp;
1215}
1216
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301217char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001218{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301219 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001220 char *buf;
1221 int i, len, ret;
1222
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001223 buf = zalloc(MAX_CMDLEN);
1224 if (buf == NULL)
1225 return NULL;
1226
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001227 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1228 tp->retprobe ? 'r' : 'p',
1229 tev->group, tev->event,
1230 tp->symbol, tp->offset);
1231 if (len <= 0)
1232 goto error;
1233
1234 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301235 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001236 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001237 if (ret <= 0)
1238 goto error;
1239 len += ret;
1240 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001241
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001242 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001243error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001244 free(buf);
1245 return NULL;
1246}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001247
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301248static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001249 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001250{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001251 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001252 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001253
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001254 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001255 pev->event = strdup(tev->event);
1256 pev->group = strdup(tev->group);
1257 if (pev->event == NULL || pev->group == NULL)
1258 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001259
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001260 /* Convert trace_point to probe_point */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301261 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001262 if (ret < 0)
1263 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001264
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001265 /* Convert trace_arg to probe_arg */
1266 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001267 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1268 if (pev->args == NULL)
1269 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001270 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001271 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001272 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001273 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301274 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001275 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001276 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001277 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001278 if (pev->args[i].name == NULL && ret >= 0)
1279 ret = -ENOMEM;
1280 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001281
1282 if (ret < 0)
1283 clear_perf_probe_event(pev);
1284
1285 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001286}
1287
1288void clear_perf_probe_event(struct perf_probe_event *pev)
1289{
1290 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001291 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001292 int i;
1293
1294 if (pev->event)
1295 free(pev->event);
1296 if (pev->group)
1297 free(pev->group);
1298 if (pp->file)
1299 free(pp->file);
1300 if (pp->function)
1301 free(pp->function);
1302 if (pp->lazy_line)
1303 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001304 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001305 if (pev->args[i].name)
1306 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001307 if (pev->args[i].var)
1308 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001309 if (pev->args[i].type)
1310 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001311 field = pev->args[i].field;
1312 while (field) {
1313 next = field->next;
1314 if (field->name)
1315 free(field->name);
1316 free(field);
1317 field = next;
1318 }
1319 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001320 if (pev->args)
1321 free(pev->args);
1322 memset(pev, 0, sizeof(*pev));
1323}
1324
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301325static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001326{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301327 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001328 int i;
1329
1330 if (tev->event)
1331 free(tev->event);
1332 if (tev->group)
1333 free(tev->group);
1334 if (tev->point.symbol)
1335 free(tev->point.symbol);
1336 for (i = 0; i < tev->nargs; i++) {
1337 if (tev->args[i].name)
1338 free(tev->args[i].name);
1339 if (tev->args[i].value)
1340 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001341 if (tev->args[i].type)
1342 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001343 ref = tev->args[i].ref;
1344 while (ref) {
1345 next = ref->next;
1346 free(ref);
1347 ref = next;
1348 }
1349 }
1350 if (tev->args)
1351 free(tev->args);
1352 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001353}
1354
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001355static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001356{
1357 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001358 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001359 int ret;
1360
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001361 __debugfs = debugfs_find_mountpoint();
1362 if (__debugfs == NULL) {
1363 pr_warning("Debugfs is not mounted.\n");
1364 return -ENOENT;
1365 }
1366
1367 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001368 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001369 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001370 if (readwrite && !probe_event_dry_run)
1371 ret = open(buf, O_RDWR, O_APPEND);
1372 else
1373 ret = open(buf, O_RDONLY, 0);
1374 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001375
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001376 if (ret < 0) {
1377 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001378 pr_warning("kprobe_events file does not exist - please"
1379 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001380 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001381 pr_warning("Failed to open kprobe_events file: %s\n",
1382 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001383 }
1384 return ret;
1385}
1386
1387/* Get raw string list of current kprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301388static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001389{
1390 int ret, idx;
1391 FILE *fp;
1392 char buf[MAX_CMDLEN];
1393 char *p;
1394 struct strlist *sl;
1395
1396 sl = strlist__new(true, NULL);
1397
1398 fp = fdopen(dup(fd), "r");
1399 while (!feof(fp)) {
1400 p = fgets(buf, MAX_CMDLEN, fp);
1401 if (!p)
1402 break;
1403
1404 idx = strlen(p) - 1;
1405 if (p[idx] == '\n')
1406 p[idx] = '\0';
1407 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001408 if (ret < 0) {
1409 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1410 strlist__delete(sl);
1411 return NULL;
1412 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001413 }
1414 fclose(fp);
1415
1416 return sl;
1417}
1418
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001419/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001420static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001421{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001422 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001423 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001424 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001425
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001426 /* Synthesize only event probe point */
1427 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001428 if (!place)
1429 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001430
1431 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001432 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001433 return ret;
1434
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001435 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001436
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001437 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001438 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001439 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001440 ret = synthesize_perf_probe_arg(&pev->args[i],
1441 buf, 128);
1442 if (ret < 0)
1443 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001444 printf(" %s", buf);
1445 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001446 }
1447 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001448 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001449 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001450}
1451
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001452/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001453int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001454{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001455 int fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301456 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001457 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001458 struct strlist *rawlist;
1459 struct str_node *ent;
1460
Masami Hiramatsu72041332010-01-05 17:47:10 -05001461 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001462 ret = init_vmlinux();
1463 if (ret < 0)
1464 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001465
1466 memset(&tev, 0, sizeof(tev));
1467 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001468
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001469 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001470 if (fd < 0)
1471 return fd;
1472
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301473 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001474 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001475 if (!rawlist)
1476 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001477
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001478 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301479 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001480 if (ret >= 0) {
1481 ret = convert_to_perf_probe_event(&tev, &pev);
1482 if (ret >= 0)
1483 ret = show_perf_probe_event(&pev);
1484 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001485 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301486 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001487 if (ret < 0)
1488 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001489 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001490 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001491
1492 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001493}
1494
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001495/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301496static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001497{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001498 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001499 struct strlist *sl, *rawlist;
1500 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301501 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001502 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001503
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001504 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301505 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001506 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001507 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301508 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001509 if (ret < 0)
1510 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001511 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001512 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1513 tev.event);
1514 if (ret >= 0)
1515 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001516 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001517 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301518 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001519 if (ret < 0)
1520 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001521 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001522 strlist__delete(rawlist);
1523
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001524 if (ret < 0) {
1525 strlist__delete(sl);
1526 return NULL;
1527 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001528 return sl;
1529}
1530
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301531static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001532{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001533 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301534 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001535
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001536 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301537 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001538 return -EINVAL;
1539 }
1540
Masami Hiramatsufa282442009-12-08 17:03:23 -05001541 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001542 if (!probe_event_dry_run) {
1543 ret = write(fd, buf, strlen(buf));
1544 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001545 pr_warning("Failed to write event: %s\n",
1546 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001547 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001548 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001549 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001550}
1551
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001552static int get_new_event_name(char *buf, size_t len, const char *base,
1553 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001554{
1555 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001556
1557 /* Try no suffix */
1558 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001559 if (ret < 0) {
1560 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1561 return ret;
1562 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001563 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001564 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001565
Masami Hiramatsud761b082009-12-15 10:32:25 -05001566 if (!allow_suffix) {
1567 pr_warning("Error: event \"%s\" already exists. "
1568 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001569 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001570 }
1571
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001572 /* Try to add suffix */
1573 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001574 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001575 if (ret < 0) {
1576 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1577 return ret;
1578 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001579 if (!strlist__has_entry(namelist, buf))
1580 break;
1581 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001582 if (i == MAX_EVENT_INDEX) {
1583 pr_warning("Too many events are on the same function.\n");
1584 ret = -ERANGE;
1585 }
1586
1587 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001588}
1589
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301590static int __add_probe_trace_events(struct perf_probe_event *pev,
1591 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001592 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001593{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001594 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301595 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001596 char buf[64];
1597 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001598 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001599
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001600 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001601 if (fd < 0)
1602 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001603 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301604 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001605 if (!namelist) {
1606 pr_debug("Failed to get current event list.\n");
1607 return -EIO;
1608 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001609
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001610 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001611 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001612 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001613 tev = &tevs[i];
1614 if (pev->event)
1615 event = pev->event;
1616 else
1617 if (pev->point.function)
1618 event = pev->point.function;
1619 else
1620 event = tev->point.symbol;
1621 if (pev->group)
1622 group = pev->group;
1623 else
1624 group = PERFPROBE_GROUP;
1625
1626 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001627 ret = get_new_event_name(buf, 64, event,
1628 namelist, allow_suffix);
1629 if (ret < 0)
1630 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001631 event = buf;
1632
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001633 tev->event = strdup(event);
1634 tev->group = strdup(group);
1635 if (tev->event == NULL || tev->group == NULL) {
1636 ret = -ENOMEM;
1637 break;
1638 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301639 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001640 if (ret < 0)
1641 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001642 /* Add added event name to namelist */
1643 strlist__add(namelist, event);
1644
1645 /* Trick here - save current event/group */
1646 event = pev->event;
1647 group = pev->group;
1648 pev->event = tev->event;
1649 pev->group = tev->group;
1650 show_perf_probe_event(pev);
1651 /* Trick here - restore current event/group */
1652 pev->event = (char *)event;
1653 pev->group = (char *)group;
1654
1655 /*
1656 * Probes after the first probe which comes from same
1657 * user input are always allowed to add suffix, because
1658 * there might be several addresses corresponding to
1659 * one code line.
1660 */
1661 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001662 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001663
1664 if (ret >= 0) {
1665 /* Show how to use the event. */
1666 printf("\nYou can now use it on all perf tools, such as:\n\n");
1667 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1668 tev->event);
1669 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001670
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001671 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001672 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001673 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001674}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001675
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301676static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1677 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001678 int max_tevs, const char *module)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001679{
1680 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001681 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301682 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001683
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001684 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001685 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001686 if (ret != 0)
1687 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001688
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001689 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301690 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001691 if (tev == NULL)
1692 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001693
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001694 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001695 tev->point.symbol = strdup(pev->point.function);
1696 if (tev->point.symbol == NULL) {
1697 ret = -ENOMEM;
1698 goto error;
1699 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001700 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001701 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001702 tev->nargs = pev->nargs;
1703 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301704 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001705 * tev->nargs);
1706 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001707 ret = -ENOMEM;
1708 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001709 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001710 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001711 if (pev->args[i].name) {
1712 tev->args[i].name = strdup(pev->args[i].name);
1713 if (tev->args[i].name == NULL) {
1714 ret = -ENOMEM;
1715 goto error;
1716 }
1717 }
1718 tev->args[i].value = strdup(pev->args[i].var);
1719 if (tev->args[i].value == NULL) {
1720 ret = -ENOMEM;
1721 goto error;
1722 }
1723 if (pev->args[i].type) {
1724 tev->args[i].type = strdup(pev->args[i].type);
1725 if (tev->args[i].type == NULL) {
1726 ret = -ENOMEM;
1727 goto error;
1728 }
1729 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001730 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001731 }
1732
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001733 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001734 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001735 if (!sym) {
1736 pr_warning("Kernel symbol \'%s\' not found.\n",
1737 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001738 ret = -ENOENT;
1739 goto error;
1740 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001741
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001742 return 1;
1743error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301744 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001745 free(tev);
1746 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001747 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001748}
1749
1750struct __event_package {
1751 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301752 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001753 int ntevs;
1754};
1755
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001756int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001757 int max_tevs, const char *module, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001758{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001759 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001760 struct __event_package *pkgs;
1761
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001762 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1763 if (pkgs == NULL)
1764 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001765
1766 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001767 ret = init_vmlinux();
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001768 if (ret < 0) {
1769 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001770 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001771 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001772
1773 /* Loop 1: convert all events */
1774 for (i = 0; i < npevs; i++) {
1775 pkgs[i].pev = &pevs[i];
1776 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301777 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001778 &pkgs[i].tevs,
1779 max_tevs,
1780 module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001781 if (ret < 0)
1782 goto end;
1783 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001784 }
1785
1786 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001787 for (i = 0; i < npevs && ret >= 0; i++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301788 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001789 pkgs[i].ntevs, force_add);
1790end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001791 /* Loop 3: cleanup and free trace events */
1792 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001793 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301794 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001795 free(pkgs[i].tevs);
1796 }
1797 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001798
1799 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001800}
1801
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301802static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001803{
1804 char *p;
1805 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001806 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001807
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301808 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001809 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1810 if (ret < 0)
1811 goto error;
1812
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001813 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001814 if (!p) {
1815 pr_debug("Internal error: %s should have ':' but not.\n",
1816 ent->s);
1817 ret = -ENOTSUP;
1818 goto error;
1819 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001820 *p = '/';
1821
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001822 pr_debug("Writing event: %s\n", buf);
1823 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001824 if (ret < 0)
1825 goto error;
1826
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001827 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001828 return 0;
1829error:
1830 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1831 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001832}
1833
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301834static int del_trace_probe_event(int fd, const char *group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001835 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001836{
1837 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001838 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001839 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001840
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001841 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1842 if (ret < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001843 pr_err("Failed to copy event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001844 return ret;
1845 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001846
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001847 if (strpbrk(buf, "*?")) { /* Glob-exp */
1848 strlist__for_each_safe(ent, n, namelist)
1849 if (strglobmatch(ent->s, buf)) {
1850 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301851 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001852 if (ret < 0)
1853 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001854 strlist__remove(namelist, ent);
1855 }
1856 } else {
1857 ent = strlist__find(namelist, buf);
1858 if (ent) {
1859 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301860 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001861 if (ret >= 0)
1862 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001863 }
1864 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001865 if (found == 0 && ret >= 0)
1866 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1867
1868 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001869}
1870
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001871int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001872{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001873 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001874 const char *group, *event;
1875 char *p, *str;
1876 struct str_node *ent;
1877 struct strlist *namelist;
1878
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001879 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001880 if (fd < 0)
1881 return fd;
1882
Masami Hiramatsufa282442009-12-08 17:03:23 -05001883 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301884 namelist = get_probe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001885 if (namelist == NULL)
1886 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001887
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001888 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001889 str = strdup(ent->s);
1890 if (str == NULL) {
1891 ret = -ENOMEM;
1892 break;
1893 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001894 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001895 p = strchr(str, ':');
1896 if (p) {
1897 group = str;
1898 *p = '\0';
1899 event = p + 1;
1900 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001901 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001902 event = str;
1903 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001904 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301905 ret = del_trace_probe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001906 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001907 if (ret < 0)
1908 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001909 }
1910 strlist__delete(namelist);
1911 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001912
1913 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001914}
1915