1
2
3
4
5
6
7
8
9
10
11
12#include <inttypes.h>
13#include <dwarf.h>
14#include <elfutils/libdwfl.h>
15
16#include "util/thread.h"
17#include "util/callchain.h"
18#include "util/debug.h"
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33static char *debuginfo_path;
34
35static const Dwfl_Callbacks offline_callbacks = {
36 .debuginfo_path = &debuginfo_path,
37 .find_debuginfo = dwfl_standard_find_debuginfo,
38 .section_address = dwfl_offline_section_address,
39};
40
41
42
43
44
45
46static int check_return_reg(int ra_regno, Dwarf_Frame *frame)
47{
48 Dwarf_Op ops_mem[2];
49 Dwarf_Op dummy;
50 Dwarf_Op *ops = &dummy;
51 size_t nops;
52 int result;
53
54 result = dwarf_frame_register(frame, ra_regno, ops_mem, &ops, &nops);
55 if (result < 0) {
56 pr_debug("dwarf_frame_register() %s\n", dwarf_errmsg(-1));
57 return -1;
58 }
59
60
61
62
63 if (nops != 0 || ops != NULL)
64 return 0;
65
66
67
68
69
70 result = dwarf_frame_cfa(frame, &ops, &nops);
71 if (result < 0) {
72 pr_debug("dwarf_frame_cfa() returns %d, %s\n", result,
73 dwarf_errmsg(-1));
74 return -1;
75 }
76
77
78
79
80 if (nops == 1 && ops[0].atom == DW_OP_bregx && ops[0].number == 1 &&
81 ops[0].number2 == 0)
82 return 1;
83
84
85
86
87 return 2;
88}
89
90
91
92
93static Dwarf_Frame *get_eh_frame(Dwfl_Module *mod, Dwarf_Addr pc)
94{
95 int result;
96 Dwarf_Addr bias;
97 Dwarf_CFI *cfi;
98 Dwarf_Frame *frame;
99
100 cfi = dwfl_module_eh_cfi(mod, &bias);
101 if (!cfi) {
102 pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
103 return NULL;
104 }
105
106 result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);
107 if (result) {
108 pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
109 return NULL;
110 }
111
112 return frame;
113}
114
115
116
117
118static Dwarf_Frame *get_dwarf_frame(Dwfl_Module *mod, Dwarf_Addr pc)
119{
120 Dwarf_CFI *cfi;
121 Dwarf_Addr bias;
122 Dwarf_Frame *frame;
123 int result;
124
125 cfi = dwfl_module_dwarf_cfi(mod, &bias);
126 if (!cfi) {
127 pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
128 return NULL;
129 }
130
131 result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);
132 if (result) {
133 pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
134 return NULL;
135 }
136
137 return frame;
138}
139
140
141
142
143
144
145
146
147
148static int check_return_addr(struct dso *dso, u64 map_start, Dwarf_Addr pc)
149{
150 int rc = -1;
151 Dwfl *dwfl;
152 Dwfl_Module *mod;
153 Dwarf_Frame *frame;
154 int ra_regno;
155 Dwarf_Addr start = pc;
156 Dwarf_Addr end = pc;
157 bool signalp;
158 const char *exec_file = dso->long_name;
159
160 dwfl = dso->dwfl;
161
162 if (!dwfl) {
163 dwfl = dwfl_begin(&offline_callbacks);
164 if (!dwfl) {
165 pr_debug("dwfl_begin() failed: %s\n", dwarf_errmsg(-1));
166 return -1;
167 }
168
169 mod = dwfl_report_elf(dwfl, exec_file, exec_file, -1,
170 map_start, false);
171 if (!mod) {
172 pr_debug("dwfl_report_elf() failed %s\n",
173 dwarf_errmsg(-1));
174
175
176
177
178
179 dwfl_end(dwfl);
180 goto out;
181 }
182 dso->dwfl = dwfl;
183 }
184
185 mod = dwfl_addrmodule(dwfl, pc);
186 if (!mod) {
187 pr_debug("dwfl_addrmodule() failed, %s\n", dwarf_errmsg(-1));
188 goto out;
189 }
190
191
192
193
194
195 frame = get_eh_frame(mod, pc);
196 if (!frame) {
197 frame = get_dwarf_frame(mod, pc);
198 if (!frame)
199 goto out;
200 }
201
202 ra_regno = dwarf_frame_info(frame, &start, &end, &signalp);
203 if (ra_regno < 0) {
204 pr_debug("Return address register unavailable: %s\n",
205 dwarf_errmsg(-1));
206 goto out;
207 }
208
209 rc = check_return_reg(ra_regno, frame);
210
211out:
212 return rc;
213}
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)
239{
240 struct addr_location al;
241 struct dso *dso = NULL;
242 int rc;
243 u64 ip;
244 u64 skip_slot = -1;
245
246 if (chain->nr < 3)
247 return skip_slot;
248
249 ip = chain->ips[2];
250
251 thread__find_addr_location(thread, PERF_RECORD_MISC_USER,
252 MAP__FUNCTION, ip, &al);
253
254 if (al.map)
255 dso = al.map->dso;
256
257 if (!dso) {
258 pr_debug("%" PRIx64 " dso is NULL\n", ip);
259 return skip_slot;
260 }
261
262 rc = check_return_addr(dso, al.map->start, ip);
263
264 pr_debug("[DSO %s, sym %s, ip 0x%" PRIx64 "] rc %d\n",
265 dso->long_name, al.sym->name, ip, rc);
266
267 if (rc == 0) {
268
269
270
271 skip_slot = 2;
272 } else if (rc == 2) {
273
274
275
276
277 skip_slot = 3;
278 }
279 return skip_slot;
280}
281