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