Fix MacOS createdump testing failures (#42357)
authorMike McLaughlin <mikem@microsoft.com>
Thu, 17 Sep 2020 16:53:07 +0000 (09:53 -0700)
committerGitHub <noreply@github.com>
Thu, 17 Sep 2020 16:53:07 +0000 (09:53 -0700)
Fix MacOS createdump testing failures

There are a few things that are causing MacOS dumps not to be complete and causing the diagnostics SOS tests to fail:

1) The threads were completely stopped with the per thread suspend. Switch from thread_suspend to task_suspend.
2) .Stopping filtering out SM_EMPTY share_mode type memory regions. There are read/write sections that are needed in the dump by the tools

src/coreclr/src/debug/createdump/crashinfomac.cpp
src/coreclr/src/debug/createdump/memoryregion.h

index 033f98d..afc2125 100644 (file)
@@ -24,49 +24,7 @@ void
 CrashInfo::CleanupAndResumeProcess()
 {
     // Resume all the threads suspended in EnumerateAndSuspendThreads
-    for (ThreadInfo* thread : m_threads)
-    {
-        ::thread_resume(thread->Port());
-    }
-}
-
-static
-kern_return_t
-SuspendMachThread(thread_act_t thread, int tid)
-{
-    kern_return_t result;
-
-    while (true)
-    {
-        result = thread_suspend(thread);
-        if (result != KERN_SUCCESS)
-        {
-            fprintf(stderr, "thread_suspend(%d) FAILED %x %s\n", tid, result, mach_error_string(result));
-            break;
-        }
-
-        // Ensure that if the thread was running in the kernel, the kernel operation
-        // is safely aborted so that it can be restarted later.
-        result = thread_abort_safely(thread);
-        if (result == KERN_SUCCESS)
-        {
-            break;
-        }
-        else
-        {
-            TRACE("thread_abort_safely(%d) FAILED %x %s\n", tid, result, mach_error_string(result));
-        }
-        // The thread was running in the kernel executing a non-atomic operation
-        // that cannot be restarted, so we need to resume the thread and retry
-        result = thread_resume(thread);
-        if (result != KERN_SUCCESS)
-        {
-            fprintf(stderr, "thread_resume(%d) FAILED %x %s\n", tid, result, mach_error_string(result));
-            break;
-        }
-    }
-
-    return result;
+    ::task_resume(Task());
 }
 
 //
@@ -78,7 +36,14 @@ CrashInfo::EnumerateAndSuspendThreads()
     thread_act_port_array_t threadList;
     mach_msg_type_number_t threadCount;
 
-    kern_return_t result = ::task_threads(Task(), &threadList, &threadCount);
+    kern_return_t result = ::task_suspend(Task());
+    if (result != KERN_SUCCESS)
+    {
+        fprintf(stderr, "task_suspend(%d) FAILED %x %s\n", m_pid, result, mach_error_string(result));
+        return false;
+    }
+
+    result = ::task_threads(Task(), &threadList, &threadCount);
     if (result != KERN_SUCCESS)
     {
         fprintf(stderr, "task_threads(%d) FAILED %x %s\n", m_pid, result, mach_error_string(result));
@@ -102,11 +67,6 @@ CrashInfo::EnumerateAndSuspendThreads()
             tid = tident.thread_id;
         }
 
-        result = SuspendMachThread(threadList[i], tid);
-        if (result != KERN_SUCCESS)
-        {
-            return false;
-        }
         // Add to the list of threads
         ThreadInfo* thread = new ThreadInfo(*this, tid, threadList[i]);
         m_threads.push_back(thread);
@@ -148,7 +108,7 @@ CrashInfo::EnumerateMemoryRegions()
             fprintf(stderr, "mach_vm_region_recurse for address %016llx %08llx FAILED %x %s\n", address, size, result, mach_error_string(result));
             return false;
         }
-        TRACE("%016llx - %016llx (%06llx) %08llx %s %d %d %d %c%c%c\n",
+        TRACE("%016llx - %016llx (%06llx) %08llx %s %d %d %d %c%c%c %02x\n",
             address,
             address + size,
             size / PAGE_SIZE,
@@ -159,14 +119,15 @@ CrashInfo::EnumerateMemoryRegions()
             depth,
             (info.protection & VM_PROT_READ) ? 'r' : '-',
             (info.protection & VM_PROT_WRITE) ? 'w' : '-',
-            (info.protection & VM_PROT_EXECUTE) ? 'x' : '-');
+            (info.protection & VM_PROT_EXECUTE) ? 'x' : '-',
+            info.protection);
 
         if (info.is_submap) {
             depth++;
         }
         else
         {
-            if (info.share_mode != SM_EMPTY && (info.protection & (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)) != 0)
+            if ((info.protection & (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)) != 0)
             {
                 MemoryRegion memoryRegion(ConvertProtectionFlags(info.protection), address, address + size, info.offset);
                 m_allMemoryRegions.insert(memoryRegion);
index c308510..f1b8a79 100644 (file)
@@ -124,7 +124,7 @@ public:
 
     void Trace() const
     {
-        TRACE("%" PRIA PRIx64 " - %" PRIA PRIx64 " (%06" PRId64 ") %" PRIA PRIx64 " %c%c%c%c%c%c %s\n",
+        TRACE("%" PRIA PRIx64 " - %" PRIA PRIx64 " (%06" PRIx64 ") %" PRIA PRIx64 " %c%c%c%c%c%c %02x %s\n",
             m_startAddress,
             m_endAddress,
             Size() / PAGE_SIZE,
@@ -135,6 +135,7 @@ public:
             (m_flags & MEMORY_REGION_FLAG_SHARED) ? 's' : '-',
             (m_flags & MEMORY_REGION_FLAG_PRIVATE) ? 'p' : '-',
             (m_flags & MEMORY_REGION_FLAG_MEMORY_BACKED) ? 'b' : '-',
+            m_flags,
             m_fileName.c_str());
     }
 };