[Lldb-commits] [lldb] r186033 - Add support for listing inferior thread names on Linux.

Matt Kopec Matt.Kopec at intel.com
Wed Jul 10 13:53:11 PDT 2013


Author: mkopec
Date: Wed Jul 10 15:53:11 2013
New Revision: 186033

URL: http://llvm.org/viewvc/llvm-project?rev=186033&view=rev
Log:
Add support for listing inferior thread names on Linux.

Modified:
    lldb/trunk/source/Host/common/Host.cpp
    lldb/trunk/source/Host/linux/Host.cpp
    lldb/trunk/source/Host/macosx/Host.mm
    lldb/trunk/source/Plugins/Process/Linux/ProcessMonitor.cpp
    lldb/trunk/source/Plugins/Process/POSIX/POSIXThread.cpp
    lldb/trunk/source/Plugins/Process/POSIX/POSIXThread.h
    lldb/trunk/source/Plugins/Process/POSIX/ProcessPOSIX.cpp

Modified: lldb/trunk/source/Host/common/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Host.cpp?rev=186033&r1=186032&r2=186033&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Host.cpp (original)
+++ lldb/trunk/source/Host/common/Host.cpp Wed Jul 10 15:53:11 2013
@@ -629,40 +629,6 @@ Host::ThreadJoin (lldb::thread_t thread,
     return err == 0;
 }
 
-
-std::string
-Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
-{
-    std::string thread_name;
-#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
-    // We currently can only get the name of a thread in the current process.
-    if (pid == Host::GetCurrentProcessID())
-    {
-        char pthread_name[1024];
-        if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
-        {
-            if (pthread_name[0])
-            {
-                thread_name = pthread_name;
-            }
-        }
-        else
-        {
-            dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
-            if (current_queue != NULL)
-            {
-                const char *queue_name = dispatch_queue_get_label (current_queue);
-                if (queue_name && queue_name[0])
-                {
-                    thread_name = queue_name;
-                }
-            }
-        }
-    }
-#endif
-    return thread_name;
-}
-
 bool
 Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
 {

Modified: lldb/trunk/source/Host/linux/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/linux/Host.cpp?rev=186033&r1=186032&r2=186033&view=diff
==============================================================================
--- lldb/trunk/source/Host/linux/Host.cpp (original)
+++ lldb/trunk/source/Host/linux/Host.cpp Wed Jul 10 15:53:11 2013
@@ -104,6 +104,13 @@ ReadProcPseudoFile (lldb::pid_t pid, con
     return buf_sp;
 }
 
+lldb::DataBufferSP
+ReadProcPseudoFile (lldb::pid_t pid, lldb::tid_t tid, const char *name)
+{
+    std::string process_thread_pseudo_file = "task/" + std::to_string(tid) + "/" + name;
+    return ReadProcPseudoFile(pid, process_thread_pseudo_file.c_str());
+}
+
 } // anonymous namespace
 
 static bool
@@ -472,6 +479,33 @@ Host::ThreadCreated (const char *thread_
     }
 }
 
+std::string
+Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
+{
+    const size_t thread_name_max_size = 16;
+    char pthread_name[thread_name_max_size];
+    std::string thread_name;
+    // Read the /proc/$PID/stat file.
+    lldb::DataBufferSP buf_sp = ReadProcPseudoFile (pid, tid, "stat");
+
+    // The file/thread name of the executable is stored in parenthesis. Search for the first
+    // '(' and last ')' and copy everything in between.
+    const char *filename_start = ::strchr ((const char *)buf_sp->GetBytes(), '(');
+    const char *filename_end = ::strrchr ((const char *)buf_sp->GetBytes(), ')');
+
+    if (filename_start && filename_end)
+    {
+        ++filename_start;
+        size_t length = filename_end - filename_start;
+        if (length > thread_name_max_size)
+            length = thread_name_max_size;
+        strncpy(pthread_name, filename_start, length);
+        thread_name = std::string(pthread_name, length);
+    }
+
+    return thread_name;
+}
+
 void
 Host::Backtrace (Stream &strm, uint32_t max_frames)
 {

Modified: lldb/trunk/source/Host/macosx/Host.mm
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/macosx/Host.mm?rev=186033&r1=186032&r2=186033&view=diff
==============================================================================
--- lldb/trunk/source/Host/macosx/Host.mm (original)
+++ lldb/trunk/source/Host/macosx/Host.mm Wed Jul 10 15:53:11 2013
@@ -148,6 +148,39 @@ Host::ThreadCreated (const char *thread_
     }
 }
 
