LLDB mainline
ScriptedPythonInterface.cpp
Go to the documentation of this file.
1//===-- ScriptedPythonInterface.cpp ---------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "../lldb-python.h"
10
12#include "lldb/API/SBValue.h"
14#include "lldb/Host/Config.h"
15#include "lldb/Utility/Log.h"
17
22#include <optional>
23
24using namespace lldb;
25using namespace lldb_private;
26
30
31template <>
38
39template <>
46
47template <>
50 if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(
52 return ScriptInterpreterBridge::GetStatus(*sb_error);
53 error =
54 Status::FromErrorString("Couldn't cast lldb::SBError to lldb::Status.");
55
56 return {};
57}
58
59template <>
62 if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>(
64 return ScriptInterpreterBridge::GetEvent(*sb_event);
66 "Couldn't cast lldb::SBEvent to lldb_private::Event.");
67
68 return nullptr;
69}
70
71template <>
75 if (lldb::SBCommandReturnObject *sb_cmd_retobj =
76 reinterpret_cast<lldb::SBCommandReturnObject *>(
78 p.get())))
80 error =
81 Status::FromErrorString("couldn't cast lldb::SBCommandReturnObject to "
82 "lldb_private::CommandReturnObject.");
83 return nullptr;
84}
85
86template <>
90 if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>(
92 return ScriptInterpreterBridge::GetStream(*sb_stream);
94 "Couldn't cast lldb::SBStream to lldb_private::Stream.");
95
96 return nullptr;
97}
98
99template <>
103 if (lldb::SBFrame *sb_frame = reinterpret_cast<lldb::SBFrame *>(
107 "Couldn't cast lldb::SBFrame to lldb_private::StackFrame.");
108
109 return nullptr;
110}
111
112template <>
116 if (lldb::SBThread *sb_thread = reinterpret_cast<lldb::SBThread *>(
118 return ScriptInterpreterBridge::GetThread(*sb_thread);
120 "Couldn't cast lldb::SBThread to lldb_private::Thread.");
121
122 return nullptr;
123}
124
125template <>
129 if (lldb::SBSymbolContext *sb_symbol_context =
130 reinterpret_cast<lldb::SBSymbolContext *>(
132 return ScriptInterpreterBridge::GetSymbolContext(*sb_symbol_context);
134 "Couldn't cast lldb::SBSymbolContext to lldb_private::SymbolContext.");
135
136 return {};
137}
138
139template <>
143 lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
145
146 if (!sb_data) {
148 "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
149 return nullptr;
150 }
151
153}
154
155template <>
159 lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
161
162 if (!sb_breakpoint) {
164 "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
165 return nullptr;
166 }
167
168 return ScriptInterpreterBridge::GetBreakpoint(*sb_breakpoint);
169}
170
171template <>
175 lldb::SBBreakpointLocation *sb_break_loc =
176 reinterpret_cast<lldb::SBBreakpointLocation *>(
178
179 if (!sb_break_loc) {
181 "Couldn't cast lldb::SBBreakpointLocation to "
182 "lldb::BreakpointLocationSP.");
183 return nullptr;
184 }
185
187}
188
189template <>
192 lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(
194
195 if (!sb_attach_info) {
197 "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
198 return nullptr;
199 }
200
201 return ScriptInterpreterBridge::GetProcessAttachInfo(*sb_attach_info);
202}
203
204template <>
207 lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(
209
210 if (!sb_launch_info) {
212 "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
213 return nullptr;
214 }
215
216 return ScriptInterpreterBridge::GetProcessLaunchInfo(*sb_launch_info);
217}
218
219template <>
220std::optional<MemoryRegionInfo>
222 std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
223
224 lldb::SBMemoryRegionInfo *sb_mem_reg_info =
225 reinterpret_cast<lldb::SBMemoryRegionInfo *>(
227
228 if (!sb_mem_reg_info) {
230 "Couldn't cast lldb::SBMemoryRegionInfo to "
231 "lldb_private::MemoryRegionInfo.");
232 return {};
233 }
234
235 return ScriptInterpreterBridge::GetMemoryRegionInfo(*sb_mem_reg_info);
236}
237
238template <>
242
243 lldb::SBExecutionContext *sb_exe_ctx =
244 reinterpret_cast<lldb::SBExecutionContext *>(
246
247 if (!sb_exe_ctx) {
249 "Couldn't cast lldb::SBExecutionContext to "
250 "lldb::ExecutionContextRefSP.");
251 return {};
252 }
253
255}
256
257template <>
262 llvm::Expected<unsigned long long> unsigned_or_err = p.AsUnsignedLongLong();
263 if (!unsigned_or_err) {
264 error = (Status::FromError(unsigned_or_err.takeError()));
265 return ret_val;
266 }
267 unsigned long long unsigned_val = *unsigned_or_err;
269 error = Status("value too large for lldb::DescriptionLevel.");
270 return ret_val;
271 }
272 return static_cast<lldb::DescriptionLevel>(unsigned_val);
273}
274
275template <>
279
280 lldb::SBFrameList *sb_frame_list = reinterpret_cast<lldb::SBFrameList *>(
282
283 if (!sb_frame_list) {
285 "couldn't cast lldb::SBFrameList to lldb::StackFrameListSP.");
286 return {};
287 }
288
289 return ScriptInterpreterBridge::GetStackFrameList(*sb_frame_list);
290}
291
292template <>
296 lldb::SBValue *sb_value = reinterpret_cast<lldb::SBValue *>(
298 if (!sb_value) {
300 "couldn't cast lldb::SBValue to lldb::ValueObjectSP");
301 return {};
302 }
303
305}
306
307template <>
311 lldb::SBTarget *sb_target = reinterpret_cast<lldb::SBTarget *>(
313 if (!sb_target) {
315 "couldn't cast lldb::SBTarget to lldb::TargetSP");
316 return {};
317 }
318
319 return ScriptInterpreterBridge::GetTarget(*sb_target);
320}
321
322template <>
326 // Two Python return shapes are accepted here so callers can go through
327 // Dispatch<ValueObjectListSP>() uniformly: an `SBValueList` wrapper
328 // (what most extension methods return) and a plain Python `list` of
329 // `SBValue` (what `get_recognized_arguments` is documented to return).
330 lldb::ValueObjectListSP out = std::make_shared<ValueObjectList>();
331 if (auto *sb_value_list = reinterpret_cast<lldb::SBValueList *>(
333 for (uint32_t i = 0, e = sb_value_list->GetSize(); i < e; ++i) {
334 SBValue value = sb_value_list->GetValueAtIndex(i);
335 out->Append(ScriptInterpreterBridge::GetValueObject(value));
336 }
337 return out;
338 }
339
340 // Fallback: a plain Python `list` of `SBValue`. Round-trip through
341 // `CreateStructuredObject` so we don't touch the `PyList_*` C API
342 // directly; unknown-shape items surface as `StructuredData::Generic`
343 // holding their opaque `PyObject*`, which we hand back to SWIG to
344 // recover the SBValue wrapper.
346 StructuredData::Array *arr = structured ? structured->GetAsArray() : nullptr;
347 if (arr) {
348 size_t index = 0;
349 bool aborted = false;
350 arr->ForEach([&](StructuredData::Object *item) {
351 StructuredData::Generic *generic = item ? item->GetAsGeneric() : nullptr;
352 if (!generic) {
354 "ValueObjectList item at index {0} is not a "
355 "StructuredData::Generic",
356 index);
357 aborted = true;
358 return false;
359 }
360 auto *sb_value = reinterpret_cast<lldb::SBValue *>(
362 static_cast<PyObject *>(generic->GetValue())));
363 if (sb_value)
364 if (auto valobj_sp = ScriptInterpreterBridge::GetValueObject(*sb_value))
365 out->Append(valobj_sp);
366 ++index;
367 return true;
368 });
369 if (aborted)
370 return {};
371 return out;
372 }
373
375 "couldn't extract ValueObjectList from Python return value");
376 return {};
377}
378
379template <>
380std::optional<lldb::ValueType>
382 std::optional<lldb::ValueType>>(python::PythonObject &p, Status &error) {
383 if (p.IsNone())
384 return std::nullopt;
385
386 llvm::Expected<unsigned long long> val = p.AsUnsignedLongLong();
387 if (!val) {
388 error = Status::FromError(val.takeError());
389 return std::nullopt;
390 }
391 unsigned long long unmasked = *val & ~kValueTypeFlagsMask;
392 unsigned long long flags = *val & kValueTypeFlagsMask;
393 if (unmasked == eValueTypeInvalid || unmasked > kLastValueType) {
395 "value type invalid or too large (got {0} | {1:x})", unmasked, flags);
396 return std::nullopt;
397 }
398
399 return static_cast<ValueType>(unmasked | flags);
400}
401
402template <>
406 if (lldb::SBDebugger *sb_dbg = reinterpret_cast<lldb::SBDebugger *>(
410 "couldn't cast lldb::SBDebugger to lldb::DebuggerSP.");
411 return {};
412}
413
414template <>
415std::vector<std::string>
418 std::vector<std::string> result;
420 if (!list.IsValid()) {
422 "couldn't extract std::vector<std::string>: not a Python list.");
423 return result;
424 }
425
426 const uint32_t size = list.GetSize();
427 result.reserve(size);
428 for (uint32_t i = 0; i < size; ++i) {
430 list.GetItemAtIndex(i).get());
431 if (!item.IsValid())
432 continue;
433 result.push_back(item.GetString().str());
434 }
435 return result;
436}
static llvm::raw_ostream & error(Stream &strm)
Represents a list of SBFrame objects.
Definition SBFrameList.h:31
static SymbolContext GetSymbolContext(const lldb::SBSymbolContext &sym_ctx)
static lldb::DataExtractorSP GetDataExtractor(const lldb::SBData &data)
static lldb::BreakpointLocationSP GetBreakpointLocation(const lldb::SBBreakpointLocation &break_loc)
static lldb::ProcessLaunchInfoSP GetProcessLaunchInfo(const lldb::SBLaunchInfo &launch_info)
static CommandReturnObject * GetCommandReturnObject(const lldb::SBCommandReturnObject &cmd_retobj)
static lldb::BreakpointSP GetBreakpoint(const lldb::SBBreakpoint &breakpoint)
static lldb::ThreadSP GetThread(const lldb::SBThread &thread)
static lldb::ExecutionContextRefSP GetExecutionContextRef(const lldb::SBExecutionContext &exe_ctx)
static lldb::TargetSP GetTarget(const lldb::SBTarget &target)
static lldb::DebuggerSP GetDebugger(const lldb::SBDebugger &debugger)
static lldb::ProcessAttachInfoSP GetProcessAttachInfo(const lldb::SBAttachInfo &attach_info)
static lldb::StackFrameSP GetStackFrame(const lldb::SBFrame &frame)
static lldb::StackFrameListSP GetStackFrameList(const lldb::SBFrameList &frame_list)
static lldb::StreamSP GetStream(const lldb::SBStream &stream)
static Event * GetEvent(const lldb::SBEvent &event)
static std::optional< MemoryRegionInfo > GetMemoryRegionInfo(const lldb::SBMemoryRegionInfo &mem_region)
static lldb::ValueObjectSP GetValueObject(const lldb::SBValue &value)
static Status GetStatus(const lldb::SBError &error)
ScriptInterpreterPythonImpl & m_interpreter
T ExtractValueFromPythonObject(python::PythonObject &p, Status &error)
ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter)
An error handling class.
Definition Status.h:118
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition Status.h:151
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition Status.cpp:136
bool ForEach(std::function< bool(Object *object)> const &foreach_callback) const
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
std::shared_ptr< Array > ArraySP
Defines a symbol context baton that can be handed other debug core functions.
StructuredData::DictionarySP CreateStructuredDictionary() const
StructuredData::ArraySP CreateStructuredArray() const
StructuredData::ObjectSP CreateStructuredObject() const
llvm::Expected< unsigned long long > AsUnsignedLongLong() const
void * LLDBSWIGPython_CastPyObjectToSBExecutionContext(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBFrameList(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBThread(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBData(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBTarget(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBSymbolContext(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBStream(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBCommandReturnObject(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBDebugger(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBBreakpointLocation(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBError(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBLaunchInfo(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBValueList(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBValue(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBEvent(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBBreakpoint(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBFrame(PyObject *data)
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eDescriptionLevelBrief
@ kNumDescriptionLevels
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::ProcessAttachInfo > ProcessAttachInfoSP
std::shared_ptr< lldb_private::Stream > StreamSP
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
std::shared_ptr< lldb_private::ValueObjectList > ValueObjectListSP
std::shared_ptr< lldb_private::Debugger > DebuggerSP
class LLDB_API SBMemoryRegionInfo
Definition SBDefines.h:88
@ kValueTypeFlagsMask
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
std::shared_ptr< lldb_private::ProcessLaunchInfo > ProcessLaunchInfoSP
std::shared_ptr< lldb_private::StackFrameList > StackFrameListSP
std::shared_ptr< lldb_private::ExecutionContextRef > ExecutionContextRefSP