Fix alloc tracker locking issue

Fixes a lock level violation where we attempt to take the thread list lock
while already holding the alloc tracker lock. We now avoid holding the alloc
tracker lock when updating allocation entrypoints.

Bug: 13646642
Change-Id: Iab505dae67d0c754031fe30d50a7cbd5e4ba5785
diff --git a/runtime/debugger.cc b/runtime/debugger.cc
index c18d5c6e..1ff2003 100644
--- a/runtime/debugger.cc
+++ b/runtime/debugger.cc
@@ -3767,22 +3767,27 @@
 }
 
 void Dbg::SetAllocTrackingEnabled(bool enabled) {
-  MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
   if (enabled) {
-    if (recent_allocation_records_ == NULL) {
-      alloc_record_max_ = GetAllocTrackerMax();
-      LOG(INFO) << "Enabling alloc tracker (" << alloc_record_max_ << " entries of "
-                << kMaxAllocRecordStackDepth << " frames, taking "
-                << PrettySize(sizeof(AllocRecord) * alloc_record_max_) << ")";
-      alloc_record_head_ = alloc_record_count_ = 0;
-      recent_allocation_records_ = new AllocRecord[alloc_record_max_];
-      CHECK(recent_allocation_records_ != NULL);
+    {
+      MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
+      if (recent_allocation_records_ == NULL) {
+        alloc_record_max_ = GetAllocTrackerMax();
+        LOG(INFO) << "Enabling alloc tracker (" << alloc_record_max_ << " entries of "
+            << kMaxAllocRecordStackDepth << " frames, taking "
+            << PrettySize(sizeof(AllocRecord) * alloc_record_max_) << ")";
+        alloc_record_head_ = alloc_record_count_ = 0;
+        recent_allocation_records_ = new AllocRecord[alloc_record_max_];
+        CHECK(recent_allocation_records_ != NULL);
+      }
     }
     Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints();
   } else {
     Runtime::Current()->GetInstrumentation()->UninstrumentQuickAllocEntryPoints();
-    delete[] recent_allocation_records_;
-    recent_allocation_records_ = NULL;
+    {
+      MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
+      delete[] recent_allocation_records_;
+      recent_allocation_records_ = NULL;
+    }
   }
 }