Skip to content

Commit 4890cb0

Browse files
committed
[lldb] Expose Platform::Attach through the SB API (llvm#68050)
Expose Platform::Attach through the SB API. rdar://116188959 (cherry picked from commit 2da99a1)
1 parent 1511e98 commit 4890cb0

File tree

7 files changed

+97
-0
lines changed

7 files changed

+97
-0
lines changed

lldb/include/lldb/API/SBAttachInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ class LLDB_API SBAttachInfo {
197197

198198
protected:
199199
friend class SBTarget;
200+
friend class SBPlatform;
200201

201202
friend class lldb_private::ScriptInterpreter;
202203

lldb/include/lldb/API/SBDebugger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ class LLDB_API SBDebugger {
483483
friend class SBProcess;
484484
friend class SBSourceManager;
485485
friend class SBStructuredData;
486+
friend class SBPlatform;
486487
friend class SBTarget;
487488
friend class SBTrace;
488489

lldb/include/lldb/API/SBPlatform.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLDB_API_SBPLATFORM_H
1111

1212
#include "lldb/API/SBDefines.h"
13+
#include "lldb/API/SBProcess.h"
1314

1415
#include <functional>
1516

@@ -18,6 +19,7 @@ struct PlatformShellCommand;
1819

1920
namespace lldb {
2021

22+
class SBAttachInfo;
2123
class SBLaunchInfo;
2224

2325
class LLDB_API SBPlatformConnectOptions {
@@ -149,6 +151,9 @@ class LLDB_API SBPlatform {
149151

150152
SBError Launch(SBLaunchInfo &launch_info);
151153

154+
SBProcess Attach(SBAttachInfo &attach_info, const SBDebugger &debugger,
155+
SBTarget &target, SBError &error);
156+
152157
SBError Kill(const lldb::pid_t pid);
153158

154159
SBError

lldb/include/lldb/API/SBProcess.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ class LLDB_API SBProcess {
450450
friend class SBExecutionContext;
451451
friend class SBFunction;
452452
friend class SBModule;
453+
friend class SBPlatform;
453454
friend class SBTarget;
454455
friend class SBThread;
455456
friend class SBValue;

lldb/packages/Python/lldbsuite/test/gdbclientutils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ def respond(self, packet):
193193
return self.vFile(packet)
194194
if packet.startswith("vRun;"):
195195
return self.vRun(packet)
196+
if packet.startswith("qLaunchGDBServer;"):
197+
_, host = packet.partition(";")[2].split(":")
198+
return self.qLaunchGDBServer(host)
196199
if packet.startswith("qLaunchSuccess"):
197200
return self.qLaunchSuccess()
198201
if packet.startswith("QEnvironment:"):
@@ -326,6 +329,9 @@ def vFile(self, packet):
326329
def vRun(self, packet):
327330
return ""
328331

332+
def qLaunchGDBServer(self, host):
333+
raise self.UnexpectedPacketException()
334+
329335
def qLaunchSuccess(self):
330336
return ""
331337

lldb/source/API/SBPlatform.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBPlatform.h"
10+
#include "lldb/API/SBDebugger.h"
1011
#include "lldb/API/SBEnvironment.h"
1112
#include "lldb/API/SBError.h"
1213
#include "lldb/API/SBFileSpec.h"
1314
#include "lldb/API/SBLaunchInfo.h"
1415
#include "lldb/API/SBPlatform.h"
16+
#include "lldb/API/SBTarget.h"
1517
#include "lldb/API/SBUnixSignals.h"
1618
#include "lldb/Host/File.h"
1719
#include "lldb/Target/Platform.h"
@@ -573,6 +575,29 @@ SBError SBPlatform::Launch(SBLaunchInfo &launch_info) {
573575
});
574576
}
575577

578+
SBProcess SBPlatform::Attach(SBAttachInfo &attach_info,
579+
const SBDebugger &debugger, SBTarget &target,
580+
SBError &error) {
581+
LLDB_INSTRUMENT_VA(this, attach_info, debugger, target, error);
582+
583+
if (PlatformSP platform_sp = GetSP()) {
584+
if (platform_sp->IsConnected()) {
585+
ProcessAttachInfo &info = attach_info.ref();
586+
Status status;
587+
ProcessSP process_sp = platform_sp->Attach(info, debugger.ref(),
588+
target.GetSP().get(), status);
589+
error.SetError(status);
590+
return SBProcess(process_sp);
591+
}
592+
593+
error.SetErrorString("not connected");
594+
return {};
595+
}
596+
597+
error.SetErrorString("invalid platform");
598+
return {};
599+
}
600+
576601
SBError SBPlatform::Kill(const lldb::pid_t pid) {
577602
LLDB_INSTRUMENT_VA(this, pid);
578603
return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import lldb
2+
from lldbsuite.test.lldbtest import *
3+
from lldbsuite.test.decorators import *
4+
from lldbsuite.test.gdbclientutils import *
5+
from lldbsuite.test.lldbgdbclient import GDBRemoteTestBase
6+
7+
8+
class TestPlatformAttach(GDBRemoteTestBase):
9+
@skipIfRemote
10+
@expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr52451")
11+
def test_attach(self):
12+
"""Test attaching by name"""
13+
14+
class MyPlatformResponder(MockGDBServerResponder):
15+
def __init__(self, port):
16+
MockGDBServerResponder.__init__(self)
17+
self.port = port
18+
19+
def qLaunchGDBServer(self, _):
20+
return "pid:1337;port:{};".format(self.port)
21+
22+
def qfProcessInfo(self, packet):
23+
return "pid:95117;name:666f6f;"
24+
25+
class MyGDBResponder(MockGDBServerResponder):
26+
def __init__(self):
27+
MockGDBServerResponder.__init__(self)
28+
29+
def vAttach(self, _):
30+
return "OK"
31+
32+
self.server.responder = MyGDBResponder()
33+
port = self.server._socket._server_socket.getsockname()[1]
34+
35+
platform_socket = TCPServerSocket()
36+
platform_server = MockGDBServer(platform_socket)
37+
platform_server.responder = MyPlatformResponder(port)
38+
platform_server.start()
39+
40+
error = lldb.SBError()
41+
platform = lldb.SBPlatform("remote-linux")
42+
self.dbg.SetSelectedPlatform(platform)
43+
44+
error = platform.ConnectRemote(
45+
lldb.SBPlatformConnectOptions(platform_server.get_connect_url())
46+
)
47+
self.assertSuccess(error)
48+
self.assertTrue(platform.IsConnected())
49+
50+
attach_info = lldb.SBAttachInfo()
51+
attach_info.SetExecutable("foo")
52+
53+
target = lldb.SBTarget()
54+
process = platform.Attach(attach_info, self.dbg, target, error)
55+
self.assertSuccess(error)
56+
self.assertEqual(process.GetProcessID(), 95117)
57+
58+
platform.DisconnectRemote()

0 commit comments

Comments
 (0)