+std::string
+Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
+{
+    std::string thread_name;
+#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
+    // We currently can only get the name of a thread in the current process.
+    if (pid == Host::GetCurrentProcessID())
+    {
+        char pthread_name[1024];
+        if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0)
+        {
+            if (pthread_name[0])
+            {
+                thread_name = pthread_name;
+            }
+        }
+        else
+        {
+            dispatch_queue_t current_queue = ::dispatch_get_current_queue ();
+            if (current_queue != NULL)
+            {
+                const char *queue_name = dispatch_queue_get_label (current_queue);
+                if (queue_name && queue_name[0])
+                {
+                    thread_name = queue_name;
+                }
+            }
+        }
+    }
+#endif
+    return thread_name;
+}
+
 bool
 Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle_directory)
 {

Modified: lldb/trunk/source/Plugins/Process/Linux/ProcessMonitor.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/Linux/ProcessMonitor.cpp?rev=186033&r1=186032&r2=186033&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/Linux/ProcessMonitor.cpp (original)
+++ lldb/trunk/source/Plugins/Process/Linux/ProcessMonitor.cpp Wed Jul 10 15:53:11 2013
@@ -1098,6 +1098,7 @@ ProcessMonitor::Launch(LaunchArgs *args)
     lldb::pid_t pid;
 
     lldb::ThreadSP inferior;
+    POSIXThread *thread;
     Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
 
     // Propagate the environment if one is not supplied.
@@ -1232,6 +1233,10 @@ ProcessMonitor::Launch(LaunchArgs *args)
     // FIXME: should we be letting UpdateThreadList handle this?
     // FIXME: by using pids instead of tids, we can only support one thread.
     inferior.reset(new POSIXThread(process, pid));
+
+    thread = static_cast<POSIXThread*>(inferior.get());
+    thread->SetName(Host::GetThreadName(pid, pid).c_str());
+
     if (log)
         log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
     process.GetThreadList().AddThread(inferior);
@@ -1292,6 +1297,7 @@ ProcessMonitor::Attach(AttachArgs *args)
     ProcessMonitor *monitor = args->m_monitor;
     ProcessLinux &process = monitor->GetProcess();
     lldb::ThreadSP inferior;
+    POSIXThread *thread;
     Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
 
     // Use a map to keep track of the threads which we have attached/need to attach.
@@ -1357,6 +1363,10 @@ ProcessMonitor::Attach(AttachArgs *args)
 
                 // Update the process thread list with the attached thread.
                 inferior.reset(new POSIXThread(process, tid));
+
+                thread = static_cast<POSIXThread*>(inferior.get());
+                thread->SetName(Host::GetThreadName(pid, tid).c_str());
+
                 if (log)
                     log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
                 process.GetThreadList().AddThread(inferior);

Modified: lldb/trunk/source/Plugins/Process/POSIX/POSIXThread.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/POSIX/POSIXThread.cpp?rev=186033&r1=186032&r2=186033&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/POSIX/POSIXThread.cpp (original)
+++ lldb/trunk/source/Plugins/Process/POSIX/POSIXThread.cpp Wed Jul 10 15:53:11 2013
@@ -41,7 +41,9 @@ using namespace lldb_private;
 
 POSIXThread::POSIXThread(Process &process, lldb::tid_t tid)
     : Thread(process, tid),
-      m_frame_ap()
+      m_frame_ap (),
+      m_breakpoint (),
+      m_thread_name ()
 {
     Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
     if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
@@ -104,6 +106,23 @@ POSIXThread::GetInfo()
     return NULL;
 }
 
+void
+POSIXThread::SetName (const char *name)
+{
+    if (name && name[0])
+        m_thread_name.assign (name);
+    else
+        m_thread_name.clear();
+}
+
+const char *
+POSIXThread::GetName ()
+{
+    if (m_thread_name.empty())
+        return NULL;
+    return m_thread_name.c_str();
+}
+
 lldb::RegisterContextSP
 POSIXThread::GetRegisterContext()
 {

Modified: lldb/trunk/source/Plugins/Process/POSIX/POSIXThread.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/POSIX/POSIXThread.h?rev=186033&r1=186032&r2=186033&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/POSIX/POSIXThread.h (original)
+++ lldb/trunk/source/Plugins/Process/POSIX/POSIXThread.h Wed Jul 10 15:53:11 2013
@@ -13,6 +13,7 @@
 // C Includes
 // C++ Includes
 #include <memory>
+#include <string>
 
 // Other libraries and framework includes
 #include "lldb/Target/Thread.h"
@@ -46,6 +47,12 @@ public:
     const char *
     GetInfo();
 
+    void
+    SetName (const char *name);
+
+    const char *
+    GetName ();
+
     virtual lldb::RegisterContextSP
     GetRegisterContext();
 
@@ -100,6 +107,8 @@ private:
 
     lldb::BreakpointSiteSP m_breakpoint;
 
+    std::string m_thread_name;
+
     ProcessMonitor &
     GetMonitor();
 

Modified: lldb/trunk/source/Plugins/Process/POSIX/ProcessPOSIX.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/POSIX/ProcessPOSIX.cpp?rev=186033&r1=186032&r2=186033&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/POSIX/ProcessPOSIX.cpp (original)
+++ lldb/trunk/source/Plugins/Process/POSIX/ProcessPOSIX.cpp Wed Jul 10 15:53:11 2013
@@ -491,8 +491,13 @@ ProcessPOSIX::RefreshStateAfterStop()
         {
             if (log)
                 log->Printf ("ProcessPOSIX::%s() adding thread, tid = %" PRIi64, __FUNCTION__, message.GetChildTID());
+            lldb::tid_t child_tid = message.GetChildTID();
             ThreadSP thread_sp;
-            thread_sp.reset(new POSIXThread(*this, message.GetChildTID()));
+            thread_sp.reset(new POSIXThread(*this, child_tid));
+
+            POSIXThread *thread = static_cast<POSIXThread*>(thread_sp.get());
+            thread->SetName(Host::GetThreadName(GetID(), child_tid).c_str());
+
             m_thread_list.AddThread(thread_sp);
         }
 





More information about the lldb-commits mailing list