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
11#include "lldb/API/SBDebugger.h"
12#include "lldb/Host/Config.h"
13#include "lldb/Utility/Log.h"
15
20#include <optional>
21
22using namespace lldb;
23using namespace lldb_private;
24
28
29template <>
36
37template <>
44
45template <>
48 if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(
50 return m_interpreter.GetStatusFromSBError(*sb_error);
51 error =
52 Status::FromErrorString("Couldn't cast lldb::SBError to lldb::Status.");
53
54 return {};
55}
56
57template <>
60 if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>(
62 return m_interpreter.GetOpaqueTypeFromSBEvent(*sb_event);
64 "Couldn't cast lldb::SBEvent to lldb_private::Event.");
65
66 return nullptr;
67}
68
69template <>
73 if (lldb::SBCommandReturnObject *sb_cmd_retobj =
74 reinterpret_cast<lldb::SBCommandReturnObject *>(
76 p.get())))
77 return m_interpreter.GetOpaqueTypeFromSBCommandReturnObject(*sb_cmd_retobj);
78 error =
79 Status::FromErrorString("couldn't cast lldb::SBCommandReturnObject to "
80 "lldb_private::CommandReturnObject.");
81 return nullptr;
82}
83
84template <>
88 if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>(
90 return m_interpreter.GetOpaqueTypeFromSBStream(*sb_stream);
92 "Couldn't cast lldb::SBStream to lldb_private::Stream.");
93
94 return nullptr;
95}
96
97template <>
101 if (lldb::SBFrame *sb_frame = reinterpret_cast<lldb::SBFrame *>(
103 return m_interpreter.GetOpaqueTypeFromSBFrame(*sb_frame);
105 "Couldn't cast lldb::SBFrame to lldb_private::StackFrame.");
106
107 return nullptr;
108}
109
110template <>
114 if (lldb::SBThread *sb_thread = reinterpret_cast<lldb::SBThread *>(
116 return m_interpreter.GetOpaqueTypeFromSBThread(*sb_thread);
118 "Couldn't cast lldb::SBThread to lldb_private::Thread.");
119
120 return nullptr;
121}
122
123template <>
127 if (lldb::SBSymbolContext *sb_symbol_context =
128 reinterpret_cast<lldb::SBSymbolContext *>(
130 return m_interpreter.GetOpaqueTypeFromSBSymbolContext(*sb_symbol_context);
132 "Couldn't cast lldb::SBSymbolContext to lldb_private::SymbolContext.");
133
134 return {};
135}
136
137template <>
141 lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
143
144 if (!sb_data) {
146 "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
147 return nullptr;
148 }
149
150 return m_interpreter.GetDataExtractorFromSBData(*sb_data);
151}
152
153template <>
157 lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
159
160 if (!sb_breakpoint) {
162 "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
163 return nullptr;
164 }
165
166 return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);
167}
168
169template <>
173 lldb::SBBreakpointLocation *sb_break_loc =
174 reinterpret_cast<lldb::SBBreakpointLocation *>(
176
177 if (!sb_break_loc) {
179 "Couldn't cast lldb::SBBreakpointLocation to "
180 "lldb::BreakpointLocationSP.");
181 return nullptr;
182 }
183
184 return m_interpreter.GetOpaqueTypeFromSBBreakpointLocation(*sb_break_loc);
185}
186
187template <>
190 lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(
192
193 if (!sb_attach_info) {
195 "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
196 return nullptr;
197 }
198
199 return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);
200}
201
202template <>
205 lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(
207
208 if (!sb_launch_info) {
210 "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
211 return nullptr;
212 }
213
214 return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);
215}
216
217template <>
218std::optional<MemoryRegionInfo>
220 std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
221
222 lldb::SBMemoryRegionInfo *sb_mem_reg_info =
223 reinterpret_cast<lldb::SBMemoryRegionInfo *>(
225
226 if (!sb_mem_reg_info) {
228 "Couldn't cast lldb::SBMemoryRegionInfo to "
229 "lldb_private::MemoryRegionInfo.");
230 return {};
231 }
232
233 return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
234}
235
236template <>
240
241 lldb::SBExecutionContext *sb_exe_ctx =
242 reinterpret_cast<lldb::SBExecutionContext *>(
244
245 if (!sb_exe_ctx) {
247 "Couldn't cast lldb::SBExecutionContext to "
248 "lldb::ExecutionContextRefSP.");
249 return {};
250 }
251
252 return m_interpreter.GetOpaqueTypeFromSBExecutionContext(*sb_exe_ctx);
253}
254
255template <>
260 llvm::Expected<unsigned long long> unsigned_or_err = p.AsUnsignedLongLong();
261 if (!unsigned_or_err) {
262 error = (Status::FromError(unsigned_or_err.takeError()));
263 return ret_val;
264 }
265 unsigned long long unsigned_val = *unsigned_or_err;
267 error = Status("value too large for lldb::DescriptionLevel.");
268 return ret_val;
269 }
270 return static_cast<lldb::DescriptionLevel>(unsigned_val);
271}
272
273template <>
277
278 lldb::SBFrameList *sb_frame_list = reinterpret_cast<lldb::SBFrameList *>(
280
281 if (!sb_frame_list) {
283 "couldn't cast lldb::SBFrameList to lldb::StackFrameListSP.");
284 return {};
285 }
286
287 return m_interpreter.GetOpaqueTypeFromSBFrameList(*sb_frame_list);
288}
289
290template <>
294 lldb::SBValue *sb_value = reinterpret_cast<lldb::SBValue *>(
296 if (!sb_value) {
298 "couldn't cast lldb::SBValue to lldb::ValueObjectSP");
299 return {};
300 }
301
302 return m_interpreter.GetOpaqueTypeFromSBValue(*sb_value);
303}
304
305template <>
309 lldb::SBTarget *sb_target = reinterpret_cast<lldb::SBTarget *>(
311 if (!sb_target) {
313 "couldn't cast lldb::SBTarget to lldb::TargetSP");
314 return {};
315 }
316
317 return m_interpreter.GetOpaqueTypeFromSBTarget(*sb_target);
318}
319
320template <>
324 // Two Python return shapes are accepted here so callers can go through
325 // Dispatch<ValueObjectListSP>() uniformly: an `SBValueList` wrapper
326 // (what most extension methods return) and a plain Python `list` of
327 // `SBValue` (what `get_recognized_arguments` is documented to return).
328 lldb::ValueObjectListSP out = std::make_shared<ValueObjectList>();
329 if (auto *sb_value_list = reinterpret_cast<lldb::SBValueList *>(
331 for (uint32_t i = 0, e = sb_value_list->GetSize(); i < e; ++i) {
332 SBValue value = sb_value_list->GetValueAtIndex(i);
333 out->Append(m_interpreter.GetOpaqueTypeFromSBValue(value));
334 }
335 return out;
336 }
337
338 // Fallback: a plain Python `list` of `SBValue`. Round-trip through
339 // `CreateStructuredObject` so we don't touch the `PyList_*` C API
340 // directly; unknown-shape items surface as `StructuredData::Generic`
341 // holding their opaque `PyObject*`, which we hand back to SWIG to
342 // recover the SBValue wrapper.
344 StructuredData::Array *arr = structured ? structured->GetAsArray() : nullptr;
345 if (arr) {
346 size_t index = 0;
347 bool aborted = false;
348 arr->ForEach([&](StructuredData::Object *item) {
349 StructuredData::Generic *generic = item ? item->GetAsGeneric() : nullptr;
350 if (!generic) {
352 "ValueObjectList item at index {0} is not a "
353 "StructuredData::Generic",
354 index);
355 aborted = true;
356 return false;
357 }
358 auto *sb_value = reinterpret_cast<lldb::SBValue *>(
360 static_cast<PyObject *>(generic->GetValue())));
361 if (sb_value)
362 if (auto valobj_sp = m_interpreter.GetOpaqueTypeFromSBValue(*sb_value))
363 out->Append(valobj_sp);
364 ++index;
365 return true;
366 });
367 if (aborted)
368 return {};
369 return out;
370 }
371
373 "couldn't extract ValueObjectList from Python return value");
374 return {};
375}
376
377template <>
378std::optional<lldb::ValueType>
380 std::optional<lldb::ValueType>>(python::PythonObject &p, Status &error) {
381 if (p.IsNone())
382 return std::nullopt;
383
384 llvm::Expected<unsigned long long> val = p.AsUnsignedLongLong();
385 if (!val) {
386 error = Status::FromError(val.takeError());
387 return std::nullopt;
388 }
389 unsigned long long unmasked = *val & ~kValueTypeFlagsMask;
390 unsigned long long flags = *val & kValueTypeFlagsMask;
391 if (unmasked == eValueTypeInvalid || unmasked > kLastValueType) {
393 "value type invalid or too large (got {0} | {1:x})", unmasked, flags);
394 return std::nullopt;
395 }
396
397 return static_cast<ValueType>(unmasked | flags);
398}
399
400template <>
404 if (lldb::SBDebugger *sb_dbg = reinterpret_cast<lldb::SBDebugger *>(
406 return m_interpreter.GetOpaqueTypeFromSBDebugger(*sb_dbg);
408 "couldn't cast lldb::SBDebugger to lldb::DebuggerSP.");
409 return {};
410}
411
412template <>
413std::vector<std::string>
416 std::vector<std::string> result;
418 if (!list.IsValid()) {
420 "couldn't extract std::vector<std::string>: not a Python list.");
421 return result;
422 }
423
424 const uint32_t size = list.GetSize();
425 result.reserve(size);
426 for (uint32_t i = 0; i < size; ++i) {
428 list.GetItemAtIndex(i).get());
429 if (!item.IsValid())
430 continue;
431 result.push_back(item.GetString().str());
432 }
433 return result;
434}
static llvm::raw_ostream & error(Stream &strm)
Represents a list of SBFrame objects.
Definition SBFrameList.h:31
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