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