1//===----------------------------------------------------------------------===//
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 "config.h"
15#include "libunwind_ext.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__) && !defined(__wasm__)
30#include "AddressSpace.hpp"
31#include "UnwindCursor.hpp"
32
33using namespace libunwind;
34
35/// internal object to represent this process's address space
36LocalAddressSpace 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(__powerpc__)
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__) && defined(__arch64__)
71#define REGISTER_KIND Registers_sparc64
72#elif defined(__sparc__)
73# define REGISTER_KIND Registers_sparc
74#elif defined(__riscv)
75# define REGISTER_KIND Registers_riscv
76#elif defined(__ve__)
77# define REGISTER_KIND Registers_ve
78#elif defined(__s390x__)
79# define REGISTER_KIND Registers_s390x
80#elif defined(__loongarch__) && __loongarch_grlen == 64
81#define REGISTER_KIND Registers_loongarch
82#else
83# error Architecture not supported
84#endif
85 // Use "placement new" to allocate UnwindCursor in the cursor buffer.
86 new (reinterpret_cast<UnwindCursor<LocalAddressSpace, REGISTER_KIND> *>(cursor))
87 UnwindCursor<LocalAddressSpace, REGISTER_KIND>(
88 context, LocalAddressSpace::sThisAddressSpace);
89#undef REGISTER_KIND
90 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
91 co->setInfoBasedOnIPRegister();
92
93 return UNW_ESUCCESS;
94}
95_LIBUNWIND_WEAK_ALIAS(__unw_init_local, unw_init_local)
96
97/// Get value of specified register at cursor position in stack frame.
98_LIBUNWIND_HIDDEN int __unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
99 unw_word_t *value) {
100 _LIBUNWIND_TRACE_API("__unw_get_reg(cursor=%p, regNum=%d, &value=%p)",
101 static_cast<void *>(cursor), regNum,
102 static_cast<void *>(value));
103 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
104 if (co->validReg(regNum)) {
105 *value = co->getReg(regNum);
106 return UNW_ESUCCESS;
107 }
108 return UNW_EBADREG;
109}
110_LIBUNWIND_WEAK_ALIAS(__unw_get_reg, unw_get_reg)
111
112/// Set value of specified register at cursor position in stack frame.
113_LIBUNWIND_HIDDEN int __unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
114 unw_word_t value) {
115 _LIBUNWIND_TRACE_API("__unw_set_reg(cursor=%p, regNum=%d, value=0x%" PRIxPTR
116 ")",
117 static_cast<void *>(cursor), regNum, value);
118 typedef LocalAddressSpace::pint_t pint_t;
119 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
120 if (co->validReg(regNum)) {
121 // special case altering IP to re-find info (being called by personality
122 // function)
123 if (regNum == UNW_REG_IP) {
124 unw_proc_info_t info;
125 // First, get the FDE for the old location and then update it.
126 co->getInfo(&info);
127
128 pint_t sp = (pint_t)co->getReg(UNW_REG_SP);
129
130#if defined(_LIBUNWIND_TARGET_AARCH64_AUTHENTICATED_UNWINDING)
131 {
132 // It is only valid to set the IP within the current function. This is
133 // important for ptrauth, otherwise the IP cannot be correctly signed.
134 // The current signature of `value` is via the schema:
135 // __ptrauth(ptrauth_key_return_address, <<sp>>, 0)
136 // For this to be generally usable we manually re-sign it to the
137 // directly supported schema:
138 // __ptrauth(ptrauth_key_return_address, 1, 0)
139 unw_word_t
140 __unwind_ptrauth_restricted_intptr(ptrauth_key_return_address, 1,
141 0) authenticated_value;
142 unw_word_t opaque_value = (uint64_t)ptrauth_auth_and_resign(
143 (void *)value, ptrauth_key_return_address, sp,
144 ptrauth_key_return_address, &authenticated_value);
145 memmove(reinterpret_cast<void *>(&authenticated_value),
146 reinterpret_cast<void *>(&opaque_value),
147 sizeof(authenticated_value));
148 if (authenticated_value < info.start_ip ||
149 authenticated_value > info.end_ip)
150 _LIBUNWIND_ABORT("PC vs frame info mismatch");
151
152 // PC should have been signed with the sp, so we verify that
153 // roundtripping does not fail. The `ptrauth_auth_and_resign` is
154 // guaranteed to trap on authentication failure even without FPAC
155 // feature.
156 pint_t pc = (pint_t)co->getReg(UNW_REG_IP);
157 if (ptrauth_auth_and_resign((void *)pc, ptrauth_key_return_address, sp,
158 ptrauth_key_return_address,
159 sp) != (void *)pc) {
160 _LIBUNWIND_LOG(
161 "Bad unwind with PAuth-enabled ABI (0x%zX, 0x%zX)->0x%zX\n", pc,
162 sp,
163 (pint_t)ptrauth_auth_data((void *)pc, ptrauth_key_return_address,
164 sp));
165 _LIBUNWIND_ABORT("Bad unwind with PAuth-enabled ABI");
166 }
167 }
168#endif
169
170 // If the original call expects stack adjustment, perform this now.
171 // Normal frame unwinding would have included the offset already in the
172 // CFA computation.
173 // Note: for PA-RISC and other platforms where the stack grows up,
174 // this should actually be - info.gp. LLVM doesn't currently support
175 // any such platforms and Clang doesn't export a macro for them.
176 if (info.gp)
177 co->setReg(UNW_REG_SP, sp + info.gp);
178 co->setReg(UNW_REG_IP, value);
179 co->setInfoBasedOnIPRegister(false);
180 } else {
181 co->setReg(regNum, (pint_t)value);
182 }
183 return UNW_ESUCCESS;
184 }
185 return UNW_EBADREG;
186}
187_LIBUNWIND_WEAK_ALIAS(__unw_set_reg, unw_set_reg)
188
189/// Get value of specified float register at cursor position in stack frame.
190_LIBUNWIND_HIDDEN int __unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
191 unw_fpreg_t *value) {
192 _LIBUNWIND_TRACE_API("__unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)",
193 static_cast<void *>(cursor), regNum,
194 static_cast<void *>(value));
195 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
196 if (co->validFloatReg(regNum)) {
197 *value = co->getFloatReg(regNum);
198 return UNW_ESUCCESS;
199 }
200 return UNW_EBADREG;
201}
202_LIBUNWIND_WEAK_ALIAS(__unw_get_fpreg, unw_get_fpreg)
203
204/// Set value of specified float register at cursor position in stack frame.
205_LIBUNWIND_HIDDEN int __unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
206 unw_fpreg_t value) {
207#if defined(_LIBUNWIND_ARM_EHABI)
208 _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%llX)",
209 static_cast<void *>(cursor), regNum, value);
210#else
211 _LIBUNWIND_TRACE_API("__unw_set_fpreg(cursor=%p, regNum=%d, value=%g)",
212 static_cast<void *>(cursor), regNum, value);
213#endif
214 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
215 if (co->validFloatReg(regNum)) {
216 co->setFloatReg(regNum, value);
217 return UNW_ESUCCESS;
218 }
219 return UNW_EBADREG;
220}
221_LIBUNWIND_WEAK_ALIAS(__unw_set_fpreg, unw_set_fpreg)
222
223/// Move cursor to next frame.
224_LIBUNWIND_HIDDEN int __unw_step(unw_cursor_t *cursor) {
225 _LIBUNWIND_TRACE_API("__unw_step(cursor=%p)", static_cast<void *>(cursor));
226 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
227 return co->step();
228}
229_LIBUNWIND_WEAK_ALIAS(__unw_step, unw_step)
230
231// Move cursor to next frame and for stage2 of unwinding.
232// This resets MTE tags of tagged frames to zero.
233extern "C" _LIBUNWIND_HIDDEN int __unw_step_stage2(unw_cursor_t *cursor) {
234 _LIBUNWIND_TRACE_API("__unw_step_stage2(cursor=%p)",
235 static_cast<void *>(cursor));
236 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
237 return co->step(true);
238}
239
240/// Get unwind info at cursor position in stack frame.
241_LIBUNWIND_HIDDEN int __unw_get_proc_info(unw_cursor_t *cursor,
242 unw_proc_info_t *info) {
243 _LIBUNWIND_TRACE_API("__unw_get_proc_info(cursor=%p, &info=%p)",
244 static_cast<void *>(cursor), static_cast<void *>(info));
245 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
246 co->getInfo(info);
247 if (info->end_ip == 0)
248 return UNW_ENOINFO;
249 return UNW_ESUCCESS;
250}
251_LIBUNWIND_WEAK_ALIAS(__unw_get_proc_info, unw_get_proc_info)
252
253/// Rebalance the execution flow by injecting the right amount of `ret`
254/// instruction relatively to the amount of `walkedFrames` then resume execution
255/// at cursor position (aka longjump).
256_LIBUNWIND_HIDDEN int __unw_resume_with_frames_walked(unw_cursor_t *cursor,
257 unsigned walkedFrames) {
258 _LIBUNWIND_TRACE_API("__unw_resume(cursor=%p, walkedFrames=%u)",
259 static_cast<void *>(cursor), walkedFrames);
260#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
261 // Inform the ASan runtime that now might be a good time to clean stuff up.
262 __asan_handle_no_return();
263#endif
264#ifdef _LIBUNWIND_TRACE_RET_INJECT
265 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
266 co->setWalkedFrames(walkedFrames);
267#endif
268 return __unw_resume(cursor);
269}
270_LIBUNWIND_WEAK_ALIAS(__unw_resume_with_frames_walked,
271 unw_resume_with_frames_walked)
272
273/// Legacy function. Resume execution at cursor position (aka longjump).
274_LIBUNWIND_HIDDEN int __unw_resume(unw_cursor_t *cursor) {
275 _LIBUNWIND_TRACE_API("__unw_resume(cursor=%p)", static_cast<void *>(cursor));
276#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
277 // Inform the ASan runtime that now might be a good time to clean stuff up.
278 __asan_handle_no_return();
279#endif
280 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
281 co->jumpto();
282 return UNW_EUNSPEC;
283}
284_LIBUNWIND_WEAK_ALIAS(__unw_resume, unw_resume)
285
286/// Get name of function at cursor position in stack frame.
287_LIBUNWIND_HIDDEN int __unw_get_proc_name(unw_cursor_t *cursor, char *buf,
288 size_t bufLen, unw_word_t *offset) {
289 _LIBUNWIND_TRACE_API("__unw_get_proc_name(cursor=%p, &buf=%p, bufLen=%lu)",
290 static_cast<void *>(cursor), static_cast<void *>(buf),
291 static_cast<unsigned long>(bufLen));
292 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
293 if (co->getFunctionName(buf, bufLen, offset))
294 return UNW_ESUCCESS;
295 return UNW_EUNSPEC;
296}
297_LIBUNWIND_WEAK_ALIAS(__unw_get_proc_name, unw_get_proc_name)
298
299/// Checks if a register is a floating-point register.
300_LIBUNWIND_HIDDEN int __unw_is_fpreg(unw_cursor_t *cursor,
301 unw_regnum_t regNum) {
302 _LIBUNWIND_TRACE_API("__unw_is_fpreg(cursor=%p, regNum=%d)",
303 static_cast<void *>(cursor), regNum);
304 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
305 return co->validFloatReg(regNum);
306}
307_LIBUNWIND_WEAK_ALIAS(__unw_is_fpreg, unw_is_fpreg)
308
309/// Get name of specified register at cursor position in stack frame.
310_LIBUNWIND_HIDDEN const char *__unw_regname(unw_cursor_t *cursor,
311 unw_regnum_t regNum) {
312 _LIBUNWIND_TRACE_API("__unw_regname(cursor=%p, regNum=%d)",
313 static_cast<void *>(cursor), regNum);
314 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
315 return co->getRegisterName(regNum);
316}
317_LIBUNWIND_WEAK_ALIAS(__unw_regname, unw_regname)
318
319/// Checks if current frame is signal trampoline.
320_LIBUNWIND_HIDDEN int __unw_is_signal_frame(unw_cursor_t *cursor) {
321 _LIBUNWIND_TRACE_API("__unw_is_signal_frame(cursor=%p)",
322 static_cast<void *>(cursor));
323 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
324 return co->isSignalFrame();
325}
326_LIBUNWIND_WEAK_ALIAS(__unw_is_signal_frame, unw_is_signal_frame)
327
328#ifdef _AIX
329_LIBUNWIND_EXPORT uintptr_t __unw_get_data_rel_base(unw_cursor_t *cursor) {
330 _LIBUNWIND_TRACE_API("unw_get_data_rel_base(cursor=%p)",
331 static_cast<void *>(cursor));
332 AbstractUnwindCursor *co = reinterpret_cast<AbstractUnwindCursor *>(cursor);
333 return co->getDataRelBase();
334}
335_LIBUNWIND_WEAK_ALIAS(__unw_get_data_rel_base, unw_get_data_rel_base)
336#endif
337
338#ifdef __arm__
339// Save VFP registers d0-d15 using FSTMIADX instead of FSTMIADD
340_LIBUNWIND_HIDDEN void __unw_save_vfp_as_X(unw_cursor_t *cursor) {
341 _LIBUNWIND_TRACE_API("__unw_get_fpreg_save_vfp_as_X(cursor=%p)",
342 static_cast<void *>(cursor));
343 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
344 return co->saveVFPAsX();
345}
346_LIBUNWIND_WEAK_ALIAS(__unw_save_vfp_as_X, unw_save_vfp_as_X)
347#endif
348
349
350#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
351/// SPI: walks cached DWARF entries
352_LIBUNWIND_HIDDEN void __unw_iterate_dwarf_unwind_cache(void (*func)(
353 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
354 _LIBUNWIND_TRACE_API("__unw_iterate_dwarf_unwind_cache(func=%p)",
355 reinterpret_cast<void *>(func));
356 DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func);
357}
358_LIBUNWIND_WEAK_ALIAS(__unw_iterate_dwarf_unwind_cache,
359 unw_iterate_dwarf_unwind_cache)
360
361/// IPI: for __register_frame()
362void __unw_add_dynamic_fde(unw_word_t fde) {
363 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
364 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
365 const char *message = CFI_Parser<LocalAddressSpace>::decodeFDE(
366 addressSpace&: LocalAddressSpace::sThisAddressSpace,
367 fdeStart: (LocalAddressSpace::pint_t) fde, fdeInfo: &fdeInfo, cieInfo: &cieInfo);
368 if (message == NULL) {
369 // dynamically registered FDEs don't have a mach_header group they are in.
370 // Use fde as mh_group
371 unw_word_t mh_group = fdeInfo.fdeStart;
372 DwarfFDECache<LocalAddressSpace>::add(mh: (LocalAddressSpace::pint_t)mh_group,
373 ip_start: fdeInfo.pcStart, ip_end: fdeInfo.pcEnd,
374 fde: fdeInfo.fdeStart);
375 } else {
376 _LIBUNWIND_DEBUG_LOG("__unw_add_dynamic_fde: bad fde: %s", message);
377 }
378}
379
380/// IPI: for __deregister_frame()
381void __unw_remove_dynamic_fde(unw_word_t fde) {
382 // fde is own mh_group
383 DwarfFDECache<LocalAddressSpace>::removeAllIn(mh: (LocalAddressSpace::pint_t)fde);
384}
385
386void __unw_add_dynamic_eh_frame_section(unw_word_t eh_frame_start) {
387 // The eh_frame section start serves as the mh_group
388 unw_word_t mh_group = eh_frame_start;
389 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
390 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
391 auto p = (LocalAddressSpace::pint_t)eh_frame_start;
392 while (LocalAddressSpace::sThisAddressSpace.get32(addr: p)) {
393 if (CFI_Parser<LocalAddressSpace>::decodeFDE(
394 addressSpace&: LocalAddressSpace::sThisAddressSpace, fdeStart: p, fdeInfo: &fdeInfo, cieInfo: &cieInfo,
395 useCIEInfo: true) == NULL) {
396 DwarfFDECache<LocalAddressSpace>::add(mh: (LocalAddressSpace::pint_t)mh_group,
397 ip_start: fdeInfo.pcStart, ip_end: fdeInfo.pcEnd,
398 fde: fdeInfo.fdeStart);
399 p += fdeInfo.fdeLength;
400 } else if (CFI_Parser<LocalAddressSpace>::parseCIE(
401 addressSpace&: LocalAddressSpace::sThisAddressSpace, cie: p, cieInfo: &cieInfo) == NULL) {
402 p += cieInfo.cieLength;
403 } else
404 return;
405 }
406}
407
408void __unw_remove_dynamic_eh_frame_section(unw_word_t eh_frame_start) {
409 // The eh_frame section start serves as the mh_group
410 DwarfFDECache<LocalAddressSpace>::removeAllIn(
411 mh: (LocalAddressSpace::pint_t)eh_frame_start);
412}
413
414#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
415
416/// Maps the UNW_* error code to a textual representation
417_LIBUNWIND_HIDDEN const char *__unw_strerror(int error_code) {
418 switch (error_code) {
419 case UNW_ESUCCESS:
420 return "no error";
421 case UNW_EUNSPEC:
422 return "unspecified (general) error";
423 case UNW_ENOMEM:
424 return "out of memory";
425 case UNW_EBADREG:
426 return "bad register number";
427 case UNW_EREADONLYREG:
428 return "attempt to write read-only register";
429 case UNW_ESTOPUNWIND:
430 return "stop unwinding";
431 case UNW_EINVALIDIP:
432 return "invalid IP";
433 case UNW_EBADFRAME:
434 return "bad frame";
435 case UNW_EINVAL:
436 return "unsupported operation or bad value";
437 case UNW_EBADVERSION:
438 return "unwind info has unsupported version";
439 case UNW_ENOINFO:
440 return "no unwind info found";
441#if defined(_LIBUNWIND_TARGET_AARCH64) && !defined(_LIBUNWIND_IS_NATIVE_ONLY)
442 case UNW_ECROSSRASIGNING:
443 return "cross unwind with return address signing";
444#endif
445 }
446 return "invalid error code";
447}
448_LIBUNWIND_WEAK_ALIAS(__unw_strerror, unw_strerror)
449
450#endif // !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
451
452#ifdef __APPLE__
453
454namespace libunwind {
455
456static constexpr size_t MAX_DYNAMIC_UNWIND_SECTIONS_FINDERS = 8;
457
458static RWMutex findDynamicUnwindSectionsLock;
459static size_t numDynamicUnwindSectionsFinders = 0;
460static unw_find_dynamic_unwind_sections
461 dynamicUnwindSectionsFinders[MAX_DYNAMIC_UNWIND_SECTIONS_FINDERS] = {0};
462
463bool findDynamicUnwindSections(void *addr, unw_dynamic_unwind_sections *info) {
464 bool found = false;
465 findDynamicUnwindSectionsLock.lock_shared();
466 for (size_t i = 0; i != numDynamicUnwindSectionsFinders; ++i) {
467 if (dynamicUnwindSectionsFinders[i]((unw_word_t)addr, info)) {
468 found = true;
469 break;
470 }
471 }
472 findDynamicUnwindSectionsLock.unlock_shared();
473 return found;
474}
475
476} // namespace libunwind
477
478int __unw_add_find_dynamic_unwind_sections(
479 unw_find_dynamic_unwind_sections find_dynamic_unwind_sections) {
480 findDynamicUnwindSectionsLock.lock();
481
482 // Check that we have enough space...
483 if (numDynamicUnwindSectionsFinders == MAX_DYNAMIC_UNWIND_SECTIONS_FINDERS) {
484 findDynamicUnwindSectionsLock.unlock();
485 return UNW_ENOMEM;
486 }
487
488 // Check for value already present...
489 for (size_t i = 0; i != numDynamicUnwindSectionsFinders; ++i) {
490 if (dynamicUnwindSectionsFinders[i] == find_dynamic_unwind_sections) {
491 findDynamicUnwindSectionsLock.unlock();
492 return UNW_EINVAL;
493 }
494 }
495
496 // Success -- add callback entry.
497 dynamicUnwindSectionsFinders[numDynamicUnwindSectionsFinders++] =
498 find_dynamic_unwind_sections;
499 findDynamicUnwindSectionsLock.unlock();
500
501 return UNW_ESUCCESS;
502}
503
504int __unw_remove_find_dynamic_unwind_sections(
505 unw_find_dynamic_unwind_sections find_dynamic_unwind_sections) {
506 findDynamicUnwindSectionsLock.lock();
507
508 // Find index to remove.
509 size_t finderIdx = numDynamicUnwindSectionsFinders;
510 for (size_t i = 0; i != numDynamicUnwindSectionsFinders; ++i) {
511 if (dynamicUnwindSectionsFinders[i] == find_dynamic_unwind_sections) {
512 finderIdx = i;
513 break;
514 }
515 }
516
517 // If no such registration is present then error out.
518 if (finderIdx == numDynamicUnwindSectionsFinders) {
519 findDynamicUnwindSectionsLock.unlock();
520 return UNW_EINVAL;
521 }
522
523 // Remove entry.
524 for (size_t i = finderIdx; i != numDynamicUnwindSectionsFinders - 1; ++i)
525 dynamicUnwindSectionsFinders[i] = dynamicUnwindSectionsFinders[i + 1];
526 dynamicUnwindSectionsFinders[--numDynamicUnwindSectionsFinders] = nullptr;
527
528 findDynamicUnwindSectionsLock.unlock();
529 return UNW_ESUCCESS;
530}
531
532#endif // __APPLE__
533
534// Add logging hooks in Debug builds only
535#ifndef NDEBUG
536#include <stdlib.h>
537
538_LIBUNWIND_HIDDEN
539bool logAPIs() {
540 // do manual lock to avoid use of _cxa_guard_acquire or initializers
541 static bool checked = false;
542 static bool log = false;
543 if (!checked) {
544 log = (getenv(name: "LIBUNWIND_PRINT_APIS") != NULL);
545 checked = true;
546 }
547 return log;
548}
549
550_LIBUNWIND_HIDDEN
551bool logUnwinding() {
552 // do manual lock to avoid use of _cxa_guard_acquire or initializers
553 static bool checked = false;
554 static bool log = false;
555 if (!checked) {
556 log = (getenv(name: "LIBUNWIND_PRINT_UNWINDING") != NULL);
557 checked = true;
558 }
559 return log;
560}
561
562_LIBUNWIND_HIDDEN
563bool logDWARF() {
564 // do manual lock to avoid use of _cxa_guard_acquire or initializers
565 static bool checked = false;
566 static bool log = false;
567 if (!checked) {
568 log = (getenv(name: "LIBUNWIND_PRINT_DWARF") != NULL);
569 checked = true;
570 }
571 return log;
572}
573
574#endif // NDEBUG
575
576