perf probe: Remove die() from probe-event code
[GitHub/mt8127/android_kernel_alcatel_ttab.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
146a1439 56#define semantic_error(msg ...) pr_err("Semantic error :" msg)
50656eec 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 */
146a1439 79static int init_vmlinux(void)
e0faa8d3 80{
146a1439
MH
81 int ret;
82
e0faa8d3
MH
83 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);
146a1439
MH
88 ret = symbol__init();
89 if (ret < 0) {
90 pr_debug("Failed to init symbol map.\n");
91 goto out;
92 }
e0faa8d3
MH
93
94 map_groups__init(&kmap_groups);
146a1439
MH
95 ret = map_groups__create_kernel_maps(&kmap_groups, kmaps);
96 if (ret < 0)
97 pr_debug("Failed to create kernel maps.\n");
98
99out:
100 if (ret < 0)
101 pr_warning("Failed to init vmlinux path.\n");
102 return ret;
e0faa8d3
MH
103}
104
4b4da7f7 105#ifdef DWARF_SUPPORT
e0faa8d3
MH
106static int open_vmlinux(void)
107{
108 if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) {
109 pr_debug("Failed to load kernel map.\n");
110 return -EINVAL;
111 }
112 pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name);
113 return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
114}
4b4da7f7 115
146a1439
MH
116/* Convert trace point to probe point with debuginfo */
117static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
118 struct perf_probe_point *pp)
4b4da7f7
MH
119{
120 struct symbol *sym;
146a1439 121 int fd, ret = -ENOENT;
4b4da7f7
MH
122
123 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
124 tp->symbol, NULL);
125 if (sym) {
126 fd = open_vmlinux();
146a1439
MH
127 if (fd >= 0) {
128 ret = find_perf_probe_point(fd,
129 sym->start + tp->offset, pp);
130 close(fd);
131 }
4b4da7f7
MH
132 }
133 if (ret <= 0) {
146a1439
MH
134 pr_debug("Failed to find corresponding probes from "
135 "debuginfo. Use kprobe event information.\n");
4b4da7f7
MH
136 pp->function = xstrdup(tp->symbol);
137 pp->offset = tp->offset;
138 }
139 pp->retprobe = tp->retprobe;
146a1439
MH
140
141 return 0;
4b4da7f7
MH
142}
143
144/* Try to find perf_probe_event with debuginfo */
145static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
146 struct kprobe_trace_event **tevs)
147{
148 bool need_dwarf = perf_probe_event_need_dwarf(pev);
149 int fd, ntevs;
150
151 fd = open_vmlinux();
152 if (fd < 0) {
146a1439
MH
153 if (need_dwarf) {
154 pr_warning("Failed to open debuginfo file.\n");
155 return fd;
156 }
4b4da7f7
MH
157 pr_debug("Could not open vmlinux. Try to use symbols.\n");
158 return 0;
159 }
160
161 /* Searching trace events corresponding to probe event */
162 ntevs = find_kprobe_trace_events(fd, pev, tevs);
163 close(fd);
164
146a1439
MH
165 if (ntevs > 0) { /* Succeeded to find trace events */
166 pr_debug("find %d kprobe_trace_events.\n", ntevs);
4b4da7f7 167 return ntevs;
146a1439 168 }
4b4da7f7 169
146a1439
MH
170 if (ntevs == 0) { /* No error but failed to find probe point. */
171 pr_warning("Probe point '%s' not found.\n",
172 synthesize_perf_probe_point(&pev->point));
173 return -ENOENT;
174 }
175 /* Error path : ntevs < 0 */
4b4da7f7 176 if (need_dwarf) {
b55a87ad 177 if (ntevs == -EBADF)
4b4da7f7
MH
178 pr_warning("No dwarf info found in the vmlinux - "
179 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
146a1439 180 return ntevs;
4b4da7f7
MH
181 }
182 pr_debug("An error occurred in debuginfo analysis."
183 " Try to use symbols.\n");
184 return 0;
4b4da7f7
MH
185}
186
187#define LINEBUF_SIZE 256
188#define NR_ADDITIONAL_LINES 2
189
146a1439 190static int show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
4b4da7f7
MH
191{
192 char buf[LINEBUF_SIZE];
193 const char *color = PERF_COLOR_BLUE;
194
195 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
196 goto error;
197 if (!skip) {
198 if (show_num)
199 fprintf(stdout, "%7u %s", l, buf);
200 else
201 color_fprintf(stdout, color, " %s", buf);
202 }
203
204 while (strlen(buf) == LINEBUF_SIZE - 1 &&
205 buf[LINEBUF_SIZE - 2] != '\n') {
206 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
207 goto error;
208 if (!skip) {
209 if (show_num)
210 fprintf(stdout, "%s", buf);
211 else
212 color_fprintf(stdout, color, "%s", buf);
213 }
214 }
146a1439
MH
215
216 return 0;
4b4da7f7
MH
217error:
218 if (feof(fp))
146a1439 219 pr_warning("Source file is shorter than expected.\n");
4b4da7f7 220 else
146a1439
MH
221 pr_warning("File read error: %s\n", strerror(errno));
222
223 return -1;
4b4da7f7
MH
224}
225
226/*
227 * Show line-range always requires debuginfo to find source file and
228 * line number.
229 */
146a1439 230int show_line_range(struct line_range *lr)
4b4da7f7
MH
231{
232 unsigned int l = 1;
233 struct line_node *ln;
234 FILE *fp;
235 int fd, ret;
236
237 /* Search a line range */
146a1439
MH
238 ret = init_vmlinux();
239 if (ret < 0)
240 return ret;
241
4b4da7f7 242 fd = open_vmlinux();
146a1439
MH
243 if (fd < 0) {
244 pr_warning("Failed to open debuginfo file.\n");
245 return fd;
246 }
247
4b4da7f7 248 ret = find_line_range(fd, lr);
4b4da7f7 249 close(fd);
146a1439
MH
250 if (ret == 0) {
251 pr_warning("Specified source line is not found.\n");
252 return -ENOENT;
253 } else if (ret < 0) {
254 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
255 return ret;
256 }
4b4da7f7
MH
257
258 setup_pager();
259
260 if (lr->function)
261 fprintf(stdout, "<%s:%d>\n", lr->function,
262 lr->start - lr->offset);
263 else
264 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
265
266 fp = fopen(lr->path, "r");
146a1439
MH
267 if (fp == NULL) {
268 pr_warning("Failed to open %s: %s\n", lr->path,
269 strerror(errno));
270 return -errno;
271 }
4b4da7f7 272 /* Skip to starting line number */
146a1439
MH
273 while (l < lr->start && ret >= 0)
274 ret = show_one_line(fp, l++, true, false);
275 if (ret < 0)
276 goto end;
4b4da7f7
MH
277
278 list_for_each_entry(ln, &lr->line_list, list) {
146a1439
MH
279 while (ln->line > l && ret >= 0)
280 ret = show_one_line(fp, (l++) - lr->offset,
281 false, false);
282 if (ret >= 0)
283 ret = show_one_line(fp, (l++) - lr->offset,
284 false, true);
285 if (ret < 0)
286 goto end;
4b4da7f7
MH
287 }
288
289 if (lr->end == INT_MAX)
290 lr->end = l + NR_ADDITIONAL_LINES;
146a1439
MH
291 while (l < lr->end && !feof(fp) && ret >= 0)
292 ret = show_one_line(fp, (l++) - lr->offset, false, false);
293end:
4b4da7f7 294 fclose(fp);
146a1439 295 return ret;
4b4da7f7
MH
296}
297
298#else /* !DWARF_SUPPORT */
299
146a1439 300static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
4b4da7f7
MH
301 struct perf_probe_point *pp)
302{
303 pp->function = xstrdup(tp->symbol);
304 pp->offset = tp->offset;
305 pp->retprobe = tp->retprobe;
146a1439
MH
306
307 return 0;
4b4da7f7
MH
308}
309
310static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
311 struct kprobe_trace_event **tevs __unused)
312{
146a1439
MH
313 if (perf_probe_event_need_dwarf(pev)) {
314 pr_warning("Debuginfo-analysis is not supported.\n");
315 return -ENOSYS;
316 }
4b4da7f7
MH
317 return 0;
318}
319
146a1439 320int show_line_range(struct line_range *lr __unused)
4b4da7f7 321{
146a1439
MH
322 pr_warning("Debuginfo-analysis is not supported.\n");
323 return -ENOSYS;
4b4da7f7
MH
324}
325
e0faa8d3
MH
326#endif
327
146a1439 328int parse_line_range_desc(const char *arg, struct line_range *lr)
631c9def
MH
329{
330 const char *ptr;
331 char *tmp;
332 /*
333 * <Syntax>
334 * SRC:SLN[+NUM|-ELN]
335 * FUNC[:SLN[+NUM|-ELN]]
336 */
337 ptr = strchr(arg, ':');
338 if (ptr) {
339 lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0);
340 if (*tmp == '+')
341 lr->end = lr->start + (unsigned int)strtoul(tmp + 1,
342 &tmp, 0);
343 else if (*tmp == '-')
344 lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0);
345 else
346 lr->end = 0;
347 pr_debug("Line range is %u to %u\n", lr->start, lr->end);
146a1439 348 if (lr->end && lr->start > lr->end) {
631c9def 349 semantic_error("Start line must be smaller"
146a1439
MH
350 " than end line.\n");
351 return -EINVAL;
352 }
353 if (*tmp != '\0') {
354 semantic_error("Tailing with invalid character '%d'.\n",
631c9def 355 *tmp);
146a1439
MH
356 return -EINVAL;
357 }
31facc5f 358 tmp = xstrndup(arg, (ptr - arg));
631c9def 359 } else
31facc5f 360 tmp = xstrdup(arg);
631c9def
MH
361
362 if (strchr(tmp, '.'))
363 lr->file = tmp;
364 else
365 lr->function = tmp;
146a1439
MH
366
367 return 0;
631c9def
MH
368}
369
b7702a21
MH
370/* Check the name is good for event/group */
371static bool check_event_name(const char *name)
372{
373 if (!isalpha(*name) && *name != '_')
374 return false;
375 while (*++name != '\0') {
376 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
377 return false;
378 }
379 return true;
380}
381
50656eec 382/* Parse probepoint definition. */
146a1439 383static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
50656eec 384{
4235b045 385 struct perf_probe_point *pp = &pev->point;
50656eec
MH
386 char *ptr, *tmp;
387 char c, nc = 0;
388 /*
389 * <Syntax>
2a9c8c36
MH
390 * perf probe [EVENT=]SRC[:LN|;PTN]
391 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
af663d75
MH
392 *
393 * TODO:Group name support
50656eec
MH
394 */
395
2a9c8c36
MH
396 ptr = strpbrk(arg, ";=@+%");
397 if (ptr && *ptr == '=') { /* Event name */
af663d75
MH
398 *ptr = '\0';
399 tmp = ptr + 1;
146a1439
MH
400 if (strchr(arg, ':')) {
401 semantic_error("Group name is not supported yet.\n");
402 return -ENOTSUP;
403 }
404 if (!check_event_name(arg)) {
b7702a21 405 semantic_error("%s is bad for event name -it must "
146a1439
MH
406 "follow C symbol-naming rule.\n", arg);
407 return -EINVAL;
408 }
4235b045
MH
409 pev->event = xstrdup(arg);
410 pev->group = NULL;
af663d75
MH
411 arg = tmp;
412 }
413
2a9c8c36 414 ptr = strpbrk(arg, ";:+@%");
50656eec
MH
415 if (ptr) {
416 nc = *ptr;
417 *ptr++ = '\0';
418 }
419
420 /* Check arg is function or file and copy it */
421 if (strchr(arg, '.')) /* File */
31facc5f 422 pp->file = xstrdup(arg);
50656eec 423 else /* Function */
31facc5f 424 pp->function = xstrdup(arg);
50656eec
MH
425
426 /* Parse other options */
427 while (ptr) {
428 arg = ptr;
429 c = nc;
2a9c8c36 430 if (c == ';') { /* Lazy pattern must be the last part */
31facc5f 431 pp->lazy_line = xstrdup(arg);
2a9c8c36
MH
432 break;
433 }
434 ptr = strpbrk(arg, ";:+@%");
50656eec
MH
435 if (ptr) {
436 nc = *ptr;
437 *ptr++ = '\0';
438 }
439 switch (c) {
440 case ':': /* Line number */
441 pp->line = strtoul(arg, &tmp, 0);
146a1439 442 if (*tmp != '\0') {
2a9c8c36 443 semantic_error("There is non-digit char"
146a1439
MH
444 " in line number.\n");
445 return -EINVAL;
446 }
50656eec
MH
447 break;
448 case '+': /* Byte offset from a symbol */
449 pp->offset = strtoul(arg, &tmp, 0);
146a1439 450 if (*tmp != '\0') {
2a9c8c36 451 semantic_error("There is non-digit character"
146a1439
MH
452 " in offset.\n");
453 return -EINVAL;
454 }
50656eec
MH
455 break;
456 case '@': /* File name */
146a1439
MH
457 if (pp->file) {
458 semantic_error("SRC@SRC is not allowed.\n");
459 return -EINVAL;
460 }
31facc5f 461 pp->file = xstrdup(arg);
50656eec
MH
462 break;
463 case '%': /* Probe places */
464 if (strcmp(arg, "return") == 0) {
465 pp->retprobe = 1;
146a1439
MH
466 } else { /* Others not supported yet */
467 semantic_error("%%%s is not supported.\n", arg);
468 return -ENOTSUP;
469 }
50656eec 470 break;
146a1439
MH
471 default: /* Buggy case */
472 pr_err("This program has a bug at %s:%d.\n",
473 __FILE__, __LINE__);
474 return -ENOTSUP;
50656eec
MH
475 break;
476 }
477 }
478
479 /* Exclusion check */
146a1439 480 if (pp->lazy_line && pp->line) {
2a9c8c36 481 semantic_error("Lazy pattern can't be used with line number.");
146a1439
MH
482 return -EINVAL;
483 }
2a9c8c36 484
146a1439 485 if (pp->lazy_line && pp->offset) {
2a9c8c36 486 semantic_error("Lazy pattern can't be used with offset.");
146a1439
MH
487 return -EINVAL;
488 }
2a9c8c36 489
146a1439 490 if (pp->line && pp->offset) {
50656eec 491 semantic_error("Offset can't be used with line number.");
146a1439
MH
492 return -EINVAL;
493 }
50656eec 494
146a1439 495 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
2a9c8c36
MH
496 semantic_error("File always requires line number or "
497 "lazy pattern.");
146a1439
MH
498 return -EINVAL;
499 }
50656eec 500
146a1439 501 if (pp->offset && !pp->function) {
50656eec 502 semantic_error("Offset requires an entry function.");
146a1439
MH
503 return -EINVAL;
504 }
50656eec 505
146a1439 506 if (pp->retprobe && !pp->function) {
50656eec 507 semantic_error("Return probe requires an entry function.");
146a1439
MH
508 return -EINVAL;
509 }
50656eec 510
146a1439 511 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
2a9c8c36
MH
512 semantic_error("Offset/Line/Lazy pattern can't be used with "
513 "return probe.");
146a1439
MH
514 return -EINVAL;
515 }
50656eec 516
4235b045 517 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
2a9c8c36
MH
518 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
519 pp->lazy_line);
146a1439 520 return 0;
50656eec
MH
521}
522
7df2f329 523/* Parse perf-probe event argument */
146a1439 524static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
7df2f329 525{
48481938 526 char *tmp;
7df2f329
MH
527 struct perf_probe_arg_field **fieldp;
528
529 pr_debug("parsing arg: %s into ", str);
530
48481938
MH
531 tmp = strchr(str, '=');
532 if (tmp) {
533 arg->name = xstrndup(str, tmp - str);
11a1ca35 534 pr_debug("name:%s ", arg->name);
48481938
MH
535 str = tmp + 1;
536 }
537
11a1ca35
MH
538 tmp = strchr(str, ':');
539 if (tmp) { /* Type setting */
540 *tmp = '\0';
541 arg->type = xstrdup(tmp + 1);
542 pr_debug("type:%s ", arg->type);
543 }
544
7df2f329
MH
545 tmp = strpbrk(str, "-.");
546 if (!is_c_varname(str) || !tmp) {
547 /* A variable, register, symbol or special value */
48481938
MH
548 arg->var = xstrdup(str);
549 pr_debug("%s\n", arg->var);
146a1439 550 return 0;
7df2f329
MH
551 }
552
553 /* Structure fields */
48481938
MH
554 arg->var = xstrndup(str, tmp - str);
555 pr_debug("%s, ", arg->var);
7df2f329
MH
556 fieldp = &arg->field;
557
558 do {
559 *fieldp = xzalloc(sizeof(struct perf_probe_arg_field));
560 if (*tmp == '.') {
561 str = tmp + 1;
562 (*fieldp)->ref = false;
563 } else if (tmp[1] == '>') {
564 str = tmp + 2;
565 (*fieldp)->ref = true;
146a1439
MH
566 } else {
567 semantic_error("Argument parse error: %s\n", str);
568 return -EINVAL;
569 }
7df2f329
MH
570
571 tmp = strpbrk(str, "-.");
572 if (tmp) {
573 (*fieldp)->name = xstrndup(str, tmp - str);
574 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
575 fieldp = &(*fieldp)->next;
576 }
577 } while (tmp);
578 (*fieldp)->name = xstrdup(str);
579 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
df0faf4b
MH
580
581 /* If no name is specified, set the last field name */
582 if (!arg->name)
583 arg->name = xstrdup((*fieldp)->name);
146a1439
MH
584
585 return 0;
7df2f329
MH
586}
587
4235b045 588/* Parse perf-probe event command */
146a1439 589int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
50656eec 590{
e1c01d61 591 char **argv;
146a1439 592 int argc, i, ret = 0;
fac13fd5 593
4235b045 594 argv = argv_split(cmd, &argc);
146a1439
MH
595 if (!argv) {
596 pr_debug("Failed to split arguments.\n");
597 return -ENOMEM;
598 }
599 if (argc - 1 > MAX_PROBE_ARGS) {
600 semantic_error("Too many probe arguments (%d).\n", argc - 1);
601 ret = -ERANGE;
602 goto out;
603 }
50656eec 604 /* Parse probe point */
146a1439
MH
605 ret = parse_perf_probe_point(argv[0], pev);
606 if (ret < 0)
607 goto out;
50656eec 608
e1c01d61 609 /* Copy arguments and ensure return probe has no C argument */
4235b045
MH
610 pev->nargs = argc - 1;
611 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
146a1439
MH
612 for (i = 0; i < pev->nargs && ret >= 0; i++) {
613 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
614 if (ret >= 0 &&
615 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
4235b045 616 semantic_error("You can't specify local variable for"
146a1439
MH
617 " kretprobe.\n");
618 ret = -EINVAL;
619 }
e1c01d61 620 }
146a1439 621out:
e1c01d61 622 argv_free(argv);
146a1439
MH
623
624 return ret;
50656eec
MH
625}
626
4235b045
MH
627/* Return true if this perf_probe_event requires debuginfo */
628bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
629{
630 int i;
631
632 if (pev->point.file || pev->point.line || pev->point.lazy_line)
633 return true;
634
635 for (i = 0; i < pev->nargs; i++)
48481938 636 if (is_c_varname(pev->args[i].var))
4235b045
MH
637 return true;
638
639 return false;
640}
641
4de189fe 642/* Parse kprobe_events event into struct probe_point */
146a1439 643int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
4de189fe 644{
4235b045 645 struct kprobe_trace_point *tp = &tev->point;
4de189fe
MH
646 char pr;
647 char *p;
648 int ret, i, argc;
649 char **argv;
650
4235b045
MH
651 pr_debug("Parsing kprobe_events: %s\n", cmd);
652 argv = argv_split(cmd, &argc);
146a1439
MH
653 if (!argv) {
654 pr_debug("Failed to split arguments.\n");
655 return -ENOMEM;
656 }
657 if (argc < 2) {
658 semantic_error("Too few probe arguments.\n");
659 ret = -ERANGE;
660 goto out;
661 }
4de189fe
MH
662
663 /* Scan event and group name. */
93aaa45a 664 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
4235b045
MH
665 &pr, (float *)(void *)&tev->group,
666 (float *)(void *)&tev->event);
146a1439
MH
667 if (ret != 3) {
668 semantic_error("Failed to parse event name: %s\n", argv[0]);
669 ret = -EINVAL;
670 goto out;
671 }
4235b045 672 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
4de189fe 673
4235b045 674 tp->retprobe = (pr == 'r');
4de189fe
MH
675
676 /* Scan function name and offset */
4235b045
MH
677 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
678 &tp->offset);
4de189fe 679 if (ret == 1)
4235b045 680 tp->offset = 0;
4de189fe 681
4235b045
MH
682 tev->nargs = argc - 2;
683 tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
684 for (i = 0; i < tev->nargs; i++) {
4de189fe
MH
685 p = strchr(argv[i + 2], '=');
686 if (p) /* We don't need which register is assigned. */
4235b045
MH
687 *p++ = '\0';
688 else
689 p = argv[i + 2];
690 tev->args[i].name = xstrdup(argv[i + 2]);
691 /* TODO: parse regs and offset */
692 tev->args[i].value = xstrdup(p);
4de189fe 693 }
146a1439
MH
694 ret = 0;
695out:
4de189fe 696 argv_free(argv);
146a1439 697 return ret;
4de189fe
MH
698}
699
7df2f329
MH
700/* Compose only probe arg */
701int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
702{
703 struct perf_probe_arg_field *field = pa->field;
704 int ret;
705 char *tmp = buf;
706
48481938
MH
707 if (pa->name && pa->var)
708 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
709 else
710 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
7df2f329
MH
711 if (ret <= 0)
712 goto error;
713 tmp += ret;
714 len -= ret;
715
716 while (field) {
717 ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
718 field->name);
719 if (ret <= 0)
720 goto error;
721 tmp += ret;
722 len -= ret;
723 field = field->next;
724 }
11a1ca35
MH
725
726 if (pa->type) {
727 ret = e_snprintf(tmp, len, ":%s", pa->type);
728 if (ret <= 0)
729 goto error;
730 tmp += ret;
731 len -= ret;
732 }
733
7df2f329
MH
734 return tmp - buf;
735error:
146a1439
MH
736 pr_debug("Failed to synthesize perf probe argument: %s",
737 strerror(-ret));
738 return ret;
7df2f329
MH
739}
740
4235b045
MH
741/* Compose only probe point (not argument) */
742static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
4de189fe 743{
fb1587d8
MH
744 char *buf, *tmp;
745 char offs[32] = "", line[32] = "", file[32] = "";
746 int ret, len;
4de189fe 747
4235b045 748 buf = xzalloc(MAX_CMDLEN);
4de189fe 749 if (pp->offset) {
fb1587d8 750 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
4de189fe
MH
751 if (ret <= 0)
752 goto error;
753 }
754 if (pp->line) {
fb1587d8
MH
755 ret = e_snprintf(line, 32, ":%d", pp->line);
756 if (ret <= 0)
757 goto error;
758 }
759 if (pp->file) {
760 len = strlen(pp->file) - 32;
761 if (len < 0)
762 len = 0;
763 tmp = strchr(pp->file + len, '/');
764 if (!tmp)
765 tmp = pp->file + len - 1;
766 ret = e_snprintf(file, 32, "@%s", tmp + 1);
4de189fe
MH
767 if (ret <= 0)
768 goto error;
769 }
770
771 if (pp->function)
fb1587d8
MH
772 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
773 offs, pp->retprobe ? "%return" : "", line,
774 file);
4de189fe 775 else
fb1587d8 776 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
4235b045
MH
777 if (ret <= 0)
778 goto error;
779
780 return buf;
7ef17aaf 781error:
146a1439
MH
782 pr_debug("Failed to synthesize perf probe point: %s",
783 strerror(-ret));
784 free(buf);
785 return NULL;
7ef17aaf
MH
786}
787
4235b045
MH
788#if 0
789char *synthesize_perf_probe_command(struct perf_probe_event *pev)
7ef17aaf
MH
790{
791 char *buf;
792 int i, len, ret;
793
4235b045
MH
794 buf = synthesize_perf_probe_point(&pev->point);
795 if (!buf)
796 return NULL;
4de189fe 797
4235b045
MH
798 len = strlen(buf);
799 for (i = 0; i < pev->nargs; i++) {
4de189fe 800 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
4235b045
MH
801 pev->args[i].name);
802 if (ret <= 0) {
803 free(buf);
804 return NULL;
805 }
4de189fe
MH
806 len += ret;
807 }
4de189fe 808
4235b045
MH
809 return buf;
810}
811#endif
812
813static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
814 char **buf, size_t *buflen,
815 int depth)
816{
817 int ret;
818 if (ref->next) {
819 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
820 buflen, depth + 1);
821 if (depth < 0)
822 goto out;
823 }
824
825 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
826 if (ret < 0)
827 depth = ret;
828 else {
829 *buf += ret;
830 *buflen -= ret;
831 }
832out:
833 return depth;
4de189fe 834
4de189fe
MH
835}
836
4235b045
MH
837static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
838 char *buf, size_t buflen)
50656eec 839{
4235b045
MH
840 int ret, depth = 0;
841 char *tmp = buf;
842
843 /* Argument name or separator */
844 if (arg->name)
845 ret = e_snprintf(buf, buflen, " %s=", arg->name);
846 else
847 ret = e_snprintf(buf, buflen, " ");
848 if (ret < 0)
849 return ret;
850 buf += ret;
851 buflen -= ret;
852
853 /* Dereferencing arguments */
854 if (arg->ref) {
855 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
856 &buflen, 1);
857 if (depth < 0)
858 return depth;
859 }
860
861 /* Print argument value */
862 ret = e_snprintf(buf, buflen, "%s", arg->value);
863 if (ret < 0)
864 return ret;
865 buf += ret;
866 buflen -= ret;
867
868 /* Closing */
869 while (depth--) {
870 ret = e_snprintf(buf, buflen, ")");
871 if (ret < 0)
872 return ret;
873 buf += ret;
874 buflen -= ret;
875 }
4984912e
MH
876 /* Print argument type */
877 if (arg->type) {
878 ret = e_snprintf(buf, buflen, ":%s", arg->type);
879 if (ret <= 0)
880 return ret;
881 buf += ret;
882 }
4235b045
MH
883
884 return buf - tmp;
885}
886
887char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
888{
889 struct kprobe_trace_point *tp = &tev->point;
50656eec
MH
890 char *buf;
891 int i, len, ret;
892
4235b045
MH
893 buf = xzalloc(MAX_CMDLEN);
894 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
895 tp->retprobe ? 'r' : 'p',
896 tev->group, tev->event,
897 tp->symbol, tp->offset);
898 if (len <= 0)
50656eec 899 goto error;
50656eec 900
4235b045
MH
901 for (i = 0; i < tev->nargs; i++) {
902 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
903 MAX_CMDLEN - len);
4de189fe 904 if (ret <= 0)
50656eec
MH
905 goto error;
906 len += ret;
907 }
50656eec 908
4235b045 909 return buf;
50656eec 910error:
4235b045
MH
911 free(buf);
912 return NULL;
913}
50656eec 914
146a1439
MH
915int convert_to_perf_probe_event(struct kprobe_trace_event *tev,
916 struct perf_probe_event *pev)
4235b045
MH
917{
918 char buf[64];
146a1439 919 int i, ret;
4235b045 920
4b4da7f7 921 /* Convert event/group name */
fb1587d8
MH
922 pev->event = xstrdup(tev->event);
923 pev->group = xstrdup(tev->group);
924
4b4da7f7 925 /* Convert trace_point to probe_point */
146a1439
MH
926 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
927 if (ret < 0)
928 return ret;
4b4da7f7 929
4235b045
MH
930 /* Convert trace_arg to probe_arg */
931 pev->nargs = tev->nargs;
932 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
146a1439 933 for (i = 0; i < tev->nargs && ret >= 0; i++)
4235b045
MH
934 if (tev->args[i].name)
935 pev->args[i].name = xstrdup(tev->args[i].name);
936 else {
146a1439
MH
937 ret = synthesize_kprobe_trace_arg(&tev->args[i],
938 buf, 64);
4235b045
MH
939 pev->args[i].name = xstrdup(buf);
940 }
146a1439
MH
941
942 if (ret < 0)
943 clear_perf_probe_event(pev);
944
945 return ret;
4235b045
MH
946}
947
948void clear_perf_probe_event(struct perf_probe_event *pev)
949{
950 struct perf_probe_point *pp = &pev->point;
7df2f329 951 struct perf_probe_arg_field *field, *next;
4235b045
MH
952 int i;
953
954 if (pev->event)
955 free(pev->event);
956 if (pev->group)
957 free(pev->group);
958 if (pp->file)
959 free(pp->file);
960 if (pp->function)
961 free(pp->function);
962 if (pp->lazy_line)
963 free(pp->lazy_line);
7df2f329 964 for (i = 0; i < pev->nargs; i++) {
4235b045
MH
965 if (pev->args[i].name)
966 free(pev->args[i].name);
48481938
MH
967 if (pev->args[i].var)
968 free(pev->args[i].var);
11a1ca35
MH
969 if (pev->args[i].type)
970 free(pev->args[i].type);
7df2f329
MH
971 field = pev->args[i].field;
972 while (field) {
973 next = field->next;
974 if (field->name)
975 free(field->name);
976 free(field);
977 field = next;
978 }
979 }
4235b045
MH
980 if (pev->args)
981 free(pev->args);
982 memset(pev, 0, sizeof(*pev));
983}
984
985void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
986{
987 struct kprobe_trace_arg_ref *ref, *next;
988 int i;
989
990 if (tev->event)
991 free(tev->event);
992 if (tev->group)
993 free(tev->group);
994 if (tev->point.symbol)
995 free(tev->point.symbol);
996 for (i = 0; i < tev->nargs; i++) {
997 if (tev->args[i].name)
998 free(tev->args[i].name);
999 if (tev->args[i].value)
1000 free(tev->args[i].value);
4984912e
MH
1001 if (tev->args[i].type)
1002 free(tev->args[i].type);
4235b045
MH
1003 ref = tev->args[i].ref;
1004 while (ref) {
1005 next = ref->next;
1006 free(ref);
1007 ref = next;
1008 }
1009 }
1010 if (tev->args)
1011 free(tev->args);
1012 memset(tev, 0, sizeof(*tev));
50656eec
MH
1013}
1014
f4d7da49 1015static int open_kprobe_events(bool readwrite)
4de189fe
MH
1016{
1017 char buf[PATH_MAX];
1018 int ret;
1019
1020 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
146a1439
MH
1021 if (ret >= 0) {
1022 if (readwrite && !probe_event_dry_run)
1023 ret = open(buf, O_RDWR, O_APPEND);
1024 else
1025 ret = open(buf, O_RDONLY, 0);
1026 }
f4d7da49 1027
4de189fe
MH
1028 if (ret < 0) {
1029 if (errno == ENOENT)
146a1439
MH
1030 pr_warning("kprobe_events file does not exist - please"
1031 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
4de189fe 1032 else
146a1439
MH
1033 pr_warning("Failed to open kprobe_events file: %s\n",
1034 strerror(errno));
4de189fe
MH
1035 }
1036 return ret;
1037}
1038
1039/* Get raw string list of current kprobe_events */
4235b045 1040static struct strlist *get_kprobe_trace_command_rawlist(int fd)
4de189fe
MH
1041{
1042 int ret, idx;
1043 FILE *fp;
1044 char buf[MAX_CMDLEN];
1045 char *p;
1046 struct strlist *sl;
1047
1048 sl = strlist__new(true, NULL);
1049
1050 fp = fdopen(dup(fd), "r");
1051 while (!feof(fp)) {
1052 p = fgets(buf, MAX_CMDLEN, fp);
1053 if (!p)
1054 break;
1055
1056 idx = strlen(p) - 1;
1057 if (p[idx] == '\n')
1058 p[idx] = '\0';
1059 ret = strlist__add(sl, buf);
146a1439
MH
1060 if (ret < 0) {
1061 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1062 strlist__delete(sl);
1063 return NULL;
1064 }
4de189fe
MH
1065 }
1066 fclose(fp);
1067
1068 return sl;
1069}
1070
278498d4 1071/* Show an event */
146a1439 1072static int show_perf_probe_event(struct perf_probe_event *pev)
278498d4 1073{
7e990a51 1074 int i, ret;
278498d4 1075 char buf[128];
4235b045 1076 char *place;
278498d4 1077
4235b045
MH
1078 /* Synthesize only event probe point */
1079 place = synthesize_perf_probe_point(&pev->point);
146a1439
MH
1080 if (!place)
1081 return -EINVAL;
4235b045
MH
1082
1083 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
7e990a51 1084 if (ret < 0)
146a1439
MH
1085 return ret;
1086
fb1587d8 1087 printf(" %-20s (on %s", buf, place);
278498d4 1088
4235b045 1089 if (pev->nargs > 0) {
278498d4 1090 printf(" with");
7df2f329 1091 for (i = 0; i < pev->nargs; i++) {
146a1439
MH
1092 ret = synthesize_perf_probe_arg(&pev->args[i],
1093 buf, 128);
1094 if (ret < 0)
1095 break;
7df2f329
MH
1096 printf(" %s", buf);
1097 }
278498d4
MH
1098 }
1099 printf(")\n");
4235b045 1100 free(place);
146a1439 1101 return ret;
278498d4
MH
1102}
1103
4de189fe 1104/* List up current perf-probe events */
146a1439 1105int show_perf_probe_events(void)
4de189fe 1106{
146a1439 1107 int fd, ret;
4235b045
MH
1108 struct kprobe_trace_event tev;
1109 struct perf_probe_event pev;
4de189fe
MH
1110 struct strlist *rawlist;
1111 struct str_node *ent;
1112
72041334 1113 setup_pager();
146a1439
MH
1114 ret = init_vmlinux();
1115 if (ret < 0)
1116 return ret;
4235b045
MH
1117
1118 memset(&tev, 0, sizeof(tev));
1119 memset(&pev, 0, sizeof(pev));
72041334 1120
f4d7da49 1121 fd = open_kprobe_events(false);
146a1439
MH
1122 if (fd < 0)
1123 return fd;
1124
4235b045 1125 rawlist = get_kprobe_trace_command_rawlist(fd);
4de189fe 1126 close(fd);
146a1439
MH
1127 if (!rawlist)
1128 return -ENOENT;
4de189fe 1129
adf365f4 1130 strlist__for_each(ent, rawlist) {
146a1439
MH
1131 ret = parse_kprobe_trace_command(ent->s, &tev);
1132 if (ret >= 0) {
1133 ret = convert_to_perf_probe_event(&tev, &pev);
1134 if (ret >= 0)
1135 ret = show_perf_probe_event(&pev);
1136 }
4235b045
MH
1137 clear_perf_probe_event(&pev);
1138 clear_kprobe_trace_event(&tev);
146a1439
MH
1139 if (ret < 0)
1140 break;
4de189fe 1141 }
4de189fe 1142 strlist__delete(rawlist);
146a1439
MH
1143
1144 return ret;
4de189fe
MH
1145}
1146
b498ce1f 1147/* Get current perf-probe event names */
4235b045 1148static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
b498ce1f 1149{
fa28244d 1150 char buf[128];
b498ce1f
MH
1151 struct strlist *sl, *rawlist;
1152 struct str_node *ent;
4235b045 1153 struct kprobe_trace_event tev;
146a1439 1154 int ret = 0;
b498ce1f 1155
4235b045 1156 memset(&tev, 0, sizeof(tev));
b498ce1f 1157
4235b045 1158 rawlist = get_kprobe_trace_command_rawlist(fd);
e1d2017b 1159 sl = strlist__new(true, NULL);
adf365f4 1160 strlist__for_each(ent, rawlist) {
146a1439
MH
1161 ret = parse_kprobe_trace_command(ent->s, &tev);
1162 if (ret < 0)
1163 break;
fa28244d 1164 if (include_group) {
146a1439
MH
1165 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1166 tev.event);
1167 if (ret >= 0)
1168 ret = strlist__add(sl, buf);
fa28244d 1169 } else
146a1439 1170 ret = strlist__add(sl, tev.event);
4235b045 1171 clear_kprobe_trace_event(&tev);
146a1439
MH
1172 if (ret < 0)
1173 break;
b498ce1f 1174 }
b498ce1f
MH
1175 strlist__delete(rawlist);
1176
146a1439
MH
1177 if (ret < 0) {
1178 strlist__delete(sl);
1179 return NULL;
1180 }
b498ce1f
MH
1181 return sl;
1182}
1183
146a1439 1184static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
50656eec
MH
1185{
1186 int ret;
4235b045 1187 char *buf = synthesize_kprobe_trace_command(tev);
50656eec 1188
146a1439
MH
1189 if (!buf) {
1190 pr_debug("Failed to synthesize kprobe trace event.\n");
1191 return -EINVAL;
1192 }
1193
fa28244d 1194 pr_debug("Writing event: %s\n", buf);
f4d7da49
MH
1195 if (!probe_event_dry_run) {
1196 ret = write(fd, buf, strlen(buf));
1197 if (ret <= 0)
146a1439
MH
1198 pr_warning("Failed to write event: %s\n",
1199 strerror(errno));
f4d7da49 1200 }
4235b045 1201 free(buf);
146a1439 1202 return ret;
50656eec
MH
1203}
1204
146a1439
MH
1205static int get_new_event_name(char *buf, size_t len, const char *base,
1206 struct strlist *namelist, bool allow_suffix)
b498ce1f
MH
1207{
1208 int i, ret;
17f88fcd
MH
1209
1210 /* Try no suffix */
1211 ret = e_snprintf(buf, len, "%s", base);
146a1439
MH
1212 if (ret < 0) {
1213 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1214 return ret;
1215 }
17f88fcd 1216 if (!strlist__has_entry(namelist, buf))
146a1439 1217 return 0;
17f88fcd 1218
d761b08b
MH
1219 if (!allow_suffix) {
1220 pr_warning("Error: event \"%s\" already exists. "
1221 "(Use -f to force duplicates.)\n", base);
146a1439 1222 return -EEXIST;
d761b08b
MH
1223 }
1224
17f88fcd
MH
1225 /* Try to add suffix */
1226 for (i = 1; i < MAX_EVENT_INDEX; i++) {
b498ce1f 1227 ret = e_snprintf(buf, len, "%s_%d", base, i);
146a1439
MH
1228 if (ret < 0) {
1229 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1230 return ret;
1231 }
b498ce1f
MH
1232 if (!strlist__has_entry(namelist, buf))
1233 break;
1234 }
146a1439
MH
1235 if (i == MAX_EVENT_INDEX) {
1236 pr_warning("Too many events are on the same function.\n");
1237 ret = -ERANGE;
1238 }
1239
1240 return ret;
b498ce1f
MH
1241}
1242
146a1439
MH
1243static int __add_kprobe_trace_events(struct perf_probe_event *pev,
1244 struct kprobe_trace_event *tevs,
1245 int ntevs, bool allow_suffix)
50656eec 1246{
146a1439 1247 int i, fd, ret;
55632770 1248 struct kprobe_trace_event *tev = NULL;
4235b045
MH
1249 char buf[64];
1250 const char *event, *group;
b498ce1f 1251 struct strlist *namelist;
50656eec 1252
f4d7da49 1253 fd = open_kprobe_events(true);
146a1439
MH
1254 if (fd < 0)
1255 return fd;
b498ce1f 1256 /* Get current event names */
4235b045 1257 namelist = get_kprobe_trace_event_names(fd, false);
146a1439
MH
1258 if (!namelist) {
1259 pr_debug("Failed to get current event list.\n");
1260 return -EIO;
1261 }
4235b045 1262
146a1439 1263 ret = 0;
4235b045 1264 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
146a1439 1265 for (i = 0; i < ntevs && ret >= 0; i++) {
4235b045
MH
1266 tev = &tevs[i];
1267 if (pev->event)
1268 event = pev->event;
1269 else
1270 if (pev->point.function)
1271 event = pev->point.function;
1272 else
1273 event = tev->point.symbol;
1274 if (pev->group)
1275 group = pev->group;
1276 else
1277 group = PERFPROBE_GROUP;
1278
1279 /* Get an unused new event name */
146a1439
MH
1280 ret = get_new_event_name(buf, 64, event,
1281 namelist, allow_suffix);
1282 if (ret < 0)
1283 break;
4235b045
MH
1284 event = buf;
1285
1286 tev->event = xstrdup(event);
1287 tev->group = xstrdup(group);
146a1439
MH
1288 ret = write_kprobe_trace_event(fd, tev);
1289 if (ret < 0)
1290 break;
4235b045
MH
1291 /* Add added event name to namelist */
1292 strlist__add(namelist, event);
1293
1294 /* Trick here - save current event/group */
1295 event = pev->event;
1296 group = pev->group;
1297 pev->event = tev->event;
1298 pev->group = tev->group;
1299 show_perf_probe_event(pev);
1300 /* Trick here - restore current event/group */
1301 pev->event = (char *)event;
1302 pev->group = (char *)group;
1303
1304 /*
1305 * Probes after the first probe which comes from same
1306 * user input are always allowed to add suffix, because
1307 * there might be several addresses corresponding to
1308 * one code line.
1309 */
1310 allow_suffix = true;
50656eec 1311 }
146a1439
MH
1312
1313 if (ret >= 0) {
1314 /* Show how to use the event. */
1315 printf("\nYou can now use it on all perf tools, such as:\n\n");
1316 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1317 tev->event);
1318 }
a9b495b0 1319
e1d2017b 1320 strlist__delete(namelist);
50656eec 1321 close(fd);
146a1439 1322 return ret;
50656eec 1323}
fa28244d 1324
4235b045
MH
1325static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
1326 struct kprobe_trace_event **tevs)
e0faa8d3
MH
1327{
1328 struct symbol *sym;
4235b045
MH
1329 int ntevs = 0, i;
1330 struct kprobe_trace_event *tev;
1331
4b4da7f7
MH
1332 /* Convert perf_probe_event with debuginfo */
1333 ntevs = try_to_find_kprobe_trace_events(pev, tevs);
146a1439 1334 if (ntevs != 0)
4b4da7f7 1335 return ntevs;
e0faa8d3 1336
4235b045
MH
1337 /* Allocate trace event buffer */
1338 ntevs = 1;
1339 tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event));
1340
1341 /* Copy parameters */
1342 tev->point.symbol = xstrdup(pev->point.function);
1343 tev->point.offset = pev->point.offset;
1344 tev->nargs = pev->nargs;
1345 if (tev->nargs) {
1346 tev->args = xzalloc(sizeof(struct kprobe_trace_arg)
1347 * tev->nargs);
48481938
MH
1348 for (i = 0; i < tev->nargs; i++) {
1349 if (pev->args[i].name)
1350 tev->args[i].name = xstrdup(pev->args[i].name);
1351 tev->args[i].value = xstrdup(pev->args[i].var);
11a1ca35
MH
1352 if (pev->args[i].type)
1353 tev->args[i].type = xstrdup(pev->args[i].type);
48481938 1354 }
4235b045
MH
1355 }
1356
1357 /* Currently just checking function name from symbol map */
1358 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
1359 tev->point.symbol, NULL);
146a1439
MH
1360 if (!sym) {
1361 pr_warning("Kernel symbol \'%s\' not found.\n",
1362 tev->point.symbol);
1363 return -ENOENT;
1364 }
4235b045
MH
1365 return ntevs;
1366}
1367
1368struct __event_package {
1369 struct perf_probe_event *pev;
1370 struct kprobe_trace_event *tevs;
1371 int ntevs;
1372};
1373
146a1439
MH
1374int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1375 bool force_add)
4235b045 1376{
146a1439 1377 int i, j, ret;
4235b045
MH
1378 struct __event_package *pkgs;
1379
1380 pkgs = xzalloc(sizeof(struct __event_package) * npevs);
1381
1382 /* Init vmlinux path */
146a1439
MH
1383 ret = init_vmlinux();
1384 if (ret < 0)
1385 return ret;
4235b045
MH
1386
1387 /* Loop 1: convert all events */
1388 for (i = 0; i < npevs; i++) {
1389 pkgs[i].pev = &pevs[i];
1390 /* Convert with or without debuginfo */
146a1439
MH
1391 ret = convert_to_kprobe_trace_events(pkgs[i].pev,
1392 &pkgs[i].tevs);
1393 if (ret < 0)
1394 goto end;
1395 pkgs[i].ntevs = ret;
e0faa8d3
MH
1396 }
1397
4235b045 1398 /* Loop 2: add all events */
146a1439
MH
1399 for (i = 0; i < npevs && ret >= 0; i++)
1400 ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1401 pkgs[i].ntevs, force_add);
1402end:
1403 /* Loop 3: cleanup trace events */
4235b045 1404 for (i = 0; i < npevs; i++)
146a1439
MH
1405 for (j = 0; j < pkgs[i].ntevs; j++)
1406 clear_kprobe_trace_event(&pkgs[i].tevs[j]);
1407
1408 return ret;
e0faa8d3
MH
1409}
1410
146a1439 1411static int __del_trace_kprobe_event(int fd, struct str_node *ent)
bbbb521b
MH
1412{
1413 char *p;
1414 char buf[128];
4235b045 1415 int ret;
bbbb521b
MH
1416
1417 /* Convert from perf-probe event to trace-kprobe event */
146a1439
MH
1418 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1419 if (ret < 0)
1420 goto error;
1421
bbbb521b 1422 p = strchr(buf + 2, ':');
146a1439
MH
1423 if (!p) {
1424 pr_debug("Internal error: %s should have ':' but not.\n",
1425 ent->s);
1426 ret = -ENOTSUP;
1427 goto error;
1428 }
bbbb521b
MH
1429 *p = '/';
1430
4235b045
MH
1431 pr_debug("Writing event: %s\n", buf);
1432 ret = write(fd, buf, strlen(buf));
146a1439
MH
1433 if (ret < 0)
1434 goto error;
1435
bbbb521b 1436 printf("Remove event: %s\n", ent->s);
146a1439
MH
1437 return 0;
1438error:
1439 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1440 return ret;
bbbb521b
MH
1441}
1442
146a1439
MH
1443static int del_trace_kprobe_event(int fd, const char *group,
1444 const char *event, struct strlist *namelist)
fa28244d
MH
1445{
1446 char buf[128];
bbbb521b 1447 struct str_node *ent, *n;
146a1439 1448 int found = 0, ret = 0;
fa28244d 1449
146a1439
MH
1450 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1451 if (ret < 0) {
1452 pr_err("Failed to copy event.");
1453 return ret;
1454 }
fa28244d 1455
bbbb521b
MH
1456 if (strpbrk(buf, "*?")) { /* Glob-exp */
1457 strlist__for_each_safe(ent, n, namelist)
1458 if (strglobmatch(ent->s, buf)) {
1459 found++;
146a1439
MH
1460 ret = __del_trace_kprobe_event(fd, ent);
1461 if (ret < 0)
1462 break;
bbbb521b
MH
1463 strlist__remove(namelist, ent);
1464 }
1465 } else {
1466 ent = strlist__find(namelist, buf);
1467 if (ent) {
1468 found++;
146a1439
MH
1469 ret = __del_trace_kprobe_event(fd, ent);
1470 if (ret >= 0)
1471 strlist__remove(namelist, ent);
bbbb521b
MH
1472 }
1473 }
146a1439
MH
1474 if (found == 0 && ret >= 0)
1475 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1476
1477 return ret;
fa28244d
MH
1478}
1479
146a1439 1480int del_perf_probe_events(struct strlist *dellist)
fa28244d 1481{
146a1439 1482 int fd, ret = 0;
fa28244d
MH
1483 const char *group, *event;
1484 char *p, *str;
1485 struct str_node *ent;
1486 struct strlist *namelist;
1487
f4d7da49 1488 fd = open_kprobe_events(true);
146a1439
MH
1489 if (fd < 0)
1490 return fd;
1491
fa28244d 1492 /* Get current event names */
4235b045 1493 namelist = get_kprobe_trace_event_names(fd, true);
146a1439
MH
1494 if (namelist == NULL)
1495 return -EINVAL;
fa28244d 1496
adf365f4 1497 strlist__for_each(ent, dellist) {
31facc5f 1498 str = xstrdup(ent->s);
bbbb521b 1499 pr_debug("Parsing: %s\n", str);
fa28244d
MH
1500 p = strchr(str, ':');
1501 if (p) {
1502 group = str;
1503 *p = '\0';
1504 event = p + 1;
1505 } else {
bbbb521b 1506 group = "*";
fa28244d
MH
1507 event = str;
1508 }
bbbb521b 1509 pr_debug("Group: %s, Event: %s\n", group, event);
146a1439 1510 ret = del_trace_kprobe_event(fd, group, event, namelist);
fa28244d 1511 free(str);
146a1439
MH
1512 if (ret < 0)
1513 break;
fa28244d
MH
1514 }
1515 strlist__delete(namelist);
1516 close(fd);
146a1439
MH
1517
1518 return ret;
fa28244d
MH
1519}
1520