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

[compiler-rt] Add DumpAllRegisters impl #99049

Merged
merged 5 commits into from
Jul 18, 2024

Conversation

chestnykh
Copy link
Contributor

@chestnykh chestnykh commented Jul 16, 2024

  • 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...

- 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...
`
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 16, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Dmitriy Chestnykh (chestnykh)

Changes
  • 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...


Full diff: https://github.com/llvm/llvm-project/pull/99049.diff

2 Files Affected:

  • (modified) compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp (+82-1)
  • (modified) compiler-rt/test/sanitizer_common/TestCases/Posix/dump_registers.cpp (+8-4)
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;
 }

Fox example gcc warns about `RegNumToRegName` is unused
and about `ucontext` is unused
@chestnykh
Copy link
Contributor Author

- Restore dump_registers.cpp test in Posix/ dir
- Add linux-specifix tests inside Linux/ directory
Mac uses single dump_registers.cpp test inside Darwin/ directory
though checks in this test are partially 'restricted'
For linux we provide one test per each supported architecture with
checks for each part of `DumpAllRegisters` output
@chestnykh
Copy link
Contributor Author

chestnykh commented Jul 18, 2024

@vitalybuka i've covered i386 arch and described in the third commit message why i restored common dump_register.cpp test and made two separate dump_register_.cpp tests instead

Copy link

github-actions bot commented Jul 18, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@vitalybuka vitalybuka merged commit bda1893 into llvm:main Jul 18, 2024
4 of 5 checks passed
sgundapa pushed a commit to sgundapa/upstream_effort that referenced this pull request Jul 23, 2024
- 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...
chestnykh added a commit to chestnykh/llvm-project that referenced this pull request Jul 24, 2024
A couple of previous commits leaded to wrong endif
placement inside the source that caused build probled in
https://lab.llvm.org/buildbot/#/builders/13/builds/1020

See llvm#99613 llvm#99049
chestnykh added a commit to chestnykh/llvm-project that referenced this pull request Jul 24, 2024
A couple of previous commits leaded to wrong endif
placement inside the source that caused build problem in
https://lab.llvm.org/buildbot/#/builders/13/builds/1020

See llvm#99613 llvm#99049
chestnykh added a commit that referenced this pull request Jul 24, 2024
A couple of previous commits leaded to wrong endif placement inside the
source that caused build problem in
https://lab.llvm.org/buildbot/#/builders/13/builds/1020

See #99613 #99049
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary:
- 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...

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251400
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary:
A couple of previous commits leaded to wrong endif placement inside the
source that caused build problem in
https://lab.llvm.org/buildbot/#/builders/13/builds/1020

See #99613 #99049

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60250654
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants