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