1 // This header is included in all the test programs (C and C++) and provides a
2 // hook for dealing with platform-specifics.
3 #if defined(_WIN32) || defined(_WIN64)
4 #ifdef COMPILING_LLDB_TEST_DLL
5 #define LLDB_TEST_API __declspec(dllexport)
6 #else
7 #define LLDB_TEST_API __declspec(dllimport)
8 #endif
9 #else
10 #define LLDB_TEST_API
11 #endif
12 
13 #if defined(__cplusplus) && defined(_MSC_VER) && (_HAS_EXCEPTIONS == 0)
14 // Compiling MSVC libraries with _HAS_EXCEPTIONS=0, eliminates most but not all
15 // calls to __uncaught_exception.  Unfortunately, it does seem to eliminate
16 // the delcaration of __uncaught_excpeiton.  Including <eh.h> ensures that it is
17 // declared.  This may not be necessary after MSVC 12.
18 #include <eh.h>
19 #endif
20 
21 
22 // On some systems (e.g., some versions of linux) it is not possible to attach to a process
23 // without it giving us special permissions. This defines the lldb_enable_attach macro, which
24 // should perform any such actions, if needed by the platform. This is a macro instead of a
25 // function to avoid the need for complex linking of the test programs.
26 #if defined(__linux__)
27 #include <sys/prctl.h>
28 
29 #if defined(PR_SET_PTRACER) && defined(PR_SET_PTRACER_ANY)
30 // For now we execute on best effort basis.  If this fails for some reason, so be it.
31 #define lldb_enable_attach()                                                          \
32     do                                                                                \
33     {                                                                                 \
34         const int prctl_result = prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0);  \
35         (void)prctl_result;                                                           \
36     } while (0)
37 
38 #endif
39 
40 #else // not linux
41 
42 #define lldb_enable_attach()
43 
44 #endif
45 
46 #if defined(__APPLE__) && defined(LLDB_USING_LIBSTDCPP)
47 
48 // on Darwin, libstdc++ is missing <atomic>, so this would cause any test to fail building
49 // since this header file is being included in every C-family test case, we need to not include it
50 // on Darwin, most tests use libc++ by default, so this will only affect tests that explicitly require libstdc++
51 
52 #else
53 #ifdef __cplusplus
54 #include <atomic>
55 
56 // Note that although hogging the CPU while waiting for a variable to change
57 // would be terrible in production code, it's great for testing since it
58 // avoids a lot of messy context switching to get multiple threads synchronized.
59 
60 typedef std::atomic<int> pseudo_barrier_t;
61 #define pseudo_barrier_wait(barrier)        \
62     do                                      \
63     {                                       \
64         --(barrier);                        \
65         while ((barrier).load() > 0)        \
66             ;                               \
67     } while (0)
68 
69 #define pseudo_barrier_init(barrier, count) \
70     do                                      \
71     {                                       \
72         (barrier) = (count);                \
73     } while (0)
74 #endif // __cplusplus
75 #endif // defined(__APPLE__) && defined(LLDB_USING_LIBSTDCPP)
76