On macOS si_code is always set to TRAP_BRKPT.
From: Piotr Caban piotr@codeweavers.com
On macOS si_code is always set to TRAP_BRKPT. --- dlls/ntdll/unix/signal_x86_64.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/dlls/ntdll/unix/signal_x86_64.c b/dlls/ntdll/unix/signal_x86_64.c index 6c87e347eac..b54a3ca88f8 100644 --- a/dlls/ntdll/unix/signal_x86_64.c +++ b/dlls/ntdll/unix/signal_x86_64.c @@ -2687,13 +2687,31 @@ static void trap_handler( int signal, siginfo_t *siginfo, void *sigcontext ) EXCEPTION_RECORD rec = { 0 }; struct xcontext context; ucontext_t *ucontext = sigcontext; + int si_code;
if (handle_syscall_trap( sigcontext )) return;
rec.ExceptionAddress = (void *)RIP_sig(ucontext); save_context( &context, sigcontext );
- switch (siginfo->si_code) +#if defined(__APPLE__) + switch (TRAP_sig(ucontext)) + { + case TRAP_x86_TRCTRAP: + si_code = TRAP_TRACE; + break; + case TRAP_x86_BPTFLT: + si_code = TRAP_BRKPT; + break; + default: + si_code = siginfo->si_code; + break; + } +#else + si_code = siginfo->si_code; +#endif + + switch (si_code) { case TRAP_TRACE: /* Single-step exception */ case 4 /* TRAP_HWBKPT */: /* Hardware breakpoint exception */
Jinoh Kang (@iamahuman) commented about dlls/ntdll/unix/signal_x86_64.c:
EXCEPTION_RECORD rec = { 0 }; struct xcontext context; ucontext_t *ucontext = sigcontext;
int si_code;
if (handle_syscall_trap( sigcontext )) return;
rec.ExceptionAddress = (void *)RIP_sig(ucontext); save_context( &context, sigcontext );
- switch (siginfo->si_code)
+#if defined(__APPLE__)
- switch (TRAP_sig(ucontext))
I noticed that we already rely on `TRAP_SIG(ucontext)` to determine the correct exception in `segv_handler`.
If we can trust `TRAP_SIG(ucontext)` over `siginfo->si_code` for `SIGTRAP` too in all platforms, perhaps it makes sense to make this switch unconditional.
On Sat Jul 9 06:52:04 2022 +0000, Jinoh Kang wrote:
I noticed that we already rely on `TRAP_SIG(ucontext)` to determine the correct exception in `segv_handler`. If we can trust `TRAP_SIG(ucontext)` over `siginfo->si_code` for `SIGTRAP` too in all platforms, perhaps it makes sense to make this switch unconditional.
I haven't done it this way because I was unsure of what happens in case of single step and hardware breakpoint on the same instruction. I turns out it doesn't need special handling in x86_64 case. I've also done some testing with icebp instruction, after changing the code it no longer needs special handling. I will push changed version.