[lldb] Make Platform::DebugProcess take a Target reference
authorPavel Labath <pavel@labath.sk>
Thu, 16 Sep 2021 09:14:16 +0000 (11:14 +0200)
committerPavel Labath <pavel@labath.sk>
Thu, 16 Sep 2021 09:33:47 +0000 (11:33 +0200)
instead of a pointer. There are just two callers of this function, and
both of them have a valid target pointer, so there's no need for all
implementations to concern themselves with whether the pointer is null.

14 files changed:
lldb/include/lldb/Target/Platform.h
lldb/source/Commands/CommandObjectPlatform.cpp
lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.cpp
lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.h
lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.h
lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp
lldb/source/Plugins/Platform/Windows/PlatformWindows.h
lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h
lldb/source/Target/Platform.cpp
lldb/source/Target/Target.cpp

index df46466..878739c 100644 (file)
@@ -363,11 +363,9 @@ public:
   /// platforms will want to subclass this function in order to be able to
   /// intercept STDIO and possibly launch a separate process that will debug
   /// the debuggee.
-  virtual lldb::ProcessSP
-  DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
-               Target *target, // Can be nullptr, if nullptr create a new
-                               // target, else use existing one
-               Status &error);
+  virtual lldb::ProcessSP DebugProcess(ProcessLaunchInfo &launch_info,
+                                       Debugger &debugger, Target &target,
+                                       Status &error);
 
   virtual lldb::ProcessSP ConnectProcess(llvm::StringRef connect_url,
                                          llvm::StringRef plugin_name,
index 7760eba..c4444d4 100644 (file)
@@ -1171,7 +1171,7 @@ protected:
           target->GetRunArguments(m_options.launch_info.GetArguments());
 
         ProcessSP process_sp(platform_sp->DebugProcess(
-            m_options.launch_info, debugger, target, error));
+            m_options.launch_info, debugger, *target, error));
         if (process_sp && process_sp->IsAlive()) {
           result.SetStatus(eReturnStatusSuccessFinishNoResult);
           return true;
index 925a3d1..0919ba2 100644 (file)
@@ -177,11 +177,10 @@ Status PlatformAppleSimulator::DisconnectRemote() {
 #endif
 }
 
-lldb::ProcessSP PlatformAppleSimulator::DebugProcess(
-    ProcessLaunchInfo &launch_info, Debugger &debugger,
-    Target *target, // Can be NULL, if NULL create a new target, else use
-                    // existing one
-    Status &error) {
+lldb::ProcessSP
+PlatformAppleSimulator::DebugProcess(ProcessLaunchInfo &launch_info,
+                                     Debugger &debugger, Target &target,
+                                     Status &error) {
 #if defined(__APPLE__)
   ProcessSP process_sp;
   // Make sure we stop at the entry point
@@ -195,7 +194,7 @@ lldb::ProcessSP PlatformAppleSimulator::DebugProcess(
   if (error.Success()) {
     if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
       ProcessAttachInfo attach_info(launch_info);
-      process_sp = Attach(attach_info, debugger, target, error);
+      process_sp = Attach(attach_info, debugger, &target, error);
       if (process_sp) {
         launch_info.SetHijackListener(attach_info.GetHijackListener());
 
index 2e6ce02..fc428ad 100644 (file)
@@ -60,7 +60,7 @@ public:
 
   lldb::ProcessSP DebugProcess(lldb_private::ProcessLaunchInfo &launch_info,
                                lldb_private::Debugger &debugger,
-                               lldb_private::Target *target,
+                               lldb_private::Target &target,
                                lldb_private::Status &error) override;
 
   bool GetSupportedArchitectureAtIndex(uint32_t idx,
index 62da060..943b157 100644 (file)
@@ -1226,12 +1226,9 @@ PlatformDarwin::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
     return 1;
 }
 
-lldb::ProcessSP
-PlatformDarwin::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
-                             Target *target, // Can be NULL, if NULL create
-                                             // a new target, else use existing
-                                             // one
-                             Status &error) {
+lldb::ProcessSP PlatformDarwin::DebugProcess(ProcessLaunchInfo &launch_info,
+                                             Debugger &debugger, Target &target,
+                                             Status &error) {
   ProcessSP process_sp;
 
   if (IsHost()) {
index 22f7ad4..c3862f1 100644 (file)
@@ -71,7 +71,7 @@ public:
 
   lldb::ProcessSP DebugProcess(lldb_private::ProcessLaunchInfo &launch_info,
                                lldb_private::Debugger &debugger,
-                               lldb_private::Target *target,
+                               lldb_private::Target &target,
                                lldb_private::Status &error) override;
 
   void CalculateTrapHandlerSymbolNames() override;
index 71917af..4a93e58 100644 (file)
@@ -410,13 +410,11 @@ lldb::ProcessSP PlatformPOSIX::Attach(ProcessAttachInfo &attach_info,
   return process_sp;
 }
 
-lldb::ProcessSP
-PlatformPOSIX::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
-                            Target *target, // Can be NULL, if NULL create a new
-                                            // target, else use existing one
-                            Status &error) {
+lldb::ProcessSP PlatformPOSIX::DebugProcess(ProcessLaunchInfo &launch_info,
+                                            Debugger &debugger, Target &target,
+                                            Status &error) {
   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
-  LLDB_LOG(log, "target {0}", target);
+  LLDB_LOG(log, "target {0}", &target);
 
   ProcessSP process_sp;
 
@@ -442,29 +440,10 @@ PlatformPOSIX::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
   // worry about the target getting them as well.
   launch_info.SetLaunchInSeparateProcessGroup(true);
 
-  // Ensure we have a target.
-  if (target == nullptr) {
-    LLDB_LOG(log, "creating new target");
-    TargetSP new_target_sp;
-    error = debugger.GetTargetList().CreateTarget(
-        debugger, "", "", eLoadDependentsNo, nullptr, new_target_sp);
-    if (error.Fail()) {
-      LLDB_LOG(log, "failed to create new target: {0}", error);
-      return process_sp;
-    }
-
-    target = new_target_sp.get();
-    if (!target) {
-      error.SetErrorString("CreateTarget() returned nullptr");
-      LLDB_LOG(log, "error: {0}", error);
-      return process_sp;
-    }
-  }
-
   // Now create the gdb-remote process.
   LLDB_LOG(log, "having target create process with gdb-remote plugin");
   process_sp =
-      target->CreateProcess(launch_info.GetListener(), "gdb-remote", nullptr,
+      target.CreateProcess(launch_info.GetListener(), "gdb-remote", nullptr,
                             true);
 
   if (!process_sp) {
@@ -518,8 +497,8 @@ PlatformPOSIX::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
       LLDB_LOG(log, "not using process STDIO pty");
   } else {
     LLDB_LOG(log, "{0}", error);
-    // FIXME figure out appropriate cleanup here.  Do we delete the target? Do
-    // we delete the process?  Does our caller do that?
+    // FIXME figure out appropriate cleanup here. Do we delete the process?
+    // Does our caller do that?
   }
 
   return process_sp;
index 1cba4c5..511797c 100644 (file)
@@ -47,11 +47,7 @@ public:
 
   lldb::ProcessSP DebugProcess(lldb_private::ProcessLaunchInfo &launch_info,
                                lldb_private::Debugger &debugger,
-                               lldb_private::Target *target, // Can be nullptr,
-                                                             // if nullptr
-                                                             // create a new
-                                                             // target, else use
-                                                             // existing one
+                               lldb_private::Target &target,
                                lldb_private::Status &error) override;
 
   std::string GetPlatformSpecificConnectionInformation() override;
index fd28eec..fdb6758 100644 (file)
@@ -198,7 +198,7 @@ Status PlatformWindows::DisconnectRemote() {
 }
 
 ProcessSP PlatformWindows::DebugProcess(ProcessLaunchInfo &launch_info,
-                                        Debugger &debugger, Target *target,
+                                        Debugger &debugger, Target &target,
                                         Status &error) {
   // Windows has special considerations that must be followed when launching or
   // attaching to a process.  The key requirement is that when launching or
@@ -230,9 +230,9 @@ ProcessSP PlatformWindows::DebugProcess(ProcessLaunchInfo &launch_info,
   if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
     // This is a process attach.  Don't need to launch anything.
     ProcessAttachInfo attach_info(launch_info);
-    return Attach(attach_info, debugger, target, error);
+    return Attach(attach_info, debugger, &target, error);
   } else {
-    ProcessSP process_sp = target->CreateProcess(
+    ProcessSP process_sp = target.CreateProcess(
         launch_info.GetListener(), launch_info.GetProcessPluginName(), nullptr,
         false);
 
index 8c02d8e..aff6924 100644 (file)
@@ -42,7 +42,7 @@ public:
 
   lldb::ProcessSP DebugProcess(lldb_private::ProcessLaunchInfo &launch_info,
                                lldb_private::Debugger &debugger,
-                               lldb_private::Target *target,
+                               lldb_private::Target &target,
                                lldb_private::Status &error) override;
 
   lldb::ProcessSP Attach(lldb_private::ProcessAttachInfo &attach_info,
index 5282086..01a0ec5 100644 (file)
@@ -475,11 +475,10 @@ Status PlatformRemoteGDBServer::KillProcess(const lldb::pid_t pid) {
   return Status();
 }
 
-lldb::ProcessSP PlatformRemoteGDBServer::DebugProcess(
-    ProcessLaunchInfo &launch_info, Debugger &debugger,
-    Target *target, // Can be NULL, if NULL create a new target, else use
-                    // existing one
-    Status &error) {
+lldb::ProcessSP
+PlatformRemoteGDBServer::DebugProcess(ProcessLaunchInfo &launch_info,
+                                      Debugger &debugger, Target &target,
+                                      Status &error) {
   lldb::ProcessSP process_sp;
   if (IsRemote()) {
     if (IsConnected()) {
@@ -489,32 +488,21 @@ lldb::ProcessSP PlatformRemoteGDBServer::DebugProcess(
         error.SetErrorStringWithFormat("unable to launch a GDB server on '%s'",
                                        GetHostname());
       } else {
-        if (target == nullptr) {
-          TargetSP new_target_sp;
-
-          error = debugger.GetTargetList().CreateTarget(
-              debugger, "", "", eLoadDependentsNo, nullptr, new_target_sp);
-          target = new_target_sp.get();
-        } else
-          error.Clear();
-
-        if (target && error.Success()) {
-          // The darwin always currently uses the GDB remote debugger plug-in
-          // so even when debugging locally we are debugging remotely!
-          process_sp = target->CreateProcess(launch_info.GetListener(),
-                                             "gdb-remote", nullptr, true);
-
-          if (process_sp) {
+        // The darwin always currently uses the GDB remote debugger plug-in
+        // so even when debugging locally we are debugging remotely!
+        process_sp = target.CreateProcess(launch_info.GetListener(),
+                                          "gdb-remote", nullptr, true);
+
+        if (process_sp) {
+          error = process_sp->ConnectRemote(connect_url.c_str());
+          // Retry the connect remote one time...
+          if (error.Fail())
             error = process_sp->ConnectRemote(connect_url.c_str());
-            // Retry the connect remote one time...
-            if (error.Fail())
-              error = process_sp->ConnectRemote(connect_url.c_str());
-            if (error.Success())
-              error = process_sp->Launch(launch_info);
-            else if (debugserver_pid != LLDB_INVALID_PROCESS_ID) {
-              printf("error: connect remote failed (%s)\n", error.AsCString());
-              KillSpawnedProcess(debugserver_pid);
-            }
+          if (error.Success())
+            error = process_sp->Launch(launch_info);
+          else if (debugserver_pid != LLDB_INVALID_PROCESS_ID) {
+            printf("error: connect remote failed (%s)\n", error.AsCString());
+            KillSpawnedProcess(debugserver_pid);
           }
         }
       }
index 834140f..4f0e3c2 100644 (file)
@@ -62,10 +62,7 @@ public:
   Status KillProcess(const lldb::pid_t pid) override;
 
   lldb::ProcessSP DebugProcess(ProcessLaunchInfo &launch_info,
-                               Debugger &debugger,
-                               Target *target, // Can be NULL, if NULL create a
-                                               // new target, else use existing
-                                               // one
+                               Debugger &debugger, Target &target,
                                Status &error) override;
 
   lldb::ProcessSP Attach(ProcessAttachInfo &attach_info, Debugger &debugger,
index 23141c2..a9b97f7 100644 (file)
@@ -1088,14 +1088,11 @@ Status Platform::KillProcess(const lldb::pid_t pid) {
   return Status();
 }
 
-lldb::ProcessSP
-Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
-                       Target *target, // Can be nullptr, if nullptr create a
-                                       // new target, else use existing one
-                       Status &error) {
+lldb::ProcessSP Platform::DebugProcess(ProcessLaunchInfo &launch_info,
+                                       Debugger &debugger, Target &target,
+                                       Status &error) {
   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
-  LLDB_LOGF(log, "Platform::%s entered (target %p)", __FUNCTION__,
-            static_cast<void *>(target));
+  LLDB_LOG(log, "target = {0})", &target);
 
   ProcessSP process_sp;
   // Make sure we stop at the entry point
@@ -1117,7 +1114,7 @@ Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
        filter_callback = get_filter_func(++i, iteration_complete)) {
     if (filter_callback) {
       // Give this ProcessLaunchInfo filter a chance to adjust the launch info.
-      error = (*filter_callback)(launch_info, target);
+      error = (*filter_callback)(launch_info, &target);
       if (!error.Success()) {
         LLDB_LOGF(log,
                   "Platform::%s() StructuredDataPlugin launch "
@@ -1135,7 +1132,7 @@ Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
               __FUNCTION__, launch_info.GetProcessID());
     if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
       ProcessAttachInfo attach_info(launch_info);
-      process_sp = Attach(attach_info, debugger, target, error);
+      process_sp = Attach(attach_info, debugger, &target, error);
       if (process_sp) {
         LLDB_LOGF(log, "Platform::%s Attach() succeeded, Process plugin: %s",
                   __FUNCTION__, process_sp->GetPluginName().AsCString());
index 1599289..e24aa58 100644 (file)
@@ -2993,7 +2993,7 @@ Status Target::Launch(ProcessLaunchInfo &launch_info, Stream *stream) {
     DeleteCurrentProcess();
 
     m_process_sp =
-        GetPlatform()->DebugProcess(launch_info, debugger, this, error);
+        GetPlatform()->DebugProcess(launch_info, debugger, *this, error);
 
   } else {
     LLDB_LOGF(log,