blob: 19de8b77973d0fbefeb8ad78dc4f9d0326aa8e86 [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
2 * probe-event.c : perf-probe definition to kprobe_events format converter
3 *
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 Hiramatsu4b4da7f2010-03-22 13:10:26 -030045#include "trace-event.h" /* For __unused */
46#include "parse-events.h" /* For debugfs_path */
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 Hiramatsu50656ee2009-11-30 19:19:58 -050056#define semantic_error(msg ...) die("Semantic error :" msg)
57
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);
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040075static struct map_groups kmap_groups;
76static struct map *kmaps[MAP__NR_TYPES];
77
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030078/* Initialize symbol maps and path of vmlinux */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040079static void init_vmlinux(void)
80{
81 symbol_conf.sort_by_name = true;
82 if (symbol_conf.vmlinux_name == NULL)
83 symbol_conf.try_vmlinux_path = true;
84 else
85 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
86 if (symbol__init() < 0)
87 die("Failed to init symbol map.");
88
89 map_groups__init(&kmap_groups);
90 if (map_groups__create_kernel_maps(&kmap_groups, kmaps) < 0)
91 die("Failed to create kernel maps.");
92}
93
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030094#ifdef DWARF_SUPPORT
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040095static int open_vmlinux(void)
96{
97 if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) {
98 pr_debug("Failed to load kernel map.\n");
99 return -EINVAL;
100 }
101 pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name);
102 return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
103}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300104
105static void convert_to_perf_probe_point(struct kprobe_trace_point *tp,
106 struct perf_probe_point *pp)
107{
108 struct symbol *sym;
109 int fd, ret = 0;
110
111 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
112 tp->symbol, NULL);
113 if (sym) {
114 fd = open_vmlinux();
115 ret = find_perf_probe_point(fd, sym->start + tp->offset, pp);
116 close(fd);
117 }
118 if (ret <= 0) {
119 pp->function = xstrdup(tp->symbol);
120 pp->offset = tp->offset;
121 }
122 pp->retprobe = tp->retprobe;
123}
124
125/* Try to find perf_probe_event with debuginfo */
126static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
127 struct kprobe_trace_event **tevs)
128{
129 bool need_dwarf = perf_probe_event_need_dwarf(pev);
130 int fd, ntevs;
131
132 fd = open_vmlinux();
133 if (fd < 0) {
134 if (need_dwarf)
135 die("Could not open debuginfo file.");
136
137 pr_debug("Could not open vmlinux. Try to use symbols.\n");
138 return 0;
139 }
140
141 /* Searching trace events corresponding to probe event */
142 ntevs = find_kprobe_trace_events(fd, pev, tevs);
143 close(fd);
144
145 if (ntevs > 0) /* Succeeded to find trace events */
146 return ntevs;
147
148 if (ntevs == 0) /* No error but failed to find probe point. */
149 die("Probe point '%s' not found. - probe not added.",
150 synthesize_perf_probe_point(&pev->point));
151
152 /* Error path */
153 if (need_dwarf) {
154 if (ntevs == -ENOENT)
155 pr_warning("No dwarf info found in the vmlinux - "
156 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
157 die("Could not analyze debuginfo.");
158 }
159 pr_debug("An error occurred in debuginfo analysis."
160 " Try to use symbols.\n");
161 return 0;
162
163}
164
165#define LINEBUF_SIZE 256
166#define NR_ADDITIONAL_LINES 2
167
168static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
169{
170 char buf[LINEBUF_SIZE];
171 const char *color = PERF_COLOR_BLUE;
172
173 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
174 goto error;
175 if (!skip) {
176 if (show_num)
177 fprintf(stdout, "%7u %s", l, buf);
178 else
179 color_fprintf(stdout, color, " %s", buf);
180 }
181
182 while (strlen(buf) == LINEBUF_SIZE - 1 &&
183 buf[LINEBUF_SIZE - 2] != '\n') {
184 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
185 goto error;
186 if (!skip) {
187 if (show_num)
188 fprintf(stdout, "%s", buf);
189 else
190 color_fprintf(stdout, color, "%s", buf);
191 }
192 }
193 return;
194error:
195 if (feof(fp))
196 die("Source file is shorter than expected.");
197 else
198 die("File read error: %s", strerror(errno));
199}
200
201/*
202 * Show line-range always requires debuginfo to find source file and
203 * line number.
204 */
205void show_line_range(struct line_range *lr)
206{
207 unsigned int l = 1;
208 struct line_node *ln;
209 FILE *fp;
210 int fd, ret;
211
212 /* Search a line range */
213 init_vmlinux();
214 fd = open_vmlinux();
215 if (fd < 0)
216 die("Could not open debuginfo file.");
217 ret = find_line_range(fd, lr);
218 if (ret <= 0)
219 die("Source line is not found.\n");
220 close(fd);
221
222 setup_pager();
223
224 if (lr->function)
225 fprintf(stdout, "<%s:%d>\n", lr->function,
226 lr->start - lr->offset);
227 else
228 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
229
230 fp = fopen(lr->path, "r");
231 if (fp == NULL)
232 die("Failed to open %s: %s", lr->path, strerror(errno));
233 /* Skip to starting line number */
234 while (l < lr->start)
235 show_one_line(fp, l++, true, false);
236
237 list_for_each_entry(ln, &lr->line_list, list) {
238 while (ln->line > l)
239 show_one_line(fp, (l++) - lr->offset, false, false);
240 show_one_line(fp, (l++) - lr->offset, false, true);
241 }
242
243 if (lr->end == INT_MAX)
244 lr->end = l + NR_ADDITIONAL_LINES;
245 while (l < lr->end && !feof(fp))
246 show_one_line(fp, (l++) - lr->offset, false, false);
247
248 fclose(fp);
249}
250
251#else /* !DWARF_SUPPORT */
252
253static void convert_to_perf_probe_point(struct kprobe_trace_point *tp,
254 struct perf_probe_point *pp)
255{
256 pp->function = xstrdup(tp->symbol);
257 pp->offset = tp->offset;
258 pp->retprobe = tp->retprobe;
259}
260
261static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
262 struct kprobe_trace_event **tevs __unused)
263{
264 if (perf_probe_event_need_dwarf(pev))
265 die("Debuginfo-analysis is not supported");
266 return 0;
267}
268
269void show_line_range(struct line_range *lr __unused)
270{
271 die("Debuginfo-analysis is not supported");
272}
273
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400274#endif
275
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500276void parse_line_range_desc(const char *arg, struct line_range *lr)
277{
278 const char *ptr;
279 char *tmp;
280 /*
281 * <Syntax>
282 * SRC:SLN[+NUM|-ELN]
283 * FUNC[:SLN[+NUM|-ELN]]
284 */
285 ptr = strchr(arg, ':');
286 if (ptr) {
287 lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0);
288 if (*tmp == '+')
289 lr->end = lr->start + (unsigned int)strtoul(tmp + 1,
290 &tmp, 0);
291 else if (*tmp == '-')
292 lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0);
293 else
294 lr->end = 0;
295 pr_debug("Line range is %u to %u\n", lr->start, lr->end);
296 if (lr->end && lr->start > lr->end)
297 semantic_error("Start line must be smaller"
298 " than end line.");
299 if (*tmp != '\0')
300 semantic_error("Tailing with invalid character '%d'.",
301 *tmp);
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400302 tmp = xstrndup(arg, (ptr - arg));
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500303 } else
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400304 tmp = xstrdup(arg);
Masami Hiramatsu631c9de2010-01-06 09:45:34 -0500305
306 if (strchr(tmp, '.'))
307 lr->file = tmp;
308 else
309 lr->function = tmp;
310}
311
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500312/* Check the name is good for event/group */
313static bool check_event_name(const char *name)
314{
315 if (!isalpha(*name) && *name != '_')
316 return false;
317 while (*++name != '\0') {
318 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
319 return false;
320 }
321 return true;
322}
323
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500324/* Parse probepoint definition. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400325static void parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500326{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400327 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500328 char *ptr, *tmp;
329 char c, nc = 0;
330 /*
331 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500332 * perf probe [EVENT=]SRC[:LN|;PTN]
333 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500334 *
335 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500336 */
337
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500338 ptr = strpbrk(arg, ";=@+%");
339 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500340 *ptr = '\0';
341 tmp = ptr + 1;
342 ptr = strchr(arg, ':');
343 if (ptr) /* Group name is not supported yet. */
344 semantic_error("Group name is not supported yet.");
Masami Hiramatsub7702a22009-12-16 17:24:15 -0500345 if (!check_event_name(arg))
346 semantic_error("%s is bad for event name -it must "
347 "follow C symbol-naming rule.", arg);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400348 pev->event = xstrdup(arg);
349 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -0500350 arg = tmp;
351 }
352
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500353 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500354 if (ptr) {
355 nc = *ptr;
356 *ptr++ = '\0';
357 }
358
359 /* Check arg is function or file and copy it */
360 if (strchr(arg, '.')) /* File */
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400361 pp->file = xstrdup(arg);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500362 else /* Function */
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400363 pp->function = xstrdup(arg);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500364
365 /* Parse other options */
366 while (ptr) {
367 arg = ptr;
368 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500369 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400370 pp->lazy_line = xstrdup(arg);
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500371 break;
372 }
373 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500374 if (ptr) {
375 nc = *ptr;
376 *ptr++ = '\0';
377 }
378 switch (c) {
379 case ':': /* Line number */
380 pp->line = strtoul(arg, &tmp, 0);
381 if (*tmp != '\0')
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500382 semantic_error("There is non-digit char"
383 " in line number.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500384 break;
385 case '+': /* Byte offset from a symbol */
386 pp->offset = strtoul(arg, &tmp, 0);
387 if (*tmp != '\0')
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500388 semantic_error("There is non-digit character"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500389 " in offset.");
390 break;
391 case '@': /* File name */
392 if (pp->file)
393 semantic_error("SRC@SRC is not allowed.");
Masami Hiramatsu31facc52010-03-16 18:05:30 -0400394 pp->file = xstrdup(arg);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500395 break;
396 case '%': /* Probe places */
397 if (strcmp(arg, "return") == 0) {
398 pp->retprobe = 1;
399 } else /* Others not supported yet */
400 semantic_error("%%%s is not supported.", arg);
401 break;
402 default:
403 DIE_IF("Program has a bug.");
404 break;
405 }
406 }
407
408 /* Exclusion check */
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500409 if (pp->lazy_line && pp->line)
410 semantic_error("Lazy pattern can't be used with line number.");
411
412 if (pp->lazy_line && pp->offset)
413 semantic_error("Lazy pattern can't be used with offset.");
414
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500415 if (pp->line && pp->offset)
416 semantic_error("Offset can't be used with line number.");
417
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500418 if (!pp->line && !pp->lazy_line && pp->file && !pp->function)
419 semantic_error("File always requires line number or "
420 "lazy pattern.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500421
422 if (pp->offset && !pp->function)
423 semantic_error("Offset requires an entry function.");
424
425 if (pp->retprobe && !pp->function)
426 semantic_error("Return probe requires an entry function.");
427
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500428 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe)
429 semantic_error("Offset/Line/Lazy pattern can't be used with "
430 "return probe.");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500431
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400432 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500433 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
434 pp->lazy_line);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500435}
436
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400437/* Parse perf-probe event argument */
438static void parse_perf_probe_arg(const char *str, struct perf_probe_arg *arg)
439{
Masami Hiramatsu48481932010-04-12 13:16:53 -0400440 char *tmp;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400441 struct perf_probe_arg_field **fieldp;
442
443 pr_debug("parsing arg: %s into ", str);
444
Masami Hiramatsu48481932010-04-12 13:16:53 -0400445 tmp = strchr(str, '=');
446 if (tmp) {
447 arg->name = xstrndup(str, tmp - str);
448 str = tmp + 1;
449 }
450
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400451 tmp = strpbrk(str, "-.");
452 if (!is_c_varname(str) || !tmp) {
453 /* A variable, register, symbol or special value */
Masami Hiramatsu48481932010-04-12 13:16:53 -0400454 arg->var = xstrdup(str);
455 pr_debug("%s\n", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400456 return;
457 }
458
459 /* Structure fields */
Masami Hiramatsu48481932010-04-12 13:16:53 -0400460 arg->var = xstrndup(str, tmp - str);
461 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400462 fieldp = &arg->field;
463
464 do {
465 *fieldp = xzalloc(sizeof(struct perf_probe_arg_field));
466 if (*tmp == '.') {
467 str = tmp + 1;
468 (*fieldp)->ref = false;
469 } else if (tmp[1] == '>') {
470 str = tmp + 2;
471 (*fieldp)->ref = true;
472 } else
473 semantic_error("Argument parse error: %s", str);
474
475 tmp = strpbrk(str, "-.");
476 if (tmp) {
477 (*fieldp)->name = xstrndup(str, tmp - str);
478 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
479 fieldp = &(*fieldp)->next;
480 }
481 } while (tmp);
482 (*fieldp)->name = xstrdup(str);
483 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -0400484
485 /* If no name is specified, set the last field name */
486 if (!arg->name)
487 arg->name = xstrdup((*fieldp)->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400488}
489
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400490/* Parse perf-probe event command */
491void parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500492{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500493 char **argv;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -0500494 int argc, i;
495
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400496 argv = argv_split(cmd, &argc);
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500497 if (!argv)
498 die("argv_split failed.");
499 if (argc > MAX_PROBE_ARGS + 1)
500 semantic_error("Too many arguments");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500501
502 /* Parse probe point */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400503 parse_perf_probe_point(argv[0], pev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500504
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500505 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400506 pev->nargs = argc - 1;
507 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
508 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400509 parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400510 if (is_c_varname(pev->args[i].var) && pev->point.retprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400511 semantic_error("You can't specify local variable for"
512 " kretprobe");
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500513 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500514
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500515 argv_free(argv);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500516}
517
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400518/* Return true if this perf_probe_event requires debuginfo */
519bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500520{
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400521 int i;
522
523 if (pev->point.file || pev->point.line || pev->point.lazy_line)
524 return true;
525
526 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -0400527 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400528 return true;
529
530 return false;
531}
532
533/* Parse kprobe_events event into struct probe_point */
534void parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
535{
536 struct kprobe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500537 char pr;
538 char *p;
539 int ret, i, argc;
540 char **argv;
541
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400542 pr_debug("Parsing kprobe_events: %s\n", cmd);
543 argv = argv_split(cmd, &argc);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500544 if (!argv)
545 die("argv_split failed.");
546 if (argc < 2)
547 semantic_error("Too less arguments.");
548
549 /* Scan event and group name. */
Liming Wang93aaa452009-12-02 16:42:54 +0800550 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400551 &pr, (float *)(void *)&tev->group,
552 (float *)(void *)&tev->event);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500553 if (ret != 3)
554 semantic_error("Failed to parse event name: %s", argv[0]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400555 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500556
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400557 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500558
559 /* Scan function name and offset */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400560 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
561 &tp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500562 if (ret == 1)
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400563 tp->offset = 0;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500564
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400565 tev->nargs = argc - 2;
566 tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
567 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500568 p = strchr(argv[i + 2], '=');
569 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400570 *p++ = '\0';
571 else
572 p = argv[i + 2];
573 tev->args[i].name = xstrdup(argv[i + 2]);
574 /* TODO: parse regs and offset */
575 tev->args[i].value = xstrdup(p);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500576 }
577
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500578 argv_free(argv);
579}
580
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400581/* Compose only probe arg */
582int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
583{
584 struct perf_probe_arg_field *field = pa->field;
585 int ret;
586 char *tmp = buf;
587
Masami Hiramatsu48481932010-04-12 13:16:53 -0400588 if (pa->name && pa->var)
589 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
590 else
591 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400592 if (ret <= 0)
593 goto error;
594 tmp += ret;
595 len -= ret;
596
597 while (field) {
598 ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
599 field->name);
600 if (ret <= 0)
601 goto error;
602 tmp += ret;
603 len -= ret;
604 field = field->next;
605 }
606 return tmp - buf;
607error:
608 die("Failed to synthesize perf probe argument: %s", strerror(-ret));
609}
610
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400611/* Compose only probe point (not argument) */
612static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500613{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400614 char *buf, *tmp;
615 char offs[32] = "", line[32] = "", file[32] = "";
616 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -0500617
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400618 buf = xzalloc(MAX_CMDLEN);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500619 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400620 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500621 if (ret <= 0)
622 goto error;
623 }
624 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400625 ret = e_snprintf(line, 32, ":%d", pp->line);
626 if (ret <= 0)
627 goto error;
628 }
629 if (pp->file) {
630 len = strlen(pp->file) - 32;
631 if (len < 0)
632 len = 0;
633 tmp = strchr(pp->file + len, '/');
634 if (!tmp)
635 tmp = pp->file + len - 1;
636 ret = e_snprintf(file, 32, "@%s", tmp + 1);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500637 if (ret <= 0)
638 goto error;
639 }
640
641 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400642 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
643 offs, pp->retprobe ? "%return" : "", line,
644 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500645 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400646 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500647 if (ret <= 0)
648 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500649
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400650 return buf;
651error:
652 die("Failed to synthesize perf probe point: %s", strerror(-ret));
653}
654
655#if 0
656char *synthesize_perf_probe_command(struct perf_probe_event *pev)
657{
658 char *buf;
659 int i, len, ret;
660
661 buf = synthesize_perf_probe_point(&pev->point);
662 if (!buf)
663 return NULL;
664
665 len = strlen(buf);
666 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500667 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400668 pev->args[i].name);
669 if (ret <= 0) {
670 free(buf);
671 return NULL;
672 }
673 len += ret;
674 }
675
676 return buf;
677}
678#endif
679
680static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
681 char **buf, size_t *buflen,
682 int depth)
683{
684 int ret;
685 if (ref->next) {
686 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
687 buflen, depth + 1);
688 if (depth < 0)
689 goto out;
690 }
691
692 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
693 if (ret < 0)
694 depth = ret;
695 else {
696 *buf += ret;
697 *buflen -= ret;
698 }
699out:
700 return depth;
701
702}
703
704static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
705 char *buf, size_t buflen)
706{
707 int ret, depth = 0;
708 char *tmp = buf;
709
710 /* Argument name or separator */
711 if (arg->name)
712 ret = e_snprintf(buf, buflen, " %s=", arg->name);
713 else
714 ret = e_snprintf(buf, buflen, " ");
715 if (ret < 0)
716 return ret;
717 buf += ret;
718 buflen -= ret;
719
720 /* Dereferencing arguments */
721 if (arg->ref) {
722 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
723 &buflen, 1);
724 if (depth < 0)
725 return depth;
726 }
727
728 /* Print argument value */
729 ret = e_snprintf(buf, buflen, "%s", arg->value);
730 if (ret < 0)
731 return ret;
732 buf += ret;
733 buflen -= ret;
734
735 /* Closing */
736 while (depth--) {
737 ret = e_snprintf(buf, buflen, ")");
738 if (ret < 0)
739 return ret;
740 buf += ret;
741 buflen -= ret;
742 }
743
744 return buf - tmp;
745}
746
747char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
748{
749 struct kprobe_trace_point *tp = &tev->point;
750 char *buf;
751 int i, len, ret;
752
753 buf = xzalloc(MAX_CMDLEN);
754 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
755 tp->retprobe ? 'r' : 'p',
756 tev->group, tev->event,
757 tp->symbol, tp->offset);
758 if (len <= 0)
759 goto error;
760
761 for (i = 0; i < tev->nargs; i++) {
762 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
763 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500764 if (ret <= 0)
765 goto error;
766 len += ret;
767 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500768
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400769 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500770error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400771 free(buf);
772 return NULL;
773}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500774
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400775void convert_to_perf_probe_event(struct kprobe_trace_event *tev,
776 struct perf_probe_event *pev)
777{
778 char buf[64];
779 int i;
780
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300781 /* Convert event/group name */
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400782 pev->event = xstrdup(tev->event);
783 pev->group = xstrdup(tev->group);
784
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300785 /* Convert trace_point to probe_point */
786 convert_to_perf_probe_point(&tev->point, &pev->point);
787
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400788 /* Convert trace_arg to probe_arg */
789 pev->nargs = tev->nargs;
790 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
791 for (i = 0; i < tev->nargs; i++)
792 if (tev->args[i].name)
793 pev->args[i].name = xstrdup(tev->args[i].name);
794 else {
795 synthesize_kprobe_trace_arg(&tev->args[i], buf, 64);
796 pev->args[i].name = xstrdup(buf);
797 }
798}
799
800void clear_perf_probe_event(struct perf_probe_event *pev)
801{
802 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400803 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400804 int i;
805
806 if (pev->event)
807 free(pev->event);
808 if (pev->group)
809 free(pev->group);
810 if (pp->file)
811 free(pp->file);
812 if (pp->function)
813 free(pp->function);
814 if (pp->lazy_line)
815 free(pp->lazy_line);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400816 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400817 if (pev->args[i].name)
818 free(pev->args[i].name);
Masami Hiramatsu48481932010-04-12 13:16:53 -0400819 if (pev->args[i].var)
820 free(pev->args[i].var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400821 field = pev->args[i].field;
822 while (field) {
823 next = field->next;
824 if (field->name)
825 free(field->name);
826 free(field);
827 field = next;
828 }
829 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400830 if (pev->args)
831 free(pev->args);
832 memset(pev, 0, sizeof(*pev));
833}
834
835void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
836{
837 struct kprobe_trace_arg_ref *ref, *next;
838 int i;
839
840 if (tev->event)
841 free(tev->event);
842 if (tev->group)
843 free(tev->group);
844 if (tev->point.symbol)
845 free(tev->point.symbol);
846 for (i = 0; i < tev->nargs; i++) {
847 if (tev->args[i].name)
848 free(tev->args[i].name);
849 if (tev->args[i].value)
850 free(tev->args[i].value);
851 ref = tev->args[i].ref;
852 while (ref) {
853 next = ref->next;
854 free(ref);
855 ref = next;
856 }
857 }
858 if (tev->args)
859 free(tev->args);
860 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500861}
862
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -0400863static int open_kprobe_events(bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500864{
865 char buf[PATH_MAX];
866 int ret;
867
868 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
869 if (ret < 0)
870 die("Failed to make kprobe_events path.");
871
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -0400872 if (readwrite && !probe_event_dry_run)
873 ret = open(buf, O_RDWR, O_APPEND);
874 else
875 ret = open(buf, O_RDONLY, 0);
876
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500877 if (ret < 0) {
878 if (errno == ENOENT)
879 die("kprobe_events file does not exist -"
Liming Wang63bbd5e2009-12-29 16:37:09 +0800880 " please rebuild with CONFIG_KPROBE_EVENT.");
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500881 else
882 die("Could not open kprobe_events file: %s",
883 strerror(errno));
884 }
885 return ret;
886}
887
888/* Get raw string list of current kprobe_events */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400889static struct strlist *get_kprobe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500890{
891 int ret, idx;
892 FILE *fp;
893 char buf[MAX_CMDLEN];
894 char *p;
895 struct strlist *sl;
896
897 sl = strlist__new(true, NULL);
898
899 fp = fdopen(dup(fd), "r");
900 while (!feof(fp)) {
901 p = fgets(buf, MAX_CMDLEN, fp);
902 if (!p)
903 break;
904
905 idx = strlen(p) - 1;
906 if (p[idx] == '\n')
907 p[idx] = '\0';
908 ret = strlist__add(sl, buf);
909 if (ret < 0)
910 die("strlist__add failed: %s", strerror(-ret));
911 }
912 fclose(fp);
913
914 return sl;
915}
916
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500917/* Show an event */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400918static void show_perf_probe_event(struct perf_probe_event *pev)
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500919{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500920 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500921 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400922 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500923
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400924 /* Synthesize only event probe point */
925 place = synthesize_perf_probe_point(&pev->point);
926
927 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -0500928 if (ret < 0)
929 die("Failed to copy event: %s", strerror(-ret));
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400930 printf(" %-20s (on %s", buf, place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500931
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400932 if (pev->nargs > 0) {
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500933 printf(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -0400934 for (i = 0; i < pev->nargs; i++) {
935 synthesize_perf_probe_arg(&pev->args[i], buf, 128);
936 printf(" %s", buf);
937 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500938 }
939 printf(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400940 free(place);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500941}
942
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500943/* List up current perf-probe events */
944void show_perf_probe_events(void)
945{
Masami Hiramatsu7ef17aa2009-12-15 10:32:47 -0500946 int fd;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400947 struct kprobe_trace_event tev;
948 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500949 struct strlist *rawlist;
950 struct str_node *ent;
951
Masami Hiramatsu72041332010-01-05 17:47:10 -0500952 setup_pager();
Masami Hiramatsufb1587d2010-03-16 18:06:19 -0400953 init_vmlinux();
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400954
955 memset(&tev, 0, sizeof(tev));
956 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -0500957
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -0400958 fd = open_kprobe_events(false);
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400959 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500960 close(fd);
961
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500962 strlist__for_each(ent, rawlist) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400963 parse_kprobe_trace_command(ent->s, &tev);
964 convert_to_perf_probe_event(&tev, &pev);
Masami Hiramatsu278498d2009-12-08 17:02:40 -0500965 /* Show an event */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400966 show_perf_probe_event(&pev);
967 clear_perf_probe_event(&pev);
968 clear_kprobe_trace_event(&tev);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -0500969 }
970
971 strlist__delete(rawlist);
972}
973
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500974/* Get current perf-probe event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400975static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500976{
Masami Hiramatsufa282442009-12-08 17:03:23 -0500977 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500978 struct strlist *sl, *rawlist;
979 struct str_node *ent;
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400980 struct kprobe_trace_event tev;
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500981
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400982 memset(&tev, 0, sizeof(tev));
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500983
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400984 rawlist = get_kprobe_trace_command_rawlist(fd);
Masami Hiramatsue1d20172009-12-07 12:00:46 -0500985 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -0500986 strlist__for_each(ent, rawlist) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400987 parse_kprobe_trace_command(ent->s, &tev);
Masami Hiramatsufa282442009-12-08 17:03:23 -0500988 if (include_group) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400989 if (e_snprintf(buf, 128, "%s:%s", tev.group,
990 tev.event) < 0)
Masami Hiramatsufa282442009-12-08 17:03:23 -0500991 die("Failed to copy group:event name.");
992 strlist__add(sl, buf);
993 } else
Masami Hiramatsu4235b042010-03-16 18:06:12 -0400994 strlist__add(sl, tev.event);
995 clear_kprobe_trace_event(&tev);
Masami Hiramatsub498ce12009-11-30 19:20:25 -0500996 }
997
998 strlist__delete(rawlist);
999
1000 return sl;
1001}
1002
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001003static void write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001004{
1005 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001006 char *buf = synthesize_kprobe_trace_command(tev);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001007
Masami Hiramatsufa282442009-12-08 17:03:23 -05001008 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001009 if (!probe_event_dry_run) {
1010 ret = write(fd, buf, strlen(buf));
1011 if (ret <= 0)
1012 die("Failed to write event: %s", strerror(errno));
1013 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001014 free(buf);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001015}
1016
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001017static void get_new_event_name(char *buf, size_t len, const char *base,
Masami Hiramatsud761b082009-12-15 10:32:25 -05001018 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001019{
1020 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001021
1022 /* Try no suffix */
1023 ret = e_snprintf(buf, len, "%s", base);
1024 if (ret < 0)
1025 die("snprintf() failed: %s", strerror(-ret));
1026 if (!strlist__has_entry(namelist, buf))
1027 return;
1028
Masami Hiramatsud761b082009-12-15 10:32:25 -05001029 if (!allow_suffix) {
1030 pr_warning("Error: event \"%s\" already exists. "
1031 "(Use -f to force duplicates.)\n", base);
1032 die("Can't add new event.");
1033 }
1034
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05001035 /* Try to add suffix */
1036 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001037 ret = e_snprintf(buf, len, "%s_%d", base, i);
1038 if (ret < 0)
1039 die("snprintf() failed: %s", strerror(-ret));
1040 if (!strlist__has_entry(namelist, buf))
1041 break;
1042 }
1043 if (i == MAX_EVENT_INDEX)
1044 die("Too many events are on the same function.");
1045}
1046
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001047static void __add_kprobe_trace_events(struct perf_probe_event *pev,
1048 struct kprobe_trace_event *tevs,
1049 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001050{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001051 int i, fd;
Ingo Molnar55632772010-03-18 16:51:16 +01001052 struct kprobe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001053 char buf[64];
1054 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001055 struct strlist *namelist;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001056
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001057 fd = open_kprobe_events(true);
Masami Hiramatsub498ce12009-11-30 19:20:25 -05001058 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001059 namelist = get_kprobe_trace_event_names(fd, false);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001060
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001061 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
1062 for (i = 0; i < ntevs; i++) {
1063 tev = &tevs[i];
1064 if (pev->event)
1065 event = pev->event;
1066 else
1067 if (pev->point.function)
1068 event = pev->point.function;
1069 else
1070 event = tev->point.symbol;
1071 if (pev->group)
1072 group = pev->group;
1073 else
1074 group = PERFPROBE_GROUP;
1075
1076 /* Get an unused new event name */
1077 get_new_event_name(buf, 64, event, namelist, allow_suffix);
1078 event = buf;
1079
1080 tev->event = xstrdup(event);
1081 tev->group = xstrdup(group);
1082 write_kprobe_trace_event(fd, tev);
1083 /* Add added event name to namelist */
1084 strlist__add(namelist, event);
1085
1086 /* Trick here - save current event/group */
1087 event = pev->event;
1088 group = pev->group;
1089 pev->event = tev->event;
1090 pev->group = tev->group;
1091 show_perf_probe_event(pev);
1092 /* Trick here - restore current event/group */
1093 pev->event = (char *)event;
1094 pev->group = (char *)group;
1095
1096 /*
1097 * Probes after the first probe which comes from same
1098 * user input are always allowed to add suffix, because
1099 * there might be several addresses corresponding to
1100 * one code line.
1101 */
1102 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001103 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001104 /* Show how to use the event. */
1105 printf("\nYou can now use it on all perf tools, such as:\n\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001106 printf("\tperf record -e %s:%s -a sleep 1\n\n", tev->group, tev->event);
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05001107
Masami Hiramatsue1d20172009-12-07 12:00:46 -05001108 strlist__delete(namelist);
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001109 close(fd);
1110}
Masami Hiramatsufa282442009-12-08 17:03:23 -05001111
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001112static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
1113 struct kprobe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001114{
1115 struct symbol *sym;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001116 int ntevs = 0, i;
1117 struct kprobe_trace_event *tev;
1118
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001119 /* Convert perf_probe_event with debuginfo */
1120 ntevs = try_to_find_kprobe_trace_events(pev, tevs);
1121 if (ntevs > 0)
1122 return ntevs;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001123
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001124 /* Allocate trace event buffer */
1125 ntevs = 1;
1126 tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event));
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001127
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001128 /* Copy parameters */
1129 tev->point.symbol = xstrdup(pev->point.function);
1130 tev->point.offset = pev->point.offset;
1131 tev->nargs = pev->nargs;
1132 if (tev->nargs) {
1133 tev->args = xzalloc(sizeof(struct kprobe_trace_arg)
1134 * tev->nargs);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001135 for (i = 0; i < tev->nargs; i++) {
1136 if (pev->args[i].name)
1137 tev->args[i].name = xstrdup(pev->args[i].name);
1138 tev->args[i].value = xstrdup(pev->args[i].var);
1139 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001140 }
1141
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001142 /* Currently just checking function name from symbol map */
1143 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
1144 tev->point.symbol, NULL);
1145 if (!sym)
1146 die("Kernel symbol \'%s\' not found - probe not added.",
1147 tev->point.symbol);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001148
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001149 return ntevs;
1150}
1151
1152struct __event_package {
1153 struct perf_probe_event *pev;
1154 struct kprobe_trace_event *tevs;
1155 int ntevs;
1156};
1157
1158void add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1159 bool force_add)
1160{
1161 int i;
1162 struct __event_package *pkgs;
1163
1164 pkgs = xzalloc(sizeof(struct __event_package) * npevs);
1165
1166 /* Init vmlinux path */
1167 init_vmlinux();
1168
1169 /* Loop 1: convert all events */
1170 for (i = 0; i < npevs; i++) {
1171 pkgs[i].pev = &pevs[i];
1172 /* Convert with or without debuginfo */
1173 pkgs[i].ntevs = convert_to_kprobe_trace_events(pkgs[i].pev,
1174 &pkgs[i].tevs);
1175 }
1176
1177 /* Loop 2: add all events */
1178 for (i = 0; i < npevs; i++)
1179 __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1180 pkgs[i].ntevs, force_add);
1181 /* TODO: cleanup all trace events? */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001182}
1183
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001184static void __del_trace_kprobe_event(int fd, struct str_node *ent)
1185{
1186 char *p;
1187 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001188 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001189
1190 /* Convert from perf-probe event to trace-kprobe event */
1191 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
1192 die("Failed to copy event.");
1193 p = strchr(buf + 2, ':');
1194 if (!p)
1195 die("Internal error: %s should have ':' but not.", ent->s);
1196 *p = '/';
1197
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001198 pr_debug("Writing event: %s\n", buf);
1199 ret = write(fd, buf, strlen(buf));
1200 if (ret <= 0)
1201 die("Failed to write event: %s", strerror(errno));
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001202 printf("Remove event: %s\n", ent->s);
1203}
1204
Masami Hiramatsufa282442009-12-08 17:03:23 -05001205static void del_trace_kprobe_event(int fd, const char *group,
1206 const char *event, struct strlist *namelist)
1207{
1208 char buf[128];
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001209 struct str_node *ent, *n;
1210 int found = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001211
1212 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
1213 die("Failed to copy event.");
Masami Hiramatsufa282442009-12-08 17:03:23 -05001214
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001215 if (strpbrk(buf, "*?")) { /* Glob-exp */
1216 strlist__for_each_safe(ent, n, namelist)
1217 if (strglobmatch(ent->s, buf)) {
1218 found++;
1219 __del_trace_kprobe_event(fd, ent);
1220 strlist__remove(namelist, ent);
1221 }
1222 } else {
1223 ent = strlist__find(namelist, buf);
1224 if (ent) {
1225 found++;
1226 __del_trace_kprobe_event(fd, ent);
1227 strlist__remove(namelist, ent);
1228 }
1229 }
1230 if (found == 0)
1231 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001232}
1233
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001234void del_perf_probe_events(struct strlist *dellist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05001235{
1236 int fd;
Masami Hiramatsufa282442009-12-08 17:03:23 -05001237 const char *group, *event;
1238 char *p, *str;
1239 struct str_node *ent;
1240 struct strlist *namelist;
1241
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001242 fd = open_kprobe_events(true);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001243 /* Get current event names */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001244 namelist = get_kprobe_trace_event_names(fd, true);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001245
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05001246 strlist__for_each(ent, dellist) {
Masami Hiramatsu31facc52010-03-16 18:05:30 -04001247 str = xstrdup(ent->s);
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001248 pr_debug("Parsing: %s\n", str);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001249 p = strchr(str, ':');
1250 if (p) {
1251 group = str;
1252 *p = '\0';
1253 event = p + 1;
1254 } else {
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001255 group = "*";
Masami Hiramatsufa282442009-12-08 17:03:23 -05001256 event = str;
1257 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05001258 pr_debug("Group: %s, Event: %s\n", group, event);
Masami Hiramatsufa282442009-12-08 17:03:23 -05001259 del_trace_kprobe_event(fd, group, event, namelist);
1260 free(str);
1261 }
1262 strlist__delete(namelist);
1263 close(fd);
1264}
1265