Skip to content

Commit 7a9f4ff

Browse files
committed
runtime: fix compile failure from use of _Unwind_Backtrace() on FreeBSD
FreeBSD uses libunwind from <https://github.com/libunwind/libunwind>. Its <unwind.h> guards _Unwind_Backtrace with _GNU_SOURCE guards. Define _GNU_SOURCE so that withCurrentBacktraceImpl() can access _Unwind_Backtrace. Note that there are quite a few <unwind.h>s: 1. gcc provides one as part of its libgcc (FreeBSD does not use gcc and provides its own libgcc that does not have an <unwind.h>) 2. LLVM's libunwind provides one (FreeBSD does not use LLVM's libcxxabi or LLVM's libunwind) 3. <https://github.com/libunwind/libunwind> provides one (FreeBSD has this one) 4. clang provides one (FreeBSD *has* this one, but the header search path for C++ prefers swiftlang#3)
1 parent 4603c84 commit 7a9f4ff

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

stdlib/public/runtime/Errors.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@
2222
#include <mutex>
2323
#endif
2424

25+
#if defined(__ELF__)
26+
# if defined(__FreeBSD__) && !defined(_GNU_SOURCE)
27+
// In order to access _Unwind_Backtrace() from (the non-LLVM, non-GCC)
28+
// <unwind.h> in FreeBSD, define _GNU_SOURCE around the include. This is
29+
// similar to what clang's unwind.h does.
30+
// Do this before including other headers which may in turn include <unwind.h>,
31+
// which has a header guard.
32+
# define _GNU_SOURCE
33+
# define _SHOULD_UNDEFINE_GNU_SOURCE
34+
# endif
35+
36+
#include <unwind.h>
37+
38+
# ifdef _SHOULD_UNDEFINE_GNU_SOURCE
39+
# undef _GNU_SOURCE
40+
# undef _SHOULD_UNDEFINE_GNU_SOURCE
41+
# endif
42+
#endif
43+
2544
#include <inttypes.h>
2645
#include <stdint.h>
2746
#include <stdio.h>
@@ -56,10 +75,6 @@
5675
#include <android/log.h>
5776
#endif
5877

59-
#if defined(__ELF__)
60-
#include <unwind.h>
61-
#endif
62-
6378
#include <inttypes.h>
6479

6580
#ifdef SWIFT_HAVE_CRASHREPORTERCLIENT

0 commit comments

Comments
 (0)