[PATCH] D32182: Implement function to get registers from suspended thread on darwin

Francis Ricci via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 18 11:31:53 PDT 2017


fjricci created this revision.

https://reviews.llvm.org/D32182

Files:
  lib/sanitizer_common/sanitizer_stoptheworld_mac.cc


Index: lib/sanitizer_common/sanitizer_stoptheworld_mac.cc
===================================================================
--- lib/sanitizer_common/sanitizer_stoptheworld_mac.cc
+++ lib/sanitizer_common/sanitizer_stoptheworld_mac.cc
@@ -49,11 +49,30 @@
 }
 
 #if defined(__x86_64__)
+typedef x86_thread_state64_t regs_struct;
+#define THREAD_STATE x86_THREAD_STATE64
 #define THREAD_STATE_COUNT x86_THREAD_STATE64_COUNT
+
+#define SP_REG __rsp
+
 #elif defined(__aarch64__)
+typedef arm_thread_state64_t regs_struct;
+#define THREAD_STATE ARM_THREAD_STATE64
 #define THREAD_STATE_COUNT ARM_THREAD_STATE64_COUNT
+
+#if __DARWIN_UNIX03
+#define SP_REG __sp
+#else
+#define SP_REG sp
+#endif
+
 #elif defined(__i386)
+typedef x86_thread_state32_t regs_struct;
+#define THREAD_STATE x86_THREAD_STATE32
 #define THREAD_STATE_COUNT x86_THREAD_STATE32_COUNT
+
+#define SP_REG __esp
+
 #else
 #error "Unsupported architecture"
 #endif
@@ -93,8 +112,26 @@
 
 PtraceRegistersStatus SuspendedThreadsListMac::GetRegistersAndSP(
     uptr index, uptr *buffer, uptr *sp) const {
-  CHECK(0 && "unimplemented");
-  return REGISTERS_UNAVAILABLE_FATAL;
+  thread_t thread = GetThread(index);
+  regs_struct regs;
+  int err;
+  mach_msg_type_number_t reg_count = THREAD_STATE_COUNT;
+  err =
+      thread_get_state(thread, THREAD_STATE, (thread_state_t)&regs, &reg_count);
+  if (err != KERN_SUCCESS) {
+    VReport(1, "Error - unable to get registers for a thread\n");
+    // KERN_INVALID_ARGUMENT indicates that either the flavor is invalid,
+    // or the thread does not exist. The other possible error case,
+    // MIG_ARRAY_TOO_LARGE, means that the state is too large, but it's
+    // still safe to proceed.
+    return err == KERN_INVALID_ARGUMENT ? REGISTERS_UNAVAILABLE_FATAL
+                                        : REGISTERS_UNAVAILABLE;
+  }
+
+  internal_memcpy(buffer, &regs, sizeof(regs));
+  *sp = regs.SP_REG;
+
+  return REGISTERS_AVAILABLE;
 }
 
 uptr SuspendedThreadsListMac::RegisterCount() const {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32182.95607.patch
Type: text/x-patch
Size: 2013 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170418/3c2fa389/attachment.bin>


More information about the llvm-commits mailing list