perf probe: Free perf_probe_event in cleanup_perf_probe_events()
[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/fs.h>
44 #include "trace-event.h" /* For __maybe_unused */
45 #include "probe-event.h"
46 #include "probe-finder.h"
47 #include "probe-file.h"
48 #include "session.h"
49
50 #define MAX_CMDLEN 256
51 #define PERFPROBE_GROUP "probe"
52
53 bool probe_event_dry_run; /* Dry run flag */
54 struct probe_conf probe_conf;
55
56 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
57
58 int e_snprintf(char *str, size_t size, const char *format, ...)
59 {
60 int ret;
61 va_list ap;
62 va_start(ap, format);
63 ret = vsnprintf(str, size, format, ap);
64 va_end(ap);
65 if (ret >= (int)size)
66 ret = -E2BIG;
67 return ret;
68 }
69
70 static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
71 static struct machine *host_machine;
72
73 /* Initialize symbol maps and path of vmlinux/modules */
74 static int init_symbol_maps(bool user_only)
75 {
76 int ret;
77
78 symbol_conf.sort_by_name = true;
79 symbol_conf.allow_aliases = true;
80 ret = symbol__init(NULL);
81 if (ret < 0) {
82 pr_debug("Failed to init symbol map.\n");
83 goto out;
84 }
85
86 if (host_machine || user_only) /* already initialized */
87 return 0;
88
89 if (symbol_conf.vmlinux_name)
90 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
91
92 host_machine = machine__new_host();
93 if (!host_machine) {
94 pr_debug("machine__new_host() failed.\n");
95 symbol__exit();
96 ret = -1;
97 }
98 out:
99 if (ret < 0)
100 pr_warning("Failed to init vmlinux path.\n");
101 return ret;
102 }
103
104 static void exit_symbol_maps(void)
105 {
106 if (host_machine) {
107 machine__delete(host_machine);
108 host_machine = NULL;
109 }
110 symbol__exit();
111 }
112
113 static struct symbol *__find_kernel_function_by_name(const char *name,
114 struct map **mapp)
115 {
116 return machine__find_kernel_function_by_name(host_machine, name, mapp,
117 NULL);
118 }
119
120 static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
121 {
122 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
123 }
124
125 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
126 {
127 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
128 struct kmap *kmap;
129
130 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
131 return NULL;
132
133 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
134 if (!kmap)
135 return NULL;
136 return kmap->ref_reloc_sym;
137 }
138
139 static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
140 {
141 struct ref_reloc_sym *reloc_sym;
142 struct symbol *sym;
143 struct map *map;
144
145 /* ref_reloc_sym is just a label. Need a special fix*/
146 reloc_sym = kernel_get_ref_reloc_sym();
147 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
148 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
149 else {
150 sym = __find_kernel_function_by_name(name, &map);
151 if (sym)
152 return map->unmap_ip(map, sym->start) -
153 ((reloc) ? 0 : map->reloc);
154 }
155 return 0;
156 }
157
158 static struct map *kernel_get_module_map(const char *module)
159 {
160 struct map_groups *grp = &host_machine->kmaps;
161 struct maps *maps = &grp->maps[MAP__FUNCTION];
162 struct map *pos;
163
164 /* A file path -- this is an offline module */
165 if (module && strchr(module, '/'))
166 return machine__findnew_module_map(host_machine, 0, module);
167
168 if (!module)
169 module = "kernel";
170
171 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
172 if (strncmp(pos->dso->short_name + 1, module,
173 pos->dso->short_name_len - 2) == 0) {
174 return pos;
175 }
176 }
177 return NULL;
178 }
179
180 static struct map *get_target_map(const char *target, bool user)
181 {
182 /* Init maps of given executable or kernel */
183 if (user)
184 return dso__new_map(target);
185 else
186 return kernel_get_module_map(target);
187 }
188
189 static void put_target_map(struct map *map, bool user)
190 {
191 if (map && user) {
192 /* Only the user map needs to be released */
193 map__put(map);
194 }
195 }
196
197
198 static int convert_exec_to_group(const char *exec, char **result)
199 {
200 char *ptr1, *ptr2, *exec_copy;
201 char buf[64];
202 int ret;
203
204 exec_copy = strdup(exec);
205 if (!exec_copy)
206 return -ENOMEM;
207
208 ptr1 = basename(exec_copy);
209 if (!ptr1) {
210 ret = -EINVAL;
211 goto out;
212 }
213
214 ptr2 = strpbrk(ptr1, "-._");
215 if (ptr2)
216 *ptr2 = '\0';
217 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
218 if (ret < 0)
219 goto out;
220
221 *result = strdup(buf);
222 ret = *result ? 0 : -ENOMEM;
223
224 out:
225 free(exec_copy);
226 return ret;
227 }
228
229 static void clear_perf_probe_point(struct perf_probe_point *pp)
230 {
231 free(pp->file);
232 free(pp->function);
233 free(pp->lazy_line);
234 }
235
236 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
237 {
238 int i;
239
240 for (i = 0; i < ntevs; i++)
241 clear_probe_trace_event(tevs + i);
242 }
243
244 static bool kprobe_blacklist__listed(unsigned long address);
245 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
246 {
247 u64 etext_addr;
248
249 /* Get the address of _etext for checking non-probable text symbol */
250 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
251
252 if (etext_addr != 0 && etext_addr < address)
253 pr_warning("%s is out of .text, skip it.\n", symbol);
254 else if (kprobe_blacklist__listed(address))
255 pr_warning("%s is blacklisted function, skip it.\n", symbol);
256 else
257 return false;
258
259 return true;
260 }
261
262 #ifdef HAVE_DWARF_SUPPORT
263
264 static int kernel_get_module_dso(const char *module, struct dso **pdso)
265 {
266 struct dso *dso;
267 struct map *map;
268 const char *vmlinux_name;
269 int ret = 0;
270
271 if (module) {
272 list_for_each_entry(dso, &host_machine->dsos.head, node) {
273 if (!dso->kernel)
274 continue;
275 if (strncmp(dso->short_name + 1, module,
276 dso->short_name_len - 2) == 0)
277 goto found;
278 }
279 pr_debug("Failed to find module %s.\n", module);
280 return -ENOENT;
281 }
282
283 map = host_machine->vmlinux_maps[MAP__FUNCTION];
284 dso = map->dso;
285
286 vmlinux_name = symbol_conf.vmlinux_name;
287 dso->load_errno = 0;
288 if (vmlinux_name)
289 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
290 else
291 ret = dso__load_vmlinux_path(dso, map, NULL);
292 found:
293 *pdso = dso;
294 return ret;
295 }
296
297 /*
298 * Some binaries like glibc have special symbols which are on the symbol
299 * table, but not in the debuginfo. If we can find the address of the
300 * symbol from map, we can translate the address back to the probe point.
301 */
302 static int find_alternative_probe_point(struct debuginfo *dinfo,
303 struct perf_probe_point *pp,
304 struct perf_probe_point *result,
305 const char *target, bool uprobes)
306 {
307 struct map *map = NULL;
308 struct symbol *sym;
309 u64 address = 0;
310 int ret = -ENOENT;
311
312 /* This can work only for function-name based one */
313 if (!pp->function || pp->file)
314 return -ENOTSUP;
315
316 map = get_target_map(target, uprobes);
317 if (!map)
318 return -EINVAL;
319
320 /* Find the address of given function */
321 map__for_each_symbol_by_name(map, pp->function, sym) {
322 if (uprobes)
323 address = sym->start;
324 else
325 address = map->unmap_ip(map, sym->start);
326 break;
327 }
328 if (!address) {
329 ret = -ENOENT;
330 goto out;
331 }
332 pr_debug("Symbol %s address found : %" PRIx64 "\n",
333 pp->function, address);
334
335 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
336 result);
337 if (ret <= 0)
338 ret = (!ret) ? -ENOENT : ret;
339 else {
340 result->offset += pp->offset;
341 result->line += pp->line;
342 result->retprobe = pp->retprobe;
343 ret = 0;
344 }
345
346 out:
347 put_target_map(map, uprobes);
348 return ret;
349
350 }
351
352 static int get_alternative_probe_event(struct debuginfo *dinfo,
353 struct perf_probe_event *pev,
354 struct perf_probe_point *tmp)
355 {
356 int ret;
357
358 memcpy(tmp, &pev->point, sizeof(*tmp));
359 memset(&pev->point, 0, sizeof(pev->point));
360 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
361 pev->target, pev->uprobes);
362 if (ret < 0)
363 memcpy(&pev->point, tmp, sizeof(*tmp));
364
365 return ret;
366 }
367
368 static int get_alternative_line_range(struct debuginfo *dinfo,
369 struct line_range *lr,
370 const char *target, bool user)
371 {
372 struct perf_probe_point pp = { .function = lr->function,
373 .file = lr->file,
374 .line = lr->start };
375 struct perf_probe_point result;
376 int ret, len = 0;
377
378 memset(&result, 0, sizeof(result));
379
380 if (lr->end != INT_MAX)
381 len = lr->end - lr->start;
382 ret = find_alternative_probe_point(dinfo, &pp, &result,
383 target, user);
384 if (!ret) {
385 lr->function = result.function;
386 lr->file = result.file;
387 lr->start = result.line;
388 if (lr->end != INT_MAX)
389 lr->end = lr->start + len;
390 clear_perf_probe_point(&pp);
391 }
392 return ret;
393 }
394
395 /* Open new debuginfo of given module */
396 static struct debuginfo *open_debuginfo(const char *module, bool silent)
397 {
398 const char *path = module;
399 char reason[STRERR_BUFSIZE];
400 struct debuginfo *ret = NULL;
401 struct dso *dso = NULL;
402 int err;
403
404 if (!module || !strchr(module, '/')) {
405 err = kernel_get_module_dso(module, &dso);
406 if (err < 0) {
407 if (!dso || dso->load_errno == 0) {
408 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
409 strcpy(reason, "(unknown)");
410 } else
411 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
412 if (!silent)
413 pr_err("Failed to find the path for %s: %s\n",
414 module ?: "kernel", reason);
415 return NULL;
416 }
417 path = dso->long_name;
418 }
419 ret = debuginfo__new(path);
420 if (!ret && !silent) {
421 pr_warning("The %s file has no debug information.\n", path);
422 if (!module || !strtailcmp(path, ".ko"))
423 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
424 else
425 pr_warning("Rebuild with -g, ");
426 pr_warning("or install an appropriate debuginfo package.\n");
427 }
428 return ret;
429 }
430
431 /* For caching the last debuginfo */
432 static struct debuginfo *debuginfo_cache;
433 static char *debuginfo_cache_path;
434
435 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
436 {
437 if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
438 (!debuginfo_cache_path && !module && debuginfo_cache))
439 goto out;
440
441 /* Copy module path */
442 free(debuginfo_cache_path);
443 if (module) {
444 debuginfo_cache_path = strdup(module);
445 if (!debuginfo_cache_path) {
446 debuginfo__delete(debuginfo_cache);
447 debuginfo_cache = NULL;
448 goto out;
449 }
450 }
451
452 debuginfo_cache = open_debuginfo(module, silent);
453 if (!debuginfo_cache)
454 zfree(&debuginfo_cache_path);
455 out:
456 return debuginfo_cache;
457 }
458
459 static void debuginfo_cache__exit(void)
460 {
461 debuginfo__delete(debuginfo_cache);
462 debuginfo_cache = NULL;
463 zfree(&debuginfo_cache_path);
464 }
465
466
467 static int get_text_start_address(const char *exec, unsigned long *address)
468 {
469 Elf *elf;
470 GElf_Ehdr ehdr;
471 GElf_Shdr shdr;
472 int fd, ret = -ENOENT;
473
474 fd = open(exec, O_RDONLY);
475 if (fd < 0)
476 return -errno;
477
478 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
479 if (elf == NULL)
480 return -EINVAL;
481
482 if (gelf_getehdr(elf, &ehdr) == NULL)
483 goto out;
484
485 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
486 goto out;
487
488 *address = shdr.sh_addr - shdr.sh_offset;
489 ret = 0;
490 out:
491 elf_end(elf);
492 return ret;
493 }
494
495 /*
496 * Convert trace point to probe point with debuginfo
497 */
498 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
499 struct perf_probe_point *pp,
500 bool is_kprobe)
501 {
502 struct debuginfo *dinfo = NULL;
503 unsigned long stext = 0;
504 u64 addr = tp->address;
505 int ret = -ENOENT;
506
507 /* convert the address to dwarf address */
508 if (!is_kprobe) {
509 if (!addr) {
510 ret = -EINVAL;
511 goto error;
512 }
513 ret = get_text_start_address(tp->module, &stext);
514 if (ret < 0)
515 goto error;
516 addr += stext;
517 } else if (tp->symbol) {
518 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
519 if (addr == 0)
520 goto error;
521 addr += tp->offset;
522 }
523
524 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
525 tp->module ? : "kernel");
526
527 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
528 if (dinfo)
529 ret = debuginfo__find_probe_point(dinfo,
530 (unsigned long)addr, pp);
531 else
532 ret = -ENOENT;
533
534 if (ret > 0) {
535 pp->retprobe = tp->retprobe;
536 return 0;
537 }
538 error:
539 pr_debug("Failed to find corresponding probes from debuginfo.\n");
540 return ret ? : -ENOENT;
541 }
542
543 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
544 int ntevs, const char *exec)
545 {
546 int i, ret = 0;
547 unsigned long stext = 0;
548
549 if (!exec)
550 return 0;
551
552 ret = get_text_start_address(exec, &stext);
553 if (ret < 0)
554 return ret;
555
556 for (i = 0; i < ntevs && ret >= 0; i++) {
557 /* point.address is the addres of point.symbol + point.offset */
558 tevs[i].point.address -= stext;
559 tevs[i].point.module = strdup(exec);
560 if (!tevs[i].point.module) {
561 ret = -ENOMEM;
562 break;
563 }
564 tevs[i].uprobes = true;
565 }
566
567 return ret;
568 }
569
570 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
571 int ntevs, const char *module)
572 {
573 int i, ret = 0;
574 char *tmp;
575
576 if (!module)
577 return 0;
578
579 tmp = strrchr(module, '/');
580 if (tmp) {
581 /* This is a module path -- get the module name */
582 module = strdup(tmp + 1);
583 if (!module)
584 return -ENOMEM;
585 tmp = strchr(module, '.');
586 if (tmp)
587 *tmp = '\0';
588 tmp = (char *)module; /* For free() */
589 }
590
591 for (i = 0; i < ntevs; i++) {
592 tevs[i].point.module = strdup(module);
593 if (!tevs[i].point.module) {
594 ret = -ENOMEM;
595 break;
596 }
597 }
598
599 free(tmp);
600 return ret;
601 }
602
603 /* Post processing the probe events */
604 static int post_process_probe_trace_events(struct probe_trace_event *tevs,
605 int ntevs, const char *module,
606 bool uprobe)
607 {
608 struct ref_reloc_sym *reloc_sym;
609 char *tmp;
610 int i, skipped = 0;
611
612 if (uprobe)
613 return add_exec_to_probe_trace_events(tevs, ntevs, module);
614
615 /* Note that currently ref_reloc_sym based probe is not for drivers */
616 if (module)
617 return add_module_to_probe_trace_events(tevs, ntevs, module);
618
619 reloc_sym = kernel_get_ref_reloc_sym();
620 if (!reloc_sym) {
621 pr_warning("Relocated base symbol is not found!\n");
622 return -EINVAL;
623 }
624
625 for (i = 0; i < ntevs; i++) {
626 if (!tevs[i].point.address || tevs[i].point.retprobe)
627 continue;
628 /* If we found a wrong one, mark it by NULL symbol */
629 if (kprobe_warn_out_range(tevs[i].point.symbol,
630 tevs[i].point.address)) {
631 tmp = NULL;
632 skipped++;
633 } else {
634 tmp = strdup(reloc_sym->name);
635 if (!tmp)
636 return -ENOMEM;
637 }
638 /* If we have no realname, use symbol for it */
639 if (!tevs[i].point.realname)
640 tevs[i].point.realname = tevs[i].point.symbol;
641 else
642 free(tevs[i].point.symbol);
643 tevs[i].point.symbol = tmp;
644 tevs[i].point.offset = tevs[i].point.address -
645 reloc_sym->unrelocated_addr;
646 }
647 return skipped;
648 }
649
650 /* Try to find perf_probe_event with debuginfo */
651 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
652 struct probe_trace_event **tevs)
653 {
654 bool need_dwarf = perf_probe_event_need_dwarf(pev);
655 struct perf_probe_point tmp;
656 struct debuginfo *dinfo;
657 int ntevs, ret = 0;
658
659 dinfo = open_debuginfo(pev->target, !need_dwarf);
660 if (!dinfo) {
661 if (need_dwarf)
662 return -ENOENT;
663 pr_debug("Could not open debuginfo. Try to use symbols.\n");
664 return 0;
665 }
666
667 pr_debug("Try to find probe point from debuginfo.\n");
668 /* Searching trace events corresponding to a probe event */
669 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
670
671 if (ntevs == 0) { /* Not found, retry with an alternative */
672 ret = get_alternative_probe_event(dinfo, pev, &tmp);
673 if (!ret) {
674 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
675 /*
676 * Write back to the original probe_event for
677 * setting appropriate (user given) event name
678 */
679 clear_perf_probe_point(&pev->point);
680 memcpy(&pev->point, &tmp, sizeof(tmp));
681 }
682 }
683
684 debuginfo__delete(dinfo);
685
686 if (ntevs > 0) { /* Succeeded to find trace events */
687 pr_debug("Found %d probe_trace_events.\n", ntevs);
688 ret = post_process_probe_trace_events(*tevs, ntevs,
689 pev->target, pev->uprobes);
690 if (ret < 0 || ret == ntevs) {
691 clear_probe_trace_events(*tevs, ntevs);
692 zfree(tevs);
693 }
694 if (ret != ntevs)
695 return ret < 0 ? ret : ntevs;
696 ntevs = 0;
697 /* Fall through */
698 }
699
700 if (ntevs == 0) { /* No error but failed to find probe point. */
701 pr_warning("Probe point '%s' not found.\n",
702 synthesize_perf_probe_point(&pev->point));
703 return -ENOENT;
704 }
705 /* Error path : ntevs < 0 */
706 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
707 if (ntevs < 0) {
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 if (arg[0] == '\0')
1197 tmp = NULL;
1198 else {
1199 tmp = strdup(arg);
1200 if (tmp == NULL)
1201 return -ENOMEM;
1202 }
1203
1204 if (file_spec)
1205 pp->file = tmp;
1206 else {
1207 pp->function = tmp;
1208
1209 /*
1210 * Keep pp->function even if this is absolute address,
1211 * so it can mark whether abs_address is valid.
1212 * Which make 'perf probe lib.bin 0x0' possible.
1213 *
1214 * Note that checking length of tmp is not needed
1215 * because when we access tmp[1] we know tmp[0] is '0',
1216 * so tmp[1] should always valid (but could be '\0').
1217 */
1218 if (tmp && !strncmp(tmp, "0x", 2)) {
1219 pp->abs_address = strtoul(pp->function, &tmp, 0);
1220 if (*tmp != '\0') {
1221 semantic_error("Invalid absolute address.\n");
1222 return -EINVAL;
1223 }
1224 }
1225 }
1226
1227 /* Parse other options */
1228 while (ptr) {
1229 arg = ptr;
1230 c = nc;
1231 if (c == ';') { /* Lazy pattern must be the last part */
1232 pp->lazy_line = strdup(arg);
1233 if (pp->lazy_line == NULL)
1234 return -ENOMEM;
1235 break;
1236 }
1237 ptr = strpbrk(arg, ";:+@%");
1238 if (ptr) {
1239 nc = *ptr;
1240 *ptr++ = '\0';
1241 }
1242 switch (c) {
1243 case ':': /* Line number */
1244 pp->line = strtoul(arg, &tmp, 0);
1245 if (*tmp != '\0') {
1246 semantic_error("There is non-digit char"
1247 " in line number.\n");
1248 return -EINVAL;
1249 }
1250 break;
1251 case '+': /* Byte offset from a symbol */
1252 pp->offset = strtoul(arg, &tmp, 0);
1253 if (*tmp != '\0') {
1254 semantic_error("There is non-digit character"
1255 " in offset.\n");
1256 return -EINVAL;
1257 }
1258 break;
1259 case '@': /* File name */
1260 if (pp->file) {
1261 semantic_error("SRC@SRC is not allowed.\n");
1262 return -EINVAL;
1263 }
1264 pp->file = strdup(arg);
1265 if (pp->file == NULL)
1266 return -ENOMEM;
1267 break;
1268 case '%': /* Probe places */
1269 if (strcmp(arg, "return") == 0) {
1270 pp->retprobe = 1;
1271 } else { /* Others not supported yet */
1272 semantic_error("%%%s is not supported.\n", arg);
1273 return -ENOTSUP;
1274 }
1275 break;
1276 default: /* Buggy case */
1277 pr_err("This program has a bug at %s:%d.\n",
1278 __FILE__, __LINE__);
1279 return -ENOTSUP;
1280 break;
1281 }
1282 }
1283
1284 /* Exclusion check */
1285 if (pp->lazy_line && pp->line) {
1286 semantic_error("Lazy pattern can't be used with"
1287 " line number.\n");
1288 return -EINVAL;
1289 }
1290
1291 if (pp->lazy_line && pp->offset) {
1292 semantic_error("Lazy pattern can't be used with offset.\n");
1293 return -EINVAL;
1294 }
1295
1296 if (pp->line && pp->offset) {
1297 semantic_error("Offset can't be used with line number.\n");
1298 return -EINVAL;
1299 }
1300
1301 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1302 semantic_error("File always requires line number or "
1303 "lazy pattern.\n");
1304 return -EINVAL;
1305 }
1306
1307 if (pp->offset && !pp->function) {
1308 semantic_error("Offset requires an entry function.\n");
1309 return -EINVAL;
1310 }
1311
1312 if (pp->retprobe && !pp->function) {
1313 semantic_error("Return probe requires an entry function.\n");
1314 return -EINVAL;
1315 }
1316
1317 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1318 semantic_error("Offset/Line/Lazy pattern can't be used with "
1319 "return probe.\n");
1320 return -EINVAL;
1321 }
1322
1323 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1324 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1325 pp->lazy_line);
1326 return 0;
1327 }
1328
1329 /* Parse perf-probe event argument */
1330 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1331 {
1332 char *tmp, *goodname;
1333 struct perf_probe_arg_field **fieldp;
1334
1335 pr_debug("parsing arg: %s into ", str);
1336
1337 tmp = strchr(str, '=');
1338 if (tmp) {
1339 arg->name = strndup(str, tmp - str);
1340 if (arg->name == NULL)
1341 return -ENOMEM;
1342 pr_debug("name:%s ", arg->name);
1343 str = tmp + 1;
1344 }
1345
1346 tmp = strchr(str, ':');
1347 if (tmp) { /* Type setting */
1348 *tmp = '\0';
1349 arg->type = strdup(tmp + 1);
1350 if (arg->type == NULL)
1351 return -ENOMEM;
1352 pr_debug("type:%s ", arg->type);
1353 }
1354
1355 tmp = strpbrk(str, "-.[");
1356 if (!is_c_varname(str) || !tmp) {
1357 /* A variable, register, symbol or special value */
1358 arg->var = strdup(str);
1359 if (arg->var == NULL)
1360 return -ENOMEM;
1361 pr_debug("%s\n", arg->var);
1362 return 0;
1363 }
1364
1365 /* Structure fields or array element */
1366 arg->var = strndup(str, tmp - str);
1367 if (arg->var == NULL)
1368 return -ENOMEM;
1369 goodname = arg->var;
1370 pr_debug("%s, ", arg->var);
1371 fieldp = &arg->field;
1372
1373 do {
1374 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1375 if (*fieldp == NULL)
1376 return -ENOMEM;
1377 if (*tmp == '[') { /* Array */
1378 str = tmp;
1379 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1380 (*fieldp)->ref = true;
1381 if (*tmp != ']' || tmp == str + 1) {
1382 semantic_error("Array index must be a"
1383 " number.\n");
1384 return -EINVAL;
1385 }
1386 tmp++;
1387 if (*tmp == '\0')
1388 tmp = NULL;
1389 } else { /* Structure */
1390 if (*tmp == '.') {
1391 str = tmp + 1;
1392 (*fieldp)->ref = false;
1393 } else if (tmp[1] == '>') {
1394 str = tmp + 2;
1395 (*fieldp)->ref = true;
1396 } else {
1397 semantic_error("Argument parse error: %s\n",
1398 str);
1399 return -EINVAL;
1400 }
1401 tmp = strpbrk(str, "-.[");
1402 }
1403 if (tmp) {
1404 (*fieldp)->name = strndup(str, tmp - str);
1405 if ((*fieldp)->name == NULL)
1406 return -ENOMEM;
1407 if (*str != '[')
1408 goodname = (*fieldp)->name;
1409 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1410 fieldp = &(*fieldp)->next;
1411 }
1412 } while (tmp);
1413 (*fieldp)->name = strdup(str);
1414 if ((*fieldp)->name == NULL)
1415 return -ENOMEM;
1416 if (*str != '[')
1417 goodname = (*fieldp)->name;
1418 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1419
1420 /* If no name is specified, set the last field name (not array index)*/
1421 if (!arg->name) {
1422 arg->name = strdup(goodname);
1423 if (arg->name == NULL)
1424 return -ENOMEM;
1425 }
1426 return 0;
1427 }
1428
1429 /* Parse perf-probe event command */
1430 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1431 {
1432 char **argv;
1433 int argc, i, ret = 0;
1434
1435 argv = argv_split(cmd, &argc);
1436 if (!argv) {
1437 pr_debug("Failed to split arguments.\n");
1438 return -ENOMEM;
1439 }
1440 if (argc - 1 > MAX_PROBE_ARGS) {
1441 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1442 ret = -ERANGE;
1443 goto out;
1444 }
1445 /* Parse probe point */
1446 ret = parse_perf_probe_point(argv[0], pev);
1447 if (ret < 0)
1448 goto out;
1449
1450 /* Copy arguments and ensure return probe has no C argument */
1451 pev->nargs = argc - 1;
1452 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1453 if (pev->args == NULL) {
1454 ret = -ENOMEM;
1455 goto out;
1456 }
1457 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1458 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1459 if (ret >= 0 &&
1460 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1461 semantic_error("You can't specify local variable for"
1462 " kretprobe.\n");
1463 ret = -EINVAL;
1464 }
1465 }
1466 out:
1467 argv_free(argv);
1468
1469 return ret;
1470 }
1471
1472 /* Return true if this perf_probe_event requires debuginfo */
1473 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1474 {
1475 int i;
1476
1477 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1478 return true;
1479
1480 for (i = 0; i < pev->nargs; i++)
1481 if (is_c_varname(pev->args[i].var))
1482 return true;
1483
1484 return false;
1485 }
1486
1487 /* Parse probe_events event into struct probe_point */
1488 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1489 {
1490 struct probe_trace_point *tp = &tev->point;
1491 char pr;
1492 char *p;
1493 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1494 int ret, i, argc;
1495 char **argv;
1496
1497 pr_debug("Parsing probe_events: %s\n", cmd);
1498 argv = argv_split(cmd, &argc);
1499 if (!argv) {
1500 pr_debug("Failed to split arguments.\n");
1501 return -ENOMEM;
1502 }
1503 if (argc < 2) {
1504 semantic_error("Too few probe arguments.\n");
1505 ret = -ERANGE;
1506 goto out;
1507 }
1508
1509 /* Scan event and group name. */
1510 argv0_str = strdup(argv[0]);
1511 if (argv0_str == NULL) {
1512 ret = -ENOMEM;
1513 goto out;
1514 }
1515 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1516 fmt2_str = strtok_r(NULL, "/", &fmt);
1517 fmt3_str = strtok_r(NULL, " \t", &fmt);
1518 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1519 || fmt3_str == NULL) {
1520 semantic_error("Failed to parse event name: %s\n", argv[0]);
1521 ret = -EINVAL;
1522 goto out;
1523 }
1524 pr = fmt1_str[0];
1525 tev->group = strdup(fmt2_str);
1526 tev->event = strdup(fmt3_str);
1527 if (tev->group == NULL || tev->event == NULL) {
1528 ret = -ENOMEM;
1529 goto out;
1530 }
1531 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1532
1533 tp->retprobe = (pr == 'r');
1534
1535 /* Scan module name(if there), function name and offset */
1536 p = strchr(argv[1], ':');
1537 if (p) {
1538 tp->module = strndup(argv[1], p - argv[1]);
1539 p++;
1540 } else
1541 p = argv[1];
1542 fmt1_str = strtok_r(p, "+", &fmt);
1543 /* only the address started with 0x */
1544 if (fmt1_str[0] == '0') {
1545 /*
1546 * Fix a special case:
1547 * if address == 0, kernel reports something like:
1548 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1549 * Newer kernel may fix that, but we want to
1550 * support old kernel also.
1551 */
1552 if (strcmp(fmt1_str, "0x") == 0) {
1553 if (!argv[2] || strcmp(argv[2], "(null)")) {
1554 ret = -EINVAL;
1555 goto out;
1556 }
1557 tp->address = 0;
1558
1559 free(argv[2]);
1560 for (i = 2; argv[i + 1] != NULL; i++)
1561 argv[i] = argv[i + 1];
1562
1563 argv[i] = NULL;
1564 argc -= 1;
1565 } else
1566 tp->address = strtoul(fmt1_str, NULL, 0);
1567 } else {
1568 /* Only the symbol-based probe has offset */
1569 tp->symbol = strdup(fmt1_str);
1570 if (tp->symbol == NULL) {
1571 ret = -ENOMEM;
1572 goto out;
1573 }
1574 fmt2_str = strtok_r(NULL, "", &fmt);
1575 if (fmt2_str == NULL)
1576 tp->offset = 0;
1577 else
1578 tp->offset = strtoul(fmt2_str, NULL, 10);
1579 }
1580
1581 tev->nargs = argc - 2;
1582 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1583 if (tev->args == NULL) {
1584 ret = -ENOMEM;
1585 goto out;
1586 }
1587 for (i = 0; i < tev->nargs; i++) {
1588 p = strchr(argv[i + 2], '=');
1589 if (p) /* We don't need which register is assigned. */
1590 *p++ = '\0';
1591 else
1592 p = argv[i + 2];
1593 tev->args[i].name = strdup(argv[i + 2]);
1594 /* TODO: parse regs and offset */
1595 tev->args[i].value = strdup(p);
1596 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1597 ret = -ENOMEM;
1598 goto out;
1599 }
1600 }
1601 ret = 0;
1602 out:
1603 free(argv0_str);
1604 argv_free(argv);
1605 return ret;
1606 }
1607
1608 /* Compose only probe arg */
1609 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1610 {
1611 struct perf_probe_arg_field *field = pa->field;
1612 int ret;
1613 char *tmp = buf;
1614
1615 if (pa->name && pa->var)
1616 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1617 else
1618 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1619 if (ret <= 0)
1620 goto error;
1621 tmp += ret;
1622 len -= ret;
1623
1624 while (field) {
1625 if (field->name[0] == '[')
1626 ret = e_snprintf(tmp, len, "%s", field->name);
1627 else
1628 ret = e_snprintf(tmp, len, "%s%s",
1629 field->ref ? "->" : ".", field->name);
1630 if (ret <= 0)
1631 goto error;
1632 tmp += ret;
1633 len -= ret;
1634 field = field->next;
1635 }
1636
1637 if (pa->type) {
1638 ret = e_snprintf(tmp, len, ":%s", pa->type);
1639 if (ret <= 0)
1640 goto error;
1641 tmp += ret;
1642 len -= ret;
1643 }
1644
1645 return tmp - buf;
1646 error:
1647 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1648 return ret;
1649 }
1650
1651 /* Compose only probe point (not argument) */
1652 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1653 {
1654 char *buf, *tmp;
1655 char offs[32] = "", line[32] = "", file[32] = "";
1656 int ret, len;
1657
1658 buf = zalloc(MAX_CMDLEN);
1659 if (buf == NULL) {
1660 ret = -ENOMEM;
1661 goto error;
1662 }
1663 if (pp->offset) {
1664 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1665 if (ret <= 0)
1666 goto error;
1667 }
1668 if (pp->line) {
1669 ret = e_snprintf(line, 32, ":%d", pp->line);
1670 if (ret <= 0)
1671 goto error;
1672 }
1673 if (pp->file) {
1674 tmp = pp->file;
1675 len = strlen(tmp);
1676 if (len > 30) {
1677 tmp = strchr(pp->file + len - 30, '/');
1678 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1679 }
1680 ret = e_snprintf(file, 32, "@%s", tmp);
1681 if (ret <= 0)
1682 goto error;
1683 }
1684
1685 if (pp->function)
1686 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1687 offs, pp->retprobe ? "%return" : "", line,
1688 file);
1689 else
1690 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1691 if (ret <= 0)
1692 goto error;
1693
1694 return buf;
1695 error:
1696 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1697 free(buf);
1698 return NULL;
1699 }
1700
1701 #if 0
1702 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1703 {
1704 char *buf;
1705 int i, len, ret;
1706
1707 buf = synthesize_perf_probe_point(&pev->point);
1708 if (!buf)
1709 return NULL;
1710
1711 len = strlen(buf);
1712 for (i = 0; i < pev->nargs; i++) {
1713 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1714 pev->args[i].name);
1715 if (ret <= 0) {
1716 free(buf);
1717 return NULL;
1718 }
1719 len += ret;
1720 }
1721
1722 return buf;
1723 }
1724 #endif
1725
1726 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1727 char **buf, size_t *buflen,
1728 int depth)
1729 {
1730 int ret;
1731 if (ref->next) {
1732 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1733 buflen, depth + 1);
1734 if (depth < 0)
1735 goto out;
1736 }
1737
1738 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1739 if (ret < 0)
1740 depth = ret;
1741 else {
1742 *buf += ret;
1743 *buflen -= ret;
1744 }
1745 out:
1746 return depth;
1747
1748 }
1749
1750 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1751 char *buf, size_t buflen)
1752 {
1753 struct probe_trace_arg_ref *ref = arg->ref;
1754 int ret, depth = 0;
1755 char *tmp = buf;
1756
1757 /* Argument name or separator */
1758 if (arg->name)
1759 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1760 else
1761 ret = e_snprintf(buf, buflen, " ");
1762 if (ret < 0)
1763 return ret;
1764 buf += ret;
1765 buflen -= ret;
1766
1767 /* Special case: @XXX */
1768 if (arg->value[0] == '@' && arg->ref)
1769 ref = ref->next;
1770
1771 /* Dereferencing arguments */
1772 if (ref) {
1773 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1774 &buflen, 1);
1775 if (depth < 0)
1776 return depth;
1777 }
1778
1779 /* Print argument value */
1780 if (arg->value[0] == '@' && arg->ref)
1781 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1782 arg->ref->offset);
1783 else
1784 ret = e_snprintf(buf, buflen, "%s", arg->value);
1785 if (ret < 0)
1786 return ret;
1787 buf += ret;
1788 buflen -= ret;
1789
1790 /* Closing */
1791 while (depth--) {
1792 ret = e_snprintf(buf, buflen, ")");
1793 if (ret < 0)
1794 return ret;
1795 buf += ret;
1796 buflen -= ret;
1797 }
1798 /* Print argument type */
1799 if (arg->type) {
1800 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1801 if (ret <= 0)
1802 return ret;
1803 buf += ret;
1804 }
1805
1806 return buf - tmp;
1807 }
1808
1809 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1810 {
1811 struct probe_trace_point *tp = &tev->point;
1812 char *buf;
1813 int i, len, ret;
1814
1815 buf = zalloc(MAX_CMDLEN);
1816 if (buf == NULL)
1817 return NULL;
1818
1819 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1820 tev->group, tev->event);
1821 if (len <= 0)
1822 goto error;
1823
1824 /* Uprobes must have tp->module */
1825 if (tev->uprobes && !tp->module)
1826 goto error;
1827 /*
1828 * If tp->address == 0, then this point must be a
1829 * absolute address uprobe.
1830 * try_to_find_absolute_address() should have made
1831 * tp->symbol to "0x0".
1832 */
1833 if (tev->uprobes && !tp->address) {
1834 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1835 goto error;
1836 }
1837
1838 /* Use the tp->address for uprobes */
1839 if (tev->uprobes)
1840 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1841 tp->module, tp->address);
1842 else if (!strncmp(tp->symbol, "0x", 2))
1843 /* Absolute address. See try_to_find_absolute_address() */
1844 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s0x%lx",
1845 tp->module ?: "", tp->module ? ":" : "",
1846 tp->address);
1847 else
1848 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1849 tp->module ?: "", tp->module ? ":" : "",
1850 tp->symbol, tp->offset);
1851
1852 if (ret <= 0)
1853 goto error;
1854 len += ret;
1855
1856 for (i = 0; i < tev->nargs; i++) {
1857 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1858 MAX_CMDLEN - len);
1859 if (ret <= 0)
1860 goto error;
1861 len += ret;
1862 }
1863
1864 return buf;
1865 error:
1866 free(buf);
1867 return NULL;
1868 }
1869
1870 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1871 struct perf_probe_point *pp,
1872 bool is_kprobe)
1873 {
1874 struct symbol *sym = NULL;
1875 struct map *map;
1876 u64 addr = tp->address;
1877 int ret = -ENOENT;
1878
1879 if (!is_kprobe) {
1880 map = dso__new_map(tp->module);
1881 if (!map)
1882 goto out;
1883 sym = map__find_symbol(map, addr, NULL);
1884 } else {
1885 if (tp->symbol)
1886 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1887 if (addr) {
1888 addr += tp->offset;
1889 sym = __find_kernel_function(addr, &map);
1890 }
1891 }
1892 if (!sym)
1893 goto out;
1894
1895 pp->retprobe = tp->retprobe;
1896 pp->offset = addr - map->unmap_ip(map, sym->start);
1897 pp->function = strdup(sym->name);
1898 ret = pp->function ? 0 : -ENOMEM;
1899
1900 out:
1901 if (map && !is_kprobe) {
1902 map__put(map);
1903 }
1904
1905 return ret;
1906 }
1907
1908 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1909 struct perf_probe_point *pp,
1910 bool is_kprobe)
1911 {
1912 char buf[128];
1913 int ret;
1914
1915 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1916 if (!ret)
1917 return 0;
1918 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1919 if (!ret)
1920 return 0;
1921
1922 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1923
1924 if (tp->symbol) {
1925 pp->function = strdup(tp->symbol);
1926 pp->offset = tp->offset;
1927 } else {
1928 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1929 if (ret < 0)
1930 return ret;
1931 pp->function = strdup(buf);
1932 pp->offset = 0;
1933 }
1934 if (pp->function == NULL)
1935 return -ENOMEM;
1936
1937 pp->retprobe = tp->retprobe;
1938
1939 return 0;
1940 }
1941
1942 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1943 struct perf_probe_event *pev, bool is_kprobe)
1944 {
1945 char buf[64] = "";
1946 int i, ret;
1947
1948 /* Convert event/group name */
1949 pev->event = strdup(tev->event);
1950 pev->group = strdup(tev->group);
1951 if (pev->event == NULL || pev->group == NULL)
1952 return -ENOMEM;
1953
1954 /* Convert trace_point to probe_point */
1955 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1956 if (ret < 0)
1957 return ret;
1958
1959 /* Convert trace_arg to probe_arg */
1960 pev->nargs = tev->nargs;
1961 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1962 if (pev->args == NULL)
1963 return -ENOMEM;
1964 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1965 if (tev->args[i].name)
1966 pev->args[i].name = strdup(tev->args[i].name);
1967 else {
1968 ret = synthesize_probe_trace_arg(&tev->args[i],
1969 buf, 64);
1970 pev->args[i].name = strdup(buf);
1971 }
1972 if (pev->args[i].name == NULL && ret >= 0)
1973 ret = -ENOMEM;
1974 }
1975
1976 if (ret < 0)
1977 clear_perf_probe_event(pev);
1978
1979 return ret;
1980 }
1981
1982 void clear_perf_probe_event(struct perf_probe_event *pev)
1983 {
1984 struct perf_probe_arg_field *field, *next;
1985 int i;
1986
1987 free(pev->event);
1988 free(pev->group);
1989 free(pev->target);
1990 clear_perf_probe_point(&pev->point);
1991
1992 for (i = 0; i < pev->nargs; i++) {
1993 free(pev->args[i].name);
1994 free(pev->args[i].var);
1995 free(pev->args[i].type);
1996 field = pev->args[i].field;
1997 while (field) {
1998 next = field->next;
1999 zfree(&field->name);
2000 free(field);
2001 field = next;
2002 }
2003 }
2004 free(pev->args);
2005 memset(pev, 0, sizeof(*pev));
2006 }
2007
2008 void clear_probe_trace_event(struct probe_trace_event *tev)
2009 {
2010 struct probe_trace_arg_ref *ref, *next;
2011 int i;
2012
2013 free(tev->event);
2014 free(tev->group);
2015 free(tev->point.symbol);
2016 free(tev->point.realname);
2017 free(tev->point.module);
2018 for (i = 0; i < tev->nargs; i++) {
2019 free(tev->args[i].name);
2020 free(tev->args[i].value);
2021 free(tev->args[i].type);
2022 ref = tev->args[i].ref;
2023 while (ref) {
2024 next = ref->next;
2025 free(ref);
2026 ref = next;
2027 }
2028 }
2029 free(tev->args);
2030 memset(tev, 0, sizeof(*tev));
2031 }
2032
2033 struct kprobe_blacklist_node {
2034 struct list_head list;
2035 unsigned long start;
2036 unsigned long end;
2037 char *symbol;
2038 };
2039
2040 static void kprobe_blacklist__delete(struct list_head *blacklist)
2041 {
2042 struct kprobe_blacklist_node *node;
2043
2044 while (!list_empty(blacklist)) {
2045 node = list_first_entry(blacklist,
2046 struct kprobe_blacklist_node, list);
2047 list_del(&node->list);
2048 free(node->symbol);
2049 free(node);
2050 }
2051 }
2052
2053 static int kprobe_blacklist__load(struct list_head *blacklist)
2054 {
2055 struct kprobe_blacklist_node *node;
2056 const char *__debugfs = debugfs__mountpoint();
2057 char buf[PATH_MAX], *p;
2058 FILE *fp;
2059 int ret;
2060
2061 if (__debugfs == NULL)
2062 return -ENOTSUP;
2063
2064 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2065 if (ret < 0)
2066 return ret;
2067
2068 fp = fopen(buf, "r");
2069 if (!fp)
2070 return -errno;
2071
2072 ret = 0;
2073 while (fgets(buf, PATH_MAX, fp)) {
2074 node = zalloc(sizeof(*node));
2075 if (!node) {
2076 ret = -ENOMEM;
2077 break;
2078 }
2079 INIT_LIST_HEAD(&node->list);
2080 list_add_tail(&node->list, blacklist);
2081 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2082 ret = -EINVAL;
2083 break;
2084 }
2085 p = strchr(buf, '\t');
2086 if (p) {
2087 p++;
2088 if (p[strlen(p) - 1] == '\n')
2089 p[strlen(p) - 1] = '\0';
2090 } else
2091 p = (char *)"unknown";
2092 node->symbol = strdup(p);
2093 if (!node->symbol) {
2094 ret = -ENOMEM;
2095 break;
2096 }
2097 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2098 node->start, node->end, node->symbol);
2099 ret++;
2100 }
2101 if (ret < 0)
2102 kprobe_blacklist__delete(blacklist);
2103 fclose(fp);
2104
2105 return ret;
2106 }
2107
2108 static struct kprobe_blacklist_node *
2109 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2110 unsigned long address)
2111 {
2112 struct kprobe_blacklist_node *node;
2113
2114 list_for_each_entry(node, blacklist, list) {
2115 if (node->start <= address && address <= node->end)
2116 return node;
2117 }
2118
2119 return NULL;
2120 }
2121
2122 static LIST_HEAD(kprobe_blacklist);
2123
2124 static void kprobe_blacklist__init(void)
2125 {
2126 if (!list_empty(&kprobe_blacklist))
2127 return;
2128
2129 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2130 pr_debug("No kprobe blacklist support, ignored\n");
2131 }
2132
2133 static void kprobe_blacklist__release(void)
2134 {
2135 kprobe_blacklist__delete(&kprobe_blacklist);
2136 }
2137
2138 static bool kprobe_blacklist__listed(unsigned long address)
2139 {
2140 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2141 }
2142
2143 static int perf_probe_event__sprintf(const char *group, const char *event,
2144 struct perf_probe_event *pev,
2145 const char *module,
2146 struct strbuf *result)
2147 {
2148 int i, ret;
2149 char buf[128];
2150 char *place;
2151
2152 /* Synthesize only event probe point */
2153 place = synthesize_perf_probe_point(&pev->point);
2154 if (!place)
2155 return -EINVAL;
2156
2157 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2158 if (ret < 0)
2159 goto out;
2160
2161 strbuf_addf(result, " %-20s (on %s", buf, place);
2162 if (module)
2163 strbuf_addf(result, " in %s", module);
2164
2165 if (pev->nargs > 0) {
2166 strbuf_addstr(result, " with");
2167 for (i = 0; i < pev->nargs; i++) {
2168 ret = synthesize_perf_probe_arg(&pev->args[i],
2169 buf, 128);
2170 if (ret < 0)
2171 goto out;
2172 strbuf_addf(result, " %s", buf);
2173 }
2174 }
2175 strbuf_addch(result, ')');
2176 out:
2177 free(place);
2178 return ret;
2179 }
2180
2181 /* Show an event */
2182 int show_perf_probe_event(const char *group, const char *event,
2183 struct perf_probe_event *pev,
2184 const char *module, bool use_stdout)
2185 {
2186 struct strbuf buf = STRBUF_INIT;
2187 int ret;
2188
2189 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2190 if (ret >= 0) {
2191 if (use_stdout)
2192 printf("%s\n", buf.buf);
2193 else
2194 pr_info("%s\n", buf.buf);
2195 }
2196 strbuf_release(&buf);
2197
2198 return ret;
2199 }
2200
2201 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2202 struct strfilter *filter)
2203 {
2204 char tmp[128];
2205
2206 /* At first, check the event name itself */
2207 if (strfilter__compare(filter, tev->event))
2208 return true;
2209
2210 /* Next, check the combination of name and group */
2211 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2212 return false;
2213 return strfilter__compare(filter, tmp);
2214 }
2215
2216 static int __show_perf_probe_events(int fd, bool is_kprobe,
2217 struct strfilter *filter)
2218 {
2219 int ret = 0;
2220 struct probe_trace_event tev;
2221 struct perf_probe_event pev;
2222 struct strlist *rawlist;
2223 struct str_node *ent;
2224
2225 memset(&tev, 0, sizeof(tev));
2226 memset(&pev, 0, sizeof(pev));
2227
2228 rawlist = probe_file__get_rawlist(fd);
2229 if (!rawlist)
2230 return -ENOMEM;
2231
2232 strlist__for_each(ent, rawlist) {
2233 ret = parse_probe_trace_command(ent->s, &tev);
2234 if (ret >= 0) {
2235 if (!filter_probe_trace_event(&tev, filter))
2236 goto next;
2237 ret = convert_to_perf_probe_event(&tev, &pev,
2238 is_kprobe);
2239 if (ret < 0)
2240 goto next;
2241 ret = show_perf_probe_event(pev.group, pev.event,
2242 &pev, tev.point.module,
2243 true);
2244 }
2245 next:
2246 clear_perf_probe_event(&pev);
2247 clear_probe_trace_event(&tev);
2248 if (ret < 0)
2249 break;
2250 }
2251 strlist__delete(rawlist);
2252 /* Cleanup cached debuginfo if needed */
2253 debuginfo_cache__exit();
2254
2255 return ret;
2256 }
2257
2258 /* List up current perf-probe events */
2259 int show_perf_probe_events(struct strfilter *filter)
2260 {
2261 int kp_fd, up_fd, ret;
2262
2263 setup_pager();
2264
2265 ret = init_symbol_maps(false);
2266 if (ret < 0)
2267 return ret;
2268
2269 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2270 if (ret < 0)
2271 return ret;
2272
2273 if (kp_fd >= 0)
2274 ret = __show_perf_probe_events(kp_fd, true, filter);
2275 if (up_fd >= 0 && ret >= 0)
2276 ret = __show_perf_probe_events(up_fd, false, filter);
2277 if (kp_fd > 0)
2278 close(kp_fd);
2279 if (up_fd > 0)
2280 close(up_fd);
2281 exit_symbol_maps();
2282
2283 return ret;
2284 }
2285
2286 static int get_new_event_name(char *buf, size_t len, const char *base,
2287 struct strlist *namelist, bool allow_suffix)
2288 {
2289 int i, ret;
2290 char *p;
2291
2292 if (*base == '.')
2293 base++;
2294
2295 /* Try no suffix */
2296 ret = e_snprintf(buf, len, "%s", base);
2297 if (ret < 0) {
2298 pr_debug("snprintf() failed: %d\n", ret);
2299 return ret;
2300 }
2301 /* Cut off the postfixes (e.g. .const, .isra)*/
2302 p = strchr(buf, '.');
2303 if (p && p != buf)
2304 *p = '\0';
2305 if (!strlist__has_entry(namelist, buf))
2306 return 0;
2307
2308 if (!allow_suffix) {
2309 pr_warning("Error: event \"%s\" already exists. "
2310 "(Use -f to force duplicates.)\n", base);
2311 return -EEXIST;
2312 }
2313
2314 /* Try to add suffix */
2315 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2316 ret = e_snprintf(buf, len, "%s_%d", base, i);
2317 if (ret < 0) {
2318 pr_debug("snprintf() failed: %d\n", ret);
2319 return ret;
2320 }
2321 if (!strlist__has_entry(namelist, buf))
2322 break;
2323 }
2324 if (i == MAX_EVENT_INDEX) {
2325 pr_warning("Too many events are on the same function.\n");
2326 ret = -ERANGE;
2327 }
2328
2329 return ret;
2330 }
2331
2332 /* Warn if the current kernel's uprobe implementation is old */
2333 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2334 {
2335 int i;
2336 char *buf = synthesize_probe_trace_command(tev);
2337
2338 /* Old uprobe event doesn't support memory dereference */
2339 if (!tev->uprobes || tev->nargs == 0 || !buf)
2340 goto out;
2341
2342 for (i = 0; i < tev->nargs; i++)
2343 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2344 pr_warning("Please upgrade your kernel to at least "
2345 "3.14 to have access to feature %s\n",
2346 tev->args[i].value);
2347 break;
2348 }
2349 out:
2350 free(buf);
2351 }
2352
2353 /* Set new name from original perf_probe_event and namelist */
2354 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2355 struct perf_probe_event *pev,
2356 struct strlist *namelist,
2357 bool allow_suffix)
2358 {
2359 const char *event, *group;
2360 char buf[64];
2361 int ret;
2362
2363 if (pev->event)
2364 event = pev->event;
2365 else
2366 if (pev->point.function &&
2367 (strncmp(pev->point.function, "0x", 2) != 0) &&
2368 !strisglob(pev->point.function))
2369 event = pev->point.function;
2370 else
2371 event = tev->point.realname;
2372 if (pev->group)
2373 group = pev->group;
2374 else
2375 group = PERFPROBE_GROUP;
2376
2377 /* Get an unused new event name */
2378 ret = get_new_event_name(buf, 64, event,
2379 namelist, allow_suffix);
2380 if (ret < 0)
2381 return ret;
2382
2383 event = buf;
2384
2385 tev->event = strdup(event);
2386 tev->group = strdup(group);
2387 if (tev->event == NULL || tev->group == NULL)
2388 return -ENOMEM;
2389
2390 /* Add added event name to namelist */
2391 strlist__add(namelist, event);
2392 return 0;
2393 }
2394
2395 static int __add_probe_trace_events(struct perf_probe_event *pev,
2396 struct probe_trace_event *tevs,
2397 int ntevs, bool allow_suffix)
2398 {
2399 int i, fd, ret;
2400 struct probe_trace_event *tev = NULL;
2401 struct strlist *namelist;
2402
2403 fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
2404 if (fd < 0)
2405 return fd;
2406
2407 /* Get current event names */
2408 namelist = probe_file__get_namelist(fd);
2409 if (!namelist) {
2410 pr_debug("Failed to get current event list.\n");
2411 ret = -ENOMEM;
2412 goto close_out;
2413 }
2414
2415 ret = 0;
2416 for (i = 0; i < ntevs; i++) {
2417 tev = &tevs[i];
2418 /* Skip if the symbol is out of .text or blacklisted */
2419 if (!tev->point.symbol)
2420 continue;
2421
2422 /* Set new name for tev (and update namelist) */
2423 ret = probe_trace_event__set_name(tev, pev, namelist,
2424 allow_suffix);
2425 if (ret < 0)
2426 break;
2427
2428 ret = probe_file__add_event(fd, tev);
2429 if (ret < 0)
2430 break;
2431
2432 /*
2433 * Probes after the first probe which comes from same
2434 * user input are always allowed to add suffix, because
2435 * there might be several addresses corresponding to
2436 * one code line.
2437 */
2438 allow_suffix = true;
2439 }
2440 if (ret == -EINVAL && pev->uprobes)
2441 warn_uprobe_event_compat(tev);
2442
2443 strlist__delete(namelist);
2444 close_out:
2445 close(fd);
2446 return ret;
2447 }
2448
2449 static int find_probe_functions(struct map *map, char *name,
2450 struct symbol **syms)
2451 {
2452 int found = 0;
2453 struct symbol *sym;
2454 struct rb_node *tmp;
2455
2456 if (map__load(map, NULL) < 0)
2457 return 0;
2458
2459 map__for_each_symbol(map, sym, tmp) {
2460 if (strglobmatch(sym->name, name)) {
2461 found++;
2462 if (syms && found < probe_conf.max_probes)
2463 syms[found - 1] = sym;
2464 }
2465 }
2466
2467 return found;
2468 }
2469
2470 #define strdup_or_goto(str, label) \
2471 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2472
2473 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2474 struct probe_trace_event *tev __maybe_unused,
2475 struct map *map __maybe_unused) { }
2476
2477 /*
2478 * Find probe function addresses from map.
2479 * Return an error or the number of found probe_trace_event
2480 */
2481 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2482 struct probe_trace_event **tevs)
2483 {
2484 struct map *map = NULL;
2485 struct ref_reloc_sym *reloc_sym = NULL;
2486 struct symbol *sym;
2487 struct symbol **syms = NULL;
2488 struct probe_trace_event *tev;
2489 struct perf_probe_point *pp = &pev->point;
2490 struct probe_trace_point *tp;
2491 int num_matched_functions;
2492 int ret, i, j, skipped = 0;
2493
2494 map = get_target_map(pev->target, pev->uprobes);
2495 if (!map) {
2496 ret = -EINVAL;
2497 goto out;
2498 }
2499
2500 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2501 if (!syms) {
2502 ret = -ENOMEM;
2503 goto out;
2504 }
2505
2506 /*
2507 * Load matched symbols: Since the different local symbols may have
2508 * same name but different addresses, this lists all the symbols.
2509 */
2510 num_matched_functions = find_probe_functions(map, pp->function, syms);
2511 if (num_matched_functions == 0) {
2512 pr_err("Failed to find symbol %s in %s\n", pp->function,
2513 pev->target ? : "kernel");
2514 ret = -ENOENT;
2515 goto out;
2516 } else if (num_matched_functions > probe_conf.max_probes) {
2517 pr_err("Too many functions matched in %s\n",
2518 pev->target ? : "kernel");
2519 ret = -E2BIG;
2520 goto out;
2521 }
2522
2523 if (!pev->uprobes && !pp->retprobe) {
2524 reloc_sym = kernel_get_ref_reloc_sym();
2525 if (!reloc_sym) {
2526 pr_warning("Relocated base symbol is not found!\n");
2527 ret = -EINVAL;
2528 goto out;
2529 }
2530 }
2531
2532 /* Setup result trace-probe-events */
2533 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2534 if (!*tevs) {
2535 ret = -ENOMEM;
2536 goto out;
2537 }
2538
2539 ret = 0;
2540
2541 for (j = 0; j < num_matched_functions; j++) {
2542 sym = syms[j];
2543
2544 tev = (*tevs) + ret;
2545 tp = &tev->point;
2546 if (ret == num_matched_functions) {
2547 pr_warning("Too many symbols are listed. Skip it.\n");
2548 break;
2549 }
2550 ret++;
2551
2552 if (pp->offset > sym->end - sym->start) {
2553 pr_warning("Offset %ld is bigger than the size of %s\n",
2554 pp->offset, sym->name);
2555 ret = -ENOENT;
2556 goto err_out;
2557 }
2558 /* Add one probe point */
2559 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2560 /* If we found a wrong one, mark it by NULL symbol */
2561 if (!pev->uprobes &&
2562 kprobe_warn_out_range(sym->name, tp->address)) {
2563 tp->symbol = NULL; /* Skip it */
2564 skipped++;
2565 } else if (reloc_sym) {
2566 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2567 tp->offset = tp->address - reloc_sym->addr;
2568 } else {
2569 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2570 tp->offset = pp->offset;
2571 }
2572 tp->realname = strdup_or_goto(sym->name, nomem_out);
2573
2574 tp->retprobe = pp->retprobe;
2575 if (pev->target)
2576 tev->point.module = strdup_or_goto(pev->target,
2577 nomem_out);
2578 tev->uprobes = pev->uprobes;
2579 tev->nargs = pev->nargs;
2580 if (tev->nargs) {
2581 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2582 tev->nargs);
2583 if (tev->args == NULL)
2584 goto nomem_out;
2585 }
2586 for (i = 0; i < tev->nargs; i++) {
2587 if (pev->args[i].name)
2588 tev->args[i].name =
2589 strdup_or_goto(pev->args[i].name,
2590 nomem_out);
2591
2592 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2593 nomem_out);
2594 if (pev->args[i].type)
2595 tev->args[i].type =
2596 strdup_or_goto(pev->args[i].type,
2597 nomem_out);
2598 }
2599 arch__fix_tev_from_maps(pev, tev, map);
2600 }
2601 if (ret == skipped) {
2602 ret = -ENOENT;
2603 goto err_out;
2604 }
2605
2606 out:
2607 put_target_map(map, pev->uprobes);
2608 free(syms);
2609 return ret;
2610
2611 nomem_out:
2612 ret = -ENOMEM;
2613 err_out:
2614 clear_probe_trace_events(*tevs, num_matched_functions);
2615 zfree(tevs);
2616 goto out;
2617 }
2618
2619 static int try_to_find_absolute_address(struct perf_probe_event *pev,
2620 struct probe_trace_event **tevs)
2621 {
2622 struct perf_probe_point *pp = &pev->point;
2623 struct probe_trace_event *tev;
2624 struct probe_trace_point *tp;
2625 int i, err;
2626
2627 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2628 return -EINVAL;
2629 if (perf_probe_event_need_dwarf(pev))
2630 return -EINVAL;
2631
2632 /*
2633 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2634 * absolute address.
2635 *
2636 * Only one tev can be generated by this.
2637 */
2638 *tevs = zalloc(sizeof(*tev));
2639 if (!*tevs)
2640 return -ENOMEM;
2641
2642 tev = *tevs;
2643 tp = &tev->point;
2644
2645 /*
2646 * Don't use tp->offset, use address directly, because
2647 * in synthesize_probe_trace_command() address cannot be
2648 * zero.
2649 */
2650 tp->address = pev->point.abs_address;
2651 tp->retprobe = pp->retprobe;
2652 tev->uprobes = pev->uprobes;
2653
2654 err = -ENOMEM;
2655 /*
2656 * Give it a '0x' leading symbol name.
2657 * In __add_probe_trace_events, a NULL symbol is interpreted as
2658 * invalud.
2659 */
2660 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2661 goto errout;
2662
2663 /* For kprobe, check range */
2664 if ((!tev->uprobes) &&
2665 (kprobe_warn_out_range(tev->point.symbol,
2666 tev->point.address))) {
2667 err = -EACCES;
2668 goto errout;
2669 }
2670
2671 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2672 goto errout;
2673
2674 if (pev->target) {
2675 tp->module = strdup(pev->target);
2676 if (!tp->module)
2677 goto errout;
2678 }
2679
2680 if (tev->group) {
2681 tev->group = strdup(pev->group);
2682 if (!tev->group)
2683 goto errout;
2684 }
2685
2686 if (pev->event) {
2687 tev->event = strdup(pev->event);
2688 if (!tev->event)
2689 goto errout;
2690 }
2691
2692 tev->nargs = pev->nargs;
2693 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2694 if (!tev->args) {
2695 err = -ENOMEM;
2696 goto errout;
2697 }
2698 for (i = 0; i < tev->nargs; i++)
2699 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2700
2701 return 1;
2702
2703 errout:
2704 if (*tevs) {
2705 clear_probe_trace_events(*tevs, 1);
2706 *tevs = NULL;
2707 }
2708 return err;
2709 }
2710
2711 bool __weak arch__prefers_symtab(void) { return false; }
2712
2713 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2714 struct probe_trace_event **tevs)
2715 {
2716 int ret;
2717
2718 if (pev->uprobes && !pev->group) {
2719 /* Replace group name if not given */
2720 ret = convert_exec_to_group(pev->target, &pev->group);
2721 if (ret != 0) {
2722 pr_warning("Failed to make a group name.\n");
2723 return ret;
2724 }
2725 }
2726
2727 ret = try_to_find_absolute_address(pev, tevs);
2728 if (ret > 0)
2729 return ret;
2730
2731 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2732 ret = find_probe_trace_events_from_map(pev, tevs);
2733 if (ret > 0)
2734 return ret; /* Found in symbol table */
2735 }
2736
2737 /* Convert perf_probe_event with debuginfo */
2738 ret = try_to_find_probe_trace_events(pev, tevs);
2739 if (ret != 0)
2740 return ret; /* Found in debuginfo or got an error */
2741
2742 return find_probe_trace_events_from_map(pev, tevs);
2743 }
2744
2745 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2746 {
2747 int i, ret;
2748
2749 ret = init_symbol_maps(pevs->uprobes);
2750 if (ret < 0)
2751 return ret;
2752
2753 /* Loop 1: convert all events */
2754 for (i = 0; i < npevs; i++) {
2755 /* Init kprobe blacklist if needed */
2756 if (!pevs[i].uprobes)
2757 kprobe_blacklist__init();
2758 /* Convert with or without debuginfo */
2759 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
2760 if (ret < 0)
2761 return ret;
2762 pevs[i].ntevs = ret;
2763 }
2764 /* This just release blacklist only if allocated */
2765 kprobe_blacklist__release();
2766
2767 return 0;
2768 }
2769
2770 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2771 {
2772 int i, ret = 0;
2773
2774 /* Loop 2: add all events */
2775 for (i = 0; i < npevs; i++) {
2776 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
2777 pevs[i].ntevs,
2778 probe_conf.force_add);
2779 if (ret < 0)
2780 break;
2781 }
2782 return ret;
2783 }
2784
2785 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2786 {
2787 int i, j;
2788
2789 /* Loop 3: cleanup and free trace events */
2790 for (i = 0; i < npevs; i++) {
2791 for (j = 0; j < pevs[i].ntevs; j++)
2792 clear_probe_trace_event(&pevs[i].tevs[j]);
2793 zfree(&pevs[i].tevs);
2794 pevs[i].ntevs = 0;
2795 clear_perf_probe_event(&pevs[i]);
2796 }
2797
2798 exit_symbol_maps();
2799 }
2800
2801 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2802 {
2803 int ret;
2804
2805 ret = convert_perf_probe_events(pevs, npevs);
2806 if (ret == 0)
2807 ret = apply_perf_probe_events(pevs, npevs);
2808
2809 cleanup_perf_probe_events(pevs, npevs);
2810
2811 return ret;
2812 }
2813
2814 int del_perf_probe_events(struct strfilter *filter)
2815 {
2816 int ret, ret2, ufd = -1, kfd = -1;
2817 char *str = strfilter__string(filter);
2818
2819 if (!str)
2820 return -EINVAL;
2821
2822 /* Get current event names */
2823 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
2824 if (ret < 0)
2825 goto out;
2826
2827 ret = probe_file__del_events(kfd, filter);
2828 if (ret < 0 && ret != -ENOENT)
2829 goto error;
2830
2831 ret2 = probe_file__del_events(ufd, filter);
2832 if (ret2 < 0 && ret2 != -ENOENT) {
2833 ret = ret2;
2834 goto error;
2835 }
2836 ret = 0;
2837
2838 error:
2839 if (kfd >= 0)
2840 close(kfd);
2841 if (ufd >= 0)
2842 close(ufd);
2843 out:
2844 free(str);
2845
2846 return ret;
2847 }
2848
2849 /* TODO: don't use a global variable for filter ... */
2850 static struct strfilter *available_func_filter;
2851
2852 /*
2853 * If a symbol corresponds to a function with global binding and
2854 * matches filter return 0. For all others return 1.
2855 */
2856 static int filter_available_functions(struct map *map __maybe_unused,
2857 struct symbol *sym)
2858 {
2859 if (strfilter__compare(available_func_filter, sym->name))
2860 return 0;
2861 return 1;
2862 }
2863
2864 int show_available_funcs(const char *target, struct strfilter *_filter,
2865 bool user)
2866 {
2867 struct map *map;
2868 int ret;
2869
2870 ret = init_symbol_maps(user);
2871 if (ret < 0)
2872 return ret;
2873
2874 /* Get a symbol map */
2875 if (user)
2876 map = dso__new_map(target);
2877 else
2878 map = kernel_get_module_map(target);
2879 if (!map) {
2880 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2881 return -EINVAL;
2882 }
2883
2884 /* Load symbols with given filter */
2885 available_func_filter = _filter;
2886 if (map__load(map, filter_available_functions)) {
2887 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2888 goto end;
2889 }
2890 if (!dso__sorted_by_name(map->dso, map->type))
2891 dso__sort_by_name(map->dso, map->type);
2892
2893 /* Show all (filtered) symbols */
2894 setup_pager();
2895 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2896 end:
2897 if (user) {
2898 map__put(map);
2899 }
2900 exit_symbol_maps();
2901
2902 return ret;
2903 }
2904
2905 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
2906 struct perf_probe_arg *pvar)
2907 {
2908 tvar->value = strdup(pvar->var);
2909 if (tvar->value == NULL)
2910 return -ENOMEM;
2911 if (pvar->type) {
2912 tvar->type = strdup(pvar->type);
2913 if (tvar->type == NULL)
2914 return -ENOMEM;
2915 }
2916 if (pvar->name) {
2917 tvar->name = strdup(pvar->name);
2918 if (tvar->name == NULL)
2919 return -ENOMEM;
2920 } else
2921 tvar->name = NULL;
2922 return 0;
2923 }
This page took 0.090725 seconds and 5 git commands to generate.