[compiler-rt] r178461 - [libsanitizer] Add register dumping support to SuspendedThreadsList.

Alexander Potapenko glider at google.com
Mon Apr 1 06:36:42 PDT 2013


Author: glider
Date: Mon Apr  1 08:36:42 2013
New Revision: 178461

URL: http://llvm.org/viewvc/llvm-project?rev=178461&view=rev
Log:
[libsanitizer] Add register dumping support to SuspendedThreadsList.

An interface for obtaining register contexts from suspended threads. Tailored
for LSan use.

Patch by Sergey Matveev (earthdok at google.com)

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux.cc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld.h?rev=178461&r1=178460&r2=178461&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld.h Mon Apr  1 08:36:42 2013
@@ -20,21 +20,21 @@
 namespace __sanitizer {
 typedef int SuspendedThreadID;
 
-// Holds the list of suspended threads. Also provides register dumping
-// functionality (to be implemented).
+// Holds the list of suspended threads and provides an interface to dump their
+// register contexts.
 class SuspendedThreadsList {
  public:
   SuspendedThreadsList()
     : thread_ids_(1024) {}
-  SuspendedThreadID GetThreadID(uptr index) {
+  SuspendedThreadID GetThreadID(uptr index) const {
     CHECK_LT(index, thread_ids_.size());
     return thread_ids_[index];
   }
-  void DumpRegisters(uptr index) const {
-    UNIMPLEMENTED();
-  }
-  uptr thread_count() { return thread_ids_.size(); }
-  bool Contains(SuspendedThreadID thread_id) {
+  int GetRegistersAndSP(uptr index, uptr *buffer, uptr *sp) const;
+  // The buffer in GetRegistersAndSP should be at least this big.
+  static uptr RegisterCount();
+  uptr thread_count() const { return thread_ids_.size(); }
+  bool Contains(SuspendedThreadID thread_id) const {
     for (uptr i = 0; i < thread_ids_.size(); i++) {
       if (thread_ids_[i] == thread_id)
         return true;

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux.cc?rev=178461&r1=178460&r2=178461&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux.cc Mon Apr  1 08:36:42 2013
@@ -24,6 +24,7 @@
 #include <sys/prctl.h> // for PR_* definitions
 #include <sys/ptrace.h> // for PTRACE_* definitions
 #include <sys/types.h> // for pid_t
+#include <sys/user.h> // for user_regst_struct
 #include <sys/wait.h> // for signal-related stuff
 
 #include "sanitizer_common.h"
@@ -324,6 +325,29 @@ void StopTheWorld(StopTheWorldCallback c
   sigprocmask(SIG_SETMASK, &old_sigset, &old_sigset);
 }
 
+// Platform-specific methods from SuspendedThreadsList.
+int SuspendedThreadsList::GetRegistersAndSP(uptr index,
+                                            uptr *buffer,
+                                            uptr *sp) const {
+  pid_t tid = GetThreadID(index);
+  user_regs_struct regs;
+  if (internal_ptrace(PTRACE_GETREGS, tid, NULL, &regs) != 0) {
+    Report("Could not get registers from thread %d (errno %d).\n",
+           tid, errno);
+    return -1;
+  }
+#if __WORDSIZE == 32
+    *sp = regs.esp;
+#else
+    *sp = regs.rsp;
+#endif
+  internal_memcpy(buffer, &regs, sizeof(regs));
+  return 0;
+}
+
+uptr SuspendedThreadsList::RegisterCount() {
+  return sizeof(user_regs_struct) / sizeof(uptr);
+}
 }  // namespace __sanitizer
 
 #endif  // __linux__





More information about the llvm-commits mailing list