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