blob: e72f05c3bef09258311f7192afb179a10657c450 [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
381#else /* !DWARF_SUPPORT */
382
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530383static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
384 struct perf_probe_point *pp)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300385{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400386 pp->function = strdup(tp->symbol);
387 if (pp->function == NULL)
388 return -ENOMEM;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300389 pp->offset = tp->offset;
390 pp->retprobe = tp->retprobe;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400391
392 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300393}
394
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530395static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
396 struct probe_trace_event **tevs __unused,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -0400397 int max_tevs __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300398{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400399 if (perf_probe_event_need_dwarf(pev)) {
400 pr_warning("Debuginfo-analysis is not supported.\n");
401 return -ENOSYS;
402 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300403 return 0;
404}
405
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400406int show_line_range(struct line_range *lr __unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300407{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400408 pr_warning("Debuginfo-analysis is not supported.\n");
409 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300410}
411
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400412#endif
413
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400414int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500415{
416 const char *ptr;
417 char *tmp;
418 /*
419 * <Syntax>
420 * SRC:SLN[+NUM|-ELN]
421 * FUNC[:SLN[+NUM|-ELN]]
422 */
423 ptr = strchr(arg, ':');
424 if (ptr) {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400425 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400426 if (*tmp == '+') {
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400427 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsudda4ab32010-04-14 18:39:50 -0400428 lr->end--; /*
429 * Adjust the number of lines here.
430 * If the number of lines == 1, the
431 * the end of line should be equal to
432 * the start of line.
433 */
434 } else if (*tmp == '-')
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400435 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500436 else
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400437 lr->end = INT_MAX;
438 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
439 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500440 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400441 " than end line.\n");
442 return -EINVAL;
443 }
444 if (*tmp != '\0') {
445 semantic_error("Tailing with invalid character '%d'.\n",
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500446 *tmp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400447 return -EINVAL;
448 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400449 tmp = strndup(arg, (ptr - arg));
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400450 } else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400451 tmp = strdup(arg);
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400452 lr->end = INT_MAX;
453 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400454
455 if (tmp == NULL)
456 return -ENOMEM;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500457
458 if (strchr(tmp, '.'))
459 lr->file = tmp;
460 else
461 lr->function = tmp;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400462
463 return 0;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500464}
465
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500466/* Check the name is good for event/group */
467static bool check_event_name(const char *name)
468{
469 if (!isalpha(*name) && *name != '_')
470 return false;
471 while (*++name != '\0') {
472 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
473 return false;
474 }
475 return true;
476}
477
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500478/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400479static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500480{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400481 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500482 char *ptr, *tmp;
483 char c, nc = 0;
484 /*
485 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500486 * perf probe [EVENT=]SRC[:LN|;PTN]
487 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500488 *
489 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500490 */
491
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500492 ptr = strpbrk(arg, ";=@+%");
493 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500494 *ptr = '\0';
495 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400496 if (strchr(arg, ':')) {
497 semantic_error("Group name is not supported yet.\n");
498 return -ENOTSUP;
499 }
500 if (!check_event_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500501 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400502 "follow C symbol-naming rule.\n", arg);
503 return -EINVAL;
504 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400505 pev->event = strdup(arg);
506 if (pev->event == NULL)
507 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400508 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500509 arg = tmp;
510 }
511
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500512 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500513 if (ptr) {
514 nc = *ptr;
515 *ptr++ = '\0';
516 }
517
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400518 tmp = strdup(arg);
519 if (tmp == NULL)
520 return -ENOMEM;
521
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500522 /* Check arg is function or file and copy it */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400523 if (strchr(tmp, '.')) /* File */
524 pp->file = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500525 else /* Function */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400526 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500527
528 /* Parse other options */
529 while (ptr) {
530 arg = ptr;
531 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500532 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400533 pp->lazy_line = strdup(arg);
534 if (pp->lazy_line == NULL)
535 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500536 break;
537 }
538 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500539 if (ptr) {
540 nc = *ptr;
541 *ptr++ = '\0';
542 }
543 switch (c) {
544 case ':': /* Line number */
545 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400546 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500547 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400548 " in line number.\n");
549 return -EINVAL;
550 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500551 break;
552 case '+': /* Byte offset from a symbol */
553 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400554 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500555 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400556 " in offset.\n");
557 return -EINVAL;
558 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500559 break;
560 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400561 if (pp->file) {
562 semantic_error("SRC@SRC is not allowed.\n");
563 return -EINVAL;
564 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400565 pp->file = strdup(arg);
566 if (pp->file == NULL)
567 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500568 break;
569 case '%': /* Probe places */
570 if (strcmp(arg, "return") == 0) {
571 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400572 } else { /* Others not supported yet */
573 semantic_error("%%%s is not supported.\n", arg);
574 return -ENOTSUP;
575 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500576 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400577 default: /* Buggy case */
578 pr_err("This program has a bug at %s:%d.\n",
579 __FILE__, __LINE__);
580 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500581 break;
582 }
583 }
584
585 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400586 if (pp->lazy_line && pp->line) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500587 semantic_error("Lazy pattern can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400588 return -EINVAL;
589 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500590
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400591 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500592 semantic_error("Lazy pattern can't be used with offset.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400593 return -EINVAL;
594 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500595
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400596 if (pp->line && pp->offset) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500597 semantic_error("Offset can't be used with line number.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400598 return -EINVAL;
599 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500600
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400601 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500602 semantic_error("File always requires line number or "
603 "lazy pattern.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400604 return -EINVAL;
605 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500606
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400607 if (pp->offset && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500608 semantic_error("Offset requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400609 return -EINVAL;
610 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500611
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400612 if (pp->retprobe && !pp->function) {
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500613 semantic_error("Return probe requires an entry function.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400614 return -EINVAL;
615 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500616
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400617 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500618 semantic_error("Offset/Line/Lazy pattern can't be used with "
619 "return probe.");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400620 return -EINVAL;
621 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500622
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400623 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500624 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
625 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400626 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500627}
628
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400629/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400630static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400631{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400632 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400633 struct perf_probe_arg_field **fieldp;
634
635 pr_debug("parsing arg: %s into ", str);
636
Masami Hiramatsu48481932010-04-12 13:16:53 -0400637 tmp = strchr(str, '=');
638 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400639 arg->name = strndup(str, tmp - str);
640 if (arg->name == NULL)
641 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400642 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400643 str = tmp + 1;
644 }
645
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400646 tmp = strchr(str, ':');
647 if (tmp) { /* Type setting */
648 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400649 arg->type = strdup(tmp + 1);
650 if (arg->type == NULL)
651 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400652 pr_debug("type:%s ", arg->type);
653 }
654
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400655 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400656 if (!is_c_varname(str) || !tmp) {
657 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400658 arg->var = strdup(str);
659 if (arg->var == NULL)
660 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400661 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400662 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400663 }
664
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400665 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400666 arg->var = strndup(str, tmp - str);
667 if (arg->var == NULL)
668 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400669 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -0400670 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400671 fieldp = &arg->field;
672
673 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400674 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
675 if (*fieldp == NULL)
676 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400677 if (*tmp == '[') { /* Array */
678 str = tmp;
679 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400680 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400681 if (*tmp != ']' || tmp == str + 1) {
682 semantic_error("Array index must be a"
683 " number.\n");
684 return -EINVAL;
685 }
686 tmp++;
687 if (*tmp == '\0')
688 tmp = NULL;
689 } else { /* Structure */
690 if (*tmp == '.') {
691 str = tmp + 1;
692 (*fieldp)->ref = false;
693 } else if (tmp[1] == '>') {
694 str = tmp + 2;
695 (*fieldp)->ref = true;
696 } else {
697 semantic_error("Argument parse error: %s\n",
698 str);
699 return -EINVAL;
700 }
701 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400702 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400703 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400704 (*fieldp)->name = strndup(str, tmp - str);
705 if ((*fieldp)->name == NULL)
706 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400707 if (*str != '[')
708 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400709 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
710 fieldp = &(*fieldp)->next;
711 }
712 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400713 (*fieldp)->name = strdup(str);
714 if ((*fieldp)->name == NULL)
715 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400716 if (*str != '[')
717 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400718 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400719
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400720 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400721 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400722 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400723 if (arg->name == NULL)
724 return -ENOMEM;
725 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400726 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400727}
728
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400729/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400730int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500731{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500732 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400733 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500734
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400735 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400736 if (!argv) {
737 pr_debug("Failed to split arguments.\n");
738 return -ENOMEM;
739 }
740 if (argc - 1 > MAX_PROBE_ARGS) {
741 semantic_error("Too many probe arguments (%d).\n", argc - 1);
742 ret = -ERANGE;
743 goto out;
744 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500745 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400746 ret = parse_perf_probe_point(argv[0], pev);
747 if (ret < 0)
748 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500749
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500750 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400751 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400752 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
753 if (pev->args == NULL) {
754 ret = -ENOMEM;
755 goto out;
756 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400757 for (i = 0; i < pev->nargs && ret >= 0; i++) {
758 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
759 if (ret >= 0 &&
760 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400761 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400762 " kretprobe.\n");
763 ret = -EINVAL;
764 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500765 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400766out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500767 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400768
769 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500770}
771
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400772/* Return true if this perf_probe_event requires debuginfo */
773bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500774{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400775 int i;
776
777 if (pev->point.file || pev->point.line || pev->point.lazy_line)
778 return true;
779
780 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400781 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400782 return true;
783
784 return false;
785}
786
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530787/* Parse probe_events event into struct probe_point */
788static int parse_probe_trace_command(const char *cmd,
789 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400790{
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530791 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500792 char pr;
793 char *p;
794 int ret, i, argc;
795 char **argv;
796
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530797 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400798 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400799 if (!argv) {
800 pr_debug("Failed to split arguments.\n");
801 return -ENOMEM;
802 }
803 if (argc < 2) {
804 semantic_error("Too few probe arguments.\n");
805 ret = -ERANGE;
806 goto out;
807 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500808
809 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800810 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400811 &pr, (float *)(void *)&tev->group,
812 (float *)(void *)&tev->event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400813 if (ret != 3) {
814 semantic_error("Failed to parse event name: %s\n", argv[0]);
815 ret = -EINVAL;
816 goto out;
817 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400818 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500819
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400820 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500821
822 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400823 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
824 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500825 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400826 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500827
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400828 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530829 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400830 if (tev->args == NULL) {
831 ret = -ENOMEM;
832 goto out;
833 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400834 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500835 p = strchr(argv[i + 2], '=');
836 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400837 *p++ = '\0';
838 else
839 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400840 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400841 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -0400842 tev->args[i].value = strdup(p);
843 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
844 ret = -ENOMEM;
845 goto out;
846 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500847 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400848 ret = 0;
849out:
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500850 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400851 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500852}
853
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400854/* Compose only probe arg */
855int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
856{
857 struct perf_probe_arg_field *field = pa->field;
858 int ret;
859 char *tmp = buf;
860
Masami Hiramatsu48481932010-04-12 13:16:53 -0400861 if (pa->name && pa->var)
862 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
863 else
864 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400865 if (ret <= 0)
866 goto error;
867 tmp += ret;
868 len -= ret;
869
870 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -0400871 if (field->name[0] == '[')
872 ret = e_snprintf(tmp, len, "%s", field->name);
873 else
874 ret = e_snprintf(tmp, len, "%s%s",
875 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400876 if (ret <= 0)
877 goto error;
878 tmp += ret;
879 len -= ret;
880 field = field->next;
881 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -0400882
883 if (pa->type) {
884 ret = e_snprintf(tmp, len, ":%s", pa->type);
885 if (ret <= 0)
886 goto error;
887 tmp += ret;
888 len -= ret;
889 }
890
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400891 return tmp - buf;
892error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400893 pr_debug("Failed to synthesize perf probe argument: %s",
894 strerror(-ret));
895 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400896}
897
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400898/* Compose only probe point (not argument) */
899static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500900{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400901 char *buf, *tmp;
902 char offs[32] = "", line[32] = "", file[32] = "";
903 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500904
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400905 buf = zalloc(MAX_CMDLEN);
906 if (buf == NULL) {
907 ret = -ENOMEM;
908 goto error;
909 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500910 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400911 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500912 if (ret <= 0)
913 goto error;
914 }
915 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400916 ret = e_snprintf(line, 32, ":%d", pp->line);
917 if (ret <= 0)
918 goto error;
919 }
920 if (pp->file) {
Masami Hiramatsudd259c52010-04-14 18:39:35 -0400921 len = strlen(pp->file) - 31;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400922 if (len < 0)
923 len = 0;
924 tmp = strchr(pp->file + len, '/');
925 if (!tmp)
Masami Hiramatsudd259c52010-04-14 18:39:35 -0400926 tmp = pp->file + len;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400927 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500928 if (ret <= 0)
929 goto error;
930 }
931
932 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400933 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
934 offs, pp->retprobe ? "%return" : "", line,
935 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500936 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400937 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500938 if (ret <= 0)
939 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500940
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400941 return buf;
942error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400943 pr_debug("Failed to synthesize perf probe point: %s",
944 strerror(-ret));
Masami Hiramatsue334016f12010-04-12 13:17:49 -0400945 if (buf)
946 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400947 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400948}
949
950#if 0
951char *synthesize_perf_probe_command(struct perf_probe_event *pev)
952{
953 char *buf;
954 int i, len, ret;
955
956 buf = synthesize_perf_probe_point(&pev->point);
957 if (!buf)
958 return NULL;
959
960 len = strlen(buf);
961 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500962 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400963 pev->args[i].name);
964 if (ret <= 0) {
965 free(buf);
966 return NULL;
967 }
968 len += ret;
969 }
970
971 return buf;
972}
973#endif
974
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530975static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400976 char **buf, size_t *buflen,
977 int depth)
978{
979 int ret;
980 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530981 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400982 buflen, depth + 1);
983 if (depth < 0)
984 goto out;
985 }
986
987 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
988 if (ret < 0)
989 depth = ret;
990 else {
991 *buf += ret;
992 *buflen -= ret;
993 }
994out:
995 return depth;
996
997}
998
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530999static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001000 char *buf, size_t buflen)
1001{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301002 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001003 int ret, depth = 0;
1004 char *tmp = buf;
1005
1006 /* Argument name or separator */
1007 if (arg->name)
1008 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1009 else
1010 ret = e_snprintf(buf, buflen, " ");
1011 if (ret < 0)
1012 return ret;
1013 buf += ret;
1014 buflen -= ret;
1015
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001016 /* Special case: @XXX */
1017 if (arg->value[0] == '@' && arg->ref)
1018 ref = ref->next;
1019
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001020 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001021 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301022 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001023 &buflen, 1);
1024 if (depth < 0)
1025 return depth;
1026 }
1027
1028 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001029 if (arg->value[0] == '@' && arg->ref)
1030 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1031 arg->ref->offset);
1032 else
1033 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001034 if (ret < 0)
1035 return ret;
1036 buf += ret;
1037 buflen -= ret;
1038
1039 /* Closing */
1040 while (depth--) {
1041 ret = e_snprintf(buf, buflen, ")");
1042 if (ret < 0)
1043 return ret;
1044 buf += ret;
1045 buflen -= ret;
1046 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001047 /* Print argument type */
1048 if (arg->type) {
1049 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1050 if (ret <= 0)
1051 return ret;
1052 buf += ret;
1053 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001054
1055 return buf - tmp;
1056}
1057
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301058char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001059{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301060 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001061 char *buf;
1062 int i, len, ret;
1063
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001064 buf = zalloc(MAX_CMDLEN);
1065 if (buf == NULL)
1066 return NULL;
1067
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001068 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1069 tp->retprobe ? 'r' : 'p',
1070 tev->group, tev->event,
1071 tp->symbol, tp->offset);
1072 if (len <= 0)
1073 goto error;
1074
1075 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301076 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001077 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001078 if (ret <= 0)
1079 goto error;
1080 len += ret;
1081 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001082
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001083 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001084error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001085 free(buf);
1086 return NULL;
1087}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001088
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301089static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001090 struct perf_probe_event *pev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001091{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001092 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001093 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001094
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001095 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001096 pev->event = strdup(tev->event);
1097 pev->group = strdup(tev->group);
1098 if (pev->event == NULL || pev->group == NULL)
1099 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001100
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001101 /* Convert trace_point to probe_point */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301102 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001103 if (ret < 0)
1104 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001105
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001106 /* Convert trace_arg to probe_arg */
1107 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001108 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1109 if (pev->args == NULL)
1110 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001111 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001112 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001113 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001114 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301115 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001116 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001117 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001118 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001119 if (pev->args[i].name == NULL && ret >= 0)
1120 ret = -ENOMEM;
1121 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001122
1123 if (ret < 0)
1124 clear_perf_probe_event(pev);
1125
1126 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001127}
1128
1129void clear_perf_probe_event(struct perf_probe_event *pev)
1130{
1131 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001132 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001133 int i;
1134
1135 if (pev->event)
1136 free(pev->event);
1137 if (pev->group)
1138 free(pev->group);
1139 if (pp->file)
1140 free(pp->file);
1141 if (pp->function)
1142 free(pp->function);
1143 if (pp->lazy_line)
1144 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001145 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001146 if (pev->args[i].name)
1147 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001148 if (pev->args[i].var)
1149 free(pev->args[i].var);
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001150 if (pev->args[i].type)
1151 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001152 field = pev->args[i].field;
1153 while (field) {
1154 next = field->next;
1155 if (field->name)
1156 free(field->name);
1157 free(field);
1158 field = next;
1159 }
1160 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001161 if (pev->args)
1162 free(pev->args);
1163 memset(pev, 0, sizeof(*pev));
1164}
1165
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301166static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001167{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301168 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001169 int i;
1170
1171 if (tev->event)
1172 free(tev->event);
1173 if (tev->group)
1174 free(tev->group);
1175 if (tev->point.symbol)
1176 free(tev->point.symbol);
1177 for (i = 0; i < tev->nargs; i++) {
1178 if (tev->args[i].name)
1179 free(tev->args[i].name);
1180 if (tev->args[i].value)
1181 free(tev->args[i].value);
Masami Hiramatsu49849122010-04-12 13:17:15 -04001182 if (tev->args[i].type)
1183 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001184 ref = tev->args[i].ref;
1185 while (ref) {
1186 next = ref->next;
1187 free(ref);
1188 ref = next;
1189 }
1190 }
1191 if (tev->args)
1192 free(tev->args);
1193 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001194}
1195
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001196static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001197{
1198 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001199 const char *__debugfs;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001200 int ret;
1201
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001202 __debugfs = debugfs_find_mountpoint();
1203 if (__debugfs == NULL) {
1204 pr_warning("Debugfs is not mounted.\n");
1205 return -ENOENT;
1206 }
1207
1208 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001209 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001210 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001211 if (readwrite && !probe_event_dry_run)
1212 ret = open(buf, O_RDWR, O_APPEND);
1213 else
1214 ret = open(buf, O_RDONLY, 0);
1215 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001216
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001217 if (ret < 0) {
1218 if (errno == ENOENT)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001219 pr_warning("kprobe_events file does not exist - please"
1220 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001221 else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001222 pr_warning("Failed to open kprobe_events file: %s\n",
1223 strerror(errno));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001224 }
1225 return ret;
1226}
1227
1228/* Get raw string list of current kprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301229static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001230{
1231 int ret, idx;
1232 FILE *fp;
1233 char buf[MAX_CMDLEN];
1234 char *p;
1235 struct strlist *sl;
1236
1237 sl = strlist__new(true, NULL);
1238
1239 fp = fdopen(dup(fd), "r");
1240 while (!feof(fp)) {
1241 p = fgets(buf, MAX_CMDLEN, fp);
1242 if (!p)
1243 break;
1244
1245 idx = strlen(p) - 1;
1246 if (p[idx] == '\n')
1247 p[idx] = '\0';
1248 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001249 if (ret < 0) {
1250 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1251 strlist__delete(sl);
1252 return NULL;
1253 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001254 }
1255 fclose(fp);
1256
1257 return sl;
1258}
1259
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001260/* Show an event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001261static int show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001262{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001263 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001264 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001265 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001266
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001267 /* Synthesize only event probe point */
1268 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001269 if (!place)
1270 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001271
1272 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05001273 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001274 return ret;
1275
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001276 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001277
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001278 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001279 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001280 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001281 ret = synthesize_perf_probe_arg(&pev->args[i],
1282 buf, 128);
1283 if (ret < 0)
1284 break;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001285 printf(" %s", buf);
1286 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001287 }
1288 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001289 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001290 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05001291}
1292
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001293/* List up current perf-probe events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001294int show_perf_probe_events(void)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001295{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001296 int fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301297 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001298 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001299 struct strlist *rawlist;
1300 struct str_node *ent;
1301
Masami Hiramatsu72041332010-01-05 17:47:10 -05001302 setup_pager();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001303 ret = init_vmlinux();
1304 if (ret < 0)
1305 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001306
1307 memset(&tev, 0, sizeof(tev));
1308 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05001309
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001310 fd = open_kprobe_events(false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001311 if (fd < 0)
1312 return fd;
1313
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301314 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001315 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001316 if (!rawlist)
1317 return -ENOENT;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001318
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001319 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301320 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001321 if (ret >= 0) {
1322 ret = convert_to_perf_probe_event(&tev, &pev);
1323 if (ret >= 0)
1324 ret = show_perf_probe_event(&pev);
1325 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001326 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301327 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001328 if (ret < 0)
1329 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001330 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001331 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001332
1333 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001334}
1335
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001336/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301337static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001338{
Masami Hiramatsufa282442009-12-08 17:03:23 -05001339 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001340 struct strlist *sl, *rawlist;
1341 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301342 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001343 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001344
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001345 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301346 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001347 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001348 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301349 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001350 if (ret < 0)
1351 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001352 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001353 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1354 tev.event);
1355 if (ret >= 0)
1356 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001357 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001358 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301359 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001360 if (ret < 0)
1361 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001362 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001363 strlist__delete(rawlist);
1364
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001365 if (ret < 0) {
1366 strlist__delete(sl);
1367 return NULL;
1368 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001369 return sl;
1370}
1371
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301372static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001373{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02001374 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301375 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001376
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001377 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301378 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001379 return -EINVAL;
1380 }
1381
Masami Hiramatsufa282442009-12-08 17:03:23 -05001382 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001383 if (!probe_event_dry_run) {
1384 ret = write(fd, buf, strlen(buf));
1385 if (ret <= 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001386 pr_warning("Failed to write event: %s\n",
1387 strerror(errno));
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001388 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001389 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001390 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001391}
1392
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001393static int get_new_event_name(char *buf, size_t len, const char *base,
1394 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001395{
1396 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001397
1398 /* Try no suffix */
1399 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001400 if (ret < 0) {
1401 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1402 return ret;
1403 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001404 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001405 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001406
Masami Hiramatsud761b082009-12-15 10:32:25 -05001407 if (!allow_suffix) {
1408 pr_warning("Error: event \"%s\" already exists. "
1409 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001410 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05001411 }
1412
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001413 /* Try to add suffix */
1414 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001415 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001416 if (ret < 0) {
1417 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1418 return ret;
1419 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001420 if (!strlist__has_entry(namelist, buf))
1421 break;
1422 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001423 if (i == MAX_EVENT_INDEX) {
1424 pr_warning("Too many events are on the same function.\n");
1425 ret = -ERANGE;
1426 }
1427
1428 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001429}
1430
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301431static int __add_probe_trace_events(struct perf_probe_event *pev,
1432 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001433 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001434{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001435 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301436 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001437 char buf[64];
1438 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001439 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001440
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001441 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001442 if (fd < 0)
1443 return fd;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001444 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301445 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001446 if (!namelist) {
1447 pr_debug("Failed to get current event list.\n");
1448 return -EIO;
1449 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001450
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001451 ret = 0;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001452 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001453 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001454 tev = &tevs[i];
1455 if (pev->event)
1456 event = pev->event;
1457 else
1458 if (pev->point.function)
1459 event = pev->point.function;
1460 else
1461 event = tev->point.symbol;
1462 if (pev->group)
1463 group = pev->group;
1464 else
1465 group = PERFPROBE_GROUP;
1466
1467 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001468 ret = get_new_event_name(buf, 64, event,
1469 namelist, allow_suffix);
1470 if (ret < 0)
1471 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001472 event = buf;
1473
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001474 tev->event = strdup(event);
1475 tev->group = strdup(group);
1476 if (tev->event == NULL || tev->group == NULL) {
1477 ret = -ENOMEM;
1478 break;
1479 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301480 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001481 if (ret < 0)
1482 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001483 /* Add added event name to namelist */
1484 strlist__add(namelist, event);
1485
1486 /* Trick here - save current event/group */
1487 event = pev->event;
1488 group = pev->group;
1489 pev->event = tev->event;
1490 pev->group = tev->group;
1491 show_perf_probe_event(pev);
1492 /* Trick here - restore current event/group */
1493 pev->event = (char *)event;
1494 pev->group = (char *)group;
1495
1496 /*
1497 * Probes after the first probe which comes from same
1498 * user input are always allowed to add suffix, because
1499 * there might be several addresses corresponding to
1500 * one code line.
1501 */
1502 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001503 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001504
1505 if (ret >= 0) {
1506 /* Show how to use the event. */
1507 printf("\nYou can now use it on all perf tools, such as:\n\n");
1508 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1509 tev->event);
1510 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001511
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001512 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001513 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001514 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001515}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001516
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301517static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1518 struct probe_trace_event **tevs,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001519 int max_tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001520{
1521 struct symbol *sym;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001522 int ret = 0, i;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301523 struct probe_trace_event *tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001524
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001525 /* Convert perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301526 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001527 if (ret != 0)
1528 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001529
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001530 /* Allocate trace event buffer */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301531 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001532 if (tev == NULL)
1533 return -ENOMEM;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001534
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001535 /* Copy parameters */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001536 tev->point.symbol = strdup(pev->point.function);
1537 if (tev->point.symbol == NULL) {
1538 ret = -ENOMEM;
1539 goto error;
1540 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001541 tev->point.offset = pev->point.offset;
1542 tev->nargs = pev->nargs;
1543 if (tev->nargs) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301544 tev->args = zalloc(sizeof(struct probe_trace_arg)
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001545 * tev->nargs);
1546 if (tev->args == NULL) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001547 ret = -ENOMEM;
1548 goto error;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001549 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001550 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001551 if (pev->args[i].name) {
1552 tev->args[i].name = strdup(pev->args[i].name);
1553 if (tev->args[i].name == NULL) {
1554 ret = -ENOMEM;
1555 goto error;
1556 }
1557 }
1558 tev->args[i].value = strdup(pev->args[i].var);
1559 if (tev->args[i].value == NULL) {
1560 ret = -ENOMEM;
1561 goto error;
1562 }
1563 if (pev->args[i].type) {
1564 tev->args[i].type = strdup(pev->args[i].type);
1565 if (tev->args[i].type == NULL) {
1566 ret = -ENOMEM;
1567 goto error;
1568 }
1569 }
Masami Hiramatsu48481932010-04-12 13:16:53 -04001570 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001571 }
1572
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001573 /* Currently just checking function name from symbol map */
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001574 sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION],
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001575 tev->point.symbol, NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001576 if (!sym) {
1577 pr_warning("Kernel symbol \'%s\' not found.\n",
1578 tev->point.symbol);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001579 ret = -ENOENT;
1580 goto error;
1581 }
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001582
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001583 return 1;
1584error:
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301585 clear_probe_trace_event(tev);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001586 free(tev);
1587 *tevs = NULL;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001588 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001589}
1590
1591struct __event_package {
1592 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301593 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001594 int ntevs;
1595};
1596
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001597int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001598 bool force_add, int max_tevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001599{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001600 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001601 struct __event_package *pkgs;
1602
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001603 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1604 if (pkgs == NULL)
1605 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001606
1607 /* Init vmlinux path */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001608 ret = init_vmlinux();
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001609 if (ret < 0) {
1610 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001611 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001612 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001613
1614 /* Loop 1: convert all events */
1615 for (i = 0; i < npevs; i++) {
1616 pkgs[i].pev = &pevs[i];
1617 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301618 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsuef4a3562010-04-21 15:56:40 -04001619 &pkgs[i].tevs, max_tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001620 if (ret < 0)
1621 goto end;
1622 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001623 }
1624
1625 /* Loop 2: add all events */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001626 for (i = 0; i < npevs && ret >= 0; i++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301627 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001628 pkgs[i].ntevs, force_add);
1629end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001630 /* Loop 3: cleanup and free trace events */
1631 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001632 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301633 clear_probe_trace_event(&pkgs[i].tevs[j]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09001634 free(pkgs[i].tevs);
1635 }
1636 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001637
1638 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001639}
1640
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301641static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001642{
1643 char *p;
1644 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001645 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001646
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301647 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001648 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1649 if (ret < 0)
1650 goto error;
1651
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001652 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001653 if (!p) {
1654 pr_debug("Internal error: %s should have ':' but not.\n",
1655 ent->s);
1656 ret = -ENOTSUP;
1657 goto error;
1658 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001659 *p = '/';
1660
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001661 pr_debug("Writing event: %s\n", buf);
1662 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001663 if (ret < 0)
1664 goto error;
1665
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001666 printf("Remove event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001667 return 0;
1668error:
1669 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1670 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001671}
1672
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301673static int del_trace_probe_event(int fd, const char *group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001674 const char *event, struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001675{
1676 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001677 struct str_node *ent, *n;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001678 int found = 0, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001679
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001680 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1681 if (ret < 0) {
1682 pr_err("Failed to copy event.");
1683 return ret;
1684 }
Masami Hiramatsufa282442009-12-08 17:03:23 -05001685
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001686 if (strpbrk(buf, "*?")) { /* Glob-exp */
1687 strlist__for_each_safe(ent, n, namelist)
1688 if (strglobmatch(ent->s, buf)) {
1689 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301690 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001691 if (ret < 0)
1692 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001693 strlist__remove(namelist, ent);
1694 }
1695 } else {
1696 ent = strlist__find(namelist, buf);
1697 if (ent) {
1698 found++;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301699 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001700 if (ret >= 0)
1701 strlist__remove(namelist, ent);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001702 }
1703 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001704 if (found == 0 && ret >= 0)
1705 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1706
1707 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001708}
1709
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001710int del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001711{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001712 int fd, ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001713 const char *group, *event;
1714 char *p, *str;
1715 struct str_node *ent;
1716 struct strlist *namelist;
1717
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001718 fd = open_kprobe_events(true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001719 if (fd < 0)
1720 return fd;
1721
Masami Hiramatsufa282442009-12-08 17:03:23 -05001722 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301723 namelist = get_probe_trace_event_names(fd, true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001724 if (namelist == NULL)
1725 return -EINVAL;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001726
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001727 strlist__for_each(ent, dellist) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001728 str = strdup(ent->s);
1729 if (str == NULL) {
1730 ret = -ENOMEM;
1731 break;
1732 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001733 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001734 p = strchr(str, ':');
1735 if (p) {
1736 group = str;
1737 *p = '\0';
1738 event = p + 1;
1739 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001740 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001741 event = str;
1742 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001743 pr_debug("Group: %s, Event: %s\n", group, event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301744 ret = del_trace_probe_event(fd, group, event, namelist);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001745 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001746 if (ret < 0)
1747 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001748 }
1749 strlist__delete(namelist);
1750 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001751
1752 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001753}
1754