1 //===--------------------------- libunwind.cpp ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
9 // Implements unw_* functions from <libunwind.h>
11 //===----------------------------------------------------------------------===//
13 #include <libunwind.h>
16 #include <cstdlib> // getenv
21 #include "libunwind_ext.h"
27 #if !defined(__USING_SJLJ_EXCEPTIONS__)
28 #include "AddressSpace.hpp"
29 #include "UnwindCursor.hpp"
31 using namespace libunwind;
33 /// internal object to represent this processes address space
34 LocalAddressSpace LocalAddressSpace::sThisAddressSpace;
36 _LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space =
37 (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace;
39 /// record the registers and stack position of the caller
40 extern int unw_getcontext(unw_context_t *);
41 // note: unw_getcontext() implemented in assembly
43 /// Create a cursor of a thread in this process given 'context' recorded by
45 _LIBUNWIND_EXPORT int unw_init_local(unw_cursor_t *cursor,
46 unw_context_t *context) {
47 _LIBUNWIND_TRACE_API("unw_init_local(cursor=%p, context=%p)",
48 static_cast<void *>(cursor),
49 static_cast<void *>(context));
51 # define REGISTER_KIND Registers_x86
52 #elif defined(__x86_64__)
53 # define REGISTER_KIND Registers_x86_64
54 #elif defined(__powerpc64__)
55 # define REGISTER_KIND Registers_ppc64
56 #elif defined(__ppc__)
57 # define REGISTER_KIND Registers_ppc
58 #elif defined(__aarch64__)
59 # define REGISTER_KIND Registers_arm64
60 #elif defined(__arm__)
61 # define REGISTER_KIND Registers_arm
62 #elif defined(__or1k__)
63 # define REGISTER_KIND Registers_or1k
64 #elif defined(__riscv)
65 # define REGISTER_KIND Registers_riscv
66 #elif defined(__mips__) && defined(_ABIO32) && _MIPS_SIM == _ABIO32
67 # define REGISTER_KIND Registers_mips_o32
68 #elif defined(__mips64)
69 # define REGISTER_KIND Registers_mips_newabi
70 #elif defined(__mips__)
71 # warning The MIPS architecture is not supported with this ABI and environment!
72 #elif defined(__sparc__)
73 # define REGISTER_KIND Registers_sparc
75 # error Architecture not supported
77 // Use "placement new" to allocate UnwindCursor in the cursor buffer.
78 new ((void *)cursor) UnwindCursor<LocalAddressSpace, REGISTER_KIND>(
79 context, LocalAddressSpace::sThisAddressSpace);
81 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
82 co->setInfoBasedOnIPRegister();
88 /// Create a cursor into a thread in another process.
89 _LIBUNWIND_EXPORT int unw_init_remote_thread(unw_cursor_t *cursor,
92 // special case: unw_init_remote(xx, unw_local_addr_space, xx)
93 if (as == (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace)
94 return unw_init_local(cursor, NULL); //FIXME
96 // use "placement new" to allocate UnwindCursor in the cursor buffer
97 switch (as->cpuType) {
100 UnwindCursor<RemoteAddressSpace<Pointer32<LittleEndian>>,
101 Registers_x86>(((unw_addr_space_i386 *)as)->oas, arg);
103 case CPU_TYPE_X86_64:
105 UnwindCursor<RemoteAddressSpace<Pointer64<LittleEndian>>,
106 Registers_x86_64>(((unw_addr_space_x86_64 *)as)->oas, arg);
108 case CPU_TYPE_POWERPC:
110 UnwindCursor<RemoteAddressSpace<Pointer32<BigEndian>>,
111 Registers_ppc>(((unw_addr_space_ppc *)as)->oas, arg);
120 static bool is64bit(task_t task) {
121 return false; // FIXME
124 /// Create an address_space object for use in examining another task.
125 _LIBUNWIND_EXPORT unw_addr_space_t unw_create_addr_space_for_task(task_t task) {
128 unw_addr_space_x86_64 *as = new unw_addr_space_x86_64(task);
130 as->cpuType = CPU_TYPE_X86_64;
133 unw_addr_space_i386 *as = new unw_addr_space_i386(task);
135 as->cpuType = CPU_TYPE_I386;
144 /// Delete an address_space object.
145 _LIBUNWIND_EXPORT void unw_destroy_addr_space(unw_addr_space_t asp) {
146 switch (asp->cpuType) {
147 #if __i386__ || __x86_64__
148 case CPU_TYPE_I386: {
149 unw_addr_space_i386 *as = (unw_addr_space_i386 *)asp;
153 case CPU_TYPE_X86_64: {
154 unw_addr_space_x86_64 *as = (unw_addr_space_x86_64 *)asp;
159 case CPU_TYPE_POWERPC: {
160 unw_addr_space_ppc *as = (unw_addr_space_ppc *)asp;
169 /// Get value of specified register at cursor position in stack frame.
170 _LIBUNWIND_EXPORT int unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
172 _LIBUNWIND_TRACE_API("unw_get_reg(cursor=%p, regNum=%d, &value=%p)",
173 static_cast<void *>(cursor), regNum,
174 static_cast<void *>(value));
175 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
176 if (co->validReg(regNum)) {
177 *value = co->getReg(regNum);
184 /// Set value of specified register at cursor position in stack frame.
185 _LIBUNWIND_EXPORT int unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
187 _LIBUNWIND_TRACE_API("unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR ")",
188 static_cast<void *>(cursor), regNum, value);
189 typedef LocalAddressSpace::pint_t pint_t;
190 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
191 if (co->validReg(regNum)) {
192 co->setReg(regNum, (pint_t)value);
193 // specical case altering IP to re-find info (being called by personality
195 if (regNum == UNW_REG_IP) {
196 unw_proc_info_t info;
197 // First, get the FDE for the old location and then update it.
199 co->setInfoBasedOnIPRegister(false);
200 // If the original call expects stack adjustment, perform this now.
201 // Normal frame unwinding would have included the offset already in the
203 // Note: for PA-RISC and other platforms where the stack grows up,
204 // this should actually be - info.gp. LLVM doesn't currently support
205 // any such platforms and Clang doesn't export a macro for them.
207 co->setReg(UNW_REG_SP, co->getReg(UNW_REG_SP) + info.gp);
215 /// Get value of specified float register at cursor position in stack frame.
216 _LIBUNWIND_EXPORT int unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
217 unw_fpreg_t *value) {
218 _LIBUNWIND_TRACE_API("unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)",
219 static_cast<void *>(cursor), regNum,
220 static_cast<void *>(value));
221 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
222 if (co->validFloatReg(regNum)) {
223 *value = co->getFloatReg(regNum);
230 /// Set value of specified float register at cursor position in stack frame.
231 _LIBUNWIND_EXPORT int unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
233 #if defined(_LIBUNWIND_ARM_EHABI)
234 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)",
235 static_cast<void *>(cursor), regNum, value);
237 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%g)",
238 static_cast<void *>(cursor), regNum, value);
240 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
241 if (co->validFloatReg(regNum)) {
242 co->setFloatReg(regNum, value);
249 /// Move cursor to next frame.
250 _LIBUNWIND_EXPORT int unw_step(unw_cursor_t *cursor) {
251 _LIBUNWIND_TRACE_API("unw_step(cursor=%p)", static_cast<void *>(cursor));
252 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
257 /// Get unwind info at cursor position in stack frame.
258 _LIBUNWIND_EXPORT int unw_get_proc_info(unw_cursor_t *cursor,
259 unw_proc_info_t *info) {
260 _LIBUNWIND_TRACE_API("unw_get_proc_info(cursor=%p, &info=%p)",
261 static_cast<void *>(cursor), static_cast<void *>(info));
262 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
264 if (info->end_ip == 0)
271 /// Resume execution at cursor position (aka longjump).
272 _LIBUNWIND_EXPORT int unw_resume(unw_cursor_t *cursor) {
273 _LIBUNWIND_TRACE_API("unw_resume(cursor=%p)", static_cast<void *>(cursor));
274 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
280 /// Get name of function at cursor position in stack frame.
281 _LIBUNWIND_EXPORT int unw_get_proc_name(unw_cursor_t *cursor, char *buf,
282 size_t bufLen, unw_word_t *offset) {
283 _LIBUNWIND_TRACE_API("unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)",
284 static_cast<void *>(cursor), static_cast<void *>(buf),
285 static_cast<unsigned long>(bufLen));
286 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
287 if (co->getFunctionName(buf, bufLen, offset))
294 /// Checks if a register is a floating-point register.
295 _LIBUNWIND_EXPORT int unw_is_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum) {
296 _LIBUNWIND_TRACE_API("unw_is_fpreg(cursor=%p, regNum=%d)",
297 static_cast<void *>(cursor), regNum);
298 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
299 return co->validFloatReg(regNum);
303 /// Checks if a register is a floating-point register.
304 _LIBUNWIND_EXPORT const char *unw_regname(unw_cursor_t *cursor,
305 unw_regnum_t regNum) {
306 _LIBUNWIND_TRACE_API("unw_regname(cursor=%p, regNum=%d)",
307 static_cast<void *>(cursor), regNum);
308 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
309 return co->getRegisterName(regNum);
313 /// Checks if current frame is signal trampoline.
314 _LIBUNWIND_EXPORT int unw_is_signal_frame(unw_cursor_t *cursor) {
315 _LIBUNWIND_TRACE_API("unw_is_signal_frame(cursor=%p)",
316 static_cast<void *>(cursor));
317 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
318 return co->isSignalFrame();
322 // Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD
323 _LIBUNWIND_EXPORT void unw_save_vfp_as_X(unw_cursor_t *cursor) {
324 _LIBUNWIND_TRACE_API("unw_fpreg_save_vfp_as_X(cursor=%p)",
325 static_cast<void *>(cursor));
326 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
327 return co->saveVFPAsX();
332 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
333 /// SPI: walks cached DWARF entries
334 _LIBUNWIND_EXPORT void unw_iterate_dwarf_unwind_cache(void (*func)(
335 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
336 _LIBUNWIND_TRACE_API("unw_iterate_dwarf_unwind_cache(func=%p)",
337 reinterpret_cast<void *>(func));
338 DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func);
342 /// IPI: for __register_frame()
343 void _unw_add_dynamic_fde(unw_word_t fde) {
344 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
345 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
346 const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE(
347 LocalAddressSpace::sThisAddressSpace,
348 (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo);
349 if (message == NULL) {
350 // dynamically registered FDEs don't have a mach_header group they are in.
351 // Use fde as mh_group
352 unw_word_t mh_group = fdeInfo.fdeStart;
353 DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group,
354 fdeInfo.pcStart, fdeInfo.pcEnd,
357 _LIBUNWIND_DEBUG_LOG("_unw_add_dynamic_fde: bad fde: %s", message);
361 /// IPI: for __deregister_frame()
362 void _unw_remove_dynamic_fde(unw_word_t fde) {
363 // fde is own mh_group
364 DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde);
366 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
367 #endif // !defined(__USING_SJLJ_EXCEPTIONS__)
371 // Add logging hooks in Debug builds only
377 // do manual lock to avoid use of _cxa_guard_acquire or initializers
378 static bool checked = false;
379 static bool log = false;
381 log = (getenv("LIBUNWIND_PRINT_APIS") != NULL);
388 bool logUnwinding() {
389 // do manual lock to avoid use of _cxa_guard_acquire or initializers
390 static bool checked = false;
391 static bool log = false;
393 log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL);
401 // do manual lock to avoid use of _cxa_guard_acquire or initializers
402 static bool checked = false;
403 static bool log = false;
405 log = (getenv("LIBUNWIND_PRINT_DWARF") != NULL);