]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/perf/util/probe-event.c
perf probe: Support argument name
[mirror_ubuntu-bionic-kernel.git] / tools / perf / util / probe-event.c
CommitLineData
50656eec
MH
1/*
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>
4de189fe
MH
32#include <stdarg.h>
33#include <limits.h>
50656eec
MH
34
35#undef _GNU_SOURCE
31facc5f 36#include "util.h"
50656eec 37#include "event.h"
e1c01d61 38#include "string.h"
4de189fe 39#include "strlist.h"
50656eec 40#include "debug.h"
72041334 41#include "cache.h"
631c9def 42#include "color.h"
e0faa8d3
MH
43#include "symbol.h"
44#include "thread.h"
4b4da7f7
MH
45#include "trace-event.h" /* For __unused */
46#include "parse-events.h" /* For debugfs_path */
50656eec 47#include "probe-event.h"
4235b045 48#include "probe-finder.h"
50656eec
MH
49
50#define MAX_CMDLEN 256
51#define MAX_PROBE_ARGS 128
52#define PERFPROBE_GROUP "probe"
53
f4d7da49
MH
54bool probe_event_dry_run; /* Dry run flag */
55
50656eec
MH
56#define semantic_error(msg ...) die("Semantic error :" msg)
57
4de189fe 58/* If there is no space to write, returns -E2BIG. */
84988450
MH
59static int e_snprintf(char *str, size_t size, const char *format, ...)
60 __attribute__((format(printf, 3, 4)));
61
4de189fe
MH
62static int e_snprintf(char *str, size_t size, const char *format, ...)
63{
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
4b4da7f7 74static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
e0faa8d3
MH
75static struct map_groups kmap_groups;
76static struct map *kmaps[MAP__NR_TYPES];
77
4b4da7f7 78/* Initialize symbol maps and path of vmlinux */
e0faa8d3
MH
79static 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
4b4da7f7 94#ifdef DWARF_SUPPORT
e0faa8d3
MH
95static 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}
4b4da7f7
MH
104
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
e0faa8d3
MH
274#endif
275
631c9def
MH
276void 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);
31facc5f 302 tmp = xstrndup(arg, (ptr - arg));
631c9def 303 } else
31facc5f 304 tmp = xstrdup(arg);
631c9def
MH
305
306 if (strchr(tmp, '.'))
307 lr->file = tmp;
308 else
309 lr->function = tmp;
310}
311
b7702a21
MH
312/* 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
50656eec 324/* Parse probepoint definition. */
4235b045 325static void parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
50656eec 326{
4235b045 327 struct perf_probe_point *pp = &pev->point;
50656eec
MH
328 char *ptr, *tmp;
329 char c, nc = 0;
330 /*
331 * <Syntax>
2a9c8c36
MH
332 * perf probe [EVENT=]SRC[:LN|;PTN]
333 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
af663d75
MH
334 *
335 * TODO:Group name support
50656eec
MH
336 */
337
2a9c8c36
MH
338 ptr = strpbrk(arg, ";=@+%");
339 if (ptr && *ptr == '=') { /* Event name */
af663d75
MH
340 *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.");
b7702a21
MH
345 if (!check_event_name(arg))
346 semantic_error("%s is bad for event name -it must "
347 "follow C symbol-naming rule.", arg);
4235b045
MH
348 pev->event = xstrdup(arg);
349 pev->group = NULL;
af663d75
MH
350 arg = tmp;
351 }
352
2a9c8c36 353 ptr = strpbrk(arg, ";:+@%");
50656eec
MH
354 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 */
31facc5f 361 pp->file = xstrdup(arg);
50656eec 362 else /* Function */
31facc5f 363 pp->function = xstrdup(arg);
50656eec
MH
364
365 /* Parse other options */
366 while (ptr) {
367 arg = ptr;
368 c = nc;
2a9c8c36 369 if (c == ';') { /* Lazy pattern must be the last part */
31facc5f 370 pp->lazy_line = xstrdup(arg);
2a9c8c36
MH
371 break;
372 }
373 ptr = strpbrk(arg, ";:+@%");
50656eec
MH
374 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')
2a9c8c36
MH
382 semantic_error("There is non-digit char"
383 " in line number.");
50656eec
MH
384 break;
385 case '+': /* Byte offset from a symbol */
386 pp->offset = strtoul(arg, &tmp, 0);
387 if (*tmp != '\0')
2a9c8c36 388 semantic_error("There is non-digit character"
50656eec
MH
389 " in offset.");
390 break;
391 case '@': /* File name */
392 if (pp->file)
393 semantic_error("SRC@SRC is not allowed.");
31facc5f 394 pp->file = xstrdup(arg);
50656eec
MH
395 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 */
2a9c8c36
MH
409 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
50656eec
MH
415 if (pp->line && pp->offset)
416 semantic_error("Offset can't be used with line number.");
417
2a9c8c36
MH
418 if (!pp->line && !pp->lazy_line && pp->file && !pp->function)
419 semantic_error("File always requires line number or "
420 "lazy pattern.");
50656eec
MH
421
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
2a9c8c36
MH
428 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.");
50656eec 431
4235b045 432 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
2a9c8c36
MH
433 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
434 pp->lazy_line);
50656eec
MH
435}
436
7df2f329
MH
437/* Parse perf-probe event argument */
438static void parse_perf_probe_arg(const char *str, struct perf_probe_arg *arg)
439{
48481938 440 char *tmp;
7df2f329
MH
441 struct perf_probe_arg_field **fieldp;
442
443 pr_debug("parsing arg: %s into ", str);
444
48481938
MH
445 tmp = strchr(str, '=');
446 if (tmp) {
447 arg->name = xstrndup(str, tmp - str);
448 str = tmp + 1;
449 }
450
7df2f329
MH
451 tmp = strpbrk(str, "-.");
452 if (!is_c_varname(str) || !tmp) {
453 /* A variable, register, symbol or special value */
48481938
MH
454 arg->var = xstrdup(str);
455 pr_debug("%s\n", arg->var);
7df2f329
MH
456 return;
457 }
458
459 /* Structure fields */
48481938
MH
460 arg->var = xstrndup(str, tmp - str);
461 pr_debug("%s, ", arg->var);
7df2f329
MH
462 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);
484}
485
4235b045
MH
486/* Parse perf-probe event command */
487void parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
50656eec 488{
e1c01d61 489 char **argv;
fac13fd5
MH
490 int argc, i;
491
4235b045 492 argv = argv_split(cmd, &argc);
e1c01d61
MH
493 if (!argv)
494 die("argv_split failed.");
495 if (argc > MAX_PROBE_ARGS + 1)
496 semantic_error("Too many arguments");
50656eec
MH
497
498 /* Parse probe point */
4235b045 499 parse_perf_probe_point(argv[0], pev);
50656eec 500
e1c01d61 501 /* Copy arguments and ensure return probe has no C argument */
4235b045
MH
502 pev->nargs = argc - 1;
503 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
504 for (i = 0; i < pev->nargs; i++) {
7df2f329 505 parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
48481938 506 if (is_c_varname(pev->args[i].var) && pev->point.retprobe)
4235b045
MH
507 semantic_error("You can't specify local variable for"
508 " kretprobe");
e1c01d61 509 }
50656eec 510
e1c01d61 511 argv_free(argv);
50656eec
MH
512}
513
4235b045
MH
514/* Return true if this perf_probe_event requires debuginfo */
515bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
516{
517 int i;
518
519 if (pev->point.file || pev->point.line || pev->point.lazy_line)
520 return true;
521
522 for (i = 0; i < pev->nargs; i++)
48481938 523 if (is_c_varname(pev->args[i].var))
4235b045
MH
524 return true;
525
526 return false;
527}
528
4de189fe 529/* Parse kprobe_events event into struct probe_point */
4235b045 530void parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
4de189fe 531{
4235b045 532 struct kprobe_trace_point *tp = &tev->point;
4de189fe
MH
533 char pr;
534 char *p;
535 int ret, i, argc;
536 char **argv;
537
4235b045
MH
538 pr_debug("Parsing kprobe_events: %s\n", cmd);
539 argv = argv_split(cmd, &argc);
4de189fe
MH
540 if (!argv)
541 die("argv_split failed.");
542 if (argc < 2)
543 semantic_error("Too less arguments.");
544
545 /* Scan event and group name. */
93aaa45a 546 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
4235b045
MH
547 &pr, (float *)(void *)&tev->group,
548 (float *)(void *)&tev->event);
4de189fe
MH
549 if (ret != 3)
550 semantic_error("Failed to parse event name: %s", argv[0]);
4235b045 551 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
4de189fe 552
4235b045 553 tp->retprobe = (pr == 'r');
4de189fe
MH
554
555 /* Scan function name and offset */
4235b045
MH
556 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
557 &tp->offset);
4de189fe 558 if (ret == 1)
4235b045 559 tp->offset = 0;
4de189fe 560
4235b045
MH
561 tev->nargs = argc - 2;
562 tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
563 for (i = 0; i < tev->nargs; i++) {
4de189fe
MH
564 p = strchr(argv[i + 2], '=');
565 if (p) /* We don't need which register is assigned. */
4235b045
MH
566 *p++ = '\0';
567 else
568 p = argv[i + 2];
569 tev->args[i].name = xstrdup(argv[i + 2]);
570 /* TODO: parse regs and offset */
571 tev->args[i].value = xstrdup(p);
4de189fe
MH
572 }
573
4de189fe
MH
574 argv_free(argv);
575}
576
7df2f329
MH
577/* Compose only probe arg */
578int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
579{
580 struct perf_probe_arg_field *field = pa->field;
581 int ret;
582 char *tmp = buf;
583
48481938
MH
584 if (pa->name && pa->var)
585 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
586 else
587 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
7df2f329
MH
588 if (ret <= 0)
589 goto error;
590 tmp += ret;
591 len -= ret;
592
593 while (field) {
594 ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
595 field->name);
596 if (ret <= 0)
597 goto error;
598 tmp += ret;
599 len -= ret;
600 field = field->next;
601 }
602 return tmp - buf;
603error:
604 die("Failed to synthesize perf probe argument: %s", strerror(-ret));
605}
606
4235b045
MH
607/* Compose only probe point (not argument) */
608static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
4de189fe 609{
fb1587d8
MH
610 char *buf, *tmp;
611 char offs[32] = "", line[32] = "", file[32] = "";
612 int ret, len;
4de189fe 613
4235b045 614 buf = xzalloc(MAX_CMDLEN);
4de189fe 615 if (pp->offset) {
fb1587d8 616 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
4de189fe
MH
617 if (ret <= 0)
618 goto error;
619 }
620 if (pp->line) {
fb1587d8
MH
621 ret = e_snprintf(line, 32, ":%d", pp->line);
622 if (ret <= 0)
623 goto error;
624 }
625 if (pp->file) {
626 len = strlen(pp->file) - 32;
627 if (len < 0)
628 len = 0;
629 tmp = strchr(pp->file + len, '/');
630 if (!tmp)
631 tmp = pp->file + len - 1;
632 ret = e_snprintf(file, 32, "@%s", tmp + 1);
4de189fe
MH
633 if (ret <= 0)
634 goto error;
635 }
636
637 if (pp->function)
fb1587d8
MH
638 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
639 offs, pp->retprobe ? "%return" : "", line,
640 file);
4de189fe 641 else
fb1587d8 642 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
4235b045
MH
643 if (ret <= 0)
644 goto error;
645
646 return buf;
7ef17aaf 647error:
4235b045 648 die("Failed to synthesize perf probe point: %s", strerror(-ret));
7ef17aaf
MH
649}
650
4235b045
MH
651#if 0
652char *synthesize_perf_probe_command(struct perf_probe_event *pev)
7ef17aaf
MH
653{
654 char *buf;
655 int i, len, ret;
656
4235b045
MH
657 buf = synthesize_perf_probe_point(&pev->point);
658 if (!buf)
659 return NULL;
4de189fe 660
4235b045
MH
661 len = strlen(buf);
662 for (i = 0; i < pev->nargs; i++) {
4de189fe 663 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
4235b045
MH
664 pev->args[i].name);
665 if (ret <= 0) {
666 free(buf);
667 return NULL;
668 }
4de189fe
MH
669 len += ret;
670 }
4de189fe 671
4235b045
MH
672 return buf;
673}
674#endif
675
676static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
677 char **buf, size_t *buflen,
678 int depth)
679{
680 int ret;
681 if (ref->next) {
682 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
683 buflen, depth + 1);
684 if (depth < 0)
685 goto out;
686 }
687
688 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
689 if (ret < 0)
690 depth = ret;
691 else {
692 *buf += ret;
693 *buflen -= ret;
694 }
695out:
696 return depth;
4de189fe 697
4de189fe
MH
698}
699
4235b045
MH
700static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
701 char *buf, size_t buflen)
50656eec 702{
4235b045
MH
703 int ret, depth = 0;
704 char *tmp = buf;
705
706 /* Argument name or separator */
707 if (arg->name)
708 ret = e_snprintf(buf, buflen, " %s=", arg->name);
709 else
710 ret = e_snprintf(buf, buflen, " ");
711 if (ret < 0)
712 return ret;
713 buf += ret;
714 buflen -= ret;
715
716 /* Dereferencing arguments */
717 if (arg->ref) {
718 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
719 &buflen, 1);
720 if (depth < 0)
721 return depth;
722 }
723
724 /* Print argument value */
725 ret = e_snprintf(buf, buflen, "%s", arg->value);
726 if (ret < 0)
727 return ret;
728 buf += ret;
729 buflen -= ret;
730
731 /* Closing */
732 while (depth--) {
733 ret = e_snprintf(buf, buflen, ")");
734 if (ret < 0)
735 return ret;
736 buf += ret;
737 buflen -= ret;
738 }
739
740 return buf - tmp;
741}
742
743char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
744{
745 struct kprobe_trace_point *tp = &tev->point;
50656eec
MH
746 char *buf;
747 int i, len, ret;
748
4235b045
MH
749 buf = xzalloc(MAX_CMDLEN);
750 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
751 tp->retprobe ? 'r' : 'p',
752 tev->group, tev->event,
753 tp->symbol, tp->offset);
754 if (len <= 0)
50656eec 755 goto error;
50656eec 756
4235b045
MH
757 for (i = 0; i < tev->nargs; i++) {
758 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
759 MAX_CMDLEN - len);
4de189fe 760 if (ret <= 0)
50656eec
MH
761 goto error;
762 len += ret;
763 }
50656eec 764
4235b045 765 return buf;
50656eec 766error:
4235b045
MH
767 free(buf);
768 return NULL;
769}
50656eec 770
4235b045
MH
771void convert_to_perf_probe_event(struct kprobe_trace_event *tev,
772 struct perf_probe_event *pev)
773{
774 char buf[64];
775 int i;
4235b045 776
4b4da7f7 777 /* Convert event/group name */
fb1587d8
MH
778 pev->event = xstrdup(tev->event);
779 pev->group = xstrdup(tev->group);
780
4b4da7f7
MH
781 /* Convert trace_point to probe_point */
782 convert_to_perf_probe_point(&tev->point, &pev->point);
783
4235b045
MH
784 /* Convert trace_arg to probe_arg */
785 pev->nargs = tev->nargs;
786 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
787 for (i = 0; i < tev->nargs; i++)
788 if (tev->args[i].name)
789 pev->args[i].name = xstrdup(tev->args[i].name);
790 else {
791 synthesize_kprobe_trace_arg(&tev->args[i], buf, 64);
792 pev->args[i].name = xstrdup(buf);
793 }
794}
795
796void clear_perf_probe_event(struct perf_probe_event *pev)
797{
798 struct perf_probe_point *pp = &pev->point;
7df2f329 799 struct perf_probe_arg_field *field, *next;
4235b045
MH
800 int i;
801
802 if (pev->event)
803 free(pev->event);
804 if (pev->group)
805 free(pev->group);
806 if (pp->file)
807 free(pp->file);
808 if (pp->function)
809 free(pp->function);
810 if (pp->lazy_line)
811 free(pp->lazy_line);
7df2f329 812 for (i = 0; i < pev->nargs; i++) {
4235b045
MH
813 if (pev->args[i].name)
814 free(pev->args[i].name);
48481938
MH
815 if (pev->args[i].var)
816 free(pev->args[i].var);
7df2f329
MH
817 field = pev->args[i].field;
818 while (field) {
819 next = field->next;
820 if (field->name)
821 free(field->name);
822 free(field);
823 field = next;
824 }
825 }
4235b045
MH
826 if (pev->args)
827 free(pev->args);
828 memset(pev, 0, sizeof(*pev));
829}
830
831void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
832{
833 struct kprobe_trace_arg_ref *ref, *next;
834 int i;
835
836 if (tev->event)
837 free(tev->event);
838 if (tev->group)
839 free(tev->group);
840 if (tev->point.symbol)
841 free(tev->point.symbol);
842 for (i = 0; i < tev->nargs; i++) {
843 if (tev->args[i].name)
844 free(tev->args[i].name);
845 if (tev->args[i].value)
846 free(tev->args[i].value);
847 ref = tev->args[i].ref;
848 while (ref) {
849 next = ref->next;
850 free(ref);
851 ref = next;
852 }
853 }
854 if (tev->args)
855 free(tev->args);
856 memset(tev, 0, sizeof(*tev));
50656eec
MH
857}
858
f4d7da49 859static int open_kprobe_events(bool readwrite)
4de189fe
MH
860{
861 char buf[PATH_MAX];
862 int ret;
863
864 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
865 if (ret < 0)
866 die("Failed to make kprobe_events path.");
867
f4d7da49
MH
868 if (readwrite && !probe_event_dry_run)
869 ret = open(buf, O_RDWR, O_APPEND);
870 else
871 ret = open(buf, O_RDONLY, 0);
872
4de189fe
MH
873 if (ret < 0) {
874 if (errno == ENOENT)
875 die("kprobe_events file does not exist -"
63bbd5e2 876 " please rebuild with CONFIG_KPROBE_EVENT.");
4de189fe
MH
877 else
878 die("Could not open kprobe_events file: %s",
879 strerror(errno));
880 }
881 return ret;
882}
883
884/* Get raw string list of current kprobe_events */
4235b045 885static struct strlist *get_kprobe_trace_command_rawlist(int fd)
4de189fe
MH
886{
887 int ret, idx;
888 FILE *fp;
889 char buf[MAX_CMDLEN];
890 char *p;
891 struct strlist *sl;
892
893 sl = strlist__new(true, NULL);
894
895 fp = fdopen(dup(fd), "r");
896 while (!feof(fp)) {
897 p = fgets(buf, MAX_CMDLEN, fp);
898 if (!p)
899 break;
900
901 idx = strlen(p) - 1;
902 if (p[idx] == '\n')
903 p[idx] = '\0';
904 ret = strlist__add(sl, buf);
905 if (ret < 0)
906 die("strlist__add failed: %s", strerror(-ret));
907 }
908 fclose(fp);
909
910 return sl;
911}
912
278498d4 913/* Show an event */
4235b045 914static void show_perf_probe_event(struct perf_probe_event *pev)
278498d4 915{
7e990a51 916 int i, ret;
278498d4 917 char buf[128];
4235b045 918 char *place;
278498d4 919
4235b045
MH
920 /* Synthesize only event probe point */
921 place = synthesize_perf_probe_point(&pev->point);
922
923 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
7e990a51
MH
924 if (ret < 0)
925 die("Failed to copy event: %s", strerror(-ret));
fb1587d8 926 printf(" %-20s (on %s", buf, place);
278498d4 927
4235b045 928 if (pev->nargs > 0) {
278498d4 929 printf(" with");
7df2f329
MH
930 for (i = 0; i < pev->nargs; i++) {
931 synthesize_perf_probe_arg(&pev->args[i], buf, 128);
932 printf(" %s", buf);
933 }
278498d4
MH
934 }
935 printf(")\n");
4235b045 936 free(place);
278498d4
MH
937}
938
4de189fe
MH
939/* List up current perf-probe events */
940void show_perf_probe_events(void)
941{
7ef17aaf 942 int fd;
4235b045
MH
943 struct kprobe_trace_event tev;
944 struct perf_probe_event pev;
4de189fe
MH
945 struct strlist *rawlist;
946 struct str_node *ent;
947
72041334 948 setup_pager();
fb1587d8 949 init_vmlinux();
4235b045
MH
950
951 memset(&tev, 0, sizeof(tev));
952 memset(&pev, 0, sizeof(pev));
72041334 953
f4d7da49 954 fd = open_kprobe_events(false);
4235b045 955 rawlist = get_kprobe_trace_command_rawlist(fd);
4de189fe
MH
956 close(fd);
957
adf365f4 958 strlist__for_each(ent, rawlist) {
4235b045
MH
959 parse_kprobe_trace_command(ent->s, &tev);
960 convert_to_perf_probe_event(&tev, &pev);
278498d4 961 /* Show an event */
4235b045
MH
962 show_perf_probe_event(&pev);
963 clear_perf_probe_event(&pev);
964 clear_kprobe_trace_event(&tev);
4de189fe
MH
965 }
966
967 strlist__delete(rawlist);
968}
969
b498ce1f 970/* Get current perf-probe event names */
4235b045 971static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
b498ce1f 972{
fa28244d 973 char buf[128];
b498ce1f
MH
974 struct strlist *sl, *rawlist;
975 struct str_node *ent;
4235b045 976 struct kprobe_trace_event tev;
b498ce1f 977
4235b045 978 memset(&tev, 0, sizeof(tev));
b498ce1f 979
4235b045 980 rawlist = get_kprobe_trace_command_rawlist(fd);
e1d2017b 981 sl = strlist__new(true, NULL);
adf365f4 982 strlist__for_each(ent, rawlist) {
4235b045 983 parse_kprobe_trace_command(ent->s, &tev);
fa28244d 984 if (include_group) {
4235b045
MH
985 if (e_snprintf(buf, 128, "%s:%s", tev.group,
986 tev.event) < 0)
fa28244d
MH
987 die("Failed to copy group:event name.");
988 strlist__add(sl, buf);
989 } else
4235b045
MH
990 strlist__add(sl, tev.event);
991 clear_kprobe_trace_event(&tev);
b498ce1f
MH
992 }
993
994 strlist__delete(rawlist);
995
996 return sl;
997}
998
4235b045 999static void write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
50656eec
MH
1000{
1001 int ret;
4235b045 1002 char *buf = synthesize_kprobe_trace_command(tev);
50656eec 1003
fa28244d 1004 pr_debug("Writing event: %s\n", buf);
f4d7da49
MH
1005 if (!probe_event_dry_run) {
1006 ret = write(fd, buf, strlen(buf));
1007 if (ret <= 0)
1008 die("Failed to write event: %s", strerror(errno));
1009 }
4235b045 1010 free(buf);
50656eec
MH
1011}
1012
b498ce1f 1013static void get_new_event_name(char *buf, size_t len, const char *base,
d761b08b 1014 struct strlist *namelist, bool allow_suffix)
b498ce1f
MH
1015{
1016 int i, ret;
17f88fcd
MH
1017
1018 /* Try no suffix */
1019 ret = e_snprintf(buf, len, "%s", base);
1020 if (ret < 0)
1021 die("snprintf() failed: %s", strerror(-ret));
1022 if (!strlist__has_entry(namelist, buf))
1023 return;
1024
d761b08b
MH
1025 if (!allow_suffix) {
1026 pr_warning("Error: event \"%s\" already exists. "
1027 "(Use -f to force duplicates.)\n", base);
1028 die("Can't add new event.");
1029 }
1030
17f88fcd
MH
1031 /* Try to add suffix */
1032 for (i = 1; i < MAX_EVENT_INDEX; i++) {
b498ce1f
MH
1033 ret = e_snprintf(buf, len, "%s_%d", base, i);
1034 if (ret < 0)
1035 die("snprintf() failed: %s", strerror(-ret));
1036 if (!strlist__has_entry(namelist, buf))
1037 break;
1038 }
1039 if (i == MAX_EVENT_INDEX)
1040 die("Too many events are on the same function.");
1041}
1042
4235b045
MH
1043static void __add_kprobe_trace_events(struct perf_probe_event *pev,
1044 struct kprobe_trace_event *tevs,
1045 int ntevs, bool allow_suffix)
50656eec 1046{
4235b045 1047 int i, fd;
55632770 1048 struct kprobe_trace_event *tev = NULL;
4235b045
MH
1049 char buf[64];
1050 const char *event, *group;
b498ce1f 1051 struct strlist *namelist;
50656eec 1052
f4d7da49 1053 fd = open_kprobe_events(true);
b498ce1f 1054 /* Get current event names */
4235b045
MH
1055 namelist = get_kprobe_trace_event_names(fd, false);
1056
1057 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
1058 for (i = 0; i < ntevs; i++) {
1059 tev = &tevs[i];
1060 if (pev->event)
1061 event = pev->event;
1062 else
1063 if (pev->point.function)
1064 event = pev->point.function;
1065 else
1066 event = tev->point.symbol;
1067 if (pev->group)
1068 group = pev->group;
1069 else
1070 group = PERFPROBE_GROUP;
1071
1072 /* Get an unused new event name */
1073 get_new_event_name(buf, 64, event, namelist, allow_suffix);
1074 event = buf;
1075
1076 tev->event = xstrdup(event);
1077 tev->group = xstrdup(group);
1078 write_kprobe_trace_event(fd, tev);
1079 /* Add added event name to namelist */
1080 strlist__add(namelist, event);
1081
1082 /* Trick here - save current event/group */
1083 event = pev->event;
1084 group = pev->group;
1085 pev->event = tev->event;
1086 pev->group = tev->group;
1087 show_perf_probe_event(pev);
1088 /* Trick here - restore current event/group */
1089 pev->event = (char *)event;
1090 pev->group = (char *)group;
1091
1092 /*
1093 * Probes after the first probe which comes from same
1094 * user input are always allowed to add suffix, because
1095 * there might be several addresses corresponding to
1096 * one code line.
1097 */
1098 allow_suffix = true;
50656eec 1099 }
a9b495b0
MH
1100 /* Show how to use the event. */
1101 printf("\nYou can now use it on all perf tools, such as:\n\n");
4235b045 1102 printf("\tperf record -e %s:%s -a sleep 1\n\n", tev->group, tev->event);
a9b495b0 1103
e1d2017b 1104 strlist__delete(namelist);
50656eec
MH
1105 close(fd);
1106}
fa28244d 1107
4235b045
MH
1108static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
1109 struct kprobe_trace_event **tevs)
e0faa8d3
MH
1110{
1111 struct symbol *sym;
4235b045
MH
1112 int ntevs = 0, i;
1113 struct kprobe_trace_event *tev;
1114
4b4da7f7
MH
1115 /* Convert perf_probe_event with debuginfo */
1116 ntevs = try_to_find_kprobe_trace_events(pev, tevs);
1117 if (ntevs > 0)
1118 return ntevs;
e0faa8d3 1119
4235b045
MH
1120 /* Allocate trace event buffer */
1121 ntevs = 1;
1122 tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event));
1123
1124 /* Copy parameters */
1125 tev->point.symbol = xstrdup(pev->point.function);
1126 tev->point.offset = pev->point.offset;
1127 tev->nargs = pev->nargs;
1128 if (tev->nargs) {
1129 tev->args = xzalloc(sizeof(struct kprobe_trace_arg)
1130 * tev->nargs);
48481938
MH
1131 for (i = 0; i < tev->nargs; i++) {
1132 if (pev->args[i].name)
1133 tev->args[i].name = xstrdup(pev->args[i].name);
1134 tev->args[i].value = xstrdup(pev->args[i].var);
1135 }
4235b045
MH
1136 }
1137
1138 /* Currently just checking function name from symbol map */
1139 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
1140 tev->point.symbol, NULL);
1141 if (!sym)
1142 die("Kernel symbol \'%s\' not found - probe not added.",
1143 tev->point.symbol);
4b4da7f7 1144
4235b045
MH
1145 return ntevs;
1146}
1147
1148struct __event_package {
1149 struct perf_probe_event *pev;
1150 struct kprobe_trace_event *tevs;
1151 int ntevs;
1152};
1153
1154void add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1155 bool force_add)
1156{
1157 int i;
1158 struct __event_package *pkgs;
1159
1160 pkgs = xzalloc(sizeof(struct __event_package) * npevs);
1161
1162 /* Init vmlinux path */
1163 init_vmlinux();
1164
1165 /* Loop 1: convert all events */
1166 for (i = 0; i < npevs; i++) {
1167 pkgs[i].pev = &pevs[i];
1168 /* Convert with or without debuginfo */
1169 pkgs[i].ntevs = convert_to_kprobe_trace_events(pkgs[i].pev,
1170 &pkgs[i].tevs);
e0faa8d3
MH
1171 }
1172
4235b045
MH
1173 /* Loop 2: add all events */
1174 for (i = 0; i < npevs; i++)
1175 __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1176 pkgs[i].ntevs, force_add);
1177 /* TODO: cleanup all trace events? */
e0faa8d3
MH
1178}
1179
bbbb521b
MH
1180static void __del_trace_kprobe_event(int fd, struct str_node *ent)
1181{
1182 char *p;
1183 char buf[128];
4235b045 1184 int ret;
bbbb521b
MH
1185
1186 /* Convert from perf-probe event to trace-kprobe event */
1187 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
1188 die("Failed to copy event.");
1189 p = strchr(buf + 2, ':');
1190 if (!p)
1191 die("Internal error: %s should have ':' but not.", ent->s);
1192 *p = '/';
1193
4235b045
MH
1194 pr_debug("Writing event: %s\n", buf);
1195 ret = write(fd, buf, strlen(buf));
1196 if (ret <= 0)
1197 die("Failed to write event: %s", strerror(errno));
bbbb521b
MH
1198 printf("Remove event: %s\n", ent->s);
1199}
1200
fa28244d
MH
1201static void del_trace_kprobe_event(int fd, const char *group,
1202 const char *event, struct strlist *namelist)
1203{
1204 char buf[128];
bbbb521b
MH
1205 struct str_node *ent, *n;
1206 int found = 0;
fa28244d
MH
1207
1208 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
1209 die("Failed to copy event.");
fa28244d 1210
bbbb521b
MH
1211 if (strpbrk(buf, "*?")) { /* Glob-exp */
1212 strlist__for_each_safe(ent, n, namelist)
1213 if (strglobmatch(ent->s, buf)) {
1214 found++;
1215 __del_trace_kprobe_event(fd, ent);
1216 strlist__remove(namelist, ent);
1217 }
1218 } else {
1219 ent = strlist__find(namelist, buf);
1220 if (ent) {
1221 found++;
1222 __del_trace_kprobe_event(fd, ent);
1223 strlist__remove(namelist, ent);
1224 }
1225 }
1226 if (found == 0)
1227 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
fa28244d
MH
1228}
1229
4235b045 1230void del_perf_probe_events(struct strlist *dellist)
fa28244d
MH
1231{
1232 int fd;
fa28244d
MH
1233 const char *group, *event;
1234 char *p, *str;
1235 struct str_node *ent;
1236 struct strlist *namelist;
1237
f4d7da49 1238 fd = open_kprobe_events(true);
fa28244d 1239 /* Get current event names */
4235b045 1240 namelist = get_kprobe_trace_event_names(fd, true);
fa28244d 1241
adf365f4 1242 strlist__for_each(ent, dellist) {
31facc5f 1243 str = xstrdup(ent->s);
bbbb521b 1244 pr_debug("Parsing: %s\n", str);
fa28244d
MH
1245 p = strchr(str, ':');
1246 if (p) {
1247 group = str;
1248 *p = '\0';
1249 event = p + 1;
1250 } else {
bbbb521b 1251 group = "*";
fa28244d
MH
1252 event = str;
1253 }
bbbb521b 1254 pr_debug("Group: %s, Event: %s\n", group, event);
fa28244d
MH
1255 del_trace_kprobe_event(fd, group, event, namelist);
1256 free(str);
1257 }
1258 strlist__delete(namelist);
1259 close(fd);
1260}
1261