Fix flaky 137-cfi test.

Avoid modifying the native debug info while we are unwinding.

Bug: 111411286
Test: test.py --host -b -r -t 137-cfi
Change-Id: Ie5a173d0fb545011413fa0326135aa9c56006e8d
diff --git a/runtime/jit/debugger_interface.cc b/runtime/jit/debugger_interface.cc
index 0ada458..f522be8 100644
--- a/runtime/jit/debugger_interface.cc
+++ b/runtime/jit/debugger_interface.cc
@@ -423,4 +423,8 @@
   return size;
 }
 
+Mutex* GetNativeDebugInfoLock() {
+  return &g_jit_debug_lock;
+}
+
 }  // namespace art
diff --git a/runtime/jit/debugger_interface.h b/runtime/jit/debugger_interface.h
index 5bb4682..19507b0 100644
--- a/runtime/jit/debugger_interface.h
+++ b/runtime/jit/debugger_interface.h
@@ -27,6 +27,7 @@
 namespace art {
 
 class DexFile;
+class Mutex;
 class Thread;
 
 // This method is declared in the compiler library.
@@ -60,6 +61,11 @@
 // Returns approximate memory used by debug info for JIT code.
 size_t GetJitMiniDebugInfoMemUsage();
 
+// Get the lock which protects the native debug info.
+// Used only in tests to unwind while the JIT thread is running.
+// TODO: Unwinding should be race-free. Remove this.
+Mutex* GetNativeDebugInfoLock();
+
 }  // namespace art
 
 #endif  // ART_RUNTIME_JIT_DEBUGGER_INTERFACE_H_
diff --git a/test/137-cfi/cfi.cc b/test/137-cfi/cfi.cc
index 4d886f5..6b1480f 100644
--- a/test/137-cfi/cfi.cc
+++ b/test/137-cfi/cfi.cc
@@ -32,9 +32,11 @@
 #include "base/file_utils.h"
 #include "base/logging.h"
 #include "base/macros.h"
+#include "base/mutex.h"
 #include "base/utils.h"
 #include "gc/heap.h"
 #include "gc/space/image_space.h"
+#include "jit/debugger_interface.h"
 #include "oat_file.h"
 #include "runtime.h"
 
@@ -84,6 +86,7 @@
 
 extern "C" JNIEXPORT jboolean JNICALL Java_Main_sigstop(JNIEnv*, jclass) {
 #if __linux__
+  MutexLock mu(Thread::Current(), *GetNativeDebugInfoLock());  // Avoid races with the JIT thread.
   raise(SIGSTOP);
 #endif
   return true;  // Prevent the compiler from tail-call optimizing this method away.
@@ -133,6 +136,8 @@
 
 extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindInProcess(JNIEnv*, jclass) {
 #if __linux__
+  MutexLock mu(Thread::Current(), *GetNativeDebugInfoLock());  // Avoid races with the JIT thread.
+
   std::unique_ptr<Backtrace> bt(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, GetTid()));
   if (!bt->Unwind(0, nullptr)) {
     printf("Cannot unwind in process.\n");