Skip to content

Commit 2d6f4d2

Browse files
authored
[compiler-rt] DumpAllRegisters implementation for netbsd i386/x86_64. (#99743)
1 parent 0e6f64c commit 2d6f4d2

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,8 +2121,26 @@ bool SignalContext::IsTrueFaultingAddress() const {
21212121
UNUSED
21222122
static const char *RegNumToRegName(int reg) {
21232123
switch (reg) {
2124-
# if SANITIZER_LINUX && SANITIZER_GLIBC
2124+
# if SANITIZER_LINUX && SANITIZER_GLIBC || SANITIZER_NETBSD
21252125
# if defined(__x86_64__)
2126+
# if SANITIZER_NETBSD
2127+
# define REG_RAX _REG_RAX
2128+
# define REG_RBX _REG_RBX
2129+
# define REG_RCX _REG_RCX
2130+
# define REG_RDX _REG_RDX
2131+
# define REG_RDI _REG_RDI
2132+
# define REG_RSI _REG_RSI
2133+
# define REG_RBP _REG_RBP
2134+
# define REG_RSP _REG_RSP
2135+
# define REG_R8 _REG_R8
2136+
# define REG_R9 _REG_R9
2137+
# define REG_R10 _REG_R10
2138+
# define REG_R11 _REG_R11
2139+
# define REG_R12 _REG_R12
2140+
# define REG_R13 _REG_R13
2141+
# define REG_R14 _REG_R14
2142+
# define REG_R15 _REG_R15
2143+
# endif
21262144
case REG_RAX:
21272145
return "rax";
21282146
case REG_RBX:
@@ -2156,6 +2174,16 @@ static const char *RegNumToRegName(int reg) {
21562174
case REG_R15:
21572175
return "r15";
21582176
# elif defined(__i386__)
2177+
# if SANITIZER_NETBSD
2178+
# define REG_EAX _REG_EAX
2179+
# define REG_EBX _REG_EBX
2180+
# define REG_ECX _REG_ECX
2181+
# define REG_EDX _REG_EDX
2182+
# define REG_EDI _REG_EDI
2183+
# define REG_ESI _REG_ESI
2184+
# define REG_EBP _REG_EBP
2185+
# define REG_ESP _REG_ESP
2186+
# endif
21592187
case REG_EAX:
21602188
return "eax";
21612189
case REG_EBX:
@@ -2296,11 +2324,23 @@ static uptr GetArmRegister(ucontext_t *ctx, int RegNum) {
22962324
UNUSED
22972325
static void DumpSingleReg(ucontext_t *ctx, int RegNum) {
22982326
const char *RegName = RegNumToRegName(RegNum);
2299-
# if defined(__x86_64__)
2327+
# if defined(__x86_64__)
23002328
Printf("%s%s = 0x%016llx ", internal_strlen(RegName) == 2 ? " " : "",
2301-
RegName, ctx->uc_mcontext.gregs[RegNum]);
2302-
# elif defined(__i386__)
2303-
Printf("%s = 0x%08x ", RegName, ctx->uc_mcontext.gregs[RegNum]);
2329+
RegName,
2330+
# if SANITIZER_LINUX
2331+
ctx->uc_mcontext.gregs[RegNum]
2332+
# elif SANITIZER_NETBSD
2333+
ctx->uc_mcontext.__gregs[RegNum]
2334+
# endif
2335+
);
2336+
# elif defined(__i386__)
2337+
Printf("%s = 0x%08x ", RegName,
2338+
# if SANITIZER_LINUX
2339+
ctx->uc_mcontext.gregs[RegNum]
2340+
# elif SANITIZER_NETBSD
2341+
ctx->uc_mcontext.__gregs[RegNum]
2342+
# endif
2343+
);
23042344
# elif defined(__arm__)
23052345
Printf("%s%s = 0x%08zx ", internal_strlen(RegName) == 2 ? " " : "", RegName,
23062346
GetArmRegister(ctx, RegNum));
@@ -2314,7 +2354,7 @@ static void DumpSingleReg(ucontext_t *ctx, int RegNum) {
23142354

23152355
void SignalContext::DumpAllRegisters(void *context) {
23162356
ucontext_t *ucontext = (ucontext_t *)context;
2317-
# if SANITIZER_LINUX && SANITIZER_GLIBC
2357+
# if SANITIZER_LINUX && SANITIZER_GLIBC || SANITIZER_NETBSD
23182358
# if defined(__x86_64__)
23192359
Report("Register values:\n");
23202360
DumpSingleReg(ucontext, REG_RAX);
@@ -2353,7 +2393,7 @@ void SignalContext::DumpAllRegisters(void *context) {
23532393
DumpSingleReg(ucontext, REG_EBP);
23542394
DumpSingleReg(ucontext, REG_ESP);
23552395
Printf("\n");
2356-
# elif defined(__arm__)
2396+
# elif defined(__arm__) && !SANITIZER_NETBSD
23572397
Report("Register values:\n");
23582398
DumpSingleReg(ucontext, REG_R0);
23592399
DumpSingleReg(ucontext, REG_R1);
@@ -2375,7 +2415,7 @@ void SignalContext::DumpAllRegisters(void *context) {
23752415
DumpSingleReg(ucontext, REG_R14);
23762416
DumpSingleReg(ucontext, REG_R15);
23772417
Printf("\n");
2378-
# elif defined(__aarch64__)
2418+
# elif defined(__aarch64__) && !SANITIZER_NETBSD
23792419
Report("Register values:\n");
23802420
for (int i = 0; i <= 31; ++i) {
23812421
DumpSingleReg(ucontext, i);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Check that sanitizer prints registers dump_registers on dump_registers=1
2+
// RUN: %clangxx %s -o %t
3+
// RUN: %env_tool_opts=dump_registers=0 not %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK-NODUMP --strict-whitespace
4+
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK-DUMP --strict-whitespace
5+
//
6+
// REQUIRES: i386-target-arch
7+
8+
#include <signal.h>
9+
10+
int main() {
11+
raise(SIGSEGV);
12+
// CHECK-DUMP: Register values
13+
// CHECK-DUMP-NEXT: eax = {{0x[0-9a-f]+}} ebx = {{0x[0-9a-f]+}} ecx = {{0x[0-9a-f]+}} edx = {{0x[0-9a-f]+}}
14+
// CHECK-DUMP-NEXT: edi = {{0x[0-9a-f]+}} esi = {{0x[0-9a-f]+}} ebp = {{0x[0-9a-f]+}} esp = {{0x[0-9a-f]+}}
15+
// CHECK-NODUMP-NOT: Register values
16+
return 0;
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Check that sanitizer prints registers dump_registers on dump_registers=1
2+
// RUN: %clangxx %s -o %t
3+
// RUN: %env_tool_opts=dump_registers=0 not %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK-NODUMP --strict-whitespace
4+
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK-DUMP --strict-whitespace
5+
//
6+
// REQUIRES: x86_64-target-arch
7+
8+
#include <signal.h>
9+
10+
int main() {
11+
raise(SIGSEGV);
12+
// CHECK-DUMP: Register values
13+
// CHECK-DUMP-NEXT: rax = {{0x[0-9a-f]+}} rbx = {{0x[0-9a-f]+}} rcx = {{0x[0-9a-f]+}} rdx = {{0x[0-9a-f]+}}
14+
// CHECK-DUMP-NEXT: rdi = {{0x[0-9a-f]+}} rsi = {{0x[0-9a-f]+}} rbp = {{0x[0-9a-f]+}} rsp = {{0x[0-9a-f]+}}
15+
// CHECK-DUMP-NEXT: r8 = {{0x[0-9a-f]+}} r9 = {{0x[0-9a-f]+}} r10 = {{0x[0-9a-f]+}} r11 = {{0x[0-9a-f]+}}
16+
// CHECK-DUMP-NEXT: r12 = {{0x[0-9a-f]+}} r13 = {{0x[0-9a-f]+}} r14 = {{0x[0-9a-f]+}} r15 = {{0x[0-9a-f]+}}
17+
// CHECK-NODUMP-NOT: Register values
18+
return 0;
19+
}

0 commit comments

Comments
 (0)