[Lldb-commits] [lldb] r187149 - Set thread names on FreeBSD

Ed Maste emaste at freebsd.org
Thu Jul 25 12:10:33 PDT 2013


Author: emaste
Date: Thu Jul 25 14:10:32 2013
New Revision: 187149

URL: http://llvm.org/viewvc/llvm-project?rev=187149&view=rev
Log:
Set thread names on FreeBSD

Also move the logic to shorten thread names from linux/Host.cpp to a new
SetShortThreadName as both FreeBSD and Linux need the functionality.

Modified:
    lldb/trunk/include/lldb/Host/Host.h
    lldb/trunk/source/Host/common/Host.cpp
    lldb/trunk/source/Host/freebsd/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=187149&r1=187148&r2=187149&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/Host.h (original)
+++ lldb/trunk/include/lldb/Host/Host.h Thu Jul 25 14:10:32 2013
@@ -304,6 +304,27 @@ public:
     SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name);
 
     //------------------------------------------------------------------
+    /// Sets a shortened name of a thread in the current process.
+    ///
+    /// @param[in] pid
+    ///     The process ID in which we are trying to name a thread.
+    ///
+    /// @param[in] tid
+    ///     The thread ID which we are trying to name.
+    ///
+    /// @param[in] name
+    ///     The current thread's name in the current process to \a name.
+    ///
+    /// @param[in] len
+    ///     The maximum length for the thread's shortened name.
+    ///
+    /// @return
+    ///     \b true if the thread name was able to be set, \b false
+    ///     otherwise.
+    static bool
+    SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name, size_t len);
+
+    //------------------------------------------------------------------
     /// Gets the FileSpec of the current process (the process that
     /// that is running the LLDB code).
     ///

Modified: lldb/trunk/source/Host/common/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Host.cpp?rev=187149&r1=187148&r2=187149&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Host.cpp (original)
+++ lldb/trunk/source/Host/common/Host.cpp Thu Jul 25 14:10:32 2013
@@ -648,6 +648,22 @@ Host::SetThreadName (lldb::pid_t pid, ll
             return true;
     }
     return false;
+#elif defined (__FreeBSD__)
+    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;
+
+    // Set the pthread name if possible
+    if (pid == curr_pid && tid == curr_tid)
+    {
+        ::pthread_set_name_np(::pthread_self(), name);
+        return true;
+    }
+    return false;
 #elif defined (__linux__) || defined (__GLIBC__)
     void *fn = dlsym (RTLD_DEFAULT, "pthread_setname_np");
     if (fn)
@@ -676,6 +692,37 @@ Host::SetThreadName (lldb::pid_t pid, ll
 #endif
 }
 
+bool
+Host::SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid,
+                          const char *thread_name, size_t len)
+{
+    char *namebuf = (char *)::malloc (len + 1);
+
+    // 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, len);
+    namebuf[len] = 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;
+        }
+        return Host::SetThreadName (pid, tid, namebuf);
+    }
+    return false;
+}
+
 FileSpec
 Host::GetProgramFileSpec ()
 {

Modified: lldb/trunk/source/Host/freebsd/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/freebsd/Host.cpp?rev=187149&r1=187148&r2=187149&view=diff
==============================================================================
--- lldb/trunk/source/Host/freebsd/Host.cpp (original)
+++ lldb/trunk/source/Host/freebsd/Host.cpp Thu Jul 25 14:10:32 2013
@@ -75,6 +75,8 @@ Host::ThreadCreated (const char *thread_
     {
         ::pthread_setspecific (g_thread_create_key, new FreeBSDThread(thread_name));
     }
+
+    Host::SetShortThreadName (LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID, thread_name, 16);
 }
 
 std::string

Modified: lldb/trunk/source/Host/linux/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/linux/Host.cpp?rev=187149&r1=187148&r2=187149&view=diff
==============================================================================
--- lldb/trunk/source/Host/linux/Host.cpp (original)
+++ lldb/trunk/source/Host/linux/Host.cpp Thu Jul 25 14:10:32 2013
@@ -451,32 +451,7 @@ Host::ThreadCreated (const char *thread_
 {
     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);
-        }
+        Host::SetShortThreadName (LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID, thread_name, 16);
     }
 }
 





More information about the lldb-commits mailing list