perf probe: List probes with line number and file name
[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"
50656eec
MH
45#include "parse-events.h" /* For debugfs_path */
46#include "probe-event.h"
4235b045 47#include "probe-finder.h"
50656eec
MH
48
49#define MAX_CMDLEN 256
50#define MAX_PROBE_ARGS 128
51#define PERFPROBE_GROUP "probe"
52
f4d7da49
MH
53bool probe_event_dry_run; /* Dry run flag */
54
50656eec
MH
55#define semantic_error(msg ...) die("Semantic error :" msg)
56
4de189fe 57/* If there is no space to write, returns -E2BIG. */
84988450
MH
58static int e_snprintf(char *str, size_t size, const char *format, ...)
59 __attribute__((format(printf, 3, 4)));
60
4de189fe
MH
61static int e_snprintf(char *str, size_t size, const char *format, ...)
62{
63 int ret;
64 va_list ap;
65 va_start(ap, format);
66 ret = vsnprintf(str, size, format, ap);
67 va_end(ap);
68 if (ret >= (int)size)
69 ret = -E2BIG;
70 return ret;
71}
72
e0faa8d3
MH
73static struct map_groups kmap_groups;
74static struct map *kmaps[MAP__NR_TYPES];
75
76/* Initialize symbol maps for vmlinux */
77static void init_vmlinux(void)
78{
79 symbol_conf.sort_by_name = true;
80 if (symbol_conf.vmlinux_name == NULL)
81 symbol_conf.try_vmlinux_path = true;
82 else
83 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
84 if (symbol__init() < 0)
85 die("Failed to init symbol map.");
86
87 map_groups__init(&kmap_groups);
88 if (map_groups__create_kernel_maps(&kmap_groups, kmaps) < 0)
89 die("Failed to create kernel maps.");
90}
91
92#ifndef NO_DWARF_SUPPORT
93static int open_vmlinux(void)
94{
95 if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) {
96 pr_debug("Failed to load kernel map.\n");
97 return -EINVAL;
98 }
99 pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name);
100 return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
101}
102#endif
103
631c9def
MH
104void parse_line_range_desc(const char *arg, struct line_range *lr)
105{
106 const char *ptr;
107 char *tmp;
108 /*
109 * <Syntax>
110 * SRC:SLN[+NUM|-ELN]
111 * FUNC[:SLN[+NUM|-ELN]]
112 */
113 ptr = strchr(arg, ':');
114 if (ptr) {
115 lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0);
116 if (*tmp == '+')
117 lr->end = lr->start + (unsigned int)strtoul(tmp + 1,
118 &tmp, 0);
119 else if (*tmp == '-')
120 lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0);
121 else
122 lr->end = 0;
123 pr_debug("Line range is %u to %u\n", lr->start, lr->end);
124 if (lr->end && lr->start > lr->end)
125 semantic_error("Start line must be smaller"
126 " than end line.");
127 if (*tmp != '\0')
128 semantic_error("Tailing with invalid character '%d'.",
129 *tmp);
31facc5f 130 tmp = xstrndup(arg, (ptr - arg));
631c9def 131 } else
31facc5f 132 tmp = xstrdup(arg);
631c9def
MH
133
134 if (strchr(tmp, '.'))
135 lr->file = tmp;
136 else
137 lr->function = tmp;
138}
139
b7702a21
MH
140/* Check the name is good for event/group */
141static bool check_event_name(const char *name)
142{
143 if (!isalpha(*name) && *name != '_')
144 return false;
145 while (*++name != '\0') {
146 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
147 return false;
148 }
149 return true;
150}
151
50656eec 152/* Parse probepoint definition. */
4235b045 153static void parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
50656eec 154{
4235b045 155 struct perf_probe_point *pp = &pev->point;
50656eec
MH
156 char *ptr, *tmp;
157 char c, nc = 0;
158 /*
159 * <Syntax>
2a9c8c36
MH
160 * perf probe [EVENT=]SRC[:LN|;PTN]
161 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
af663d75
MH
162 *
163 * TODO:Group name support
50656eec
MH
164 */
165
2a9c8c36
MH
166 ptr = strpbrk(arg, ";=@+%");
167 if (ptr && *ptr == '=') { /* Event name */
af663d75
MH
168 *ptr = '\0';
169 tmp = ptr + 1;
170 ptr = strchr(arg, ':');
171 if (ptr) /* Group name is not supported yet. */
172 semantic_error("Group name is not supported yet.");
b7702a21
MH
173 if (!check_event_name(arg))
174 semantic_error("%s is bad for event name -it must "
175 "follow C symbol-naming rule.", arg);
4235b045
MH
176 pev->event = xstrdup(arg);
177 pev->group = NULL;
af663d75
MH
178 arg = tmp;
179 }
180
2a9c8c36 181 ptr = strpbrk(arg, ";:+@%");
50656eec
MH
182 if (ptr) {
183 nc = *ptr;
184 *ptr++ = '\0';
185 }
186
187 /* Check arg is function or file and copy it */
188 if (strchr(arg, '.')) /* File */
31facc5f 189 pp->file = xstrdup(arg);
50656eec 190 else /* Function */
31facc5f 191 pp->function = xstrdup(arg);
50656eec
MH
192
193 /* Parse other options */
194 while (ptr) {
195 arg = ptr;
196 c = nc;
2a9c8c36 197 if (c == ';') { /* Lazy pattern must be the last part */
31facc5f 198 pp->lazy_line = xstrdup(arg);
2a9c8c36
MH
199 break;
200 }
201 ptr = strpbrk(arg, ";:+@%");
50656eec
MH
202 if (ptr) {
203 nc = *ptr;
204 *ptr++ = '\0';
205 }
206 switch (c) {
207 case ':': /* Line number */
208 pp->line = strtoul(arg, &tmp, 0);
209 if (*tmp != '\0')
2a9c8c36
MH
210 semantic_error("There is non-digit char"
211 " in line number.");
50656eec
MH
212 break;
213 case '+': /* Byte offset from a symbol */
214 pp->offset = strtoul(arg, &tmp, 0);
215 if (*tmp != '\0')
2a9c8c36 216 semantic_error("There is non-digit character"
50656eec
MH
217 " in offset.");
218 break;
219 case '@': /* File name */
220 if (pp->file)
221 semantic_error("SRC@SRC is not allowed.");
31facc5f 222 pp->file = xstrdup(arg);
50656eec
MH
223 break;
224 case '%': /* Probe places */
225 if (strcmp(arg, "return") == 0) {
226 pp->retprobe = 1;
227 } else /* Others not supported yet */
228 semantic_error("%%%s is not supported.", arg);
229 break;
230 default:
231 DIE_IF("Program has a bug.");
232 break;
233 }
234 }
235
236 /* Exclusion check */
2a9c8c36
MH
237 if (pp->lazy_line && pp->line)
238 semantic_error("Lazy pattern can't be used with line number.");
239
240 if (pp->lazy_line && pp->offset)
241 semantic_error("Lazy pattern can't be used with offset.");
242
50656eec
MH
243 if (pp->line && pp->offset)
244 semantic_error("Offset can't be used with line number.");
245
2a9c8c36
MH
246 if (!pp->line && !pp->lazy_line && pp->file && !pp->function)
247 semantic_error("File always requires line number or "
248 "lazy pattern.");
50656eec
MH
249
250 if (pp->offset && !pp->function)
251 semantic_error("Offset requires an entry function.");
252
253 if (pp->retprobe && !pp->function)
254 semantic_error("Return probe requires an entry function.");
255
2a9c8c36
MH
256 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe)
257 semantic_error("Offset/Line/Lazy pattern can't be used with "
258 "return probe.");
50656eec 259
4235b045 260 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
2a9c8c36
MH
261 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
262 pp->lazy_line);
50656eec
MH
263}
264
4235b045
MH
265/* Parse perf-probe event command */
266void parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
50656eec 267{
e1c01d61 268 char **argv;
fac13fd5
MH
269 int argc, i;
270
4235b045 271 argv = argv_split(cmd, &argc);
e1c01d61
MH
272 if (!argv)
273 die("argv_split failed.");
274 if (argc > MAX_PROBE_ARGS + 1)
275 semantic_error("Too many arguments");
50656eec
MH
276
277 /* Parse probe point */
4235b045 278 parse_perf_probe_point(argv[0], pev);
50656eec 279
e1c01d61 280 /* Copy arguments and ensure return probe has no C argument */
4235b045
MH
281 pev->nargs = argc - 1;
282 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
283 for (i = 0; i < pev->nargs; i++) {
284 pev->args[i].name = xstrdup(argv[i + 1]);
285 if (is_c_varname(pev->args[i].name) && pev->point.retprobe)
286 semantic_error("You can't specify local variable for"
287 " kretprobe");
e1c01d61 288 }
50656eec 289
e1c01d61 290 argv_free(argv);
50656eec
MH
291}
292
4235b045
MH
293/* Return true if this perf_probe_event requires debuginfo */
294bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
295{
296 int i;
297
298 if (pev->point.file || pev->point.line || pev->point.lazy_line)
299 return true;
300
301 for (i = 0; i < pev->nargs; i++)
302 if (is_c_varname(pev->args[i].name))
303 return true;
304
305 return false;
306}
307
4de189fe 308/* Parse kprobe_events event into struct probe_point */
4235b045 309void parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
4de189fe 310{
4235b045 311 struct kprobe_trace_point *tp = &tev->point;
4de189fe
MH
312 char pr;
313 char *p;
314 int ret, i, argc;
315 char **argv;
316
4235b045
MH
317 pr_debug("Parsing kprobe_events: %s\n", cmd);
318 argv = argv_split(cmd, &argc);
4de189fe
MH
319 if (!argv)
320 die("argv_split failed.");
321 if (argc < 2)
322 semantic_error("Too less arguments.");
323
324 /* Scan event and group name. */
93aaa45a 325 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
4235b045
MH
326 &pr, (float *)(void *)&tev->group,
327 (float *)(void *)&tev->event);
4de189fe
MH
328 if (ret != 3)
329 semantic_error("Failed to parse event name: %s", argv[0]);
4235b045 330 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
4de189fe 331
4235b045 332 tp->retprobe = (pr == 'r');
4de189fe
MH
333
334 /* Scan function name and offset */
4235b045
MH
335 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
336 &tp->offset);
4de189fe 337 if (ret == 1)
4235b045 338 tp->offset = 0;
4de189fe 339
4235b045
MH
340 tev->nargs = argc - 2;
341 tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
342 for (i = 0; i < tev->nargs; i++) {
4de189fe
MH
343 p = strchr(argv[i + 2], '=');
344 if (p) /* We don't need which register is assigned. */
4235b045
MH
345 *p++ = '\0';
346 else
347 p = argv[i + 2];
348 tev->args[i].name = xstrdup(argv[i + 2]);
349 /* TODO: parse regs and offset */
350 tev->args[i].value = xstrdup(p);
4de189fe
MH
351 }
352
4de189fe
MH
353 argv_free(argv);
354}
355
4235b045
MH
356/* Compose only probe point (not argument) */
357static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
4de189fe 358{
fb1587d8
MH
359 char *buf, *tmp;
360 char offs[32] = "", line[32] = "", file[32] = "";
361 int ret, len;
4de189fe 362
4235b045 363 buf = xzalloc(MAX_CMDLEN);
4de189fe 364 if (pp->offset) {
fb1587d8 365 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
4de189fe
MH
366 if (ret <= 0)
367 goto error;
368 }
369 if (pp->line) {
fb1587d8
MH
370 ret = e_snprintf(line, 32, ":%d", pp->line);
371 if (ret <= 0)
372 goto error;
373 }
374 if (pp->file) {
375 len = strlen(pp->file) - 32;
376 if (len < 0)
377 len = 0;
378 tmp = strchr(pp->file + len, '/');
379 if (!tmp)
380 tmp = pp->file + len - 1;
381 ret = e_snprintf(file, 32, "@%s", tmp + 1);
4de189fe
MH
382 if (ret <= 0)
383 goto error;
384 }
385
386 if (pp->function)
fb1587d8
MH
387 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
388 offs, pp->retprobe ? "%return" : "", line,
389 file);
4de189fe 390 else
fb1587d8 391 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
4235b045
MH
392 if (ret <= 0)
393 goto error;
394
395 return buf;
7ef17aaf 396error:
4235b045 397 die("Failed to synthesize perf probe point: %s", strerror(-ret));
7ef17aaf
MH
398}
399
4235b045
MH
400#if 0
401char *synthesize_perf_probe_command(struct perf_probe_event *pev)
7ef17aaf
MH
402{
403 char *buf;
404 int i, len, ret;
405
4235b045
MH
406 buf = synthesize_perf_probe_point(&pev->point);
407 if (!buf)
408 return NULL;
4de189fe 409
4235b045
MH
410 len = strlen(buf);
411 for (i = 0; i < pev->nargs; i++) {
4de189fe 412 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
4235b045
MH
413 pev->args[i].name);
414 if (ret <= 0) {
415 free(buf);
416 return NULL;
417 }
4de189fe
MH
418 len += ret;
419 }
4de189fe 420
4235b045
MH
421 return buf;
422}
423#endif
424
425static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
426 char **buf, size_t *buflen,
427 int depth)
428{
429 int ret;
430 if (ref->next) {
431 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
432 buflen, depth + 1);
433 if (depth < 0)
434 goto out;
435 }
436
437 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
438 if (ret < 0)
439 depth = ret;
440 else {
441 *buf += ret;
442 *buflen -= ret;
443 }
444out:
445 return depth;
4de189fe 446
4de189fe
MH
447}
448
4235b045
MH
449static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
450 char *buf, size_t buflen)
50656eec 451{
4235b045
MH
452 int ret, depth = 0;
453 char *tmp = buf;
454
455 /* Argument name or separator */
456 if (arg->name)
457 ret = e_snprintf(buf, buflen, " %s=", arg->name);
458 else
459 ret = e_snprintf(buf, buflen, " ");
460 if (ret < 0)
461 return ret;
462 buf += ret;
463 buflen -= ret;
464
465 /* Dereferencing arguments */
466 if (arg->ref) {
467 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
468 &buflen, 1);
469 if (depth < 0)
470 return depth;
471 }
472
473 /* Print argument value */
474 ret = e_snprintf(buf, buflen, "%s", arg->value);
475 if (ret < 0)
476 return ret;
477 buf += ret;
478 buflen -= ret;
479
480 /* Closing */
481 while (depth--) {
482 ret = e_snprintf(buf, buflen, ")");
483 if (ret < 0)
484 return ret;
485 buf += ret;
486 buflen -= ret;
487 }
488
489 return buf - tmp;
490}
491
492char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
493{
494 struct kprobe_trace_point *tp = &tev->point;
50656eec
MH
495 char *buf;
496 int i, len, ret;
497
4235b045
MH
498 buf = xzalloc(MAX_CMDLEN);
499 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
500 tp->retprobe ? 'r' : 'p',
501 tev->group, tev->event,
502 tp->symbol, tp->offset);
503 if (len <= 0)
50656eec 504 goto error;
50656eec 505
4235b045
MH
506 for (i = 0; i < tev->nargs; i++) {
507 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
508 MAX_CMDLEN - len);
4de189fe 509 if (ret <= 0)
50656eec
MH
510 goto error;
511 len += ret;
512 }
50656eec 513
4235b045 514 return buf;
50656eec 515error:
4235b045
MH
516 free(buf);
517 return NULL;
518}
50656eec 519
4235b045
MH
520void convert_to_perf_probe_event(struct kprobe_trace_event *tev,
521 struct perf_probe_event *pev)
522{
523 char buf[64];
524 int i;
fb1587d8
MH
525#ifndef NO_DWARF_SUPPORT
526 struct symbol *sym;
527 int fd, ret = 0;
528
529 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
530 tev->point.symbol, NULL);
531 if (sym) {
532 fd = open_vmlinux();
533 ret = find_perf_probe_point(fd, sym->start + tev->point.offset,
534 &pev->point);
535 close(fd);
536 }
537 if (ret <= 0) {
538 pev->point.function = xstrdup(tev->point.symbol);
539 pev->point.offset = tev->point.offset;
540 }
541#else
4235b045
MH
542 /* Convert trace_point to probe_point */
543 pev->point.function = xstrdup(tev->point.symbol);
544 pev->point.offset = tev->point.offset;
fb1587d8 545#endif
4235b045
MH
546 pev->point.retprobe = tev->point.retprobe;
547
fb1587d8
MH
548 pev->event = xstrdup(tev->event);
549 pev->group = xstrdup(tev->group);
550
4235b045
MH
551 /* Convert trace_arg to probe_arg */
552 pev->nargs = tev->nargs;
553 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
554 for (i = 0; i < tev->nargs; i++)
555 if (tev->args[i].name)
556 pev->args[i].name = xstrdup(tev->args[i].name);
557 else {
558 synthesize_kprobe_trace_arg(&tev->args[i], buf, 64);
559 pev->args[i].name = xstrdup(buf);
560 }
561}
562
563void clear_perf_probe_event(struct perf_probe_event *pev)
564{
565 struct perf_probe_point *pp = &pev->point;
566 int i;
567
568 if (pev->event)
569 free(pev->event);
570 if (pev->group)
571 free(pev->group);
572 if (pp->file)
573 free(pp->file);
574 if (pp->function)
575 free(pp->function);
576 if (pp->lazy_line)
577 free(pp->lazy_line);
578 for (i = 0; i < pev->nargs; i++)
579 if (pev->args[i].name)
580 free(pev->args[i].name);
581 if (pev->args)
582 free(pev->args);
583 memset(pev, 0, sizeof(*pev));
584}
585
586void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
587{
588 struct kprobe_trace_arg_ref *ref, *next;
589 int i;
590
591 if (tev->event)
592 free(tev->event);
593 if (tev->group)
594 free(tev->group);
595 if (tev->point.symbol)
596 free(tev->point.symbol);
597 for (i = 0; i < tev->nargs; i++) {
598 if (tev->args[i].name)
599 free(tev->args[i].name);
600 if (tev->args[i].value)
601 free(tev->args[i].value);
602 ref = tev->args[i].ref;
603 while (ref) {
604 next = ref->next;
605 free(ref);
606 ref = next;
607 }
608 }
609 if (tev->args)
610 free(tev->args);
611 memset(tev, 0, sizeof(*tev));
50656eec
MH
612}
613
f4d7da49 614static int open_kprobe_events(bool readwrite)
4de189fe
MH
615{
616 char buf[PATH_MAX];
617 int ret;
618
619 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
620 if (ret < 0)
621 die("Failed to make kprobe_events path.");
622
f4d7da49
MH
623 if (readwrite && !probe_event_dry_run)
624 ret = open(buf, O_RDWR, O_APPEND);
625 else
626 ret = open(buf, O_RDONLY, 0);
627
4de189fe
MH
628 if (ret < 0) {
629 if (errno == ENOENT)
630 die("kprobe_events file does not exist -"
63bbd5e2 631 " please rebuild with CONFIG_KPROBE_EVENT.");
4de189fe
MH
632 else
633 die("Could not open kprobe_events file: %s",
634 strerror(errno));
635 }
636 return ret;
637}
638
639/* Get raw string list of current kprobe_events */
4235b045 640static struct strlist *get_kprobe_trace_command_rawlist(int fd)
4de189fe
MH
641{
642 int ret, idx;
643 FILE *fp;
644 char buf[MAX_CMDLEN];
645 char *p;
646 struct strlist *sl;
647
648 sl = strlist__new(true, NULL);
649
650 fp = fdopen(dup(fd), "r");
651 while (!feof(fp)) {
652 p = fgets(buf, MAX_CMDLEN, fp);
653 if (!p)
654 break;
655
656 idx = strlen(p) - 1;
657 if (p[idx] == '\n')
658 p[idx] = '\0';
659 ret = strlist__add(sl, buf);
660 if (ret < 0)
661 die("strlist__add failed: %s", strerror(-ret));
662 }
663 fclose(fp);
664
665 return sl;
666}
667
278498d4 668/* Show an event */
4235b045 669static void show_perf_probe_event(struct perf_probe_event *pev)
278498d4 670{
7e990a51 671 int i, ret;
278498d4 672 char buf[128];
4235b045 673 char *place;
278498d4 674
4235b045
MH
675 /* Synthesize only event probe point */
676 place = synthesize_perf_probe_point(&pev->point);
677
678 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
7e990a51
MH
679 if (ret < 0)
680 die("Failed to copy event: %s", strerror(-ret));
fb1587d8 681 printf(" %-20s (on %s", buf, place);
278498d4 682
4235b045 683 if (pev->nargs > 0) {
278498d4 684 printf(" with");
4235b045
MH
685 for (i = 0; i < pev->nargs; i++)
686 printf(" %s", pev->args[i].name);
278498d4
MH
687 }
688 printf(")\n");
4235b045 689 free(place);
278498d4
MH
690}
691
4de189fe
MH
692/* List up current perf-probe events */
693void show_perf_probe_events(void)
694{
7ef17aaf 695 int fd;
4235b045
MH
696 struct kprobe_trace_event tev;
697 struct perf_probe_event pev;
4de189fe
MH
698 struct strlist *rawlist;
699 struct str_node *ent;
700
72041334 701 setup_pager();
fb1587d8 702 init_vmlinux();
4235b045
MH
703
704 memset(&tev, 0, sizeof(tev));
705 memset(&pev, 0, sizeof(pev));
72041334 706
f4d7da49 707 fd = open_kprobe_events(false);
4235b045 708 rawlist = get_kprobe_trace_command_rawlist(fd);
4de189fe
MH
709 close(fd);
710
adf365f4 711 strlist__for_each(ent, rawlist) {
4235b045
MH
712 parse_kprobe_trace_command(ent->s, &tev);
713 convert_to_perf_probe_event(&tev, &pev);
278498d4 714 /* Show an event */
4235b045
MH
715 show_perf_probe_event(&pev);
716 clear_perf_probe_event(&pev);
717 clear_kprobe_trace_event(&tev);
4de189fe
MH
718 }
719
720 strlist__delete(rawlist);
721}
722
b498ce1f 723/* Get current perf-probe event names */
4235b045 724static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
b498ce1f 725{
fa28244d 726 char buf[128];
b498ce1f
MH
727 struct strlist *sl, *rawlist;
728 struct str_node *ent;
4235b045 729 struct kprobe_trace_event tev;
b498ce1f 730
4235b045 731 memset(&tev, 0, sizeof(tev));
b498ce1f 732
4235b045 733 rawlist = get_kprobe_trace_command_rawlist(fd);
e1d2017b 734 sl = strlist__new(true, NULL);
adf365f4 735 strlist__for_each(ent, rawlist) {
4235b045 736 parse_kprobe_trace_command(ent->s, &tev);
fa28244d 737 if (include_group) {
4235b045
MH
738 if (e_snprintf(buf, 128, "%s:%s", tev.group,
739 tev.event) < 0)
fa28244d
MH
740 die("Failed to copy group:event name.");
741 strlist__add(sl, buf);
742 } else
4235b045
MH
743 strlist__add(sl, tev.event);
744 clear_kprobe_trace_event(&tev);
b498ce1f
MH
745 }
746
747 strlist__delete(rawlist);
748
749 return sl;
750}
751
4235b045 752static void write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
50656eec
MH
753{
754 int ret;
4235b045 755 char *buf = synthesize_kprobe_trace_command(tev);
50656eec 756
fa28244d 757 pr_debug("Writing event: %s\n", buf);
f4d7da49
MH
758 if (!probe_event_dry_run) {
759 ret = write(fd, buf, strlen(buf));
760 if (ret <= 0)
761 die("Failed to write event: %s", strerror(errno));
762 }
4235b045 763 free(buf);
50656eec
MH
764}
765
b498ce1f 766static void get_new_event_name(char *buf, size_t len, const char *base,
d761b08b 767 struct strlist *namelist, bool allow_suffix)
b498ce1f
MH
768{
769 int i, ret;
17f88fcd
MH
770
771 /* Try no suffix */
772 ret = e_snprintf(buf, len, "%s", base);
773 if (ret < 0)
774 die("snprintf() failed: %s", strerror(-ret));
775 if (!strlist__has_entry(namelist, buf))
776 return;
777
d761b08b
MH
778 if (!allow_suffix) {
779 pr_warning("Error: event \"%s\" already exists. "
780 "(Use -f to force duplicates.)\n", base);
781 die("Can't add new event.");
782 }
783
17f88fcd
MH
784 /* Try to add suffix */
785 for (i = 1; i < MAX_EVENT_INDEX; i++) {
b498ce1f
MH
786 ret = e_snprintf(buf, len, "%s_%d", base, i);
787 if (ret < 0)
788 die("snprintf() failed: %s", strerror(-ret));
789 if (!strlist__has_entry(namelist, buf))
790 break;
791 }
792 if (i == MAX_EVENT_INDEX)
793 die("Too many events are on the same function.");
794}
795
4235b045
MH
796static void __add_kprobe_trace_events(struct perf_probe_event *pev,
797 struct kprobe_trace_event *tevs,
798 int ntevs, bool allow_suffix)
50656eec 799{
4235b045
MH
800 int i, fd;
801 struct kprobe_trace_event *tev;
802 char buf[64];
803 const char *event, *group;
b498ce1f 804 struct strlist *namelist;
50656eec 805
f4d7da49 806 fd = open_kprobe_events(true);
b498ce1f 807 /* Get current event names */
4235b045
MH
808 namelist = get_kprobe_trace_event_names(fd, false);
809
810 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
811 for (i = 0; i < ntevs; i++) {
812 tev = &tevs[i];
813 if (pev->event)
814 event = pev->event;
815 else
816 if (pev->point.function)
817 event = pev->point.function;
818 else
819 event = tev->point.symbol;
820 if (pev->group)
821 group = pev->group;
822 else
823 group = PERFPROBE_GROUP;
824
825 /* Get an unused new event name */
826 get_new_event_name(buf, 64, event, namelist, allow_suffix);
827 event = buf;
828
829 tev->event = xstrdup(event);
830 tev->group = xstrdup(group);
831 write_kprobe_trace_event(fd, tev);
832 /* Add added event name to namelist */
833 strlist__add(namelist, event);
834
835 /* Trick here - save current event/group */
836 event = pev->event;
837 group = pev->group;
838 pev->event = tev->event;
839 pev->group = tev->group;
840 show_perf_probe_event(pev);
841 /* Trick here - restore current event/group */
842 pev->event = (char *)event;
843 pev->group = (char *)group;
844
845 /*
846 * Probes after the first probe which comes from same
847 * user input are always allowed to add suffix, because
848 * there might be several addresses corresponding to
849 * one code line.
850 */
851 allow_suffix = true;
50656eec 852 }
a9b495b0
MH
853 /* Show how to use the event. */
854 printf("\nYou can now use it on all perf tools, such as:\n\n");
4235b045 855 printf("\tperf record -e %s:%s -a sleep 1\n\n", tev->group, tev->event);
a9b495b0 856
e1d2017b 857 strlist__delete(namelist);
50656eec
MH
858 close(fd);
859}
fa28244d 860
4235b045
MH
861static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
862 struct kprobe_trace_event **tevs)
e0faa8d3
MH
863{
864 struct symbol *sym;
4235b045 865 bool need_dwarf;
e0faa8d3
MH
866#ifndef NO_DWARF_SUPPORT
867 int fd;
868#endif
4235b045
MH
869 int ntevs = 0, i;
870 struct kprobe_trace_event *tev;
871
872 need_dwarf = perf_probe_event_need_dwarf(pev);
e0faa8d3
MH
873
874 if (need_dwarf)
875#ifdef NO_DWARF_SUPPORT
876 die("Debuginfo-analysis is not supported");
877#else /* !NO_DWARF_SUPPORT */
878 pr_debug("Some probes require debuginfo.\n");
879
880 fd = open_vmlinux();
881 if (fd < 0) {
882 if (need_dwarf)
883 die("Could not open debuginfo file.");
884
885 pr_debug("Could not open vmlinux/module file."
886 " Try to use symbols.\n");
887 goto end_dwarf;
888 }
889
890 /* Searching probe points */
4235b045
MH
891 ntevs = find_kprobe_trace_events(fd, pev, tevs);
892
893 if (ntevs > 0) /* Found */
894 goto found;
895
896 if (ntevs == 0) /* No error but failed to find probe point. */
897 die("Probe point '%s' not found. - probe not added.",
898 synthesize_perf_probe_point(&pev->point));
899
900 /* Error path */
901 if (need_dwarf) {
902 if (ntevs == -ENOENT)
903 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
904 die("Could not analyze debuginfo.");
e0faa8d3 905 }
4235b045
MH
906 pr_debug("An error occurred in debuginfo analysis."
907 " Try to use symbols.\n");
e0faa8d3
MH
908
909end_dwarf:
910#endif /* !NO_DWARF_SUPPORT */
911
4235b045
MH
912 /* Allocate trace event buffer */
913 ntevs = 1;
914 tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event));
915
916 /* Copy parameters */
917 tev->point.symbol = xstrdup(pev->point.function);
918 tev->point.offset = pev->point.offset;
919 tev->nargs = pev->nargs;
920 if (tev->nargs) {
921 tev->args = xzalloc(sizeof(struct kprobe_trace_arg)
922 * tev->nargs);
923 for (i = 0; i < tev->nargs; i++)
924 tev->args[i].value = xstrdup(pev->args[i].name);
925 }
926
927 /* Currently just checking function name from symbol map */
928 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
929 tev->point.symbol, NULL);
930 if (!sym)
931 die("Kernel symbol \'%s\' not found - probe not added.",
932 tev->point.symbol);
933found:
934 close(fd);
935 return ntevs;
936}
937
938struct __event_package {
939 struct perf_probe_event *pev;
940 struct kprobe_trace_event *tevs;
941 int ntevs;
942};
943
944void add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
945 bool force_add)
946{
947 int i;
948 struct __event_package *pkgs;
949
950 pkgs = xzalloc(sizeof(struct __event_package) * npevs);
951
952 /* Init vmlinux path */
953 init_vmlinux();
954
955 /* Loop 1: convert all events */
956 for (i = 0; i < npevs; i++) {
957 pkgs[i].pev = &pevs[i];
958 /* Convert with or without debuginfo */
959 pkgs[i].ntevs = convert_to_kprobe_trace_events(pkgs[i].pev,
960 &pkgs[i].tevs);
e0faa8d3
MH
961 }
962
4235b045
MH
963 /* Loop 2: add all events */
964 for (i = 0; i < npevs; i++)
965 __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
966 pkgs[i].ntevs, force_add);
967 /* TODO: cleanup all trace events? */
e0faa8d3
MH
968}
969
bbbb521b
MH
970static void __del_trace_kprobe_event(int fd, struct str_node *ent)
971{
972 char *p;
973 char buf[128];
4235b045 974 int ret;
bbbb521b
MH
975
976 /* Convert from perf-probe event to trace-kprobe event */
977 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
978 die("Failed to copy event.");
979 p = strchr(buf + 2, ':');
980 if (!p)
981 die("Internal error: %s should have ':' but not.", ent->s);
982 *p = '/';
983
4235b045
MH
984 pr_debug("Writing event: %s\n", buf);
985 ret = write(fd, buf, strlen(buf));
986 if (ret <= 0)
987 die("Failed to write event: %s", strerror(errno));
bbbb521b
MH
988 printf("Remove event: %s\n", ent->s);
989}
990
fa28244d
MH
991static void del_trace_kprobe_event(int fd, const char *group,
992 const char *event, struct strlist *namelist)
993{
994 char buf[128];
bbbb521b
MH
995 struct str_node *ent, *n;
996 int found = 0;
fa28244d
MH
997
998 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
999 die("Failed to copy event.");
fa28244d 1000
bbbb521b
MH
1001 if (strpbrk(buf, "*?")) { /* Glob-exp */
1002 strlist__for_each_safe(ent, n, namelist)
1003 if (strglobmatch(ent->s, buf)) {
1004 found++;
1005 __del_trace_kprobe_event(fd, ent);
1006 strlist__remove(namelist, ent);
1007 }
1008 } else {
1009 ent = strlist__find(namelist, buf);
1010 if (ent) {
1011 found++;
1012 __del_trace_kprobe_event(fd, ent);
1013 strlist__remove(namelist, ent);
1014 }
1015 }
1016 if (found == 0)
1017 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
fa28244d
MH
1018}
1019
4235b045 1020void del_perf_probe_events(struct strlist *dellist)
fa28244d
MH
1021{
1022 int fd;
fa28244d
MH
1023 const char *group, *event;
1024 char *p, *str;
1025 struct str_node *ent;
1026 struct strlist *namelist;
1027
f4d7da49 1028 fd = open_kprobe_events(true);
fa28244d 1029 /* Get current event names */
4235b045 1030 namelist = get_kprobe_trace_event_names(fd, true);
fa28244d 1031
adf365f4 1032 strlist__for_each(ent, dellist) {
31facc5f 1033 str = xstrdup(ent->s);
bbbb521b 1034 pr_debug("Parsing: %s\n", str);
fa28244d
MH
1035 p = strchr(str, ':');
1036 if (p) {
1037 group = str;
1038 *p = '\0';
1039 event = p + 1;
1040 } else {
bbbb521b 1041 group = "*";
fa28244d
MH
1042 event = str;
1043 }
bbbb521b 1044 pr_debug("Group: %s, Event: %s\n", group, event);
fa28244d
MH
1045 del_trace_kprobe_event(fd, group, event, namelist);
1046 free(str);
1047 }
1048 strlist__delete(namelist);
1049 close(fd);
1050}
1051
631c9def 1052#define LINEBUF_SIZE 256
5c8d1cbb 1053#define NR_ADDITIONAL_LINES 2
631c9def
MH
1054
1055static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
1056{
1057 char buf[LINEBUF_SIZE];
1058 const char *color = PERF_COLOR_BLUE;
1059
1060 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
1061 goto error;
1062 if (!skip) {
1063 if (show_num)
1064 fprintf(stdout, "%7u %s", l, buf);
1065 else
1066 color_fprintf(stdout, color, " %s", buf);
1067 }
1068
1069 while (strlen(buf) == LINEBUF_SIZE - 1 &&
1070 buf[LINEBUF_SIZE - 2] != '\n') {
1071 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
1072 goto error;
1073 if (!skip) {
1074 if (show_num)
1075 fprintf(stdout, "%s", buf);
1076 else
1077 color_fprintf(stdout, color, "%s", buf);
1078 }
1079 }
1080 return;
1081error:
1082 if (feof(fp))
1083 die("Source file is shorter than expected.");
1084 else
1085 die("File read error: %s", strerror(errno));
1086}
1087
1088void show_line_range(struct line_range *lr)
1089{
1090 unsigned int l = 1;
1091 struct line_node *ln;
1092 FILE *fp;
e0faa8d3
MH
1093 int fd, ret;
1094
1095 /* Search a line range */
1096 init_vmlinux();
1097 fd = open_vmlinux();
1098 if (fd < 0)
1099 die("Could not open debuginfo file.");
1100 ret = find_line_range(fd, lr);
1101 if (ret <= 0)
1102 die("Source line is not found.\n");
1103 close(fd);
631c9def
MH
1104
1105 setup_pager();
1106
1107 if (lr->function)
1108 fprintf(stdout, "<%s:%d>\n", lr->function,
1109 lr->start - lr->offset);
1110 else
1111 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
1112
1113 fp = fopen(lr->path, "r");
1114 if (fp == NULL)
1115 die("Failed to open %s: %s", lr->path, strerror(errno));
1116 /* Skip to starting line number */
1117 while (l < lr->start)
1118 show_one_line(fp, l++, true, false);
1119
1120 list_for_each_entry(ln, &lr->line_list, list) {
1121 while (ln->line > l)
1122 show_one_line(fp, (l++) - lr->offset, false, false);
1123 show_one_line(fp, (l++) - lr->offset, false, true);
1124 }
5c8d1cbb
MH
1125
1126 if (lr->end == INT_MAX)
1127 lr->end = l + NR_ADDITIONAL_LINES;
1128 while (l < lr->end && !feof(fp))
1129 show_one_line(fp, (l++) - lr->offset, false, false);
1130
631c9def
MH
1131 fclose(fp);
1132}
e0faa8d3
MH
1133
1134