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 #ifndef NDEBUG 15 #include <cstdlib> // getenv 16 #endif 17 18 #include "libunwind_ext.h" 19 #include "config.h" 20 21 #include <stdlib.h> 22 23 24 #if !defined(__USING_SJLJ_EXCEPTIONS__) 25 #include "AddressSpace.hpp" 26 #include "UnwindCursor.hpp" 27 28 using namespace libunwind; 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 #ifdef UNW_REMOTE 83 /// Create a cursor into a thread in another process. 84 _LIBUNWIND_EXPORT int unw_init_remote_thread(unw_cursor_t *cursor, 85 unw_addr_space_t as, 86 void *arg) { 87 // special case: unw_init_remote(xx, unw_local_addr_space, xx) 88 if (as == (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace) 89 return unw_init_local(cursor, NULL); //FIXME 90 91 // use "placement new" to allocate UnwindCursor in the cursor buffer 92 switch (as->cpuType) { 93 case CPU_TYPE_I386: 94 new ((void *)cursor) 95 UnwindCursor<RemoteAddressSpace<Pointer32<LittleEndian>>, 96 Registers_x86>(((unw_addr_space_i386 *)as)->oas, arg); 97 break; 98 case CPU_TYPE_X86_64: 99 new ((void *)cursor) 100 UnwindCursor<RemoteAddressSpace<Pointer64<LittleEndian>>, 101 Registers_x86_64>(((unw_addr_space_x86_64 *)as)->oas, arg); 102 break; 103 case CPU_TYPE_POWERPC: 104 new ((void *)cursor) 105 UnwindCursor<RemoteAddressSpace<Pointer32<BigEndian>>, 106 Registers_ppc>(((unw_addr_space_ppc *)as)->oas, arg); 107 break; 108 default: 109 return UNW_EUNSPEC; 110 } 111 return UNW_ESUCCESS; 112 } 113 114 115 static bool is64bit(task_t task) { 116 return false; // FIXME 117 } 118 119 /// Create an address_space object for use in examining another task. 120 _LIBUNWIND_EXPORT unw_addr_space_t unw_create_addr_space_for_task(task_t task) { 121 #if __i386__ 122 if (is64bit(task)) { 123 unw_addr_space_x86_64 *as = malloc(sizeof(unw_addr_space_x86_64)); 124 new (as) unw_addr_space_x86_64(task); 125 as->taskPort = task; 126 as->cpuType = CPU_TYPE_X86_64; 127 //as->oas 128 } else { 129 unw_addr_space_i386 *as = malloc(sizeof(unw_addr_space_i386)); 130 new (as) unw_addr_space_i386(task); 131 as->taskPort = task; 132 as->cpuType = CPU_TYPE_I386; 133 //as->oas 134 } 135 #else 136 // FIXME 137 #endif 138 } 139 140 141 /// Delete an address_space object. 142 _LIBUNWIND_EXPORT void unw_destroy_addr_space(unw_addr_space_t asp) { 143 switch (asp->cpuType) { 144 #if __i386__ || __x86_64__ 145 case CPU_TYPE_I386: { 146 unw_addr_space_i386 *as = (unw_addr_space_i386 *)asp; 147 as->~unw_addr_space_i386(); 148 free(as); 149 } 150 break; 151 case CPU_TYPE_X86_64: { 152 unw_addr_space_x86_64 *as = (unw_addr_space_x86_64 *)asp; 153 as->~unw_addr_space_x86_64(); 154 free(as); 155 } 156 break; 157 #endif 158 case CPU_TYPE_POWERPC: { 159 unw_addr_space_ppc *as = (unw_addr_space_ppc *)asp; 160 as->~unw_addr_space_ppc(); 161 free(as); 162 } 163 break; 164 } 165 } 166 #endif // UNW_REMOTE 167 168 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, 171 unw_word_t *value) { 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); 178 return UNW_ESUCCESS; 179 } 180 return UNW_EBADREG; 181 } 182 183 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, 186 unw_word_t value) { 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 194 // function) 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. 198 co->getInfo(&info); 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 202 // CFA computation. 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. 206 if (info.gp) 207 co->setReg(UNW_REG_SP, co->getReg(UNW_REG_SP) + info.gp); 208 } 209 return UNW_ESUCCESS; 210 } 211 return UNW_EBADREG; 212 } 213 214 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); 224 return UNW_ESUCCESS; 225 } 226 return UNW_EBADREG; 227 } 228 229 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, 232 unw_fpreg_t value) { 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); 236 #else 237 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%g)", 238 static_cast<void *>(cursor), regNum, value); 239 #endif 240 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 241 if (co->validFloatReg(regNum)) { 242 co->setFloatReg(regNum, value); 243 return UNW_ESUCCESS; 244 } 245 return UNW_EBADREG; 246 } 247 248 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; 253 return co->step(); 254 } 255 256 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; 263 co->getInfo(info); 264 if (info->end_ip == 0) 265 return UNW_ENOINFO; 266 else 267 return UNW_ESUCCESS; 268 } 269 270 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; 275 co->jumpto(); 276 return UNW_EUNSPEC; 277 } 278 279 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)) 288 return UNW_ESUCCESS; 289 else 290 return UNW_EUNSPEC; 291 } 292 293 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); 300 } 301 302 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); 310 } 311 312 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(); 319 } 320 321 #ifdef __arm__ 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(); 328 } 329 #endif 330 331 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); 339 } 340 341 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, 355 fdeInfo.fdeStart); 356 } else { 357 _LIBUNWIND_DEBUG_LOG("_unw_add_dynamic_fde: bad fde: %s", message); 358 } 359 } 360 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); 365 } 366 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 367 #endif // !defined(__USING_SJLJ_EXCEPTIONS__) 368 369 370 371 // Add logging hooks in Debug builds only 372 #ifndef NDEBUG 373 #include <stdlib.h> 374 375 _LIBUNWIND_HIDDEN 376 bool logAPIs() { 377 // do manual lock to avoid use of _cxa_guard_acquire or initializers 378 static bool checked = false; 379 static bool log = false; 380 if (!checked) { 381 log = (getenv("LIBUNWIND_PRINT_APIS") != NULL); 382 checked = true; 383 } 384 return log; 385 } 386 387 _LIBUNWIND_HIDDEN 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; 392 if (!checked) { 393 log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL); 394 checked = true; 395 } 396 return log; 397 } 398 399 _LIBUNWIND_HIDDEN 400 bool logDWARF() { 401 // do manual lock to avoid use of _cxa_guard_acquire or initializers 402 static bool checked = false; 403 static bool log = false; 404 if (!checked) { 405 log = (getenv("LIBUNWIND_PRINT_DWARF") != NULL); 406 checked = true; 407 } 408 return log; 409 } 410 411 #endif // NDEBUG 412 413