From 0be7dab9dc533114cb35021acc4529d8c3bec877 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Tue, 4 Jan 2022 13:29:26 +0000 Subject: [PATCH 1/2] Define macro for debugtrap Also fix typos in macro names. --- debugbreak.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/debugbreak.h b/debugbreak.h index bfb8288..e55aceb 100644 --- a/debugbreak.h +++ b/debugbreak.h @@ -37,8 +37,9 @@ extern "C" { #endif #define DEBUG_BREAK_USE_TRAP_INSTRUCTION 1 -#define DEBUG_BREAK_USE_BULTIN_TRAP 2 +#define DEBUG_BREAK_USE_BUILTIN_TRAP 2 #define DEBUG_BREAK_USE_SIGTRAP 3 +#define DEBUG_BREAK_USE_BUILTIN_DEBUGTRAP 4 #if defined(__i386__) || defined(__x86_64__) #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION @@ -91,7 +92,7 @@ __inline__ static void trap_instruction(void) * Same problem and workaround as Thumb mode */ } #elif defined(__aarch64__) && defined(__APPLE__) - #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_BULTIN_DEBUGTRAP + #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_BUILTIN_DEBUGTRAP #elif defined(__aarch64__) #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION __attribute__((always_inline)) @@ -142,13 +143,13 @@ __inline__ static void debug_break(void) { trap_instruction(); } -#elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BULTIN_DEBUGTRAP +#elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BUILTIN_DEBUGTRAP __attribute__((always_inline)) __inline__ static void debug_break(void) { __builtin_debugtrap(); } -#elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BULTIN_TRAP +#elif DEBUG_BREAK_IMPL == DEBUG_BREAK_USE_BUILTIN_TRAP __attribute__((always_inline)) __inline__ static void debug_break(void) { From 83bf7e933311b88613cbaadeced9c2e2c811054a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20V=C3=A1rady?= Date: Tue, 1 Feb 2022 23:50:41 +0100 Subject: [PATCH 2/2] Use __builtin_debugtrap wherever available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also fixes a build issue on macOS (Apple silicon) when a real GCC compiler is used: > warning: implicit declaration of function '__builtin_debugtrap'; Signed-off-by: Jonathan Wakely Signed-off-by: László Várady --- README.md | 4 ++-- debugbreak.h | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 965092c..d0e7516 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,8 @@ See table below for the behavior of **debug_break()** on other architecturs. Behavior on Different Architectures ---------------- +`__builtin_debugtrap()` is used wherever available, otherwise: + | Architecture | debug_break() | | ------------- | ------------- | | x86/x86-64 | `int3` | @@ -122,6 +124,4 @@ Behavior on Different Architectures | POWER | `.4byte 0x7d821008` | | RISC-V | `.4byte 0x00100073` | | MSVC compiler | `__debugbreak` | -| Apple compiler on AArch64 | `__builtin_trap()` | | Otherwise | `raise(SIGTRAP)` | - diff --git a/debugbreak.h b/debugbreak.h index e55aceb..36f3b79 100644 --- a/debugbreak.h +++ b/debugbreak.h @@ -91,8 +91,6 @@ __inline__ static void trap_instruction(void) /* Known problem: * Same problem and workaround as Thumb mode */ } -#elif defined(__aarch64__) && defined(__APPLE__) - #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_BUILTIN_DEBUGTRAP #elif defined(__aarch64__) #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_TRAP_INSTRUCTION __attribute__((always_inline)) @@ -134,6 +132,12 @@ __inline__ static void trap_instruction(void) #define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_SIGTRAP #endif +#if defined(__has_builtin) +# if __has_builtin(__builtin_debugtrap) +# undef DEBUG_BREAK_IMPL +# define DEBUG_BREAK_IMPL DEBUG_BREAK_USE_BUILTIN_DEBUGTRAP +# endif +#endif #ifndef DEBUG_BREAK_IMPL #error "debugbreak.h is not supported on this target"