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__)
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(__APPLE__) && defined(__arm__)) ||                               \
73     (defined(__arm64__) || defined(__aarch64__)) ||                            \
74     (defined(__APPLE__) && defined(__mips__))
75 #define _LIBUNWIND_BUILD_ZERO_COST_APIS 1
76 #else
77 #define _LIBUNWIND_BUILD_ZERO_COST_APIS 0
78 #endif
79 
80 #define _LIBUNWIND_ABORT(msg)                                                  \
81   do {                                                                         \
82     fprintf(stderr, "libunwind: %s %s:%d - %s\n", __func__, __FILE__,          \
83             __LINE__, msg);                                                    \
84     fflush(stderr);                                                            \
85     abort();                                                                   \
86   } while (0)
87 #define _LIBUNWIND_LOG(msg, ...) fprintf(stderr, "libunwind: " msg "\n", __VA_ARGS__)
88 
89 #if defined(_LIBUNWIND_HAS_NO_THREADS)
90   // only used with pthread calls, not needed for the single-threaded builds
91   #define _LIBUNWIND_LOG_NON_ZERO(x)
92 #endif
93 
94 // Macros that define away in non-Debug builds
95 #ifdef NDEBUG
96   #define _LIBUNWIND_DEBUG_LOG(msg, ...)
97   #define _LIBUNWIND_TRACE_API(msg, ...)
98   #define _LIBUNWIND_TRACING_UNWINDING 0
99   #define _LIBUNWIND_TRACE_UNWINDING(msg, ...)
100   #ifndef _LIBUNWIND_LOG_NON_ZERO
101     #define _LIBUNWIND_LOG_NON_ZERO(x) x
102   #endif
103 #else
104   #ifdef __cplusplus
105     extern "C" {
106   #endif
107     extern  bool logAPIs();
108     extern  bool logUnwinding();
109   #ifdef __cplusplus
110     }
111   #endif
112   #define _LIBUNWIND_DEBUG_LOG(msg, ...)  _LIBUNWIND_LOG(msg, __VA_ARGS__)
113   #ifndef _LIBUNWIND_LOG_NON_ZERO
114     #define _LIBUNWIND_LOG_NON_ZERO(x) \
115               do { \
116                 int _err = x; \
117                 if ( _err != 0 ) \
118                   _LIBUNWIND_LOG("" #x "=%d in %s", _err, __FUNCTION__); \
119                } while (0)
120   #endif
121   #define _LIBUNWIND_TRACE_API(msg, ...) \
122             do { \
123               if ( logAPIs() ) _LIBUNWIND_LOG(msg, __VA_ARGS__); \
124             } while(0)
125   #define _LIBUNWIND_TRACE_UNWINDING(msg, ...) \
126             do { \
127               if ( logUnwinding() ) _LIBUNWIND_LOG(msg, __VA_ARGS__); \
128             } while(0)
129   #define _LIBUNWIND_TRACING_UNWINDING logUnwinding()
130 #endif
131 
132 #ifdef __cplusplus
133 // Used to fit UnwindCursor and Registers_xxx types against unw_context_t /
134 // unw_cursor_t sized memory blocks.
135 #if defined(_LIBUNWIND_IS_NATIVE_ONLY)
136 # define COMP_OP ==
137 #else
138 # define COMP_OP <
139 #endif
140 template <typename _Type, typename _Mem>
141 struct check_fit {
142   template <typename T>
143   struct blk_count {
144     static const size_t count =
145       (sizeof(T) + sizeof(uint64_t) - 1) / sizeof(uint64_t);
146   };
147   static const bool does_fit =
148     (blk_count<_Type>::count COMP_OP blk_count<_Mem>::count);
149 };
150 #undef COMP_OP
151 #endif // __cplusplus
152 
153 #endif // LIBUNWIND_CONFIG_H
154