Skip to content

Commit c834354

Browse files
committed
[lldb] Expose Platform::Attach through the SB API
Expose Platform::Attach through the SB API. rdar://116188959
1 parent 457f582 commit c834354

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
@@ -487,6 +487,7 @@ class LLDB_API SBDebugger {
487487
friend class SBProcess;
488488
friend class SBSourceManager;
489489
friend class SBStructuredData;
490+
friend class SBPlatform;
490491
friend class SBTarget;
491492
friend class SBTrace;
492493

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
@@ -449,6 +449,7 @@ class LLDB_API SBProcess {
449449
friend class SBExecutionContext;
450450
friend class SBFunction;
451451
friend class SBModule;
452+
friend class SBPlatform;
452453
friend class SBTarget;
453454
friend class SBThread;
454455
friend class SBValue;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ def respond(self, packet):
196196
return self.vFile(packet)
197197
if packet.startswith("vRun;"):
198198
return self.vRun(packet)
199+
if packet.startswith("qLaunchGDBServer;"):
200+
_, host = packet.partition(";")[2].split(":")
201+
return self.qLaunchGDBServer(host)
199202
if packet.startswith("qLaunchSuccess"):
200203
return self.qLaunchSuccess()
201204
if packet.startswith("QEnvironment:"):
@@ -329,6 +332,9 @@ def vFile(self, packet):
329332
def vRun(self, packet):
330333
return ""
331334

335+
def qLaunchGDBServer(self, host):
336+
raise self.UnexpectedPacketException()
337+
332338
def qLaunchSuccess(self):
333339
return ""
334340

lldb/source/API/SBPlatform.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
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/SBModuleSpec.h"
1516
#include "lldb/API/SBPlatform.h"
17+
#include "lldb/API/SBTarget.h"
1618
#include "lldb/API/SBUnixSignals.h"
1719
#include "lldb/Host/File.h"
1820
#include "lldb/Target/Platform.h"
@@ -574,6 +576,29 @@ SBError SBPlatform::Launch(SBLaunchInfo &launch_info) {
574576
});
575577
}
576578

579+
SBProcess SBPlatform::Attach(SBAttachInfo &attach_info,
580+
const SBDebugger &debugger, SBTarget &target,
581+
SBError &error) {
582+
LLDB_INSTRUMENT_VA(this, attach_info, debugger, target, error);
583+
584+
if (PlatformSP platform_sp = GetSP()) {
585+
if (platform_sp->IsConnected()) {
586+
ProcessAttachInfo &info = attach_info.ref();
587+
Status status;
588+
ProcessSP process_sp = platform_sp->Attach(info, debugger.ref(),
589+
target.GetSP().get(), status);
590+
error.SetError(status);
591+
return SBProcess(process_sp);
592+
}
593+
594+
error.SetErrorString("not connected");
595+
return {};
596+
}
597+
598+
error.SetErrorString("invalid platform");
599+
return {};
600+
}
601+
577602
SBError SBPlatform::Kill(const lldb::pid_t pid) {
578603
LLDB_INSTRUMENT_VA(this, pid);
579604
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)