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