Fix -XX:DumpGCPerformanceOnShutdown for debug builds

There was a failing DCHECK from CheckUnattachedThread. The fix is
to dump after attaching the shutdown thread.

Bug: 35644369
Test: test-art-host

Change-Id: I3d927e380888418167c101b2f09d1e547fe728cf
diff --git a/runtime/gc/heap_test.cc b/runtime/gc/heap_test.cc
index 515a6fd..6d426c2 100644
--- a/runtime/gc/heap_test.cc
+++ b/runtime/gc/heap_test.cc
@@ -72,6 +72,11 @@
   bitmap->Set(fake_end_of_heap_object);
 }
 
+TEST_F(HeapTest, DumpGCPerformanceOnShutdown) {
+  Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references */ false);
+  Runtime::Current()->SetDumpGCPerformanceOnShutdown(true);
+}
+
 class ZygoteHeapTest : public CommonRuntimeTest {
   void SetUpRuntimeOptions(RuntimeOptions* options) {
     CommonRuntimeTest::SetUpRuntimeOptions(options);
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 48efbe5..e563027 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -269,13 +269,6 @@
     UnloadNativeBridge();
   }
 
-  if (dump_gc_performance_on_shutdown_) {
-    // This can't be called from the Heap destructor below because it
-    // could call RosAlloc::InspectAll() which needs the thread_list
-    // to be still alive.
-    heap_->DumpGcPerformanceInfo(LOG_STREAM(INFO));
-  }
-
   Thread* self = Thread::Current();
   const bool attach_shutdown_thread = self == nullptr;
   if (attach_shutdown_thread) {
@@ -285,6 +278,13 @@
     LOG(WARNING) << "Current thread not detached in Runtime shutdown";
   }
 
+  if (dump_gc_performance_on_shutdown_) {
+    // This can't be called from the Heap destructor below because it
+    // could call RosAlloc::InspectAll() which needs the thread_list
+    // to be still alive.
+    heap_->DumpGcPerformanceInfo(LOG_STREAM(INFO));
+  }
+
   if (jit_ != nullptr) {
     // Stop the profile saver thread before marking the runtime as shutting down.
     // The saver will try to dump the profiles before being sopped and that
diff --git a/runtime/runtime.h b/runtime/runtime.h
index 92feabb..20db628 100644
--- a/runtime/runtime.h
+++ b/runtime/runtime.h
@@ -661,6 +661,10 @@
 
   void InitThreadGroups(Thread* self);
 
+  void SetDumpGCPerformanceOnShutdown(bool value) {
+    dump_gc_performance_on_shutdown_ = value;
+  }
+
  private:
   static void InitPlatformSignalHandlers();