Skip to content

Commit 2362f11

Browse files
committed
[lldb/Plugins] Add Attach capabilities to ScriptedProcess
This patch adds process attach capabilities to the ScriptedProcess plugin. This doesn't really expects a PID or process name, since the process state is already script, however, this allows to create a scripted process without requiring to have an executuble in the target. In order to do so, this patch also turns the scripted process related getters and setters from the `ProcessLaunchInfo` and `ProcessAttachInfo` classes to a `ScriptedMetadata` instance and moves it in the `ProcessInfo` class, so it can be accessed interchangeably. This also adds the necessary SWIG wrappers to convert the internal `Process{Attach,Launch}InfoSP` into a `SB{Attach,Launch}Info` to pass it as argument the scripted process python implementation and convert it back to the internal representation. rdar://104577406 Differential Revision: https://reviews.llvm.org/D143104 Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 6ab3990 commit 2362f11

31 files changed

+316
-109
lines changed

lldb/bindings/python/python-swigsafecast.swig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ PythonObject ToSWIGWrapper(const SymbolContext &sym_ctx) {
8989
SWIGTYPE_p_lldb__SBSymbolContext);
9090
}
9191

92+
PythonObject ToSWIGWrapper(lldb::ProcessLaunchInfoSP launch_info_sp) {
93+
return ToSWIGHelper(new lldb::ProcessLaunchInfoSP(std::move(launch_info_sp)),
94+
SWIGTYPE_p_lldb__SBLaunchInfo);
95+
}
96+
97+
PythonObject ToSWIGWrapper(lldb::ProcessAttachInfoSP attach_info_sp) {
98+
return ToSWIGHelper(new lldb::ProcessAttachInfoSP(std::move(attach_info_sp)),
99+
SWIGTYPE_p_lldb__SBAttachInfo);
100+
}
101+
92102
ScopedPythonObject<lldb::SBCommandReturnObject>
93103
ToSWIGWrapper(CommandReturnObject &cmd_retobj) {
94104
return ScopedPythonObject<lldb::SBCommandReturnObject>(

lldb/bindings/python/python-wrapper.swig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,30 @@ void *lldb_private::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) {
732732
return sb_ptr;
733733
}
734734

735+
void *lldb_private::LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject * data) {
736+
lldb::SBAttachInfo *sb_ptr = nullptr;
737+
738+
int valid_cast =
739+
SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBAttachInfo, 0);
740+
741+
if (valid_cast == -1)
742+
return NULL;
743+
744+
return sb_ptr;
745+
}
746+
747+
void *lldb_private::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(PyObject * data) {
748+
lldb::SBLaunchInfo *sb_ptr = nullptr;
749+
750+
int valid_cast =
751+
SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBLaunchInfo, 0);
752+
753+
if (valid_cast == -1)
754+
return NULL;
755+
756+
return sb_ptr;
757+
}
758+
735759
void *lldb_private::LLDBSWIGPython_CastPyObjectToSBError(PyObject * data) {
736760
lldb::SBError *sb_ptr = nullptr;
737761

lldb/examples/python/scripted_process/scripted_process.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ def launch(self):
160160
"""
161161
return lldb.SBError()
162162

163+
def attach(self, attach_info):
164+
""" Simulate the scripted process attach.
165+
166+
Args:
167+
attach_info (lldb.SBAttachInfo): The information related to the
168+
process we're attaching to.
169+
170+
Returns:
171+
lldb.SBError: An `lldb.SBError` with error code 0.
172+
"""
173+
return lldb.SBError()
174+
163175
def resume(self):
164176
""" Simulate the scripted process resume.
165177

lldb/include/lldb/API/SBAttachInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
#include "lldb/API/SBDefines.h"
1313

14+
namespace lldb_private {
15+
class ScriptInterpreter;
16+
}
17+
1418
namespace lldb {
1519

1620
class SBTarget;
@@ -175,6 +179,8 @@ class LLDB_API SBAttachInfo {
175179
protected:
176180
friend class SBTarget;
177181

182+
friend class lldb_private::ScriptInterpreter;
183+
178184
lldb_private::ProcessAttachInfo &ref();
179185

180186
ProcessAttachInfoSP m_opaque_sp;

lldb/include/lldb/API/SBLaunchInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace lldb_private {
1515
class SBLaunchInfoImpl;
16+
class ScriptInterpreter;
1617
}
1718

1819
namespace lldb {
@@ -183,6 +184,8 @@ class LLDB_API SBLaunchInfo {
183184
friend class SBPlatform;
184185
friend class SBTarget;
185186

187+
friend class lldb_private::ScriptInterpreter;
188+
186189
const lldb_private::ProcessLaunchInfo &ref() const;
187190
void set_ref(const lldb_private::ProcessLaunchInfo &info);
188191

lldb/include/lldb/Host/ProcessLaunchInfo.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "lldb/Host/PseudoTerminal.h"
2121
#include "lldb/Utility/FileSpec.h"
2222
#include "lldb/Utility/ProcessInfo.h"
23-
#include "lldb/Utility/StructuredData.h"
2423

2524
namespace lldb_private {
2625

@@ -144,28 +143,6 @@ class ProcessLaunchInfo : public ProcessInfo {
144143
return m_flags.Test(lldb::eLaunchFlagDetachOnError);
145144
}
146145

147-
bool IsScriptedProcess() const {
148-
return !m_scripted_process_class_name.empty();
149-
}
150-
151-
std::string GetScriptedProcessClassName() const {
152-
return m_scripted_process_class_name;
153-
}
154-
155-
void SetScriptedProcessClassName(std::string name) {
156-
m_scripted_process_class_name = name;
157-
}
158-
159-
lldb_private::StructuredData::DictionarySP
160-
GetScriptedProcessDictionarySP() const {
161-
return m_scripted_process_dictionary_sp;
162-
}
163-
164-
void SetScriptedProcessDictionarySP(
165-
lldb_private::StructuredData::DictionarySP dictionary_sp) {
166-
m_scripted_process_dictionary_sp = dictionary_sp;
167-
}
168-
169146
protected:
170147
FileSpec m_working_dir;
171148
std::string m_plugin_name;
@@ -179,11 +156,6 @@ class ProcessLaunchInfo : public ProcessInfo {
179156
// meaning to the upper levels of lldb.
180157
lldb::ListenerSP m_listener_sp;
181158
lldb::ListenerSP m_hijack_listener_sp;
182-
std::string m_scripted_process_class_name; // The name of the class that will
183-
// manage a scripted process.
184-
StructuredData::DictionarySP
185-
m_scripted_process_dictionary_sp; // A dictionary that holds key/value
186-
// pairs passed to the scripted process.
187159
};
188160
}
189161

lldb/include/lldb/Interpreter/ScriptInterpreter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
#ifndef LLDB_INTERPRETER_SCRIPTINTERPRETER_H
1010
#define LLDB_INTERPRETER_SCRIPTINTERPRETER_H
1111

12+
#include "lldb/API/SBAttachInfo.h"
1213
#include "lldb/API/SBData.h"
1314
#include "lldb/API/SBError.h"
15+
#include "lldb/API/SBLaunchInfo.h"
1416
#include "lldb/API/SBMemoryRegionInfo.h"
1517
#include "lldb/Breakpoint/BreakpointOptions.h"
1618
#include "lldb/Core/PluginInterface.h"
@@ -568,6 +570,12 @@ class ScriptInterpreter : public PluginInterface {
568570

569571
Status GetStatusFromSBError(const lldb::SBError &error) const;
570572

573+
lldb::ProcessAttachInfoSP
574+
GetOpaqueTypeFromSBAttachInfo(const lldb::SBAttachInfo &attach_info) const;
575+
576+
lldb::ProcessLaunchInfoSP
577+
GetOpaqueTypeFromSBLaunchInfo(const lldb::SBLaunchInfo &launch_info) const;
578+
571579
llvm::Optional<MemoryRegionInfo> GetOpaqueTypeFromSBMemoryRegionInfo(
572580
const lldb::SBMemoryRegionInfo &mem_region) const;
573581

lldb/include/lldb/Interpreter/ScriptedMetadata.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "OptionGroupPythonClassWithDict.h"
1313

1414
#include "lldb/Host/Host.h"
15-
#include "lldb/Host/ProcessLaunchInfo.h"
15+
#include "lldb/Utility/ProcessInfo.h"
1616
#include "lldb/Utility/StructuredData.h"
1717

1818
namespace lldb_private {
@@ -22,9 +22,12 @@ class ScriptedMetadata {
2222
StructuredData::DictionarySP dict_sp)
2323
: m_class_name(class_name.data()), m_args_sp(dict_sp) {}
2424

25-
ScriptedMetadata(const ProcessLaunchInfo &launch_info) {
26-
m_class_name = launch_info.GetScriptedProcessClassName();
27-
m_args_sp = launch_info.GetScriptedProcessDictionarySP();
25+
ScriptedMetadata(const ProcessInfo &process_info) {
26+
lldb::ScriptedMetadataSP metadata_sp = process_info.GetScriptedMetadata();
27+
if (metadata_sp) {
28+
m_class_name = metadata_sp->GetClassName();
29+
m_args_sp = metadata_sp->GetArgsSP();
30+
}
2831
}
2932

3033
ScriptedMetadata(const OptionGroupPythonClassWithDict &option_group) {
@@ -33,6 +36,8 @@ class ScriptedMetadata {
3336
m_args_sp = opt_group.GetStructuredData();
3437
}
3538

39+
explicit operator bool() const { return !m_class_name.empty(); }
40+
3641
llvm::StringRef GetClassName() const { return m_class_name; }
3742
StructuredData::DictionarySP GetArgsSP() const { return m_args_sp; }
3843

lldb/include/lldb/Interpreter/ScriptedProcessInterface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class ScriptedProcessInterface : virtual public ScriptedInterface {
2929

3030
virtual StructuredData::DictionarySP GetCapabilities() { return {}; }
3131

32+
virtual Status Attach(const ProcessAttachInfo &attach_info) {
33+
return Status("ScriptedProcess did not attach");
34+
}
35+
3236
virtual Status Launch() { return Status("ScriptedProcess did not launch"); }
3337

3438
virtual Status Resume() { return Status("ScriptedProcess did not resume"); }

lldb/include/lldb/Target/Process.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -196,28 +196,6 @@ class ProcessAttachInfo : public ProcessInstanceInfo {
196196

197197
lldb::ListenerSP GetListenerForProcess(Debugger &debugger);
198198

199-
bool IsScriptedProcess() const {
200-
return !m_scripted_process_class_name.empty();
201-
}
202-
203-
std::string GetScriptedProcessClassName() const {
204-
return m_scripted_process_class_name;
205-
}
206-
207-
void SetScriptedProcessClassName(std::string name) {
208-
m_scripted_process_class_name = name;
209-
}
210-
211-
lldb_private::StructuredData::DictionarySP
212-
GetScriptedProcessDictionarySP() const {
213-
return m_scripted_process_dictionary_sp;
214-
}
215-
216-
void SetScriptedProcessDictionarySP(
217-
lldb_private::StructuredData::DictionarySP dictionary_sp) {
218-
m_scripted_process_dictionary_sp = dictionary_sp;
219-
}
220-
221199
protected:
222200
lldb::ListenerSP m_listener_sp;
223201
lldb::ListenerSP m_hijack_listener_sp;
@@ -235,11 +213,6 @@ class ProcessAttachInfo : public ProcessInstanceInfo {
235213
false; // Use an async attach where we start the attach and return
236214
// immediately (used by GUI programs with --waitfor so they can
237215
// call SBProcess::Stop() to cancel attach)
238-
std::string m_scripted_process_class_name; // The name of the class that will
239-
// manage a scripted process.
240-
StructuredData::DictionarySP
241-
m_scripted_process_dictionary_sp; // A dictionary that holds key/value
242-
// pairs passed to the scripted process.
243216
};
244217

245218
// This class tracks the Modification state of the process. Things that can

0 commit comments

Comments
 (0)