blob: bf9a2594572c7ff3e94adfab36dce813476bb6a9 [file] [log] [blame]
Naveen N. Raod2332092015-04-28 17:35:35 +05301/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * Copyright (C) 2015 Naveen N. Rao, IBM Corporation
7 */
8
9#include "debug.h"
10#include "symbol.h"
Naveen N. Rao031b84c2015-04-28 17:35:37 +053011#include "map.h"
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +053012#include "probe-event.h"
Naveen N. Rao44ca9342017-03-08 13:56:10 +053013#include "probe-file.h"
Naveen N. Raod2332092015-04-28 17:35:35 +053014
15#ifdef HAVE_LIBELF_SUPPORT
16bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
17{
18 return ehdr.e_type == ET_EXEC ||
19 ehdr.e_type == ET_REL ||
20 ehdr.e_type == ET_DYN;
21}
Ananth N Mavinakayanahallic50fc0a2015-04-28 17:35:38 +053022
Naveen N. Raod2332092015-04-28 17:35:35 +053023#endif
Naveen N. Raofb6d5942015-04-28 17:35:36 +053024
25#if !defined(_CALL_ELF) || _CALL_ELF != 2
26int arch__choose_best_symbol(struct symbol *syma,
27 struct symbol *symb __maybe_unused)
28{
29 char *sym = syma->name;
30
31 /* Skip over any initial dot */
32 if (*sym == '.')
33 sym++;
34
35 /* Avoid "SyS" kernel syscall aliases */
36 if (strlen(sym) >= 3 && !strncmp(sym, "SyS", 3))
37 return SYMBOL_B;
38 if (strlen(sym) >= 10 && !strncmp(sym, "compat_SyS", 10))
39 return SYMBOL_B;
40
41 return SYMBOL_A;
42}
Naveen N. Rao031b84c2015-04-28 17:35:37 +053043
44/* Allow matching against dot variants */
45int arch__compare_symbol_names(const char *namea, const char *nameb)
46{
47 /* Skip over initial dot */
48 if (*namea == '.')
49 namea++;
50 if (*nameb == '.')
51 nameb++;
52
53 return strcmp(namea, nameb);
54}
Paul Clarked80406452017-04-25 13:15:49 -050055
56int arch__compare_symbol_names_n(const char *namea, const char *nameb,
57 unsigned int n)
58{
59 /* Skip over initial dot */
60 if (*namea == '.')
61 namea++;
62 if (*nameb == '.')
63 nameb++;
64
65 return strncmp(namea, nameb, n);
66}
Naveen N. Raofb6d5942015-04-28 17:35:36 +053067#endif
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +053068
69#if defined(_CALL_ELF) && _CALL_ELF == 2
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +053070
Naveen N. Rao0b3c2262016-04-12 14:40:50 +053071#ifdef HAVE_LIBELF_SUPPORT
72void arch__sym_update(struct symbol *s, GElf_Sym *sym)
73{
74 s->arch_sym = sym->st_other;
75}
76#endif
77
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +053078#define PPC64LE_LEP_OFFSET 8
79
80void arch__fix_tev_from_maps(struct perf_probe_event *pev,
Naveen N. Rao0b3c2262016-04-12 14:40:50 +053081 struct probe_trace_event *tev, struct map *map,
82 struct symbol *sym)
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +053083{
Naveen N. Rao0b3c2262016-04-12 14:40:50 +053084 int lep_offset;
85
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +053086 /*
Naveen N. Rao239aeba2016-04-12 14:40:49 +053087 * When probing at a function entry point, we normally always want the
88 * LEP since that catches calls to the function through both the GEP and
89 * the LEP. Hence, we would like to probe at an offset of 8 bytes if
90 * the user only specified the function entry.
91 *
92 * However, if the user specifies an offset, we fall back to using the
93 * GEP since all userspace applications (objdump/readelf) show function
94 * disassembly with offsets from the GEP.
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +053095 */
Naveen N. Rao44ca9342017-03-08 13:56:10 +053096 if (pev->point.offset || !map || !sym)
Naveen N. Rao239aeba2016-04-12 14:40:49 +053097 return;
98
Naveen N. Rao44ca9342017-03-08 13:56:10 +053099 /* For kretprobes, add an offset only if the kernel supports it */
100 if (!pev->uprobes && pev->point.retprobe) {
101#ifdef HAVE_LIBELF_SUPPORT
102 if (!kretprobe_offset_is_supported())
103#endif
104 return;
105 }
106
Naveen N. Rao0b3c2262016-04-12 14:40:50 +0530107 lep_offset = PPC64_LOCAL_ENTRY_OFFSET(sym->arch_sym);
108
Naveen N. Rao239aeba2016-04-12 14:40:49 +0530109 if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS)
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +0530110 tev->point.offset += PPC64LE_LEP_OFFSET;
Naveen N. Rao0b3c2262016-04-12 14:40:50 +0530111 else if (lep_offset) {
112 if (pev->uprobes)
113 tev->point.address += lep_offset;
114 else
115 tev->point.offset += lep_offset;
116 }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +0530117}
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500118
Ravi Bangoriaf046f3d2016-08-11 14:43:59 -0300119#ifdef HAVE_LIBELF_SUPPORT
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500120void arch__post_process_probe_trace_events(struct perf_probe_event *pev,
121 int ntevs)
122{
123 struct probe_trace_event *tev;
124 struct map *map;
125 struct symbol *sym = NULL;
126 struct rb_node *tmp;
127 int i = 0;
128
129 map = get_target_map(pev->target, pev->uprobes);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300130 if (!map || map__load(map) < 0)
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500131 return;
132
133 for (i = 0; i < ntevs; i++) {
134 tev = &pev->tevs[i];
135 map__for_each_symbol(map, sym, tmp) {
136 if (map->unmap_ip(map, sym->start) == tev->point.address)
137 arch__fix_tev_from_maps(pev, tev, map, sym);
138 }
139 }
140}
Ravi Bangoriaf046f3d2016-08-11 14:43:59 -0300141#endif /* HAVE_LIBELF_SUPPORT */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500142
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +0530143#endif