Index: include/lldb/Core/DataBuffer.h =================================================================== --- include/lldb/Core/DataBuffer.h +++ include/lldb/Core/DataBuffer.h @@ -14,6 +14,8 @@ #include #include +#include "lldb/lldb-types.h" + namespace lldb_private { //---------------------------------------------------------------------- Index: include/lldb/Host/HostThread.h =================================================================== --- /dev/null +++ include/lldb/Host/HostThread.h @@ -0,0 +1,51 @@ +//===-- HostThread.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_HostThread_h_ +#define lldb_Host_HostThread_h_ + +//---------------------------------------------------------------------- +/// @class HostInfo HostInfo.h "lldb/Host/HostThread.h" +/// @brief A class that represents a thread running inside of a process on the +/// local machine. +/// +/// HostThread allows querying and manipulation of threads running on the host +/// machine. +/// +/// HostThread is implemented such that on any given platform, the type +/// |HostThread| resolves statically to the specific thread implementation for +/// the specific platform. +/// +//---------------------------------------------------------------------- + +#if defined(_WIN32) +#include "lldb/Host/windows/HostThreadWindows.h" +#define HOST_THREAD_TYPE HostThreadWindows +#elif defined(__linux__) +#include "lldb/Host/linux/HostThreadLinux.h" +#define HOST_THREAD_TYPE HostThreadLinux +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) +#include "lldb/Host/freebsd/HostThreadFreeBSD.h" +#define HOST_THREAD_TYPE HostThreadFreeBSD.h +#elif defined(__APPLE__) +#include "lldb/Host/macosx/HostThreadMacOSX.h" +#define HOST_THREAD_TYPE HostThreadMacOSX +#else +#include "lldb/Host/posix/HostThreadPosix.h" +#define HOST_THREAD_TYPE HostThreadPosix +#endif + +namespace lldb_private +{ +typedef HOST_THREAD_TYPE HostThread; +} + +#undef HOST_THREAD_TYPE + +#endif Index: include/lldb/Host/freebsd/HostThreadFreeBSD.h =================================================================== --- /dev/null +++ include/lldb/Host/freebsd/HostThreadFreeBSD.h @@ -0,0 +1,30 @@ +//===-- 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/Core/Error.h" +#include "lldb/Host/posix/HostThreadPosix.h" +#include "llvm/ADT/StringRef.h" + +#include + +namespace lldb_private +{ + +class HostThreadFreeBSD : public HostThreadPosix +{ + public: + Error SetName(llvm::StringRef name); + std::string GetName() const; +}; +} + +#endif Index: include/lldb/Host/linux/HostThreadLinux.h =================================================================== --- /dev/null +++ include/lldb/Host/linux/HostThreadLinux.h @@ -0,0 +1,30 @@ +//===-- 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/Core/Error.h" +#include "lldb/Host/posix/HostThreadPosix.h" +#include "llvm/ADT/StringRef.h" + +#include + +namespace lldb_private +{ + +class HostThreadLinux : public HostThreadPosix +{ + public: + Error SetName(llvm::StringRef name); + std::string GetName() const; +}; +} + +#endif Index: include/lldb/Host/macosx/HostThreadMacOSX.h =================================================================== --- /dev/null +++ include/lldb/Host/macosx/HostThreadMacOSX.h @@ -0,0 +1,30 @@ +//===-- HostThreadMacOSX.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_macosx_HostThreadMacOSX_h_ +#define lldb_Host_macosx_HostThreadMacOSX_h_ + +#include "lldb/Core/Error.h" +#include "lldb/Host/posix/HostThreadPosix.h" +#include "llvm/ADT/StringRef.h" + +#include + +namespace lldb_private +{ + +class HostThreadMacOSX : public HostThreadPosix +{ + public: + Error SetName(llvm::StringRef name); + std::string GetName() const; +}; +} + +#endif Index: include/lldb/Host/posix/HostThreadPosix.h =================================================================== --- /dev/null +++ include/lldb/Host/posix/HostThreadPosix.h @@ -0,0 +1,43 @@ +//===-- HostThreadWindows.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_HostThreadPosix_h_ +#define lldb_Host_HostThreadPosix_h_ + +#include "lldb/lldb-types.h" +#include "lldb/Core/Error.h" + +#include "llvm/ADT/SmallString.h" + +namespace lldb_private +{ + +class FileSpec; + +class HostThreadPosix +{ + public: + HostThreadPosix(); + ~HostThreadPosix(); + + Error Create(lldb::thread_t thread); + + lldb::thread_t GetThreadHandle() const; + bool IsValid() const; + + Error Cancel(); + Error Detach(); + Error Join(lldb::thread_result_t *result); + + private: + lldb::thread_t m_thread; +}; +} + +#endif Index: include/lldb/Host/windows/HostThreadWindows.h =================================================================== --- /dev/null +++ include/lldb/Host/windows/HostThreadWindows.h @@ -0,0 +1,59 @@ +//===-- HostThreadWindows.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_windows_HostThreadWindows_h_ +#define lldb_Host_windows_HostThreadWindows_h_ + +#include "lldb/lldb-types.h" +#include "lldb/Core/Error.h" + +#include "llvm/ADT/StringRef.h" + +namespace lldb_private +{ + +class HostThreadWindows +{ + public: + enum Priority + { + kPriorityIdle, + kPriorityLow, + kPriorityBelowNormal, + kPriorityNormal, + kPriorityAboveNormal, + kPriorityHigh + }; + + HostThreadWindows(); + ~HostThreadWindows(); + + Error Create(lldb::tid_t tid); + Error Create(lldb::thread_t thread); + Error Suspend(); + Error Resume(); + Error Join(lldb::thread_result_t *result); + + Error SetPriority(Priority priority); + Error GetPriority(Priority &priority) const; + + Error SetName(llvm::StringRef name); + + lldb::tid_t GetThreadId() const; + + private: + bool InternalSetThreadName(llvm::StringRef name); + void Close(); + + lldb::tid_t m_tid; + lldb::thread_t m_thread; +}; +} + +#endif Index: include/lldb/lldb-types.h =================================================================== --- include/lldb/lldb-types.h +++ include/lldb/lldb-types.h @@ -50,7 +50,7 @@ typedef void* condition_t; typedef void* rwlock_t; typedef void* process_t; // Process type is HANDLE - typedef uintptr_t thread_t; // Host thread type + typedef void* thread_t; // Host thread type typedef uint32_t thread_key_t; typedef void * thread_arg_t; // Host thread argument type typedef unsigned thread_result_t; // Host thread result type Index: lldb.xcodeproj/project.pbxproj =================================================================== --- lldb.xcodeproj/project.pbxproj +++ lldb.xcodeproj/project.pbxproj @@ -589,6 +589,9 @@ 3FDFE53119A292F0009756A7 /* HostInfoPosix.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFE53019A292F0009756A7 /* HostInfoPosix.cpp */; }; 3FDFE53319A29304009756A7 /* HostInfoPosix.h in Headers */ = {isa = PBXBuildFile; fileRef = 3FDFE53219A29304009756A7 /* HostInfoPosix.h */; }; 3FDFE53519A29327009756A7 /* HostInfoBase.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFE53419A29327009756A7 /* HostInfoBase.cpp */; }; + 3FDFE56919AF9BC7009756A7 /* HostThreadMacOSX.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFE56819AF9BC7009756A7 /* HostThreadMacOSX.cpp */; }; + 3FDFE56C19AF9C44009756A7 /* HostProcessPosix.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFE56A19AF9C44009756A7 /* HostProcessPosix.cpp */; }; + 3FDFE56D19AF9C44009756A7 /* HostThreadPosix.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFE56B19AF9C44009756A7 /* HostThreadPosix.cpp */; }; 449ACC98197DEA0B008D175E /* FastDemangle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 449ACC96197DE9EC008D175E /* FastDemangle.cpp */; }; 490A36C0180F0E6F00BA31F8 /* PlatformWindows.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 490A36BD180F0E6F00BA31F8 /* PlatformWindows.cpp */; }; 490A36C2180F0E9300BA31F8 /* PlatformWindows.h in Headers */ = {isa = PBXBuildFile; fileRef = 490A36BE180F0E6F00BA31F8 /* PlatformWindows.h */; }; @@ -1752,7 +1755,7 @@ 3FDFDDC1199D34E2009756A7 /* FileSystem.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = FileSystem.h; path = include/lldb/Host/FileSystem.h; sourceTree = ""; }; 3FDFDDC5199D37ED009756A7 /* FileSystem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = FileSystem.cpp; path = source/Host/posix/FileSystem.cpp; sourceTree = ""; }; 3FDFE52B19A2917A009756A7 /* HostInfoMacOSX.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = HostInfoMacOSX.mm; path = source/Host/macosx/HostInfoMacOSX.mm; sourceTree = ""; }; - 3FDFE52D19A291AF009756A7 /* HostInfoMacOSX.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = HostInfoMacOSX.h; path = include/lldb/Host/macosx/HostInfoMacOSX.h; sourceTree = ""; }; + 3FDFE52D19A291AF009756A7 /* HostInfoMacOSX.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = HostInfoMacOSX.h; path = include/lldb/Host/macosx/HostInfoMacOSX.h; sourceTree = ""; }; 3FDFE53019A292F0009756A7 /* HostInfoPosix.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = HostInfoPosix.cpp; path = source/Host/posix/HostInfoPosix.cpp; sourceTree = ""; }; 3FDFE53219A29304009756A7 /* HostInfoPosix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = HostInfoPosix.h; path = include/lldb/Host/posix/HostInfoPosix.h; sourceTree = ""; }; 3FDFE53419A29327009756A7 /* HostInfoBase.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HostInfoBase.cpp; sourceTree = ""; }; @@ -1775,6 +1778,26 @@ 3FDFE54919A2946B009756A7 /* HostInfoWindows.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = HostInfoWindows.h; path = include/lldb/Host/windows/HostInfoWindows.h; sourceTree = ""; }; 3FDFE54A19A2946B009756A7 /* win32.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = win32.h; path = include/lldb/Host/windows/win32.h; sourceTree = ""; }; 3FDFE54B19A2946B009756A7 /* windows.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = windows.h; path = include/lldb/Host/windows/windows.h; sourceTree = ""; }; + 3FDFE55E19AF9B14009756A7 /* Host.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = Host.cpp; path = source/Host/freebsd/Host.cpp; sourceTree = ""; }; + 3FDFE55F19AF9B14009756A7 /* HostThreadFreeBSD.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = HostThreadFreeBSD.cpp; path = source/Host/freebsd/HostThreadFreeBSD.cpp; sourceTree = ""; }; + 3FDFE56019AF9B39009756A7 /* HostThreadFreeBSD.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = HostThreadFreeBSD.h; path = include/lldb/Host/freebsd/HostThreadFreeBSD.h; sourceTree = ""; }; + 3FDFE56219AF9B60009756A7 /* HostThreadLinux.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = HostThreadLinux.cpp; sourceTree = ""; }; + 3FDFE56319AF9B77009756A7 /* Config.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Config.h; path = include/lldb/Host/linux/Config.h; sourceTree = SOURCE_ROOT; }; + 3FDFE56419AF9B77009756A7 /* HostInfoLinux.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = HostInfoLinux.h; path = include/lldb/Host/linux/HostInfoLinux.h; sourceTree = SOURCE_ROOT; }; + 3FDFE56519AF9B77009756A7 /* HostThreadLinux.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = HostThreadLinux.h; path = include/lldb/Host/linux/HostThreadLinux.h; sourceTree = SOURCE_ROOT; }; + 3FDFE56619AF9BB2009756A7 /* Config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Config.h; path = include/lldb/Host/macosx/Config.h; sourceTree = ""; }; + 3FDFE56719AF9BB2009756A7 /* HostThreadMacOSX.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = HostThreadMacOSX.h; path = include/lldb/Host/macosx/HostThreadMacOSX.h; sourceTree = ""; }; + 3FDFE56819AF9BC7009756A7 /* HostThreadMacOSX.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = HostThreadMacOSX.cpp; path = source/Host/macosx/HostThreadMacOSX.cpp; sourceTree = ""; }; + 3FDFE56A19AF9C44009756A7 /* HostProcessPosix.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = HostProcessPosix.cpp; path = source/Host/posix/HostProcessPosix.cpp; sourceTree = ""; }; + 3FDFE56B19AF9C44009756A7 /* HostThreadPosix.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = HostThreadPosix.cpp; path = source/Host/posix/HostThreadPosix.cpp; sourceTree = ""; }; + 3FDFE56E19AF9C5A009756A7 /* HostProcessPosix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = HostProcessPosix.h; path = include/lldb/Host/posix/HostProcessPosix.h; sourceTree = ""; }; + 3FDFE56F19AF9C5A009756A7 /* HostThreadPosix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = HostThreadPosix.h; path = include/lldb/Host/posix/HostThreadPosix.h; sourceTree = ""; }; + 3FDFE57019AF9CA0009756A7 /* HostProcessWindows.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = HostProcessWindows.cpp; path = source/Host/windows/HostProcessWindows.cpp; sourceTree = ""; }; + 3FDFE57119AF9CA0009756A7 /* HostThreadWindows.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = HostThreadWindows.cpp; path = source/Host/windows/HostThreadWindows.cpp; sourceTree = ""; }; + 3FDFE57219AF9CD3009756A7 /* HostProcessWindows.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = HostProcessWindows.h; path = include/lldb/Host/windows/HostProcessWindows.h; sourceTree = ""; }; + 3FDFE57319AF9CD3009756A7 /* HostThreadWindows.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = HostThreadWindows.h; path = include/lldb/Host/windows/HostThreadWindows.h; sourceTree = ""; }; + 3FDFE57419AFABFD009756A7 /* HostProcess.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = HostProcess.h; path = include/lldb/Host/HostProcess.h; sourceTree = ""; }; + 3FDFE57519AFABFD009756A7 /* HostThread.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = HostThread.h; path = include/lldb/Host/HostThread.h; sourceTree = ""; }; 449ACC96197DE9EC008D175E /* FastDemangle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = FastDemangle.cpp; path = source/Core/FastDemangle.cpp; sourceTree = ""; }; 4906FD4012F2255300A2A77C /* ASTDumper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ASTDumper.cpp; path = source/Expression/ASTDumper.cpp; sourceTree = ""; }; 4906FD4412F2257600A2A77C /* ASTDumper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ASTDumper.h; path = include/lldb/Expression/ASTDumper.h; sourceTree = ""; }; @@ -2313,8 +2336,12 @@ 233B009C19610D130090E598 /* linux */ = { isa = PBXGroup; children = ( + 3FDFE56319AF9B77009756A7 /* Config.h */, 233B009D19610D6B0090E598 /* Host.cpp */, 3FDFE53619A2933E009756A7 /* HostInfoLinux.cpp */, + 3FDFE56419AF9B77009756A7 /* HostInfoLinux.h */, + 3FDFE56219AF9B60009756A7 /* HostThreadLinux.cpp */, + 3FDFE56519AF9B77009756A7 /* HostThreadLinux.h */, ); name = linux; path = source/Host/linux; @@ -3513,6 +3540,8 @@ 26BC7DD410F1B7D500F91463 /* Host.h */, 3FDFE53719A2936B009756A7 /* HostInfo.h */, 3FDFE53819A2936B009756A7 /* HostInfoBase.h */, + 3FDFE57419AFABFD009756A7 /* HostProcess.h */, + 3FDFE57519AFABFD009756A7 /* HostThread.h */, 236124A61986B50E004EFC37 /* IOObject.h */, 26BC7DD510F1B7D500F91463 /* Mutex.h */, 232CB60B191E00CC00EF39FC /* NativeBreakpoint.cpp */, @@ -3748,8 +3777,7 @@ 26BC7EE510F1B88100F91463 /* MacOSX */ = { isa = PBXGroup; children = ( - 3FDFE52D19A291AF009756A7 /* HostInfoMacOSX.h */, - 3FDFE52B19A2917A009756A7 /* HostInfoMacOSX.mm */, + 3FDFE56619AF9BB2009756A7 /* Config.h */, 26BC7EED10F1B8AD00F91463 /* CFCBundle.cpp */, 26BC7EEE10F1B8AD00F91463 /* CFCBundle.h */, 26BC7EEF10F1B8AD00F91463 /* CFCData.cpp */, @@ -3764,6 +3792,10 @@ 26BC7EF810F1B8AD00F91463 /* CFCString.cpp */, 26BC7EF910F1B8AD00F91463 /* CFCString.h */, 26BC7EE810F1B88F00F91463 /* Host.mm */, + 3FDFE52B19A2917A009756A7 /* HostInfoMacOSX.mm */, + 3FDFE52D19A291AF009756A7 /* HostInfoMacOSX.h */, + 3FDFE56819AF9BC7009756A7 /* HostThreadMacOSX.cpp */, + 3FDFE56719AF9BB2009756A7 /* HostThreadMacOSX.h */, 2689B0B5113EE47E00A4AEDB /* Symbols.cpp */, EDC6D49114E5C15C001B75F8 /* launcherXPCService */, ); @@ -3964,8 +3996,12 @@ 3FDFDDC4199D37BE009756A7 /* posix */ = { isa = PBXGroup; children = ( - 3FDFE53219A29304009756A7 /* HostInfoPosix.h */, 3FDFE53019A292F0009756A7 /* HostInfoPosix.cpp */, + 3FDFE53219A29304009756A7 /* HostInfoPosix.h */, + 3FDFE56A19AF9C44009756A7 /* HostProcessPosix.cpp */, + 3FDFE56E19AF9C5A009756A7 /* HostProcessPosix.h */, + 3FDFE56B19AF9C44009756A7 /* HostThreadPosix.cpp */, + 3FDFE56F19AF9C5A009756A7 /* HostThreadPosix.h */, 3FDFDDC5199D37ED009756A7 /* FileSystem.cpp */, ); name = posix; @@ -3975,8 +4011,11 @@ isa = PBXGroup; children = ( 3FDFE53C19A293CA009756A7 /* Config.h */, - 3FDFE53D19A293CA009756A7 /* HostInfoFreeBSD.h */, + 3FDFE55E19AF9B14009756A7 /* Host.cpp */, 3FDFE53B19A293B3009756A7 /* HostInfoFreeBSD.cpp */, + 3FDFE53D19A293CA009756A7 /* HostInfoFreeBSD.h */, + 3FDFE55F19AF9B14009756A7 /* HostThreadFreeBSD.cpp */, + 3FDFE56019AF9B39009756A7 /* HostThreadFreeBSD.h */, ); name = freebsd; sourceTree = ""; @@ -3990,13 +4029,17 @@ 3FDFE54019A29448009756A7 /* EditLineWin.cpp */, 3FDFE54119A29448009756A7 /* FileSystem.cpp */, 3FDFE54219A29448009756A7 /* Host.cpp */, - 3FDFE54919A2946B009756A7 /* HostInfoWindows.h */, 3FDFE54319A29448009756A7 /* HostInfoWindows.cpp */, + 3FDFE54919A2946B009756A7 /* HostInfoWindows.h */, + 3FDFE57019AF9CA0009756A7 /* HostProcessWindows.cpp */, + 3FDFE57219AF9CD3009756A7 /* HostProcessWindows.h */, + 3FDFE57119AF9CA0009756A7 /* HostThreadWindows.cpp */, + 3FDFE57319AF9CD3009756A7 /* HostThreadWindows.h */, 3FDFE54419A29448009756A7 /* Mutex.cpp */, 3FDFE54519A29448009756A7 /* ProcessRunLock.cpp */, 3FDFE54A19A2946B009756A7 /* win32.h */, - 3FDFE54B19A2946B009756A7 /* windows.h */, 3FDFE54619A29448009756A7 /* Windows.cpp */, + 3FDFE54B19A2946B009756A7 /* windows.h */, ); name = windows; sourceTree = ""; @@ -4922,6 +4965,7 @@ 2689006E13353E1A00698AC0 /* File.cpp in Sources */, 94D6A0AB16CEB55F00833B6E /* NSDictionary.cpp in Sources */, 2689006F13353E1A00698AC0 /* FileSpec.cpp in Sources */, + 3FDFE56919AF9BC7009756A7 /* HostThreadMacOSX.cpp in Sources */, 2689007013353E1A00698AC0 /* Condition.cpp in Sources */, 2689007113353E1A00698AC0 /* Host.cpp in Sources */, 2635879417822FC2004C30BA /* SymbolVendorELF.cpp in Sources */, @@ -4972,6 +5016,7 @@ AF1729D6182C907200E0AB97 /* HistoryThread.cpp in Sources */, 268900AF13353E5000698AC0 /* UnwindLLDB.cpp in Sources */, 268900B013353E5000698AC0 /* RegisterContextLLDB.cpp in Sources */, + 3FDFE56C19AF9C44009756A7 /* HostProcessPosix.cpp in Sources */, 268900B413353E5000698AC0 /* RegisterContextMacOSXFrameBackchain.cpp in Sources */, 268900B513353E5000698AC0 /* StopInfoMachException.cpp in Sources */, 268900B613353E5000698AC0 /* UnwindMacOSXFrameBackchain.cpp in Sources */, @@ -5069,6 +5114,7 @@ 2689010313353E6F00698AC0 /* ThreadPlanStepRange.cpp in Sources */, 2689010413353E6F00698AC0 /* ThreadPlanStepInRange.cpp in Sources */, 2689010513353E6F00698AC0 /* ThreadPlanStepOverRange.cpp in Sources */, + 3FDFE56D19AF9C44009756A7 /* HostThreadPosix.cpp in Sources */, 2689010613353E6F00698AC0 /* ThreadPlanRunToAddress.cpp in Sources */, 2689010713353E6F00698AC0 /* ThreadPlanStepThrough.cpp in Sources */, 2689010813353E6F00698AC0 /* ThreadPlanStepUntil.cpp in Sources */, Index: source/Host/CMakeLists.txt =================================================================== --- source/Host/CMakeLists.txt +++ source/Host/CMakeLists.txt @@ -34,6 +34,7 @@ windows/Host.cpp windows/HostInfoWindows.cpp windows/HostProcessWindows.cpp + windows/HostThreadWindows.cpp windows/ProcessRunLock.cpp windows/Mutex.cpp windows/Condition.cpp @@ -45,12 +46,14 @@ posix/FileSystem.cpp posix/HostInfoPosix.cpp posix/HostProcessPosix.cpp + posix/HostThreadPosix.cpp ) if (CMAKE_SYSTEM_NAME MATCHES "Darwin") include_directories(SYSTEM ${LIBXML2_INCLUDE_DIR}) add_host_subdirectory(macosx macosx/Host.mm + macosx/HostThreadMacOSX.cpp macosx/Symbols.cpp macosx/cfcpp/CFCBundle.cpp macosx/cfcpp/CFCData.cpp @@ -64,11 +67,13 @@ add_host_subdirectory(linux linux/Host.cpp linux/HostInfoLinux.cpp + linux/HostThreadLinux.cpp ) elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD") add_host_subdirectory(freebsd freebsd/Host.cpp freebsd/HostInfoFreeBSD.cpp + freebsd/HostThreadFreeBSD.cpp ) endif() endif() Index: source/Host/common/Host.cpp =================================================================== --- source/Host/common/Host.cpp +++ source/Host/common/Host.cpp @@ -48,6 +48,10 @@ #include #endif +#if defined(__linux__) +#include "Plugins/Process/Linux/ProcFileReader.h" +#endif + // C++ includes #include @@ -484,7 +488,7 @@ HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg); #ifdef _WIN32 - thread = ::_beginthreadex(0, 0, ThreadCreateTrampoline, info_ptr, 0, NULL); + thread = (void *)::_beginthreadex(0, 0, ThreadCreateTrampoline, info_ptr, 0, NULL); int err = thread <= 0 ? GetLastError() : 0; #else int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr); @@ -613,6 +617,84 @@ #endif } +std::string +Host::GetThreadName(lldb::pid_t pid, lldb::tid_t tid) +{ + std::string thread_name; + +#if defined(__FreeBSD__) + struct kinfo_proc *kp = nullptr, *nkp; + size_t len = 0; + int error; + int name[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD, (int)pid}; + + while (1) + { + error = sysctl(name, 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 std::string(); + } + kp = nkp; + continue; + } + if (error != 0) + len = 0; + break; + } + + for (size_t i = 0; i < len / sizeof(*kp); i++) + { + if (kp[i].ki_tid == (int)tid) + { + thread_name = kp[i].ki_tdname; + break; + } + } + free(kp); +#elif 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; + } + } + } + } +#elif defined(__linux__) + // Read /proc/$TID/comm file. + lldb::DataBufferSP buf_sp = ProcFileReader::ReadIntoDataBuffer(tid, "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); + + thread_name = std::string(comm_str, length); +#endif + return thread_name; +} + bool Host::SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *thread_name, size_t len) Index: source/Host/freebsd/Host.cpp =================================================================== --- source/Host/freebsd/Host.cpp +++ source/Host/freebsd/Host.cpp @@ -83,46 +83,6 @@ Host::SetShortThreadName (LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID, thread_name, 16); } -std::string -Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid) -{ - struct kinfo_proc *kp = nullptr, *nkp; - size_t len = 0; - int error; - int name[4] = { - CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD, (int)pid - }; - - while (1) { - error = sysctl(name, 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 std::string(); - } - kp = nkp; - continue; - } - if (error != 0) - len = 0; - break; - } - - std::string thread_name; - for (size_t i = 0; i < len / sizeof(*kp); i++) { - if (kp[i].ki_tid == (int)tid) { - thread_name = kp[i].ki_tdname; - break; - } - } - free(kp); - return thread_name; -} - void Host::Backtrace (Stream &strm, uint32_t max_frames) { Index: source/Host/freebsd/HostThreadFreeBSD.cpp =================================================================== --- /dev/null +++ source/Host/freebsd/HostThreadFreeBSD.cpp @@ -0,0 +1,76 @@ +//===-- 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" + +// C includes +#include +#include +#include + +// C++ includes +#include + +using namespace lldb_private; + +Error +HostThreadFreeBSD::SetName(lldb::thread_t thread, llvm::StringRef name) +{ + Error error; + + ::pthread_set_name_np(GetThreadHandle(), name); + + return error; +} + +std::string +HostThreadFreeBSD::GetName() const +{ + // TODO(zturner): We should be trying to get the pid from m_thread. How to do that? + int pid = Host::GetCurrentProcessID(); + + struct kinfo_proc *kp = nullptr, *nkp; + size_t len = 0; + int error; + int name[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD, (int)pid}; + + while (1) + { + error = sysctl(name, 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 std::string(); + } + kp = nkp; + continue; + } + if (error != 0) + len = 0; + break; + } + + std::string thread_name; + for (size_t i = 0; i < len / sizeof(*kp); i++) + { + if (kp[i].ki_tid == (int)GetThreadHandle()) + { + thread_name = kp[i].ki_tdname; + break; + } + } + free(kp); + return thread_name; +} Index: source/Host/linux/Host.cpp =================================================================== --- source/Host/linux/Host.cpp +++ source/Host/linux/Host.cpp @@ -380,22 +380,6 @@ } } -std::string -Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid) -{ - assert(pid != LLDB_INVALID_PROCESS_ID); - assert(tid != LLDB_INVALID_THREAD_ID); - - // Read /proc/$TID/comm file. - lldb::DataBufferSP buf_sp = ProcFileReader::ReadIntoDataBuffer (tid, "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); - - std::string thread_name(comm_str, length); - return thread_name; -} - void Host::Backtrace (Stream &strm, uint32_t max_frames) { Index: source/Host/linux/HostThreadLinux.cpp =================================================================== --- /dev/null +++ source/Host/linux/HostThreadLinux.cpp @@ -0,0 +1,40 @@ +//===-- 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/Core/DataBuffer.h" +#include "lldb/Host/linux/HostThreadLinux.h" +#include "Plugins/Process/Linux/ProcFileReader.h" + +#include + +using namespace lldb_private; + +Error +HostThreadLinux::SetName(llvm::StringRef name) +{ + Error error; + + int err = ::pthread_setname_np(GetThreadHandle(), name.data()); + error.SetError(err, lldb::eErrorTypePOSIX); + + return error; +} + +std::string +HostThreadLinux::GetName() const +{ + // Read /proc/$TID/comm file. + lldb::DataBufferSP buf_sp = ProcFileReader::ReadIntoDataBuffer(GetThreadHandle(), "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); + + std::string thread_name(comm_str, length); + return thread_name; +} Index: source/Host/macosx/Host.mm =================================================================== --- source/Host/macosx/Host.mm +++ source/Host/macosx/Host.mm @@ -145,39 +145,6 @@ } } -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) { Index: source/Host/macosx/HostThreadMacOSX.cpp =================================================================== --- /dev/null +++ source/Host/macosx/HostThreadMacOSX.cpp @@ -0,0 +1,63 @@ +//===-- HostThreadMacOSX.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/macosx/HostThreadMacOSX.h" +#include "lldb/Host/Host.h" + +#include + +using namespace lldb_private; + +Error +HostThreadMacOSX::SetName(llvm::StringRef name) +{ + Error error; +#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5 + // Set the pthread name if possible + if (GetThreadHandle()() != Host::GetCurrentThread()) + error.SetErrorString("Cannot set the name of the non-current thread."); + else + { + int err = ::pthread_setname_np(name); + error.SetError(err, lldb::eErrorTypePOSIX); + } +#else + error.SetErrorString("Setting the thread name is not supported on this platform."); +#endif + return error; +} + +std::string +HostThreadMacOSX::GetName() const +{ + std::string thread_name; +#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5 + char pthread_name[1024]; + if (::pthread_getname_np(GetThreadHandle(), pthread_name, sizeof(pthread_name)) == 0) + { + if (pthread_name[0]) + { + thread_name = pthread_name; + } + } + else if (GetThreadHandle() == Host::GetCurrentThread()) + { + 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; +} Index: source/Host/posix/HostThreadPosix.cpp =================================================================== --- /dev/null +++ source/Host/posix/HostThreadPosix.cpp @@ -0,0 +1,78 @@ +//===-- HostThreadPosix.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/posix/HostThreadPosix.h" + +#include "llvm/ADT/STLExtras.h" + +#include + +using namespace lldb_private; + +HostThreadPosix::HostThreadPosix() + : m_thread(LLDB_INVALID_HOST_THREAD) +{ +} + +HostThreadPosix::~HostThreadPosix() +{ + m_thread = LLDB_INVALID_HOST_THREAD; +} + +Error +HostThreadPosix::Create(lldb::thread_t thread) +{ + Error error; + m_thread = thread; + return error; +} + +Error +HostThreadPosix::Cancel() +{ + Error error; + int err = ::pthread_cancel(m_thread); + error.SetError(err, lldb::eErrorTypePOSIX); + return error; +} + +Error +HostThreadPosix::Detach() +{ + Error error; + if (!IsValid()) + return error; + + int err = ::pthread_detach(m_thread); + error.SetError(err, lldb::eErrorTypePOSIX); + m_thread = LLDB_INVALID_HOST_THREAD; + return error; +} + +Error +HostThreadPosix::Join(lldb::thread_result_t *result) +{ + Error error; + int err = ::pthread_join(m_thread, result); + error.SetError(err, lldb::eErrorTypePOSIX); + m_thread = LLDB_INVALID_HOST_THREAD; + return error; +} + +lldb::thread_t +HostThreadPosix::GetThreadHandle() const +{ + return m_thread; +} + +bool +HostThreadPosix::IsValid() const +{ + return GetThreadHandle() != LLDB_INVALID_HOST_THREAD; +} Index: source/Host/windows/HostThreadWindows.cpp =================================================================== --- /dev/null +++ source/Host/windows/HostThreadWindows.cpp @@ -0,0 +1,234 @@ +//===-- HostThreadWindows.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/windows/windows.h" + +#include "lldb/Host/windows/HostThreadWindows.h" + +#include "llvm/ADT/STLExtras.h" + +using namespace lldb_private; + +namespace +{ +static const DWORD MS_VC_EXCEPTION = 0x406D1388; + +#pragma pack(push, 8) +struct THREADNAME_INFO +{ + DWORD dwType; // Must be 0x1000. + LPCSTR szName; // Pointer to thread name + DWORD dwThreadId; // Thread ID (-1 == current thread) + DWORD dwFlags; // Reserved. Do not use. +}; +#pragma pack(pop) +} + +HostThreadWindows::HostThreadWindows() + : m_tid(0) + , m_thread(0) +{ +} + +HostThreadWindows::~HostThreadWindows() +{ + Close(); +} + +Error +HostThreadWindows::Create(lldb::tid_t tid) +{ + Close(); + + Error error; + m_thread = ::OpenThread(THREAD_QUERY_INFORMATION | THREAD_SUSPEND_RESUME, FALSE, tid); + if (m_thread == NULL) + { + error.SetError(::GetLastError(), lldb::eErrorTypeWin32); + return error; + } + m_tid = tid; + return error; +} + +Error +HostThreadWindows::Create(lldb::thread_t thread) +{ + Error error; + + m_tid = ::GetThreadId(thread); + if (m_tid == 0) + { + error.SetError(::GetLastError(), lldb::eErrorTypeWin32); + return error; + } + m_thread = thread; + return error; +} + +Error +HostThreadWindows::Suspend() +{ + Error error; + + if ((DWORD)-1 == ::SuspendThread(m_thread)) + error.SetError(::GetLastError(), lldb::eErrorTypeWin32); + + return error; +} + +Error +HostThreadWindows::Resume() +{ + Error error; + + if ((DWORD)-1 == ::ResumeThread(m_thread)) + error.SetError(::GetLastError(), lldb::eErrorTypeWin32); + + return error; +} + +Error +HostThreadWindows::Join(lldb::thread_result_t *result) +{ + Error error; + if (WAIT_OBJECT_0 != ::WaitForSingleObject(m_thread, INFINITE)) + { + error.SetError(::GetLastError(), lldb::eErrorTypeWin32); + return error; + } + + DWORD dword_result = 0; + if (!::GetExitCodeThread(m_thread, &dword_result)) + *result = 0; + *result = dword_result; + return error; +} + +Error +HostThreadWindows::SetPriority(Priority priority) +{ + Error error; + int win32_priority = 0; + switch (priority) + { + case kPriorityIdle: + win32_priority = THREAD_PRIORITY_IDLE; + break; + case kPriorityLow: + win32_priority = THREAD_PRIORITY_LOWEST; + break; + case kPriorityBelowNormal: + win32_priority = THREAD_PRIORITY_BELOW_NORMAL; + break; + case kPriorityNormal: + win32_priority = THREAD_PRIORITY_NORMAL; + break; + case kPriorityAboveNormal: + win32_priority = THREAD_PRIORITY_ABOVE_NORMAL; + break; + case kPriorityHigh: + win32_priority = THREAD_PRIORITY_HIGHEST; + break; + default: + error.SetError(ERROR_INVALID_PARAMETER, lldb::eErrorTypeWin32); + return error; + } + + if (!::SetThreadPriority(m_thread, win32_priority)) + error.SetError(::GetLastError(), lldb::eErrorTypeWin32); + + return error; +} + +Error +HostThreadWindows::GetPriority(Priority &priority) const +{ + Error error; + int win32_priority = 0; + win32_priority = ::GetThreadPriority(m_thread); + if (THREAD_PRIORITY_ERROR_RETURN == win32_priority) + { + error.SetError(::GetLastError(), lldb::eErrorTypeWin32); + return error; + } + + switch (win32_priority) + { + case THREAD_PRIORITY_IDLE: + priority = kPriorityIdle; + break; + case THREAD_PRIORITY_LOWEST: + priority = kPriorityLow; + break; + case THREAD_PRIORITY_BELOW_NORMAL: + priority = kPriorityBelowNormal; + break; + case THREAD_PRIORITY_NORMAL: + priority = kPriorityNormal; + break; + case THREAD_PRIORITY_ABOVE_NORMAL: + priority = kPriorityAboveNormal; + break; + case THREAD_PRIORITY_HIGHEST: + priority = kPriorityHigh; + break; + } + + return error; +} + +Error +HostThreadWindows::SetName(llvm::StringRef name) +{ + Error error; + + if (!InternalSetThreadName(name.data())) + error.SetErrorString("Unable to set thread name."); + return error; +} + +bool +HostThreadWindows::InternalSetThreadName(llvm::StringRef name) +{ +// Other compilers don't support SEH, so we can only set the thread if compiling with MSVC. +#if defined(_MSC_VER) + if (m_tid == 0) + return false; + + THREADNAME_INFO info; + info.dwType = 0x1000; + info.szName = name.data(); + info.dwThreadId = m_tid; + info.dwFlags = 0; + + __try { ::RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info); } + __except(EXCEPTION_EXECUTE_HANDLER) {} + + return true; +#else + return false; +#endif +} + +lldb::tid_t +HostThreadWindows::GetThreadId() const +{ + return m_tid; +} + +void +HostThreadWindows::Close() +{ + if (m_thread != NULL) + ::CloseHandle(m_thread); + + m_thread = NULL; + m_tid = 0; +}