Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4baddb7

Browse files
bzcheesemanc-rhodes
authored andcommitted
[lldb] Add conversions for SBValueList and SBValue to the python bridge. (llvm#178574)
This patch adds support for: - PyObject -> SBValueList (which was surprisingly not there before!) - PyObject -> SBValue - SBValue -> ValueObjectSP using the ScriptInterpreter These three are the main remaining plumbing changes necessary before we can get to the meat of actually using ScriptedFrame to provide values to the printer/etc. Future patches build off this change in order to allow ScriptedFrames to provide variables and get values for variable expressions. (cherry picked from commit 8122d0e)
1 parent 341793b commit 4baddb7

7 files changed

Lines changed: 81 additions & 0 deletions

File tree

lldb/bindings/python/python-wrapper.swig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,18 @@ void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data
545545
return sb_ptr;
546546
}
547547

548+
void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBValueList(PyObject * data) {
549+
lldb::SBValueList *sb_ptr = NULL;
550+
551+
int valid_cast =
552+
SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBValueList, 0);
553+
554+
if (valid_cast == -1)
555+
return NULL;
556+
557+
return sb_ptr;
558+
}
559+
548560
void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *
549561
data) {
550562
lldb::SBMemoryRegionInfo *sb_ptr = NULL;

lldb/include/lldb/API/SBValue.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,9 @@ class LLDB_API SBValue {
505505
void SetSP(const lldb::ValueObjectSP &sp, lldb::DynamicValueType use_dynamic,
506506
bool use_synthetic, const char *name);
507507

508+
protected:
509+
friend class lldb_private::ScriptInterpreter;
510+
508511
private:
509512
typedef std::shared_ptr<lldb_private::ValueImpl> ValueImplSP;
510513
ValueImplSP m_opaque_sp;

lldb/include/lldb/Interpreter/ScriptInterpreter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,9 @@ class ScriptInterpreter : public PluginInterface {
609609
lldb::StackFrameListSP
610610
GetOpaqueTypeFromSBFrameList(const lldb::SBFrameList &exe_ctx) const;
611611

612+
lldb::ValueObjectSP
613+
GetOpaqueTypeFromSBValue(const lldb::SBValue &value) const;
614+
612615
protected:
613616
Debugger &m_debugger;
614617
lldb::ScriptLanguage m_script_lang;

lldb/source/Interpreter/ScriptInterpreter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "lldb/Utility/Status.h"
1616
#include "lldb/Utility/Stream.h"
1717
#include "lldb/Utility/StringList.h"
18+
#include "lldb/ValueObject/ValueObject.h"
1819
#if defined(_WIN32)
1920
#include "lldb/Host/windows/ConnectionGenericFileWindows.h"
2021
#endif
@@ -162,6 +163,15 @@ lldb::StackFrameListSP ScriptInterpreter::GetOpaqueTypeFromSBFrameList(
162163
return frame_list.m_opaque_sp;
163164
}
164165

166+
lldb::ValueObjectSP
167+
ScriptInterpreter::GetOpaqueTypeFromSBValue(const lldb::SBValue &value) const {
168+
if (!value.m_opaque_sp)
169+
return lldb::ValueObjectSP();
170+
171+
lldb_private::ValueLocker locker;
172+
return locker.GetLockedSP(*value.m_opaque_sp);
173+
}
174+
165175
lldb::ScriptLanguage
166176
ScriptInterpreter::StringToLanguage(const llvm::StringRef &language) {
167177
if (language.equals_insensitive(LanguageToString(eScriptLanguageNone)))

lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPythonInterface.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "../ScriptInterpreterPythonImpl.h"
1919
#include "ScriptedPythonInterface.h"
2020
#include "lldb/Symbol/SymbolContext.h"
21+
#include "lldb/ValueObject/ValueObjectList.h"
2122
#include <optional>
2223

2324
using namespace lldb;
@@ -273,4 +274,41 @@ ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StackFrameListSP>(
273274
return m_interpreter.GetOpaqueTypeFromSBFrameList(*sb_frame_list);
274275
}
275276

277+
template <>
278+
lldb::ValueObjectSP
279+
ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::ValueObjectSP>(
280+
python::PythonObject &p, Status &error) {
281+
lldb::SBValue *sb_value = reinterpret_cast<lldb::SBValue *>(
282+
python::LLDBSWIGPython_CastPyObjectToSBValue(p.get()));
283+
if (!sb_value) {
284+
error = Status::FromErrorStringWithFormat(
285+
"couldn't cast lldb::SBValue to lldb::ValueObjectSP");
286+
return {};
287+
}
288+
289+
return m_interpreter.GetOpaqueTypeFromSBValue(*sb_value);
290+
}
291+
292+
template <>
293+
lldb::ValueObjectListSP
294+
ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::ValueObjectListSP>(
295+
python::PythonObject &p, Status &error) {
296+
lldb::SBValueList *sb_value_list = reinterpret_cast<lldb::SBValueList *>(
297+
python::LLDBSWIGPython_CastPyObjectToSBValueList(p.get()));
298+
299+
if (!sb_value_list) {
300+
error = Status::FromErrorStringWithFormat(
301+
"couldn't cast lldb::SBValueList to lldb::ValueObjectListSP");
302+
return {};
303+
}
304+
305+
lldb::ValueObjectListSP out = std::make_shared<ValueObjectList>();
306+
for (uint32_t i = 0, e = sb_value_list->GetSize(); i < e; ++i) {
307+
SBValue value = sb_value_list->GetValueAtIndex(i);
308+
out->Append(m_interpreter.GetOpaqueTypeFromSBValue(value));
309+
}
310+
311+
return out;
312+
}
313+
276314
#endif

lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPythonInterface.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,10 @@ class ScriptedPythonInterface : virtual public ScriptedInterface {
656656
return python::SWIGBridge::ToSWIGWrapper(arg);
657657
}
658658

659+
python::PythonObject Transform(lldb::ValueObjectSP arg) {
660+
return python::SWIGBridge::ToSWIGWrapper(arg);
661+
}
662+
659663
template <typename T, typename U>
660664
void ReverseTransform(T &original_arg, U transformed_arg, Status &error) {
661665
// If U is not a PythonObject, don't touch it!
@@ -814,6 +818,16 @@ lldb::StackFrameListSP
814818
ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StackFrameListSP>(
815819
python::PythonObject &p, Status &error);
816820

821+
template <>
822+
lldb::ValueObjectSP
823+
ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::ValueObjectSP>(
824+
python::PythonObject &p, Status &error);
825+
826+
template <>
827+
lldb::ValueObjectListSP
828+
ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::ValueObjectListSP>(
829+
python::PythonObject &p, Status &error);
830+
817831
} // namespace lldb_private
818832

819833
#endif // LLDB_ENABLE_PYTHON

lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ void *LLDBSWIGPython_CastPyObjectToSBThread(PyObject *data);
269269
void *LLDBSWIGPython_CastPyObjectToSBFrame(PyObject *data);
270270
void *LLDBSWIGPython_CastPyObjectToSBSymbolContext(PyObject *data);
271271
void *LLDBSWIGPython_CastPyObjectToSBValue(PyObject *data);
272+
void *LLDBSWIGPython_CastPyObjectToSBValueList(PyObject *data);
272273
void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data);
273274
void *LLDBSWIGPython_CastPyObjectToSBExecutionContext(PyObject *data);
274275
void *LLDBSWIGPython_CastPyObjectToSBFrameList(PyObject *data);

0 commit comments

Comments
 (0)