1 //===----------------------------- config.h -------------------------------===//
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 //  Defines macros used within libunwind project.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 
14 #ifndef LIBUNWIND_CONFIG_H
15 #define LIBUNWIND_CONFIG_H
16 
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 
22 // Define static_assert() unless already defined by compiler.
23 #ifndef __has_feature
24   #define __has_feature(__x) 0
25 #endif
26 #if !(__has_feature(cxx_static_assert)) && !defined(static_assert)
27   #define static_assert(__b, __m) \
28       extern int compile_time_assert_failed[ ( __b ) ? 1 : -1 ]  \
29                                                   __attribute__( ( unused ) );
30 #endif
31 
32 // Platform specific configuration defines.
33 #ifdef __APPLE__
34   #if defined(FOR_DYLD)
35     #define _LIBUNWIND_SUPPORT_COMPACT_UNWIND 1
36     #define _LIBUNWIND_SUPPORT_DWARF_UNWIND   0
37     #define _LIBUNWIND_SUPPORT_DWARF_INDEX    0
38   #else
39     #define _LIBUNWIND_SUPPORT_COMPACT_UNWIND 1
40     #define _LIBUNWIND_SUPPORT_DWARF_UNWIND   1
41     #define _LIBUNWIND_SUPPORT_DWARF_INDEX    0
42   #endif
43 #else
44   #if defined(__ARM_DWARF_EH__) || !defined(__arm__)
45     #define _LIBUNWIND_SUPPORT_COMPACT_UNWIND 0
46     #define _LIBUNWIND_SUPPORT_DWARF_UNWIND 1
47     #define _LIBUNWIND_SUPPORT_DWARF_INDEX 1
48   #else
49     #define _LIBUNWIND_SUPPORT_COMPACT_UNWIND 0
50     #define _LIBUNWIND_SUPPORT_DWARF_UNWIND 0
51     #define _LIBUNWIND_SUPPORT_DWARF_INDEX 0
52   #endif
53 #endif
54 
55 // FIXME: these macros are not correct for COFF targets
56 #define _LIBUNWIND_EXPORT __attribute__((visibility("default")))
57 #define _LIBUNWIND_HIDDEN __attribute__((visibility("hidden")))
58 
59 #if (defined(__APPLE__) && defined(__arm__)) || defined(__USING_SJLJ_EXCEPTIONS__)
60 #define _LIBUNWIND_BUILD_SJLJ_APIS 1
61 #else
62 #define _LIBUNWIND_BUILD_SJLJ_APIS 0
63 #endif
64 
65 #if defined(__i386__) || defined(__x86_64__) || defined(__ppc__) || defined(__ppc64__)
66 #define _LIBUNWIND_SUPPORT_FRAME_APIS 1
67 #else
68 #define _LIBUNWIND_SUPPORT_FRAME_APIS 0
69 #endif
70 
71 #if defined(__i386__) || defined(__x86_64__) ||                                \
72     defined(__ppc__) || defined(__ppc64__) ||                                  \
73     (!defined(__APPLE__) && defined(__arm__)) ||                               \
74     (defined(__arm64__) || defined(__aarch64__)) ||                            \
75     (defined(__APPLE__) && defined(__mips__))
76 #define _LIBUNWIND_BUILD_ZERO_COST_APIS 1
77 #else
78 #define _LIBUNWIND_BUILD_ZERO_COST_APIS 0
79 #endif
80 
81 #define _LIBUNWIND_ABORT(msg)                                                  \
82   do {                                                                         \
83     fprintf(stderr, "libunwind: %s %s:%d - %s\n", __func__, __FILE__,          \
84             __LINE__, msg);                                                    \
85     fflush(stderr);                                                            \
86     abort();                                                                   \
87   } while (0)
88 #define _LIBUNWIND_LOG(msg, ...) fprintf(stderr, "libunwind: " msg "\n", __VA_ARGS__)
89 
90 #if defined(_LIBUNWIND_HAS_NO_THREADS)
91   // only used with pthread calls, not needed for the single-threaded builds
92   #define _LIBUNWIND_LOG_NON_ZERO(x)
93 #endif
94 
95 // Macros that define away in non-Debug builds
96 #ifdef NDEBUG
97   #define _LIBUNWIND_DEBUG_LOG(msg, ...)
98   #define _LIBUNWIND_TRACE_API(msg, ...)
99   #define _LIBUNWIND_TRACING_UNWINDING 0
100   #define _LIBUNWIND_TRACE_UNWINDING(msg, ...)
101   #ifndef _LIBUNWIND_LOG_NON_ZERO
102     #define _LIBUNWIND_LOG_NON_ZERO(x) x
103   #endif
104 #else
105   #ifdef __cplusplus
106     extern "C" {
107   #endif
108     extern  bool logAPIs();
109     extern  bool logUnwinding();
110   #ifdef __cplusplus
111     }
112   #endif
113   #define _LIBUNWIND_DEBUG_LOG(msg, ...)  _LIBUNWIND_LOG(msg, __VA_ARGS__)
114   #ifndef _LIBUNWIND_LOG_NON_ZERO
115     #define _LIBUNWIND_LOG_NON_ZERO(x) \
116               do { \
117                 int _err = x; \
118                 if ( _err != 0 ) \
119                   _LIBUNWIND_LOG("" #x "=%d in %s", _err, __FUNCTION__); \
120                } while (0)
121   #endif
122   #define _LIBUNWIND_TRACE_API(msg, ...) \
123             do { \
124               if ( logAPIs() ) _LIBUNWIND_LOG(msg, __VA_ARGS__); \
125             } while(0)
126   #define _LIBUNWIND_TRACE_UNWINDING(msg, ...) \
127             do { \
128               if ( logUnwinding() ) _LIBUNWIND_LOG(msg, __VA_ARGS__); \
129             } while(0)
130   #define _LIBUNWIND_TRACING_UNWINDING logUnwinding()
131 #endif
132 
133 #ifdef __cplusplus
134 // Used to fit UnwindCursor and Registers_xxx types against unw_context_t /
135 // unw_cursor_t sized memory blocks.
136 #if defined(_LIBUNWIND_IS_NATIVE_ONLY)
137 # define COMP_OP ==
138 #else
139 # define COMP_OP <
140 #endif
141 template <typename _Type, typename _Mem>
142 struct check_fit {
143   template <typename T>
144   struct blk_count {
145     static const size_t count =
146       (sizeof(T) + sizeof(uint64_t) - 1) / sizeof(uint64_t);
147   };
148   static const bool does_fit =
149     (blk_count<_Type>::count COMP_OP blk_count<_Mem>::count);
150 };
151 #undef COMP_OP
152 #endif // __cplusplus
153 
154 #endif // LIBUNWIND_CONFIG_H
155