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