summaryrefslogtreecommitdiffstats
path: root/lldb/source
diff options
context:
space:
mode:
Diffstat (limited to 'lldb/source')
-rw-r--r--lldb/source/API/SBHostOS.cpp39
-rw-r--r--lldb/source/Core/Communication.cpp27
-rw-r--r--lldb/source/Core/Debugger.cpp59
-rw-r--r--lldb/source/Core/Log.cpp8
-rw-r--r--lldb/source/Host/CMakeLists.txt21
-rw-r--r--lldb/source/Host/common/Host.cpp234
-rw-r--r--lldb/source/Host/common/HostInfoBase.cpp6
-rw-r--r--lldb/source/Host/common/HostNativeThreadBase.cpp92
-rw-r--r--lldb/source/Host/common/HostThread.cpp84
-rw-r--r--lldb/source/Host/common/ThisThread.cpp52
-rw-r--r--lldb/source/Host/common/ThreadLauncher.cpp48
-rw-r--r--lldb/source/Host/freebsd/Host.cpp74
-rw-r--r--lldb/source/Host/freebsd/HostInfoFreeBSD.cpp6
-rw-r--r--lldb/source/Host/freebsd/HostThreadFreeBSD.cpp80
-rw-r--r--lldb/source/Host/freebsd/ThisThread.cpp29
-rw-r--r--lldb/source/Host/linux/Host.cpp25
-rw-r--r--lldb/source/Host/linux/HostInfoLinux.cpp6
-rw-r--r--lldb/source/Host/linux/HostThreadLinux.cpp47
-rw-r--r--lldb/source/Host/linux/ThisThread.cpp29
-rw-r--r--lldb/source/Host/macosx/Host.mm140
-rw-r--r--lldb/source/Host/macosx/HostThreadMacOSX.mm102
-rw-r--r--lldb/source/Host/macosx/ThisThread.cpp39
-rw-r--r--lldb/source/Host/posix/HostThreadPosix.cpp63
-rw-r--r--lldb/source/Host/windows/Host.cpp51
-rw-r--r--lldb/source/Host/windows/HostThreadWindows.cpp80
-rw-r--r--lldb/source/Host/windows/ThisThread.cpp60
-rw-r--r--lldb/source/Plugins/Process/CMakeLists.txt1
-rw-r--r--lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp39
-rw-r--r--lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.h5
-rw-r--r--lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp37
-rw-r--r--lldb/source/Plugins/Process/Linux/NativeProcessLinux.h5
-rw-r--r--lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp8
-rw-r--r--lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp36
-rw-r--r--lldb/source/Plugins/Process/Linux/ProcessMonitor.h5
-rw-r--r--lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp24
-rw-r--r--lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h3
-rw-r--r--lldb/source/Plugins/Process/POSIX/POSIXThread.cpp6
-rw-r--r--lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp12
-rw-r--r--lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h4
-rw-r--r--lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp38
-rw-r--r--lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h11
-rw-r--r--lldb/source/Target/Process.cpp48
42 files changed, 1081 insertions, 702 deletions
diff --git a/lldb/source/API/SBHostOS.cpp b/lldb/source/API/SBHostOS.cpp
index ec1e2f2e9cb..4a65eee1064 100644
--- a/lldb/source/API/SBHostOS.cpp
+++ b/lldb/source/API/SBHostOS.cpp
@@ -13,6 +13,12 @@
#include "lldb/Core/Log.h"
#include "lldb/Host/Host.h"
#include "lldb/Host/HostInfo.h"
+#include "lldb/Host/HostThread.h"
+#include "lldb/Host/ThreadLauncher.h"
+
+#if !defined(_WIN32)
+#include "lldb/Host/HostNativeThread.h"
+#endif
using namespace lldb;
using namespace lldb_private;
@@ -69,31 +75,54 @@ SBHostOS::ThreadCreate
// FIXME: You should log the return value?
- return Host::ThreadCreate (name, thread_function, thread_arg, error_ptr ? error_ptr->get() : NULL);
+ HostThread thread(ThreadLauncher::LaunchThread(name, thread_function, thread_arg, error_ptr ? error_ptr->get() : NULL));
+ return thread.Release();
}
void
SBHostOS::ThreadCreated (const char *name)
{
- Host::ThreadCreated (name);
}
bool
SBHostOS::ThreadCancel (lldb::thread_t thread, SBError *error_ptr)
{
- return Host::ThreadCancel (thread, error_ptr ? error_ptr->get() : NULL);
+ Error error;
+ HostThread host_thread(thread);
+ error = host_thread.Cancel();
+ if (error_ptr)
+ error_ptr->SetError(error);
+ host_thread.Release();
+ return error.Success();
}
bool
SBHostOS::ThreadDetach (lldb::thread_t thread, SBError *error_ptr)
{
- return Host::ThreadDetach (thread, error_ptr ? error_ptr->get() : NULL);
+ Error error;
+#if defined(_WIN32)
+ if (error_ptr)
+ error_ptr->SetErrorString("ThreadDetach is not supported on this platform");
+#else
+ HostThread host_thread(thread);
+ error = ((HostThreadPosix &)host_thread.GetNativeThread()).Detach();
+ if (error_ptr)
+ error_ptr->SetError(error);
+ host_thread.Release();
+#endif
+ return error.Success();
}
bool
SBHostOS::ThreadJoin (lldb::thread_t thread, lldb::thread_result_t *result, SBError *error_ptr)
{
- return Host::ThreadJoin (thread, result, error_ptr ? error_ptr->get() : NULL);
+ Error error;
+ HostThread host_thread(thread);
+ error = host_thread.Join(result);
+ if (error_ptr)
+ error_ptr->SetError(error);
+ host_thread.Release();
+ return error.Success();
}
diff --git a/lldb/source/Core/Communication.cpp b/lldb/source/Core/Communication.cpp
index d71c9881a6f..4f8628a1f37 100644
--- a/lldb/source/Core/Communication.cpp
+++ b/lldb/source/Core/Communication.cpp
@@ -18,6 +18,8 @@
#include "lldb/Core/Timer.h"
#include "lldb/Core/Event.h"
#include "lldb/Host/Host.h"
+#include "lldb/Host/HostThread.h"
+#include "lldb/Host/ThreadLauncher.h"
#include <string.h>
using namespace lldb;
@@ -36,7 +38,6 @@ Communication::GetStaticBroadcasterClass ()
Communication::Communication(const char *name) :
Broadcaster (NULL, name),
m_connection_sp (),
- m_read_thread (LLDB_INVALID_HOST_THREAD),
m_read_thread_enabled (false),
m_bytes(),
m_bytes_mutex (Mutex::eMutexTypeRecursive),
@@ -232,7 +233,7 @@ Communication::StartReadThread (Error *error_ptr)
if (error_ptr)
error_ptr->Clear();
- if (IS_VALID_LLDB_HOST_THREAD(m_read_thread))
+ if (m_read_thread.GetState() == eThreadStateRunning)
return true;
lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
@@ -243,8 +244,8 @@ Communication::StartReadThread (Error *error_ptr)
snprintf(thread_name, sizeof(thread_name), "<lldb.comm.%s>", m_broadcaster_name.AsCString());
m_read_thread_enabled = true;
- m_read_thread = Host::ThreadCreate (thread_name, Communication::ReadThread, this, error_ptr);
- if (!IS_VALID_LLDB_HOST_THREAD(m_read_thread))
+ m_read_thread = ThreadLauncher::LaunchThread(thread_name, Communication::ReadThread, this, error_ptr);
+ if (m_read_thread.GetState() != eThreadStateRunning)
m_read_thread_enabled = false;
return m_read_thread_enabled;
}
@@ -252,7 +253,7 @@ Communication::StartReadThread (Error *error_ptr)
bool
Communication::StopReadThread (Error *error_ptr)
{
- if (!IS_VALID_LLDB_HOST_THREAD(m_read_thread))
+ if (m_read_thread.GetState() != eThreadStateRunning)
return true;
lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
@@ -262,22 +263,22 @@ Communication::StopReadThread (Error *error_ptr)
BroadcastEvent (eBroadcastBitReadThreadShouldExit, NULL);
- //Host::ThreadCancel (m_read_thread, error_ptr);
+ // error = m_read_thread.Cancel();
- bool status = Host::ThreadJoin (m_read_thread, NULL, error_ptr);
- m_read_thread = LLDB_INVALID_HOST_THREAD;
- return status;
+ Error error = m_read_thread.Join(nullptr);
+ m_read_thread.Reset();
+ return error.Success();
}
bool
Communication::JoinReadThread (Error *error_ptr)
{
- if (!IS_VALID_LLDB_HOST_THREAD(m_read_thread))
+ if (m_read_thread.GetState() != eThreadStateRunning)
return true;
- bool success = Host::ThreadJoin (m_read_thread, NULL, error_ptr);
- m_read_thread = LLDB_INVALID_HOST_THREAD;
- return success;
+ Error error = m_read_thread.Join(nullptr);
+ m_read_thread.Reset();
+ return error.Success();
}
size_t
diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp
index 17829634767..9319d27d33b 100644
--- a/lldb/source/Core/Debugger.cpp
+++ b/lldb/source/Core/Debugger.cpp
@@ -36,6 +36,7 @@
#include "lldb/DataFormatters/TypeSummary.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Host/Terminal.h"
+#include "lldb/Host/ThreadLauncher.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/OptionValueSInt64.h"
#include "lldb/Interpreter/OptionValueString.h"
@@ -620,24 +621,22 @@ Debugger::FindTargetWithProcess (Process *process)
return target_sp;
}
-Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
- UserID (g_unique_id++),
- Properties(OptionValuePropertiesSP(new OptionValueProperties())),
- m_input_file_sp (new StreamFile (stdin, false)),
- m_output_file_sp (new StreamFile (stdout, false)),
- m_error_file_sp (new StreamFile (stderr, false)),
- m_terminal_state (),
- m_target_list (*this),
- m_platform_list (),
- m_listener ("lldb.Debugger"),
- m_source_manager_ap(),
- m_source_file_cache(),
- m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
- m_input_reader_stack (),
- m_instance_name (),
- m_loaded_plugins (),
- m_event_handler_thread (LLDB_INVALID_HOST_THREAD),
- m_io_handler_thread (LLDB_INVALID_HOST_THREAD)
+Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton)
+ : UserID(g_unique_id++)
+ , Properties(OptionValuePropertiesSP(new OptionValueProperties()))
+ , m_input_file_sp(new StreamFile(stdin, false))
+ , m_output_file_sp(new StreamFile(stdout, false))
+ , m_error_file_sp(new StreamFile(stderr, false))
+ , m_terminal_state()
+ , m_target_list(*this)
+ , m_platform_list()
+ , m_listener("lldb.Debugger")
+ , m_source_manager_ap()
+ , m_source_file_cache()
+ , m_command_interpreter_ap(new CommandInterpreter(*this, eScriptLanguageDefault, false))
+ , m_input_reader_stack()
+ , m_instance_name()
+ , m_loaded_plugins()
{
char instance_cstr[256];
snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
@@ -3337,19 +3336,19 @@ Debugger::EventHandlerThread (lldb::thread_arg_t arg)
bool
Debugger::StartEventHandlerThread()
{
- if (!IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
- m_event_handler_thread = Host::ThreadCreate("lldb.debugger.event-handler", EventHandlerThread, this, NULL);
- return IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread);
+ if (m_event_handler_thread.GetState() != eThreadStateRunning)
+ m_event_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.event-handler", EventHandlerThread, this, NULL);
+ return m_event_handler_thread.GetState() == eThreadStateRunning;
}
void
Debugger::StopEventHandlerThread()
{
- if (IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
+ if (m_event_handler_thread.GetState() == eThreadStateRunning)
{
GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
- Host::ThreadJoin(m_event_handler_thread, NULL, NULL);
- m_event_handler_thread = LLDB_INVALID_HOST_THREAD;
+ m_event_handler_thread.Join(nullptr);
+ m_event_handler_thread.Reset();
}
}
@@ -3366,20 +3365,20 @@ Debugger::IOHandlerThread (lldb::thread_arg_t arg)
bool
Debugger::StartIOHandlerThread()
{
- if (!IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
- m_io_handler_thread = Host::ThreadCreate("lldb.debugger.io-handler", IOHandlerThread, this, NULL);
- return IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread);
+ if (m_io_handler_thread.GetState() != eThreadStateRunning)
+ m_io_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.io-handler", IOHandlerThread, this, NULL);
+ return m_io_handler_thread.GetState() == eThreadStateRunning;
}
void
Debugger::StopIOHandlerThread()
{
- if (IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
+ if (m_io_handler_thread.GetState() == eThreadStateRunning)
{
if (m_input_file_sp)
m_input_file_sp->GetFile().Close();
- Host::ThreadJoin(m_io_handler_thread, NULL, NULL);
- m_io_handler_thread = LLDB_INVALID_HOST_THREAD;
+ m_io_handler_thread.Join(nullptr);
+ m_io_handler_thread.Reset();
}
}
diff --git a/lldb/source/Core/Log.cpp b/lldb/source/Core/Log.cpp
index 3adca6e5385..d205d363b76 100644
--- a/lldb/source/Core/Log.cpp
+++ b/lldb/source/Core/Log.cpp
@@ -26,9 +26,12 @@
#include "lldb/Core/StreamFile.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Host/Host.h"
-#include "lldb/Host/TimeValue.h"
#include "lldb/Host/Mutex.h"
+#include "lldb/Host/ThisThread.h"
+#include "lldb/Host/TimeValue.h"
#include "lldb/Interpreter/Args.h"
+
+#include "llvm/ADT/SmallString.h"
using namespace lldb;
using namespace lldb_private;
@@ -113,7 +116,8 @@ Log::PrintfWithFlagsVarArg (uint32_t flags, const char *format, va_list args)
// Add the thread name if requested
if (m_options.Test (LLDB_LOG_OPTION_PREPEND_THREAD_NAME))
{
- std::string thread_name (Host::GetThreadName (getpid(), Host::GetCurrentThreadID()));
+ llvm::SmallString<32> thread_name;
+ ThisThread::GetName(thread_name);
if (!thread_name.empty())
header.Printf ("%s ", thread_name.c_str());
}
diff --git a/lldb/source/Host/CMakeLists.txt b/lldb/source/Host/CMakeLists.txt
index d296295245f..0051f902c71 100644
--- a/lldb/source/Host/CMakeLists.txt
+++ b/lldb/source/Host/CMakeLists.txt
@@ -11,6 +11,8 @@ add_host_subdirectory(common
common/FileSpec.cpp
common/Host.cpp
common/HostInfoBase.cpp
+ common/HostNativeThreadBase.cpp
+ common/HostThread.cpp
common/IOObject.cpp
common/Mutex.cpp
common/NativeBreakpoint.cpp
@@ -25,34 +27,41 @@ add_host_subdirectory(common
common/SoftwareBreakpoint.cpp
common/Symbols.cpp
common/Terminal.cpp
+ common/ThisThread.cpp
+ common/ThreadLauncher.cpp
common/TimeValue.cpp
)
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
add_host_subdirectory(windows
+ windows/Condition.cpp
+ windows/EditLineWin.cpp
windows/FileSystem.cpp
windows/Host.cpp
windows/HostInfoWindows.cpp
windows/HostProcessWindows.cpp
- windows/ProcessRunLock.cpp
+ windows/HostThreadWindows.cpp
windows/Mutex.cpp
- windows/Condition.cpp
+ windows/ProcessRunLock.cpp
+ windows/ThisThread.cpp
windows/Windows.cpp
- windows/EditLineWin.cpp
)
else()
add_host_subdirectory(posix
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/Symbols.cpp
macosx/HostInfoMacOSX.mm
+ macosx/HostThreadMacOSX.mm
+ macosx/Symbols.cpp
+ macosx/ThisThread.cpp
macosx/cfcpp/CFCBundle.cpp
macosx/cfcpp/CFCData.cpp
macosx/cfcpp/CFCMutableArray.cpp
@@ -65,11 +74,15 @@ else()
add_host_subdirectory(linux
linux/Host.cpp
linux/HostInfoLinux.cpp
+ linux/HostThreadLinux.cpp
+ linux/ThisThread.cpp
)
elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
add_host_subdirectory(freebsd
freebsd/Host.cpp
freebsd/HostInfoFreeBSD.cpp
+ freebsd/HostThreadFreeBSD.cpp
+ freebsd/ThisThread.cpp
)
endif()
endif()
diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp
index 00c2fa37b38..ce1af3288fc 100644
--- a/lldb/source/Host/common/Host.cpp
+++ b/lldb/source/Host/common/Host.cpp
@@ -48,6 +48,10 @@
#include <pthread_np.h>
#endif
+#if defined(__linux__)
+#include "Plugins/Process/Linux/ProcFileReader.h"
+#endif
+
// C++ includes
#include <limits>
@@ -66,6 +70,7 @@
#include "lldb/Host/FileSpec.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/Mutex.h"
+#include "lldb/Host/ThreadLauncher.h"
#include "lldb/lldb-private-forward.h"
#include "lldb/Target/FileAction.h"
#include "lldb/Target/Process.h"
@@ -95,13 +100,6 @@ extern "C"
using namespace lldb;
using namespace lldb_private;
-// Define maximum thread name length
-#if defined (__linux__) || defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__NetBSD__)
-uint32_t const Host::MAX_THREAD_NAME_LENGTH = 16;
-#else
-uint32_t const Host::MAX_THREAD_NAME_LENGTH = std::numeric_limits<uint32_t>::max ();
-#endif
-
#if !defined (__APPLE__) && !defined (_WIN32)
struct MonitorInfo
{
@@ -114,16 +112,9 @@ struct MonitorInfo
static thread_result_t
MonitorChildProcessThreadFunction (void *arg);
-lldb::thread_t
-Host::StartMonitoringChildProcess
-(
- Host::MonitorChildProcessCallback callback,
- void *callback_baton,
- lldb::pid_t pid,
- bool monitor_signals
-)
+HostThread
+Host::StartMonitoringChildProcess(Host::MonitorChildProcessCallback callback, void *callback_baton, lldb::pid_t pid, bool monitor_signals)
{
- lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
MonitorInfo * info_ptr = new MonitorInfo();
info_ptr->pid = pid;
@@ -132,24 +123,8 @@ Host::StartMonitoringChildProcess
info_ptr->monitor_signals = monitor_signals;
char thread_name[256];
-
- if (Host::MAX_THREAD_NAME_LENGTH <= 16)
- {
- // On some platforms, the thread name is limited to 16 characters. We need to
- // abbreviate there or the pid info would get truncated.
- ::snprintf (thread_name, sizeof(thread_name), "wait4(%" PRIu64 ")", pid);
- }
- else
- {
- ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid);
- }
-
- thread = ThreadCreate (thread_name,
- MonitorChildProcessThreadFunction,
- info_ptr,
- NULL);
-
- return thread;
+ ::snprintf(thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid);
+ return ThreadLauncher::LaunchThread(thread_name, MonitorChildProcessThreadFunction, info_ptr, NULL);
}
//------------------------------------------------------------------
@@ -429,11 +404,6 @@ Host::WillTerminate ()
#if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__) // see macosx/Host.mm
void
-Host::ThreadCreated (const char *thread_name)
-{
-}
-
-void
Host::Backtrace (Stream &strm, uint32_t max_frames)
{
// TODO: Is there a way to backtrace the current process on other systems?
@@ -448,101 +418,8 @@ Host::GetEnvironment (StringList &env)
#endif // #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__)
-struct HostThreadCreateInfo
-{
- std::string thread_name;
- thread_func_t thread_fptr;
- thread_arg_t thread_arg;
-
- HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
- thread_name (name ? name : ""),
- thread_fptr (fptr),
- thread_arg (arg)
- {
- }
-};
-
-static thread_result_t
-#ifdef _WIN32
-__stdcall
-#endif
-ThreadCreateTrampoline (thread_arg_t arg)
-{
- HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
- Host::ThreadCreated (info->thread_name.c_str());
- thread_func_t thread_fptr = info->thread_fptr;
- thread_arg_t thread_arg = info->thread_arg;
-
- Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
- if (log)
- log->Printf("thread created");
-
- delete info;
- return thread_fptr (thread_arg);
-}
-
-lldb::thread_t
-Host::ThreadCreate
-(
- const char *thread_name,
- thread_func_t thread_fptr,
- thread_arg_t thread_arg,
- Error *error
-)
-{
- lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
-
- // Host::ThreadCreateTrampoline will delete this pointer for us.
- HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
-
-#ifdef _WIN32
- thread = ::_beginthreadex(0, 0, ThreadCreateTrampoline, info_ptr, 0, NULL);
- int err = thread <= 0 ? GetLastError() : 0;
-#else
- int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
-#endif
- if (err == 0)
- {
- if (error)
- error->Clear();
- return thread;
- }
-
- if (error)
- error->SetError (err, eErrorTypePOSIX);
-
- return LLDB_INVALID_HOST_THREAD;
-}
-
#ifndef _WIN32
-bool
-Host::ThreadCancel (lldb::thread_t thread, Error *error)
-{
- int err = ::pthread_cancel (thread);
- if (error)
- error->SetError(err, eErrorTypePOSIX);
- return err == 0;
-}
-
-bool
-Host::ThreadDetach (lldb::thread_t thread, Error *error)
-{
- int err = ::pthread_detach (thread);
- if (error)
- error->SetError(err, eErrorTypePOSIX);
- return err == 0;
-}
-
-bool
-Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
-{
- int err = ::pthread_join (thread, thread_result_ptr);
- if (error)
- error->SetError(err, eErrorTypePOSIX);
- return err == 0;
-}
-
lldb::thread_key_t
Host::ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback)
{
@@ -563,99 +440,6 @@ Host::ThreadLocalStorageSet(lldb::thread_key_t key, void *value)
::pthread_setspecific (key, value);
}
-bool
-Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
-{
-#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
- lldb::pid_t curr_pid = Host::GetCurrentProcessID();
- lldb::tid_t curr_tid = Host::GetCurrentThreadID();
- if (pid == LLDB_INVALID_PROCESS_ID)
- pid = curr_pid;
-
- if (tid == LLDB_INVALID_THREAD_ID)
- tid = curr_tid;
-
- // Set the pthread name if possible
- if (pid == curr_pid && tid == curr_tid)
- {
- if (::pthread_setname_np (name) == 0)
- return true;
- }
- return false;
-#elif defined (__FreeBSD__)
- lldb::pid_t curr_pid = Host::GetCurrentProcessID();
- lldb::tid_t curr_tid = Host::GetCurrentThreadID();
- if (pid == LLDB_INVALID_PROCESS_ID)
- pid = curr_pid;
-
- if (tid == LLDB_INVALID_THREAD_ID)
- tid = curr_tid;
-
- // Set the pthread name if possible
- if (pid == curr_pid && tid == curr_tid)
- {
- ::pthread_set_name_np (::pthread_self(), name);
- return true;
- }
- return false;
-#elif defined (__linux__) || defined (__GLIBC__)
- void *fn = dlsym (RTLD_DEFAULT, "pthread_setname_np");
- if (fn)
- {
- lldb::pid_t curr_pid = Host::GetCurrentProcessID();
- lldb::tid_t curr_tid = Host::GetCurrentThreadID();
- if (pid == LLDB_INVALID_PROCESS_ID)
- pid = curr_pid;
-
- if (tid == LLDB_INVALID_THREAD_ID)
- tid = curr_tid;
-
- if (pid == curr_pid && tid == curr_tid)
- {
- int (*pthread_setname_np_func)(pthread_t thread, const char *name);
- *reinterpret_cast<void **> (&pthread_setname_np_func) = fn;
-
- if (pthread_setname_np_func (::pthread_self(), name) == 0)
- return true;
- }
- }
- return false;
-#else
- return false;
-#endif
-}
-
-bool
-Host::SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid,
- const char *thread_name, size_t len)
-{
- std::unique_ptr<char[]> namebuf(new char[len+1]);
-
- // Thread names are coming in like '<lldb.comm.debugger.edit>' and
- // '<lldb.comm.debugger.editline>'. So just chopping the end of the string
- // off leads to a lot of similar named threads. Go through the thread name
- // and search for the last dot and use that.
- const char *lastdot = ::strrchr (thread_name, '.');
-
- if (lastdot && lastdot != thread_name)
- thread_name = lastdot + 1;
- ::strncpy (namebuf.get(), thread_name, len);
- namebuf[len] = 0;
-
- int namebuflen = strlen(namebuf.get());
- if (namebuflen > 0)
- {
- if (namebuf[namebuflen - 1] == '(' || namebuf[namebuflen - 1] == '>')
- {
- // Trim off trailing '(' and '>' characters for a bit more cleanup.
- namebuflen--;
- namebuf[namebuflen] = 0;
- }
- return Host::SetThreadName (pid, tid, namebuf.get());
- }
- return false;
-}
-
#endif
#if !defined (__APPLE__) // see Host.mm
diff --git a/lldb/source/Host/common/HostInfoBase.cpp b/lldb/source/Host/common/HostInfoBase.cpp
index 4eb43bfaf6f..9af71ec9bcc 100644
--- a/lldb/source/Host/common/HostInfoBase.cpp
+++ b/lldb/source/Host/common/HostInfoBase.cpp
@@ -94,6 +94,12 @@ HostInfoBase::GetNumberCPUS()
return g_fields->m_number_cpus;
}
+uint32_t
+HostInfoBase::GetMaxThreadNameLength()
+{
+ return 0;
+}
+
llvm::StringRef
HostInfoBase::GetVendorString()
{
diff --git a/lldb/source/Host/common/HostNativeThreadBase.cpp b/lldb/source/Host/common/HostNativeThreadBase.cpp
new file mode 100644
index 00000000000..7a2bd6d217f
--- /dev/null
+++ b/lldb/source/Host/common/HostNativeThreadBase.cpp
@@ -0,0 +1,92 @@
+//===-- HostNativeThreadBase.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/Log.h"
+#include "lldb/Host/HostInfo.h"
+#include "lldb/Host/HostNativeThreadBase.h"
+#include "lldb/Host/ThisThread.h"
+#include "lldb/Host/ThreadLauncher.h"
+#include "llvm/ADT/StringExtras.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+HostNativeThreadBase::HostNativeThreadBase()
+ : m_thread(LLDB_INVALID_HOST_THREAD)
+ , m_state(eThreadStateInvalid)
+ , m_result(0)
+{
+}
+
+HostNativeThreadBase::HostNativeThreadBase(thread_t thread)
+ : m_thread(thread)
+ , m_state((thread == LLDB_INVALID_HOST_THREAD) ? eThreadStateInvalid : eThreadStateRunning)
+ , m_result(0)
+{
+}
+
+void
+HostNativeThreadBase::SetState(ThreadState state)
+{
+ m_state = state;
+}
+
+ThreadState
+HostNativeThreadBase::GetState() const
+{
+ return m_state;
+}
+
+lldb::thread_t
+HostNativeThreadBase::GetSystemHandle() const
+{
+ return m_thread;
+}
+
+lldb::thread_result_t
+HostNativeThreadBase::GetResult() const
+{
+ return m_result;
+}
+
+void
+HostNativeThreadBase::Reset()
+{
+ m_thread = LLDB_INVALID_HOST_THREAD;
+ m_state = eThreadStateInvalid;
+ m_result = 0;
+}
+
+lldb::thread_t
+HostNativeThreadBase::Release()
+{
+ lldb::thread_t result = m_thread;
+ m_thread = LLDB_INVALID_HOST_THREAD;
+ m_state = eThreadStateInvalid;
+ m_result = 0;
+
+ return result;
+}
+
+lldb::thread_result_t
+HostNativeThreadBase::ThreadCreateTrampoline(lldb::thread_arg_t arg)
+{
+ ThreadLauncher::HostThreadCreateInfo *info = (ThreadLauncher::HostThreadCreateInfo *)arg;
+ ThisThread::SetName(info->thread_name.c_str(), HostInfo::GetMaxThreadNameLength());
+
+ thread_func_t thread_fptr = info->thread_fptr;
+ thread_arg_t thread_arg = info->thread_arg;
+
+ Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
+ if (log)
+ log->Printf("thread created");
+
+ delete info;
+ return thread_fptr(thread_arg);
+}
diff --git a/lldb/source/Host/common/HostThread.cpp b/lldb/source/Host/common/HostThread.cpp
new file mode 100644
index 00000000000..82eacc9473a
--- /dev/null
+++ b/lldb/source/Host/common/HostThread.cpp
@@ -0,0 +1,84 @@
+//===-- HostThread.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/HostNativeThread.h"
+#include "lldb/Host/HostThread.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+HostThread::HostThread()
+ : m_native_thread(new HostNativeThread)
+{
+}
+
+HostThread::HostThread(lldb::thread_t thread)
+ : m_native_thread(new HostNativeThread(thread))
+{
+}
+
+Error
+HostThread::Join(lldb::thread_result_t *result)
+{
+ return m_native_thread->Join(result);
+}
+
+Error
+HostThread::Cancel()
+{
+ return m_native_thread->Cancel();
+}
+
+void
+HostThread::Reset()
+{
+ return m_native_thread->Reset();
+}
+
+lldb::thread_t
+HostThread::Release()
+{
+ return m_native_thread->Release();
+}
+
+void
+HostThread::SetState(ThreadState state)
+{
+ m_native_thread->SetState(state);
+}
+
+ThreadState
+HostThread::GetState() const
+{
+ return m_native_thread->GetState();
+}
+
+HostNativeThreadBase &
+HostThread::GetNativeThread()
+{
+ return *m_native_thread;
+}
+
+const HostNativeThreadBase &
+HostThread::GetNativeThread() const
+{
+ return *m_native_thread;
+}
+
+lldb::thread_result_t
+HostThread::GetResult() const
+{
+ return m_native_thread->GetResult();
+}
+
+bool
+HostThread::EqualsThread(lldb::thread_t thread) const
+{
+ return m_native_thread->GetSystemHandle() == thread;
+}
diff --git a/lldb/source/Host/common/ThisThread.cpp b/lldb/source/Host/common/ThisThread.cpp
new file mode 100644
index 00000000000..5802781f50b
--- /dev/null
+++ b/lldb/source/Host/common/ThisThread.cpp
@@ -0,0 +1,52 @@
+//===-- ThisThread.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/Error.h"
+#include "lldb/Host/HostInfo.h"
+#include "lldb/Host/ThisThread.h"
+
+#include "llvm/ADT/STLExtras.h"
+
+#include <algorithm>
+
+using namespace lldb;
+using namespace lldb_private;
+
+void
+ThisThread::SetName(llvm::StringRef name, int max_length)
+{
+ std::string truncated_name(name.data());
+
+ // Thread names are coming in like '<lldb.comm.debugger.edit>' and
+ // '<lldb.comm.debugger.editline>'. So just chopping the end of the string
+ // off leads to a lot of similar named threads. Go through the thread name
+ // and search for the last dot and use that.
+
+ if (max_length > 0 && truncated_name.length() > max_length)
+ {
+ // First see if we can get lucky by removing any initial or final braces.
+ std::string::size_type begin = truncated_name.find_first_not_of("(<");
+ std::string::size_type end = truncated_name.find_last_not_of(")>.");
+ if (end - begin > max_length)
+ {
+ // We're still too long. Since this is a dotted component, use everything after the last
+ // dot, up to a maximum of |length| characters.
+ std::string::size_type last_dot = truncated_name.find_last_of(".");
+ if (last_dot != std::string::npos)
+ begin = last_dot + 1;
+
+ end = std::min(end, begin + max_length);
+ }
+
+ std::string::size_type count = end - begin + 1;
+ truncated_name = truncated_name.substr(begin, count);
+ }
+
+ SetName(truncated_name.c_str());
+}
diff --git a/lldb/source/Host/common/ThreadLauncher.cpp b/lldb/source/Host/common/ThreadLauncher.cpp
new file mode 100644
index 00000000000..350d4491b6c
--- /dev/null
+++ b/lldb/source/Host/common/ThreadLauncher.cpp
@@ -0,0 +1,48 @@
+//===-- ThreadLauncher.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/Core/Log.h"
+#include "lldb/Host/HostNativeThread.h"
+#include "lldb/Host/HostThread.h"
+#include "lldb/Host/ThisThread.h"
+#include "lldb/Host/ThreadLauncher.h"
+
+#if defined(_WIN32)
+#include "lldb/Host/windows/windows.h"
+#endif
+
+using namespace lldb;
+using namespace lldb_private;
+
+HostThread
+ThreadLauncher::LaunchThread(llvm::StringRef name, lldb::thread_func_t thread_function, lldb::thread_arg_t thread_arg, Error *error_ptr)
+{
+ Error error;
+ if (error_ptr)
+ error_ptr->Clear();
+
+ // Host::ThreadCreateTrampoline will delete this pointer for us.
+ HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo(name.data(), thread_function, thread_arg);
+ lldb::thread_t thread;
+#ifdef _WIN32
+ thread = (lldb::thread_t)::_beginthreadex(0, 0, HostNativeThread::ThreadCreateTrampoline, info_ptr, 0, NULL);
+ if (thread == (lldb::thread_t)(-1L))
+ error.SetError(::GetLastError(), eErrorTypeWin32);
+#else
+ int err = ::pthread_create(&thread, NULL, HostNativeThread::ThreadCreateTrampoline, info_ptr);
+ error.SetError(err, eErrorTypePOSIX);
+#endif
+ if (error_ptr)
+ *error_ptr = error;
+ if (!error.Success())
+ thread = LLDB_INVALID_HOST_THREAD;
+
+ return HostThread(thread);
+}
diff --git a/lldb/source/Host/freebsd/Host.cpp b/lldb/source/Host/freebsd/Host.cpp
index dc092e86d78..5f065e2810f 100644
--- a/lldb/source/Host/freebsd/Host.cpp
+++ b/lldb/source/Host/freebsd/Host.cpp
@@ -50,80 +50,6 @@ extern "C" {
using namespace lldb;
using namespace lldb_private;
-class FreeBSDThread
-{
-public:
- FreeBSDThread(const char *thread_name)
- {
- Host::SetThreadName (LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID, thread_name);
- }
- static void PThreadDestructor (void *v)
- {
- delete (FreeBSDThread*)v;
- }
-};
-
-static pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT;
-static pthread_key_t g_thread_create_key = 0;
-
-static void
-InitThreadCreated()
-{
- ::pthread_key_create (&g_thread_create_key, FreeBSDThread::PThreadDestructor);
-}
-
-void
-Host::ThreadCreated (const char *thread_name)
-{
- ::pthread_once (&g_thread_create_once, InitThreadCreated);
- if (g_thread_create_key)
- {
- ::pthread_setspecific (g_thread_create_key, new FreeBSDThread(thread_name));
- }
-
- 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)
{
diff --git a/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp b/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp
index 78458259f0a..d51109320c3 100644
--- a/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp
+++ b/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp
@@ -17,6 +17,12 @@
using namespace lldb_private;
+uint32_t
+HostInfoFreeBSD::GetMaxThreadNameLength()
+{
+ return 16;
+}
+
bool
HostInfoFreeBSD::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update)
{
diff --git a/lldb/source/Host/freebsd/HostThreadFreeBSD.cpp b/lldb/source/Host/freebsd/HostThreadFreeBSD.cpp
new file mode 100644
index 00000000000..ce2d44e24ad
--- /dev/null
+++ b/lldb/source/Host/freebsd/HostThreadFreeBSD.cpp
@@ -0,0 +1,80 @@
+//===-- 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 <pthread.h>
+#include <pthread_np.h>
+#include <stdlib.h>
+#include <sys/sysctl.h>
+
+// C++ includes
+#include <string>
+
+using namespace lldb_private;
+
+HostThreadFreeBSD::HostThreadFreeBSD()
+{
+}
+
+HostThreadFreeBSD::HostThreadFreeBSD(lldb::thread_t thread)
+ : HostThreadPosix(thread)
+{
+}
+
+void
+HostThreadFreeBSD::SetName(lldb::thread_t thread, llvm::StringRef name)
+{
+ ::pthread_set_name_np(thread, name);
+}
+
+void
+HostThreadFreeBSD::GetName(lldb::thread_t thread, 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 == (int)thread)
+ {
+ name.append(kp[i].ki_tdname, strlen(kp[i].ki_tdname));
+ break;
+ }
+ }
+ free(kp);
+}
diff --git a/lldb/source/Host/freebsd/ThisThread.cpp b/lldb/source/Host/freebsd/ThisThread.cpp
new file mode 100644
index 00000000000..6a506187aaf
--- /dev/null
+++ b/lldb/source/Host/freebsd/ThisThread.cpp
@@ -0,0 +1,29 @@
+//===-- ThisThread.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 "lldb/Host/ThisThread.h"
+
+#include "llvm/ADT/SmallVector.h"
+
+#include <pthread.h>
+
+using namespace lldb_private;
+
+void
+ThisThread::SetName(llvm::StringRef name)
+{
+ HostThread::SetName(::pthread_self(), name);
+}
+
+void
+ThisThread::GetName(llvm::SmallVectorImpl<char> &name)
+{
+ HostThread::GetName(::pthread_self(), name);
+}
diff --git a/lldb/source/Host/linux/Host.cpp b/lldb/source/Host/linux/Host.cpp
index 0bad90ee22e..3d90fbb16c5 100644
--- a/lldb/source/Host/linux/Host.cpp
+++ b/lldb/source/Host/linux/Host.cpp
@@ -373,31 +373,6 @@ Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
}
void
-Host::ThreadCreated (const char *thread_name)
-{
- if (!Host::SetThreadName (LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID, thread_name))
- {
- 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)
-{
- 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)
{
if (max_frames > 0)
diff --git a/lldb/source/Host/linux/HostInfoLinux.cpp b/lldb/source/Host/linux/HostInfoLinux.cpp
index 6d55c503a0f..c18975ca160 100644
--- a/lldb/source/Host/linux/HostInfoLinux.cpp
+++ b/lldb/source/Host/linux/HostInfoLinux.cpp
@@ -47,6 +47,12 @@ HostInfoLinux::Initialize()
g_fields = new HostInfoLinuxFields();
}
+uint32_t
+HostInfoLinux::GetMaxThreadNameLength()
+{
+ return 16;
+}
+
bool
HostInfoLinux::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update)
{
diff --git a/lldb/source/Host/linux/HostThreadLinux.cpp b/lldb/source/Host/linux/HostThreadLinux.cpp
new file mode 100644
index 00000000000..619bd245ab2
--- /dev/null
+++ b/lldb/source/Host/linux/HostThreadLinux.cpp
@@ -0,0 +1,47 @@
+//===-- 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 "llvm/ADT/SmallVector.h"
+
+#include <pthread.h>
+
+using namespace lldb_private;
+
+HostThreadLinux::HostThreadLinux()
+ : HostThreadPosix()
+{
+}
+
+HostThreadLinux::HostThreadLinux(lldb::thread_t thread)
+ : HostThreadPosix(thread)
+{
+}
+
+void
+HostThreadLinux::SetName(lldb::thread_t thread, llvm::StringRef name)
+{
+ ::pthread_setname_np(thread, name.data());
+}
+
+void
+HostThreadLinux::GetName(lldb::thread_t thread, llvm::SmallVectorImpl<char> &name)
+{
+ // Read /proc/$TID/comm file.
+ lldb::DataBufferSP buf_sp = 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/linux/ThisThread.cpp b/lldb/source/Host/linux/ThisThread.cpp
new file mode 100644
index 00000000000..1c68c8ba16d
--- /dev/null
+++ b/lldb/source/Host/linux/ThisThread.cpp
@@ -0,0 +1,29 @@
+//===-- ThisThread.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/HostNativeThread.h"
+#include "lldb/Host/ThisThread.h"
+
+#include "llvm/ADT/SmallVector.h"
+
+#include <pthread.h>
+
+using namespace lldb_private;
+
+void
+ThisThread::SetName(llvm::StringRef name)
+{
+ HostNativeThread::SetName(::pthread_self(), name);
+}
+
+void
+ThisThread::GetName(llvm::SmallVectorImpl<char> &name)
+{
+ HostNativeThread::GetName(::pthread_self(), name);
+}
diff --git a/lldb/source/Host/macosx/Host.mm b/lldb/source/Host/macosx/Host.mm
index efc95c70bbb..37ff095817d 100644
--- a/lldb/source/Host/macosx/Host.mm
+++ b/lldb/source/Host/macosx/Host.mm
@@ -47,7 +47,9 @@
#include "lldb/Core/StreamString.h"
#include "lldb/Host/Endian.h"
#include "lldb/Host/FileSpec.h"
+#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfo.h"
+#include "lldb/Host/ThreadLauncher.h"
#include "lldb/Target/Platform.h"
#include "lldb/Target/Process.h"
#include "lldb/Utility/CleanUp.h"
@@ -77,107 +79,6 @@ extern "C"
using namespace lldb;
using namespace lldb_private;
-static pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT;
-static pthread_key_t g_thread_create_key = 0;
-
-class MacOSXDarwinThread
-{
-public:
- MacOSXDarwinThread(const char *thread_name) :
- m_pool (nil)
- {
- // Register our thread with the collector if garbage collection is enabled.
- if (objc_collectingEnabled())
- {
-#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5
- // On Leopard and earlier there is no way objc_registerThreadWithCollector
- // function, so we do it manually.
- auto_zone_register_thread(auto_zone());
-#else
- // On SnowLeopard and later we just call the thread registration function.
- objc_registerThreadWithCollector();
-#endif
- }
- else
- {
- m_pool = [[NSAutoreleasePool alloc] init];
- }
-
-
- Host::SetThreadName (LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID, thread_name);
- }
-
- ~MacOSXDarwinThread()
- {
- if (m_pool)
- {
- [m_pool drain];
- m_pool = nil;
- }
- }
-
- static void PThreadDestructor (void *v)
- {
- if (v)
- delete static_cast<MacOSXDarwinThread*>(v);
- ::pthread_setspecific (g_thread_create_key, NULL);
- }
-
-protected:
- NSAutoreleasePool * m_pool;
-private:
- DISALLOW_COPY_AND_ASSIGN (MacOSXDarwinThread);
-};
-
-static void
-InitThreadCreated()
-{
- ::pthread_key_create (&g_thread_create_key, MacOSXDarwinThread::PThreadDestructor);
-}
-
-void
-Host::ThreadCreated (const char *thread_name)
-{
- ::pthread_once (&g_thread_create_once, InitThreadCreated);
- if (g_thread_create_key)
- {
- ::pthread_setspecific (g_thread_create_key, new MacOSXDarwinThread(thread_name));
- }
-}
-
-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)
{
@@ -642,28 +543,23 @@ LaunchInNewTerminalWithAppleScript (const char *exe_path, ProcessLaunchInfo &lau
// in a shell and the shell will fork/exec a couple of times before we get
// to the process that we wanted to launch. So when our process actually
// gets launched, we will handshake with it and get the process ID for it.
- lldb::thread_t accept_thread = Host::ThreadCreate (unix_socket_name,
- AcceptPIDFromInferior,
- connect_url,
- &lldb_error);
-
+ HostThread accept_thread = ThreadLauncher::LaunchThread(unix_socket_name, AcceptPIDFromInferior, connect_url, &lldb_error);
[applescript executeAndReturnError:nil];
thread_result_t accept_thread_result = NULL;
- if (Host::ThreadJoin (accept_thread, &accept_thread_result, &lldb_error))
+ lldb_error = accept_thread.Join(&accept_thread_result);
+ if (lldb_error.Success() && accept_thread_result)
{
- if (accept_thread_result)
- {
- pid = (intptr_t)accept_thread_result;
-
- // Wait for process to be stopped at the entry point by watching
- // for the process status to be set to SSTOP which indicates it it
- // SIGSTOP'ed at the entry point
- WaitForProcessToSIGSTOP (pid, 5);
- }
+ pid = (intptr_t)accept_thread_result;
+
+ // Wait for process to be stopped at the entry point by watching
+ // for the process status to be set to SSTOP which indicates it it
+ // SIGSTOP'ed at the entry point
+ WaitForProcessToSIGSTOP(pid, 5);
}
- ::unlink (unix_socket_name);
+
+ FileSystem::Unlink(unix_socket_name);
[applescript release];
if (pid != LLDB_INVALID_PROCESS_ID)
launch_info.SetProcessID (pid);
@@ -1492,13 +1388,9 @@ Host::LaunchProcess (ProcessLaunchInfo &launch_info)
return error;
}
-lldb::thread_t
-Host::StartMonitoringChildProcess (Host::MonitorChildProcessCallback callback,
- void *callback_baton,
- lldb::pid_t pid,
- bool monitor_signals)
+HostThread
+Host::StartMonitoringChildProcess(Host::MonitorChildProcessCallback callback, void *callback_baton, lldb::pid_t pid, bool monitor_signals)
{
- lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
unsigned long mask = DISPATCH_PROC_EXIT;
if (monitor_signals)
mask |= DISPATCH_PROC_SIGNAL;
@@ -1584,7 +1476,7 @@ Host::StartMonitoringChildProcess (Host::MonitorChildProcessCallback callback,
::dispatch_resume (source);
}
- return thread;
+ return HostThread();
}
//----------------------------------------------------------------------
diff --git a/lldb/source/Host/macosx/HostThreadMacOSX.mm b/lldb/source/Host/macosx/HostThreadMacOSX.mm
new file mode 100644
index 00000000000..c84a78efd9e
--- /dev/null
+++ b/lldb/source/Host/macosx/HostThreadMacOSX.mm
@@ -0,0 +1,102 @@
+//===-- 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 <CoreFoundation/CoreFoundation.h>
+#include <Foundation/Foundation.h>
+
+#include <objc/objc-auto.h>
+#include <pthread.h>
+
+using namespace lldb_private;
+
+namespace
+{
+
+pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT;
+pthread_key_t g_thread_create_key = 0;
+
+class MacOSXDarwinThread
+{
+ public:
+ MacOSXDarwinThread()
+ : m_pool(nil)
+ {
+ // Register our thread with the collector if garbage collection is enabled.
+ if (objc_collectingEnabled())
+ {
+#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5
+ // On Leopard and earlier there is no way objc_registerThreadWithCollector
+ // function, so we do it manually.
+ auto_zone_register_thread(auto_zone());
+#else
+ // On SnowLeopard and later we just call the thread registration function.
+ objc_registerThreadWithCollector();
+#endif
+ }
+ else
+ {
+ m_pool = [[NSAutoreleasePool alloc] init];
+ }
+ }
+
+ ~MacOSXDarwinThread()
+ {
+ if (m_pool)
+ {
+ [m_pool drain];
+ m_pool = nil;
+ }
+ }
+
+ static void
+ PThreadDestructor(void *v)
+ {
+ if (v)
+ delete static_cast<MacOSXDarwinThread *>(v);
+ ::pthread_setspecific(g_thread_create_key, NULL);
+ }
+
+ protected:
+ NSAutoreleasePool *m_pool;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MacOSXDarwinThread);
+};
+
+void
+InitThreadCreated()
+{
+ ::pthread_key_create(&g_thread_create_key, MacOSXDarwinThread::PThreadDestructor);
+}
+} // namespace
+
+HostThreadMacOSX::HostThreadMacOSX()
+ : HostThreadPosix()
+{
+}
+
+HostThreadMacOSX::HostThreadMacOSX(lldb::thread_t thread)
+ : HostThreadPosix(thread)
+{
+}
+
+lldb::thread_result_t
+HostThreadMacOSX::ThreadCreateTrampoline(lldb::thread_arg_t arg)
+{
+ ::pthread_once(&g_thread_create_once, InitThreadCreated);
+ if (g_thread_create_key)
+ {
+ ::pthread_setspecific(g_thread_create_key, new MacOSXDarwinThread());
+ }
+
+ return HostThreadPosix::ThreadCreateTrampoline(arg);
+}
diff --git a/lldb/source/Host/macosx/ThisThread.cpp b/lldb/source/Host/macosx/ThisThread.cpp
new file mode 100644
index 00000000000..95c7f2bf1e3
--- /dev/null
+++ b/lldb/source/Host/macosx/ThisThread.cpp
@@ -0,0 +1,39 @@
+//===-- ThisThread.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/ThisThread.h"
+
+#include <pthread.h>
+
+using namespace lldb_private;
+
+void
+ThisThread::SetName(llvm::StringRef name)
+{
+#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
+ ::pthread_setname_np(name);
+#endif
+}
+
+void
+ThisThread::GetName(llvm::SmallVectorImpl<char> &name)
+{
+#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
+ char pthread_name[1024];
+ 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])
+ {
+ name = queue_name;
+ }
+ }
+#endif
+}
diff --git a/lldb/source/Host/posix/HostThreadPosix.cpp b/lldb/source/Host/posix/HostThreadPosix.cpp
new file mode 100644
index 00000000000..4e62ad5abce
--- /dev/null
+++ b/lldb/source/Host/posix/HostThreadPosix.cpp
@@ -0,0 +1,63 @@
+//===-- 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/Core/Error.h"
+#include "lldb/Host/posix/HostThreadPosix.h"
+
+#include <pthread.h>
+
+using namespace lldb_private;
+
+HostThreadPosix::HostThreadPosix()
+{
+}
+
+HostThreadPosix::HostThreadPosix(lldb::thread_t thread)
+ : HostNativeThreadBase(thread)
+{
+}
+
+HostThreadPosix::~HostThreadPosix()
+{
+}
+
+Error
+HostThreadPosix::Join(lldb::thread_result_t *result)
+{
+ Error error;
+ lldb::thread_result_t thread_result;
+ int err = ::pthread_join(m_thread, &thread_result);
+ error.SetError(err, lldb::eErrorTypePOSIX);
+ if (err == 0)
+ {
+ m_state = (m_state == eThreadStateCancelling) ? eThreadStateCancelled : eThreadStateExited;
+ }
+ return error;
+}
+
+Error
+HostThreadPosix::Cancel()
+{
+ Error error;
+ int err = ::pthread_cancel(m_thread);
+ error.SetError(err, lldb::eErrorTypePOSIX);
+ if (err == 0)
+ m_state = eThreadStateCancelling;
+
+ return error;
+}
+
+Error
+HostThreadPosix::Detach()
+{
+ Error error;
+ int err = ::pthread_detach(m_thread);
+ error.SetError(err, lldb::eErrorTypePOSIX);
+ return error;
+}
diff --git a/lldb/source/Host/windows/Host.cpp b/lldb/source/Host/windows/Host.cpp
index bc0f48b49c7..4542025f6ee 100644
--- a/lldb/source/Host/windows/Host.cpp
+++ b/lldb/source/Host/windows/Host.cpp
@@ -110,12 +110,6 @@ Host::GetAuxvData(lldb_private::Process *process)
return 0;
}
-std::string
-Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
-{
- return std::string();
-}
-
lldb::tid_t
Host::GetCurrentThreadID()
{
@@ -128,26 +122,6 @@ Host::GetCurrentThread ()
return lldb::thread_t(::GetCurrentThread());
}
-bool
-Host::ThreadCancel (lldb::thread_t thread, Error *error)
-{
- int err = ::TerminateThread((HANDLE)thread, 0);
- return err == 0;
-}
-
-bool
-Host::ThreadDetach (lldb::thread_t thread, Error *error)
-{
- return ThreadCancel(thread, error);
-}
-
-bool
-Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
-{
- WaitForSingleObject((HANDLE) thread, INFINITE);
- return true;
-}
-
lldb::thread_key_t
Host::ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback)
{
@@ -166,19 +140,6 @@ Host::ThreadLocalStorageSet(lldb::thread_key_t key, void *value)
::TlsSetValue (key, value);
}
-bool
-Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
-{
- return false;
-}
-
-bool
-Host::SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid,
- const char *thread_name, size_t len)
-{
- return false;
-}
-
void
Host::Kill(lldb::pid_t pid, int signo)
{
@@ -260,14 +221,8 @@ Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
return true;
}
-lldb::thread_t
-Host::StartMonitoringChildProcess
-(
- Host::MonitorChildProcessCallback callback,
- void *callback_baton,
- lldb::pid_t pid,
- bool monitor_signals
-)
+HostThread
+Host::StartMonitoringChildProcess(Host::MonitorChildProcessCallback callback, void *callback_baton, lldb::pid_t pid, bool monitor_signals)
{
- return LLDB_INVALID_HOST_THREAD;
+ return HostThread();
} \ No newline at end of file
diff --git a/lldb/source/Host/windows/HostThreadWindows.cpp b/lldb/source/Host/windows/HostThreadWindows.cpp
new file mode 100644
index 00000000000..5d526cc2530
--- /dev/null
+++ b/lldb/source/Host/windows/HostThreadWindows.cpp
@@ -0,0 +1,80 @@
+//===-- 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/Core/Error.h"
+
+#include "lldb/Host/windows/windows.h"
+#include "lldb/Host/windows/HostThreadWindows.h"
+
+#include "llvm/ADT/STLExtras.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+HostThreadWindows::HostThreadWindows()
+ : HostNativeThreadBase()
+{
+}
+
+HostThreadWindows::HostThreadWindows(lldb::thread_t thread)
+ : HostNativeThreadBase(thread)
+{
+}
+
+HostThreadWindows::~HostThreadWindows()
+{
+ Reset();
+}
+
+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;
+ }
+
+ m_state = (m_state == eThreadStateCancelling) ? eThreadStateCancelled : eThreadStateExited;
+
+ if (result)
+ {
+ DWORD dword_result = 0;
+ if (!::GetExitCodeThread(m_thread, &dword_result))
+ *result = 0;
+ *result = dword_result;
+ }
+ return error;
+}
+
+Error
+HostThreadWindows::Cancel()
+{
+ Error error;
+
+ DWORD result = ::QueueUserAPC(::ExitThread, m_thread, 0);
+ error.SetError(result, eErrorTypeWin32);
+ return error;
+}
+
+lldb::tid_t
+HostThreadWindows::GetThreadId() const
+{
+ return ::GetThreadId(m_thread);
+}
+
+void
+HostThreadWindows::Reset()
+{
+ if (m_thread != LLDB_INVALID_HOST_THREAD)
+ ::CloseHandle(m_thread);
+
+ HostNativeThreadBase::Reset();
+}
diff --git a/lldb/source/Host/windows/ThisThread.cpp b/lldb/source/Host/windows/ThisThread.cpp
new file mode 100644
index 00000000000..9c37d7c57e7
--- /dev/null
+++ b/lldb/source/Host/windows/ThisThread.cpp
@@ -0,0 +1,60 @@
+//===-- ThisThread.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/Error.h"
+
+#include "lldb/Host/windows/windows.h"
+#include "lldb/Host/ThisThread.h"
+
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallVector.h"
+
+using namespace lldb;
+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)
+}
+
+void
+ThisThread::SetName(llvm::StringRef name)
+{
+// Other compilers don't yet support SEH, so we can only set the thread if compiling with MSVC.
+// TODO(zturner): Once clang-cl supports SEH, relax this conditional.
+#if defined(_MSC_VER)
+ THREADNAME_INFO info;
+ info.dwType = 0x1000;
+ info.szName = name.data();
+ info.dwThreadId = ::GetCurrentThreadId();
+ info.dwFlags = 0;
+
+ __try { ::RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info); }
+ __except(EXCEPTION_EXECUTE_HANDLER) {}
+#endif
+}
+
+void
+ThisThread::GetName(llvm::SmallVectorImpl<char> &name)
+{
+ // Getting the thread name is not supported on Windows.
+ // TODO(zturner): In SetName(), make a TLS entry that contains the thread's name, and in this function
+ // try to extract that TLS entry.
+ name.clear();
+}
diff --git a/lldb/source/Plugins/Process/CMakeLists.txt b/lldb/source/Plugins/Process/CMakeLists.txt
index b6008cfcd5d..ad51c892f42 100644
--- a/lldb/source/Plugins/Process/CMakeLists.txt
+++ b/lldb/source/Plugins/Process/CMakeLists.txt
@@ -7,7 +7,6 @@ elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
add_subdirectory(Windows)
elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
- add_subdirectory(POSIX)
add_subdirectory(MacOSX-Kernel)
endif()
add_subdirectory(gdb-remote)
diff --git a/lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp b/lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp
index 63439b15511..3227a391ee8 100644
--- a/lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp
+++ b/lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp
@@ -25,6 +25,7 @@
#include "lldb/Core/RegisterValue.h"
#include "lldb/Core/Scalar.h"
#include "lldb/Host/Host.h"
+#include "lldb/Host/ThreadLauncher.h"
#include "lldb/Target/Thread.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Utility/PseudoTerminal.h"
@@ -810,8 +811,6 @@ ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
const lldb_private::ProcessLaunchInfo & /* launch_info */,
lldb_private::Error &error)
: m_process(static_cast<ProcessFreeBSD *>(process)),
- m_operation_thread(LLDB_INVALID_HOST_THREAD),
- m_monitor_thread(LLDB_INVALID_HOST_THREAD),
m_pid(LLDB_INVALID_PROCESS_ID),
m_terminal_fd(-1),
m_operation(0)
@@ -852,7 +851,7 @@ WAIT_AGAIN:
// Finally, start monitoring the child process for change in state.
m_monitor_thread = Host::StartMonitoringChildProcess(
ProcessMonitor::MonitorCallback, this, GetPID(), true);
- if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
+ if (m_monitor_thread.GetState() != eThreadStateRunning)
{
error.SetErrorToGenericError();
error.SetErrorString("Process launch failed.");
@@ -864,8 +863,6 @@ ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
lldb::pid_t pid,
lldb_private::Error &error)
: m_process(static_cast<ProcessFreeBSD *>(process)),
- m_operation_thread(LLDB_INVALID_HOST_THREAD),
- m_monitor_thread(LLDB_INVALID_HOST_THREAD),
m_pid(pid),
m_terminal_fd(-1),
m_operation(0)
@@ -904,7 +901,7 @@ WAIT_AGAIN:
// Finally, start monitoring the child process for change in state.
m_monitor_thread = Host::StartMonitoringChildProcess(
ProcessMonitor::MonitorCallback, this, GetPID(), true);
- if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
+ if (m_monitor_thread.GetState() != eThreadStateRunning)
{
error.SetErrorToGenericError();
error.SetErrorString("Process attach failed.");
@@ -924,11 +921,10 @@ ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
{
static const char *g_thread_name = "lldb.process.freebsd.operation";
- if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
+ if (m_operation_thread.GetState() == eThreadStateRunning)
return;
- m_operation_thread =
- Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
+ m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
}
void *
@@ -1101,11 +1097,10 @@ ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error
{
static const char *g_thread_name = "lldb.process.freebsd.operation";
- if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
+ if (m_operation_thread.GetState() == eThreadStateRunning)
return;
- m_operation_thread =
- Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
+ m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
}
void *
@@ -1714,13 +1709,11 @@ ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
void
ProcessMonitor::StopMonitoringChildProcess()
{
- lldb::thread_result_t thread_result;
-
- if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
+ if (m_monitor_thread.GetState() == eThreadStateRunning)
{
- Host::ThreadCancel(m_monitor_thread, NULL);
- Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
- m_monitor_thread = LLDB_INVALID_HOST_THREAD;
+ m_monitor_thread.Cancel();
+ m_monitor_thread.Join(nullptr);
+ m_monitor_thread.Reset();
}
}
@@ -1764,12 +1757,10 @@ ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
void
ProcessMonitor::StopOpThread()
{
- lldb::thread_result_t result;
-
- if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
+ if (m_operation_thread.GetState() != eThreadStateRunning)
return;
- Host::ThreadCancel(m_operation_thread, NULL);
- Host::ThreadJoin(m_operation_thread, &result, NULL);
- m_operation_thread = LLDB_INVALID_HOST_THREAD;
+ m_operation_thread.Cancel();
+ m_operation_thread.Join(nullptr);
+ m_operation_thread.Reset();
}
diff --git a/lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.h b/lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.h
index 314743b0075..0f319348b46 100644
--- a/lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.h
+++ b/lldb/source/Plugins/Process/FreeBSD/ProcessMonitor.h
@@ -17,6 +17,7 @@
// C++ Includes
// Other libraries and framework includes
#include "lldb/lldb-types.h"
+#include "lldb/Host/HostThread.h"
#include "lldb/Host/Mutex.h"
namespace lldb_private
@@ -212,8 +213,8 @@ public:
private:
ProcessFreeBSD *m_process;
- lldb::thread_t m_operation_thread;
- lldb::thread_t m_monitor_thread;
+ HostThread m_operation_thread;
+ HostThread m_monitor_thread;
lldb::pid_t m_pid;
int m_terminal_fd;
diff --git a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
index 22f355a7c89..1bfeb54c3da 100644
--- a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
+++ b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
@@ -45,6 +45,7 @@
#include "lldb/Core/State.h"
#include "lldb/Host/Host.h"
#include "lldb/Host/HostInfo.h"
+#include "lldb/Host/ThreadLauncher.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Target/NativeRegisterContext.h"
#include "lldb/Target/ProcessLaunchInfo.h"
@@ -1209,8 +1210,6 @@ NativeProcessLinux::AttachToProcess (
NativeProcessLinux::NativeProcessLinux () :
NativeProcessProtocol (LLDB_INVALID_PROCESS_ID),
m_arch (),
- m_operation_thread (LLDB_INVALID_HOST_THREAD),
- m_monitor_thread (LLDB_INVALID_HOST_THREAD),
m_operation (nullptr),
m_operation_mutex (),
m_operation_pending (),
@@ -1289,7 +1288,7 @@ WAIT_AGAIN:
// Finally, start monitoring the child process for change in state.
m_monitor_thread = Host::StartMonitoringChildProcess(
NativeProcessLinux::MonitorCallback, this, GetID(), true);
- if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
+ if (m_monitor_thread.GetState() != eThreadStateRunning)
{
error.SetErrorToGenericError();
error.SetErrorString ("Process attach failed to create monitor thread for NativeProcessLinux::MonitorCallback.");
@@ -1367,7 +1366,7 @@ WAIT_AGAIN:
// Finally, start monitoring the child process for change in state.
m_monitor_thread = Host::StartMonitoringChildProcess (
NativeProcessLinux::MonitorCallback, this, GetID (), true);
- if (!IS_VALID_LLDB_HOST_THREAD (m_monitor_thread))
+ if (m_monitor_thread.GetState() != eThreadStateRunning)
{
error.SetErrorToGenericError ();
error.SetErrorString ("Process attach failed to create monitor thread for NativeProcessLinux::MonitorCallback.");
@@ -1388,11 +1387,10 @@ NativeProcessLinux::StartLaunchOpThread(LaunchArgs *args, Error &error)
{
static const char *g_thread_name = "lldb.process.nativelinux.operation";
- if (IS_VALID_LLDB_HOST_THREAD (m_operation_thread))
+ if (m_operation_thread.GetState() == eThreadStateRunning)
return;
- m_operation_thread =
- Host::ThreadCreate (g_thread_name, LaunchOpThread, args, &error);
+ m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
}
void *
@@ -1698,11 +1696,10 @@ NativeProcessLinux::StartAttachOpThread(AttachArgs *args, lldb_private::Error &e
{
static const char *g_thread_name = "lldb.process.linux.operation";
- if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
+ if (m_operation_thread.GetState() == eThreadStateRunning)
return;
- m_operation_thread =
- Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
+ m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
}
void *
@@ -3396,13 +3393,11 @@ NativeProcessLinux::DupDescriptor(const char *path, int fd, int flags)
void
NativeProcessLinux::StopMonitoringChildProcess()
{
- lldb::thread_result_t thread_result;
-
- if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
+ if (m_monitor_thread.GetState() == eThreadStateRunning)
{
- Host::ThreadCancel(m_monitor_thread, NULL);
- Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
- m_monitor_thread = LLDB_INVALID_HOST_THREAD;
+ m_monitor_thread.Cancel();
+ m_monitor_thread.Join(nullptr);
+ m_monitor_thread.Reset();
}
}
@@ -3424,14 +3419,12 @@ NativeProcessLinux::StopMonitor()
void
NativeProcessLinux::StopOpThread()
{
- lldb::thread_result_t result;
-
- if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
+ if (m_operation_thread.GetState() != eThreadStateRunning)
return;
- Host::ThreadCancel(m_operation_thread, NULL);
- Host::ThreadJoin(m_operation_thread, &result, NULL);
- m_operation_thread = LLDB_INVALID_HOST_THREAD;
+ m_operation_thread.Cancel();
+ m_operation_thread.Join(nullptr);
+ m_operation_thread.Reset();
}
bool
diff --git a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
index ee71376a3b2..c94a78bd2ea 100644
--- a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
+++ b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
@@ -21,6 +21,7 @@
#include "lldb/Core/ArchSpec.h"
#include "lldb/lldb-types.h"
#include "lldb/Host/Debug.h"
+#include "lldb/Host/HostThread.h"
#include "lldb/Host/Mutex.h"
#include "lldb/Target/MemoryRegionInfo.h"
@@ -170,8 +171,8 @@ namespace lldb_private
lldb_private::ArchSpec m_arch;
- lldb::thread_t m_operation_thread;
- lldb::thread_t m_monitor_thread;
+ HostThread m_operation_thread;
+ HostThread m_monitor_thread;
// current operation which must be executed on the priviliged thread
void *m_operation;
diff --git a/lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp b/lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp
index d9e80d6c459..8e010a55d85 100644
--- a/lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp
+++ b/lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp
@@ -18,8 +18,12 @@
#include "lldb/Core/State.h"
#include "lldb/Host/Host.h"
#include "lldb/Host/HostInfo.h"
+#include "lldb/Host/HostNativeThread.h"
#include "lldb/lldb-enumerations.h"
#include "lldb/lldb-private-log.h"
+
+#include "llvm/ADT/SmallString.h"
+
#include "Plugins/Process/Utility/RegisterContextLinux_arm64.h"
#include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
#include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
@@ -65,7 +69,9 @@ NativeThreadLinux::GetName()
return "<unknown: no process>";
// const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ());
- return Host::GetThreadName (process_sp->GetID (), GetID ()).c_str ();
+ llvm::SmallString<32> thread_name;
+ HostNativeThread::GetName(GetID(), thread_name);
+ return thread_name.c_str();
}
lldb::StateType
diff --git a/lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp b/lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp
index bfe8dc984a4..c830124fe91 100644
--- a/lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp
+++ b/lldb/source/Plugins/Process/Linux/ProcessMonitor.cpp
@@ -32,6 +32,8 @@
#include "lldb/Core/RegisterValue.h"
#include "lldb/Core/Scalar.h"
#include "lldb/Host/Host.h"
+#include "lldb/Host/HostThread.h"
+#include "lldb/Host/ThreadLauncher.h"
#include "lldb/Target/Thread.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Utility/PseudoTerminal.h"
@@ -1104,7 +1106,7 @@ WAIT_AGAIN:
// Finally, start monitoring the child process for change in state.
m_monitor_thread = Host::StartMonitoringChildProcess(
ProcessMonitor::MonitorCallback, this, GetPID(), true);
- if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
+ if (m_monitor_thread.GetState() != eThreadStateRunning)
{
error.SetErrorToGenericError();
error.SetErrorString("Process launch failed.");
@@ -1155,7 +1157,7 @@ WAIT_AGAIN:
// Finally, start monitoring the child process for change in state.
m_monitor_thread = Host::StartMonitoringChildProcess(
ProcessMonitor::MonitorCallback, this, GetPID(), true);
- if (!IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
+ if (m_monitor_thread.GetState() != eThreadStateRunning)
{
error.SetErrorToGenericError();
error.SetErrorString("Process attach failed.");
@@ -1175,11 +1177,10 @@ ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
{
static const char *g_thread_name = "lldb.process.linux.operation";
- if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
+ if (m_operation_thread.GetState() == eThreadStateRunning)
return;
- m_operation_thread =
- Host::ThreadCreate(g_thread_name, LaunchOpThread, args, &error);
+ m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
}
void *
@@ -1399,11 +1400,10 @@ ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error
{
static const char *g_thread_name = "lldb.process.linux.operation";
- if (IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
+ if (m_operation_thread.GetState() == eThreadStateRunning)
return;
- m_operation_thread =
- Host::ThreadCreate(g_thread_name, AttachOpThread, args, &error);
+ m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
}
void *
@@ -2373,13 +2373,11 @@ ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
void
ProcessMonitor::StopMonitoringChildProcess()
{
- lldb::thread_result_t thread_result;
-
- if (IS_VALID_LLDB_HOST_THREAD(m_monitor_thread))
+ if (m_monitor_thread.GetState() == eThreadStateRunning)
{
- Host::ThreadCancel(m_monitor_thread, NULL);
- Host::ThreadJoin(m_monitor_thread, &thread_result, NULL);
- m_monitor_thread = LLDB_INVALID_HOST_THREAD;
+ m_monitor_thread.Cancel();
+ m_monitor_thread.Join(nullptr);
+ m_monitor_thread.Reset();
}
}
@@ -2400,12 +2398,10 @@ ProcessMonitor::StopMonitor()
void
ProcessMonitor::StopOpThread()
{
- lldb::thread_result_t result;
-
- if (!IS_VALID_LLDB_HOST_THREAD(m_operation_thread))
+ if (m_operation_thread.GetState() != eThreadStateRunning)
return;
- Host::ThreadCancel(m_operation_thread, NULL);
- Host::ThreadJoin(m_operation_thread, &result, NULL);
- m_operation_thread = LLDB_INVALID_HOST_THREAD;
+ m_operation_thread.Cancel();
+ m_operation_thread.Join(nullptr);
+ m_operation_thread.Reset();
}
diff --git a/lldb/source/Plugins/Process/Linux/ProcessMonitor.h b/lldb/source/Plugins/Process/Linux/ProcessMonitor.h
index bd3253412a1..5e83665b47d 100644
--- a/lldb/source/Plugins/Process/Linux/ProcessMonitor.h
+++ b/lldb/source/Plugins/Process/Linux/ProcessMonitor.h
@@ -17,6 +17,7 @@
// C++ Includes
// Other libraries and framework includes
#include "lldb/lldb-types.h"
+#include "lldb/Host/HostThread.h"
#include "lldb/Host/Mutex.h"
namespace lldb_private
@@ -195,8 +196,8 @@ public:
private:
ProcessLinux *m_process;
- lldb::thread_t m_operation_thread;
- lldb::thread_t m_monitor_thread;
+ lldb_private::HostThread m_operation_thread;
+ lldb_private::HostThread m_monitor_thread;
lldb::pid_t m_pid;
int m_terminal_fd;
diff --git a/lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp b/lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
index 8b1850dcccf..9ea65adf5c6 100644
--- a/lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
+++ b/lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
@@ -23,6 +23,7 @@
#include "lldb/Host/Host.h"
#include "lldb/Host/Symbols.h"
#include "lldb/Host/Socket.h"
+#include "lldb/Host/ThreadLauncher.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandObject.h"
#include "lldb/Interpreter/CommandObjectMultiword.h"
@@ -176,7 +177,6 @@ ProcessKDP::ProcessKDP(Target& target, Listener &listener) :
Process (target, listener),
m_comm("lldb.process.kdp-remote.communication"),
m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"),
- m_async_thread (LLDB_INVALID_HOST_THREAD),
m_dyld_plugin_name (),
m_kernel_load_addr (LLDB_INVALID_ADDRESS),
m_command_sp(),
@@ -469,8 +469,8 @@ ProcessKDP::DoResume ()
Error error;
Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS));
// Only start the async thread if we try to do any process control
- if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
- StartAsyncThread ();
+ if (m_async_thread.GetState() != eThreadStateRunning)
+ StartAsyncThread();
bool resume = false;
@@ -869,12 +869,12 @@ ProcessKDP::StartAsyncThread ()
if (log)
log->Printf ("ProcessKDP::StartAsyncThread ()");
-
- if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
+
+ if (m_async_thread.GetState() == eThreadStateRunning)
return true;
- m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
- return IS_VALID_LLDB_HOST_THREAD(m_async_thread);
+ m_async_thread = ThreadLauncher::LaunchThread("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
+ return m_async_thread.GetState() == eThreadStateRunning;
}
void
@@ -888,10 +888,10 @@ ProcessKDP::StopAsyncThread ()
m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
// Stop the stdio thread
- if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
+ if (m_async_thread.GetState() == eThreadStateRunning)
{
- Host::ThreadJoin (m_async_thread, NULL, NULL);
- m_async_thread = LLDB_INVALID_HOST_THREAD;
+ m_async_thread.Join(nullptr);
+ m_async_thread.Reset();
}
}
@@ -1003,8 +1003,8 @@ ProcessKDP::AsyncThread (void *arg)
log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...",
arg,
pid);
-
- process->m_async_thread = LLDB_INVALID_HOST_THREAD;
+
+ process->m_async_thread.Reset();
return NULL;
}
diff --git a/lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h b/lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h
index f6432bbe5aa..f7f12350fa5 100644
--- a/lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h
+++ b/lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h
@@ -24,6 +24,7 @@
#include "lldb/Core/StreamString.h"
#include "lldb/Core/StringList.h"
#include "lldb/Core/ThreadSafeValue.h"
+#include "lldb/Host/HostThread.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Thread.h"
@@ -246,7 +247,7 @@ protected:
//------------------------------------------------------------------
CommunicationKDP m_comm;
lldb_private::Broadcaster m_async_broadcaster;
- lldb::thread_t m_async_thread;
+ lldb_private::HostThread m_async_thread;
lldb_private::ConstString m_dyld_plugin_name;
lldb::addr_t m_kernel_load_addr;
lldb::CommandObjectSP m_command_sp;
diff --git a/lldb/source/Plugins/Process/POSIX/POSIXThread.cpp b/lldb/source/Plugins/Process/POSIX/POSIXThread.cpp
index d48f8f9dd30..c6c646915d5 100644
--- a/lldb/source/Plugins/Process/POSIX/POSIXThread.cpp
+++ b/lldb/source/Plugins/Process/POSIX/POSIXThread.cpp
@@ -20,11 +20,13 @@
#include "lldb/Core/Debugger.h"
#include "lldb/Core/State.h"
#include "lldb/Host/Host.h"
+#include "lldb/Host/HostNativeThread.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/StopInfo.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/ThreadSpec.h"
+#include "llvm/ADT/SmallString.h"
#include "POSIXStopInfo.h"
#include "POSIXThread.h"
#include "ProcessPOSIX.h"
@@ -140,7 +142,9 @@ POSIXThread::GetName ()
{
if (!m_thread_name_valid)
{
- SetName(Host::GetThreadName(GetProcess()->GetID(), GetID()).c_str());
+ llvm::SmallString<32> thread_name;
+ HostNativeThread::GetName(GetID(), thread_name);
+ m_thread_name = thread_name.c_str();
m_thread_name_valid = true;
}
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
index 1f4dd93976e..2db518dee62 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
@@ -26,6 +26,7 @@
#include "lldb/Host/Host.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Host/Socket.h"
+#include "lldb/Host/ThreadLauncher.h"
#include "lldb/Host/TimeValue.h"
#include "lldb/Target/Process.h"
@@ -153,7 +154,6 @@ GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name,
m_history (512),
m_send_acks (true),
m_is_platform (is_platform),
- m_listen_thread (LLDB_INVALID_HOST_THREAD),
m_listen_url ()
{
}
@@ -606,7 +606,7 @@ Error
GDBRemoteCommunication::StartListenThread (const char *hostname, uint16_t port)
{
Error error;
- if (IS_VALID_LLDB_HOST_THREAD(m_listen_thread))
+ if (m_listen_thread.GetState() == eThreadStateRunning)
{
error.SetErrorString("listen thread already running");
}
@@ -619,7 +619,7 @@ GDBRemoteCommunication::StartListenThread (const char *hostname, uint16_t port)
snprintf(listen_url, sizeof(listen_url), "listen://%i", port);
m_listen_url = listen_url;
SetConnection(new ConnectionFileDescriptor());
- m_listen_thread = Host::ThreadCreate (listen_url, GDBRemoteCommunication::ListenThread, this, &error);
+ m_listen_thread = ThreadLauncher::LaunchThread(listen_url, GDBRemoteCommunication::ListenThread, this, &error);
}
return error;
}
@@ -627,10 +627,10 @@ GDBRemoteCommunication::StartListenThread (const char *hostname, uint16_t port)
bool
GDBRemoteCommunication::JoinListenThread ()
{
- if (IS_VALID_LLDB_HOST_THREAD(m_listen_thread))
+ if (m_listen_thread.GetState() == eThreadStateRunning)
{
- Host::ThreadJoin(m_listen_thread, NULL, NULL);
- m_listen_thread = LLDB_INVALID_HOST_THREAD;
+ m_listen_thread.Join(nullptr);
+ m_listen_thread.Reset();
}
return true;
}
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
index b11d3856320..ac203a62788 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
@@ -20,6 +20,7 @@
#include "lldb/lldb-public.h"
#include "lldb/Core/Communication.h"
#include "lldb/Core/Listener.h"
+#include "lldb/Host/HostThread.h"
#include "lldb/Host/Mutex.h"
#include "lldb/Host/Predicate.h"
#include "lldb/Host/TimeValue.h"
@@ -281,8 +282,7 @@ protected:
ListenThread (lldb::thread_arg_t arg);
private:
-
- lldb::thread_t m_listen_thread;
+ lldb_private::HostThread m_listen_thread;
std::string m_listen_url;
diff --git a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
index f35d954caa7..28708be5ccb 100644
--- a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -42,7 +42,9 @@
#include "lldb/Core/StreamString.h"
#include "lldb/Core/Timer.h"
#include "lldb/Core/Value.h"
+#include "lldb/Host/HostThread.h"
#include "lldb/Host/Symbols.h"
+#include "lldb/Host/ThreadLauncher.h"
#include "lldb/Host/TimeValue.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandObject.h"
@@ -272,8 +274,6 @@ ProcessGDBRemote::ProcessGDBRemote(Target& target, Listener &listener) :
m_last_stop_packet_mutex (Mutex::eMutexTypeNormal),
m_register_info (),
m_async_broadcaster (NULL, "lldb.process.gdb-remote.async-broadcaster"),
- m_async_thread (LLDB_INVALID_HOST_THREAD),
- m_async_thread_state(eAsyncThreadNotStarted),
m_async_thread_state_mutex(Mutex::eMutexTypeRecursive),
m_thread_ids (),
m_continue_c_tids (),
@@ -1432,7 +1432,7 @@ ProcessGDBRemote::DoResume ()
TimeValue timeout;
timeout = TimeValue::Now();
timeout.OffsetWithSeconds (5);
- if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
+ if (m_async_thread.GetState() != eThreadStateRunning)
{
error.SetErrorString ("Trying to resume but the async thread is dead.");
if (log)
@@ -2891,30 +2891,22 @@ ProcessGDBRemote::StartAsyncThread ()
log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
Mutex::Locker start_locker(m_async_thread_state_mutex);
- if (m_async_thread_state == eAsyncThreadNotStarted)
+ if (m_async_thread.GetState() != eThreadStateRunning)
{
// Create a thread that watches our internal state and controls which
// events make it to clients (into the DCProcess event queue).
- m_async_thread = Host::ThreadCreate ("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL);
- if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
- {
- m_async_thread_state = eAsyncThreadRunning;
- return true;
- }
- else
- return false;
+
+ m_async_thread = ThreadLauncher::LaunchThread("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL);
}
else
{
// Somebody tried to start the async thread while it was either being started or stopped. If the former, and
// it started up successfully, then say all's well. Otherwise it is an error, since we aren't going to restart it.
if (log)
- log->Printf ("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread_state);
- if (m_async_thread_state == eAsyncThreadRunning)
- return true;
- else
- return false;
+ log->Printf("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread.GetState());
}
+
+ return (m_async_thread.GetState() == eThreadStateRunning);
}
void
@@ -2926,7 +2918,7 @@ ProcessGDBRemote::StopAsyncThread ()
log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
Mutex::Locker start_locker(m_async_thread_state_mutex);
- if (m_async_thread_state == eAsyncThreadRunning)
+ if (m_async_thread.GetState() == eThreadStateRunning)
{
m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
@@ -2934,16 +2926,12 @@ ProcessGDBRemote::StopAsyncThread ()
m_gdb_comm.Disconnect(); // Disconnect from the debug server.
// Stop the stdio thread
- if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
- {
- Host::ThreadJoin (m_async_thread, NULL, NULL);
- }
- m_async_thread_state = eAsyncThreadDone;
+ m_async_thread.Join(nullptr);
}
else
{
if (log)
- log->Printf ("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread_state);
+ log->Printf("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread.GetState());
}
}
@@ -3086,7 +3074,7 @@ ProcessGDBRemote::AsyncThread (void *arg)
if (log)
log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", __FUNCTION__, arg, process->GetID());
- process->m_async_thread = LLDB_INVALID_HOST_THREAD;
+ process->m_async_thread.Reset();
return NULL;
}
diff --git a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
index 942b31c84dd..5e1365c86ee 100644
--- a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
+++ b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
@@ -25,6 +25,7 @@
#include "lldb/Core/StringList.h"
#include "lldb/Core/StructuredData.h"
#include "lldb/Core/ThreadSafeValue.h"
+#include "lldb/Host/HostThread.h"
#include "lldb/lldb-private-forward.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Thread.h"
@@ -324,13 +325,6 @@ protected:
eBroadcastBitAsyncThreadShouldExit = (1 << 1),
eBroadcastBitAsyncThreadDidExit = (1 << 2)
};
-
- typedef enum AsyncThreadState
- {
- eAsyncThreadNotStarted,
- eAsyncThreadRunning,
- eAsyncThreadDone
- } AsyncThreadState;
lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
GDBRemoteCommunicationClient m_gdb_comm;
@@ -339,8 +333,7 @@ protected:
lldb_private::Mutex m_last_stop_packet_mutex;
GDBRemoteDynamicRegisterInfo m_register_info;
lldb_private::Broadcaster m_async_broadcaster;
- lldb::thread_t m_async_thread;
- AsyncThreadState m_async_thread_state;
+ lldb_private::HostThread m_async_thread;
lldb_private::Mutex m_async_thread_state_mutex;
typedef std::vector<lldb::tid_t> tid_collection;
typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
index 28f02359b74..41dcbf45042 100644
--- a/lldb/source/Target/Process.cpp
+++ b/lldb/source/Target/Process.cpp
@@ -27,8 +27,10 @@
#include "lldb/Expression/ClangUserExpression.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Host/Host.h"
+#include "lldb/Host/HostInfo.h"
#include "lldb/Host/Pipe.h"
#include "lldb/Host/Terminal.h"
+#include "lldb/Host/ThreadLauncher.h"
#include "lldb/Target/ABI.h"
#include "lldb/Target/DynamicLoader.h"
#include "lldb/Target/JITLoader.h"
@@ -680,7 +682,6 @@ Process::Process(Target &target, Listener &listener, const UnixSignalsSP &unix_s
m_private_state_control_broadcaster (NULL, "lldb.process.internal_state_control_broadcaster"),
m_private_state_listener ("lldb.process.internal_state_listener"),
m_private_state_control_wait(),
- m_private_state_thread (LLDB_INVALID_HOST_THREAD),
m_mod_id (),
m_process_unique_id(0),
m_thread_index_id (0),
@@ -770,6 +771,11 @@ Process::~Process()
if (log)
log->Printf ("%p Process::~Process()", static_cast<void*>(this));
StopPrivateStateThread();
+
+ // ThreadList::Clear() will try to acquire this process's mutex, so
+ // explicitly clear the thread list here to ensure that the mutex
+ // is not destroyed before the thread list.
+ m_thread_list.Clear();
}
const ProcessPropertiesSP &
@@ -3833,26 +3839,25 @@ Process::StartPrivateStateThread (bool force)
// events make it to clients (into the DCProcess event queue).
char thread_name[1024];
- if (Host::MAX_THREAD_NAME_LENGTH <= 16)
+ if (HostInfo::GetMaxThreadNameLength() <= 30)
{
- // On platforms with abbreviated thread name lengths, choose thread names that fit within the limit.
- if (already_running)
- snprintf(thread_name, sizeof(thread_name), "intern-state-OV");
- else
- snprintf(thread_name, sizeof(thread_name), "intern-state");
+ // On platforms with abbreviated thread name lengths, choose thread names that fit within the limit.
+ if (already_running)
+ snprintf(thread_name, sizeof(thread_name), "intern-state-OV");
+ else
+ snprintf(thread_name, sizeof(thread_name), "intern-state");
}
else
{
if (already_running)
- snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", GetID());
+ snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", GetID());
else
- snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID());
+ snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID());
}
// Create the private state thread, and start it running.
- m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL);
- bool success = IS_VALID_LLDB_HOST_THREAD(m_private_state_thread);
- if (success)
+ m_private_state_thread = ThreadLauncher::LaunchThread(thread_name, Process::PrivateStateThread, this, NULL);
+ if (m_private_state_thread.GetState() == eThreadStateRunning)
{
ResumePrivateStateThread();
return true;
@@ -3901,8 +3906,8 @@ Process::ControlPrivateStateThread (uint32_t signal)
// Signal the private state thread. First we should copy this is case the
// thread starts exiting since the private state thread will NULL this out
// when it exits
- const lldb::thread_t private_state_thread = m_private_state_thread;
- if (IS_VALID_LLDB_HOST_THREAD(private_state_thread))
+ HostThread private_state_thread(m_private_state_thread);
+ if (private_state_thread.GetState() == eThreadStateRunning)
{
TimeValue timeout_time;
bool timed_out;
@@ -3920,8 +3925,7 @@ Process::ControlPrivateStateThread (uint32_t signal)
{
if (timed_out)
{
- Error error;
- Host::ThreadCancel (private_state_thread, &error);
+ Error error = private_state_thread.Cancel();
if (log)
log->Printf ("Timed out responding to the control event, cancel got error: \"%s\".", error.AsCString());
}
@@ -3932,8 +3936,8 @@ Process::ControlPrivateStateThread (uint32_t signal)
}
thread_result_t result = NULL;
- Host::ThreadJoin (private_state_thread, &result, NULL);
- m_private_state_thread = LLDB_INVALID_HOST_THREAD;
+ private_state_thread.Join(&result);
+ m_private_state_thread.Reset();
}
}
else
@@ -4177,7 +4181,7 @@ Process::RunPrivateStateThread ()
m_public_run_lock.SetStopped();
m_private_state_control_wait.SetValue (true, eBroadcastAlways);
- m_private_state_thread = LLDB_INVALID_HOST_THREAD;
+ m_private_state_thread.Reset();
return NULL;
}
@@ -4954,12 +4958,12 @@ Process::RunThreadPlan (ExecutionContext &exe_ctx,
selected_tid = LLDB_INVALID_THREAD_ID;
}
- lldb::thread_t backup_private_state_thread = LLDB_INVALID_HOST_THREAD;
+ HostThread backup_private_state_thread;
lldb::StateType old_state;
lldb::ThreadPlanSP stopper_base_plan_sp;
Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
- if (Host::GetCurrentThread() == m_private_state_thread)
+ if (m_private_state_thread.EqualsThread(Host::GetCurrentThread()))
{
// Yikes, we are running on the private state thread! So we can't wait for public events on this thread, since
// we are the thread that is generating public events.
@@ -5564,7 +5568,7 @@ Process::RunThreadPlan (ExecutionContext &exe_ctx,
} // END WAIT LOOP
// If we had to start up a temporary private state thread to run this thread plan, shut it down now.
- if (IS_VALID_LLDB_HOST_THREAD(backup_private_state_thread))
+ if (backup_private_state_thread.GetState() != eThreadStateInvalid)
{
StopPrivateStateThread();
Error error;
OpenPOWER on IntegriCloud