blob: 288ebe8279d2bb8b4ac124b7b76b2f1cd033b68d [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 Hiramatsu4b4da7f2010-03-22 13:10:26 -030077/* Initialize symbol maps and path of vmlinux */
Masami Hiramatsu146a1432010-04-12 13:17:42 -040078static int init_vmlinux(void)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040079{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080080 struct dso *kernel;
Masami Hiramatsu146a1432010-04-12 13:17:42 -040081 int ret;
82
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040083 symbol_conf.sort_by_name = true;
84 if (symbol_conf.vmlinux_name == NULL)
85 symbol_conf.try_vmlinux_path = true;
86 else
87 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040088 ret = symbol__init();
89 if (ret < 0) {
90 pr_debug("Failed to init symbol map.\n");
91 goto out;
92 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040093
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030094 ret = machine__init(&machine, "/", 0);
95 if (ret < 0)
96 goto out;
97
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080098 kernel = dso__new_kernel(symbol_conf.vmlinux_name);
99 if (kernel == NULL)
100 die("Failed to create kernel dso.");
101
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300102 ret = __machine__create_kernel_maps(&machine, kernel);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400103 if (ret < 0)
104 pr_debug("Failed to create kernel maps.\n");
105
106out:
107 if (ret < 0)
108 pr_warning("Failed to init vmlinux path.\n");
109 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400110}
111
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300112#ifdef DWARF_SUPPORT
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400113static int open_vmlinux(void)
114{
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300115 if (map__load(machine.vmlinux_maps[MAP__FUNCTION], NULL) < 0) {
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400116 pr_debug("Failed to load kernel map.\n");
117 return -EINVAL;
118 }
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300119 pr_debug("Try to open %s\n", machine.vmlinux_maps[MAP__FUNCTION]->dso->long_name);
120 return open(machine.vmlinux_maps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400121}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300122
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530123/*
124 * Convert trace point to probe point with debuginfo
125 * Currently only handles kprobes.
126 */
127static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400128 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300129{
130 struct symbol *sym;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400131 int fd, ret = -ENOENT;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300132
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300133 sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION],
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300134 tp->symbol, NULL);
135 if (sym) {
136 fd = open_vmlinux();
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400137 if (fd >= 0) {
138 ret = find_perf_probe_point(fd,
139 sym->start + tp->offset, pp);
140 close(fd);
141 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300142 }
143 if (ret <= 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400144 pr_debug("Failed to find corresponding probes from "
145 "debuginfo. Use kprobe event information.\n");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400146 pp->function = strdup(tp->symbol);
147 if (pp->function == NULL)
148 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300149 pp->offset = tp->offset;
150 }
151 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400152
153 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300154}
155
156/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530157static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
158 struct probe_trace_event **tevs,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -0400159 int max_tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300160{
161 bool need_dwarf = perf_probe_event_need_dwarf(pev);
162 int fd, ntevs;
163
164 fd = open_vmlinux();
165 if (fd < 0) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400166 if (need_dwarf) {
167 pr_warning("Failed to open debuginfo file.\n");
168 return fd;
169 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300170 pr_debug("Could not open vmlinux. Try to use symbols.\n");
171 return 0;
172 }
173
174 /* Searching trace events corresponding to probe event */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530175 ntevs = find_probe_trace_events(fd, pev, tevs, max_tevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300176 close(fd);
177
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400178 if (ntevs > 0) { /* Succeeded to find trace events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530179 pr_debug("find %d probe_trace_events.\n", ntevs);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300180 return ntevs;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400181 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300182
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400183 if (ntevs == 0) { /* No error but failed to find probe point. */
184 pr_warning("Probe point '%s' not found.\n",
185 synthesize_perf_probe_point(&pev->point));
186 return -ENOENT;
187 }
188 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400189 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
190 if (ntevs == -EBADF) {
191 pr_warning("Warning: No dwarf info found in the vmlinux - "
192 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
193 if (!need_dwarf) {
194 pr_debug("Trying to use symbols.\nn");
195 return 0;
196 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300197 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400198 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300199}
200
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900201/*
202 * Find a src file from a DWARF tag path. Prepend optional source path prefix
203 * and chop off leading directories that do not exist. Result is passed back as
204 * a newly allocated path on success.
205 * Return 0 if file was found and readable, -errno otherwise.
206 */
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900207static int get_real_path(const char *raw_path, const char *comp_dir,
208 char **new_path)
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900209{
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900210 const char *prefix = symbol_conf.source_prefix;
211
212 if (!prefix) {
213 if (raw_path[0] != '/' && comp_dir)
214 /* If not an absolute path, try to use comp_dir */
215 prefix = comp_dir;
216 else {
217 if (access(raw_path, R_OK) == 0) {
218 *new_path = strdup(raw_path);
219 return 0;
220 } else
221 return -errno;
222 }
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900223 }
224
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900225 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900226 if (!*new_path)
227 return -ENOMEM;
228
229 for (;;) {
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900230 sprintf(*new_path, "%s/%s", prefix, raw_path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900231
232 if (access(*new_path, R_OK) == 0)
233 return 0;
234
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900235 if (!symbol_conf.source_prefix)
236 /* In case of searching comp_dir, don't retry */
237 return -errno;
238
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900239 switch (errno) {
240 case ENAMETOOLONG:
241 case ENOENT:
242 case EROFS:
243 case EFAULT:
244 raw_path = strchr(++raw_path, '/');
245 if (!raw_path) {
246 free(*new_path);
247 *new_path = NULL;
248 return -ENOENT;
249 }
250 continue;
251
252 default:
253 free(*new_path);
254 *new_path = NULL;
255 return -errno;
256 }
257 }
258}
259
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300260#define LINEBUF_SIZE 256
261#define NR_ADDITIONAL_LINES 2
262
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400263static int show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300264{
265 char buf[LINEBUF_SIZE];
266 const char *color = PERF_COLOR_BLUE;
267
268 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
269 goto error;
270 if (!skip) {
271 if (show_num)
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400272 fprintf(stdout, "%7d %s", l, buf);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300273 else
274 color_fprintf(stdout, color, " %s", buf);
275 }
276
277 while (strlen(buf) == LINEBUF_SIZE - 1 &&
278 buf[LINEBUF_SIZE - 2] != '\n') {
279 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
280 goto error;
281 if (!skip) {
282 if (show_num)
283 fprintf(stdout, "%s", buf);
284 else
285 color_fprintf(stdout, color, "%s", buf);
286 }
287 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400288
289 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300290error:
291 if (feof(fp))
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400292 pr_warning("Source file is shorter than expected.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300293 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400294 pr_warning("File read error: %s\n", strerror(errno));
295
296 return -1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300297}
298
299/*
300 * Show line-range always requires debuginfo to find source file and
301 * line number.
302 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400303int show_line_range(struct line_range *lr)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300304{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400305 int l = 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300306 struct line_node *ln;
307 FILE *fp;
308 int fd, ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900309 char *tmp;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300310
311 /* Search a line range */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400312 ret = init_vmlinux();
313 if (ret < 0)
314 return ret;
315
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300316 fd = open_vmlinux();
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400317 if (fd < 0) {
318 pr_warning("Failed to open debuginfo file.\n");
319 return fd;
320 }
321
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300322 ret = find_line_range(fd, lr);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300323 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400324 if (ret == 0) {
325 pr_warning("Specified source line is not found.\n");
326 return -ENOENT;
327 } else if (ret < 0) {
328 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
329 return ret;
330 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300331
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900332 /* Convert source file path */
333 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900334 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900335 free(tmp); /* Free old path */
336 if (ret < 0) {
337 pr_warning("Failed to find source file. (%d)\n", ret);
338 return ret;
339 }
340
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300341 setup_pager();
342
343 if (lr->function)
344 fprintf(stdout, "<%s:%d>\n", lr->function,
345 lr->start - lr->offset);
346 else
347 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
348
349 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400350 if (fp == NULL) {
351 pr_warning("Failed to open %s: %s\n", lr->path,
352 strerror(errno));
353 return -errno;
354 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300355 /* Skip to starting line number */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400356 while (l < lr->start && ret >= 0)
357 ret = show_one_line(fp, l++, true, false);
358 if (ret < 0)
359 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300360
361 list_for_each_entry(ln, &lr->line_list, list) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400362 while (ln->line > l && ret >= 0)
363 ret = show_one_line(fp, (l++) - lr->offset,
364 false, false);
365 if (ret >= 0)
366 ret = show_one_line(fp, (l++) - lr->offset,
367 false, true);
368 if (ret < 0)
369 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300370 }
371
372 if (lr->end == INT_MAX)
373 lr->end = l + NR_ADDITIONAL_LINES;
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400374 while (l <= lr->end && !feof(fp) && ret >= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400375 ret = show_one_line(fp, (l++) - lr->offset, false, false);
376end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300377 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400378 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300379}
380
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900381static int show_available_vars_at(int fd, struct perf_probe_event *pev,
382 int max_vls)
383{
384 char *buf;
385 int ret, i;
386 struct str_node *node;
387 struct variable_list *vls = NULL, *vl;
388
389 buf = synthesize_perf_probe_point(&pev->point);
390 if (!buf)
391 return -EINVAL;
392 pr_debug("Searching variables at %s\n", buf);
393
394 ret = find_available_vars_at(fd, pev, &vls, max_vls);
395 if (ret > 0) {
396 /* Some variables were found */
397 fprintf(stdout, "Available variables at %s\n", buf);
398 for (i = 0; i < ret; i++) {
399 vl = &vls[i];
400 /*
401 * A probe point might be converted to
402 * several trace points.
403 */
404 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
405 vl->point.offset);
406 free(vl->point.symbol);
407 if (vl->vars) {
408 strlist__for_each(node, vl->vars)
409 fprintf(stdout, "\t\t%s\n", node->s);
410 strlist__delete(vl->vars);
411 } else
412 fprintf(stdout, "(No variables)\n");
413 }
414 free(vls);
415 } else
416 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
417
418 free(buf);
419 return ret;
420}
421
422/* Show available variables on given probe point */
423int show_available_vars(struct perf_probe_event *pevs, int npevs,
424 int max_vls)
425{
426 int i, fd, ret = 0;
427
428 ret = init_vmlinux();
429 if (ret < 0)
430 return ret;
431
432 fd = open_vmlinux();
433 if (fd < 0) {
434 pr_warning("Failed to open debuginfo file.\n");
435 return fd;
436 }
437
438 setup_pager();
439
440 for (i = 0; i < npevs && ret >= 0; i++)
441 ret = show_available_vars_at(fd, &pevs[i], max_vls);
442
443 close(fd);
444 return ret;
445}
446
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300447#else /* !DWARF_SUPPORT */
448
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530449static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
450 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300451{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400452 pp->function = strdup(tp->symbol);
453 if (pp->function == NULL)
454 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300455 pp->offset = tp->offset;
456 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400457
458 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300459}
460
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530461static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
462 struct probe_trace_event **tevs __unused,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -0400463 int max_tevs __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300464{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400465 if (perf_probe_event_need_dwarf(pev)) {
466 pr_warning("Debuginfo-analysis is not supported.\n");
467 return -ENOSYS;
468 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300469 return 0;
470}
471
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400472int show_line_range(struct line_range *lr __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300473{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400474 pr_warning("Debuginfo-analysis is not supported.\n");
475 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300476}
477
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900478int show_available_vars(struct perf_probe_event *pevs __unused,
479 int npevs __unused, int max_probe_points __unused)
480{
481 pr_warning("Debuginfo-analysis is not supported.\n");
482 return -ENOSYS;
483}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400484#endif
485
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400486int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500487{
488 const char *ptr;
489 char *tmp;
490 /*
491 * <Syntax>
492 * SRC:SLN[+NUM|-ELN]
493 * FUNC[:SLN[+NUM|-ELN]]
494 */
495 ptr = strchr(arg, ':');
496 if (ptr) {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400497 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400498 if (*tmp == '+') {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400499 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400500 lr->end--; /*
501 * Adjust the number of lines here.
502 * If the number of lines == 1, the
503 * the end of line should be equal to
504 * the start of line.
505 */
506 } else if (*tmp == '-')
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400507 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500508 else
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400509 lr->end = INT_MAX;
510 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
511 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500512 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400513 " than end line.\n");
514 return -EINVAL;
515 }
516 if (*tmp != '\0') {
517 semantic_error("Tailing with invalid character '%d'.\n",
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500518 *tmp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400519 return -EINVAL;
520 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400521 tmp = strndup(arg, (ptr - arg));
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400522 } else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400523 tmp = strdup(arg);
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400524 lr->end = INT_MAX;
525 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400526
527 if (tmp == NULL)
528 return -ENOMEM;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500529
530 if (strchr(tmp, '.'))
531 lr->file = tmp;
532 else
533 lr->function = tmp;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400534
535 return 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500536}
537
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500538/* Check the name is good for event/group */
539static bool check_event_name(const char *name)
540{
541 if (!isalpha(*name) && *name != '_')
542 return false;
543 while (*++name != '\0') {
544 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
545 return false;
546 }
547 return true;
548}
549
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500550/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400551static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500552{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400553 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500554 char *ptr, *tmp;
555 char c, nc = 0;
556 /*
557 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500558 * perf probe [EVENT=]SRC[:LN|;PTN]
559 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500560 *
561 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500562 */
563
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500564 ptr = strpbrk(arg, ";=@+%");
565 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500566 *ptr = '\0';
567 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400568 if (strchr(arg, ':')) {
569 semantic_error("Group name is not supported yet.\n");
570 return -ENOTSUP;
571 }
572 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500573 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400574 "follow C symbol-naming rule.\n", arg);
575 return -EINVAL;
576 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400577 pev->event = strdup(arg);
578 if (pev->event == NULL)
579 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400580 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500581 arg = tmp;
582 }
583
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500584 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500585 if (ptr) {
586 nc = *ptr;
587 *ptr++ = '\0';
588 }
589
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400590 tmp = strdup(arg);
591 if (tmp == NULL)
592 return -ENOMEM;
593
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500594 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400595 if (strchr(tmp, '.')) /* File */
596 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500597 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400598 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500599
600 /* Parse other options */
601 while (ptr) {
602 arg = ptr;
603 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500604 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400605 pp->lazy_line = strdup(arg);
606 if (pp->lazy_line == NULL)
607 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500608 break;
609 }
610 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500611 if (ptr) {
612 nc = *ptr;
613 *ptr++ = '\0';
614 }
615 switch (c) {
616 case ':': /* Line number */
617 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400618 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500619 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400620 " in line number.\n");
621 return -EINVAL;
622 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500623 break;
624 case '+': /* Byte offset from a symbol */
625 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400626 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500627 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400628 " in offset.\n");
629 return -EINVAL;
630 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500631 break;
632 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400633 if (pp->file) {
634 semantic_error("SRC@SRC is not allowed.\n");
635 return -EINVAL;
636 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400637 pp->file = strdup(arg);
638 if (pp->file == NULL)
639 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500640 break;
641 case '%': /* Probe places */
642 if (strcmp(arg, "return") == 0) {
643 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400644 } else { /* Others not supported yet */
645 semantic_error("%%%s is not supported.\n", arg);
646 return -ENOTSUP;
647 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500648 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400649 default: /* Buggy case */
650 pr_err("This program has a bug at %s:%d.\n",
651 __FILE__, __LINE__);
652 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500653 break;
654 }
655 }
656
657 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400658 if (pp->lazy_line && pp->line) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500659 semantic_error("Lazy pattern can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400660 return -EINVAL;
661 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500662
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400663 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500664 semantic_error("Lazy pattern can't be used with offset.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400665 return -EINVAL;
666 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500667
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400668 if (pp->line && pp->offset) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500669 semantic_error("Offset can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400670 return -EINVAL;
671 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500672
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400673 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500674 semantic_error("File always requires line number or "
675 "lazy pattern.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400676 return -EINVAL;
677 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500678
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400679 if (pp->offset && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500680 semantic_error("Offset requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400681 return -EINVAL;
682 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500683
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400684 if (pp->retprobe && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500685 semantic_error("Return probe requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400686 return -EINVAL;
687 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500688
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400689 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500690 semantic_error("Offset/Line/Lazy pattern can't be used with "
691 "return probe.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400692 return -EINVAL;
693 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500694
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400695 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500696 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
697 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400698 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500699}
700
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400701/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400702static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400703{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400704 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400705 struct perf_probe_arg_field **fieldp;
706
707 pr_debug("parsing arg: %s into ", str);
708
Masami Hiramatsu48481932010-04-12 13:16:53 -0400709 tmp = strchr(str, '=');
710 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400711 arg->name = strndup(str, tmp - str);
712 if (arg->name == NULL)
713 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400714 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400715 str = tmp + 1;
716 }
717
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400718 tmp = strchr(str, ':');
719 if (tmp) { /* Type setting */
720 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400721 arg->type = strdup(tmp + 1);
722 if (arg->type == NULL)
723 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400724 pr_debug("type:%s ", arg->type);
725 }
726
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400727 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400728 if (!is_c_varname(str) || !tmp) {
729 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400730 arg->var = strdup(str);
731 if (arg->var == NULL)
732 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400733 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400734 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400735 }
736
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400737 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400738 arg->var = strndup(str, tmp - str);
739 if (arg->var == NULL)
740 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400741 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400742 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400743 fieldp = &arg->field;
744
745 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400746 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
747 if (*fieldp == NULL)
748 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400749 if (*tmp == '[') { /* Array */
750 str = tmp;
751 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400752 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400753 if (*tmp != ']' || tmp == str + 1) {
754 semantic_error("Array index must be a"
755 " number.\n");
756 return -EINVAL;
757 }
758 tmp++;
759 if (*tmp == '\0')
760 tmp = NULL;
761 } else { /* Structure */
762 if (*tmp == '.') {
763 str = tmp + 1;
764 (*fieldp)->ref = false;
765 } else if (tmp[1] == '>') {
766 str = tmp + 2;
767 (*fieldp)->ref = true;
768 } else {
769 semantic_error("Argument parse error: %s\n",
770 str);
771 return -EINVAL;
772 }
773 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400774 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400775 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400776 (*fieldp)->name = strndup(str, tmp - str);
777 if ((*fieldp)->name == NULL)
778 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400779 if (*str != '[')
780 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400781 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
782 fieldp = &(*fieldp)->next;
783 }
784 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400785 (*fieldp)->name = strdup(str);
786 if ((*fieldp)->name == NULL)
787 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400788 if (*str != '[')
789 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400790 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400791
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400792 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400793 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400794 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400795 if (arg->name == NULL)
796 return -ENOMEM;
797 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400798 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400799}
800
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400801/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400802int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500803{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500804 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400805 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500806
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400807 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400808 if (!argv) {
809 pr_debug("Failed to split arguments.\n");
810 return -ENOMEM;
811 }
812 if (argc - 1 > MAX_PROBE_ARGS) {
813 semantic_error("Too many probe arguments (%d).\n", argc - 1);
814 ret = -ERANGE;
815 goto out;
816 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500817 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400818 ret = parse_perf_probe_point(argv[0], pev);
819 if (ret < 0)
820 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500821
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500822 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400823 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400824 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
825 if (pev->args == NULL) {
826 ret = -ENOMEM;
827 goto out;
828 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400829 for (i = 0; i < pev->nargs && ret >= 0; i++) {
830 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
831 if (ret >= 0 &&
832 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400833 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400834 " kretprobe.\n");
835 ret = -EINVAL;
836 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500837 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400838out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500839 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400840
841 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500842}
843
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400844/* Return true if this perf_probe_event requires debuginfo */
845bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500846{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400847 int i;
848
849 if (pev->point.file || pev->point.line || pev->point.lazy_line)
850 return true;
851
852 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400853 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400854 return true;
855
856 return false;
857}
858
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530859/* Parse probe_events event into struct probe_point */
860static int parse_probe_trace_command(const char *cmd,
861 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400862{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530863 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500864 char pr;
865 char *p;
866 int ret, i, argc;
867 char **argv;
868
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530869 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400870 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400871 if (!argv) {
872 pr_debug("Failed to split arguments.\n");
873 return -ENOMEM;
874 }
875 if (argc < 2) {
876 semantic_error("Too few probe arguments.\n");
877 ret = -ERANGE;
878 goto out;
879 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500880
881 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800882 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400883 &pr, (float *)(void *)&tev->group,
884 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400885 if (ret != 3) {
886 semantic_error("Failed to parse event name: %s\n", argv[0]);
887 ret = -EINVAL;
888 goto out;
889 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400890 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500891
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400892 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500893
894 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400895 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
896 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500897 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400898 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500899
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400900 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530901 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400902 if (tev->args == NULL) {
903 ret = -ENOMEM;
904 goto out;
905 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400906 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500907 p = strchr(argv[i + 2], '=');
908 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400909 *p++ = '\0';
910 else
911 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400912 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400913 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400914 tev->args[i].value = strdup(p);
915 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
916 ret = -ENOMEM;
917 goto out;
918 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500919 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400920 ret = 0;
921out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500922 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400923 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500924}
925
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400926/* Compose only probe arg */
927int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
928{
929 struct perf_probe_arg_field *field = pa->field;
930 int ret;
931 char *tmp = buf;
932
Masami Hiramatsu48481932010-04-12 13:16:53 -0400933 if (pa->name && pa->var)
934 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
935 else
936 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400937 if (ret <= 0)
938 goto error;
939 tmp += ret;
940 len -= ret;
941
942 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400943 if (field->name[0] == '[')
944 ret = e_snprintf(tmp, len, "%s", field->name);
945 else
946 ret = e_snprintf(tmp, len, "%s%s",
947 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400948 if (ret <= 0)
949 goto error;
950 tmp += ret;
951 len -= ret;
952 field = field->next;
953 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400954
955 if (pa->type) {
956 ret = e_snprintf(tmp, len, ":%s", pa->type);
957 if (ret <= 0)
958 goto error;
959 tmp += ret;
960 len -= ret;
961 }
962
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400963 return tmp - buf;
964error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400965 pr_debug("Failed to synthesize perf probe argument: %s",
966 strerror(-ret));
967 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400968}
969
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400970/* Compose only probe point (not argument) */
971static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500972{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400973 char *buf, *tmp;
974 char offs[32] = "", line[32] = "", file[32] = "";
975 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500976
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400977 buf = zalloc(MAX_CMDLEN);
978 if (buf == NULL) {
979 ret = -ENOMEM;
980 goto error;
981 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500982 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400983 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500984 if (ret <= 0)
985 goto error;
986 }
987 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400988 ret = e_snprintf(line, 32, ":%d", pp->line);
989 if (ret <= 0)
990 goto error;
991 }
992 if (pp->file) {
Masami Hiramatsudd259c52010-04-14 18:39:35 -0400993 len = strlen(pp->file) - 31;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400994 if (len < 0)
995 len = 0;
996 tmp = strchr(pp->file + len, '/');
997 if (!tmp)
Masami Hiramatsudd259c52010-04-14 18:39:35 -0400998 tmp = pp->file + len;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400999 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001000 if (ret <= 0)
1001 goto error;
1002 }
1003
1004 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001005 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1006 offs, pp->retprobe ? "%return" : "", line,
1007 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001008 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001009 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001010 if (ret <= 0)
1011 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001012
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001013 return buf;
1014error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001015 pr_debug("Failed to synthesize perf probe point: %s",
1016 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001017 if (buf)
1018 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001019 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001020}
1021
1022#if 0
1023char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1024{
1025 char *buf;
1026 int i, len, ret;
1027
1028 buf = synthesize_perf_probe_point(&pev->point);
1029 if (!buf)
1030 return NULL;
1031
1032 len = strlen(buf);
1033 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001034 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001035 pev->args[i].name);
1036 if (ret <= 0) {
1037 free(buf);
1038 return NULL;
1039 }
1040 len += ret;
1041 }
1042
1043 return buf;
1044}
1045#endif
1046
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301047static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001048 char **buf, size_t *buflen,
1049 int depth)
1050{
1051 int ret;
1052 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301053 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001054 buflen, depth + 1);
1055 if (depth < 0)
1056 goto out;
1057 }
1058
1059 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1060 if (ret < 0)
1061 depth = ret;
1062 else {
1063 *buf += ret;
1064 *buflen -= ret;
1065 }
1066out:
1067 return depth;
1068
1069}
1070
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301071static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001072 char *buf, size_t buflen)
1073{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301074 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001075 int ret, depth = 0;
1076 char *tmp = buf;
1077
1078 /* Argument name or separator */
1079 if (arg->name)
1080 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1081 else
1082 ret = e_snprintf(buf, buflen, " ");
1083 if (ret < 0)
1084 return ret;
1085 buf += ret;
1086 buflen -= ret;
1087
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001088 /* Special case: @XXX */
1089 if (arg->value[0] == '@' && arg->ref)
1090 ref = ref->next;
1091
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001092 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001093 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301094 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001095 &buflen, 1);
1096 if (depth < 0)
1097 return depth;
1098 }
1099
1100 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001101 if (arg->value[0] == '@' && arg->ref)
1102 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1103 arg->ref->offset);
1104 else
1105 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001106 if (ret < 0)
1107 return ret;
1108 buf += ret;
1109 buflen -= ret;
1110
1111 /* Closing */
1112 while (depth--) {
1113 ret = e_snprintf(buf, buflen, ")");
1114 if (ret < 0)
1115 return ret;
1116 buf += ret;
1117 buflen -= ret;
1118 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001119 /* Print argument type */
1120 if (arg->type) {
1121 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1122 if (ret <= 0)
1123 return ret;
1124 buf += ret;
1125 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001126
1127 return buf - tmp;
1128}
1129
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301130char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001131{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301132 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001133 char *buf;
1134 int i, len, ret;
1135
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001136 buf = zalloc(MAX_CMDLEN);
1137 if (buf == NULL)
1138 return NULL;
1139
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001140 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1141 tp->retprobe ? 'r' : 'p',
1142 tev->group, tev->event,
1143 tp->symbol, tp->offset);
1144 if (len <= 0)
1145 goto error;
1146
1147 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301148 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001149 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001150 if (ret <= 0)
1151 goto error;
1152 len += ret;
1153 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001154
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001155 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001156error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001157 free(buf);
1158 return NULL;
1159}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001160
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301161static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001162 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001163{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001164 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001165 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001166
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001167 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001168 pev->event = strdup(tev->event);
1169 pev->group = strdup(tev->group);
1170 if (pev->event == NULL || pev->group == NULL)
1171 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001172
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001173 /* Convert trace_point to probe_point */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301174 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001175 if (ret < 0)
1176 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001177
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001178 /* Convert trace_arg to probe_arg */
1179 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001180 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1181 if (pev->args == NULL)
1182 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001183 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001184 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001185 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001186 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301187 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001188 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001189 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001190 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001191 if (pev->args[i].name == NULL && ret >= 0)
1192 ret = -ENOMEM;
1193 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001194
1195 if (ret < 0)
1196 clear_perf_probe_event(pev);
1197
1198 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001199}
1200
1201void clear_perf_probe_event(struct perf_probe_event *pev)
1202{
1203 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001204 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001205 int i;
1206
1207 if (pev->event)
1208 free(pev->event);
1209 if (pev->group)
1210 free(pev->group);
1211 if (pp->file)
1212 free(pp->file);
1213 if (pp->function)
1214 free(pp->function);
1215 if (pp->lazy_line)
1216 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001217 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001218 if (pev->args[i].name)
1219 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001220 if (pev->args[i].var)
1221 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001222 if (pev->args[i].type)
1223 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001224 field = pev->args[i].field;
1225 while (field) {
1226 next = field->next;
1227 if (field->name)
1228 free(field->name);
1229 free(field);
1230 field = next;
1231 }
1232 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001233 if (pev->args)
1234 free(pev->args);
1235 memset(pev, 0, sizeof(*pev));
1236}
1237
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301238static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001239{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301240 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001241 int i;
1242
1243 if (tev->event)
1244 free(tev->event);
1245 if (tev->group)
1246 free(tev->group);
1247 if (tev->point.symbol)
1248 free(tev->point.symbol);
1249 for (i = 0; i < tev->nargs; i++) {
1250 if (tev->args[i].name)
1251 free(tev->args[i].name);
1252 if (tev->args[i].value)
1253 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001254 if (tev->args[i].type)
1255 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001256 ref = tev->args[i].ref;
1257 while (ref) {
1258 next = ref->next;
1259 free(ref);
1260 ref = next;
1261 }
1262 }
1263 if (tev->args)
1264 free(tev->args);
1265 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001266}
1267
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001268static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001269{
1270 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001271 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001272 int ret;
1273
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001274 __debugfs = debugfs_find_mountpoint();
1275 if (__debugfs == NULL) {
1276 pr_warning("Debugfs is not mounted.\n");
1277 return -ENOENT;
1278 }
1279
1280 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001281 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001282 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001283 if (readwrite && !probe_event_dry_run)
1284 ret = open(buf, O_RDWR, O_APPEND);
1285 else
1286 ret = open(buf, O_RDONLY, 0);
1287 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001288
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001289 if (ret < 0) {
1290 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001291 pr_warning("kprobe_events file does not exist - please"
1292 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001293 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001294 pr_warning("Failed to open kprobe_events file: %s\n",
1295 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001296 }
1297 return ret;
1298}
1299
1300/* Get raw string list of current kprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301301static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001302{
1303 int ret, idx;
1304 FILE *fp;
1305 char buf[MAX_CMDLEN];
1306 char *p;
1307 struct strlist *sl;
1308
1309 sl = strlist__new(true, NULL);
1310
1311 fp = fdopen(dup(fd), "r");
1312 while (!feof(fp)) {
1313 p = fgets(buf, MAX_CMDLEN, fp);
1314 if (!p)
1315 break;
1316
1317 idx = strlen(p) - 1;
1318 if (p[idx] == '\n')
1319 p[idx] = '\0';
1320 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001321 if (ret < 0) {
1322 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1323 strlist__delete(sl);
1324 return NULL;
1325 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001326 }
1327 fclose(fp);
1328
1329 return sl;
1330}
1331
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001332/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001333static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001334{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001335 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001336 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001337 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001338
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001339 /* Synthesize only event probe point */
1340 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001341 if (!place)
1342 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001343
1344 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001345 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001346 return ret;
1347
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001348 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001349
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001350 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001351 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001352 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001353 ret = synthesize_perf_probe_arg(&pev->args[i],
1354 buf, 128);
1355 if (ret < 0)
1356 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001357 printf(" %s", buf);
1358 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001359 }
1360 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001361 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001362 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001363}
1364
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001365/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001366int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001367{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001368 int fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301369 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001370 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001371 struct strlist *rawlist;
1372 struct str_node *ent;
1373
Masami Hiramatsu72041332010-01-05 17:47:10 -05001374 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001375 ret = init_vmlinux();
1376 if (ret < 0)
1377 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001378
1379 memset(&tev, 0, sizeof(tev));
1380 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001381
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001382 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001383 if (fd < 0)
1384 return fd;
1385
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301386 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001387 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001388 if (!rawlist)
1389 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001390
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001391 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301392 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001393 if (ret >= 0) {
1394 ret = convert_to_perf_probe_event(&tev, &pev);
1395 if (ret >= 0)
1396 ret = show_perf_probe_event(&pev);
1397 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001398 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301399 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001400 if (ret < 0)
1401 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001402 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001403 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001404
1405 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001406}
1407
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001408/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301409static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001410{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001411 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001412 struct strlist *sl, *rawlist;
1413 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301414 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001415 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001416
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001417 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301418 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001419 sl = strlist__new(true, NULL);
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 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001424 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001425 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1426 tev.event);
1427 if (ret >= 0)
1428 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001429 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001430 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301431 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001432 if (ret < 0)
1433 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001434 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001435 strlist__delete(rawlist);
1436
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001437 if (ret < 0) {
1438 strlist__delete(sl);
1439 return NULL;
1440 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001441 return sl;
1442}
1443
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301444static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001445{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001446 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301447 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001448
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001449 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301450 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001451 return -EINVAL;
1452 }
1453
Masami Hiramatsufa282442009-12-08 17:03:23 -05001454 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001455 if (!probe_event_dry_run) {
1456 ret = write(fd, buf, strlen(buf));
1457 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001458 pr_warning("Failed to write event: %s\n",
1459 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001460 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001461 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001462 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001463}
1464
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001465static int get_new_event_name(char *buf, size_t len, const char *base,
1466 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001467{
1468 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001469
1470 /* Try no suffix */
1471 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001472 if (ret < 0) {
1473 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1474 return ret;
1475 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001476 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001477 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001478
Masami Hiramatsud761b082009-12-15 10:32:25 -05001479 if (!allow_suffix) {
1480 pr_warning("Error: event \"%s\" already exists. "
1481 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001482 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001483 }
1484
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001485 /* Try to add suffix */
1486 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001487 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001488 if (ret < 0) {
1489 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1490 return ret;
1491 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001492 if (!strlist__has_entry(namelist, buf))
1493 break;
1494 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001495 if (i == MAX_EVENT_INDEX) {
1496 pr_warning("Too many events are on the same function.\n");
1497 ret = -ERANGE;
1498 }
1499
1500 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001501}
1502
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301503static int __add_probe_trace_events(struct perf_probe_event *pev,
1504 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001505 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001506{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001507 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301508 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001509 char buf[64];
1510 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001511 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001512
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001513 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001514 if (fd < 0)
1515 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001516 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301517 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001518 if (!namelist) {
1519 pr_debug("Failed to get current event list.\n");
1520 return -EIO;
1521 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001522
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001523 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001524 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001525 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001526 tev = &tevs[i];
1527 if (pev->event)
1528 event = pev->event;
1529 else
1530 if (pev->point.function)
1531 event = pev->point.function;
1532 else
1533 event = tev->point.symbol;
1534 if (pev->group)
1535 group = pev->group;
1536 else
1537 group = PERFPROBE_GROUP;
1538
1539 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001540 ret = get_new_event_name(buf, 64, event,
1541 namelist, allow_suffix);
1542 if (ret < 0)
1543 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001544 event = buf;
1545
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001546 tev->event = strdup(event);
1547 tev->group = strdup(group);
1548 if (tev->event == NULL || tev->group == NULL) {
1549 ret = -ENOMEM;
1550 break;
1551 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301552 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001553 if (ret < 0)
1554 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001555 /* Add added event name to namelist */
1556 strlist__add(namelist, event);
1557
1558 /* Trick here - save current event/group */
1559 event = pev->event;
1560 group = pev->group;
1561 pev->event = tev->event;
1562 pev->group = tev->group;
1563 show_perf_probe_event(pev);
1564 /* Trick here - restore current event/group */
1565 pev->event = (char *)event;
1566 pev->group = (char *)group;
1567
1568 /*
1569 * Probes after the first probe which comes from same
1570 * user input are always allowed to add suffix, because
1571 * there might be several addresses corresponding to
1572 * one code line.
1573 */
1574 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001575 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001576
1577 if (ret >= 0) {
1578 /* Show how to use the event. */
1579 printf("\nYou can now use it on all perf tools, such as:\n\n");
1580 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1581 tev->event);
1582 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001583
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001584 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001585 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001586 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001587}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001588
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301589static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1590 struct probe_trace_event **tevs,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001591 int max_tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001592{
1593 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001594 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301595 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001596
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001597 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301598 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001599 if (ret != 0)
1600 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001601
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001602 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301603 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001604 if (tev == NULL)
1605 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001606
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001607 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001608 tev->point.symbol = strdup(pev->point.function);
1609 if (tev->point.symbol == NULL) {
1610 ret = -ENOMEM;
1611 goto error;
1612 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001613 tev->point.offset = pev->point.offset;
Masami Hiramatsu04ddd042010-08-27 20:38:53 +09001614 tev->point.retprobe = pev->point.retprobe;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001615 tev->nargs = pev->nargs;
1616 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301617 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001618 * tev->nargs);
1619 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001620 ret = -ENOMEM;
1621 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001622 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001623 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001624 if (pev->args[i].name) {
1625 tev->args[i].name = strdup(pev->args[i].name);
1626 if (tev->args[i].name == NULL) {
1627 ret = -ENOMEM;
1628 goto error;
1629 }
1630 }
1631 tev->args[i].value = strdup(pev->args[i].var);
1632 if (tev->args[i].value == NULL) {
1633 ret = -ENOMEM;
1634 goto error;
1635 }
1636 if (pev->args[i].type) {
1637 tev->args[i].type = strdup(pev->args[i].type);
1638 if (tev->args[i].type == NULL) {
1639 ret = -ENOMEM;
1640 goto error;
1641 }
1642 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001643 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001644 }
1645
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001646 /* Currently just checking function name from symbol map */
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001647 sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION],
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001648 tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001649 if (!sym) {
1650 pr_warning("Kernel symbol \'%s\' not found.\n",
1651 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001652 ret = -ENOENT;
1653 goto error;
1654 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001655
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001656 return 1;
1657error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301658 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001659 free(tev);
1660 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001661 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001662}
1663
1664struct __event_package {
1665 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301666 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001667 int ntevs;
1668};
1669
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001670int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001671 bool force_add, int max_tevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001672{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001673 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001674 struct __event_package *pkgs;
1675
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001676 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1677 if (pkgs == NULL)
1678 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001679
1680 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001681 ret = init_vmlinux();
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001682 if (ret < 0) {
1683 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001684 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001685 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001686
1687 /* Loop 1: convert all events */
1688 for (i = 0; i < npevs; i++) {
1689 pkgs[i].pev = &pevs[i];
1690 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301691 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001692 &pkgs[i].tevs, max_tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001693 if (ret < 0)
1694 goto end;
1695 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001696 }
1697
1698 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001699 for (i = 0; i < npevs && ret >= 0; i++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301700 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001701 pkgs[i].ntevs, force_add);
1702end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001703 /* Loop 3: cleanup and free trace events */
1704 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001705 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301706 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001707 free(pkgs[i].tevs);
1708 }
1709 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001710
1711 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001712}
1713
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301714static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001715{
1716 char *p;
1717 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001718 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001719
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301720 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001721 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1722 if (ret < 0)
1723 goto error;
1724
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001725 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001726 if (!p) {
1727 pr_debug("Internal error: %s should have ':' but not.\n",
1728 ent->s);
1729 ret = -ENOTSUP;
1730 goto error;
1731 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001732 *p = '/';
1733
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001734 pr_debug("Writing event: %s\n", buf);
1735 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001736 if (ret < 0)
1737 goto error;
1738
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001739 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001740 return 0;
1741error:
1742 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1743 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001744}
1745
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301746static int del_trace_probe_event(int fd, const char *group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001747 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001748{
1749 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001750 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001751 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001752
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001753 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1754 if (ret < 0) {
1755 pr_err("Failed to copy event.");
1756 return ret;
1757 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001758
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001759 if (strpbrk(buf, "*?")) { /* Glob-exp */
1760 strlist__for_each_safe(ent, n, namelist)
1761 if (strglobmatch(ent->s, buf)) {
1762 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301763 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001764 if (ret < 0)
1765 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001766 strlist__remove(namelist, ent);
1767 }
1768 } else {
1769 ent = strlist__find(namelist, buf);
1770 if (ent) {
1771 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301772 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001773 if (ret >= 0)
1774 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001775 }
1776 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001777 if (found == 0 && ret >= 0)
1778 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1779
1780 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001781}
1782
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001783int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001784{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001785 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001786 const char *group, *event;
1787 char *p, *str;
1788 struct str_node *ent;
1789 struct strlist *namelist;
1790
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001791 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001792 if (fd < 0)
1793 return fd;
1794
Masami Hiramatsufa282442009-12-08 17:03:23 -05001795 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301796 namelist = get_probe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001797 if (namelist == NULL)
1798 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001799
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001800 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001801 str = strdup(ent->s);
1802 if (str == NULL) {
1803 ret = -ENOMEM;
1804 break;
1805 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001806 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001807 p = strchr(str, ':');
1808 if (p) {
1809 group = str;
1810 *p = '\0';
1811 event = p + 1;
1812 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001813 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001814 event = str;
1815 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001816 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301817 ret = del_trace_probe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001818 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001819 if (ret < 0)
1820 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001821 }
1822 strlist__delete(namelist);
1823 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001824
1825 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001826}
1827