Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CATCH_TRAP doesn't work well on ARM Macs #2422

Closed
stefanhaller opened this issue May 12, 2022 · 3 comments
Closed

CATCH_TRAP doesn't work well on ARM Macs #2422

stefanhaller opened this issue May 12, 2022 · 3 comments
Labels

Comments

@stefanhaller
Copy link

stefanhaller commented May 12, 2022

Describe the bug
On ARM Macs ("Apple Silicon"), CATCH_TRAP does work correctly in that it does stop in the debugger. However, you can't single-step from there, or even continue; you are stuck at the same source line forever.

This can be fixed by using the instruction d43e0000 instead of the d4200000 that is used now. This instruction is generated by the __builtin_debugtrap() function, which is also available on i386 and x86_64 and generates the int $3 instruction there. So a simple fix looks like this (against the devel branch):

diff --git a/src/catch2/internal/catch_debugger.hpp b/src/catch2/internal/catch_debugger.hpp
index c300da72..e3b0a24b 100644
--- a/src/catch2/internal/catch_debugger.hpp
+++ b/src/catch2/internal/catch_debugger.hpp
@@ -16,11 +16,7 @@ namespace Catch {
 
 #ifdef CATCH_PLATFORM_MAC
 
-    #if defined(__i386__) || defined(__x86_64__)
-        #define CATCH_TRAP() __asm__("int $3\n" : : ) /* NOLINT */
-    #elif defined(__aarch64__)
-        #define CATCH_TRAP()  __asm__(".inst 0xd4200000")
-    #endif
+    #define CATCH_TRAP() __builtin_debugtrap()
 
 #elif defined(CATCH_PLATFORM_IPHONE)
 

Platform information:

  • OS: macOS 12.3.1
  • Compiler+version: Apple clang version 13.0.0 (clang-1300.0.29.30)
  • Catch version: v2.13.9 and v3.0.0-preview5
@horenmar
Copy link
Member

Is this universally supported? Quick look around the internet suggests that this is supported by some (most?) versions of Clang, but not e.g. GCC.

@stefanhaller
Copy link
Author

You're right, it's clang-specific. I was fooled by this claim that it works universally, without reading the comments further down that debunk it. Sorry.

In that case, the fix would be just to replace 0xd4200000 with 0xd43e0000 in the existing code. (It would of course be possible to check __has_builtin(__builtin_debugtrap) and use it if available, and fall back to assembly if not, but it doesn't seem worth it.)

@horenmar
Copy link
Member

Is this also an issue for the iphone configuration? It also uses 0xd4200000 for dbg break.

horenmar added a commit that referenced this issue May 15, 2022
The old instruction would cause the debugger to be stuck at the
triggering source line forever, while the new one should have the
expected semantics, where the debugger can then single-step,
continue. or generally do things, afterwards.

Closes #2422
@horenmar horenmar added the Bug label May 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants