diff options
author | Pavel Labath <labath@google.com> | 2017-03-17 09:51:23 +0000 |
---|---|---|
committer | Pavel Labath <labath@google.com> | 2017-03-17 09:51:23 +0000 |
commit | 225b79524de987d810f4085e6a1bf61a289e4adf (patch) | |
tree | ce526121fcde21c1dd730c01c9629b3d3a465d3e | |
parent | c7c9b75804e8cd0f25b421fc5146213dc482e95b (diff) | |
download | bcm5719-llvm-225b79524de987d810f4085e6a1bf61a289e4adf.tar.gz bcm5719-llvm-225b79524de987d810f4085e6a1bf61a289e4adf.zip |
Remove HostThreadLinux/Free/NetBSD
Summary:
These classes existed only because of the GetName() static function,
which can be moved to a more natural place anyway. I move the linux
version to NativeProcessLinux (and get rid of ProcFileReader), the
freebsd version to ProcessFreeBSD (and fix a bug where it was using the
current process ID, instead of the inferior pid), and remove the NetBSD
version (which was probably incorrect anyway, as it assumes the current
process instead of the inferior.
I also add an llgs test to that verifies thread names are read
correctly.
Reviewers: zturner, krytarowski, emaste
Subscribers: lldb-commits, mgorny
Differential Revision: https://reviews.llvm.org/D30981
llvm-svn: 298058
14 files changed, 116 insertions, 265 deletions
diff --git a/lldb/include/lldb/Host/HostNativeThread.h b/lldb/include/lldb/Host/HostNativeThread.h index b0411cad7a3..e33d978d5ae 100644 --- a/lldb/include/lldb/Host/HostNativeThread.h +++ b/lldb/include/lldb/Host/HostNativeThread.h @@ -14,14 +14,10 @@ #if defined(_WIN32) #include "lldb/Host/windows/HostThreadWindows.h" -#elif defined(__linux__) -#include "lldb/Host/linux/HostThreadLinux.h" -#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -#include "lldb/Host/freebsd/HostThreadFreeBSD.h" -#elif defined(__NetBSD__) -#include "lldb/Host/netbsd/HostThreadNetBSD.h" #elif defined(__APPLE__) #include "lldb/Host/macosx/HostThreadMacOSX.h" +#else +#include "lldb/Host/posix/HostThreadPosix.h" #endif #endif diff --git a/lldb/include/lldb/Host/HostNativeThreadForward.h b/lldb/include/lldb/Host/HostNativeThreadForward.h index 5b832136acf..4691a22ac84 100644 --- a/lldb/include/lldb/Host/HostNativeThreadForward.h +++ b/lldb/include/lldb/Host/HostNativeThreadForward.h @@ -14,18 +14,12 @@ namespace lldb_private { #if defined(_WIN32) class HostThreadWindows; typedef HostThreadWindows HostNativeThread; -#elif defined(__linux__) -class HostThreadLinux; -typedef HostThreadLinux HostNativeThread; -#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -class HostThreadFreeBSD; -typedef HostThreadFreeBSD HostNativeThread; -#elif defined(__NetBSD__) -class HostThreadNetBSD; -typedef HostThreadNetBSD HostNativeThread; #elif defined(__APPLE__) class HostThreadMacOSX; typedef HostThreadMacOSX HostNativeThread; +#else +class HostThreadPosix; +typedef HostThreadPosix HostNativeThread; #endif } diff --git a/lldb/include/lldb/Host/freebsd/HostThreadFreeBSD.h b/lldb/include/lldb/Host/freebsd/HostThreadFreeBSD.h deleted file mode 100644 index 2f1d6ceae25..00000000000 --- a/lldb/include/lldb/Host/freebsd/HostThreadFreeBSD.h +++ /dev/null @@ -1,29 +0,0 @@ -//===-- HostThreadFreeBSD.h -------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef lldb_Host_freebsd_HostThreadFreeBSD_h_ -#define lldb_Host_freebsd_HostThreadFreeBSD_h_ - -#include "lldb/Host/posix/HostThreadPosix.h" - -#include "llvm/ADT/SmallString.h" -#include "llvm/ADT/StringRef.h" - -namespace lldb_private { - -class HostThreadFreeBSD : public HostThreadPosix { -public: - HostThreadFreeBSD(); - HostThreadFreeBSD(lldb::thread_t thread); - - static void GetName(lldb::tid_t tid, llvm::SmallVectorImpl<char> &name); -}; -} - -#endif diff --git a/lldb/include/lldb/Host/linux/HostThreadLinux.h b/lldb/include/lldb/Host/linux/HostThreadLinux.h deleted file mode 100644 index 407ee22e635..00000000000 --- a/lldb/include/lldb/Host/linux/HostThreadLinux.h +++ /dev/null @@ -1,29 +0,0 @@ -//===-- HostThreadLinux.h ---------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef lldb_Host_linux_HostThreadLinux_h_ -#define lldb_Host_linux_HostThreadLinux_h_ - -#include "lldb/Host/posix/HostThreadPosix.h" - -#include "llvm/ADT/SmallString.h" -#include "llvm/ADT/StringRef.h" - -namespace lldb_private { - -class HostThreadLinux : public HostThreadPosix { -public: - HostThreadLinux(); - HostThreadLinux(lldb::thread_t thread); - - static void GetName(lldb::thread_t thread, llvm::SmallVectorImpl<char> &name); -}; -} - -#endif diff --git a/lldb/include/lldb/Host/netbsd/HostThreadNetBSD.h b/lldb/include/lldb/Host/netbsd/HostThreadNetBSD.h deleted file mode 100644 index 385f014899f..00000000000 --- a/lldb/include/lldb/Host/netbsd/HostThreadNetBSD.h +++ /dev/null @@ -1,30 +0,0 @@ -//===-- HostThreadNetBSD.h -------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef lldb_Host_netbsd_HostThreadNetBSD_h_ -#define lldb_Host_netbsd_HostThreadNetBSD_h_ - -#include "lldb/Host/posix/HostThreadPosix.h" - -#include "llvm/ADT/SmallString.h" -#include "llvm/ADT/StringRef.h" - -namespace lldb_private { - -class HostThreadNetBSD : public HostThreadPosix { -public: - HostThreadNetBSD(); - HostThreadNetBSD(lldb::thread_t thread); - - static void SetName(lldb::thread_t tid, llvm::StringRef &name); - static void GetName(lldb::thread_t tid, llvm::SmallVectorImpl<char> &name); -}; -} - -#endif diff --git a/lldb/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/Makefile b/lldb/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/Makefile new file mode 100644 index 00000000000..8817fff55e8 --- /dev/null +++ b/lldb/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/Makefile @@ -0,0 +1,6 @@ +LEVEL = ../../../make + +ENABLE_THREADS := YES +CXX_SOURCES := main.cpp + +include $(LEVEL)/Makefile.rules diff --git a/lldb/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/TestGdbRemoteThreadName.py b/lldb/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/TestGdbRemoteThreadName.py new file mode 100644 index 00000000000..5bfcd660c2a --- /dev/null +++ b/lldb/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/TestGdbRemoteThreadName.py @@ -0,0 +1,41 @@ +from __future__ import print_function + +import gdbremote_testcase +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil + + +class TestGdbRemoteThreadName(gdbremote_testcase.GdbRemoteTestCaseBase): + + mydir = TestBase.compute_mydir(__file__) + + def run_and_check_name(self, expected_name): + self.test_sequence.add_log_lines(["read packet: $vCont;c#a8", + {"direction": "send", + "regex": + r"^\$T([0-9a-fA-F]{2})([^#]+)#[0-9a-fA-F]{2}$", + "capture": { + 1: "signal", + 2: "key_vals_text"}}, + ], + True) + + context = self.expect_gdbremote_sequence() + self.assertIsNotNone(context) + + sigint = lldbutil.get_signal_number("SIGINT") + self.assertEqual(sigint, int(context.get("signal"), 16)) + kv_dict = self.parse_key_val_dict(context.get("key_vals_text")) + self.assertEqual(expected_name, kv_dict.get("name")) + + @llgs_test + def test(self): + """ Make sure lldb-server can retrieve inferior thread name""" + self.init_llgs_test() + self.build() + self.set_inferior_startup_launch() + procs = self.prep_debug_monitor_and_inferior() + + self.run_and_check_name("hello world") + self.run_and_check_name("goodbye world") diff --git a/lldb/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/main.cpp b/lldb/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/main.cpp new file mode 100644 index 00000000000..0403031143b --- /dev/null +++ b/lldb/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/main.cpp @@ -0,0 +1,22 @@ +#include <pthread.h> +#include <signal.h> + +void set_thread_name(const char *name) { +#if defined(__APPLE__) + ::pthread_setname_np(name); +#elif defined(__FreeBSD__) + ::pthread_set_name_np(::pthread_self(), name); +#elif defined(__linux__) + ::pthread_setname_np(::pthread_self(), name); +#elif defined(__NetBSD__) + ::pthread_setname_np(::pthread_self(), "%s", name); +#endif +} + +int main() { + set_thread_name("hello world"); + raise(SIGINT); + set_thread_name("goodbye world"); + raise(SIGINT); + return 0; +} diff --git a/lldb/source/Host/CMakeLists.txt b/lldb/source/Host/CMakeLists.txt index e03577f42c9..8cc70abbb47 100644 --- a/lldb/source/Host/CMakeLists.txt +++ b/lldb/source/Host/CMakeLists.txt @@ -119,7 +119,6 @@ else() linux/AbstractSocket.cpp linux/Host.cpp linux/HostInfoLinux.cpp - linux/HostThreadLinux.cpp linux/LibcGlue.cpp linux/Support.cpp ) @@ -134,14 +133,12 @@ else() add_host_subdirectory(freebsd freebsd/Host.cpp freebsd/HostInfoFreeBSD.cpp - freebsd/HostThreadFreeBSD.cpp ) elseif (CMAKE_SYSTEM_NAME MATCHES "NetBSD") add_host_subdirectory(netbsd netbsd/Host.cpp netbsd/HostInfoNetBSD.cpp - netbsd/HostThreadNetBSD.cpp ) endif() endif() diff --git a/lldb/source/Host/freebsd/HostThreadFreeBSD.cpp b/lldb/source/Host/freebsd/HostThreadFreeBSD.cpp deleted file mode 100644 index 97d4d9d05b6..00000000000 --- a/lldb/source/Host/freebsd/HostThreadFreeBSD.cpp +++ /dev/null @@ -1,70 +0,0 @@ -//===-- HostThreadFreeBSD.cpp -----------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -// lldb Includes -#include "lldb/Host/freebsd/HostThreadFreeBSD.h" -#include "lldb/Host/Host.h" - -// C includes -#include <errno.h> -#include <pthread.h> -#if defined(__FreeBSD__) -#include <pthread_np.h> -#endif -#include <stdlib.h> -#include <sys/sysctl.h> -#include <sys/user.h> - -// C++ includes -#include <string> - -using namespace lldb_private; - -HostThreadFreeBSD::HostThreadFreeBSD() {} - -HostThreadFreeBSD::HostThreadFreeBSD(lldb::thread_t thread) - : HostThreadPosix(thread) {} - -void HostThreadFreeBSD::GetName(lldb::tid_t tid, - llvm::SmallVectorImpl<char> &name) { - name.clear(); - int pid = Host::GetCurrentProcessID(); - - struct kinfo_proc *kp = nullptr, *nkp; - size_t len = 0; - int error; - int ctl[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD, - (int)pid}; - - while (1) { - error = sysctl(ctl, 4, kp, &len, nullptr, 0); - if (kp == nullptr || (error != 0 && errno == ENOMEM)) { - // Add extra space in case threads are added before next call. - len += sizeof(*kp) + len / 10; - nkp = (struct kinfo_proc *)realloc(kp, len); - if (nkp == nullptr) { - free(kp); - return; - } - kp = nkp; - continue; - } - if (error != 0) - len = 0; - break; - } - - for (size_t i = 0; i < len / sizeof(*kp); i++) { - if (kp[i].ki_tid == (lwpid_t)tid) { - name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname)); - break; - } - } - free(kp); -} diff --git a/lldb/source/Host/linux/HostThreadLinux.cpp b/lldb/source/Host/linux/HostThreadLinux.cpp deleted file mode 100644 index 7e55a4531be..00000000000 --- a/lldb/source/Host/linux/HostThreadLinux.cpp +++ /dev/null @@ -1,34 +0,0 @@ -//===-- HostThreadLinux.cpp -------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "lldb/Host/linux/HostThreadLinux.h" -#include "Plugins/Process/Linux/ProcFileReader.h" -#include "lldb/Utility/DataBuffer.h" - -#include "llvm/ADT/SmallVector.h" - -using namespace lldb_private; - -HostThreadLinux::HostThreadLinux() : HostThreadPosix() {} - -HostThreadLinux::HostThreadLinux(lldb::thread_t thread) - : HostThreadPosix(thread) {} - -void HostThreadLinux::GetName(lldb::thread_t thread, - llvm::SmallVectorImpl<char> &name) { - // Read /proc/$TID/comm file. - lldb::DataBufferSP buf_sp = - process_linux::ProcFileReader::ReadIntoDataBuffer(thread, "comm"); - const char *comm_str = (const char *)buf_sp->GetBytes(); - const char *cr_str = ::strchr(comm_str, '\n'); - size_t length = cr_str ? (cr_str - comm_str) : strlen(comm_str); - - name.clear(); - name.append(comm_str, comm_str + length); -} diff --git a/lldb/source/Host/netbsd/HostThreadNetBSD.cpp b/lldb/source/Host/netbsd/HostThreadNetBSD.cpp deleted file mode 100644 index e8c106b7f22..00000000000 --- a/lldb/source/Host/netbsd/HostThreadNetBSD.cpp +++ /dev/null @@ -1,43 +0,0 @@ -//===-- HostThreadNetBSD.cpp -----------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -// lldb Includes -#include "lldb/Host/netbsd/HostThreadNetBSD.h" -#include "lldb/Host/Host.h" - -// C includes -#include <errno.h> -#include <pthread.h> -#include <stdlib.h> -#include <string.h> -#include <sys/sysctl.h> -#include <sys/user.h> - -// C++ includes -#include <string> - -using namespace lldb_private; - -HostThreadNetBSD::HostThreadNetBSD() {} - -HostThreadNetBSD::HostThreadNetBSD(lldb::thread_t thread) - : HostThreadPosix(thread) {} - -void HostThreadNetBSD::SetName(lldb::thread_t thread, llvm::StringRef &name) { - ::pthread_setname_np(thread, "%s", const_cast<char *>(name.data())); -} - -void HostThreadNetBSD::GetName(lldb::thread_t thread, - llvm::SmallVectorImpl<char> &name) { - char buf[PTHREAD_MAX_NAMELEN_NP]; - ::pthread_getname_np(thread, buf, PTHREAD_MAX_NAMELEN_NP); - - name.clear(); - name.append(buf, buf + strlen(buf)); -} diff --git a/lldb/source/Plugins/Process/FreeBSD/FreeBSDThread.cpp b/lldb/source/Plugins/Process/FreeBSD/FreeBSDThread.cpp index 91a1bc989d9..6e9cc9c13b0 100644 --- a/lldb/source/Plugins/Process/FreeBSD/FreeBSDThread.cpp +++ b/lldb/source/Plugins/Process/FreeBSD/FreeBSDThread.cpp @@ -113,9 +113,41 @@ void FreeBSDThread::SetName(const char *name) { const char *FreeBSDThread::GetName() { if (!m_thread_name_valid) { - llvm::SmallString<32> thread_name; - HostNativeThread::GetName(GetID(), thread_name); - m_thread_name = thread_name.c_str(); + m_thread_name.clear(); + int pid = GetProcess()->GetID(); + + struct kinfo_proc *kp = nullptr, *nkp; + size_t len = 0; + int error; + int ctl[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD, + pid}; + + while (1) { + error = sysctl(ctl, 4, kp, &len, nullptr, 0); + if (kp == nullptr || (error != 0 && errno == ENOMEM)) { + // Add extra space in case threads are added before next call. + len += sizeof(*kp) + len / 10; + nkp = (struct kinfo_proc *)realloc(kp, len); + if (nkp == nullptr) { + free(kp); + return nullptr; + } + kp = nkp; + continue; + } + if (error != 0) + len = 0; + break; + } + + for (size_t i = 0; i < len / sizeof(*kp); i++) { + if (kp[i].ki_tid == (lwpid_t)tid) { + m_thread_name.append(kp[i].ki_tdname, + kp[i].ki_tdname + strlen(kp[i].ki_tdname)); + break; + } + } + free(kp); m_thread_name_valid = true; } diff --git a/lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp b/lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp index f97816e18b3..04b6fe6d71e 100644 --- a/lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp +++ b/lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp @@ -19,6 +19,7 @@ #include "lldb/Core/State.h" #include "lldb/Host/HostNativeThread.h" #include "lldb/Host/linux/Ptrace.h" +#include "lldb/Host/linux/Support.h" #include "lldb/Utility/LLDBAssert.h" #include "lldb/Utility/Log.h" #include "lldb/lldb-enumerations.h" @@ -90,15 +91,12 @@ NativeThreadLinux::NativeThreadLinux(NativeProcessLinux *process, m_stop_info(), m_reg_context_sp(), m_stop_description() {} std::string NativeThreadLinux::GetName() { - NativeProcessProtocolSP process_sp = m_process_wp.lock(); - if (!process_sp) - return "<unknown: no process>"; - - // const NativeProcessLinux *const process = - // reinterpret_cast<NativeProcessLinux*> (process_sp->get ()); - llvm::SmallString<32> thread_name; - HostNativeThread::GetName(GetID(), thread_name); - return thread_name.c_str(); + NativeProcessLinux &process = GetProcess(); + + auto BufferOrError = getProcFile(process.GetID(), GetID(), "comm"); + if (!BufferOrError) + return ""; + return BufferOrError.get()->getBuffer().rtrim('\n'); } lldb::StateType NativeThreadLinux::GetState() { return m_state; } |