[Lldb-commits] [lldb] r181722 - Add setting of lldb thread names on Linux.

Matt Kopec Matt.Kopec at intel.com
Mon May 13 12:33:58 PDT 2013


Author: mkopec
Date: Mon May 13 14:33:58 2013
New Revision: 181722

URL: http://llvm.org/viewvc/llvm-project?rev=181722&view=rev
Log:
Add setting of lldb thread names on Linux.

Patch by Mike Sartain.

Modified:
    lldb/trunk/include/lldb/Host/Host.h
    lldb/trunk/source/Host/common/Host.cpp
    lldb/trunk/source/Host/linux/Host.cpp

Modified: lldb/trunk/include/lldb/Host/Host.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/Host.h?rev=181722&r1=181721&r2=181722&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/Host.h (original)
+++ lldb/trunk/include/lldb/Host/Host.h Mon May 13 14:33:58 2013
@@ -299,7 +299,7 @@ public:
     ///     \b true if the thread name was able to be set, \b false
     ///     otherwise.
     //------------------------------------------------------------------
-    static void
+    static bool
     SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name);
 
     //------------------------------------------------------------------

Modified: lldb/trunk/source/Host/common/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Host.cpp?rev=181722&r1=181721&r2=181722&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Host.cpp (original)
+++ lldb/trunk/source/Host/common/Host.cpp Mon May 13 14:33:58 2013
@@ -512,7 +512,8 @@ Host::WillTerminate ()
 {
 }
 
-#if !defined (__APPLE__) && !defined (__FreeBSD__) // see macosx/Host.mm
+#if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__linux__) // see macosx/Host.mm
+
 void
 Host::ThreadCreated (const char *thread_name)
 {
@@ -531,7 +532,7 @@ Host::GetEnvironment (StringList &env)
     return 0;
 }
 
-#endif
+#endif // #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__linux__)
 
 struct HostThreadCreateInfo
 {
@@ -652,7 +653,7 @@ Host::GetThreadName (lldb::pid_t pid, ll
     return thread_name;
 }
 
-void
+bool
 Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
 {
 #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
@@ -667,8 +668,33 @@ Host::SetThreadName (lldb::pid_t pid, ll
     // Set the pthread name if possible
     if (pid == curr_pid && tid == curr_tid)
     {
-        ::pthread_setname_np (name);
+        if (::pthread_setname_np (name) == 0)
+            return true;
+    }
+    return false;
+#elif defined (__linux__)
+    void *fn = dlsym (RTLD_DEFAULT, "pthread_setname_np");
+    if (fn)
+    {
+        int (*pthread_setname_np_func)(pthread_t thread, const char *name);
+        *reinterpret_cast<void **> (&pthread_setname_np_func) = fn;
+
+        lldb::pid_t curr_pid = Host::GetCurrentProcessID();
+        lldb::tid_t curr_tid = Host::GetCurrentThreadID();
+
+        if (pid == LLDB_INVALID_PROCESS_ID)
+            pid = curr_pid;
+
+        if (tid == LLDB_INVALID_THREAD_ID)
+            tid = curr_tid;
+
+        if (pid == curr_pid)
+        {
+            if (pthread_setname_np_func (tid, name) == 0)
+                return true;
+        }
     }
+    return false;
 #endif
 }
 

Modified: lldb/trunk/source/Host/linux/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/linux/Host.cpp?rev=181722&r1=181721&r2=181722&view=diff
==============================================================================
--- lldb/trunk/source/Host/linux/Host.cpp (original)
+++ lldb/trunk/source/Host/linux/Host.cpp Mon May 13 14:33:58 2013
@@ -169,4 +169,51 @@ Host::GetProcessInfo (lldb::pid_t pid, P
     // FIXME: Parse /proc/<pid>/status to get uid, gid, euid, egid and parent_pid
 
     return true;
-}
\ No newline at end of file
+}
+
+void
+Host::ThreadCreated (const char *thread_name)
+{
+    if (!Host::SetThreadName (LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID, thread_name))
+    {
+        // pthread_setname_np_func can fail if the thread name is longer than
+        //  the supported limit on Linux. When this occurs, the error ERANGE is returned
+        // and SetThreadName will fail. Let's drop it down to 16 characters and try again.
+        char namebuf[16];
+
+        // Thread names are coming in like '<lldb.comm.debugger.edit>' and '<lldb.comm.debugger.editline>'
+        // So just chopping the end of the string off leads to a lot of similar named threads.
+        // Go through the thread name and search for the last dot and use that.
+        const char *lastdot = ::strrchr( thread_name, '.' );
+
+        if (lastdot && lastdot != thread_name)
+            thread_name = lastdot + 1;
+        ::strncpy (namebuf, thread_name, sizeof(namebuf));
+        namebuf[ sizeof(namebuf) - 1 ] = 0;
+
+        int namebuflen = strlen(namebuf);
+        if (namebuflen > 0)
+        {
+            if (namebuf[namebuflen - 1] == '(' || namebuf[namebuflen - 1] == '>')
+            {
+                // Trim off trailing '(' and '>' characters for a bit more cleanup.
+                namebuflen--;
+                namebuf[namebuflen] = 0;
+            }
+            Host::SetThreadName (LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID, namebuf);
+        }
+    }
+}
+
+void
+Host::Backtrace (Stream &strm, uint32_t max_frames)
+{
+    // TODO: Is there a way to backtrace the current process on linux?
+}
+
+size_t
+Host::GetEnvironment (StringList &env)
+{
+    // TODO: Is there a way to the host environment for this process on linux?
+    return 0;
+}





More information about the lldb-commits mailing list