Add ScopedGCCriticalSection to Trace::StopTracing.

This attempts to fix a deadlock in 545-tracing-and-jit with the read
barrier config. I think the cause is the same as what CL 174096
describes and it missed this one in Trace::StopTracing.

Bug: 26429931
Bug: 12687968
Change-Id: I02ca45e2667c9f6afc27d73919d6c31d204cc621
diff --git a/runtime/trace.cc b/runtime/trace.cc
index 99b2296..6b82641 100644
--- a/runtime/trace.cc
+++ b/runtime/trace.cc
@@ -389,9 +389,10 @@
   bool stop_alloc_counting = false;
   Runtime* const runtime = Runtime::Current();
   Trace* the_trace = nullptr;
+  Thread* const self = Thread::Current();
   pthread_t sampling_pthread = 0U;
   {
-    MutexLock mu(Thread::Current(), *Locks::trace_lock_);
+    MutexLock mu(self, *Locks::trace_lock_);
     if (the_trace_ == nullptr) {
       LOG(ERROR) << "Trace stop requested, but no trace currently running";
     } else {
@@ -409,6 +410,9 @@
   }
 
   {
+    gc::ScopedGCCriticalSection gcs(self,
+                                    gc::kGcCauseInstrumentation,
+                                    gc::kCollectorTypeInstrumentation);
     ScopedSuspendAll ssa(__FUNCTION__);
     if (the_trace != nullptr) {
       stop_alloc_counting = (the_trace->flags_ & Trace::kTraceCountAllocs) != 0;
@@ -417,7 +421,7 @@
       }
 
       if (the_trace->trace_mode_ == TraceMode::kSampling) {
-        MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
+        MutexLock mu(self, *Locks::thread_list_lock_);
         runtime->GetThreadList()->ForEach(ClearThreadStackTraceAndClockBase, nullptr);
       } else {
         runtime->GetInstrumentation()->DisableMethodTracing(kTracerInstrumentationKey);