[compiler-rt] [compiler-rt] Add `DumpAllRegisters` impl (PR #99049)

Dmitriy Chestnykh via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 16 07:27:07 PDT 2024


https://github.com/chestnykh created https://github.com/llvm/llvm-project/pull/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...
`

>From 66b8337cba8ee56eaf2689e127f95867ccccc388 Mon Sep 17 00:00:00 2001
From: Dmitry Chestnykh <dm.chestnykh at gmail.com>
Date: Tue, 16 Jul 2024 17:22:29 +0300
Subject: [PATCH] [compiler-rt] Add `DumpAllRegisters` impl

- 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...
`
---
 .../lib/sanitizer_common/sanitizer_linux.cpp  | 83 ++++++++++++++++++-
 .../TestCases/Posix/dump_registers.cpp        | 12 ++-
 2 files changed, 90 insertions(+), 5 deletions(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
index 7935c88204a05..58233a3c0f607 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
@@ -2118,8 +2118,89 @@ bool SignalContext::IsTrueFaultingAddress() const {
   return si->si_signo == SIGSEGV && si->si_code != 128;
 }
 
+static const char *RegNumToRegName(int reg) {
+#  if defined(__x86_64__)
+  switch (reg) {
+    case REG_RAX:
+      return "rax";
+    case REG_RBX:
+      return "rbx";
+    case REG_RCX:
+      return "rcx";
+    case REG_RDX:
+      return "rdx";
+    case REG_RDI:
+      return "rdi";
+    case REG_RSI:
+      return "rsi";
+    case REG_RBP:
+      return "rbp";
+    case REG_RSP:
+      return "rsp";
+    case REG_R8:
+      return "r8";
+    case REG_R9:
+      return "r9";
+    case REG_R10:
+      return "r10";
+    case REG_R11:
+      return "r11";
+    case REG_R12:
+      return "r12";
+    case REG_R13:
+      return "r13";
+    case REG_R14:
+      return "r14";
+    case REG_R15:
+      return "r15";
+    default:
+      return NULL;
+  }
+#  endif
+  return NULL;
+}
+
 void SignalContext::DumpAllRegisters(void *context) {
-  // FIXME: Implement this.
+#  if SANITIZER_LINUX
+  ucontext_t *ucontext = (ucontext_t *)context;
+#    define DUMPREG64(r)                             \
+      Printf("%s = 0x%016llx  ", RegNumToRegName(r), \
+             ucontext->uc_mcontext.gregs[r]);
+#    define DUMPREG_(r) \
+      Printf(" ");      \
+      DUMPREG(r);
+#    define DUMPREG__(r) \
+      Printf("  ");      \
+      DUMPREG(r);
+#    define DUMPREG___(r) \
+      Printf("   ");      \
+      DUMPREG(r);
+#    if defined(__x86_64__)
+#      define DUMPREG(r) DUMPREG64(r)
+  Report("Register values:\n");
+  DUMPREG(REG_RAX);
+  DUMPREG(REG_RBX);
+  DUMPREG(REG_RCX);
+  DUMPREG(REG_RDX);
+  Printf("\n");
+  DUMPREG(REG_RDI);
+  DUMPREG(REG_RSI);
+  DUMPREG(REG_RBP);
+  DUMPREG(REG_RSP);
+  Printf("\n");
+  DUMPREG_(REG_R8);
+  DUMPREG_(REG_R9);
+  DUMPREG(REG_R10);
+  DUMPREG(REG_R11);
+  Printf("\n");
+  DUMPREG(REG_R12);
+  DUMPREG(REG_R13);
+  DUMPREG(REG_R14);
+  DUMPREG(REG_R15);
+  Printf("\n");
+#    endif
+#  endif
+  // FIXME: Implement this for other OSes and architectures.
 }
 
 static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/dump_registers.cpp b/compiler-rt/test/sanitizer_common/TestCases/Posix/dump_registers.cpp
index f09b2bf4447cc..2cbd40e924263 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/dump_registers.cpp
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/dump_registers.cpp
@@ -1,16 +1,20 @@
 // Check that sanitizer prints registers dump_registers on dump_registers=1
 // RUN: %clangxx  %s -o %t
-// RUN: %env_tool_opts=dump_registers=0 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NODUMP
-// RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-DUMP
+// RUN: %env_tool_opts=dump_registers=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NODUMP
+// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-DUMP
 //
-// FIXME: Implement.
-// XFAIL: *
+// FIXME: Implement for other OSes and architectures.
+// REQUIRES: x86_64-target-arch, linux
 
 #include <signal.h>
 
 int main() {
   raise(SIGSEGV);
   // CHECK-DUMP: Register values
+  // CHECK-DUMP-NEXT: rax = {{0x[0-9a-f]+}}  rbx = {{0x[0-9a-f]+}}  rcx = {{0x[0-9a-f]+}}  rdx = {{0x[0-9a-f]+}}
+  // CHECK-DUMP-NEXT: rdi = {{0x[0-9a-f]+}}  rsi = {{0x[0-9a-f]+}}  rbp = {{0x[0-9a-f]+}}  rsp = {{0x[0-9a-f]+}}
+  // CHECK-DUMP-NEXT:  r8 = {{0x[0-9a-f]+}}   r9 = {{0x[0-9a-f]+}}  r10 = {{0x[0-9a-f]+}}  r11 = {{0x[0-9a-f]+}}
+  // CHECK-DUMP-NEXT: r12 = {{0x[0-9a-f]+}}   r13 = {{0x[0-9a-f]+}} r14 = {{0x[0-9a-f]+}}  r15 = {{0x[0-9a-f]+}}
   // CHECK-NODUMP-NOT: Register values
   return 0;
 }



More information about the llvm-commits mailing list