From fdcbc5c4c7c67bba06e038ac96a2e8bc49b91f84 Mon Sep 17 00:00:00 2001 From: Jeff Hao Date: Tue, 14 Oct 2014 13:12:01 -0700 Subject: [PATCH] Store exiting thread ids and names while tracing to output later. Bug: 17909204 Change-Id: I4ce027af7c51fb310d6cdbdf067ae340e4c932ff --- runtime/thread_list.cc | 4 ++++ runtime/trace.cc | 12 ++++++++++++ runtime/trace.h | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/runtime/thread_list.cc b/runtime/thread_list.cc index 725a70841..011bf96d1 100644 --- a/runtime/thread_list.cc +++ b/runtime/thread_list.cc @@ -34,6 +34,7 @@ #include "monitor.h" #include "scoped_thread_state_change.h" #include "thread.h" +#include "trace.h" #include "utils.h" #include "well_known_classes.h" @@ -838,6 +839,9 @@ void ThreadList::Unregister(Thread* self) { // suspend and so on, must happen at this point, and not in ~Thread. self->Destroy(); + // If tracing, remember thread id and name before thread exits. + Trace::StoreExitingThreadInfo(self); + uint32_t thin_lock_id = self->GetThreadId(); while (self != nullptr) { // Remove and delete the Thread* while holding the thread_list_lock_ and diff --git a/runtime/trace.cc b/runtime/trace.cc index 4bb388f3e..4c5e90913 100644 --- a/runtime/trace.cc +++ b/runtime/trace.cc @@ -706,9 +706,21 @@ static void DumpThread(Thread* t, void* arg) { void Trace::DumpThreadList(std::ostream& os) { Thread* self = Thread::Current(); + for (auto it : exited_threads_) { + os << it.first << "\t" << it.second << "\n"; + } Locks::thread_list_lock_->AssertNotHeld(self); MutexLock mu(self, *Locks::thread_list_lock_); Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os); } +void Trace::StoreExitingThreadInfo(Thread* thread) { + MutexLock mu(thread, *Locks::trace_lock_); + if (the_trace_ != nullptr) { + std::string name; + thread->GetThreadName(name); + the_trace_->exited_threads_.Put(thread->GetTid(), name); + } +} + } // namespace art diff --git a/runtime/trace.h b/runtime/trace.h index 45a02dab3..ead1c29c7 100644 --- a/runtime/trace.h +++ b/runtime/trace.h @@ -104,6 +104,8 @@ class Trace FINAL : public instrumentation::InstrumentationListener { static std::vector* AllocStackTrace(); // Clear and store an old stack trace for later use. static void FreeStackTrace(std::vector* stack_trace); + // Save id and name of a thread before it exits. + static void StoreExitingThreadInfo(Thread* thread); private: explicit Trace(File* trace_file, int buffer_size, int flags, bool sampling_enabled); @@ -166,6 +168,9 @@ class Trace FINAL : public instrumentation::InstrumentationListener { // Did we overflow the buffer recording traces? bool overflow_; + // Map of thread ids and names that have already exited. + SafeMap exited_threads_; + DISALLOW_COPY_AND_ASSIGN(Trace); }; -- 2.11.0