[Lldb-commits] [lldb] r365226 - Change LaunchThread interface to return an expected.

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Fri Jul 5 10:42:08 PDT 2019


Author: jdevlieghere
Date: Fri Jul  5 10:42:08 2019
New Revision: 365226

URL: http://llvm.org/viewvc/llvm-project?rev=365226&view=rev
Log:
Change LaunchThread interface to return an expected.

Change the interface to return an expected, instead of taking a Status
pointer.

Differential revision: https://reviews.llvm.org/D64163

Modified:
    lldb/trunk/include/lldb/Host/ThreadLauncher.h
    lldb/trunk/source/API/SBHostOS.cpp
    lldb/trunk/source/Core/Communication.cpp
    lldb/trunk/source/Core/Debugger.cpp
    lldb/trunk/source/Host/common/TaskPool.cpp
    lldb/trunk/source/Host/common/ThreadLauncher.cpp
    lldb/trunk/source/Host/macosx/objcxx/Host.mm
    lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
    lldb/trunk/source/Target/Process.cpp

Modified: lldb/trunk/include/lldb/Host/ThreadLauncher.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/ThreadLauncher.h?rev=365226&r1=365225&r2=365226&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/ThreadLauncher.h (original)
+++ lldb/trunk/include/lldb/Host/ThreadLauncher.h Fri Jul  5 10:42:08 2019
@@ -1,5 +1,4 @@
-//===-- ThreadLauncher.h -----------------------------------------*- C++
-//-*-===//
+//===-- ThreadLauncher.h ----------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -11,18 +10,18 @@
 #define lldb_Host_ThreadLauncher_h_
 
 #include "lldb/Host/HostThread.h"
-#include "lldb/Utility/Status.h"
 #include "lldb/lldb-types.h"
 
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Error.h"
 
 namespace lldb_private {
 
 class ThreadLauncher {
 public:
-  static HostThread
+  static llvm::Expected<HostThread>
   LaunchThread(llvm::StringRef name, lldb::thread_func_t thread_function,
-               lldb::thread_arg_t thread_arg, Status *error_ptr,
+               lldb::thread_arg_t thread_arg,
                size_t min_stack_byte_size = 0); // Minimum stack size in bytes,
                                                 // set stack size to zero for
                                                 // default platform thread stack

Modified: lldb/trunk/source/API/SBHostOS.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBHostOS.cpp?rev=365226&r1=365225&r2=365226&view=diff
==============================================================================
--- lldb/trunk/source/API/SBHostOS.cpp (original)
+++ lldb/trunk/source/API/SBHostOS.cpp Fri Jul  5 10:42:08 2019
@@ -107,10 +107,17 @@ lldb::thread_t SBHostOS::ThreadCreate(co
   LLDB_RECORD_DUMMY(lldb::thread_t, SBHostOS, ThreadCreate,
                     (lldb::thread_func_t, void *, SBError *), name,
                     thread_function, thread_arg, error_ptr);
-  HostThread thread(
-      ThreadLauncher::LaunchThread(name, thread_function, thread_arg,
-                                   error_ptr ? error_ptr->get() : nullptr));
-  return thread.Release();
+  llvm::Expected<HostThread> thread =
+      ThreadLauncher::LaunchThread(name, thread_function, thread_arg);
+  if (!thread) {
+    if (error_ptr)
+      error_ptr->SetError(Status(thread.takeError()));
+    else
+      llvm::consumeError(thread.takeError());
+    return LLDB_INVALID_HOST_THREAD;
+  }
+
+  return thread->Release();
 }
 
 void SBHostOS::ThreadCreated(const char *name) {

Modified: lldb/trunk/source/Core/Communication.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Communication.cpp?rev=365226&r1=365225&r2=365226&view=diff
==============================================================================
--- lldb/trunk/source/Core/Communication.cpp (original)
+++ lldb/trunk/source/Core/Communication.cpp Fri Jul  5 10:42:08 2019
@@ -204,10 +204,23 @@ bool Communication::StartReadThread(Stat
 
   m_read_thread_enabled = true;
   m_read_thread_did_exit = false;
-  m_read_thread = ThreadLauncher::LaunchThread(
-      thread_name, Communication::ReadThread, this, error_ptr);
+  auto maybe_thread = ThreadLauncher::LaunchThread(
+      thread_name, Communication::ReadThread, this);
+  if (maybe_thread) {
+    m_read_thread = *maybe_thread;
+  } else {
+    if (error_ptr)
+      *error_ptr = Status(maybe_thread.takeError());
+    else {
+      LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST),
+               "failed to launch host thread: {}",
+               llvm::toString(maybe_thread.takeError()));
+    }
+  }
+
   if (!m_read_thread.IsJoinable())
     m_read_thread_enabled = false;
+
   return m_read_thread_enabled;
 }
 

Modified: lldb/trunk/source/Core/Debugger.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Debugger.cpp?rev=365226&r1=365225&r2=365226&view=diff
==============================================================================
--- lldb/trunk/source/Core/Debugger.cpp (original)
+++ lldb/trunk/source/Core/Debugger.cpp Fri Jul  5 10:42:08 2019
@@ -1642,8 +1642,17 @@ bool Debugger::StartEventHandlerThread()
         full_name.AsCString() : "dbg.evt-handler";
 
     // Use larger 8MB stack for this thread
-    m_event_handler_thread = ThreadLauncher::LaunchThread(thread_name,
-        EventHandlerThread, this, nullptr, g_debugger_event_thread_stack_bytes);
+    llvm::Expected<HostThread> event_handler_thread =
+        ThreadLauncher::LaunchThread(thread_name, EventHandlerThread, this,
+                                     g_debugger_event_thread_stack_bytes);
+
+    if (event_handler_thread) {
+      m_event_handler_thread = *event_handler_thread;
+    } else {
+      LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST),
+               "failed to launch host thread: {}",
+               llvm::toString(event_handler_thread.takeError()));
+    }
 
     // Make sure DefaultEventHandler() is running and listening to events
     // before we return from this function. We are only listening for events of
@@ -1674,10 +1683,18 @@ lldb::thread_result_t Debugger::IOHandle
 bool Debugger::HasIOHandlerThread() { return m_io_handler_thread.IsJoinable(); }
 
 bool Debugger::StartIOHandlerThread() {
-  if (!m_io_handler_thread.IsJoinable())
-    m_io_handler_thread = ThreadLauncher::LaunchThread(
-        "lldb.debugger.io-handler", IOHandlerThread, this, nullptr,
+  if (!m_io_handler_thread.IsJoinable()) {
+    llvm::Expected<HostThread> io_handler_thread = ThreadLauncher::LaunchThread(
+        "lldb.debugger.io-handler", IOHandlerThread, this,
         8 * 1024 * 1024); // Use larger 8MB stack for this thread
+    if (io_handler_thread) {
+      m_io_handler_thread = *io_handler_thread;
+    } else {
+      LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST),
+               "failed to launch host thread: {}",
+               llvm::toString(io_handler_thread.takeError()));
+    }
+  }
   return m_io_handler_thread.IsJoinable();
 }
 

Modified: lldb/trunk/source/Host/common/TaskPool.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/TaskPool.cpp?rev=365226&r1=365225&r2=365226&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/TaskPool.cpp (original)
+++ lldb/trunk/source/Host/common/TaskPool.cpp Fri Jul  5 10:42:08 2019
@@ -8,6 +8,7 @@
 
 #include "lldb/Host/TaskPool.h"
 #include "lldb/Host/ThreadLauncher.h"
+#include "lldb/Utility/Log.h"
 
 #include <cstdint>
 #include <queue>
@@ -65,9 +66,16 @@ void TaskPoolImpl::AddTask(std::function
     // Note that this detach call needs to happen with the m_tasks_mutex held.
     // This prevents the thread from exiting prematurely and triggering a linux
     // libc bug (https://sourceware.org/bugzilla/show_bug.cgi?id=19951).
-    lldb_private::ThreadLauncher::LaunchThread("task-pool.worker", WorkerPtr,
-                                               this, nullptr, min_stack_size)
-        .Release();
+    llvm::Expected<HostThread> host_thread =
+        lldb_private::ThreadLauncher::LaunchThread(
+            "task-pool.worker", WorkerPtr, this, min_stack_size);
+    if (host_thread) {
+      host_thread->Release();
+    } else {
+      LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST),
+               "failed to launch host thread: {}",
+               llvm::toString(host_thread.takeError()));
+    }
   }
 }
 

Modified: lldb/trunk/source/Host/common/ThreadLauncher.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/ThreadLauncher.cpp?rev=365226&r1=365225&r2=365226&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/ThreadLauncher.cpp (original)
+++ lldb/trunk/source/Host/common/ThreadLauncher.cpp Fri Jul  5 10:42:08 2019
@@ -20,15 +20,9 @@
 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,
