Index: base/trace_event/heap_profiler_allocation_context_tracker.cc |
diff --git a/base/trace_event/heap_profiler_allocation_context_tracker.cc b/base/trace_event/heap_profiler_allocation_context_tracker.cc |
index fd4e21abe5d62d6594e6fd5bf56544228cce92f1..7f3dfcc1a4635c729b4b4ae44512a7b9377bfe0c 100644 |
--- a/base/trace_event/heap_profiler_allocation_context_tracker.cc |
+++ b/base/trace_event/heap_profiler_allocation_context_tracker.cc |
@@ -11,6 +11,10 @@ |
#include "base/threading/thread_local_storage.h" |
#include "base/trace_event/heap_profiler_allocation_context.h" |
+#if defined(OS_LINUX) || defined(OS_ANDROID) |
+#include <sys/prctl.h> |
+#endif |
+ |
namespace base { |
namespace trace_event { |
@@ -142,7 +146,30 @@ AllocationContext AllocationContextTracker::GetContextSnapshot() { |
auto backtrace = std::begin(ctx.backtrace.frames); |
auto backtrace_end = std::end(ctx.backtrace.frames); |
- // Add the thread name as the first entry |
+#if defined(OS_LINUX) || defined(OS_ANDROID) |
+ // If the thread name is not set, try to get it from prctl. Thread name might |
+ // not be set in cases where the thread started before heap profiling was |
+ // enabled. |
+ if (!thread_name_) { |
+ // Ignore the string allocation and allocations from prctl to avoid |
Dmitry Skiba
2016/05/13 18:34:06
prctl is a syscall, there can be no reentrancy.
ssid
2016/05/13 20:48:08
Ah yes, I thought it was causing reentrancy too. F
|
+ // reentrancy. |
+ ignore_scope_depth_++; |
+ char* leaked_str = new char[16]; |
+ int err = prctl(PR_GET_NAME, leaked_str); |
+ ignore_scope_depth_--; |
+ |
+ if (err) { |
+ // Set thread name to "Unknown" so that we call prctl only once per |
+ // thread. |
+ thread_name_ = "Unknown"; |
Dmitry Skiba
2016/05/13 18:34:06
Let's format thread id instead. Hmm, and I think s
ssid
2016/05/13 20:48:08
Hm, do you think it is useful to see a thread id i
|
+ free(leaked_str); |
Dmitry Skiba
2016/05/13 18:34:06
Use 'delete []' here since you allocated with new.
ssid
2016/05/13 20:48:09
Yes, fixed.
|
+ } else { |
+ thread_name_ = leaked_str; |
+ } |
+ } |
+#endif // defined(OS_LINUX) || defined(OS_ANDROID) |
+ |
+ // Add the thread name as the first entry in pseudo stack. |
if (thread_name_) { |
*backtrace++ = StackFrame::FromThreadName(thread_name_); |
} |