Chromium Code Reviews| 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..7d6ee5e36c677a0fdc403c8ba5bde909bc56db17 100644 |
| --- a/base/trace_event/heap_profiler_allocation_context_tracker.cc |
| +++ b/base/trace_event/heap_profiler_allocation_context_tracker.cc |
| @@ -8,9 +8,14 @@ |
| #include <iterator> |
| #include "base/atomicops.h" |
| +#include "base/threading/platform_thread.h" |
| #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 { |
| @@ -33,6 +38,27 @@ void DestructAllocationContextTracker(void* alloc_ctx_tracker) { |
| delete static_cast<AllocationContextTracker*>(alloc_ctx_tracker); |
| } |
| +// Gets the thread name from kernel if available or returns a string with id. |
| +// This function intenionally leaks the allocated strings since they are used to |
| +// tag allocations even after the thread dies. |
| +const char* GetThreadName() { |
|
Primiano Tucci (use gerrit)
2016/05/17 19:38:43
can you make this a void GetThreadName(char* out_n
Dmitry Skiba
2016/05/17 20:05:18
Let's simply call function LeakThreadName(), and p
Primiano Tucci (use gerrit)
2016/05/17 20:46:12
You could pass a std::string* as arugment and leak
ssid
2016/05/18 03:38:05
Changed the name and added ANNOTATE_LEAKING_OBJECT
|
| + char name[16]; |
| +#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. |
| + int err = prctl(PR_GET_NAME, name); |
| + if (!err) { |
| + return strdup(name); |
|
Primiano Tucci (use gerrit)
2016/05/17 19:38:44
once you move this, make sure you include leak_ann
ssid
2016/05/18 03:38:05
Done.
|
| + } |
| +#endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| + |
| + // Use tid if we don't have a thread name. |
| + snprintf(name, sizeof(name), "%lu", |
| + static_cast<unsigned long>(PlatformThread::CurrentId())); |
| + return strdup(name); |
|
Primiano Tucci (use gerrit)
2016/05/17 19:38:43
instead of strduping twice what you want to do bel
ssid
2016/05/18 03:38:05
Keepin strdup twice.
|
| +} |
| + |
| } // namespace |
| // static |
| @@ -142,7 +168,15 @@ 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 (!thread_name_) { |
| + // Ignore the string allocation made by GetThreadName to avoid reentrancy. |
| + ignore_scope_depth_++; |
| + thread_name_ = GetThreadName(); |
| + DCHECK(thread_name_); |
| + ignore_scope_depth_--; |
| + } |
| + |
| + // Add the thread name as the first entry in pseudo stack. |
| if (thread_name_) { |
| *backtrace++ = StackFrame::FromThreadName(thread_name_); |
| } |