31 #define debug_break __debugbreak
39 #define DEBUG_BREAK_USE_TRAP_INSTRUCTION 1
40 #define DEBUG_BREAK_USE_BULTIN_TRAP 2
41 #define DEBUG_BREAK_USE_SIGTRAP 3
43 #if defined(__i386__) || defined(__x86_64__)
44 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
45 __inline__
static void trap_instruction(
void) { __asm__
volatile(
"int $0x03"); }
46 #elif defined(__thumb__)
47 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
49 __attribute__((always_inline)) __inline__
static void trap_instruction(
void) {
54 __asm__
volatile(
".inst 0xde01");
57 __asm__
volatile(
".inst.w 0xf7f0a000");
77 #elif defined(__arm__) && !defined(__thumb__)
78 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
79 __attribute__((always_inline)) __inline__
static void trap_instruction(
void) {
82 __asm__
volatile(
".inst 0xe7f001f0");
86 #elif defined(__aarch64__) && defined(__APPLE__)
87 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_BULTIN_DEBUGTRAP
88 #elif defined(__aarch64__)
89 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
90 __attribute__((always_inline)) __inline__
static void trap_instruction(
void) {
93 __asm__
volatile(
".inst 0xd4200000");
95 #elif defined(__powerpc__)
97 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
98 __attribute__((always_inline)) __inline__
static void trap_instruction(
void) {
101 __asm__
volatile(
".4byte 0x7d821008");
110 #elif defined(__riscv)
113 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION
114 __attribute__((always_inline)) __inline__
static void trap_instruction(
void) {
117 __asm__
volatile(
".4byte 0x00100073");
120 #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_SIGTRAP
124 #ifndef DEBUG_BREAK_IMPL
125 #error "debugbreak.h is not supported on this target"
126 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_TRAP_INSTRUCTION
127 __attribute__((always_inline)) __inline__
static void debug_break(
void) { trap_instruction(); }
128 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BULTIN_DEBUGTRAP
129 __attribute__((always_inline)) __inline__
static void debug_break(
void) { __builtin_debugtrap(); }
130 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BULTIN_TRAP
131 __attribute__((always_inline)) __inline__
static void debug_break(
void) { __builtin_trap(); }
132 #elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_SIGTRAP
134 __attribute__((always_inline)) __inline__
static void debug_break(
void) {
raise(SIGTRAP); }
136 #error "invalid DEBUG_BREAK_IMPL value"
150 #ifdef IS_DEBUGGER_ACTIVE
151 __attribute__((always_inline)) __inline__
static bool isDebuggerActive() {
152 return IS_DEBUGGER_ACTIVE();
158 ErrnoGuard() : m_oldErrno(errno) {}
159 ~ErrnoGuard() { errno = m_oldErrno; }
166 __attribute__((always_inline)) __inline__
static bool isDebuggerActive() {
168 std::ifstream in(
"/proc/self/status");
169 for (std::string line; std::getline(in, line);) {
170 static const int PREFIX_LEN = 11;
171 if (line.compare(0, PREFIX_LEN,
"TracerPid:\t") == 0) {
172 return line.length() > PREFIX_LEN && line[PREFIX_LEN] !=
'0';
177 #elif defined(__APPLE__)
182 __attribute__((always_inline)) __inline__
static bool isDebuggerActive() {
188 info.kp_proc.p_flag = 0;
193 mib[2] = KERN_PROC_PID;
197 if (sysctl(mib, (
sizeof(mib) /
sizeof(*mib)), &info, &size, 0, 0) != 0) {
198 std::cerr <<
"\nCall to sysctl failed - unable to determine if debugger is active **\n";
202 return ((info.kp_proc.p_flag & P_TRACED) != 0);
204 #elif defined(__MINGW32__) || defined(__MINGW64__)
205 extern "C" __declspec(dllimport)
int __stdcall IsDebuggerPresent();
206 __attribute__((always_inline)) __inline__
static bool isDebuggerActive() {
207 return ::IsDebuggerPresent() != 0;
209 #elif defined(_MSC_VER)
210 extern "C" __declspec(dllimport)
int __stdcall IsDebuggerPresent();
211 inline __forceinline
static bool isDebuggerActive() {
212 return ::IsDebuggerPresent() != 0;
216 __attribute__((always_inline)) __inline__
static bool isDebuggerActive() {
return false; }
__attribute__((always_inline)) __inline__ static void debug_break(void)
Definition: debugbreak.h:134