Index: lldb/trunk/include/lldb/Host/HostNativeThread.h =================================================================== --- lldb/trunk/include/lldb/Host/HostNativeThread.h +++ lldb/trunk/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 Index: lldb/trunk/include/lldb/Host/HostNativeThreadForward.h =================================================================== --- lldb/trunk/include/lldb/Host/HostNativeThreadForward.h +++ lldb/trunk/include/lldb/Host/HostNativeThreadForward.h @@ -14,18 +14,12 @@ #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 } Index: lldb/trunk/include/lldb/Host/freebsd/HostThreadFreeBSD.h =================================================================== --- lldb/trunk/include/lldb/Host/freebsd/HostThreadFreeBSD.h +++ lldb/trunk/include/lldb/Host/freebsd/HostThreadFreeBSD.h @@ -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 &name); -}; -} - -#endif Index: lldb/trunk/include/lldb/Host/linux/HostThreadLinux.h =================================================================== --- lldb/trunk/include/lldb/Host/linux/HostThreadLinux.h +++ lldb/trunk/include/lldb/Host/linux/HostThreadLinux.h @@ -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 &name); -}; -} - -#endif Index: lldb/trunk/include/lldb/Host/netbsd/HostThreadNetBSD.h =================================================================== --- lldb/trunk/include/lldb/Host/netbsd/HostThreadNetBSD.h +++ lldb/trunk/include/lldb/Host/netbsd/HostThreadNetBSD.h @@ -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 &name); -}; -} - -#endif Index: lldb/trunk/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/Makefile =================================================================== --- lldb/trunk/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/Makefile +++ lldb/trunk/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 Index: lldb/trunk/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/TestGdbRemoteThreadName.py =================================================================== --- lldb/trunk/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/TestGdbRemoteThreadName.py +++ lldb/trunk/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") Index: lldb/trunk/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/main.cpp =================================================================== --- lldb/trunk/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/main.cpp +++ lldb/trunk/packages/Python/lldbsuite/test/tools/lldb-server/thread-name/main.cpp @@ -0,0 +1,22 @@ +#include +#include + +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; +} Index: lldb/trunk/source/Host/CMakeLists.txt =================================================================== --- lldb/trunk/source/Host/CMakeLists.txt +++ lldb/trunk/source/Host/CMakeLists.txt @@ -119,7 +119,6 @@ linux/AbstractSocket.cpp linux/Host.cpp linux/HostInfoLinux.cpp - linux/HostThreadLinux.cpp linux/LibcGlue.cpp linux/Support.cpp ) @@ -134,14 +133,12 @@ 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() Index: lldb/trunk/source/Host/freebsd/HostThreadFreeBSD.cpp =================================================================== --- lldb/trunk/source/Host/freebsd/HostThreadFreeBSD.cpp +++ lldb/trunk/source/Host/freebsd/HostThreadFreeBSD.cpp @@ -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 -#include -#if defined(__FreeBSD__) -#include -#endif -#include -#include -#include - -// C++ includes -#include - -using namespace lldb_private; - -HostThreadFreeBSD::HostThreadFreeBSD() {} - -HostThreadFreeBSD::HostThreadFreeBSD(lldb::thread_t thread) - : HostThreadPosix(thread) {} - -void HostThreadFreeBSD::GetName(lldb::tid_t tid, - llvm::SmallVectorImpl &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); -} Index: lldb/trunk/source/Host/linux/HostThreadLinux.cpp =================================================================== --- lldb/trunk/source/Host/linux/HostThreadLinux.cpp +++ lldb/trunk/source/Host/linux/HostThreadLinux.cpp @@ -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 &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); -} Index: lldb/trunk/source/Host/netbsd/HostThreadNetBSD.cpp =================================================================== --- lldb/trunk/source/Host/netbsd/HostThreadNetBSD.cpp +++ lldb/trunk/source/Host/netbsd/HostThreadNetBSD.cpp @@ -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 -#include -#include -#include -#include -#include - -// C++ includes -#include - -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(name.data())); -} - -void HostThreadNetBSD::GetName(lldb::thread_t thread, - llvm::SmallVectorImpl &name) { - char buf[PTHREAD_MAX_NAMELEN_NP]; - ::pthread_getname_np(thread, buf, PTHREAD_MAX_NAMELEN_NP); - - name.clear(); - name.append(buf, buf + strlen(buf)); -} Index: lldb/trunk/source/Plugins/Process/FreeBSD/FreeBSDThread.cpp =================================================================== --- lldb/trunk/source/Plugins/Process/FreeBSD/FreeBSDThread.cpp +++ lldb/trunk/source/Plugins/Process/FreeBSD/FreeBSDThread.cpp @@ -113,9 +113,41 @@ 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; } Index: lldb/trunk/source/Plugins/Process/Linux/NativeThreadLinux.cpp =================================================================== --- lldb/trunk/source/Plugins/Process/Linux/NativeThreadLinux.cpp +++ lldb/trunk/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 @@ 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 ""; - - // const NativeProcessLinux *const process = - // reinterpret_cast (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; }