Skip to content

[lldb] Expose Platform::Attach through the SB API #68050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2023

Conversation

JDevlieghere
Copy link
Member

Expose Platform::Attach through the SB API.

rdar://116188959

@llvmbot
Copy link
Member

llvmbot commented Oct 2, 2023

@llvm/pr-subscribers-lldb

Changes

Expose Platform::Attach through the SB API.

rdar://116188959


Full diff: https://github.com/llvm/llvm-project/pull/68050.diff

7 Files Affected:

  • (modified) lldb/include/lldb/API/SBAttachInfo.h (+1)
  • (modified) lldb/include/lldb/API/SBDebugger.h (+1)
  • (modified) lldb/include/lldb/API/SBPlatform.h (+5)
  • (modified) lldb/include/lldb/API/SBProcess.h (+1)
  • (modified) lldb/packages/Python/lldbsuite/test/gdbclientutils.py (+6)
  • (modified) lldb/source/API/SBPlatform.cpp (+25)
  • (added) lldb/test/API/functionalities/gdb_remote_client/TestPlatformAttach.py (+58)
diff --git a/lldb/include/lldb/API/SBAttachInfo.h b/lldb/include/lldb/API/SBAttachInfo.h
index ea1145e625856f0..c18655fee77e0ac 100644
--- a/lldb/include/lldb/API/SBAttachInfo.h
+++ b/lldb/include/lldb/API/SBAttachInfo.h
@@ -197,6 +197,7 @@ class LLDB_API SBAttachInfo {
 
 protected:
   friend class SBTarget;
+  friend class SBPlatform;
 
   friend class lldb_private::ScriptInterpreter;
 
diff --git a/lldb/include/lldb/API/SBDebugger.h b/lldb/include/lldb/API/SBDebugger.h
index 29cf2c16fad4bd7..218113a7a391f35 100644
--- a/lldb/include/lldb/API/SBDebugger.h
+++ b/lldb/include/lldb/API/SBDebugger.h
@@ -487,6 +487,7 @@ class LLDB_API SBDebugger {
   friend class SBProcess;
   friend class SBSourceManager;
   friend class SBStructuredData;
+  friend class SBPlatform;
   friend class SBTarget;
   friend class SBTrace;
 
diff --git a/lldb/include/lldb/API/SBPlatform.h b/lldb/include/lldb/API/SBPlatform.h
index 6567277a5d161e7..614ee3202def5bc 100644
--- a/lldb/include/lldb/API/SBPlatform.h
+++ b/lldb/include/lldb/API/SBPlatform.h
@@ -10,6 +10,7 @@
 #define LLDB_API_SBPLATFORM_H
 
 #include "lldb/API/SBDefines.h"
+#include "lldb/API/SBProcess.h"
 
 #include <functional>
 
@@ -19,6 +20,7 @@ struct PlatformShellCommand;
 namespace lldb {
 
 class SBLaunchInfo;
+class SBAttachInfo;
 
 class LLDB_API SBPlatformConnectOptions {
 public:
@@ -149,6 +151,9 @@ class LLDB_API SBPlatform {
 
   SBError Launch(SBLaunchInfo &launch_info);
 
+  SBProcess Attach(SBAttachInfo &attach_info, const SBDebugger &debugger,
+                   SBTarget &target, SBError &error);
+
   SBError Kill(const lldb::pid_t pid);
 
   SBError
diff --git a/lldb/include/lldb/API/SBProcess.h b/lldb/include/lldb/API/SBProcess.h
index 16527bb0291fcb4..8c1c81418f83d12 100644
--- a/lldb/include/lldb/API/SBProcess.h
+++ b/lldb/include/lldb/API/SBProcess.h
@@ -449,6 +449,7 @@ class LLDB_API SBProcess {
   friend class SBExecutionContext;
   friend class SBFunction;
   friend class SBModule;
+  friend class SBPlatform;
   friend class SBTarget;
   friend class SBThread;
   friend class SBValue;
diff --git a/lldb/packages/Python/lldbsuite/test/gdbclientutils.py b/lldb/packages/Python/lldbsuite/test/gdbclientutils.py
index a0104d36df8d903..1784487323ad6be 100644
--- a/lldb/packages/Python/lldbsuite/test/gdbclientutils.py
+++ b/lldb/packages/Python/lldbsuite/test/gdbclientutils.py
@@ -196,6 +196,9 @@ def respond(self, packet):
             return self.vFile(packet)
         if packet.startswith("vRun;"):
             return self.vRun(packet)
+        if packet.startswith("qLaunchGDBServer;"):
+            _, host = packet.partition(";")[2].split(":")
+            return self.qLaunchGDBServer(host)
         if packet.startswith("qLaunchSuccess"):
             return self.qLaunchSuccess()
         if packet.startswith("QEnvironment:"):
@@ -329,6 +332,9 @@ def vFile(self, packet):
     def vRun(self, packet):
         return ""
 
+    def qLaunchGDBServer(self, host):
+        raise self.UnexpectedPacketException()
+
     def qLaunchSuccess(self):
         return ""
 
diff --git a/lldb/source/API/SBPlatform.cpp b/lldb/source/API/SBPlatform.cpp
index f8300a5bab30e41..7dfbb1373989c02 100644
--- a/lldb/source/API/SBPlatform.cpp
+++ b/lldb/source/API/SBPlatform.cpp
@@ -7,12 +7,14 @@
 //===----------------------------------------------------------------------===//
 
 #include "lldb/API/SBPlatform.h"
+#include "lldb/API/SBDebugger.h"
 #include "lldb/API/SBEnvironment.h"
 #include "lldb/API/SBError.h"
 #include "lldb/API/SBFileSpec.h"
 #include "lldb/API/SBLaunchInfo.h"
 #include "lldb/API/SBModuleSpec.h"
 #include "lldb/API/SBPlatform.h"
+#include "lldb/API/SBTarget.h"
 #include "lldb/API/SBUnixSignals.h"
 #include "lldb/Host/File.h"
 #include "lldb/Target/Platform.h"
@@ -574,6 +576,29 @@ SBError SBPlatform::Launch(SBLaunchInfo &launch_info) {
   });
 }
 
+SBProcess SBPlatform::Attach(SBAttachInfo &attach_info,
+                             const SBDebugger &debugger, SBTarget &target,
+                             SBError &error) {
+  LLDB_INSTRUMENT_VA(this, attach_info);
+
+  if (PlatformSP platform_sp = GetSP()) {
+    if (platform_sp->IsConnected()) {
+      ProcessAttachInfo &info = attach_info.ref();
+      Status status;
+      ProcessSP process_sp = platform_sp->Attach(info, debugger.ref(),
+                                                 target.GetSP().get(), status);
+      error.SetError(status);
+      return SBProcess(process_sp);
+    }
+
+    error.SetErrorString("not connected");
+    return {};
+  }
+
+  error.SetErrorString("invalid platform");
+  return {};
+}
+
 SBError SBPlatform::Kill(const lldb::pid_t pid) {
   LLDB_INSTRUMENT_VA(this, pid);
   return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
diff --git a/lldb/test/API/functionalities/gdb_remote_client/TestPlatformAttach.py b/lldb/test/API/functionalities/gdb_remote_client/TestPlatformAttach.py
new file mode 100644
index 000000000000000..d62e86b2a3c1d20
--- /dev/null
+++ b/lldb/test/API/functionalities/gdb_remote_client/TestPlatformAttach.py
@@ -0,0 +1,58 @@
+import lldb
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test.decorators import *
+from lldbsuite.test.gdbclientutils import *
+from lldbsuite.test.lldbgdbclient import GDBRemoteTestBase
+
+
+class TestPlatformAttach(GDBRemoteTestBase):
+    @skipIfRemote
+    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr52451")
+    def test_attach(self):
+        """Test attaching by name"""
+
+        class MyPlatformResponder(MockGDBServerResponder):
+            def __init__(self, port):
+                MockGDBServerResponder.__init__(self)
+                self.port = port
+
+            def qLaunchGDBServer(self, _):
+                return "pid:1337;port:{};".format(self.port)
+
+            def qfProcessInfo(self, packet):
+                return "pid:95117;name:666f6f;"
+
+        class MyGDBResponder(MockGDBServerResponder):
+            def __init__(self):
+                MockGDBServerResponder.__init__(self)
+
+            def vAttach(self, _):
+                return "OK"
+
+        self.server.responder = MyGDBResponder()
+        port = self.server._socket._server_socket.getsockname()[1]
+
+        platform_socket = TCPServerSocket()
+        platform_server = MockGDBServer(platform_socket)
+        platform_server.responder = MyPlatformResponder(port)
+        platform_server.start()
+
+        error = lldb.SBError()
+        platform = lldb.SBPlatform("remote-linux")
+        self.dbg.SetSelectedPlatform(platform)
+
+        error = platform.ConnectRemote(
+            lldb.SBPlatformConnectOptions(platform_server.get_connect_url())
+        )
+        self.assertSuccess(error)
+        self.assertTrue(platform.IsConnected())
+
+        attach_info = lldb.SBAttachInfo()
+        attach_info.SetExecutable("foo")
+
+        target = lldb.SBTarget()
+        process = platform.Attach(attach_info, self.dbg, target, error)
+        self.assertSuccess(error)
+        self.assertEqual(process.GetProcessID(), 95117)
+
+        platform.DisconnectRemote()

Copy link
Member

@bulbazord bulbazord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me, but I want to really make sure we get this interface right. Left some comments inline.

SBProcess SBPlatform::Attach(SBAttachInfo &attach_info,
const SBDebugger &debugger, SBTarget &target,
SBError &error) {
LLDB_INSTRUMENT_VA(this, attach_info);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to instrument more things than just attach_info, right?

Copy link
Member

@medismailben medismailben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Expose Platform::Attach through the SB API.

rdar://116188959
@JDevlieghere JDevlieghere force-pushed the jdevlieghere/sbplatformattach branch from 6cf631f to c834354 Compare October 3, 2023 15:41
Copy link
Member

@bulbazord bulbazord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@JDevlieghere JDevlieghere merged commit 2da99a1 into llvm:main Oct 3, 2023
@JDevlieghere JDevlieghere deleted the jdevlieghere/sbplatformattach branch October 3, 2023 17:34
@antmox
Copy link
Contributor

antmox commented Oct 4, 2023

Hi! Could this commit cause the lldb-aarch64-windows bot failure ?
https://lab.llvm.org/buildbot/#/builders/219/builds/6086
https://lab.llvm.org/buildbot/#/builders/219/builds/6076
Not sure if it's an unexpected pass or a timeout.
Could you please take a look ?

@DavidSpickett
Copy link
Collaborator

@antmox You could remove the expected failure marker and see what the result is then. It will be less confusing.

It may be that it does pass, but occasionally times out for another reason. Though that is weird given that all the server parts involved are mocks.

antmox added a commit to antmox/llvm-project that referenced this pull request Oct 4, 2023
antmox added a commit that referenced this pull request Oct 4, 2023
@DavidSpickett
Copy link
Collaborator

DavidSpickett commented Oct 4, 2023

I've skipped it entirely in ceec9a7 since it sometimes will time out.

@antmox
Copy link
Contributor

antmox commented Oct 4, 2023

Yes, thanks @DavidSpickett

@JDevlieghere
Copy link
Member Author

Thank you @antmox and @DavidSpickett for following up on this.

JDevlieghere added a commit to swiftlang/llvm-project that referenced this pull request Oct 4, 2023
Expose Platform::Attach through the SB API.

rdar://116188959
(cherry picked from commit 2da99a1)
JDevlieghere pushed a commit to swiftlang/llvm-project that referenced this pull request Oct 4, 2023
… windows (llvm#68193)

Looks like this test passes since llvm#68050.

(cherry picked from commit 75f295c)
JDevlieghere added a commit to swiftlang/llvm-project that referenced this pull request Oct 9, 2023
Expose Platform::Attach through the SB API.

rdar://116188959
(cherry picked from commit 2da99a1)
JDevlieghere pushed a commit to swiftlang/llvm-project that referenced this pull request Oct 9, 2023
… windows (llvm#68193)

Looks like this test passes since llvm#68050.

(cherry picked from commit 75f295c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants