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(__hexagon__) 54 # define REGISTER_KIND Registers_hexagon 55 #elif defined(__mips__) && defined(_ABIO32) && _MIPS_SIM == _ABIO32 56 # define REGISTER_KIND Registers_mips_o32 57 #elif defined(__mips64) 58 # define REGISTER_KIND Registers_mips_newabi 59 #elif defined(__mips__) 60 # warning The MIPS architecture is not supported with this ABI and environment! 61 #elif defined(__sparc__) 62 # define REGISTER_KIND Registers_sparc 63 #elif defined(__riscv) && __riscv_xlen == 64 64 # define REGISTER_KIND Registers_riscv 65 #elif defined(__ve__) 66 # define REGISTER_KIND Registers_ve 67 #else 68 # error Architecture not supported 69 #endif 70 // Use "placement new" to allocate UnwindCursor in the cursor buffer. 71 new (reinterpret_cast<UnwindCursor<LocalAddressSpace, REGISTER_KIND> *>(cursor)) 72 UnwindCursor<LocalAddressSpace, REGISTER_KIND>( 73 context, LocalAddressSpace::sThisAddressSpace); 74 #undef REGISTER_KIND 75 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 76 co->setInfoBasedOnIPRegister(); 77 78 return UNW_ESUCCESS; 79 } 80 _LIBUNWIND_WEAK_ALIAS(__unw_init_local, unw_init_local) 81 82 /// Get value of specified register at cursor position in stack frame. 83 _LIBUNWIND_HIDDEN 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 _LIBUNWIND_WEAK_ALIAS(__unw_get_reg, unw_get_reg) 96 97 /// Set value of specified register at cursor position in stack frame. 98 _LIBUNWIND_HIDDEN 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 ")", 102 static_cast<void *>(cursor), regNum, value); 103 typedef LocalAddressSpace::pint_t pint_t; 104 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 105 if (co->validReg(regNum)) { 106 co->setReg(regNum, (pint_t)value); 107 // specical case altering IP to re-find info (being called by personality 108 // function) 109 if (regNum == UNW_REG_IP) { 110 unw_proc_info_t info; 111 // First, get the FDE for the old location and then update it. 112 co->getInfo(&info); 113 co->setInfoBasedOnIPRegister(false); 114 // If the original call expects stack adjustment, perform this now. 115 // Normal frame unwinding would have included the offset already in the 116 // CFA computation. 117 // Note: for PA-RISC and other platforms where the stack grows up, 118 // this should actually be - info.gp. LLVM doesn't currently support 119 // any such platforms and Clang doesn't export a macro for them. 120 if (info.gp) 121 co->setReg(UNW_REG_SP, co->getReg(UNW_REG_SP) + info.gp); 122 } 123 return UNW_ESUCCESS; 124 } 125 return UNW_EBADREG; 126 } 127 _LIBUNWIND_WEAK_ALIAS(__unw_set_reg, unw_set_reg) 128 129 /// Get value of specified float register at cursor position in stack frame. 130 _LIBUNWIND_HIDDEN int __unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, 131 unw_fpreg_t *value) { 132 _LIBUNWIND_TRACE_API("__unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)", 133 static_cast<void *>(cursor), regNum, 134 static_cast<void *>(value)); 135 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 136 if (co->validFloatReg(regNum)) { 137 *value = co->getFloatReg(regNum); 138 return UNW_ESUCCESS; 139 } 140 return UNW_EBADREG; 141 } 142 _LIBUNWIND_WEAK_ALIAS(__unw_get_fpreg, unw_get_fpreg) 143 144 /// Set value of specified float register at cursor position in stack frame. 145 _LIBUNWIND_HIDDEN int __unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, 146 unw_fpreg_t value) { 147 #if defined(_LIBUNWIND_ARM_EHABI) 148 _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)", 149 static_cast<void *>(cursor), regNum, value); 150 #else 151 _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%g)", 152 static_cast<void *>(cursor), regNum, value); 153 #endif 154 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 155 if (co->validFloatReg(regNum)) { 156 co->setFloatReg(regNum, value); 157 return UNW_ESUCCESS; 158 } 159 return UNW_EBADREG; 160 } 161 _LIBUNWIND_WEAK_ALIAS(__unw_set_fpreg, unw_set_fpreg) 162 163 /// Move cursor to next frame. 164 _LIBUNWIND_HIDDEN int __unw_step(unw_cursor_t *cursor) { 165 _LIBUNWIND_TRACE_API("__unw_step(cursor=%p)", static_cast<void *>(cursor)); 166 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 167 return co->step(); 168 } 169 _LIBUNWIND_WEAK_ALIAS(__unw_step, unw_step) 170 171 /// Get unwind info at cursor position in stack frame. 172 _LIBUNWIND_HIDDEN int __unw_get_proc_info(unw_cursor_t *cursor, 173 unw_proc_info_t *info) { 174 _LIBUNWIND_TRACE_API("__unw_get_proc_info(cursor=%p, &info=%p)", 175 static_cast<void *>(cursor), static_cast<void *>(info)); 176 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 177 co->getInfo(info); 178 if (info->end_ip == 0) 179 return UNW_ENOINFO; 180 return UNW_ESUCCESS; 181 } 182 _LIBUNWIND_WEAK_ALIAS(__unw_get_proc_info, unw_get_proc_info) 183 184 /// Resume execution at cursor position (aka longjump). 185 _LIBUNWIND_HIDDEN 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 _LIBUNWIND_WEAK_ALIAS(__unw_resume, unw_resume) 192 193 /// Get name of function at cursor position in stack frame. 194 _LIBUNWIND_HIDDEN 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 return UNW_EUNSPEC; 203 } 204 _LIBUNWIND_WEAK_ALIAS(__unw_get_proc_name, unw_get_proc_name) 205 206 /// Checks if a register is a floating-point register. 207 _LIBUNWIND_HIDDEN int __unw_is_fpreg(unw_cursor_t *cursor, 208 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 _LIBUNWIND_WEAK_ALIAS(__unw_is_fpreg, unw_is_fpreg) 215 216 /// Checks if a register is a floating-point register. 217 _LIBUNWIND_HIDDEN 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 _LIBUNWIND_WEAK_ALIAS(__unw_regname, unw_regname) 225 226 /// Checks if current frame is signal trampoline. 227 _LIBUNWIND_HIDDEN 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 _LIBUNWIND_WEAK_ALIAS(__unw_is_signal_frame, unw_is_signal_frame) 234 235 #ifdef __arm__ 236 // Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD 237 _LIBUNWIND_HIDDEN void __unw_save_vfp_as_X(unw_cursor_t *cursor) { 238 _LIBUNWIND_TRACE_API("__unw_get_fpreg_save_vfp_as_X(cursor=%p)", 239 static_cast<void *>(cursor)); 240 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 241 return co->saveVFPAsX(); 242 } 243 _LIBUNWIND_WEAK_ALIAS(__unw_save_vfp_as_X, unw_save_vfp_as_X) 244 #endif 245 246 247 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 248 /// SPI: walks cached DWARF entries 249 _LIBUNWIND_HIDDEN void __unw_iterate_dwarf_unwind_cache(void (*func)( 250 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) { 251 _LIBUNWIND_TRACE_API("__unw_iterate_dwarf_unwind_cache(func=%p)", 252 reinterpret_cast<void *>(func)); 253 DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func); 254 } 255 _LIBUNWIND_WEAK_ALIAS(__unw_iterate_dwarf_unwind_cache, 256 unw_iterate_dwarf_unwind_cache) 257 258 /// IPI: for __register_frame() 259 void __unw_add_dynamic_fde(unw_word_t fde) { 260 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; 261 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; 262 const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE( 263 LocalAddressSpace::sThisAddressSpace, 264 (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo); 265 if (message == NULL) { 266 // dynamically registered FDEs don't have a mach_header group they are in. 267 // Use fde as mh_group 268 unw_word_t mh_group = fdeInfo.fdeStart; 269 DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group, 270 fdeInfo.pcStart, fdeInfo.pcEnd, 271 fdeInfo.fdeStart); 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 DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde); 281 } 282 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 283 #endif // !defined(__USING_SJLJ_EXCEPTIONS__) 284 285 286 287 // Add logging hooks in Debug builds only 288 #ifndef NDEBUG 289 #include <stdlib.h> 290 291 _LIBUNWIND_HIDDEN 292 bool logAPIs() { 293 // do manual lock to avoid use of _cxa_guard_acquire or initializers 294 static bool checked = false; 295 static bool log = false; 296 if (!checked) { 297 log = (getenv("LIBUNWIND_PRINT_APIS") != NULL); 298 checked = true; 299 } 300 return log; 301 } 302 303 _LIBUNWIND_HIDDEN 304 bool logUnwinding() { 305 // do manual lock to avoid use of _cxa_guard_acquire or initializers 306 static bool checked = false; 307 static bool log = false; 308 if (!checked) { 309 log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL); 310 checked = true; 311 } 312 return log; 313 } 314 315 _LIBUNWIND_HIDDEN 316 bool logDWARF() { 317 // do manual lock to avoid use of _cxa_guard_acquire or initializers 318 static bool checked = false; 319 static bool log = false; 320 if (!checked) { 321 log = (getenv("LIBUNWIND_PRINT_DWARF") != NULL); 322 checked = true; 323 } 324 return log; 325 } 326 327 #endif // NDEBUG 328 329