From 9f822cd1ec166a777d9d20f0595abe99844b0a60 Mon Sep 17 00:00:00 2001 From: Michael Sartain Date: Wed, 31 Jul 2013 23:27:46 +0000 Subject: [PATCH] Fix thread name updating in Linux. "thread list" should report correct names always now. Created new LinuxThread class inherited from POSIXThread and removed linux / freebsd ifdefs Removed several un-needed set thread name calls CR (and multiple suggestions): mkopec llvm-svn: 187545 --- .../Plugins/Process/FreeBSD/ProcessMonitor.cpp | 4 +- lldb/source/Plugins/Process/Linux/CMakeLists.txt | 1 + lldb/source/Plugins/Process/Linux/LinuxThread.cpp | 63 ++++++++++++++++++++++ lldb/source/Plugins/Process/Linux/LinuxThread.h | 43 +++++++++++++++ lldb/source/Plugins/Process/Linux/ProcessLinux.cpp | 12 ++++- lldb/source/Plugins/Process/Linux/ProcessLinux.h | 4 ++ .../Plugins/Process/Linux/ProcessMonitor.cpp | 12 +---- lldb/source/Plugins/Process/POSIX/POSIXThread.cpp | 26 ++++----- lldb/source/Plugins/Process/POSIX/POSIXThread.h | 5 +- lldb/source/Plugins/Process/POSIX/ProcessPOSIX.cpp | 13 +++-- lldb/source/Plugins/Process/POSIX/ProcessPOSIX.h | 4 ++ lldb/source/Target/TargetList.cpp | 17 ++++-- 12 files changed, 163 insertions(+), 41 deletions(-) create mode 100644 lldb/source/Plugins/Process/Linux/LinuxThread.cpp create mode 100644 lldb/source/Plugins/Process/Linux/LinuxThread.h diff --git a/lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp b/lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp index d16ad9a..9fd51d2 100644 --- a/lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp +++ b/lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp @@ -1003,7 +1003,7 @@ ProcessMonitor::Launch(LaunchArgs *args) goto FINISH; // Update the process thread list with this new thread. - inferior.reset(new POSIXThread(*processSP, pid)); + inferior.reset(process.CreateNewPOSIXThread(*processSP, pid)); if (log) log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid); process.GetThreadList().AddThread(inferior); @@ -1085,7 +1085,7 @@ ProcessMonitor::Attach(AttachArgs *args) } // Update the process thread list with the attached thread. - inferior.reset(new POSIXThread(*processSP, pid)); + inferior.reset(process.CreateNewPOSIXThread(*processSP, pid)); tl.AddThread(inferior); // Let our process instance know the thread has stopped. diff --git a/lldb/source/Plugins/Process/Linux/CMakeLists.txt b/lldb/source/Plugins/Process/Linux/CMakeLists.txt index 96ccb4d..221a682 100644 --- a/lldb/source/Plugins/Process/Linux/CMakeLists.txt +++ b/lldb/source/Plugins/Process/Linux/CMakeLists.txt @@ -7,5 +7,6 @@ add_lldb_library(lldbPluginProcessLinux ProcessLinux.cpp ProcessMonitor.cpp LinuxSignals.cpp + LinuxThread.cpp ) diff --git a/lldb/source/Plugins/Process/Linux/LinuxThread.cpp b/lldb/source/Plugins/Process/Linux/LinuxThread.cpp new file mode 100644 index 0000000..edb21bd --- /dev/null +++ b/lldb/source/Plugins/Process/Linux/LinuxThread.cpp @@ -0,0 +1,63 @@ +//===-- LinuxThread.cpp -----------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// C Includes +// C++ Includes +// Other libraries and framework includes +// Project includes +#include "LinuxThread.h" + +using namespace lldb; +using namespace lldb_private; + +//------------------------------------------------------------------------------ +// Constructors and destructors. + +LinuxThread::LinuxThread(Process &process, lldb::tid_t tid) + : POSIXThread(process, tid) +{ +} + +LinuxThread::~LinuxThread() +{ +} + +//------------------------------------------------------------------------------ +// ProcessInterface protocol. + +void +LinuxThread::RefreshStateAfterStop() +{ + // Invalidate the thread names every time we get a stop event on Linux so we + // will re-read the procfs comm virtual file when folks ask for the thread name. + m_thread_name_valid = false; + + POSIXThread::RefreshStateAfterStop(); +} + +void +LinuxThread::TraceNotify(const ProcessMessage &message) +{ + RegisterContextPOSIX* reg_ctx = GetRegisterContextPOSIX(); + if (reg_ctx) + { + uint32_t num_hw_wps = reg_ctx->NumSupportedHardwareWatchpoints(); + uint32_t wp_idx; + for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++) + { + if (reg_ctx->IsWatchpointHit(wp_idx)) + { + WatchNotify(message); + return; + } + } + } + + POSIXThread::TraceNotify (message); +} \ No newline at end of file diff --git a/lldb/source/Plugins/Process/Linux/LinuxThread.h b/lldb/source/Plugins/Process/Linux/LinuxThread.h new file mode 100644 index 0000000..7d253aa --- /dev/null +++ b/lldb/source/Plugins/Process/Linux/LinuxThread.h @@ -0,0 +1,43 @@ +//===-- LinuxThread.h -------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_LinuxThread_H_ +#define liblldb_LinuxThread_H_ + +// Other libraries and framework includes +#include "POSIXThread.h" + +//------------------------------------------------------------------------------ +// @class LinuxThread +// @brief Abstraction of a Linux thread. +class LinuxThread + : public POSIXThread +{ +public: + + //------------------------------------------------------------------ + // Constructors and destructors + //------------------------------------------------------------------ + LinuxThread(lldb_private::Process &process, lldb::tid_t tid); + + virtual ~LinuxThread(); + + //-------------------------------------------------------------------------- + // LinuxThread internal API. + + // POSIXThread override + virtual void + RefreshStateAfterStop(); + +protected: + virtual void + TraceNotify(const ProcessMessage &message); +}; + +#endif // #ifndef liblldb_LinuxThread_H_ diff --git a/lldb/source/Plugins/Process/Linux/ProcessLinux.cpp b/lldb/source/Plugins/Process/Linux/ProcessLinux.cpp index c53a2eb..e503ff0 100644 --- a/lldb/source/Plugins/Process/Linux/ProcessLinux.cpp +++ b/lldb/source/Plugins/Process/Linux/ProcessLinux.cpp @@ -23,7 +23,7 @@ #include "ProcessPOSIXLog.h" #include "Plugins/Process/Utility/InferiorCallPOSIX.h" #include "ProcessMonitor.h" -#include "POSIXThread.h" +#include "LinuxThread.h" using namespace lldb; using namespace lldb_private; @@ -64,7 +64,7 @@ ProcessLinux::Initialize() // Constructors and destructors. ProcessLinux::ProcessLinux(Target& target, Listener &listener, FileSpec *core_file) - : ProcessPOSIX(target, listener), m_stopping_threads(false), m_core_file(core_file) + : ProcessPOSIX(target, listener), m_core_file(core_file), m_stopping_threads(false) { #if 0 // FIXME: Putting this code in the ctor and saving the byte order in a @@ -81,6 +81,7 @@ void ProcessLinux::Terminate() { } + lldb_private::ConstString ProcessLinux::GetPluginNameStatic() { @@ -171,6 +172,13 @@ ProcessLinux::StopAllThreads(lldb::tid_t stop_tid) log->Printf ("ProcessLinux::%s() finished", __FUNCTION__); } +// ProcessPOSIX override +POSIXThread * +ProcessLinux::CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid) +{ + return new LinuxThread(process, tid); +} + bool ProcessLinux::CanDebug(Target &target, bool plugin_specified_by_name) { diff --git a/lldb/source/Plugins/Process/Linux/ProcessLinux.h b/lldb/source/Plugins/Process/Linux/ProcessLinux.h index c651351..742627f 100644 --- a/lldb/source/Plugins/Process/Linux/ProcessLinux.h +++ b/lldb/source/Plugins/Process/Linux/ProcessLinux.h @@ -56,6 +56,7 @@ public: virtual bool UpdateThreadList(lldb_private::ThreadList &old_thread_list, lldb_private::ThreadList &new_thread_list); + //------------------------------------------------------------------ // PluginInterface protocol //------------------------------------------------------------------ @@ -94,6 +95,9 @@ public: virtual void StopAllThreads(lldb::tid_t stop_tid); + virtual POSIXThread * + CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid); + private: /// Linux-specific signal set. diff --git a/lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp b/lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp index cfb816a..e52d07c 100644 --- a/lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp +++ b/lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp @@ -1098,7 +1098,6 @@ 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,10 +1231,7 @@ ProcessMonitor::Launch(LaunchArgs *args) // Update the process thread list with this new thread. // 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(inferior.get()); - thread->SetName(Host::GetThreadName(pid, pid).c_str()); + inferior.reset(process.CreateNewPOSIXThread(process, pid)); if (log) log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid); @@ -1297,7 +1293,6 @@ 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. @@ -1362,10 +1357,7 @@ ProcessMonitor::Attach(AttachArgs *args) } // Update the process thread list with the attached thread. - inferior.reset(new POSIXThread(process, tid)); - - thread = static_cast(inferior.get()); - thread->SetName(Host::GetThreadName(pid, tid).c_str()); + inferior.reset(process.CreateNewPOSIXThread(process, tid)); if (log) log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid); diff --git a/lldb/source/Plugins/Process/POSIX/POSIXThread.cpp b/lldb/source/Plugins/Process/POSIX/POSIXThread.cpp index a5cccee..93c2966 100644 --- a/lldb/source/Plugins/Process/POSIX/POSIXThread.cpp +++ b/lldb/source/Plugins/Process/POSIX/POSIXThread.cpp @@ -45,6 +45,7 @@ POSIXThread::POSIXThread(Process &process, lldb::tid_t tid) : Thread(process, tid), m_frame_ap (), m_breakpoint (), + m_thread_name_valid (false), m_thread_name () { Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD)); @@ -111,7 +112,8 @@ POSIXThread::GetInfo() void POSIXThread::SetName (const char *name) { - if (name && name[0]) + m_thread_name_valid = (name && name[0]); + if (m_thread_name_valid) m_thread_name.assign (name); else m_thread_name.clear(); @@ -120,6 +122,12 @@ POSIXThread::SetName (const char *name) const char * POSIXThread::GetName () { + if (!m_thread_name_valid) + { + SetName(Host::GetThreadName(GetProcess()->GetID(), GetID()).c_str()); + m_thread_name_valid = true; + } + if (m_thread_name.empty()) return NULL; return m_thread_name.c_str(); @@ -460,22 +468,6 @@ POSIXThread::WatchNotify(const ProcessMessage &message) void POSIXThread::TraceNotify(const ProcessMessage &message) { -#ifndef __FreeBSD__ - RegisterContextPOSIX* reg_ctx = GetRegisterContextPOSIX(); - if (reg_ctx) - { - uint32_t num_hw_wps = reg_ctx->NumSupportedHardwareWatchpoints(); - uint32_t wp_idx; - for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++) - { - if (reg_ctx->IsWatchpointHit(wp_idx)) - { - WatchNotify(message); - return; - } - } - } -#endif SetStopInfo (StopInfo::CreateStopReasonToTrace(*this)); } diff --git a/lldb/source/Plugins/Process/POSIX/POSIXThread.h b/lldb/source/Plugins/Process/POSIX/POSIXThread.h index 59b02a7..d051d23 100644 --- a/lldb/source/Plugins/Process/POSIX/POSIXThread.h +++ b/lldb/source/Plugins/Process/POSIX/POSIXThread.h @@ -91,7 +91,7 @@ public: uint32_t FindVacantWatchpointIndex(); -private: +protected: RegisterContextPOSIX * GetRegisterContextPOSIX () { @@ -107,6 +107,7 @@ private: lldb::BreakpointSiteSP m_breakpoint; + bool m_thread_name_valid; std::string m_thread_name; ProcessMonitor & @@ -117,7 +118,7 @@ private: void BreakNotify(const ProcessMessage &message); void WatchNotify(const ProcessMessage &message); - void TraceNotify(const ProcessMessage &message); + virtual void TraceNotify(const ProcessMessage &message); void LimboNotify(const ProcessMessage &message); void SignalNotify(const ProcessMessage &message); void SignalDeliveredNotify(const ProcessMessage &message); diff --git a/lldb/source/Plugins/Process/POSIX/ProcessPOSIX.cpp b/lldb/source/Plugins/Process/POSIX/ProcessPOSIX.cpp index 1cba5f1..545e792 100644 --- a/lldb/source/Plugins/Process/POSIX/ProcessPOSIX.cpp +++ b/lldb/source/Plugins/Process/POSIX/ProcessPOSIX.cpp @@ -479,6 +479,12 @@ ProcessPOSIX::AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid) return added_to_set; } +POSIXThread * +ProcessPOSIX::CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid) +{ + return new POSIXThread(process, tid); +} + void ProcessPOSIX::RefreshStateAfterStop() { @@ -508,10 +514,7 @@ ProcessPOSIX::RefreshStateAfterStop() 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, child_tid)); - - POSIXThread *thread = static_cast(thread_sp.get()); - thread->SetName(Host::GetThreadName(GetID(), child_tid).c_str()); + thread_sp.reset(CreateNewPOSIXThread(*this, child_tid)); Mutex::Locker lock(m_thread_list.GetMutex()); @@ -821,7 +824,7 @@ ProcessPOSIX::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thre assert(m_monitor); ThreadSP thread_sp (old_thread_list.FindThreadByID (GetID(), false)); if (!thread_sp) { - thread_sp.reset(new POSIXThread(*this, GetID())); + thread_sp.reset(CreateNewPOSIXThread(*this, GetID())); } if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) diff --git a/lldb/source/Plugins/Process/POSIX/ProcessPOSIX.h b/lldb/source/Plugins/Process/POSIX/ProcessPOSIX.h index fd029ef..48b19ba 100644 --- a/lldb/source/Plugins/Process/POSIX/ProcessPOSIX.h +++ b/lldb/source/Plugins/Process/POSIX/ProcessPOSIX.h @@ -22,6 +22,7 @@ #include "ProcessMessage.h" class ProcessMonitor; +class POSIXThread; class ProcessPOSIX : public lldb_private::Process @@ -166,6 +167,9 @@ public: bool AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid); + virtual POSIXThread * + CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid); + protected: /// Target byte order. lldb::ByteOrder m_byte_order; diff --git a/lldb/source/Target/TargetList.cpp b/lldb/source/Target/TargetList.cpp index 5bd6ea9..8d907f4 100644 --- a/lldb/source/Target/TargetList.cpp +++ b/lldb/source/Target/TargetList.cpp @@ -222,8 +222,19 @@ TargetList::CreateTarget (Debugger &debugger, FileSpec file (user_exe_path, false); if (!file.Exists() && user_exe_path && user_exe_path[0] == '~') { - file = FileSpec(user_exe_path, true); + // we want to expand the tilde but we don't want to resolve any symbolic links + // so we can't use the FileSpec constructor's resolve flag + char unglobbed_path[PATH_MAX]; + unglobbed_path[0] = '\0'; + + size_t return_count = FileSpec::ResolveUsername(user_exe_path, unglobbed_path, sizeof(unglobbed_path)); + + if (return_count == 0 || return_count >= sizeof(unglobbed_path)) + ::snprintf (unglobbed_path, sizeof(unglobbed_path), "%s", user_exe_path); + + file = FileSpec(unglobbed_path, false); } + bool user_exe_path_is_bundle = false; char resolved_bundle_exe_path[PATH_MAX]; resolved_bundle_exe_path[0] = '\0'; @@ -305,8 +316,8 @@ TargetList::CreateTarget (Debugger &debugger, } else { - // Just use what the user typed - target_sp->SetArg0 (user_exe_path); + // Use resolved path + target_sp->SetArg0 (file.GetPath().c_str()); } } if (file.GetDirectory()) -- 2.7.4