[Lldb-commits] [PATCH] Add Host::GetMaxThreadNameLength(), modify private state thread name

Todd Fiala todd.fiala at gmail.com
Fri Jul 11 08:04:26 PDT 2014


Hi all,

This patch looks to provide a non-#ifdef manner to help choose better
debugger-defined thread names when operating on platforms that don't
support long thread names.  We have code that attempts to pick a smarter
short name when a longer thread name is too long to set, but that can fail
too when the shortened variant is still too long.

We have cases with the private state threads where there are two variants
of the thread name that are not unique enough, even in shortened form, to
tell them apart.  This change provides a new method,
Host::GetMaxThreadNameLength(), implemented for Linux and FreeBSD as
returning their implementation-defined limits (16), returns a value
inferred for MacOSX (Apple will want to fix this to the real value, it is
not published, I inferred the value from the max name length that the
thread name retrieval code will support), and
std::numeric_limits<uint32_t>::max() for everything else.

Patch by Shawn Best and Todd Fiala
-- 
-Todd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20140711/cf0690c7/attachment.html>
-------------- next part --------------
diff --git a/include/lldb/Host/Host.h b/include/lldb/Host/Host.h
index 92349f9..0b9637e 100644
--- a/include/lldb/Host/Host.h
+++ b/include/lldb/Host/Host.h
@@ -297,6 +297,20 @@ public:
     ThreadLocalStorageSet(lldb::thread_key_t key, void *value);
 
     //------------------------------------------------------------------
+    /// Gets the maximum name length of a thread in a process.
+    ///
+    /// This function will retrieve the maximum name length that a thread
+    /// can be assigned with SetThreadName().
+    ///
+    /// @return
+    ///     The maximum thread name length supported on a platform.
+    ///     std::numeric_limits<uint32_t>::max() will be returned if there is
+    ///     no practical limit.
+    //------------------------------------------------------------------
+    static uint32_t
+    GetMaxThreadNameLength ();
+
+    //------------------------------------------------------------------
     /// Gets the name of a thread in a process.
     ///
     /// This function will name a thread in a process using it's own
diff --git a/source/Host/common/Host.cpp b/source/Host/common/Host.cpp
index fa3d3bc..2114464 100644
--- a/source/Host/common/Host.cpp
+++ b/source/Host/common/Host.cpp
@@ -579,6 +579,13 @@ Host::WillTerminate ()
 
 #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__) // see macosx/Host.mm
 
+uint32_t
+Host::GetMaxThreadNameLength ()
+{
+    // Indicate unlimited.  Change this for your platform as needed.
+    return std::numeric_limits<uint32_t>::max ();
+}
+
 void
 Host::ThreadCreated (const char *thread_name)
 {
diff --git a/source/Host/freebsd/Host.cpp b/source/Host/freebsd/Host.cpp
index 4f6af67..206b6ec 100644
--- a/source/Host/freebsd/Host.cpp
+++ b/source/Host/freebsd/Host.cpp
@@ -83,6 +83,13 @@ Host::ThreadCreated (const char *thread_name)
     Host::SetShortThreadName (LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID, thread_name, 16);
 }
 
+uint32_t
+Host::GetMaxThreadNameLength ()
+{
+    // The pthreads max name length is 16 characters on FreeBSD.
+    return 16;
+}
+
 std::string
 Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
 {
diff --git a/source/Host/linux/Host.cpp b/source/Host/linux/Host.cpp
index 6c90006..cf84827 100644
--- a/source/Host/linux/Host.cpp
+++ b/source/Host/linux/Host.cpp
@@ -402,6 +402,13 @@ Host::ThreadCreated (const char *thread_name)
     }
 }
 
+uint32_t
+Host::GetMaxThreadNameLength ()
+{
+    // The pthreads max name length is 16 characters on Linux.
+    return 16;
+}
+
 std::string
 Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
 {
diff --git a/source/Host/macosx/Host.mm b/source/Host/macosx/Host.mm
index 69b65fb..6feee6b 100644
--- a/source/Host/macosx/Host.mm
+++ b/source/Host/macosx/Host.mm
@@ -144,6 +144,14 @@ Host::ThreadCreated (const char *thread_name)
     }
 }
 
+uint32_t
+Host::GetMaxThreadNameLength ()
+{
+    // GetThreadName() looks like it is ready to accept 1023 characters, so we'll go with
+    // that.  I couldn't find official documentation on the MacOSX pthread_setname_np() call.
+    return 1023;
+}
+
 std::string
 Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
 {
diff --git a/source/Target/Process.cpp b/source/Target/Process.cpp
index 69ea16a..b8aac65 100644
--- a/source/Target/Process.cpp
+++ b/source/Target/Process.cpp
@@ -3654,11 +3654,22 @@ Process::StartPrivateStateThread (bool force)
     // Create a thread that watches our internal state and controls which
     // events make it to clients (into the DCProcess event queue).
     char thread_name[1024];
-    if (already_running)
-        snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", GetID());
+    if (Host::GetMaxThreadNameLength () <= 16)
+    {
+        // On platforms with abbreviated thread name lengths, choose thread names that fit within the limit.
+        if (already_running)
+            snprintf(thread_name, sizeof(thread_name), "intern-state-OV");
+        else
+            snprintf(thread_name, sizeof(thread_name), "intern-state");
+    }
     else
-        snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID());
-        
+    {
+        if (already_running)
+            snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", GetID());
+        else
+            snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID());
+    }
+
     // Create the private state thread, and start it running.
     m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL);
     bool success = IS_VALID_LLDB_HOST_THREAD(m_private_state_thread);


More information about the lldb-commits mailing list