1 | //===--------------------------- libunwind.cpp ----------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | // |
8 | // Implements unw_* functions from <libunwind.h> |
9 | // |
10 | //===----------------------------------------------------------------------===// |
11 | |
12 | #include <libunwind.h> |
13 | |
14 | #include "libunwind_ext.h" |
15 | #include "config.h" |
16 | |
17 | #include <stdlib.h> |
18 | |
19 | |
20 | #if !defined(__USING_SJLJ_EXCEPTIONS__) |
21 | #include "AddressSpace.hpp" |
22 | #include "UnwindCursor.hpp" |
23 | |
24 | using namespace libunwind; |
25 | |
26 | /// internal object to represent this processes address space |
27 | LocalAddressSpace LocalAddressSpace::sThisAddressSpace; |
28 | |
29 | _LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space = |
30 | (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace; |
31 | |
32 | /// Create a cursor of a thread in this process given 'context' recorded by |
33 | /// __unw_getcontext(). |
34 | _LIBUNWIND_HIDDEN int __unw_init_local(unw_cursor_t *cursor, |
35 | unw_context_t *context) { |
36 | _LIBUNWIND_TRACE_API("__unw_init_local(cursor=%p, context=%p)" , |
37 | static_cast<void *>(cursor), |
38 | static_cast<void *>(context)); |
39 | #if defined(__i386__) |
40 | # define REGISTER_KIND Registers_x86 |
41 | #elif defined(__x86_64__) |
42 | # define REGISTER_KIND Registers_x86_64 |
43 | #elif defined(__powerpc64__) |
44 | # define REGISTER_KIND Registers_ppc64 |
45 | #elif defined(__ppc__) |
46 | # define REGISTER_KIND Registers_ppc |
47 | #elif defined(__aarch64__) |
48 | # define REGISTER_KIND Registers_arm64 |
49 | #elif defined(__arm__) |
50 | # define REGISTER_KIND Registers_arm |
51 | #elif defined(__or1k__) |
52 | # define REGISTER_KIND Registers_or1k |
53 | #elif defined(__mips__) && defined(_ABIO32) && _MIPS_SIM == _ABIO32 |
54 | # define REGISTER_KIND Registers_mips_o32 |
55 | #elif defined(__mips64) |
56 | # define REGISTER_KIND Registers_mips_newabi |
57 | #elif defined(__mips__) |
58 | # warning The MIPS architecture is not supported with this ABI and environment! |
59 | #elif defined(__sparc__) |
60 | # define REGISTER_KIND Registers_sparc |
61 | #else |
62 | # error Architecture not supported |
63 | #endif |
64 | // Use "placement new" to allocate UnwindCursor in the cursor buffer. |
65 | new (reinterpret_cast<UnwindCursor<LocalAddressSpace, REGISTER_KIND> *>(cursor)) |
66 | UnwindCursor<LocalAddressSpace, REGISTER_KIND>( |
67 | context, LocalAddressSpace::sThisAddressSpace); |
68 | #undef REGISTER_KIND |
69 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
70 | co->setInfoBasedOnIPRegister(); |
71 | |
72 | return UNW_ESUCCESS; |
73 | } |
74 | _LIBUNWIND_WEAK_ALIAS(__unw_init_local, unw_init_local) |
75 | |
76 | /// Get value of specified register at cursor position in stack frame. |
77 | _LIBUNWIND_HIDDEN int __unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum, |
78 | unw_word_t *value) { |
79 | _LIBUNWIND_TRACE_API("__unw_get_reg(cursor=%p, regNum=%d, &value=%p)" , |
80 | static_cast<void *>(cursor), regNum, |
81 | static_cast<void *>(value)); |
82 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
83 | if (co->validReg(regNum)) { |
84 | *value = co->getReg(regNum); |
85 | return UNW_ESUCCESS; |
86 | } |
87 | return UNW_EBADREG; |
88 | } |
89 | _LIBUNWIND_WEAK_ALIAS(__unw_get_reg, unw_get_reg) |
90 | |
91 | /// Set value of specified register at cursor position in stack frame. |
92 | _LIBUNWIND_HIDDEN int __unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum, |
93 | unw_word_t value) { |
94 | _LIBUNWIND_TRACE_API("__unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR |
95 | ")" , |
96 | static_cast<void *>(cursor), regNum, value); |
97 | typedef LocalAddressSpace::pint_t pint_t; |
98 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
99 | if (co->validReg(regNum)) { |
100 | co->setReg(regNum, (pint_t)value); |
101 | // specical case altering IP to re-find info (being called by personality |
102 | // function) |
103 | if (regNum == UNW_REG_IP) { |
104 | unw_proc_info_t info; |
105 | // First, get the FDE for the old location and then update it. |
106 | co->getInfo(&info); |
107 | co->setInfoBasedOnIPRegister(false); |
108 | // If the original call expects stack adjustment, perform this now. |
109 | // Normal frame unwinding would have included the offset already in the |
110 | // CFA computation. |
111 | // Note: for PA-RISC and other platforms where the stack grows up, |
112 | // this should actually be - info.gp. LLVM doesn't currently support |
113 | // any such platforms and Clang doesn't export a macro for them. |
114 | if (info.gp) |
115 | co->setReg(UNW_REG_SP, co->getReg(UNW_REG_SP) + info.gp); |
116 | } |
117 | return UNW_ESUCCESS; |
118 | } |
119 | return UNW_EBADREG; |
120 | } |
121 | _LIBUNWIND_WEAK_ALIAS(__unw_set_reg, unw_set_reg) |
122 | |
123 | /// Get value of specified float register at cursor position in stack frame. |
124 | _LIBUNWIND_HIDDEN int __unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, |
125 | unw_fpreg_t *value) { |
126 | _LIBUNWIND_TRACE_API("__unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)" , |
127 | static_cast<void *>(cursor), regNum, |
128 | static_cast<void *>(value)); |
129 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
130 | if (co->validFloatReg(regNum)) { |
131 | *value = co->getFloatReg(regNum); |
132 | return UNW_ESUCCESS; |
133 | } |
134 | return UNW_EBADREG; |
135 | } |
136 | _LIBUNWIND_WEAK_ALIAS(__unw_get_fpreg, unw_get_fpreg) |
137 | |
138 | /// Set value of specified float register at cursor position in stack frame. |
139 | _LIBUNWIND_HIDDEN int __unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, |
140 | unw_fpreg_t value) { |
141 | #if defined(_LIBUNWIND_ARM_EHABI) |
142 | _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)" , |
143 | static_cast<void *>(cursor), regNum, value); |
144 | #else |
145 | _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%g)" , |
146 | static_cast<void *>(cursor), regNum, value); |
147 | #endif |
148 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
149 | if (co->validFloatReg(regNum)) { |
150 | co->setFloatReg(regNum, value); |
151 | return UNW_ESUCCESS; |
152 | } |
153 | return UNW_EBADREG; |
154 | } |
155 | _LIBUNWIND_WEAK_ALIAS(__unw_set_fpreg, unw_set_fpreg) |
156 | |
157 | /// Move cursor to next frame. |
158 | _LIBUNWIND_HIDDEN int __unw_step(unw_cursor_t *cursor) { |
159 | _LIBUNWIND_TRACE_API("__unw_step(cursor=%p)" , static_cast<void *>(cursor)); |
160 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
161 | return co->step(); |
162 | } |
163 | _LIBUNWIND_WEAK_ALIAS(__unw_step, unw_step) |
164 | |
165 | /// Get unwind info at cursor position in stack frame. |
166 | _LIBUNWIND_HIDDEN int __unw_get_proc_info(unw_cursor_t *cursor, |
167 | unw_proc_info_t *info) { |
168 | _LIBUNWIND_TRACE_API("__unw_get_proc_info(cursor=%p, &info=%p)" , |
169 | static_cast<void *>(cursor), static_cast<void *>(info)); |
170 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
171 | co->getInfo(info); |
172 | if (info->end_ip == 0) |
173 | return UNW_ENOINFO; |
174 | else |
175 | return UNW_ESUCCESS; |
176 | } |
177 | _LIBUNWIND_WEAK_ALIAS(__unw_get_proc_info, unw_get_proc_info) |
178 | |
179 | /// Resume execution at cursor position (aka longjump). |
180 | _LIBUNWIND_HIDDEN int __unw_resume(unw_cursor_t *cursor) { |
181 | _LIBUNWIND_TRACE_API("__unw_resume(cursor=%p)" , static_cast<void *>(cursor)); |
182 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
183 | co->jumpto(); |
184 | return UNW_EUNSPEC; |
185 | } |
186 | _LIBUNWIND_WEAK_ALIAS(__unw_resume, unw_resume) |
187 | |
188 | /// Get name of function at cursor position in stack frame. |
189 | _LIBUNWIND_HIDDEN int __unw_get_proc_name(unw_cursor_t *cursor, char *buf, |
190 | size_t bufLen, unw_word_t *offset) { |
191 | _LIBUNWIND_TRACE_API("__unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)" , |
192 | static_cast<void *>(cursor), static_cast<void *>(buf), |
193 | static_cast<unsigned long>(bufLen)); |
194 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
195 | if (co->getFunctionName(buf, bufLen, offset)) |
196 | return UNW_ESUCCESS; |
197 | else |
198 | return UNW_EUNSPEC; |
199 | } |
200 | _LIBUNWIND_WEAK_ALIAS(__unw_get_proc_name, unw_get_proc_name) |
201 | |
202 | /// Checks if a register is a floating-point register. |
203 | _LIBUNWIND_HIDDEN int __unw_is_fpreg(unw_cursor_t *cursor, |
204 | unw_regnum_t regNum) { |
205 | _LIBUNWIND_TRACE_API("__unw_is_fpreg(cursor=%p, regNum=%d)" , |
206 | static_cast<void *>(cursor), regNum); |
207 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
208 | return co->validFloatReg(regNum); |
209 | } |
210 | _LIBUNWIND_WEAK_ALIAS(__unw_is_fpreg, unw_is_fpreg) |
211 | |
212 | /// Checks if a register is a floating-point register. |
213 | _LIBUNWIND_HIDDEN const char *__unw_regname(unw_cursor_t *cursor, |
214 | unw_regnum_t regNum) { |
215 | _LIBUNWIND_TRACE_API("__unw_regname(cursor=%p, regNum=%d)" , |
216 | static_cast<void *>(cursor), regNum); |
217 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
218 | return co->getRegisterName(regNum); |
219 | } |
220 | _LIBUNWIND_WEAK_ALIAS(__unw_regname, unw_regname) |
221 | |
222 | /// Checks if current frame is signal trampoline. |
223 | _LIBUNWIND_HIDDEN int __unw_is_signal_frame(unw_cursor_t *cursor) { |
224 | _LIBUNWIND_TRACE_API("__unw_is_signal_frame(cursor=%p)" , |
225 | static_cast<void *>(cursor)); |
226 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
227 | return co->isSignalFrame(); |
228 | } |
229 | _LIBUNWIND_WEAK_ALIAS(__unw_is_signal_frame, unw_is_signal_frame) |
230 | |
231 | #ifdef __arm__ |
232 | // Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD |
233 | _LIBUNWIND_HIDDEN void __unw_save_vfp_as_X(unw_cursor_t *cursor) { |
234 | _LIBUNWIND_TRACE_API("__unw_get_fpreg_save_vfp_as_X(cursor=%p)" , |
235 | static_cast<void *>(cursor)); |
236 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
237 | return co->saveVFPAsX(); |
238 | } |
239 | _LIBUNWIND_WEAK_ALIAS(__unw_save_vfp_as_X, unw_save_cfp_as_X) |
240 | #endif |
241 | |
242 | |
243 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
244 | /// SPI: walks cached DWARF entries |
245 | _LIBUNWIND_HIDDEN void __unw_iterate_dwarf_unwind_cache(void (*func)( |
246 | unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) { |
247 | _LIBUNWIND_TRACE_API("__unw_iterate_dwarf_unwind_cache(func=%p)" , |
248 | reinterpret_cast<void *>(func)); |
249 | #if !defined(_LIBUNWIND_NO_HEAP) |
250 | DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func); |
251 | #endif |
252 | } |
253 | _LIBUNWIND_WEAK_ALIAS(__unw_iterate_dwarf_unwind_cache, |
254 | unw_iterate_dwarf_unwind_cache) |
255 | |
256 | /// IPI: for __register_frame() |
257 | void __unw_add_dynamic_fde(unw_word_t fde) { |
258 | CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; |
259 | CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; |
260 | const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE( |
261 | LocalAddressSpace::sThisAddressSpace, |
262 | (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo); |
263 | if (message == NULL) { |
264 | // dynamically registered FDEs don't have a mach_header group they are in. |
265 | // Use fde as mh_group |
266 | #if !defined(_LIBUNWIND_NO_HEAP) |
267 | unw_word_t mh_group = fdeInfo.fdeStart; |
268 | DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group, |
269 | fdeInfo.pcStart, fdeInfo.pcEnd, |
270 | fdeInfo.fdeStart); |
271 | #endif |
272 | } else { |
273 | _LIBUNWIND_DEBUG_LOG("__unw_add_dynamic_fde: bad fde: %s" , message); |
274 | } |
275 | } |
276 | |
277 | /// IPI: for __deregister_frame() |
278 | void __unw_remove_dynamic_fde(unw_word_t fde) { |
279 | // fde is own mh_group |
280 | #if !defined(_LIBUNWIND_NO_HEAP) |
281 | DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde); |
282 | #endif // !defined(_LIBUNWIND_NO_HEAP) |
283 | } |
284 | #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
285 | #endif // !defined(__USING_SJLJ_EXCEPTIONS__) |
286 | |
287 | /// Convenient helper (added for jemalloc) |
288 | int unw_backtrace(void **buffer, int size) { |
289 | unw_context_t context; |
290 | unw_cursor_t cursor; |
291 | if (unw_getcontext(&context) || unw_init_local(&cursor, &context)) { |
292 | return 0; |
293 | } |
294 | |
295 | unw_word_t ip; |
296 | int current = 0; |
297 | while (unw_step(&cursor) > 0) { |
298 | if (current >= size || unw_get_reg(&cursor, UNW_REG_IP, &ip)) { |
299 | break; |
300 | } |
301 | |
302 | buffer[current++] = reinterpret_cast<void *>(static_cast<uintptr_t>(ip)); |
303 | } |
304 | |
305 | return current; |
306 | } |
307 | |
308 | |
309 | // Add logging hooks in Debug builds only |
310 | #ifndef NDEBUG |
311 | #include <stdlib.h> |
312 | |
313 | _LIBUNWIND_HIDDEN |
314 | bool logAPIs() { |
315 | // do manual lock to avoid use of _cxa_guard_acquire or initializers |
316 | static bool checked = false; |
317 | static bool log = false; |
318 | if (!checked) { |
319 | log = (getenv("LIBUNWIND_PRINT_APIS" ) != NULL); |
320 | checked = true; |
321 | } |
322 | return log; |
323 | } |
324 | |
325 | _LIBUNWIND_HIDDEN |
326 | bool logUnwinding() { |
327 | // do manual lock to avoid use of _cxa_guard_acquire or initializers |
328 | static bool checked = false; |
329 | static bool log = false; |
330 | if (!checked) { |
331 | log = (getenv("LIBUNWIND_PRINT_UNWINDING" ) != NULL); |
332 | checked = true; |
333 | } |
334 | return log; |
335 | } |
336 | |
337 | _LIBUNWIND_HIDDEN |
338 | bool logDWARF() { |
339 | // do manual lock to avoid use of _cxa_guard_acquire or initializers |
340 | static bool checked = false; |
341 | static bool log = false; |
342 | if (!checked) { |
343 | log = (getenv("LIBUNWIND_PRINT_DWARF" ) != NULL); |
344 | checked = true; |
345 | } |
346 | return log; |
347 | } |
348 | |
349 | #endif // NDEBUG |
350 | |
351 | |