Skip to content

Commit bda1893

Browse files
authored
[compiler-rt] Add DumpAllRegisters impl (#99049)
- Add implementation for x86_64 and linux - Add test The output is like ==XXYYZZ==Register values: rax = 0x... rbx = 0x... rcx = 0x... rdx = 0x... rdi = 0x... rsi = 0x... rbp = 0x... rsp = 0x... r8 = 0x... r9 = 0x... r10 = 0x... r11 = 0x... r12 = 0x... r13 = 0x... r14 = 0x... r15 = 0x...
1 parent 0684db3 commit bda1893

File tree

3 files changed

+151
-1
lines changed

3 files changed

+151
-1
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2118,8 +2118,122 @@ bool SignalContext::IsTrueFaultingAddress() const {
21182118
return si->si_signo == SIGSEGV && si->si_code != 128;
21192119
}
21202120

2121+
UNUSED
2122+
static const char *RegNumToRegName(int reg) {
2123+
switch (reg) {
2124+
# if defined(__x86_64__)
2125+
case REG_RAX:
2126+
return "rax";
2127+
case REG_RBX:
2128+
return "rbx";
2129+
case REG_RCX:
2130+
return "rcx";
2131+
case REG_RDX:
2132+
return "rdx";
2133+
case REG_RDI:
2134+
return "rdi";
2135+
case REG_RSI:
2136+
return "rsi";
2137+
case REG_RBP:
2138+
return "rbp";
2139+
case REG_RSP:
2140+
return "rsp";
2141+
case REG_R8:
2142+
return "r8";
2143+
case REG_R9:
2144+
return "r9";
2145+
case REG_R10:
2146+
return "r10";
2147+
case REG_R11:
2148+
return "r11";
2149+
case REG_R12:
2150+
return "r12";
2151+
case REG_R13:
2152+
return "r13";
2153+
case REG_R14:
2154+
return "r14";
2155+
case REG_R15:
2156+
return "r15";
2157+
# elif defined(__i386__)
2158+
case REG_EAX:
2159+
return "eax";
2160+
case REG_EBX:
2161+
return "ebx";
2162+
case REG_ECX:
2163+
return "ecx";
2164+
case REG_EDX:
2165+
return "edx";
2166+
case REG_EDI:
2167+
return "edi";
2168+
case REG_ESI:
2169+
return "esi";
2170+
case REG_EBP:
2171+
return "ebp";
2172+
case REG_ESP:
2173+
return "esp";
2174+
# endif
2175+
default:
2176+
return NULL;
2177+
}
2178+
return NULL;
2179+
}
2180+
2181+
UNUSED
2182+
static void DumpSingleReg(ucontext_t *ctx, int RegNum) {
2183+
const char *RegName = RegNumToRegName(RegNum);
2184+
# if defined(__x86_64__)
2185+
Printf("%s%s = 0x%016llx ", internal_strlen(RegName) == 2 ? " " : "",
2186+
RegName, ctx->uc_mcontext.gregs[RegNum]);
2187+
# elif defined(__i386__)
2188+
Printf("%s = 0x%08x ", RegName, ctx->uc_mcontext.gregs[RegNum]);
2189+
# endif
2190+
}
2191+
21212192
void SignalContext::DumpAllRegisters(void *context) {
2122-
// FIXME: Implement this.
2193+
# if SANITIZER_LINUX
2194+
ucontext_t *ucontext = (ucontext_t *)context;
2195+
# if defined(__x86_64__)
2196+
Report("Register values:\n");
2197+
DumpSingleReg(ucontext, REG_RAX);
2198+
DumpSingleReg(ucontext, REG_RBX);
2199+
DumpSingleReg(ucontext, REG_RCX);
2200+
DumpSingleReg(ucontext, REG_RDX);
2201+
Printf("\n");
2202+
DumpSingleReg(ucontext, REG_RDI);
2203+
DumpSingleReg(ucontext, REG_RSI);
2204+
DumpSingleReg(ucontext, REG_RBP);
2205+
DumpSingleReg(ucontext, REG_RSP);
2206+
Printf("\n");
2207+
DumpSingleReg(ucontext, REG_R8);
2208+
DumpSingleReg(ucontext, REG_R9);
2209+
DumpSingleReg(ucontext, REG_R10);
2210+
DumpSingleReg(ucontext, REG_R11);
2211+
Printf("\n");
2212+
DumpSingleReg(ucontext, REG_R12);
2213+
DumpSingleReg(ucontext, REG_R13);
2214+
DumpSingleReg(ucontext, REG_R14);
2215+
DumpSingleReg(ucontext, REG_R15);
2216+
Printf("\n");
2217+
# elif defined(__i386__)
2218+
// Duplication of this report print is caused by partial support
2219+
// of register values dumping. In case of unsupported yet architecture let's
2220+
// avoid printing 'Register values:' without actual values in the following
2221+
// output.
2222+
Report("Register values:\n");
2223+
DumpSingleReg(ucontext, REG_EAX);
2224+
DumpSingleReg(ucontext, REG_EBX);
2225+
DumpSingleReg(ucontext, REG_ECX);
2226+
DumpSingleReg(ucontext, REG_EDX);
2227+
Printf("\n");
2228+
DumpSingleReg(ucontext, REG_EDI);
2229+
DumpSingleReg(ucontext, REG_ESI);
2230+
DumpSingleReg(ucontext, REG_EBP);
2231+
DumpSingleReg(ucontext, REG_ESP);
2232+
Printf("\n");
2233+
# endif
2234+
(void)ucontext;
2235+
# endif
2236+
// FIXME: Implement this for other OSes and architectures.
21232237
}
21242238

21252239
static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
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-prefix=CHECK-NODUMP
4+
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-DUMP
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-prefix=CHECK-NODUMP
4+
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-DUMP
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)