Fix crash when debugging exception

Fixes a crash when detaching the debugger during an exception event. The cause
is a concurrent modification of Instrumentation::exception_caught_listeners_
list (consequence of debugger being detached) while we loop over its elements.

Workaround the issue by making a copy of the listeners list.

Bug: 13738672
Change-Id: I56b84c5c8f31d6fbda1ba7cb35caeefc5e7ca60f
diff --git a/runtime/instrumentation.cc b/runtime/instrumentation.cc
index 525e2b3..c798fbf 100644
--- a/runtime/instrumentation.cc
+++ b/runtime/instrumentation.cc
@@ -750,7 +750,10 @@
   if (have_exception_caught_listeners_) {
     DCHECK_EQ(thread->GetException(NULL), exception_object);
     thread->ClearException();
-    for (InstrumentationListener* listener : exception_caught_listeners_) {
+    // TODO: The copy below is due to the debug listener having an action where it can remove
+    // itself as a listener and break the iterator. The copy only works around the problem.
+    std::list<InstrumentationListener*> copy(exception_caught_listeners_);
+    for (InstrumentationListener* listener : copy) {
       listener->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc, exception_object);
     }
     thread->SetException(throw_location, exception_object);