From 24512a7367f6e6289607193eb4d544c21bb9831c Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Tue, 1 May 2012 15:34:41 -0400 Subject: [PATCH] Bug 750498 - Support IPC Thread::SetName on Linux. r=cjones --HG-- extra : rebase_source : b7eeef0e1cf7886685675eb25fcb7dbf00da2f0a --- .../src/base/platform_thread_posix.cc | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/ipc/chromium/src/base/platform_thread_posix.cc b/ipc/chromium/src/base/platform_thread_posix.cc index aa4f66157dc..8ee42ce6c33 100644 --- a/ipc/chromium/src/base/platform_thread_posix.cc +++ b/ipc/chromium/src/base/platform_thread_posix.cc @@ -11,6 +11,7 @@ #include #elif defined(OS_LINUX) #include +#include #include #endif @@ -66,12 +67,19 @@ void PlatformThread::Sleep(int duration_ms) { // static void PlatformThread::SetName(const char* name) { - // The POSIX standard does not provide for naming threads, and neither Linux - // nor Mac OS X (our two POSIX targets) provide any non-portable way of doing - // it either. (Some BSDs provide pthread_set_name_np but that isn't much of a - // consolation prize.) - // TODO(darin): decide whether stuffing the name in TLS or other in-memory - // structure would be useful for debugging or not. + // On linux we can get the thread names to show up in the debugger by setting + // the process name for the LWP. We don't want to do this for the main + // thread because that would rename the process, causing tools like killall + // to stop working. + if (PlatformThread::CurrentId() == getpid()) + return; + + // http://0pointer.de/blog/projects/name-your-threads.html + // Set the name for the LWP (which gets truncated to 15 characters). + // Note that glibc also has a 'pthread_setname_np' api, but it may not be + // available everywhere and it's only benefit over using prctl directly is + // that it can set the name of threads other than the current thread. + prctl(PR_SET_NAME, reinterpret_cast(name), 0, 0, 0); } #endif // !OS_MACOSX