blob: 327604c1253f34460cfc9630a5f30b6a444e16e5 [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 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400376 while (l < lr->start && ret >= 0)
377 ret = show_one_line(fp, l++, true, false);
378 if (ret < 0)
379 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300380
381 list_for_each_entry(ln, &lr->line_list, list) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400382 while (ln->line > l && ret >= 0)
383 ret = show_one_line(fp, (l++) - lr->offset,
384 false, false);
385 if (ret >= 0)
386 ret = show_one_line(fp, (l++) - lr->offset,
387 false, true);
388 if (ret < 0)
389 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300390 }
391
392 if (lr->end == INT_MAX)
393 lr->end = l + NR_ADDITIONAL_LINES;
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400394 while (l <= lr->end && !feof(fp) && ret >= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400395 ret = show_one_line(fp, (l++) - lr->offset, false, false);
396end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300397 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400398 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300399}
400
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900401static int show_available_vars_at(int fd, struct perf_probe_event *pev,
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900402 int max_vls, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900403{
404 char *buf;
405 int ret, i;
406 struct str_node *node;
407 struct variable_list *vls = NULL, *vl;
408
409 buf = synthesize_perf_probe_point(&pev->point);
410 if (!buf)
411 return -EINVAL;
412 pr_debug("Searching variables at %s\n", buf);
413
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900414 ret = find_available_vars_at(fd, pev, &vls, max_vls, externs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900415 if (ret > 0) {
416 /* Some variables were found */
417 fprintf(stdout, "Available variables at %s\n", buf);
418 for (i = 0; i < ret; i++) {
419 vl = &vls[i];
420 /*
421 * A probe point might be converted to
422 * several trace points.
423 */
424 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
425 vl->point.offset);
426 free(vl->point.symbol);
427 if (vl->vars) {
428 strlist__for_each(node, vl->vars)
429 fprintf(stdout, "\t\t%s\n", node->s);
430 strlist__delete(vl->vars);
431 } else
432 fprintf(stdout, "(No variables)\n");
433 }
434 free(vls);
435 } else
436 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
437
438 free(buf);
439 return ret;
440}
441
442/* Show available variables on given probe point */
443int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900444 int max_vls, const char *module, bool externs)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900445{
446 int i, fd, ret = 0;
447
448 ret = init_vmlinux();
449 if (ret < 0)
450 return ret;
451
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900452 fd = open_vmlinux(module);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900453 if (fd < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900454 pr_warning("Failed to open debug information file.\n");
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900455 return fd;
456 }
457
458 setup_pager();
459
460 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsufb8c5a52010-10-21 19:13:35 +0900461 ret = show_available_vars_at(fd, &pevs[i], max_vls, externs);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900462
463 close(fd);
464 return ret;
465}
466
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300467#else /* !DWARF_SUPPORT */
468
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530469static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900470 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300471{
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900472 struct symbol *sym;
473
474 sym = __find_kernel_function_by_name(tp->symbol, NULL);
475 if (!sym) {
476 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
477 return -ENOENT;
478 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400479 pp->function = strdup(tp->symbol);
480 if (pp->function == NULL)
481 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300482 pp->offset = tp->offset;
483 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400484
485 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300486}
487
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530488static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
489 struct probe_trace_event **tevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900490 int max_tevs __unused, const char *mod __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300491{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400492 if (perf_probe_event_need_dwarf(pev)) {
493 pr_warning("Debuginfo-analysis is not supported.\n");
494 return -ENOSYS;
495 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300496 return 0;
497}
498
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900499int show_line_range(struct line_range *lr __unused, const char *module __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300500{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400501 pr_warning("Debuginfo-analysis is not supported.\n");
502 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300503}
504
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900505int show_available_vars(struct perf_probe_event *pevs __unused,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900506 int npevs __unused, int max_vls __unused,
507 const char *module __unused, bool externs __unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900508{
509 pr_warning("Debuginfo-analysis is not supported.\n");
510 return -ENOSYS;
511}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400512#endif
513
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400514int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500515{
516 const char *ptr;
517 char *tmp;
518 /*
519 * <Syntax>
520 * SRC:SLN[+NUM|-ELN]
521 * FUNC[:SLN[+NUM|-ELN]]
522 */
523 ptr = strchr(arg, ':');
524 if (ptr) {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400525 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400526 if (*tmp == '+') {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400527 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400528 lr->end--; /*
529 * Adjust the number of lines here.
530 * If the number of lines == 1, the
531 * the end of line should be equal to
532 * the start of line.
533 */
534 } else if (*tmp == '-')
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400535 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500536 else
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400537 lr->end = INT_MAX;
538 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
539 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500540 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400541 " than end line.\n");
542 return -EINVAL;
543 }
544 if (*tmp != '\0') {
545 semantic_error("Tailing with invalid character '%d'.\n",
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500546 *tmp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400547 return -EINVAL;
548 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400549 tmp = strndup(arg, (ptr - arg));
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400550 } else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400551 tmp = strdup(arg);
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400552 lr->end = INT_MAX;
553 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400554
555 if (tmp == NULL)
556 return -ENOMEM;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500557
558 if (strchr(tmp, '.'))
559 lr->file = tmp;
560 else
561 lr->function = tmp;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400562
563 return 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500564}
565
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500566/* Check the name is good for event/group */
567static bool check_event_name(const char *name)
568{
569 if (!isalpha(*name) && *name != '_')
570 return false;
571 while (*++name != '\0') {
572 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
573 return false;
574 }
575 return true;
576}
577
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500578/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400579static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500580{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400581 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500582 char *ptr, *tmp;
583 char c, nc = 0;
584 /*
585 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500586 * perf probe [EVENT=]SRC[:LN|;PTN]
587 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500588 *
589 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500590 */
591
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500592 ptr = strpbrk(arg, ";=@+%");
593 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500594 *ptr = '\0';
595 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400596 if (strchr(arg, ':')) {
597 semantic_error("Group name is not supported yet.\n");
598 return -ENOTSUP;
599 }
600 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500601 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400602 "follow C symbol-naming rule.\n", arg);
603 return -EINVAL;
604 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400605 pev->event = strdup(arg);
606 if (pev->event == NULL)
607 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400608 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500609 arg = tmp;
610 }
611
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500612 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500613 if (ptr) {
614 nc = *ptr;
615 *ptr++ = '\0';
616 }
617
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400618 tmp = strdup(arg);
619 if (tmp == NULL)
620 return -ENOMEM;
621
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500622 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400623 if (strchr(tmp, '.')) /* File */
624 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500625 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400626 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500627
628 /* Parse other options */
629 while (ptr) {
630 arg = ptr;
631 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500632 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400633 pp->lazy_line = strdup(arg);
634 if (pp->lazy_line == NULL)
635 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500636 break;
637 }
638 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500639 if (ptr) {
640 nc = *ptr;
641 *ptr++ = '\0';
642 }
643 switch (c) {
644 case ':': /* Line number */
645 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400646 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500647 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400648 " in line number.\n");
649 return -EINVAL;
650 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500651 break;
652 case '+': /* Byte offset from a symbol */
653 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400654 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500655 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400656 " in offset.\n");
657 return -EINVAL;
658 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500659 break;
660 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400661 if (pp->file) {
662 semantic_error("SRC@SRC is not allowed.\n");
663 return -EINVAL;
664 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400665 pp->file = strdup(arg);
666 if (pp->file == NULL)
667 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500668 break;
669 case '%': /* Probe places */
670 if (strcmp(arg, "return") == 0) {
671 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400672 } else { /* Others not supported yet */
673 semantic_error("%%%s is not supported.\n", arg);
674 return -ENOTSUP;
675 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500676 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400677 default: /* Buggy case */
678 pr_err("This program has a bug at %s:%d.\n",
679 __FILE__, __LINE__);
680 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500681 break;
682 }
683 }
684
685 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400686 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900687 semantic_error("Lazy pattern can't be used with"
688 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400689 return -EINVAL;
690 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500691
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400692 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900693 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400694 return -EINVAL;
695 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500696
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400697 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900698 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400699 return -EINVAL;
700 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500701
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400702 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500703 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900704 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400705 return -EINVAL;
706 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500707
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400708 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900709 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400710 return -EINVAL;
711 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500712
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400713 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900714 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400715 return -EINVAL;
716 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500717
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400718 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500719 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900720 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400721 return -EINVAL;
722 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500723
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400724 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500725 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
726 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400727 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500728}
729
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400730/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400731static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400732{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400733 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400734 struct perf_probe_arg_field **fieldp;
735
736 pr_debug("parsing arg: %s into ", str);
737
Masami Hiramatsu48481932010-04-12 13:16:53 -0400738 tmp = strchr(str, '=');
739 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400740 arg->name = strndup(str, tmp - str);
741 if (arg->name == NULL)
742 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400743 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400744 str = tmp + 1;
745 }
746
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400747 tmp = strchr(str, ':');
748 if (tmp) { /* Type setting */
749 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400750 arg->type = strdup(tmp + 1);
751 if (arg->type == NULL)
752 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400753 pr_debug("type:%s ", arg->type);
754 }
755
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400756 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400757 if (!is_c_varname(str) || !tmp) {
758 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400759 arg->var = strdup(str);
760 if (arg->var == NULL)
761 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400762 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400763 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400764 }
765
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400766 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400767 arg->var = strndup(str, tmp - str);
768 if (arg->var == NULL)
769 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400770 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400771 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400772 fieldp = &arg->field;
773
774 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400775 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
776 if (*fieldp == NULL)
777 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400778 if (*tmp == '[') { /* Array */
779 str = tmp;
780 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400781 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400782 if (*tmp != ']' || tmp == str + 1) {
783 semantic_error("Array index must be a"
784 " number.\n");
785 return -EINVAL;
786 }
787 tmp++;
788 if (*tmp == '\0')
789 tmp = NULL;
790 } else { /* Structure */
791 if (*tmp == '.') {
792 str = tmp + 1;
793 (*fieldp)->ref = false;
794 } else if (tmp[1] == '>') {
795 str = tmp + 2;
796 (*fieldp)->ref = true;
797 } else {
798 semantic_error("Argument parse error: %s\n",
799 str);
800 return -EINVAL;
801 }
802 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400803 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400804 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400805 (*fieldp)->name = strndup(str, tmp - str);
806 if ((*fieldp)->name == NULL)
807 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400808 if (*str != '[')
809 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400810 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
811 fieldp = &(*fieldp)->next;
812 }
813 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400814 (*fieldp)->name = strdup(str);
815 if ((*fieldp)->name == NULL)
816 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400817 if (*str != '[')
818 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400819 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400820
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400821 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400822 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400823 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400824 if (arg->name == NULL)
825 return -ENOMEM;
826 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400827 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400828}
829
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400830/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400831int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500832{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500833 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400834 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500835
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400836 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400837 if (!argv) {
838 pr_debug("Failed to split arguments.\n");
839 return -ENOMEM;
840 }
841 if (argc - 1 > MAX_PROBE_ARGS) {
842 semantic_error("Too many probe arguments (%d).\n", argc - 1);
843 ret = -ERANGE;
844 goto out;
845 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500846 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400847 ret = parse_perf_probe_point(argv[0], pev);
848 if (ret < 0)
849 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500850
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500851 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400852 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400853 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
854 if (pev->args == NULL) {
855 ret = -ENOMEM;
856 goto out;
857 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400858 for (i = 0; i < pev->nargs && ret >= 0; i++) {
859 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
860 if (ret >= 0 &&
861 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400862 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400863 " kretprobe.\n");
864 ret = -EINVAL;
865 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500866 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400867out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500868 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400869
870 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500871}
872
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400873/* Return true if this perf_probe_event requires debuginfo */
874bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500875{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400876 int i;
877
878 if (pev->point.file || pev->point.line || pev->point.lazy_line)
879 return true;
880
881 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400882 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400883 return true;
884
885 return false;
886}
887
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530888/* Parse probe_events event into struct probe_point */
889static int parse_probe_trace_command(const char *cmd,
890 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400891{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530892 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500893 char pr;
894 char *p;
895 int ret, i, argc;
896 char **argv;
897
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530898 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400899 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400900 if (!argv) {
901 pr_debug("Failed to split arguments.\n");
902 return -ENOMEM;
903 }
904 if (argc < 2) {
905 semantic_error("Too few probe arguments.\n");
906 ret = -ERANGE;
907 goto out;
908 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500909
910 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800911 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400912 &pr, (float *)(void *)&tev->group,
913 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400914 if (ret != 3) {
915 semantic_error("Failed to parse event name: %s\n", argv[0]);
916 ret = -EINVAL;
917 goto out;
918 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400919 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500920
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400921 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500922
923 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400924 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
925 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500926 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400927 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500928
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400929 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530930 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400931 if (tev->args == NULL) {
932 ret = -ENOMEM;
933 goto out;
934 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400935 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500936 p = strchr(argv[i + 2], '=');
937 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400938 *p++ = '\0';
939 else
940 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400941 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400942 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400943 tev->args[i].value = strdup(p);
944 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
945 ret = -ENOMEM;
946 goto out;
947 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500948 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400949 ret = 0;
950out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500951 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400952 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500953}
954
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400955/* Compose only probe arg */
956int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
957{
958 struct perf_probe_arg_field *field = pa->field;
959 int ret;
960 char *tmp = buf;
961
Masami Hiramatsu48481932010-04-12 13:16:53 -0400962 if (pa->name && pa->var)
963 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
964 else
965 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400966 if (ret <= 0)
967 goto error;
968 tmp += ret;
969 len -= ret;
970
971 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400972 if (field->name[0] == '[')
973 ret = e_snprintf(tmp, len, "%s", field->name);
974 else
975 ret = e_snprintf(tmp, len, "%s%s",
976 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400977 if (ret <= 0)
978 goto error;
979 tmp += ret;
980 len -= ret;
981 field = field->next;
982 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400983
984 if (pa->type) {
985 ret = e_snprintf(tmp, len, ":%s", pa->type);
986 if (ret <= 0)
987 goto error;
988 tmp += ret;
989 len -= ret;
990 }
991
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400992 return tmp - buf;
993error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900994 pr_debug("Failed to synthesize perf probe argument: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400995 strerror(-ret));
996 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400997}
998
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400999/* Compose only probe point (not argument) */
1000static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001001{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001002 char *buf, *tmp;
1003 char offs[32] = "", line[32] = "", file[32] = "";
1004 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001005
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001006 buf = zalloc(MAX_CMDLEN);
1007 if (buf == NULL) {
1008 ret = -ENOMEM;
1009 goto error;
1010 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001011 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001012 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001013 if (ret <= 0)
1014 goto error;
1015 }
1016 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001017 ret = e_snprintf(line, 32, ":%d", pp->line);
1018 if (ret <= 0)
1019 goto error;
1020 }
1021 if (pp->file) {
Masami Hiramatsudd259c52010-04-14 18:39:35 -04001022 len = strlen(pp->file) - 31;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001023 if (len < 0)
1024 len = 0;
1025 tmp = strchr(pp->file + len, '/');
1026 if (!tmp)
Masami Hiramatsudd259c52010-04-14 18:39:35 -04001027 tmp = pp->file + len;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001028 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001029 if (ret <= 0)
1030 goto error;
1031 }
1032
1033 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001034 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1035 offs, pp->retprobe ? "%return" : "", line,
1036 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001037 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001038 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001039 if (ret <= 0)
1040 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001041
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001042 return buf;
1043error:
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001044 pr_debug("Failed to synthesize perf probe point: %s\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001045 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001046 if (buf)
1047 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001048 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001049}
1050
1051#if 0
1052char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1053{
1054 char *buf;
1055 int i, len, ret;
1056
1057 buf = synthesize_perf_probe_point(&pev->point);
1058 if (!buf)
1059 return NULL;
1060
1061 len = strlen(buf);
1062 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001063 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001064 pev->args[i].name);
1065 if (ret <= 0) {
1066 free(buf);
1067 return NULL;
1068 }
1069 len += ret;
1070 }
1071
1072 return buf;
1073}
1074#endif
1075
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301076static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001077 char **buf, size_t *buflen,
1078 int depth)
1079{
1080 int ret;
1081 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301082 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001083 buflen, depth + 1);
1084 if (depth < 0)
1085 goto out;
1086 }
1087
1088 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1089 if (ret < 0)
1090 depth = ret;
1091 else {
1092 *buf += ret;
1093 *buflen -= ret;
1094 }
1095out:
1096 return depth;
1097
1098}
1099
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301100static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001101 char *buf, size_t buflen)
1102{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301103 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001104 int ret, depth = 0;
1105 char *tmp = buf;
1106
1107 /* Argument name or separator */
1108 if (arg->name)
1109 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1110 else
1111 ret = e_snprintf(buf, buflen, " ");
1112 if (ret < 0)
1113 return ret;
1114 buf += ret;
1115 buflen -= ret;
1116
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001117 /* Special case: @XXX */
1118 if (arg->value[0] == '@' && arg->ref)
1119 ref = ref->next;
1120
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001121 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001122 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301123 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001124 &buflen, 1);
1125 if (depth < 0)
1126 return depth;
1127 }
1128
1129 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001130 if (arg->value[0] == '@' && arg->ref)
1131 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1132 arg->ref->offset);
1133 else
1134 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001135 if (ret < 0)
1136 return ret;
1137 buf += ret;
1138 buflen -= ret;
1139
1140 /* Closing */
1141 while (depth--) {
1142 ret = e_snprintf(buf, buflen, ")");
1143 if (ret < 0)
1144 return ret;
1145 buf += ret;
1146 buflen -= ret;
1147 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001148 /* Print argument type */
1149 if (arg->type) {
1150 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1151 if (ret <= 0)
1152 return ret;
1153 buf += ret;
1154 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001155
1156 return buf - tmp;
1157}
1158
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301159char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001160{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301161 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001162 char *buf;
1163 int i, len, ret;
1164
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001165 buf = zalloc(MAX_CMDLEN);
1166 if (buf == NULL)
1167 return NULL;
1168
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001169 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1170 tp->retprobe ? 'r' : 'p',
1171 tev->group, tev->event,
1172 tp->symbol, tp->offset);
1173 if (len <= 0)
1174 goto error;
1175
1176 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301177 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001178 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001179 if (ret <= 0)
1180 goto error;
1181 len += ret;
1182 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001183
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001184 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001185error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001186 free(buf);
1187 return NULL;
1188}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001189
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301190static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001191 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001192{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001193 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001194 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001195
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001196 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001197 pev->event = strdup(tev->event);
1198 pev->group = strdup(tev->group);
1199 if (pev->event == NULL || pev->group == NULL)
1200 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001201
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001202 /* Convert trace_point to probe_point */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301203 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001204 if (ret < 0)
1205 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001206
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001207 /* Convert trace_arg to probe_arg */
1208 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001209 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1210 if (pev->args == NULL)
1211 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001212 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001213 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001214 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001215 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301216 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001217 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001218 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001219 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001220 if (pev->args[i].name == NULL && ret >= 0)
1221 ret = -ENOMEM;
1222 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001223
1224 if (ret < 0)
1225 clear_perf_probe_event(pev);
1226
1227 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001228}
1229
1230void clear_perf_probe_event(struct perf_probe_event *pev)
1231{
1232 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001233 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001234 int i;
1235
1236 if (pev->event)
1237 free(pev->event);
1238 if (pev->group)
1239 free(pev->group);
1240 if (pp->file)
1241 free(pp->file);
1242 if (pp->function)
1243 free(pp->function);
1244 if (pp->lazy_line)
1245 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001246 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001247 if (pev->args[i].name)
1248 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001249 if (pev->args[i].var)
1250 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001251 if (pev->args[i].type)
1252 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001253 field = pev->args[i].field;
1254 while (field) {
1255 next = field->next;
1256 if (field->name)
1257 free(field->name);
1258 free(field);
1259 field = next;
1260 }
1261 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001262 if (pev->args)
1263 free(pev->args);
1264 memset(pev, 0, sizeof(*pev));
1265}
1266
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301267static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001268{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301269 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001270 int i;
1271
1272 if (tev->event)
1273 free(tev->event);
1274 if (tev->group)
1275 free(tev->group);
1276 if (tev->point.symbol)
1277 free(tev->point.symbol);
1278 for (i = 0; i < tev->nargs; i++) {
1279 if (tev->args[i].name)
1280 free(tev->args[i].name);
1281 if (tev->args[i].value)
1282 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001283 if (tev->args[i].type)
1284 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001285 ref = tev->args[i].ref;
1286 while (ref) {
1287 next = ref->next;
1288 free(ref);
1289 ref = next;
1290 }
1291 }
1292 if (tev->args)
1293 free(tev->args);
1294 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001295}
1296
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001297static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001298{
1299 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001300 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001301 int ret;
1302
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001303 __debugfs = debugfs_find_mountpoint();
1304 if (__debugfs == NULL) {
1305 pr_warning("Debugfs is not mounted.\n");
1306 return -ENOENT;
1307 }
1308
1309 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001310 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001311 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001312 if (readwrite && !probe_event_dry_run)
1313 ret = open(buf, O_RDWR, O_APPEND);
1314 else
1315 ret = open(buf, O_RDONLY, 0);
1316 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001317
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001318 if (ret < 0) {
1319 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001320 pr_warning("kprobe_events file does not exist - please"
1321 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001322 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001323 pr_warning("Failed to open kprobe_events file: %s\n",
1324 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001325 }
1326 return ret;
1327}
1328
1329/* Get raw string list of current kprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301330static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001331{
1332 int ret, idx;
1333 FILE *fp;
1334 char buf[MAX_CMDLEN];
1335 char *p;
1336 struct strlist *sl;
1337
1338 sl = strlist__new(true, NULL);
1339
1340 fp = fdopen(dup(fd), "r");
1341 while (!feof(fp)) {
1342 p = fgets(buf, MAX_CMDLEN, fp);
1343 if (!p)
1344 break;
1345
1346 idx = strlen(p) - 1;
1347 if (p[idx] == '\n')
1348 p[idx] = '\0';
1349 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001350 if (ret < 0) {
1351 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1352 strlist__delete(sl);
1353 return NULL;
1354 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001355 }
1356 fclose(fp);
1357
1358 return sl;
1359}
1360
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001361/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001362static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001363{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001364 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001365 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001366 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001367
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001368 /* Synthesize only event probe point */
1369 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001370 if (!place)
1371 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001372
1373 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001374 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001375 return ret;
1376
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001377 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001378
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001379 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001380 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001381 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001382 ret = synthesize_perf_probe_arg(&pev->args[i],
1383 buf, 128);
1384 if (ret < 0)
1385 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001386 printf(" %s", buf);
1387 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001388 }
1389 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001390 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001391 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001392}
1393
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001394/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001395int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001396{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001397 int fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301398 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001399 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001400 struct strlist *rawlist;
1401 struct str_node *ent;
1402
Masami Hiramatsu72041332010-01-05 17:47:10 -05001403 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001404 ret = init_vmlinux();
1405 if (ret < 0)
1406 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001407
1408 memset(&tev, 0, sizeof(tev));
1409 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001410
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001411 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001412 if (fd < 0)
1413 return fd;
1414
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301415 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001416 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001417 if (!rawlist)
1418 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001419
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001420 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301421 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001422 if (ret >= 0) {
1423 ret = convert_to_perf_probe_event(&tev, &pev);
1424 if (ret >= 0)
1425 ret = show_perf_probe_event(&pev);
1426 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001427 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301428 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001429 if (ret < 0)
1430 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001431 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001432 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001433
1434 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001435}
1436
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001437/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301438static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001439{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001440 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001441 struct strlist *sl, *rawlist;
1442 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301443 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001444 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001445
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001446 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301447 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001448 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001449 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301450 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001451 if (ret < 0)
1452 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001453 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001454 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1455 tev.event);
1456 if (ret >= 0)
1457 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001458 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001459 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301460 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001461 if (ret < 0)
1462 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001463 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001464 strlist__delete(rawlist);
1465
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001466 if (ret < 0) {
1467 strlist__delete(sl);
1468 return NULL;
1469 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001470 return sl;
1471}
1472
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301473static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001474{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001475 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301476 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001477
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001478 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301479 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001480 return -EINVAL;
1481 }
1482
Masami Hiramatsufa282442009-12-08 17:03:23 -05001483 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001484 if (!probe_event_dry_run) {
1485 ret = write(fd, buf, strlen(buf));
1486 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001487 pr_warning("Failed to write event: %s\n",
1488 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001489 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001490 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001491 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001492}
1493
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001494static int get_new_event_name(char *buf, size_t len, const char *base,
1495 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001496{
1497 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001498
1499 /* Try no suffix */
1500 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001501 if (ret < 0) {
1502 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1503 return ret;
1504 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001505 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001506 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001507
Masami Hiramatsud761b082009-12-15 10:32:25 -05001508 if (!allow_suffix) {
1509 pr_warning("Error: event \"%s\" already exists. "
1510 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001511 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001512 }
1513
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001514 /* Try to add suffix */
1515 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001516 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001517 if (ret < 0) {
1518 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1519 return ret;
1520 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001521 if (!strlist__has_entry(namelist, buf))
1522 break;
1523 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001524 if (i == MAX_EVENT_INDEX) {
1525 pr_warning("Too many events are on the same function.\n");
1526 ret = -ERANGE;
1527 }
1528
1529 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001530}
1531
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301532static int __add_probe_trace_events(struct perf_probe_event *pev,
1533 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001534 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001535{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001536 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301537 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001538 char buf[64];
1539 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001540 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001541
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001542 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001543 if (fd < 0)
1544 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001545 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301546 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001547 if (!namelist) {
1548 pr_debug("Failed to get current event list.\n");
1549 return -EIO;
1550 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001551
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001552 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001553 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001554 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001555 tev = &tevs[i];
1556 if (pev->event)
1557 event = pev->event;
1558 else
1559 if (pev->point.function)
1560 event = pev->point.function;
1561 else
1562 event = tev->point.symbol;
1563 if (pev->group)
1564 group = pev->group;
1565 else
1566 group = PERFPROBE_GROUP;
1567
1568 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001569 ret = get_new_event_name(buf, 64, event,
1570 namelist, allow_suffix);
1571 if (ret < 0)
1572 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001573 event = buf;
1574
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001575 tev->event = strdup(event);
1576 tev->group = strdup(group);
1577 if (tev->event == NULL || tev->group == NULL) {
1578 ret = -ENOMEM;
1579 break;
1580 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301581 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001582 if (ret < 0)
1583 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001584 /* Add added event name to namelist */
1585 strlist__add(namelist, event);
1586
1587 /* Trick here - save current event/group */
1588 event = pev->event;
1589 group = pev->group;
1590 pev->event = tev->event;
1591 pev->group = tev->group;
1592 show_perf_probe_event(pev);
1593 /* Trick here - restore current event/group */
1594 pev->event = (char *)event;
1595 pev->group = (char *)group;
1596
1597 /*
1598 * Probes after the first probe which comes from same
1599 * user input are always allowed to add suffix, because
1600 * there might be several addresses corresponding to
1601 * one code line.
1602 */
1603 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001604 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001605
1606 if (ret >= 0) {
1607 /* Show how to use the event. */
1608 printf("\nYou can now use it on all perf tools, such as:\n\n");
1609 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1610 tev->event);
1611 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001612
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001613 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001614 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001615 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001616}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001617
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301618static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1619 struct probe_trace_event **tevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001620 int max_tevs, const char *module)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001621{
1622 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001623 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301624 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001625
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001626 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001627 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001628 if (ret != 0)
1629 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001630
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001631 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301632 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001633 if (tev == NULL)
1634 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001635
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001636 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001637 tev->point.symbol = strdup(pev->point.function);
1638 if (tev->point.symbol == NULL) {
1639 ret = -ENOMEM;
1640 goto error;
1641 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001642 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001643 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001644 tev->nargs = pev->nargs;
1645 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301646 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001647 * tev->nargs);
1648 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001649 ret = -ENOMEM;
1650 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001651 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001652 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001653 if (pev->args[i].name) {
1654 tev->args[i].name = strdup(pev->args[i].name);
1655 if (tev->args[i].name == NULL) {
1656 ret = -ENOMEM;
1657 goto error;
1658 }
1659 }
1660 tev->args[i].value = strdup(pev->args[i].var);
1661 if (tev->args[i].value == NULL) {
1662 ret = -ENOMEM;
1663 goto error;
1664 }
1665 if (pev->args[i].type) {
1666 tev->args[i].type = strdup(pev->args[i].type);
1667 if (tev->args[i].type == NULL) {
1668 ret = -ENOMEM;
1669 goto error;
1670 }
1671 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001672 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001673 }
1674
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001675 /* Currently just checking function name from symbol map */
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001676 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001677 if (!sym) {
1678 pr_warning("Kernel symbol \'%s\' not found.\n",
1679 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001680 ret = -ENOENT;
1681 goto error;
1682 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001683
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001684 return 1;
1685error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301686 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001687 free(tev);
1688 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001689 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001690}
1691
1692struct __event_package {
1693 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301694 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001695 int ntevs;
1696};
1697
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001698int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001699 int max_tevs, const char *module, bool force_add)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001700{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001701 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001702 struct __event_package *pkgs;
1703
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001704 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1705 if (pkgs == NULL)
1706 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001707
1708 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001709 ret = init_vmlinux();
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001710 if (ret < 0) {
1711 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001712 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001713 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001714
1715 /* Loop 1: convert all events */
1716 for (i = 0; i < npevs; i++) {
1717 pkgs[i].pev = &pevs[i];
1718 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301719 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +09001720 &pkgs[i].tevs,
1721 max_tevs,
1722 module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001723 if (ret < 0)
1724 goto end;
1725 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001726 }
1727
1728 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001729 for (i = 0; i < npevs && ret >= 0; i++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301730 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001731 pkgs[i].ntevs, force_add);
1732end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001733 /* Loop 3: cleanup and free trace events */
1734 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001735 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301736 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001737 free(pkgs[i].tevs);
1738 }
1739 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001740
1741 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001742}
1743
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301744static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001745{
1746 char *p;
1747 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001748 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001749
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301750 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001751 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1752 if (ret < 0)
1753 goto error;
1754
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001755 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001756 if (!p) {
1757 pr_debug("Internal error: %s should have ':' but not.\n",
1758 ent->s);
1759 ret = -ENOTSUP;
1760 goto error;
1761 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001762 *p = '/';
1763
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001764 pr_debug("Writing event: %s\n", buf);
1765 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001766 if (ret < 0)
1767 goto error;
1768
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001769 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001770 return 0;
1771error:
1772 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1773 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001774}
1775
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301776static int del_trace_probe_event(int fd, const char *group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001777 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001778{
1779 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001780 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001781 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001782
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001783 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1784 if (ret < 0) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001785 pr_err("Failed to copy event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001786 return ret;
1787 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001788
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001789 if (strpbrk(buf, "*?")) { /* Glob-exp */
1790 strlist__for_each_safe(ent, n, namelist)
1791 if (strglobmatch(ent->s, buf)) {
1792 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301793 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001794 if (ret < 0)
1795 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001796 strlist__remove(namelist, ent);
1797 }
1798 } else {
1799 ent = strlist__find(namelist, buf);
1800 if (ent) {
1801 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301802 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001803 if (ret >= 0)
1804 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001805 }
1806 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001807 if (found == 0 && ret >= 0)
1808 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1809
1810 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001811}
1812
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001813int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001814{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001815 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001816 const char *group, *event;
1817 char *p, *str;
1818 struct str_node *ent;
1819 struct strlist *namelist;
1820
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001821 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001822 if (fd < 0)
1823 return fd;
1824
Masami Hiramatsufa282442009-12-08 17:03:23 -05001825 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301826 namelist = get_probe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001827 if (namelist == NULL)
1828 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001829
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001830 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001831 str = strdup(ent->s);
1832 if (str == NULL) {
1833 ret = -ENOMEM;
1834 break;
1835 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001836 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001837 p = strchr(str, ':');
1838 if (p) {
1839 group = str;
1840 *p = '\0';
1841 event = p + 1;
1842 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001843 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001844 event = str;
1845 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001846 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301847 ret = del_trace_probe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001848 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001849 if (ret < 0)
1850 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001851 }
1852 strlist__delete(namelist);
1853 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001854
1855 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001856}
1857