1 //===--------------------------- libunwind.cpp ----------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 // 9 // Implements unw_* functions from <libunwind.h> 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include <libunwind.h> 14 15 #ifndef NDEBUG 16 #include <cstdlib> // getenv 17 #endif 18 #include <new> 19 #include <algorithm> 20 21 #include "libunwind_ext.h" 22 #include "config.h" 23 24 #include <stdlib.h> 25 26 27 #if !defined(__USING_SJLJ_EXCEPTIONS__) 28 #include "AddressSpace.hpp" 29 #include "UnwindCursor.hpp" 30 31 using namespace libunwind; 32 33 /// internal object to represent this processes address space 34 LocalAddressSpace LocalAddressSpace::sThisAddressSpace; 35 36 _LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space = 37 (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace; 38 39 /// record the registers and stack position of the caller 40 extern int unw_getcontext(unw_context_t *); 41 // note: unw_getcontext() implemented in assembly 42 43 /// Create a cursor of a thread in this process given 'context' recorded by 44 /// unw_getcontext(). 45 _LIBUNWIND_EXPORT int unw_init_local(unw_cursor_t *cursor, 46 unw_context_t *context) { 47 _LIBUNWIND_TRACE_API("unw_init_local(cursor=%p, context=%p)", 48 static_cast<void *>(cursor), 49 static_cast<void *>(context)); 50 #if defined(__i386__) 51 # define REGISTER_KIND Registers_x86 52 #elif defined(__x86_64__) 53 # define REGISTER_KIND Registers_x86_64 54 #elif defined(__powerpc64__) 55 # define REGISTER_KIND Registers_ppc64 56 #elif defined(__ppc__) 57 # define REGISTER_KIND Registers_ppc 58 #elif defined(__aarch64__) 59 # define REGISTER_KIND Registers_arm64 60 #elif defined(__arm__) 61 # define REGISTER_KIND Registers_arm 62 #elif defined(__or1k__) 63 # define REGISTER_KIND Registers_or1k 64 #elif defined(__mips__) && defined(_ABIO32) && _MIPS_SIM == _ABIO32 && \ 65 defined(__mips_soft_float) 66 # define REGISTER_KIND Registers_mips_o32 67 #elif defined(__mips64) && defined(__mips_soft_float) 68 # define REGISTER_KIND Registers_mips_newabi 69 #elif defined(__mips__) 70 # warning The MIPS architecture is not supported with this ABI and environment! 71 #else 72 # error Architecture not supported 73 #endif 74 // Use "placement new" to allocate UnwindCursor in the cursor buffer. 75 new ((void *)cursor) 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 84 #ifdef UNW_REMOTE 85 /// Create a cursor into a thread in another process. 86 _LIBUNWIND_EXPORT int unw_init_remote_thread(unw_cursor_t *cursor, 87 unw_addr_space_t as, 88 void *arg) { 89 // special case: unw_init_remote(xx, unw_local_addr_space, xx) 90 if (as == (unw_addr_space_t)&LocalAddressSpace::sThisAddressSpace) 91 return unw_init_local(cursor, NULL); //FIXME 92 93 // use "placement new" to allocate UnwindCursor in the cursor buffer 94 switch (as->cpuType) { 95 case CPU_TYPE_I386: 96 new ((void *)cursor) 97 UnwindCursor<RemoteAddressSpace<Pointer32<LittleEndian>>, 98 Registers_x86>(((unw_addr_space_i386 *)as)->oas, arg); 99 break; 100 case CPU_TYPE_X86_64: 101 new ((void *)cursor) 102 UnwindCursor<RemoteAddressSpace<Pointer64<LittleEndian>>, 103 Registers_x86_64>(((unw_addr_space_x86_64 *)as)->oas, arg); 104 break; 105 case CPU_TYPE_POWERPC: 106 new ((void *)cursor) 107 UnwindCursor<RemoteAddressSpace<Pointer32<BigEndian>>, 108 Registers_ppc>(((unw_addr_space_ppc *)as)->oas, arg); 109 break; 110 default: 111 return UNW_EUNSPEC; 112 } 113 return UNW_ESUCCESS; 114 } 115 116 117 static bool is64bit(task_t task) { 118 return false; // FIXME 119 } 120 121 /// Create an address_space object for use in examining another task. 122 _LIBUNWIND_EXPORT unw_addr_space_t unw_create_addr_space_for_task(task_t task) { 123 #if __i386__ 124 if (is64bit(task)) { 125 unw_addr_space_x86_64 *as = new unw_addr_space_x86_64(task); 126 as->taskPort = task; 127 as->cpuType = CPU_TYPE_X86_64; 128 //as->oas 129 } else { 130 unw_addr_space_i386 *as = new 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 delete as; 148 } 149 break; 150 case CPU_TYPE_X86_64: { 151 unw_addr_space_x86_64 *as = (unw_addr_space_x86_64 *)asp; 152 delete as; 153 } 154 break; 155 #endif 156 case CPU_TYPE_POWERPC: { 157 unw_addr_space_ppc *as = (unw_addr_space_ppc *)asp; 158 delete as; 159 } 160 break; 161 } 162 } 163 #endif // UNW_REMOTE 164 165 166 /// Get value of specified register at cursor position in stack frame. 167 _LIBUNWIND_EXPORT int unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum, 168 unw_word_t *value) { 169 _LIBUNWIND_TRACE_API("unw_get_reg(cursor=%p, regNum=%d, &value=%p)", 170 static_cast<void *>(cursor), regNum, 171 static_cast<void *>(value)); 172 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 173 if (co->validReg(regNum)) { 174 *value = co->getReg(regNum); 175 return UNW_ESUCCESS; 176 } 177 return UNW_EBADREG; 178 } 179 180 181 /// Set value of specified register at cursor position in stack frame. 182 _LIBUNWIND_EXPORT int unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum, 183 unw_word_t value) { 184 _LIBUNWIND_TRACE_API("unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR ")", 185 static_cast<void *>(cursor), regNum, value); 186 typedef LocalAddressSpace::pint_t pint_t; 187 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 188 if (co->validReg(regNum)) { 189 co->setReg(regNum, (pint_t)value); 190 // specical case altering IP to re-find info (being called by personality 191 // function) 192 if (regNum == UNW_REG_IP) 193 co->setInfoBasedOnIPRegister(false); 194 return UNW_ESUCCESS; 195 } 196 return UNW_EBADREG; 197 } 198 199 200 /// Get value of specified float register at cursor position in stack frame. 201 _LIBUNWIND_EXPORT int unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, 202 unw_fpreg_t *value) { 203 _LIBUNWIND_TRACE_API("unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)", 204 static_cast<void *>(cursor), regNum, 205 static_cast<void *>(value)); 206 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 207 if (co->validFloatReg(regNum)) { 208 *value = co->getFloatReg(regNum); 209 return UNW_ESUCCESS; 210 } 211 return UNW_EBADREG; 212 } 213 214 215 /// Set value of specified float register at cursor position in stack frame. 216 _LIBUNWIND_EXPORT int unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum, 217 unw_fpreg_t value) { 218 #if defined(_LIBUNWIND_ARM_EHABI) 219 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)", 220 static_cast<void *>(cursor), regNum, value); 221 #else 222 _LIBUNWIND_TRACE_API("unw_set_fpreg(cursor=%p, regNum=%d, value=%g)", 223 static_cast<void *>(cursor), regNum, value); 224 #endif 225 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 226 if (co->validFloatReg(regNum)) { 227 co->setFloatReg(regNum, value); 228 return UNW_ESUCCESS; 229 } 230 return UNW_EBADREG; 231 } 232 233 234 /// Move cursor to next frame. 235 _LIBUNWIND_EXPORT int unw_step(unw_cursor_t *cursor) { 236 _LIBUNWIND_TRACE_API("unw_step(cursor=%p)", static_cast<void *>(cursor)); 237 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 238 return co->step(); 239 } 240 241 242 /// Get unwind info at cursor position in stack frame. 243 _LIBUNWIND_EXPORT int unw_get_proc_info(unw_cursor_t *cursor, 244 unw_proc_info_t *info) { 245 _LIBUNWIND_TRACE_API("unw_get_proc_info(cursor=%p, &info=%p)", 246 static_cast<void *>(cursor), static_cast<void *>(info)); 247 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 248 co->getInfo(info); 249 if (info->end_ip == 0) 250 return UNW_ENOINFO; 251 else 252 return UNW_ESUCCESS; 253 } 254 255 256 /// Resume execution at cursor position (aka longjump). 257 _LIBUNWIND_EXPORT int unw_resume(unw_cursor_t *cursor) { 258 _LIBUNWIND_TRACE_API("unw_resume(cursor=%p)", static_cast<void *>(cursor)); 259 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 260 co->jumpto(); 261 return UNW_EUNSPEC; 262 } 263 264 265 /// Get name of function at cursor position in stack frame. 266 _LIBUNWIND_EXPORT int unw_get_proc_name(unw_cursor_t *cursor, char *buf, 267 size_t bufLen, unw_word_t *offset) { 268 _LIBUNWIND_TRACE_API("unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)", 269 static_cast<void *>(cursor), static_cast<void *>(buf), 270 static_cast<unsigned long>(bufLen)); 271 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 272 if (co->getFunctionName(buf, bufLen, offset)) 273 return UNW_ESUCCESS; 274 else 275 return UNW_EUNSPEC; 276 } 277 278 279 /// Checks if a register is a floating-point register. 280 _LIBUNWIND_EXPORT int unw_is_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum) { 281 _LIBUNWIND_TRACE_API("unw_is_fpreg(cursor=%p, regNum=%d)", 282 static_cast<void *>(cursor), regNum); 283 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 284 return co->validFloatReg(regNum); 285 } 286 287 288 /// Checks if a register is a floating-point register. 289 _LIBUNWIND_EXPORT const char *unw_regname(unw_cursor_t *cursor, 290 unw_regnum_t regNum) { 291 _LIBUNWIND_TRACE_API("unw_regname(cursor=%p, regNum=%d)", 292 static_cast<void *>(cursor), regNum); 293 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 294 return co->getRegisterName(regNum); 295 } 296 297 298 /// Checks if current frame is signal trampoline. 299 _LIBUNWIND_EXPORT int unw_is_signal_frame(unw_cursor_t *cursor) { 300 _LIBUNWIND_TRACE_API("unw_is_signal_frame(cursor=%p)", 301 static_cast<void *>(cursor)); 302 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 303 return co->isSignalFrame(); 304 } 305 306 #ifdef __arm__ 307 // Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD 308 _LIBUNWIND_EXPORT void unw_save_vfp_as_X(unw_cursor_t *cursor) { 309 _LIBUNWIND_TRACE_API("unw_fpreg_save_vfp_as_X(cursor=%p)", 310 static_cast<void *>(cursor)); 311 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; 312 return co->saveVFPAsX(); 313 } 314 #endif 315 316 317 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 318 /// SPI: walks cached DWARF entries 319 _LIBUNWIND_EXPORT void unw_iterate_dwarf_unwind_cache(void (*func)( 320 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) { 321 _LIBUNWIND_TRACE_API("unw_iterate_dwarf_unwind_cache(func=%p)", 322 reinterpret_cast<void *>(func)); 323 DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func); 324 } 325 326 327 /// IPI: for __register_frame() 328 void _unw_add_dynamic_fde(unw_word_t fde) { 329 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo; 330 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo; 331 const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE( 332 LocalAddressSpace::sThisAddressSpace, 333 (LocalAddressSpace::pint_t) fde, &fdeInfo, &cieInfo); 334 if (message == NULL) { 335 // dynamically registered FDEs don't have a mach_header group they are in. 336 // Use fde as mh_group 337 unw_word_t mh_group = fdeInfo.fdeStart; 338 DwarfFDECache<LocalAddressSpace>::add((LocalAddressSpace::pint_t)mh_group, 339 fdeInfo.pcStart, fdeInfo.pcEnd, 340 fdeInfo.fdeStart); 341 } else { 342 _LIBUNWIND_DEBUG_LOG("_unw_add_dynamic_fde: bad fde: %s", message); 343 } 344 } 345 346 /// IPI: for __deregister_frame() 347 void _unw_remove_dynamic_fde(unw_word_t fde) { 348 // fde is own mh_group 349 DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde); 350 } 351 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) 352 #endif // !defined(__USING_SJLJ_EXCEPTIONS__) 353 354 355 356 // Add logging hooks in Debug builds only 357 #ifndef NDEBUG 358 #include <stdlib.h> 359 360 _LIBUNWIND_HIDDEN 361 bool logAPIs() { 362 // do manual lock to avoid use of _cxa_guard_acquire or initializers 363 static bool checked = false; 364 static bool log = false; 365 if (!checked) { 366 log = (getenv("LIBUNWIND_PRINT_APIS") != NULL); 367 checked = true; 368 } 369 return log; 370 } 371 372 _LIBUNWIND_HIDDEN 373 bool logUnwinding() { 374 // do manual lock to avoid use of _cxa_guard_acquire or initializers 375 static bool checked = false; 376 static bool log = false; 377 if (!checked) { 378 log = (getenv("LIBUNWIND_PRINT_UNWINDING") != NULL); 379 checked = true; 380 } 381 return log; 382 } 383 384 _LIBUNWIND_HIDDEN 385 bool logDWARF() { 386 // do manual lock to avoid use of _cxa_guard_acquire or initializers 387 static bool checked = false; 388 static bool log = false; 389 if (!checked) { 390 log = (getenv("LIBUNWIND_PRINT_DWARF") != NULL); 391 checked = true; 392 } 393 return log; 394 } 395 396 #endif // NDEBUG 397 398