perf report: Add infrastructure for a cycles histogram
[deliverable/linux.git] / tools / perf / util / probe-event.c
1 /*
2 * probe-event.c : perf-probe definition to probe_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 #include <sys/utsname.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <limits.h>
33 #include <elf.h>
34
35 #include "util.h"
36 #include "event.h"
37 #include "strlist.h"
38 #include "debug.h"
39 #include "cache.h"
40 #include "color.h"
41 #include "symbol.h"
42 #include "thread.h"
43 #include <api/fs/debugfs.h>
44 #include <api/fs/tracefs.h>
45 #include "trace-event.h" /* For __maybe_unused */
46 #include "probe-event.h"
47 #include "probe-finder.h"
48 #include "probe-file.h"
49 #include "session.h"
50
51 #define MAX_CMDLEN 256
52 #define PERFPROBE_GROUP "probe"
53
54 bool probe_event_dry_run; /* Dry run flag */
55 struct probe_conf probe_conf;
56
57 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
58
59 int e_snprintf(char *str, size_t size, const char *format, ...)
60 {
61 int ret;
62 va_list ap;
63 va_start(ap, format);
64 ret = vsnprintf(str, size, format, ap);
65 va_end(ap);
66 if (ret >= (int)size)
67 ret = -E2BIG;
68 return ret;
69 }
70
71 static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
72 static struct machine *host_machine;
73
74 /* Initialize symbol maps and path of vmlinux/modules */
75 static int init_symbol_maps(bool user_only)
76 {
77 int ret;
78
79 symbol_conf.sort_by_name = true;
80 symbol_conf.allow_aliases = true;
81 ret = symbol__init(NULL);
82 if (ret < 0) {
83 pr_debug("Failed to init symbol map.\n");
84 goto out;
85 }
86
87 if (host_machine || user_only) /* already initialized */
88 return 0;
89
90 if (symbol_conf.vmlinux_name)
91 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
92
93 host_machine = machine__new_host();
94 if (!host_machine) {
95 pr_debug("machine__new_host() failed.\n");
96 symbol__exit();
97 ret = -1;
98 }
99 out:
100 if (ret < 0)
101 pr_warning("Failed to init vmlinux path.\n");
102 return ret;
103 }
104
105 static void exit_symbol_maps(void)
106 {
107 if (host_machine) {
108 machine__delete(host_machine);
109 host_machine = NULL;
110 }
111 symbol__exit();
112 }
113
114 static struct symbol *__find_kernel_function_by_name(const char *name,
115 struct map **mapp)
116 {
117 return machine__find_kernel_function_by_name(host_machine, name, mapp,
118 NULL);
119 }
120
121 static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
122 {
123 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
124 }
125
126 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
127 {
128 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
129 struct kmap *kmap;
130
131 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
132 return NULL;
133
134 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
135 if (!kmap)
136 return NULL;
137 return kmap->ref_reloc_sym;
138 }
139
140 static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
141 {
142 struct ref_reloc_sym *reloc_sym;
143 struct symbol *sym;
144 struct map *map;
145
146 /* ref_reloc_sym is just a label. Need a special fix*/
147 reloc_sym = kernel_get_ref_reloc_sym();
148 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
149 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
150 else {
151 sym = __find_kernel_function_by_name(name, &map);
152 if (sym)
153 return map->unmap_ip(map, sym->start) -
154 ((reloc) ? 0 : map->reloc);
155 }
156 return 0;
157 }
158
159 static struct map *kernel_get_module_map(const char *module)
160 {
161 struct map_groups *grp = &host_machine->kmaps;
162 struct maps *maps = &grp->maps[MAP__FUNCTION];
163 struct map *pos;
164
165 /* A file path -- this is an offline module */
166 if (module && strchr(module, '/'))
167 return machine__findnew_module_map(host_machine, 0, module);
168
169 if (!module)
170 module = "kernel";
171
172 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
173 if (strncmp(pos->dso->short_name + 1, module,
174 pos->dso->short_name_len - 2) == 0) {
175 return pos;
176 }
177 }
178 return NULL;
179 }
180
181 static struct map *get_target_map(const char *target, bool user)
182 {
183 /* Init maps of given executable or kernel */
184 if (user)
185 return dso__new_map(target);
186 else
187 return kernel_get_module_map(target);
188 }
189
190 static void put_target_map(struct map *map, bool user)
191 {
192 if (map && user) {
193 /* Only the user map needs to be released */
194 map__put(map);
195 }
196 }
197
198
199 static int convert_exec_to_group(const char *exec, char **result)
200 {
201 char *ptr1, *ptr2, *exec_copy;
202 char buf[64];
203 int ret;
204
205 exec_copy = strdup(exec);
206 if (!exec_copy)
207 return -ENOMEM;
208
209 ptr1 = basename(exec_copy);
210 if (!ptr1) {
211 ret = -EINVAL;
212 goto out;
213 }
214
215 ptr2 = strpbrk(ptr1, "-._");
216 if (ptr2)
217 *ptr2 = '\0';
218 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
219 if (ret < 0)
220 goto out;
221
222 *result = strdup(buf);
223 ret = *result ? 0 : -ENOMEM;
224
225 out:
226 free(exec_copy);
227 return ret;
228 }
229
230 static void clear_perf_probe_point(struct perf_probe_point *pp)
231 {
232 free(pp->file);
233 free(pp->function);
234 free(pp->lazy_line);
235 }
236
237 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
238 {
239 int i;
240
241 for (i = 0; i < ntevs; i++)
242 clear_probe_trace_event(tevs + i);
243 }
244
245 static bool kprobe_blacklist__listed(unsigned long address);
246 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
247 {
248 u64 etext_addr;
249
250 /* Get the address of _etext for checking non-probable text symbol */
251 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
252
253 if (etext_addr != 0 && etext_addr < address)
254 pr_warning("%s is out of .text, skip it.\n", symbol);
255 else if (kprobe_blacklist__listed(address))
256 pr_warning("%s is blacklisted function, skip it.\n", symbol);
257 else
258 return false;
259
260 return true;
261 }
262
263 #ifdef HAVE_DWARF_SUPPORT
264
265 static int kernel_get_module_dso(const char *module, struct dso **pdso)
266 {
267 struct dso *dso;
268 struct map *map;
269 const char *vmlinux_name;
270 int ret = 0;
271
272 if (module) {
273 list_for_each_entry(dso, &host_machine->dsos.head, node) {
274 if (!dso->kernel)
275 continue;
276 if (strncmp(dso->short_name + 1, module,
277 dso->short_name_len - 2) == 0)
278 goto found;
279 }
280 pr_debug("Failed to find module %s.\n", module);
281 return -ENOENT;
282 }
283
284 map = host_machine->vmlinux_maps[MAP__FUNCTION];
285 dso = map->dso;
286
287 vmlinux_name = symbol_conf.vmlinux_name;
288 dso->load_errno = 0;
289 if (vmlinux_name)
290 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
291 else
292 ret = dso__load_vmlinux_path(dso, map, NULL);
293 found:
294 *pdso = dso;
295 return ret;
296 }
297
298 /*
299 * Some binaries like glibc have special symbols which are on the symbol
300 * table, but not in the debuginfo. If we can find the address of the
301 * symbol from map, we can translate the address back to the probe point.
302 */
303 static int find_alternative_probe_point(struct debuginfo *dinfo,
304 struct perf_probe_point *pp,
305 struct perf_probe_point *result,
306 const char *target, bool uprobes)
307 {
308 struct map *map = NULL;
309 struct symbol *sym;
310 u64 address = 0;
311 int ret = -ENOENT;
312
313 /* This can work only for function-name based one */
314 if (!pp->function || pp->file)
315 return -ENOTSUP;
316
317 map = get_target_map(target, uprobes);
318 if (!map)
319 return -EINVAL;
320
321 /* Find the address of given function */
322 map__for_each_symbol_by_name(map, pp->function, sym) {
323 if (uprobes)
324 address = sym->start;
325 else
326 address = map->unmap_ip(map, sym->start);
327 break;
328 }
329 if (!address) {
330 ret = -ENOENT;
331 goto out;
332 }
333 pr_debug("Symbol %s address found : %" PRIx64 "\n",
334 pp->function, address);
335
336 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
337 result);
338 if (ret <= 0)
339 ret = (!ret) ? -ENOENT : ret;
340 else {
341 result->offset += pp->offset;
342 result->line += pp->line;
343 result->retprobe = pp->retprobe;
344 ret = 0;
345 }
346
347 out:
348 put_target_map(map, uprobes);
349 return ret;
350
351 }
352
353 static int get_alternative_probe_event(struct debuginfo *dinfo,
354 struct perf_probe_event *pev,
355 struct perf_probe_point *tmp)
356 {
357 int ret;
358
359 memcpy(tmp, &pev->point, sizeof(*tmp));
360 memset(&pev->point, 0, sizeof(pev->point));
361 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
362 pev->target, pev->uprobes);
363 if (ret < 0)
364 memcpy(&pev->point, tmp, sizeof(*tmp));
365
366 return ret;
367 }
368
369 static int get_alternative_line_range(struct debuginfo *dinfo,
370 struct line_range *lr,
371 const char *target, bool user)
372 {
373 struct perf_probe_point pp = { .function = lr->function,
374 .file = lr->file,
375 .line = lr->start };
376 struct perf_probe_point result;
377 int ret, len = 0;
378
379 memset(&result, 0, sizeof(result));
380
381 if (lr->end != INT_MAX)
382 len = lr->end - lr->start;
383 ret = find_alternative_probe_point(dinfo, &pp, &result,
384 target, user);
385 if (!ret) {
386 lr->function = result.function;
387 lr->file = result.file;
388 lr->start = result.line;
389 if (lr->end != INT_MAX)
390 lr->end = lr->start + len;
391 clear_perf_probe_point(&pp);
392 }
393 return ret;
394 }
395
396 /* Open new debuginfo of given module */
397 static struct debuginfo *open_debuginfo(const char *module, bool silent)
398 {
399 const char *path = module;
400 char reason[STRERR_BUFSIZE];
401 struct debuginfo *ret = NULL;
402 struct dso *dso = NULL;
403 int err;
404
405 if (!module || !strchr(module, '/')) {
406 err = kernel_get_module_dso(module, &dso);
407 if (err < 0) {
408 if (!dso || dso->load_errno == 0) {
409 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
410 strcpy(reason, "(unknown)");
411 } else
412 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
413 if (!silent)
414 pr_err("Failed to find the path for %s: %s\n",
415 module ?: "kernel", reason);
416 return NULL;
417 }
418 path = dso->long_name;
419 }
420 ret = debuginfo__new(path);
421 if (!ret && !silent) {
422 pr_warning("The %s file has no debug information.\n", path);
423 if (!module || !strtailcmp(path, ".ko"))
424 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
425 else
426 pr_warning("Rebuild with -g, ");
427 pr_warning("or install an appropriate debuginfo package.\n");
428 }
429 return ret;
430 }
431
432 /* For caching the last debuginfo */
433 static struct debuginfo *debuginfo_cache;
434 static char *debuginfo_cache_path;
435
436 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
437 {
438 if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
439 (!debuginfo_cache_path && !module && debuginfo_cache))
440 goto out;
441
442 /* Copy module path */
443 free(debuginfo_cache_path);
444 if (module) {
445 debuginfo_cache_path = strdup(module);
446 if (!debuginfo_cache_path) {
447 debuginfo__delete(debuginfo_cache);
448 debuginfo_cache = NULL;
449 goto out;
450 }
451 }
452
453 debuginfo_cache = open_debuginfo(module, silent);
454 if (!debuginfo_cache)
455 zfree(&debuginfo_cache_path);
456 out:
457 return debuginfo_cache;
458 }
459
460 static void debuginfo_cache__exit(void)
461 {
462 debuginfo__delete(debuginfo_cache);
463 debuginfo_cache = NULL;
464 zfree(&debuginfo_cache_path);
465 }
466
467
468 static int get_text_start_address(const char *exec, unsigned long *address)
469 {
470 Elf *elf;
471 GElf_Ehdr ehdr;
472 GElf_Shdr shdr;
473 int fd, ret = -ENOENT;
474
475 fd = open(exec, O_RDONLY);
476 if (fd < 0)
477 return -errno;
478
479 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
480 if (elf == NULL)
481 return -EINVAL;
482
483 if (gelf_getehdr(elf, &ehdr) == NULL)
484 goto out;
485
486 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
487 goto out;
488
489 *address = shdr.sh_addr - shdr.sh_offset;
490 ret = 0;
491 out:
492 elf_end(elf);
493 return ret;
494 }
495
496 /*
497 * Convert trace point to probe point with debuginfo
498 */
499 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
500 struct perf_probe_point *pp,
501 bool is_kprobe)
502 {
503 struct debuginfo *dinfo = NULL;
504 unsigned long stext = 0;
505 u64 addr = tp->address;
506 int ret = -ENOENT;
507
508 /* convert the address to dwarf address */
509 if (!is_kprobe) {
510 if (!addr) {
511 ret = -EINVAL;
512 goto error;
513 }
514 ret = get_text_start_address(tp->module, &stext);
515 if (ret < 0)
516 goto error;
517 addr += stext;
518 } else {
519 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
520 if (addr == 0)
521 goto error;
522 addr += tp->offset;
523 }
524
525 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
526 tp->module ? : "kernel");
527
528 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
529 if (dinfo)
530 ret = debuginfo__find_probe_point(dinfo,
531 (unsigned long)addr, pp);
532 else
533 ret = -ENOENT;
534
535 if (ret > 0) {
536 pp->retprobe = tp->retprobe;
537 return 0;
538 }
539 error:
540 pr_debug("Failed to find corresponding probes from debuginfo.\n");
541 return ret ? : -ENOENT;
542 }
543
544 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
545 int ntevs, const char *exec)
546 {
547 int i, ret = 0;
548 unsigned long stext = 0;
549
550 if (!exec)
551 return 0;
552
553 ret = get_text_start_address(exec, &stext);
554 if (ret < 0)
555 return ret;
556
557 for (i = 0; i < ntevs && ret >= 0; i++) {
558 /* point.address is the addres of point.symbol + point.offset */
559 tevs[i].point.address -= stext;
560 tevs[i].point.module = strdup(exec);
561 if (!tevs[i].point.module) {
562 ret = -ENOMEM;
563 break;
564 }
565 tevs[i].uprobes = true;
566 }
567
568 return ret;
569 }
570
571 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
572 int ntevs, const char *module)
573 {
574 int i, ret = 0;
575 char *tmp;
576
577 if (!module)
578 return 0;
579
580 tmp = strrchr(module, '/');
581 if (tmp) {
582 /* This is a module path -- get the module name */
583 module = strdup(tmp + 1);
584 if (!module)
585 return -ENOMEM;
586 tmp = strchr(module, '.');
587 if (tmp)
588 *tmp = '\0';
589 tmp = (char *)module; /* For free() */
590 }
591
592 for (i = 0; i < ntevs; i++) {
593 tevs[i].point.module = strdup(module);
594 if (!tevs[i].point.module) {
595 ret = -ENOMEM;
596 break;
597 }
598 }
599
600 free(tmp);
601 return ret;
602 }
603
604 /* Post processing the probe events */
605 static int post_process_probe_trace_events(struct probe_trace_event *tevs,
606 int ntevs, const char *module,
607 bool uprobe)
608 {
609 struct ref_reloc_sym *reloc_sym;
610 char *tmp;
611 int i, skipped = 0;
612
613 if (uprobe)
614 return add_exec_to_probe_trace_events(tevs, ntevs, module);
615
616 /* Note that currently ref_reloc_sym based probe is not for drivers */
617 if (module)
618 return add_module_to_probe_trace_events(tevs, ntevs, module);
619
620 reloc_sym = kernel_get_ref_reloc_sym();
621 if (!reloc_sym) {
622 pr_warning("Relocated base symbol is not found!\n");
623 return -EINVAL;
624 }
625
626 for (i = 0; i < ntevs; i++) {
627 if (!tevs[i].point.address || tevs[i].point.retprobe)
628 continue;
629 /* If we found a wrong one, mark it by NULL symbol */
630 if (kprobe_warn_out_range(tevs[i].point.symbol,
631 tevs[i].point.address)) {
632 tmp = NULL;
633 skipped++;
634 } else {
635 tmp = strdup(reloc_sym->name);
636 if (!tmp)
637 return -ENOMEM;
638 }
639 /* If we have no realname, use symbol for it */
640 if (!tevs[i].point.realname)
641 tevs[i].point.realname = tevs[i].point.symbol;
642 else
643 free(tevs[i].point.symbol);
644 tevs[i].point.symbol = tmp;
645 tevs[i].point.offset = tevs[i].point.address -
646 reloc_sym->unrelocated_addr;
647 }
648 return skipped;
649 }
650
651 /* Try to find perf_probe_event with debuginfo */
652 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
653 struct probe_trace_event **tevs)
654 {
655 bool need_dwarf = perf_probe_event_need_dwarf(pev);
656 struct perf_probe_point tmp;
657 struct debuginfo *dinfo;
658 int ntevs, ret = 0;
659
660 dinfo = open_debuginfo(pev->target, !need_dwarf);
661 if (!dinfo) {
662 if (need_dwarf)
663 return -ENOENT;
664 pr_debug("Could not open debuginfo. Try to use symbols.\n");
665 return 0;
666 }
667
668 pr_debug("Try to find probe point from debuginfo.\n");
669 /* Searching trace events corresponding to a probe event */
670 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
671
672 if (ntevs == 0) { /* Not found, retry with an alternative */
673 ret = get_alternative_probe_event(dinfo, pev, &tmp);
674 if (!ret) {
675 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
676 /*
677 * Write back to the original probe_event for
678 * setting appropriate (user given) event name
679 */
680 clear_perf_probe_point(&pev->point);
681 memcpy(&pev->point, &tmp, sizeof(tmp));
682 }
683 }
684
685 debuginfo__delete(dinfo);
686
687 if (ntevs > 0) { /* Succeeded to find trace events */
688 pr_debug("Found %d probe_trace_events.\n", ntevs);
689 ret = post_process_probe_trace_events(*tevs, ntevs,
690 pev->target, pev->uprobes);
691 if (ret < 0 || ret == ntevs) {
692 clear_probe_trace_events(*tevs, ntevs);
693 zfree(tevs);
694 }
695 if (ret != ntevs)
696 return ret < 0 ? ret : ntevs;
697 ntevs = 0;
698 /* Fall through */
699 }
700
701 if (ntevs == 0) { /* No error but failed to find probe point. */
702 pr_warning("Probe point '%s' not found.\n",
703 synthesize_perf_probe_point(&pev->point));
704 return -ENOENT;
705 }
706 /* Error path : ntevs < 0 */
707 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
708 if (ntevs == -EBADF) {
709 pr_warning("Warning: No dwarf info found in the vmlinux - "
710 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
711 if (!need_dwarf) {
712 pr_debug("Trying to use symbols.\n");
713 return 0;
714 }
715 }
716 return ntevs;
717 }
718
719 #define LINEBUF_SIZE 256
720 #define NR_ADDITIONAL_LINES 2
721
722 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
723 {
724 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
725 const char *color = show_num ? "" : PERF_COLOR_BLUE;
726 const char *prefix = NULL;
727
728 do {
729 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
730 goto error;
731 if (skip)
732 continue;
733 if (!prefix) {
734 prefix = show_num ? "%7d " : " ";
735 color_fprintf(stdout, color, prefix, l);
736 }
737 color_fprintf(stdout, color, "%s", buf);
738
739 } while (strchr(buf, '\n') == NULL);
740
741 return 1;
742 error:
743 if (ferror(fp)) {
744 pr_warning("File read error: %s\n",
745 strerror_r(errno, sbuf, sizeof(sbuf)));
746 return -1;
747 }
748 return 0;
749 }
750
751 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
752 {
753 int rv = __show_one_line(fp, l, skip, show_num);
754 if (rv == 0) {
755 pr_warning("Source file is shorter than expected.\n");
756 rv = -1;
757 }
758 return rv;
759 }
760
761 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
762 #define show_one_line(f,l) _show_one_line(f,l,false,false)
763 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
764 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
765
766 /*
767 * Show line-range always requires debuginfo to find source file and
768 * line number.
769 */
770 static int __show_line_range(struct line_range *lr, const char *module,
771 bool user)
772 {
773 int l = 1;
774 struct int_node *ln;
775 struct debuginfo *dinfo;
776 FILE *fp;
777 int ret;
778 char *tmp;
779 char sbuf[STRERR_BUFSIZE];
780
781 /* Search a line range */
782 dinfo = open_debuginfo(module, false);
783 if (!dinfo)
784 return -ENOENT;
785
786 ret = debuginfo__find_line_range(dinfo, lr);
787 if (!ret) { /* Not found, retry with an alternative */
788 ret = get_alternative_line_range(dinfo, lr, module, user);
789 if (!ret)
790 ret = debuginfo__find_line_range(dinfo, lr);
791 }
792 debuginfo__delete(dinfo);
793 if (ret == 0 || ret == -ENOENT) {
794 pr_warning("Specified source line is not found.\n");
795 return -ENOENT;
796 } else if (ret < 0) {
797 pr_warning("Debuginfo analysis failed.\n");
798 return ret;
799 }
800
801 /* Convert source file path */
802 tmp = lr->path;
803 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
804
805 /* Free old path when new path is assigned */
806 if (tmp != lr->path)
807 free(tmp);
808
809 if (ret < 0) {
810 pr_warning("Failed to find source file path.\n");
811 return ret;
812 }
813
814 setup_pager();
815
816 if (lr->function)
817 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
818 lr->start - lr->offset);
819 else
820 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
821
822 fp = fopen(lr->path, "r");
823 if (fp == NULL) {
824 pr_warning("Failed to open %s: %s\n", lr->path,
825 strerror_r(errno, sbuf, sizeof(sbuf)));
826 return -errno;
827 }
828 /* Skip to starting line number */
829 while (l < lr->start) {
830 ret = skip_one_line(fp, l++);
831 if (ret < 0)
832 goto end;
833 }
834
835 intlist__for_each(ln, lr->line_list) {
836 for (; ln->i > l; l++) {
837 ret = show_one_line(fp, l - lr->offset);
838 if (ret < 0)
839 goto end;
840 }
841 ret = show_one_line_with_num(fp, l++ - lr->offset);
842 if (ret < 0)
843 goto end;
844 }
845
846 if (lr->end == INT_MAX)
847 lr->end = l + NR_ADDITIONAL_LINES;
848 while (l <= lr->end) {
849 ret = show_one_line_or_eof(fp, l++ - lr->offset);
850 if (ret <= 0)
851 break;
852 }
853 end:
854 fclose(fp);
855 return ret;
856 }
857
858 int show_line_range(struct line_range *lr, const char *module, bool user)
859 {
860 int ret;
861
862 ret = init_symbol_maps(user);
863 if (ret < 0)
864 return ret;
865 ret = __show_line_range(lr, module, user);
866 exit_symbol_maps();
867
868 return ret;
869 }
870
871 static int show_available_vars_at(struct debuginfo *dinfo,
872 struct perf_probe_event *pev,
873 struct strfilter *_filter)
874 {
875 char *buf;
876 int ret, i, nvars;
877 struct str_node *node;
878 struct variable_list *vls = NULL, *vl;
879 struct perf_probe_point tmp;
880 const char *var;
881
882 buf = synthesize_perf_probe_point(&pev->point);
883 if (!buf)
884 return -EINVAL;
885 pr_debug("Searching variables at %s\n", buf);
886
887 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
888 if (!ret) { /* Not found, retry with an alternative */
889 ret = get_alternative_probe_event(dinfo, pev, &tmp);
890 if (!ret) {
891 ret = debuginfo__find_available_vars_at(dinfo, pev,
892 &vls);
893 /* Release the old probe_point */
894 clear_perf_probe_point(&tmp);
895 }
896 }
897 if (ret <= 0) {
898 if (ret == 0 || ret == -ENOENT) {
899 pr_err("Failed to find the address of %s\n", buf);
900 ret = -ENOENT;
901 } else
902 pr_warning("Debuginfo analysis failed.\n");
903 goto end;
904 }
905
906 /* Some variables are found */
907 fprintf(stdout, "Available variables at %s\n", buf);
908 for (i = 0; i < ret; i++) {
909 vl = &vls[i];
910 /*
911 * A probe point might be converted to
912 * several trace points.
913 */
914 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
915 vl->point.offset);
916 zfree(&vl->point.symbol);
917 nvars = 0;
918 if (vl->vars) {
919 strlist__for_each(node, vl->vars) {
920 var = strchr(node->s, '\t') + 1;
921 if (strfilter__compare(_filter, var)) {
922 fprintf(stdout, "\t\t%s\n", node->s);
923 nvars++;
924 }
925 }
926 strlist__delete(vl->vars);
927 }
928 if (nvars == 0)
929 fprintf(stdout, "\t\t(No matched variables)\n");
930 }
931 free(vls);
932 end:
933 free(buf);
934 return ret;
935 }
936
937 /* Show available variables on given probe point */
938 int show_available_vars(struct perf_probe_event *pevs, int npevs,
939 struct strfilter *_filter)
940 {
941 int i, ret = 0;
942 struct debuginfo *dinfo;
943
944 ret = init_symbol_maps(pevs->uprobes);
945 if (ret < 0)
946 return ret;
947
948 dinfo = open_debuginfo(pevs->target, false);
949 if (!dinfo) {
950 ret = -ENOENT;
951 goto out;
952 }
953
954 setup_pager();
955
956 for (i = 0; i < npevs && ret >= 0; i++)
957 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
958
959 debuginfo__delete(dinfo);
960 out:
961 exit_symbol_maps();
962 return ret;
963 }
964
965 #else /* !HAVE_DWARF_SUPPORT */
966
967 static void debuginfo_cache__exit(void)
968 {
969 }
970
971 static int
972 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
973 struct perf_probe_point *pp __maybe_unused,
974 bool is_kprobe __maybe_unused)
975 {
976 return -ENOSYS;
977 }
978
979 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
980 struct probe_trace_event **tevs __maybe_unused)
981 {
982 if (perf_probe_event_need_dwarf(pev)) {
983 pr_warning("Debuginfo-analysis is not supported.\n");
984 return -ENOSYS;
985 }
986
987 return 0;
988 }
989
990 int show_line_range(struct line_range *lr __maybe_unused,
991 const char *module __maybe_unused,
992 bool user __maybe_unused)
993 {
994 pr_warning("Debuginfo-analysis is not supported.\n");
995 return -ENOSYS;
996 }
997
998 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
999 int npevs __maybe_unused,
1000 struct strfilter *filter __maybe_unused)
1001 {
1002 pr_warning("Debuginfo-analysis is not supported.\n");
1003 return -ENOSYS;
1004 }
1005 #endif
1006
1007 void line_range__clear(struct line_range *lr)
1008 {
1009 free(lr->function);
1010 free(lr->file);
1011 free(lr->path);
1012 free(lr->comp_dir);
1013 intlist__delete(lr->line_list);
1014 memset(lr, 0, sizeof(*lr));
1015 }
1016
1017 int line_range__init(struct line_range *lr)
1018 {
1019 memset(lr, 0, sizeof(*lr));
1020 lr->line_list = intlist__new(NULL);
1021 if (!lr->line_list)
1022 return -ENOMEM;
1023 else
1024 return 0;
1025 }
1026
1027 static int parse_line_num(char **ptr, int *val, const char *what)
1028 {
1029 const char *start = *ptr;
1030
1031 errno = 0;
1032 *val = strtol(*ptr, ptr, 0);
1033 if (errno || *ptr == start) {
1034 semantic_error("'%s' is not a valid number.\n", what);
1035 return -EINVAL;
1036 }
1037 return 0;
1038 }
1039
1040 /* Check the name is good for event, group or function */
1041 static bool is_c_func_name(const char *name)
1042 {
1043 if (!isalpha(*name) && *name != '_')
1044 return false;
1045 while (*++name != '\0') {
1046 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1047 return false;
1048 }
1049 return true;
1050 }
1051
1052 /*
1053 * Stuff 'lr' according to the line range described by 'arg'.
1054 * The line range syntax is described by:
1055 *
1056 * SRC[:SLN[+NUM|-ELN]]
1057 * FNC[@SRC][:SLN[+NUM|-ELN]]
1058 */
1059 int parse_line_range_desc(const char *arg, struct line_range *lr)
1060 {
1061 char *range, *file, *name = strdup(arg);
1062 int err;
1063
1064 if (!name)
1065 return -ENOMEM;
1066
1067 lr->start = 0;
1068 lr->end = INT_MAX;
1069
1070 range = strchr(name, ':');
1071 if (range) {
1072 *range++ = '\0';
1073
1074 err = parse_line_num(&range, &lr->start, "start line");
1075 if (err)
1076 goto err;
1077
1078 if (*range == '+' || *range == '-') {
1079 const char c = *range++;
1080
1081 err = parse_line_num(&range, &lr->end, "end line");
1082 if (err)
1083 goto err;
1084
1085 if (c == '+') {
1086 lr->end += lr->start;
1087 /*
1088 * Adjust the number of lines here.
1089 * If the number of lines == 1, the
1090 * the end of line should be equal to
1091 * the start of line.
1092 */
1093 lr->end--;
1094 }
1095 }
1096
1097 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1098
1099 err = -EINVAL;
1100 if (lr->start > lr->end) {
1101 semantic_error("Start line must be smaller"
1102 " than end line.\n");
1103 goto err;
1104 }
1105 if (*range != '\0') {
1106 semantic_error("Tailing with invalid str '%s'.\n", range);
1107 goto err;
1108 }
1109 }
1110
1111 file = strchr(name, '@');
1112 if (file) {
1113 *file = '\0';
1114 lr->file = strdup(++file);
1115 if (lr->file == NULL) {
1116 err = -ENOMEM;
1117 goto err;
1118 }
1119 lr->function = name;
1120 } else if (strchr(name, '/') || strchr(name, '.'))
1121 lr->file = name;
1122 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1123 lr->function = name;
1124 else { /* Invalid name */
1125 semantic_error("'%s' is not a valid function name.\n", name);
1126 err = -EINVAL;
1127 goto err;
1128 }
1129
1130 return 0;
1131 err:
1132 free(name);
1133 return err;
1134 }
1135
1136 /* Parse probepoint definition. */
1137 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1138 {
1139 struct perf_probe_point *pp = &pev->point;
1140 char *ptr, *tmp;
1141 char c, nc = 0;
1142 bool file_spec = false;
1143 /*
1144 * <Syntax>
1145 * perf probe [EVENT=]SRC[:LN|;PTN]
1146 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1147 *
1148 * TODO:Group name support
1149 */
1150 if (!arg)
1151 return -EINVAL;
1152
1153 ptr = strpbrk(arg, ";=@+%");
1154 if (ptr && *ptr == '=') { /* Event name */
1155 *ptr = '\0';
1156 tmp = ptr + 1;
1157 if (strchr(arg, ':')) {
1158 semantic_error("Group name is not supported yet.\n");
1159 return -ENOTSUP;
1160 }
1161 if (!is_c_func_name(arg)) {
1162 semantic_error("%s is bad for event name -it must "
1163 "follow C symbol-naming rule.\n", arg);
1164 return -EINVAL;
1165 }
1166 pev->event = strdup(arg);
1167 if (pev->event == NULL)
1168 return -ENOMEM;
1169 pev->group = NULL;
1170 arg = tmp;
1171 }
1172
1173 /*
1174 * Check arg is function or file name and copy it.
1175 *
1176 * We consider arg to be a file spec if and only if it satisfies
1177 * all of the below criteria::
1178 * - it does not include any of "+@%",
1179 * - it includes one of ":;", and
1180 * - it has a period '.' in the name.
1181 *
1182 * Otherwise, we consider arg to be a function specification.
1183 */
1184 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1185 /* This is a file spec if it includes a '.' before ; or : */
1186 if (memchr(arg, '.', ptr - arg))
1187 file_spec = true;
1188 }
1189
1190 ptr = strpbrk(arg, ";:+@%");
1191 if (ptr) {
1192 nc = *ptr;
1193 *ptr++ = '\0';
1194 }
1195
1196 tmp = strdup(arg);
1197 if (tmp == NULL)
1198 return -ENOMEM;
1199
1200 if (file_spec)
1201 pp->file = tmp;
1202 else
1203 pp->function = tmp;
1204
1205 /* Parse other options */
1206 while (ptr) {
1207 arg = ptr;
1208 c = nc;
1209 if (c == ';') { /* Lazy pattern must be the last part */
1210 pp->lazy_line = strdup(arg);
1211 if (pp->lazy_line == NULL)
1212 return -ENOMEM;
1213 break;
1214 }
1215 ptr = strpbrk(arg, ";:+@%");
1216 if (ptr) {
1217 nc = *ptr;
1218 *ptr++ = '\0';
1219 }
1220 switch (c) {
1221 case ':': /* Line number */
1222 pp->line = strtoul(arg, &tmp, 0);
1223 if (*tmp != '\0') {
1224 semantic_error("There is non-digit char"
1225 " in line number.\n");
1226 return -EINVAL;
1227 }
1228 break;
1229 case '+': /* Byte offset from a symbol */
1230 pp->offset = strtoul(arg, &tmp, 0);
1231 if (*tmp != '\0') {
1232 semantic_error("There is non-digit character"
1233 " in offset.\n");
1234 return -EINVAL;
1235 }
1236 break;
1237 case '@': /* File name */
1238 if (pp->file) {
1239 semantic_error("SRC@SRC is not allowed.\n");
1240 return -EINVAL;
1241 }
1242 pp->file = strdup(arg);
1243 if (pp->file == NULL)
1244 return -ENOMEM;
1245 break;
1246 case '%': /* Probe places */
1247 if (strcmp(arg, "return") == 0) {
1248 pp->retprobe = 1;
1249 } else { /* Others not supported yet */
1250 semantic_error("%%%s is not supported.\n", arg);
1251 return -ENOTSUP;
1252 }
1253 break;
1254 default: /* Buggy case */
1255 pr_err("This program has a bug at %s:%d.\n",
1256 __FILE__, __LINE__);
1257 return -ENOTSUP;
1258 break;
1259 }
1260 }
1261
1262 /* Exclusion check */
1263 if (pp->lazy_line && pp->line) {
1264 semantic_error("Lazy pattern can't be used with"
1265 " line number.\n");
1266 return -EINVAL;
1267 }
1268
1269 if (pp->lazy_line && pp->offset) {
1270 semantic_error("Lazy pattern can't be used with offset.\n");
1271 return -EINVAL;
1272 }
1273
1274 if (pp->line && pp->offset) {
1275 semantic_error("Offset can't be used with line number.\n");
1276 return -EINVAL;
1277 }
1278
1279 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1280 semantic_error("File always requires line number or "
1281 "lazy pattern.\n");
1282 return -EINVAL;
1283 }
1284
1285 if (pp->offset && !pp->function) {
1286 semantic_error("Offset requires an entry function.\n");
1287 return -EINVAL;
1288 }
1289
1290 if (pp->retprobe && !pp->function) {
1291 semantic_error("Return probe requires an entry function.\n");
1292 return -EINVAL;
1293 }
1294
1295 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1296 semantic_error("Offset/Line/Lazy pattern can't be used with "
1297 "return probe.\n");
1298 return -EINVAL;
1299 }
1300
1301 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1302 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1303 pp->lazy_line);
1304 return 0;
1305 }
1306
1307 /* Parse perf-probe event argument */
1308 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1309 {
1310 char *tmp, *goodname;
1311 struct perf_probe_arg_field **fieldp;
1312
1313 pr_debug("parsing arg: %s into ", str);
1314
1315 tmp = strchr(str, '=');
1316 if (tmp) {
1317 arg->name = strndup(str, tmp - str);
1318 if (arg->name == NULL)
1319 return -ENOMEM;
1320 pr_debug("name:%s ", arg->name);
1321 str = tmp + 1;
1322 }
1323
1324 tmp = strchr(str, ':');
1325 if (tmp) { /* Type setting */
1326 *tmp = '\0';
1327 arg->type = strdup(tmp + 1);
1328 if (arg->type == NULL)
1329 return -ENOMEM;
1330 pr_debug("type:%s ", arg->type);
1331 }
1332
1333 tmp = strpbrk(str, "-.[");
1334 if (!is_c_varname(str) || !tmp) {
1335 /* A variable, register, symbol or special value */
1336 arg->var = strdup(str);
1337 if (arg->var == NULL)
1338 return -ENOMEM;
1339 pr_debug("%s\n", arg->var);
1340 return 0;
1341 }
1342
1343 /* Structure fields or array element */
1344 arg->var = strndup(str, tmp - str);
1345 if (arg->var == NULL)
1346 return -ENOMEM;
1347 goodname = arg->var;
1348 pr_debug("%s, ", arg->var);
1349 fieldp = &arg->field;
1350
1351 do {
1352 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1353 if (*fieldp == NULL)
1354 return -ENOMEM;
1355 if (*tmp == '[') { /* Array */
1356 str = tmp;
1357 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1358 (*fieldp)->ref = true;
1359 if (*tmp != ']' || tmp == str + 1) {
1360 semantic_error("Array index must be a"
1361 " number.\n");
1362 return -EINVAL;
1363 }
1364 tmp++;
1365 if (*tmp == '\0')
1366 tmp = NULL;
1367 } else { /* Structure */
1368 if (*tmp == '.') {
1369 str = tmp + 1;
1370 (*fieldp)->ref = false;
1371 } else if (tmp[1] == '>') {
1372 str = tmp + 2;
1373 (*fieldp)->ref = true;
1374 } else {
1375 semantic_error("Argument parse error: %s\n",
1376 str);
1377 return -EINVAL;
1378 }
1379 tmp = strpbrk(str, "-.[");
1380 }
1381 if (tmp) {
1382 (*fieldp)->name = strndup(str, tmp - str);
1383 if ((*fieldp)->name == NULL)
1384 return -ENOMEM;
1385 if (*str != '[')
1386 goodname = (*fieldp)->name;
1387 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1388 fieldp = &(*fieldp)->next;
1389 }
1390 } while (tmp);
1391 (*fieldp)->name = strdup(str);
1392 if ((*fieldp)->name == NULL)
1393 return -ENOMEM;
1394 if (*str != '[')
1395 goodname = (*fieldp)->name;
1396 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1397
1398 /* If no name is specified, set the last field name (not array index)*/
1399 if (!arg->name) {
1400 arg->name = strdup(goodname);
1401 if (arg->name == NULL)
1402 return -ENOMEM;
1403 }
1404 return 0;
1405 }
1406
1407 /* Parse perf-probe event command */
1408 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1409 {
1410 char **argv;
1411 int argc, i, ret = 0;
1412
1413 argv = argv_split(cmd, &argc);
1414 if (!argv) {
1415 pr_debug("Failed to split arguments.\n");
1416 return -ENOMEM;
1417 }
1418 if (argc - 1 > MAX_PROBE_ARGS) {
1419 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1420 ret = -ERANGE;
1421 goto out;
1422 }
1423 /* Parse probe point */
1424 ret = parse_perf_probe_point(argv[0], pev);
1425 if (ret < 0)
1426 goto out;
1427
1428 /* Copy arguments and ensure return probe has no C argument */
1429 pev->nargs = argc - 1;
1430 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1431 if (pev->args == NULL) {
1432 ret = -ENOMEM;
1433 goto out;
1434 }
1435 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1436 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1437 if (ret >= 0 &&
1438 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1439 semantic_error("You can't specify local variable for"
1440 " kretprobe.\n");
1441 ret = -EINVAL;
1442 }
1443 }
1444 out:
1445 argv_free(argv);
1446
1447 return ret;
1448 }
1449
1450 /* Return true if this perf_probe_event requires debuginfo */
1451 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1452 {
1453 int i;
1454
1455 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1456 return true;
1457
1458 for (i = 0; i < pev->nargs; i++)
1459 if (is_c_varname(pev->args[i].var))
1460 return true;
1461
1462 return false;
1463 }
1464
1465 /* Parse probe_events event into struct probe_point */
1466 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1467 {
1468 struct probe_trace_point *tp = &tev->point;
1469 char pr;
1470 char *p;
1471 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1472 int ret, i, argc;
1473 char **argv;
1474
1475 pr_debug("Parsing probe_events: %s\n", cmd);
1476 argv = argv_split(cmd, &argc);
1477 if (!argv) {
1478 pr_debug("Failed to split arguments.\n");
1479 return -ENOMEM;
1480 }
1481 if (argc < 2) {
1482 semantic_error("Too few probe arguments.\n");
1483 ret = -ERANGE;
1484 goto out;
1485 }
1486
1487 /* Scan event and group name. */
1488 argv0_str = strdup(argv[0]);
1489 if (argv0_str == NULL) {
1490 ret = -ENOMEM;
1491 goto out;
1492 }
1493 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1494 fmt2_str = strtok_r(NULL, "/", &fmt);
1495 fmt3_str = strtok_r(NULL, " \t", &fmt);
1496 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1497 || fmt3_str == NULL) {
1498 semantic_error("Failed to parse event name: %s\n", argv[0]);
1499 ret = -EINVAL;
1500 goto out;
1501 }
1502 pr = fmt1_str[0];
1503 tev->group = strdup(fmt2_str);
1504 tev->event = strdup(fmt3_str);
1505 if (tev->group == NULL || tev->event == NULL) {
1506 ret = -ENOMEM;
1507 goto out;
1508 }
1509 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1510
1511 tp->retprobe = (pr == 'r');
1512
1513 /* Scan module name(if there), function name and offset */
1514 p = strchr(argv[1], ':');
1515 if (p) {
1516 tp->module = strndup(argv[1], p - argv[1]);
1517 p++;
1518 } else
1519 p = argv[1];
1520 fmt1_str = strtok_r(p, "+", &fmt);
1521 if (fmt1_str[0] == '0') /* only the address started with 0x */
1522 tp->address = strtoul(fmt1_str, NULL, 0);
1523 else {
1524 /* Only the symbol-based probe has offset */
1525 tp->symbol = strdup(fmt1_str);
1526 if (tp->symbol == NULL) {
1527 ret = -ENOMEM;
1528 goto out;
1529 }
1530 fmt2_str = strtok_r(NULL, "", &fmt);
1531 if (fmt2_str == NULL)
1532 tp->offset = 0;
1533 else
1534 tp->offset = strtoul(fmt2_str, NULL, 10);
1535 }
1536
1537 tev->nargs = argc - 2;
1538 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1539 if (tev->args == NULL) {
1540 ret = -ENOMEM;
1541 goto out;
1542 }
1543 for (i = 0; i < tev->nargs; i++) {
1544 p = strchr(argv[i + 2], '=');
1545 if (p) /* We don't need which register is assigned. */
1546 *p++ = '\0';
1547 else
1548 p = argv[i + 2];
1549 tev->args[i].name = strdup(argv[i + 2]);
1550 /* TODO: parse regs and offset */
1551 tev->args[i].value = strdup(p);
1552 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1553 ret = -ENOMEM;
1554 goto out;
1555 }
1556 }
1557 ret = 0;
1558 out:
1559 free(argv0_str);
1560 argv_free(argv);
1561 return ret;
1562 }
1563
1564 /* Compose only probe arg */
1565 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1566 {
1567 struct perf_probe_arg_field *field = pa->field;
1568 int ret;
1569 char *tmp = buf;
1570
1571 if (pa->name && pa->var)
1572 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1573 else
1574 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1575 if (ret <= 0)
1576 goto error;
1577 tmp += ret;
1578 len -= ret;
1579
1580 while (field) {
1581 if (field->name[0] == '[')
1582 ret = e_snprintf(tmp, len, "%s", field->name);
1583 else
1584 ret = e_snprintf(tmp, len, "%s%s",
1585 field->ref ? "->" : ".", field->name);
1586 if (ret <= 0)
1587 goto error;
1588 tmp += ret;
1589 len -= ret;
1590 field = field->next;
1591 }
1592
1593 if (pa->type) {
1594 ret = e_snprintf(tmp, len, ":%s", pa->type);
1595 if (ret <= 0)
1596 goto error;
1597 tmp += ret;
1598 len -= ret;
1599 }
1600
1601 return tmp - buf;
1602 error:
1603 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1604 return ret;
1605 }
1606
1607 /* Compose only probe point (not argument) */
1608 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1609 {
1610 char *buf, *tmp;
1611 char offs[32] = "", line[32] = "", file[32] = "";
1612 int ret, len;
1613
1614 buf = zalloc(MAX_CMDLEN);
1615 if (buf == NULL) {
1616 ret = -ENOMEM;
1617 goto error;
1618 }
1619 if (pp->offset) {
1620 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1621 if (ret <= 0)
1622 goto error;
1623 }
1624 if (pp->line) {
1625 ret = e_snprintf(line, 32, ":%d", pp->line);
1626 if (ret <= 0)
1627 goto error;
1628 }
1629 if (pp->file) {
1630 tmp = pp->file;
1631 len = strlen(tmp);
1632 if (len > 30) {
1633 tmp = strchr(pp->file + len - 30, '/');
1634 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1635 }
1636 ret = e_snprintf(file, 32, "@%s", tmp);
1637 if (ret <= 0)
1638 goto error;
1639 }
1640
1641 if (pp->function)
1642 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1643 offs, pp->retprobe ? "%return" : "", line,
1644 file);
1645 else
1646 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1647 if (ret <= 0)
1648 goto error;
1649
1650 return buf;
1651 error:
1652 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1653 free(buf);
1654 return NULL;
1655 }
1656
1657 #if 0
1658 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1659 {
1660 char *buf;
1661 int i, len, ret;
1662
1663 buf = synthesize_perf_probe_point(&pev->point);
1664 if (!buf)
1665 return NULL;
1666
1667 len = strlen(buf);
1668 for (i = 0; i < pev->nargs; i++) {
1669 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1670 pev->args[i].name);
1671 if (ret <= 0) {
1672 free(buf);
1673 return NULL;
1674 }
1675 len += ret;
1676 }
1677
1678 return buf;
1679 }
1680 #endif
1681
1682 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1683 char **buf, size_t *buflen,
1684 int depth)
1685 {
1686 int ret;
1687 if (ref->next) {
1688 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1689 buflen, depth + 1);
1690 if (depth < 0)
1691 goto out;
1692 }
1693
1694 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1695 if (ret < 0)
1696 depth = ret;
1697 else {
1698 *buf += ret;
1699 *buflen -= ret;
1700 }
1701 out:
1702 return depth;
1703
1704 }
1705
1706 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1707 char *buf, size_t buflen)
1708 {
1709 struct probe_trace_arg_ref *ref = arg->ref;
1710 int ret, depth = 0;
1711 char *tmp = buf;
1712
1713 /* Argument name or separator */
1714 if (arg->name)
1715 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1716 else
1717 ret = e_snprintf(buf, buflen, " ");
1718 if (ret < 0)
1719 return ret;
1720 buf += ret;
1721 buflen -= ret;
1722
1723 /* Special case: @XXX */
1724 if (arg->value[0] == '@' && arg->ref)
1725 ref = ref->next;
1726
1727 /* Dereferencing arguments */
1728 if (ref) {
1729 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1730 &buflen, 1);
1731 if (depth < 0)
1732 return depth;
1733 }
1734
1735 /* Print argument value */
1736 if (arg->value[0] == '@' && arg->ref)
1737 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1738 arg->ref->offset);
1739 else
1740 ret = e_snprintf(buf, buflen, "%s", arg->value);
1741 if (ret < 0)
1742 return ret;
1743 buf += ret;
1744 buflen -= ret;
1745
1746 /* Closing */
1747 while (depth--) {
1748 ret = e_snprintf(buf, buflen, ")");
1749 if (ret < 0)
1750 return ret;
1751 buf += ret;
1752 buflen -= ret;
1753 }
1754 /* Print argument type */
1755 if (arg->type) {
1756 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1757 if (ret <= 0)
1758 return ret;
1759 buf += ret;
1760 }
1761
1762 return buf - tmp;
1763 }
1764
1765 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1766 {
1767 struct probe_trace_point *tp = &tev->point;
1768 char *buf;
1769 int i, len, ret;
1770
1771 buf = zalloc(MAX_CMDLEN);
1772 if (buf == NULL)
1773 return NULL;
1774
1775 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1776 tev->group, tev->event);
1777 if (len <= 0)
1778 goto error;
1779
1780 /* Uprobes must have tp->address and tp->module */
1781 if (tev->uprobes && (!tp->address || !tp->module))
1782 goto error;
1783
1784 /* Use the tp->address for uprobes */
1785 if (tev->uprobes)
1786 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1787 tp->module, tp->address);
1788 else
1789 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1790 tp->module ?: "", tp->module ? ":" : "",
1791 tp->symbol, tp->offset);
1792
1793 if (ret <= 0)
1794 goto error;
1795 len += ret;
1796
1797 for (i = 0; i < tev->nargs; i++) {
1798 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1799 MAX_CMDLEN - len);
1800 if (ret <= 0)
1801 goto error;
1802 len += ret;
1803 }
1804
1805 return buf;
1806 error:
1807 free(buf);
1808 return NULL;
1809 }
1810
1811 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1812 struct perf_probe_point *pp,
1813 bool is_kprobe)
1814 {
1815 struct symbol *sym = NULL;
1816 struct map *map;
1817 u64 addr;
1818 int ret = -ENOENT;
1819
1820 if (!is_kprobe) {
1821 map = dso__new_map(tp->module);
1822 if (!map)
1823 goto out;
1824 addr = tp->address;
1825 sym = map__find_symbol(map, addr, NULL);
1826 } else {
1827 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1828 if (addr) {
1829 addr += tp->offset;
1830 sym = __find_kernel_function(addr, &map);
1831 }
1832 }
1833 if (!sym)
1834 goto out;
1835
1836 pp->retprobe = tp->retprobe;
1837 pp->offset = addr - map->unmap_ip(map, sym->start);
1838 pp->function = strdup(sym->name);
1839 ret = pp->function ? 0 : -ENOMEM;
1840
1841 out:
1842 if (map && !is_kprobe) {
1843 map__put(map);
1844 }
1845
1846 return ret;
1847 }
1848
1849 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1850 struct perf_probe_point *pp,
1851 bool is_kprobe)
1852 {
1853 char buf[128];
1854 int ret;
1855
1856 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1857 if (!ret)
1858 return 0;
1859 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1860 if (!ret)
1861 return 0;
1862
1863 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1864
1865 if (tp->symbol) {
1866 pp->function = strdup(tp->symbol);
1867 pp->offset = tp->offset;
1868 } else if (!tp->module && !is_kprobe) {
1869 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1870 if (ret < 0)
1871 return ret;
1872 pp->function = strdup(buf);
1873 pp->offset = 0;
1874 }
1875 if (pp->function == NULL)
1876 return -ENOMEM;
1877
1878 pp->retprobe = tp->retprobe;
1879
1880 return 0;
1881 }
1882
1883 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1884 struct perf_probe_event *pev, bool is_kprobe)
1885 {
1886 char buf[64] = "";
1887 int i, ret;
1888
1889 /* Convert event/group name */
1890 pev->event = strdup(tev->event);
1891 pev->group = strdup(tev->group);
1892 if (pev->event == NULL || pev->group == NULL)
1893 return -ENOMEM;
1894
1895 /* Convert trace_point to probe_point */
1896 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1897 if (ret < 0)
1898 return ret;
1899
1900 /* Convert trace_arg to probe_arg */
1901 pev->nargs = tev->nargs;
1902 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1903 if (pev->args == NULL)
1904 return -ENOMEM;
1905 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1906 if (tev->args[i].name)
1907 pev->args[i].name = strdup(tev->args[i].name);
1908 else {
1909 ret = synthesize_probe_trace_arg(&tev->args[i],
1910 buf, 64);
1911 pev->args[i].name = strdup(buf);
1912 }
1913 if (pev->args[i].name == NULL && ret >= 0)
1914 ret = -ENOMEM;
1915 }
1916
1917 if (ret < 0)
1918 clear_perf_probe_event(pev);
1919
1920 return ret;
1921 }
1922
1923 void clear_perf_probe_event(struct perf_probe_event *pev)
1924 {
1925 struct perf_probe_arg_field *field, *next;
1926 int i;
1927
1928 free(pev->event);
1929 free(pev->group);
1930 free(pev->target);
1931 clear_perf_probe_point(&pev->point);
1932
1933 for (i = 0; i < pev->nargs; i++) {
1934 free(pev->args[i].name);
1935 free(pev->args[i].var);
1936 free(pev->args[i].type);
1937 field = pev->args[i].field;
1938 while (field) {
1939 next = field->next;
1940 zfree(&field->name);
1941 free(field);
1942 field = next;
1943 }
1944 }
1945 free(pev->args);
1946 memset(pev, 0, sizeof(*pev));
1947 }
1948
1949 void clear_probe_trace_event(struct probe_trace_event *tev)
1950 {
1951 struct probe_trace_arg_ref *ref, *next;
1952 int i;
1953
1954 free(tev->event);
1955 free(tev->group);
1956 free(tev->point.symbol);
1957 free(tev->point.realname);
1958 free(tev->point.module);
1959 for (i = 0; i < tev->nargs; i++) {
1960 free(tev->args[i].name);
1961 free(tev->args[i].value);
1962 free(tev->args[i].type);
1963 ref = tev->args[i].ref;
1964 while (ref) {
1965 next = ref->next;
1966 free(ref);
1967 ref = next;
1968 }
1969 }
1970 free(tev->args);
1971 memset(tev, 0, sizeof(*tev));
1972 }
1973
1974 struct kprobe_blacklist_node {
1975 struct list_head list;
1976 unsigned long start;
1977 unsigned long end;
1978 char *symbol;
1979 };
1980
1981 static void kprobe_blacklist__delete(struct list_head *blacklist)
1982 {
1983 struct kprobe_blacklist_node *node;
1984
1985 while (!list_empty(blacklist)) {
1986 node = list_first_entry(blacklist,
1987 struct kprobe_blacklist_node, list);
1988 list_del(&node->list);
1989 free(node->symbol);
1990 free(node);
1991 }
1992 }
1993
1994 static int kprobe_blacklist__load(struct list_head *blacklist)
1995 {
1996 struct kprobe_blacklist_node *node;
1997 const char *__debugfs = debugfs_find_mountpoint();
1998 char buf[PATH_MAX], *p;
1999 FILE *fp;
2000 int ret;
2001
2002 if (__debugfs == NULL)
2003 return -ENOTSUP;
2004
2005 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2006 if (ret < 0)
2007 return ret;
2008
2009 fp = fopen(buf, "r");
2010 if (!fp)
2011 return -errno;
2012
2013 ret = 0;
2014 while (fgets(buf, PATH_MAX, fp)) {
2015 node = zalloc(sizeof(*node));
2016 if (!node) {
2017 ret = -ENOMEM;
2018 break;
2019 }
2020 INIT_LIST_HEAD(&node->list);
2021 list_add_tail(&node->list, blacklist);
2022 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2023 ret = -EINVAL;
2024 break;
2025 }
2026 p = strchr(buf, '\t');
2027 if (p) {
2028 p++;
2029 if (p[strlen(p) - 1] == '\n')
2030 p[strlen(p) - 1] = '\0';
2031 } else
2032 p = (char *)"unknown";
2033 node->symbol = strdup(p);
2034 if (!node->symbol) {
2035 ret = -ENOMEM;
2036 break;
2037 }
2038 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2039 node->start, node->end, node->symbol);
2040 ret++;
2041 }
2042 if (ret < 0)
2043 kprobe_blacklist__delete(blacklist);
2044 fclose(fp);
2045
2046 return ret;
2047 }
2048
2049 static struct kprobe_blacklist_node *
2050 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2051 unsigned long address)
2052 {
2053 struct kprobe_blacklist_node *node;
2054
2055 list_for_each_entry(node, blacklist, list) {
2056 if (node->start <= address && address <= node->end)
2057 return node;
2058 }
2059
2060 return NULL;
2061 }
2062
2063 static LIST_HEAD(kprobe_blacklist);
2064
2065 static void kprobe_blacklist__init(void)
2066 {
2067 if (!list_empty(&kprobe_blacklist))
2068 return;
2069
2070 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2071 pr_debug("No kprobe blacklist support, ignored\n");
2072 }
2073
2074 static void kprobe_blacklist__release(void)
2075 {
2076 kprobe_blacklist__delete(&kprobe_blacklist);
2077 }
2078
2079 static bool kprobe_blacklist__listed(unsigned long address)
2080 {
2081 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2082 }
2083
2084 static int perf_probe_event__sprintf(const char *group, const char *event,
2085 struct perf_probe_event *pev,
2086 const char *module,
2087 struct strbuf *result)
2088 {
2089 int i, ret;
2090 char buf[128];
2091 char *place;
2092
2093 /* Synthesize only event probe point */
2094 place = synthesize_perf_probe_point(&pev->point);
2095 if (!place)
2096 return -EINVAL;
2097
2098 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2099 if (ret < 0)
2100 goto out;
2101
2102 strbuf_addf(result, " %-20s (on %s", buf, place);
2103 if (module)
2104 strbuf_addf(result, " in %s", module);
2105
2106 if (pev->nargs > 0) {
2107 strbuf_addstr(result, " with");
2108 for (i = 0; i < pev->nargs; i++) {
2109 ret = synthesize_perf_probe_arg(&pev->args[i],
2110 buf, 128);
2111 if (ret < 0)
2112 goto out;
2113 strbuf_addf(result, " %s", buf);
2114 }
2115 }
2116 strbuf_addch(result, ')');
2117 out:
2118 free(place);
2119 return ret;
2120 }
2121
2122 /* Show an event */
2123 static int show_perf_probe_event(const char *group, const char *event,
2124 struct perf_probe_event *pev,
2125 const char *module, bool use_stdout)
2126 {
2127 struct strbuf buf = STRBUF_INIT;
2128 int ret;
2129
2130 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2131 if (ret >= 0) {
2132 if (use_stdout)
2133 printf("%s\n", buf.buf);
2134 else
2135 pr_info("%s\n", buf.buf);
2136 }
2137 strbuf_release(&buf);
2138
2139 return ret;
2140 }
2141
2142 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2143 struct strfilter *filter)
2144 {
2145 char tmp[128];
2146
2147 /* At first, check the event name itself */
2148 if (strfilter__compare(filter, tev->event))
2149 return true;
2150
2151 /* Next, check the combination of name and group */
2152 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2153 return false;
2154 return strfilter__compare(filter, tmp);
2155 }
2156
2157 static int __show_perf_probe_events(int fd, bool is_kprobe,
2158 struct strfilter *filter)
2159 {
2160 int ret = 0;
2161 struct probe_trace_event tev;
2162 struct perf_probe_event pev;
2163 struct strlist *rawlist;
2164 struct str_node *ent;
2165
2166 memset(&tev, 0, sizeof(tev));
2167 memset(&pev, 0, sizeof(pev));
2168
2169 rawlist = probe_file__get_rawlist(fd);
2170 if (!rawlist)
2171 return -ENOMEM;
2172
2173 strlist__for_each(ent, rawlist) {
2174 ret = parse_probe_trace_command(ent->s, &tev);
2175 if (ret >= 0) {
2176 if (!filter_probe_trace_event(&tev, filter))
2177 goto next;
2178 ret = convert_to_perf_probe_event(&tev, &pev,
2179 is_kprobe);
2180 if (ret < 0)
2181 goto next;
2182 ret = show_perf_probe_event(pev.group, pev.event,
2183 &pev, tev.point.module,
2184 true);
2185 }
2186 next:
2187 clear_perf_probe_event(&pev);
2188 clear_probe_trace_event(&tev);
2189 if (ret < 0)
2190 break;
2191 }
2192 strlist__delete(rawlist);
2193 /* Cleanup cached debuginfo if needed */
2194 debuginfo_cache__exit();
2195
2196 return ret;
2197 }
2198
2199 /* List up current perf-probe events */
2200 int show_perf_probe_events(struct strfilter *filter)
2201 {
2202 int kp_fd, up_fd, ret;
2203
2204 setup_pager();
2205
2206 ret = init_symbol_maps(false);
2207 if (ret < 0)
2208 return ret;
2209
2210 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2211 if (ret < 0)
2212 return ret;
2213
2214 if (kp_fd >= 0)
2215 ret = __show_perf_probe_events(kp_fd, true, filter);
2216 if (up_fd >= 0 && ret >= 0)
2217 ret = __show_perf_probe_events(up_fd, false, filter);
2218 if (kp_fd > 0)
2219 close(kp_fd);
2220 if (up_fd > 0)
2221 close(up_fd);
2222 exit_symbol_maps();
2223
2224 return ret;
2225 }
2226
2227 static int get_new_event_name(char *buf, size_t len, const char *base,
2228 struct strlist *namelist, bool allow_suffix)
2229 {
2230 int i, ret;
2231 char *p;
2232
2233 if (*base == '.')
2234 base++;
2235
2236 /* Try no suffix */
2237 ret = e_snprintf(buf, len, "%s", base);
2238 if (ret < 0) {
2239 pr_debug("snprintf() failed: %d\n", ret);
2240 return ret;
2241 }
2242 /* Cut off the postfixes (e.g. .const, .isra)*/
2243 p = strchr(buf, '.');
2244 if (p && p != buf)
2245 *p = '\0';
2246 if (!strlist__has_entry(namelist, buf))
2247 return 0;
2248
2249 if (!allow_suffix) {
2250 pr_warning("Error: event \"%s\" already exists. "
2251 "(Use -f to force duplicates.)\n", base);
2252 return -EEXIST;
2253 }
2254
2255 /* Try to add suffix */
2256 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2257 ret = e_snprintf(buf, len, "%s_%d", base, i);
2258 if (ret < 0) {
2259 pr_debug("snprintf() failed: %d\n", ret);
2260 return ret;
2261 }
2262 if (!strlist__has_entry(namelist, buf))
2263 break;
2264 }
2265 if (i == MAX_EVENT_INDEX) {
2266 pr_warning("Too many events are on the same function.\n");
2267 ret = -ERANGE;
2268 }
2269
2270 return ret;
2271 }
2272
2273 /* Warn if the current kernel's uprobe implementation is old */
2274 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2275 {
2276 int i;
2277 char *buf = synthesize_probe_trace_command(tev);
2278
2279 /* Old uprobe event doesn't support memory dereference */
2280 if (!tev->uprobes || tev->nargs == 0 || !buf)
2281 goto out;
2282
2283 for (i = 0; i < tev->nargs; i++)
2284 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2285 pr_warning("Please upgrade your kernel to at least "
2286 "3.14 to have access to feature %s\n",
2287 tev->args[i].value);
2288 break;
2289 }
2290 out:
2291 free(buf);
2292 }
2293
2294 /* Set new name from original perf_probe_event and namelist */
2295 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2296 struct perf_probe_event *pev,
2297 struct strlist *namelist,
2298 bool allow_suffix)
2299 {
2300 const char *event, *group;
2301 char buf[64];
2302 int ret;
2303
2304 if (pev->event)
2305 event = pev->event;
2306 else
2307 if (pev->point.function && !strisglob(pev->point.function))
2308 event = pev->point.function;
2309 else
2310 event = tev->point.realname;
2311 if (pev->group)
2312 group = pev->group;
2313 else
2314 group = PERFPROBE_GROUP;
2315
2316 /* Get an unused new event name */
2317 ret = get_new_event_name(buf, 64, event,
2318 namelist, allow_suffix);
2319 if (ret < 0)
2320 return ret;
2321
2322 event = buf;
2323
2324 tev->event = strdup(event);
2325 tev->group = strdup(group);
2326 if (tev->event == NULL || tev->group == NULL)
2327 return -ENOMEM;
2328
2329 /* Add added event name to namelist */
2330 strlist__add(namelist, event);
2331 return 0;
2332 }
2333
2334 static int __add_probe_trace_events(struct perf_probe_event *pev,
2335 struct probe_trace_event *tevs,
2336 int ntevs, bool allow_suffix)
2337 {
2338 int i, fd, ret;
2339 struct probe_trace_event *tev = NULL;
2340 const char *event = NULL, *group = NULL;
2341 struct strlist *namelist;
2342
2343 fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
2344 if (fd < 0)
2345 return fd;
2346
2347 /* Get current event names */
2348 namelist = probe_file__get_namelist(fd);
2349 if (!namelist) {
2350 pr_debug("Failed to get current event list.\n");
2351 ret = -ENOMEM;
2352 goto close_out;
2353 }
2354
2355 ret = 0;
2356 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
2357 for (i = 0; i < ntevs; i++) {
2358 tev = &tevs[i];
2359 /* Skip if the symbol is out of .text or blacklisted */
2360 if (!tev->point.symbol)
2361 continue;
2362
2363 /* Set new name for tev (and update namelist) */
2364 ret = probe_trace_event__set_name(tev, pev, namelist,
2365 allow_suffix);
2366 if (ret < 0)
2367 break;
2368
2369 ret = probe_file__add_event(fd, tev);
2370 if (ret < 0)
2371 break;
2372
2373 /* We use tev's name for showing new events */
2374 show_perf_probe_event(tev->group, tev->event, pev,
2375 tev->point.module, false);
2376 /* Save the last valid name */
2377 event = tev->event;
2378 group = tev->group;
2379
2380 /*
2381 * Probes after the first probe which comes from same
2382 * user input are always allowed to add suffix, because
2383 * there might be several addresses corresponding to
2384 * one code line.
2385 */
2386 allow_suffix = true;
2387 }
2388 if (ret == -EINVAL && pev->uprobes)
2389 warn_uprobe_event_compat(tev);
2390
2391 /* Note that it is possible to skip all events because of blacklist */
2392 if (ret >= 0 && event) {
2393 /* Show how to use the event. */
2394 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2395 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group, event);
2396 }
2397
2398 strlist__delete(namelist);
2399 close_out:
2400 close(fd);
2401 return ret;
2402 }
2403
2404 static int find_probe_functions(struct map *map, char *name,
2405 struct symbol **syms)
2406 {
2407 int found = 0;
2408 struct symbol *sym;
2409 struct rb_node *tmp;
2410
2411 if (map__load(map, NULL) < 0)
2412 return 0;
2413
2414 map__for_each_symbol(map, sym, tmp) {
2415 if (strglobmatch(sym->name, name)) {
2416 found++;
2417 if (syms && found < probe_conf.max_probes)
2418 syms[found - 1] = sym;
2419 }
2420 }
2421
2422 return found;
2423 }
2424
2425 #define strdup_or_goto(str, label) \
2426 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2427
2428 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2429 struct probe_trace_event *tev __maybe_unused,
2430 struct map *map __maybe_unused) { }
2431
2432 /*
2433 * Find probe function addresses from map.
2434 * Return an error or the number of found probe_trace_event
2435 */
2436 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2437 struct probe_trace_event **tevs)
2438 {
2439 struct map *map = NULL;
2440 struct ref_reloc_sym *reloc_sym = NULL;
2441 struct symbol *sym;
2442 struct symbol **syms = NULL;
2443 struct probe_trace_event *tev;
2444 struct perf_probe_point *pp = &pev->point;
2445 struct probe_trace_point *tp;
2446 int num_matched_functions;
2447 int ret, i, j, skipped = 0;
2448
2449 map = get_target_map(pev->target, pev->uprobes);
2450 if (!map) {
2451 ret = -EINVAL;
2452 goto out;
2453 }
2454
2455 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2456 if (!syms) {
2457 ret = -ENOMEM;
2458 goto out;
2459 }
2460
2461 /*
2462 * Load matched symbols: Since the different local symbols may have
2463 * same name but different addresses, this lists all the symbols.
2464 */
2465 num_matched_functions = find_probe_functions(map, pp->function, syms);
2466 if (num_matched_functions == 0) {
2467 pr_err("Failed to find symbol %s in %s\n", pp->function,
2468 pev->target ? : "kernel");
2469 ret = -ENOENT;
2470 goto out;
2471 } else if (num_matched_functions > probe_conf.max_probes) {
2472 pr_err("Too many functions matched in %s\n",
2473 pev->target ? : "kernel");
2474 ret = -E2BIG;
2475 goto out;
2476 }
2477
2478 if (!pev->uprobes && !pp->retprobe) {
2479 reloc_sym = kernel_get_ref_reloc_sym();
2480 if (!reloc_sym) {
2481 pr_warning("Relocated base symbol is not found!\n");
2482 ret = -EINVAL;
2483 goto out;
2484 }
2485 }
2486
2487 /* Setup result trace-probe-events */
2488 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2489 if (!*tevs) {
2490 ret = -ENOMEM;
2491 goto out;
2492 }
2493
2494 ret = 0;
2495
2496 for (j = 0; j < num_matched_functions; j++) {
2497 sym = syms[j];
2498
2499 tev = (*tevs) + ret;
2500 tp = &tev->point;
2501 if (ret == num_matched_functions) {
2502 pr_warning("Too many symbols are listed. Skip it.\n");
2503 break;
2504 }
2505 ret++;
2506
2507 if (pp->offset > sym->end - sym->start) {
2508 pr_warning("Offset %ld is bigger than the size of %s\n",
2509 pp->offset, sym->name);
2510 ret = -ENOENT;
2511 goto err_out;
2512 }
2513 /* Add one probe point */
2514 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2515 /* If we found a wrong one, mark it by NULL symbol */
2516 if (!pev->uprobes &&
2517 kprobe_warn_out_range(sym->name, tp->address)) {
2518 tp->symbol = NULL; /* Skip it */
2519 skipped++;
2520 } else if (reloc_sym) {
2521 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2522 tp->offset = tp->address - reloc_sym->addr;
2523 } else {
2524 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2525 tp->offset = pp->offset;
2526 }
2527 tp->realname = strdup_or_goto(sym->name, nomem_out);
2528
2529 tp->retprobe = pp->retprobe;
2530 if (pev->target)
2531 tev->point.module = strdup_or_goto(pev->target,
2532 nomem_out);
2533 tev->uprobes = pev->uprobes;
2534 tev->nargs = pev->nargs;
2535 if (tev->nargs) {
2536 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2537 tev->nargs);
2538 if (tev->args == NULL)
2539 goto nomem_out;
2540 }
2541 for (i = 0; i < tev->nargs; i++) {
2542 if (pev->args[i].name)
2543 tev->args[i].name =
2544 strdup_or_goto(pev->args[i].name,
2545 nomem_out);
2546
2547 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2548 nomem_out);
2549 if (pev->args[i].type)
2550 tev->args[i].type =
2551 strdup_or_goto(pev->args[i].type,
2552 nomem_out);
2553 }
2554 arch__fix_tev_from_maps(pev, tev, map);
2555 }
2556 if (ret == skipped) {
2557 ret = -ENOENT;
2558 goto err_out;
2559 }
2560
2561 out:
2562 put_target_map(map, pev->uprobes);
2563 free(syms);
2564 return ret;
2565
2566 nomem_out:
2567 ret = -ENOMEM;
2568 err_out:
2569 clear_probe_trace_events(*tevs, num_matched_functions);
2570 zfree(tevs);
2571 goto out;
2572 }
2573
2574 bool __weak arch__prefers_symtab(void) { return false; }
2575
2576 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2577 struct probe_trace_event **tevs)
2578 {
2579 int ret;
2580
2581 if (pev->uprobes && !pev->group) {
2582 /* Replace group name if not given */
2583 ret = convert_exec_to_group(pev->target, &pev->group);
2584 if (ret != 0) {
2585 pr_warning("Failed to make a group name.\n");
2586 return ret;
2587 }
2588 }
2589
2590 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2591 ret = find_probe_trace_events_from_map(pev, tevs);
2592 if (ret > 0)
2593 return ret; /* Found in symbol table */
2594 }
2595
2596 /* Convert perf_probe_event with debuginfo */
2597 ret = try_to_find_probe_trace_events(pev, tevs);
2598 if (ret != 0)
2599 return ret; /* Found in debuginfo or got an error */
2600
2601 return find_probe_trace_events_from_map(pev, tevs);
2602 }
2603
2604 struct __event_package {
2605 struct perf_probe_event *pev;
2606 struct probe_trace_event *tevs;
2607 int ntevs;
2608 };
2609
2610 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2611 {
2612 int i, j, ret;
2613 struct __event_package *pkgs;
2614
2615 ret = 0;
2616 pkgs = zalloc(sizeof(struct __event_package) * npevs);
2617
2618 if (pkgs == NULL)
2619 return -ENOMEM;
2620
2621 ret = init_symbol_maps(pevs->uprobes);
2622 if (ret < 0) {
2623 free(pkgs);
2624 return ret;
2625 }
2626
2627 /* Loop 1: convert all events */
2628 for (i = 0; i < npevs; i++) {
2629 pkgs[i].pev = &pevs[i];
2630 /* Init kprobe blacklist if needed */
2631 if (!pkgs[i].pev->uprobes)
2632 kprobe_blacklist__init();
2633 /* Convert with or without debuginfo */
2634 ret = convert_to_probe_trace_events(pkgs[i].pev,
2635 &pkgs[i].tevs);
2636 if (ret < 0)
2637 goto end;
2638 pkgs[i].ntevs = ret;
2639 }
2640 /* This just release blacklist only if allocated */
2641 kprobe_blacklist__release();
2642
2643 /* Loop 2: add all events */
2644 for (i = 0; i < npevs; i++) {
2645 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
2646 pkgs[i].ntevs,
2647 probe_conf.force_add);
2648 if (ret < 0)
2649 break;
2650 }
2651 end:
2652 /* Loop 3: cleanup and free trace events */
2653 for (i = 0; i < npevs; i++) {
2654 for (j = 0; j < pkgs[i].ntevs; j++)
2655 clear_probe_trace_event(&pkgs[i].tevs[j]);
2656 zfree(&pkgs[i].tevs);
2657 }
2658 free(pkgs);
2659 exit_symbol_maps();
2660
2661 return ret;
2662 }
2663
2664 int del_perf_probe_events(struct strfilter *filter)
2665 {
2666 int ret, ret2, ufd = -1, kfd = -1;
2667 char *str = strfilter__string(filter);
2668
2669 if (!str)
2670 return -EINVAL;
2671
2672 pr_debug("Delete filter: \'%s\'\n", str);
2673
2674 /* Get current event names */
2675 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
2676 if (ret < 0)
2677 goto out;
2678
2679 ret = probe_file__del_events(kfd, filter);
2680 if (ret < 0 && ret != -ENOENT)
2681 goto error;
2682
2683 ret2 = probe_file__del_events(ufd, filter);
2684 if (ret2 < 0 && ret2 != -ENOENT) {
2685 ret = ret2;
2686 goto error;
2687 }
2688 if (ret == -ENOENT && ret2 == -ENOENT)
2689 pr_debug("\"%s\" does not hit any event.\n", str);
2690 /* Note that this is silently ignored */
2691 ret = 0;
2692
2693 error:
2694 if (kfd >= 0)
2695 close(kfd);
2696 if (ufd >= 0)
2697 close(ufd);
2698 out:
2699 free(str);
2700
2701 return ret;
2702 }
2703
2704 /* TODO: don't use a global variable for filter ... */
2705 static struct strfilter *available_func_filter;
2706
2707 /*
2708 * If a symbol corresponds to a function with global binding and
2709 * matches filter return 0. For all others return 1.
2710 */
2711 static int filter_available_functions(struct map *map __maybe_unused,
2712 struct symbol *sym)
2713 {
2714 if (strfilter__compare(available_func_filter, sym->name))
2715 return 0;
2716 return 1;
2717 }
2718
2719 int show_available_funcs(const char *target, struct strfilter *_filter,
2720 bool user)
2721 {
2722 struct map *map;
2723 int ret;
2724
2725 ret = init_symbol_maps(user);
2726 if (ret < 0)
2727 return ret;
2728
2729 /* Get a symbol map */
2730 if (user)
2731 map = dso__new_map(target);
2732 else
2733 map = kernel_get_module_map(target);
2734 if (!map) {
2735 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2736 return -EINVAL;
2737 }
2738
2739 /* Load symbols with given filter */
2740 available_func_filter = _filter;
2741 if (map__load(map, filter_available_functions)) {
2742 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2743 goto end;
2744 }
2745 if (!dso__sorted_by_name(map->dso, map->type))
2746 dso__sort_by_name(map->dso, map->type);
2747
2748 /* Show all (filtered) symbols */
2749 setup_pager();
2750 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2751 end:
2752 if (user) {
2753 map__put(map);
2754 }
2755 exit_symbol_maps();
2756
2757 return ret;
2758 }
2759
This page took 0.268228 seconds and 5 git commands to generate.