blob: 3ba9c5393775a5755bd433344419404a5aee376e [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
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400290static 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
309 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300310error:
311 if (feof(fp))
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400312 pr_warning("Source file is shorter than expected.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300313 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400314 pr_warning("File read error: %s\n", strerror(errno));
315
316 return -1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300317}
318
319/*
320 * Show line-range always requires debuginfo to find source file and
321 * line number.
322 */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900323int show_line_range(struct line_range *lr, const char *module)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300324{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400325 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300326 struct line_node *ln;
327 FILE *fp;
328 int fd, ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900329 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300330
331 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400332 ret = init_vmlinux();
333 if (ret < 0)
334 return ret;
335
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900336 fd = open_vmlinux(module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400337 if (fd < 0) {
338 pr_warning("Failed to open debuginfo file.\n");
339 return fd;
340 }
341
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300342 ret = find_line_range(fd, lr);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300343 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400344 if (ret == 0) {
345 pr_warning("Specified source line is not found.\n");
346 return -ENOENT;
347 } else if (ret < 0) {
348 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
349 return ret;
350 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300351
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900352 /* Convert source file path */
353 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900354 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900355 free(tmp); /* Free old path */
356 if (ret < 0) {
357 pr_warning("Failed to find source file. (%d)\n", ret);
358 return ret;
359 }
360
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300361 setup_pager();
362
363 if (lr->function)
364 fprintf(stdout, "<%s:%d>\n", lr->function,
365 lr->start - lr->offset);
366 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100367 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300368
369 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400370 if (fp == NULL) {
371 pr_warning("Failed to open %s: %s\n", lr->path,
372 strerror(errno));
373 return -errno;
374 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300375 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100376 while (l < lr->start) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400377 ret = show_one_line(fp, l++, true, false);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100378 if (ret < 0)
379 goto end;
380 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300381
382 list_for_each_entry(ln, &lr->line_list, list) {
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100383 for (; ln->line > l; l++) {
384 ret = show_one_line(fp, l - lr->offset, false, false);
385 if (ret < 0)
386 goto end;
387 }
388 ret = show_one_line(fp, l++ - lr->offset, false, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400389 if (ret < 0)
390 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300391 }
392
393 if (lr->end == INT_MAX)
394 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100395 while (l <= lr->end && !feof(fp)) {
396 ret = show_one_line(fp, l++ - lr->offset, false, false);
397 if (ret < 0)
398 break;
399 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400400end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300401 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400402 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300403}
404
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900405static int show_available_vars_at(int fd, struct perf_probe_event *pev,
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900406 int max_vls, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900407{
408 char *buf;
409 int ret, i;
410 struct str_node *node;
411 struct variable_list *vls = NULL, *vl;
412
413 buf = synthesize_perf_probe_point(&pev->point);
414 if (!buf)
415 return -EINVAL;
416 pr_debug("Searching variables at %s\n", buf);
417
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900418 ret = find_available_vars_at(fd, pev, &vls, max_vls, externs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900419 if (ret > 0) {
420 /* Some variables were found */
421 fprintf(stdout, "Available variables at %s\n", buf);
422 for (i = 0; i < ret; i++) {
423 vl = &vls[i];
424 /*
425 * A probe point might be converted to
426 * several trace points.
427 */
428 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
429 vl->point.offset);
430 free(vl->point.symbol);
431 if (vl->vars) {
432 strlist__for_each(node, vl->vars)
433 fprintf(stdout, "\t\t%s\n", node->s);
434 strlist__delete(vl->vars);
435 } else
436 fprintf(stdout, "(No variables)\n");
437 }
438 free(vls);
439 } else
440 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
441
442 free(buf);
443 return ret;
444}
445
446/* Show available variables on given probe point */
447int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900448 int max_vls, const char *module, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900449{
450 int i, fd, ret = 0;
451
452 ret = init_vmlinux();
453 if (ret < 0)
454 return ret;
455
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900456 fd = open_vmlinux(module);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900457 if (fd < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900458 pr_warning("Failed to open debug information file.\n");
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900459 return fd;
460 }
461
462 setup_pager();
463
464 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900465 ret = show_available_vars_at(fd, &pevs[i], max_vls, externs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900466
467 close(fd);
468 return ret;
469}
470
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300471#else /* !DWARF_SUPPORT */
472
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530473static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900474 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300475{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900476 struct symbol *sym;
477
478 sym = __find_kernel_function_by_name(tp->symbol, NULL);
479 if (!sym) {
480 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
481 return -ENOENT;
482 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400483 pp->function = strdup(tp->symbol);
484 if (pp->function == NULL)
485 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300486 pp->offset = tp->offset;
487 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400488
489 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300490}
491
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530492static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
493 struct probe_trace_event **tevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900494 int max_tevs __unused, const char *mod __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300495{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400496 if (perf_probe_event_need_dwarf(pev)) {
497 pr_warning("Debuginfo-analysis is not supported.\n");
498 return -ENOSYS;
499 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300500 return 0;
501}
502
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900503int show_line_range(struct line_range *lr __unused, const char *module __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300504{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400505 pr_warning("Debuginfo-analysis is not supported.\n");
506 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300507}
508
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900509int show_available_vars(struct perf_probe_event *pevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900510 int npevs __unused, int max_vls __unused,
511 const char *module __unused, bool externs __unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900512{
513 pr_warning("Debuginfo-analysis is not supported.\n");
514 return -ENOSYS;
515}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400516#endif
517
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100518/*
519 * Stuff 'lr' according to the line range described by 'arg'.
520 * The line range syntax is described by:
521 *
522 * SRC[:SLN[+NUM|-ELN]]
523 * FNC[:SLN[+NUM|-ELN]]
524 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400525int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500526{
527 const char *ptr;
528 char *tmp;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +0100529
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500530 ptr = strchr(arg, ':');
531 if (ptr) {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400532 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400533 if (*tmp == '+') {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400534 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400535 lr->end--; /*
536 * Adjust the number of lines here.
537 * If the number of lines == 1, the
538 * the end of line should be equal to
539 * the start of line.
540 */
541 } else if (*tmp == '-')
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400542 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500543 else
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400544 lr->end = INT_MAX;
545 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
546 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500547 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400548 " than end line.\n");
549 return -EINVAL;
550 }
551 if (*tmp != '\0') {
552 semantic_error("Tailing with invalid character '%d'.\n",
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500553 *tmp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400554 return -EINVAL;
555 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400556 tmp = strndup(arg, (ptr - arg));
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400557 } else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400558 tmp = strdup(arg);
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400559 lr->end = INT_MAX;
560 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400561
562 if (tmp == NULL)
563 return -ENOMEM;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500564
565 if (strchr(tmp, '.'))
566 lr->file = tmp;
567 else
568 lr->function = tmp;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400569
570 return 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500571}
572
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500573/* Check the name is good for event/group */
574static bool check_event_name(const char *name)
575{
576 if (!isalpha(*name) && *name != '_')
577 return false;
578 while (*++name != '\0') {
579 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
580 return false;
581 }
582 return true;
583}
584
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500585/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400586static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500587{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400588 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500589 char *ptr, *tmp;
590 char c, nc = 0;
591 /*
592 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500593 * perf probe [EVENT=]SRC[:LN|;PTN]
594 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500595 *
596 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500597 */
598
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500599 ptr = strpbrk(arg, ";=@+%");
600 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500601 *ptr = '\0';
602 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400603 if (strchr(arg, ':')) {
604 semantic_error("Group name is not supported yet.\n");
605 return -ENOTSUP;
606 }
607 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500608 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400609 "follow C symbol-naming rule.\n", arg);
610 return -EINVAL;
611 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400612 pev->event = strdup(arg);
613 if (pev->event == NULL)
614 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400615 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500616 arg = tmp;
617 }
618
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500619 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500620 if (ptr) {
621 nc = *ptr;
622 *ptr++ = '\0';
623 }
624
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400625 tmp = strdup(arg);
626 if (tmp == NULL)
627 return -ENOMEM;
628
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500629 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400630 if (strchr(tmp, '.')) /* File */
631 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500632 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400633 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500634
635 /* Parse other options */
636 while (ptr) {
637 arg = ptr;
638 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500639 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400640 pp->lazy_line = strdup(arg);
641 if (pp->lazy_line == NULL)
642 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500643 break;
644 }
645 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500646 if (ptr) {
647 nc = *ptr;
648 *ptr++ = '\0';
649 }
650 switch (c) {
651 case ':': /* Line number */
652 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400653 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500654 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400655 " in line number.\n");
656 return -EINVAL;
657 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500658 break;
659 case '+': /* Byte offset from a symbol */
660 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400661 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500662 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400663 " in offset.\n");
664 return -EINVAL;
665 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500666 break;
667 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400668 if (pp->file) {
669 semantic_error("SRC@SRC is not allowed.\n");
670 return -EINVAL;
671 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400672 pp->file = strdup(arg);
673 if (pp->file == NULL)
674 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500675 break;
676 case '%': /* Probe places */
677 if (strcmp(arg, "return") == 0) {
678 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400679 } else { /* Others not supported yet */
680 semantic_error("%%%s is not supported.\n", arg);
681 return -ENOTSUP;
682 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500683 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400684 default: /* Buggy case */
685 pr_err("This program has a bug at %s:%d.\n",
686 __FILE__, __LINE__);
687 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500688 break;
689 }
690 }
691
692 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400693 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900694 semantic_error("Lazy pattern can't be used with"
695 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400696 return -EINVAL;
697 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500698
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400699 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900700 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400701 return -EINVAL;
702 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500703
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400704 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900705 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400706 return -EINVAL;
707 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500708
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400709 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500710 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900711 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400712 return -EINVAL;
713 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500714
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400715 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900716 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400717 return -EINVAL;
718 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500719
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400720 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900721 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400722 return -EINVAL;
723 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500724
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400725 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500726 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900727 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400728 return -EINVAL;
729 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500730
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400731 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500732 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
733 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400734 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500735}
736
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400737/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400738static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400739{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400740 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400741 struct perf_probe_arg_field **fieldp;
742
743 pr_debug("parsing arg: %s into ", str);
744
Masami Hiramatsu48481932010-04-12 13:16:53 -0400745 tmp = strchr(str, '=');
746 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400747 arg->name = strndup(str, tmp - str);
748 if (arg->name == NULL)
749 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400750 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400751 str = tmp + 1;
752 }
753
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400754 tmp = strchr(str, ':');
755 if (tmp) { /* Type setting */
756 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400757 arg->type = strdup(tmp + 1);
758 if (arg->type == NULL)
759 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400760 pr_debug("type:%s ", arg->type);
761 }
762
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400763 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400764 if (!is_c_varname(str) || !tmp) {
765 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400766 arg->var = strdup(str);
767 if (arg->var == NULL)
768 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400769 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400770 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400771 }
772
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400773 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400774 arg->var = strndup(str, tmp - str);
775 if (arg->var == NULL)
776 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400777 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400778 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400779 fieldp = &arg->field;
780
781 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400782 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
783 if (*fieldp == NULL)
784 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400785 if (*tmp == '[') { /* Array */
786 str = tmp;
787 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400788 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400789 if (*tmp != ']' || tmp == str + 1) {
790 semantic_error("Array index must be a"
791 " number.\n");
792 return -EINVAL;
793 }
794 tmp++;
795 if (*tmp == '\0')
796 tmp = NULL;
797 } else { /* Structure */
798 if (*tmp == '.') {
799 str = tmp + 1;
800 (*fieldp)->ref = false;
801 } else if (tmp[1] == '>') {
802 str = tmp + 2;
803 (*fieldp)->ref = true;
804 } else {
805 semantic_error("Argument parse error: %s\n",
806 str);
807 return -EINVAL;
808 }
809 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400810 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400811 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400812 (*fieldp)->name = strndup(str, tmp - str);
813 if ((*fieldp)->name == NULL)
814 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400815 if (*str != '[')
816 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400817 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
818 fieldp = &(*fieldp)->next;
819 }
820 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400821 (*fieldp)->name = strdup(str);
822 if ((*fieldp)->name == NULL)
823 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400824 if (*str != '[')
825 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400826 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400827
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400828 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400829 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400830 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400831 if (arg->name == NULL)
832 return -ENOMEM;
833 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400834 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400835}
836
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400837/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400838int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500839{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500840 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400841 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500842
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400843 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400844 if (!argv) {
845 pr_debug("Failed to split arguments.\n");
846 return -ENOMEM;
847 }
848 if (argc - 1 > MAX_PROBE_ARGS) {
849 semantic_error("Too many probe arguments (%d).\n", argc - 1);
850 ret = -ERANGE;
851 goto out;
852 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500853 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400854 ret = parse_perf_probe_point(argv[0], pev);
855 if (ret < 0)
856 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500857
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500858 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400859 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400860 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
861 if (pev->args == NULL) {
862 ret = -ENOMEM;
863 goto out;
864 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400865 for (i = 0; i < pev->nargs && ret >= 0; i++) {
866 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
867 if (ret >= 0 &&
868 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400869 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400870 " kretprobe.\n");
871 ret = -EINVAL;
872 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500873 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400874out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500875 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400876
877 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500878}
879
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400880/* Return true if this perf_probe_event requires debuginfo */
881bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500882{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400883 int i;
884
885 if (pev->point.file || pev->point.line || pev->point.lazy_line)
886 return true;
887
888 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400889 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400890 return true;
891
892 return false;
893}
894
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530895/* Parse probe_events event into struct probe_point */
896static int parse_probe_trace_command(const char *cmd,
897 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400898{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530899 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500900 char pr;
901 char *p;
902 int ret, i, argc;
903 char **argv;
904
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530905 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400906 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400907 if (!argv) {
908 pr_debug("Failed to split arguments.\n");
909 return -ENOMEM;
910 }
911 if (argc < 2) {
912 semantic_error("Too few probe arguments.\n");
913 ret = -ERANGE;
914 goto out;
915 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500916
917 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800918 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400919 &pr, (float *)(void *)&tev->group,
920 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400921 if (ret != 3) {
922 semantic_error("Failed to parse event name: %s\n", argv[0]);
923 ret = -EINVAL;
924 goto out;
925 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400926 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500927
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400928 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500929
930 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400931 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
932 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500933 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400934 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500935
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400936 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530937 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400938 if (tev->args == NULL) {
939 ret = -ENOMEM;
940 goto out;
941 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400942 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500943 p = strchr(argv[i + 2], '=');
944 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400945 *p++ = '\0';
946 else
947 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400948 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400949 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400950 tev->args[i].value = strdup(p);
951 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
952 ret = -ENOMEM;
953 goto out;
954 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500955 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400956 ret = 0;
957out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500958 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400959 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500960}
961
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400962/* Compose only probe arg */
963int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
964{
965 struct perf_probe_arg_field *field = pa->field;
966 int ret;
967 char *tmp = buf;
968
Masami Hiramatsu48481932010-04-12 13:16:53 -0400969 if (pa->name && pa->var)
970 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
971 else
972 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400973 if (ret <= 0)
974 goto error;
975 tmp += ret;
976 len -= ret;
977
978 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400979 if (field->name[0] == '[')
980 ret = e_snprintf(tmp, len, "%s", field->name);
981 else
982 ret = e_snprintf(tmp, len, "%s%s",
983 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400984 if (ret <= 0)
985 goto error;
986 tmp += ret;
987 len -= ret;
988 field = field->next;
989 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400990
991 if (pa->type) {
992 ret = e_snprintf(tmp, len, ":%s", pa->type);
993 if (ret <= 0)
994 goto error;
995 tmp += ret;
996 len -= ret;
997 }
998
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400999 return tmp - buf;
1000error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001001 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001002 strerror(-ret));
1003 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001004}
1005
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001006/* Compose only probe point (not argument) */
1007static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001008{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001009 char *buf, *tmp;
1010 char offs[32] = "", line[32] = "", file[32] = "";
1011 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001012
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001013 buf = zalloc(MAX_CMDLEN);
1014 if (buf == NULL) {
1015 ret = -ENOMEM;
1016 goto error;
1017 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001018 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001019 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001020 if (ret <= 0)
1021 goto error;
1022 }
1023 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001024 ret = e_snprintf(line, 32, ":%d", pp->line);
1025 if (ret <= 0)
1026 goto error;
1027 }
1028 if (pp->file) {
Masami Hiramatsudd259c52010-04-14 18:39:35 -04001029 len = strlen(pp->file) - 31;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001030 if (len < 0)
1031 len = 0;
1032 tmp = strchr(pp->file + len, '/');
1033 if (!tmp)
Masami Hiramatsudd259c52010-04-14 18:39:35 -04001034 tmp = pp->file + len;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001035 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001036 if (ret <= 0)
1037 goto error;
1038 }
1039
1040 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001041 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1042 offs, pp->retprobe ? "%return" : "", line,
1043 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001044 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001045 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001046 if (ret <= 0)
1047 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001048
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001049 return buf;
1050error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001051 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001052 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001053 if (buf)
1054 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001055 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001056}
1057
1058#if 0
1059char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1060{
1061 char *buf;
1062 int i, len, ret;
1063
1064 buf = synthesize_perf_probe_point(&pev->point);
1065 if (!buf)
1066 return NULL;
1067
1068 len = strlen(buf);
1069 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001070 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001071 pev->args[i].name);
1072 if (ret <= 0) {
1073 free(buf);
1074 return NULL;
1075 }
1076 len += ret;
1077 }
1078
1079 return buf;
1080}
1081#endif
1082
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301083static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001084 char **buf, size_t *buflen,
1085 int depth)
1086{
1087 int ret;
1088 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301089 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001090 buflen, depth + 1);
1091 if (depth < 0)
1092 goto out;
1093 }
1094
1095 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1096 if (ret < 0)
1097 depth = ret;
1098 else {
1099 *buf += ret;
1100 *buflen -= ret;
1101 }
1102out:
1103 return depth;
1104
1105}
1106
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301107static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001108 char *buf, size_t buflen)
1109{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301110 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001111 int ret, depth = 0;
1112 char *tmp = buf;
1113
1114 /* Argument name or separator */
1115 if (arg->name)
1116 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1117 else
1118 ret = e_snprintf(buf, buflen, " ");
1119 if (ret < 0)
1120 return ret;
1121 buf += ret;
1122 buflen -= ret;
1123
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001124 /* Special case: @XXX */
1125 if (arg->value[0] == '@' && arg->ref)
1126 ref = ref->next;
1127
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001128 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001129 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301130 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001131 &buflen, 1);
1132 if (depth < 0)
1133 return depth;
1134 }
1135
1136 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001137 if (arg->value[0] == '@' && arg->ref)
1138 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1139 arg->ref->offset);
1140 else
1141 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001142 if (ret < 0)
1143 return ret;
1144 buf += ret;
1145 buflen -= ret;
1146
1147 /* Closing */
1148 while (depth--) {
1149 ret = e_snprintf(buf, buflen, ")");
1150 if (ret < 0)
1151 return ret;
1152 buf += ret;
1153 buflen -= ret;
1154 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001155 /* Print argument type */
1156 if (arg->type) {
1157 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1158 if (ret <= 0)
1159 return ret;
1160 buf += ret;
1161 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001162
1163 return buf - tmp;
1164}
1165
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301166char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001167{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301168 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001169 char *buf;
1170 int i, len, ret;
1171
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001172 buf = zalloc(MAX_CMDLEN);
1173 if (buf == NULL)
1174 return NULL;
1175
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001176 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1177 tp->retprobe ? 'r' : 'p',
1178 tev->group, tev->event,
1179 tp->symbol, tp->offset);
1180 if (len <= 0)
1181 goto error;
1182
1183 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301184 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001185 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001186 if (ret <= 0)
1187 goto error;
1188 len += ret;
1189 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001190
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001191 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001192error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001193 free(buf);
1194 return NULL;
1195}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001196
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301197static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001198 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001199{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001200 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001201 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001202
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001203 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001204 pev->event = strdup(tev->event);
1205 pev->group = strdup(tev->group);
1206 if (pev->event == NULL || pev->group == NULL)
1207 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001208
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001209 /* Convert trace_point to probe_point */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301210 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001211 if (ret < 0)
1212 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001213
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001214 /* Convert trace_arg to probe_arg */
1215 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001216 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1217 if (pev->args == NULL)
1218 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001219 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001220 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001221 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001222 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301223 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001224 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001225 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001226 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001227 if (pev->args[i].name == NULL && ret >= 0)
1228 ret = -ENOMEM;
1229 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001230
1231 if (ret < 0)
1232 clear_perf_probe_event(pev);
1233
1234 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001235}
1236
1237void clear_perf_probe_event(struct perf_probe_event *pev)
1238{
1239 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001240 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001241 int i;
1242
1243 if (pev->event)
1244 free(pev->event);
1245 if (pev->group)
1246 free(pev->group);
1247 if (pp->file)
1248 free(pp->file);
1249 if (pp->function)
1250 free(pp->function);
1251 if (pp->lazy_line)
1252 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001253 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001254 if (pev->args[i].name)
1255 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001256 if (pev->args[i].var)
1257 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001258 if (pev->args[i].type)
1259 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001260 field = pev->args[i].field;
1261 while (field) {
1262 next = field->next;
1263 if (field->name)
1264 free(field->name);
1265 free(field);
1266 field = next;
1267 }
1268 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001269 if (pev->args)
1270 free(pev->args);
1271 memset(pev, 0, sizeof(*pev));
1272}
1273
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301274static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001275{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301276 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001277 int i;
1278
1279 if (tev->event)
1280 free(tev->event);
1281 if (tev->group)
1282 free(tev->group);
1283 if (tev->point.symbol)
1284 free(tev->point.symbol);
1285 for (i = 0; i < tev->nargs; i++) {
1286 if (tev->args[i].name)
1287 free(tev->args[i].name);
1288 if (tev->args[i].value)
1289 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001290 if (tev->args[i].type)
1291 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001292 ref = tev->args[i].ref;
1293 while (ref) {
1294 next = ref->next;
1295 free(ref);
1296 ref = next;
1297 }
1298 }
1299 if (tev->args)
1300 free(tev->args);
1301 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001302}
1303
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001304static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001305{
1306 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001307 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001308 int ret;
1309
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001310 __debugfs = debugfs_find_mountpoint();
1311 if (__debugfs == NULL) {
1312 pr_warning("Debugfs is not mounted.\n");
1313 return -ENOENT;
1314 }
1315
1316 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001317 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001318 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001319 if (readwrite && !probe_event_dry_run)
1320 ret = open(buf, O_RDWR, O_APPEND);
1321 else
1322 ret = open(buf, O_RDONLY, 0);
1323 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001324
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001325 if (ret < 0) {
1326 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001327 pr_warning("kprobe_events file does not exist - please"
1328 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001329 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001330 pr_warning("Failed to open kprobe_events file: %s\n",
1331 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001332 }
1333 return ret;
1334}
1335
1336/* Get raw string list of current kprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301337static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001338{
1339 int ret, idx;
1340 FILE *fp;
1341 char buf[MAX_CMDLEN];
1342 char *p;
1343 struct strlist *sl;
1344
1345 sl = strlist__new(true, NULL);
1346
1347 fp = fdopen(dup(fd), "r");
1348 while (!feof(fp)) {
1349 p = fgets(buf, MAX_CMDLEN, fp);
1350 if (!p)
1351 break;
1352
1353 idx = strlen(p) - 1;
1354 if (p[idx] == '\n')
1355 p[idx] = '\0';
1356 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001357 if (ret < 0) {
1358 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1359 strlist__delete(sl);
1360 return NULL;
1361 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001362 }
1363 fclose(fp);
1364
1365 return sl;
1366}
1367
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001368/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001369static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001370{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001371 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001372 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001373 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001374
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001375 /* Synthesize only event probe point */
1376 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001377 if (!place)
1378 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001379
1380 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001381 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001382 return ret;
1383
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001384 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001385
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001386 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001387 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001388 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001389 ret = synthesize_perf_probe_arg(&pev->args[i],
1390 buf, 128);
1391 if (ret < 0)
1392 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001393 printf(" %s", buf);
1394 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001395 }
1396 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001397 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001398 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001399}
1400
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001401/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001402int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001403{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001404 int fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301405 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001406 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001407 struct strlist *rawlist;
1408 struct str_node *ent;
1409
Masami Hiramatsu72041332010-01-05 17:47:10 -05001410 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001411 ret = init_vmlinux();
1412 if (ret < 0)
1413 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001414
1415 memset(&tev, 0, sizeof(tev));
1416 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001417
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001418 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001419 if (fd < 0)
1420 return fd;
1421
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301422 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001423 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001424 if (!rawlist)
1425 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001426
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001427 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301428 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001429 if (ret >= 0) {
1430 ret = convert_to_perf_probe_event(&tev, &pev);
1431 if (ret >= 0)
1432 ret = show_perf_probe_event(&pev);
1433 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001434 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301435 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001436 if (ret < 0)
1437 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001438 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001439 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001440
1441 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001442}
1443
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001444/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301445static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001446{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001447 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001448 struct strlist *sl, *rawlist;
1449 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301450 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001451 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001452
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001453 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301454 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001455 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001456 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301457 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001458 if (ret < 0)
1459 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001460 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001461 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1462 tev.event);
1463 if (ret >= 0)
1464 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001465 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001466 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301467 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001468 if (ret < 0)
1469 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001470 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001471 strlist__delete(rawlist);
1472
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001473 if (ret < 0) {
1474 strlist__delete(sl);
1475 return NULL;
1476 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001477 return sl;
1478}
1479
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301480static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001481{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001482 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301483 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001484
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001485 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301486 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001487 return -EINVAL;
1488 }
1489
Masami Hiramatsufa282442009-12-08 17:03:23 -05001490 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001491 if (!probe_event_dry_run) {
1492 ret = write(fd, buf, strlen(buf));
1493 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001494 pr_warning("Failed to write event: %s\n",
1495 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001496 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001497 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001498 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001499}
1500
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001501static int get_new_event_name(char *buf, size_t len, const char *base,
1502 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001503{
1504 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001505
1506 /* Try no suffix */
1507 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001508 if (ret < 0) {
1509 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1510 return ret;
1511 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001512 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001513 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001514
Masami Hiramatsud761b082009-12-15 10:32:25 -05001515 if (!allow_suffix) {
1516 pr_warning("Error: event \"%s\" already exists. "
1517 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001518 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001519 }
1520
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001521 /* Try to add suffix */
1522 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001523 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001524 if (ret < 0) {
1525 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1526 return ret;
1527 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001528 if (!strlist__has_entry(namelist, buf))
1529 break;
1530 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001531 if (i == MAX_EVENT_INDEX) {
1532 pr_warning("Too many events are on the same function.\n");
1533 ret = -ERANGE;
1534 }
1535
1536 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001537}
1538
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301539static int __add_probe_trace_events(struct perf_probe_event *pev,
1540 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001541 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001542{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001543 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301544 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001545 char buf[64];
1546 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001547 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001548
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001549 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001550 if (fd < 0)
1551 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001552 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301553 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001554 if (!namelist) {
1555 pr_debug("Failed to get current event list.\n");
1556 return -EIO;
1557 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001558
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001559 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001560 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001561 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001562 tev = &tevs[i];
1563 if (pev->event)
1564 event = pev->event;
1565 else
1566 if (pev->point.function)
1567 event = pev->point.function;
1568 else
1569 event = tev->point.symbol;
1570 if (pev->group)
1571 group = pev->group;
1572 else
1573 group = PERFPROBE_GROUP;
1574
1575 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001576 ret = get_new_event_name(buf, 64, event,
1577 namelist, allow_suffix);
1578 if (ret < 0)
1579 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001580 event = buf;
1581
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001582 tev->event = strdup(event);
1583 tev->group = strdup(group);
1584 if (tev->event == NULL || tev->group == NULL) {
1585 ret = -ENOMEM;
1586 break;
1587 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301588 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001589 if (ret < 0)
1590 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001591 /* Add added event name to namelist */
1592 strlist__add(namelist, event);
1593
1594 /* Trick here - save current event/group */
1595 event = pev->event;
1596 group = pev->group;
1597 pev->event = tev->event;
1598 pev->group = tev->group;
1599 show_perf_probe_event(pev);
1600 /* Trick here - restore current event/group */
1601 pev->event = (char *)event;
1602 pev->group = (char *)group;
1603
1604 /*
1605 * Probes after the first probe which comes from same
1606 * user input are always allowed to add suffix, because
1607 * there might be several addresses corresponding to
1608 * one code line.
1609 */
1610 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001611 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001612
1613 if (ret >= 0) {
1614 /* Show how to use the event. */
1615 printf("\nYou can now use it on all perf tools, such as:\n\n");
1616 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1617 tev->event);
1618 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001619
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001620 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001621 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001622 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001623}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001624
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301625static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1626 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001627 int max_tevs, const char *module)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001628{
1629 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001630 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301631 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001632
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001633 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001634 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001635 if (ret != 0)
1636 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001637
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001638 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301639 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001640 if (tev == NULL)
1641 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001642
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001643 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001644 tev->point.symbol = strdup(pev->point.function);
1645 if (tev->point.symbol == NULL) {
1646 ret = -ENOMEM;
1647 goto error;
1648 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001649 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001650 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001651 tev->nargs = pev->nargs;
1652 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301653 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001654 * tev->nargs);
1655 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001656 ret = -ENOMEM;
1657 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001658 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001659 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001660 if (pev->args[i].name) {
1661 tev->args[i].name = strdup(pev->args[i].name);
1662 if (tev->args[i].name == NULL) {
1663 ret = -ENOMEM;
1664 goto error;
1665 }
1666 }
1667 tev->args[i].value = strdup(pev->args[i].var);
1668 if (tev->args[i].value == NULL) {
1669 ret = -ENOMEM;
1670 goto error;
1671 }
1672 if (pev->args[i].type) {
1673 tev->args[i].type = strdup(pev->args[i].type);
1674 if (tev->args[i].type == NULL) {
1675 ret = -ENOMEM;
1676 goto error;
1677 }
1678 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001679 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001680 }
1681
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001682 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001683 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001684 if (!sym) {
1685 pr_warning("Kernel symbol \'%s\' not found.\n",
1686 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001687 ret = -ENOENT;
1688 goto error;
1689 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001690
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001691 return 1;
1692error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301693 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001694 free(tev);
1695 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001696 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001697}
1698
1699struct __event_package {
1700 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301701 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001702 int ntevs;
1703};
1704
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001705int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001706 int max_tevs, const char *module, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001707{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001708 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001709 struct __event_package *pkgs;
1710
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001711 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1712 if (pkgs == NULL)
1713 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001714
1715 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001716 ret = init_vmlinux();
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001717 if (ret < 0) {
1718 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001719 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001720 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001721
1722 /* Loop 1: convert all events */
1723 for (i = 0; i < npevs; i++) {
1724 pkgs[i].pev = &pevs[i];
1725 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301726 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001727 &pkgs[i].tevs,
1728 max_tevs,
1729 module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001730 if (ret < 0)
1731 goto end;
1732 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001733 }
1734
1735 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001736 for (i = 0; i < npevs && ret >= 0; i++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301737 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001738 pkgs[i].ntevs, force_add);
1739end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001740 /* Loop 3: cleanup and free trace events */
1741 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001742 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301743 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001744 free(pkgs[i].tevs);
1745 }
1746 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001747
1748 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001749}
1750
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301751static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001752{
1753 char *p;
1754 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001755 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001756
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301757 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001758 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1759 if (ret < 0)
1760 goto error;
1761
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001762 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001763 if (!p) {
1764 pr_debug("Internal error: %s should have ':' but not.\n",
1765 ent->s);
1766 ret = -ENOTSUP;
1767 goto error;
1768 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001769 *p = '/';
1770
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001771 pr_debug("Writing event: %s\n", buf);
1772 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001773 if (ret < 0)
1774 goto error;
1775
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001776 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001777 return 0;
1778error:
1779 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1780 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001781}
1782
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301783static int del_trace_probe_event(int fd, const char *group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001784 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001785{
1786 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001787 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001788 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001789
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001790 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1791 if (ret < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001792 pr_err("Failed to copy event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001793 return ret;
1794 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001795
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001796 if (strpbrk(buf, "*?")) { /* Glob-exp */
1797 strlist__for_each_safe(ent, n, namelist)
1798 if (strglobmatch(ent->s, buf)) {
1799 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301800 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001801 if (ret < 0)
1802 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001803 strlist__remove(namelist, ent);
1804 }
1805 } else {
1806 ent = strlist__find(namelist, buf);
1807 if (ent) {
1808 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301809 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001810 if (ret >= 0)
1811 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001812 }
1813 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001814 if (found == 0 && ret >= 0)
1815 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1816
1817 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001818}
1819
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001820int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001821{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001822 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001823 const char *group, *event;
1824 char *p, *str;
1825 struct str_node *ent;
1826 struct strlist *namelist;
1827
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001828 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001829 if (fd < 0)
1830 return fd;
1831
Masami Hiramatsufa282442009-12-08 17:03:23 -05001832 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301833 namelist = get_probe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001834 if (namelist == NULL)
1835 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001836
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001837 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001838 str = strdup(ent->s);
1839 if (str == NULL) {
1840 ret = -ENOMEM;
1841 break;
1842 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001843 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001844 p = strchr(str, ':');
1845 if (p) {
1846 group = str;
1847 *p = '\0';
1848 event = p + 1;
1849 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001850 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001851 event = str;
1852 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001853 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301854 ret = del_trace_probe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001855 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001856 if (ret < 0)
1857 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001858 }
1859 strlist__delete(namelist);
1860 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001861
1862 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001863}
1864