-                                        Status *error_ptr,
-                                        size_t min_stack_byte_size) {
-  Status error;
-  if (error_ptr)
-    error_ptr->Clear();
-
+llvm::Expected<HostThread> ThreadLauncher::LaunchThread(
+    llvm::StringRef name, lldb::thread_func_t thread_function,
+    lldb::thread_arg_t thread_arg, size_t min_stack_byte_size) {
   // Host::ThreadCreateTrampoline will delete this pointer for us.
   HostThreadCreateInfo *info_ptr =
       new HostThreadCreateInfo(name.data(), thread_function, thread_arg);
@@ -38,7 +32,7 @@ HostThread ThreadLauncher::LaunchThread(
       0, (unsigned)min_stack_byte_size,
       HostNativeThread::ThreadCreateTrampoline, info_ptr, 0, NULL);
   if (thread == (lldb::thread_t)(-1L))
-    error.SetError(::GetLastError(), eErrorTypeWin32);
+    return llvm::errorCodeToError(::GetLastError());
 #else
 
 // ASAN instrumentation adds a lot of bookkeeping overhead on stack frames.
@@ -73,12 +67,10 @@ HostThread ThreadLauncher::LaunchThread(
   if (destroy_attr)
     ::pthread_attr_destroy(&thread_attr);
 
-  error.SetError(err, eErrorTypePOSIX);
+  if (err)
+    return llvm::errorCodeToError(
+        std::error_code(err, std::generic_category()));
 #endif
-  if (error_ptr)
-    *error_ptr = error;
-  if (!error.Success())
-    thread = LLDB_INVALID_HOST_THREAD;
 
   return HostThread(thread);
 }

Modified: lldb/trunk/source/Host/macosx/objcxx/Host.mm
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/macosx/objcxx/Host.mm?rev=365226&r1=365225&r2=365226&view=diff
==============================================================================
--- lldb/trunk/source/Host/macosx/objcxx/Host.mm (original)
+++ lldb/trunk/source/Host/macosx/objcxx/Host.mm Fri Jul  5 10:42:08 2019
@@ -313,13 +313,16 @@ LaunchInNewTerminalWithAppleScript(const
   // 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.
-  HostThread accept_thread = ThreadLauncher::LaunchThread(
-      unix_socket_name, AcceptPIDFromInferior, connect_url, &lldb_error);
+  llvm::Expected<HostThread> accept_thread = ThreadLauncher::LaunchThread(
+      unix_socket_name, AcceptPIDFromInferior, connect_url);
+
+  if (!accept_thread)
+    return Status(accept_thread.takeError());
 
   [applescript executeAndReturnError:nil];
 
   thread_result_t accept_thread_result = NULL;
-  lldb_error = accept_thread.Join(&accept_thread_result);
+  lldb_error = accept_thread->Join(&accept_thread_result);
   if (lldb_error.Success() && accept_thread_result) {
     pid = (intptr_t)accept_thread_result;
 

Modified: lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp?rev=365226&r1=365225&r2=365226&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp (original)
+++ lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp Fri Jul  5 10:42:08 2019
@@ -32,6 +32,7 @@
 #include "lldb/Target/RegisterContext.h"
 #include "lldb/Target/Target.h"
 #include "lldb/Target/Thread.h"
+#include "lldb/Utility/Log.h"
 #include "lldb/Utility/State.h"
 #include "lldb/Utility/StringExtractor.h"
 #include "lldb/Utility/UUID.h"
@@ -739,8 +740,15 @@ bool ProcessKDP::StartAsyncThread() {
   if (m_async_thread.IsJoinable())
     return true;
 
-  m_async_thread = ThreadLauncher::LaunchThread(
-      "<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
+  llvm::Expected<HostThread> async_thread = ThreadLauncher::LaunchThread(
+      "<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this);
+  if (!async_thread) {
+    LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST),
+             "failed to launch host thread: {}",
+             llvm::toString(async_thread.takeError()));
+    return false;
+  }
+  m_async_thread = *async_thread;
   return m_async_thread.IsJoinable();
 }
 

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp?rev=365226&r1=365225&r2=365226&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp Fri Jul  5 10:42:08 2019
@@ -888,22 +888,23 @@ GDBRemoteCommunication::CheckForPacket(c
 
 Status GDBRemoteCommunication::StartListenThread(const char *hostname,
                                                  uint16_t port) {
-  Status error;
-  if (m_listen_thread.IsJoinable()) {
-    error.SetErrorString("listen thread already running");
-  } else {
-    char listen_url[512];
-    if (hostname && hostname[0])
-      snprintf(listen_url, sizeof(listen_url), "listen://%s:%i", hostname,
-               port);
-    else
-      snprintf(listen_url, sizeof(listen_url), "listen://%i", port);
-    m_listen_url = listen_url;
-    SetConnection(new ConnectionFileDescriptor());
-    m_listen_thread = ThreadLauncher::LaunchThread(
-        listen_url, GDBRemoteCommunication::ListenThread, this, &error);
-  }
-  return error;
+  if (m_listen_thread.IsJoinable())
+    return Status("listen thread already running");
+
+  char listen_url[512];
+  if (hostname && hostname[0])
+    snprintf(listen_url, sizeof(listen_url), "listen://%s:%i", hostname, port);
+  else
+    snprintf(listen_url, sizeof(listen_url), "listen://%i", port);
+  m_listen_url = listen_url;
+  SetConnection(new ConnectionFileDescriptor());
+  llvm::Expected<HostThread> listen_thread = ThreadLauncher::LaunchThread(
+      listen_url, GDBRemoteCommunication::ListenThread, this);
+  if (!listen_thread)
+    return Status(listen_thread.takeError());
+  m_listen_thread = *listen_thread;
+
+  return Status();
 }
 
 bool GDBRemoteCommunication::JoinListenThread() {

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.cpp?rev=365226&r1=365225&r2=365226&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.cpp Fri Jul  5 10:42:08 2019
@@ -203,9 +203,16 @@ bool GDBRemoteCommunicationReplayServer:
   if (!m_async_thread.IsJoinable()) {
     // Create a thread that watches our internal state and controls which
     // events make it to clients (into the DCProcess event queue).
-    m_async_thread = ThreadLauncher::LaunchThread(
+    llvm::Expected<HostThread> async_thread = ThreadLauncher::LaunchThread(
         "<lldb.gdb-replay.async>",
-        GDBRemoteCommunicationReplayServer::AsyncThread, this, nullptr);
+        GDBRemoteCommunicationReplayServer::AsyncThread, this);
+    if (!async_thread) {
+      LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST),
+               "failed to launch host thread: {}",
+               llvm::toString(async_thread.takeError()));
+      return false;
+    }
+    m_async_thread = *async_thread;
   }
 
   // Wait for handshake.

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp?rev=365226&r1=365225&r2=365226&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp Fri Jul  5 10:42:08 2019
@@ -3700,9 +3700,15 @@ bool ProcessGDBRemote::StartAsyncThread(
     // Create a thread that watches our internal state and controls which
     // events make it to clients (into the DCProcess event queue).
 
-    m_async_thread = ThreadLauncher::LaunchThread(
-        "<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this,
-        nullptr);
+    llvm::Expected<HostThread> async_thread = ThreadLauncher::LaunchThread(
+        "<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this);
+    if (!async_thread) {
+      LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST),
+               "failed to launch host thread: {}",
+               llvm::toString(async_thread.takeError()));
+      return false;
+    }
+    m_async_thread = *async_thread;
   } else if (log)
     log->Printf("ProcessGDBRemote::%s () - Called when Async thread was "
                 "already running.",

Modified: lldb/trunk/source/Target/Process.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Process.cpp?rev=365226&r1=365225&r2=365226&view=diff
==============================================================================
--- lldb/trunk/source/Target/Process.cpp (original)
+++ lldb/trunk/source/Target/Process.cpp Fri Jul  5 10:42:08 2019
@@ -3588,14 +3588,20 @@ bool Process::StartPrivateStateThread(bo
   // Create the private state thread, and start it running.
   PrivateStateThreadArgs *args_ptr =
       new PrivateStateThreadArgs(this, is_secondary_thread);
-  m_private_state_thread =
+  llvm::Expected<HostThread> private_state_thread =
       ThreadLauncher::LaunchThread(thread_name, Process::PrivateStateThread,
-                                   (void *)args_ptr, nullptr, 8 * 1024 * 1024);
-  if (m_private_state_thread.IsJoinable()) {
-    ResumePrivateStateThread();
-    return true;
-  } else
+                                   (void *)args_ptr, 8 * 1024 * 1024);
+  if (!private_state_thread) {
+    LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST),
+             "failed to launch host thread: {}",
+             llvm::toString(private_state_thread.takeError()));
     return false;
+  }
+
+  assert(private_state_thread->IsJoinable());
+  m_private_state_thread = *private_state_thread;
+  ResumePrivateStateThread();
+  return true;
 }
 
 void Process::PausePrivateStateThread() {




More information about the lldb-commits mailing list