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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBAttachInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class LLDB_API SBAttachInfo {

protected:
friend class SBTarget;
friend class SBPlatform;

friend class lldb_private::ScriptInterpreter;

Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBDebugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions lldb/include/lldb/API/SBPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLDB_API_SBPLATFORM_H

#include "lldb/API/SBDefines.h"
#include "lldb/API/SBProcess.h"

#include <functional>

Expand All @@ -18,6 +19,7 @@ struct PlatformShellCommand;

namespace lldb {

class SBAttachInfo;
class SBLaunchInfo;

class LLDB_API SBPlatformConnectOptions {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions lldb/packages/Python/lldbsuite/test/gdbclientutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:"):
Expand Down Expand Up @@ -329,6 +332,9 @@ def vFile(self, packet):
def vRun(self, packet):
return ""

def qLaunchGDBServer(self, host):
raise self.UnexpectedPacketException()

def qLaunchSuccess(self):
return ""

Expand Down
25 changes: 25 additions & 0 deletions lldb/source/API/SBPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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, debugger, target, error);

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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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()