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 <>
223 lldb::SBThreadPlan *sb_thread_plan = reinterpret_cast<lldb::SBThreadPlan *>(
225
226 if (!sb_thread_plan) {
228 "Couldn't cast lldb::SBThreadPlan to lldb::ThreadPlanSP.");
229 return {};
230 }
231
232 return ScriptInterpreterBridge::GetThreadPlan(*sb_thread_plan);
233}
234
235template <>
236std::optional<MemoryRegionInfo>
238 std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
239
240 lldb::SBMemoryRegionInfo *sb_mem_reg_info =
241 reinterpret_cast<lldb::SBMemoryRegionInfo *>(
243
244 if (!sb_mem_reg_info) {
246 "Couldn't cast lldb::SBMemoryRegionInfo to "
247 "lldb_private::MemoryRegionInfo.");
248 return {};
249 }
250
251 return ScriptInterpreterBridge::GetMemoryRegionInfo(*sb_mem_reg_info);
252}
253
254template <>
258
259 lldb::SBExecutionContext *sb_exe_ctx =
260 reinterpret_cast<lldb::SBExecutionContext *>(
262
263 if (!sb_exe_ctx) {
265 "Couldn't cast lldb::SBExecutionContext to "
266 "lldb::ExecutionContextRefSP.");
267 return {};
268 }
269
271}
272
273template <>
278 llvm::Expected<unsigned long long> unsigned_or_err = p.AsUnsignedLongLong();
279 if (!unsigned_or_err) {
280 error = (Status::FromError(unsigned_or_err.takeError()));
281 return ret_val;
282 }
283 unsigned long long unsigned_val = *unsigned_or_err;
285 error = Status("value too large for lldb::DescriptionLevel.");
286 return ret_val;
287 }
288 return static_cast<lldb::DescriptionLevel>(unsigned_val);
289}
290
291template <>
295
296 lldb::SBFrameList *sb_frame_list = reinterpret_cast<lldb::SBFrameList *>(
298
299 if (!sb_frame_list) {
301 "couldn't cast lldb::SBFrameList to lldb::StackFrameListSP.");
302 return {};
303 }
304
305 return ScriptInterpreterBridge::GetStackFrameList(*sb_frame_list);
306}
307
308template <>
312 lldb::SBValue *sb_value = reinterpret_cast<lldb::SBValue *>(
314 if (!sb_value) {
316 "couldn't cast lldb::SBValue to lldb::ValueObjectSP");
317 return {};
318 }
319
321}
322
323template <>
327 lldb::SBTarget *sb_target = reinterpret_cast<lldb::SBTarget *>(
329 if (!sb_target) {
331 "couldn't cast lldb::SBTarget to lldb::TargetSP");
332 return {};
333 }
334
335 return ScriptInterpreterBridge::GetTarget(*sb_target);
336}
337
338template <>
342 // Two Python return shapes are accepted here so callers can go through
343 // Dispatch<ValueObjectListSP>() uniformly: an `SBValueList` wrapper
344 // (what most extension methods return) and a plain Python `list` of
345 // `SBValue` (what `get_recognized_arguments` is documented to return).
346 lldb::ValueObjectListSP out = std::make_shared<ValueObjectList>();
347 if (auto *sb_value_list = reinterpret_cast<lldb::SBValueList *>(
349 for (uint32_t i = 0, e = sb_value_list->GetSize(); i < e; ++i) {
350 SBValue value = sb_value_list->GetValueAtIndex(i);
351 out->Append(ScriptInterpreterBridge::GetValueObject(value));
352 }
353 return out;
354 }
355
356 // Fallback: a plain Python `list` of `SBValue`. Round-trip through
357 // `CreateStructuredObject` so we don't touch the `PyList_*` C API
358 // directly; unknown-shape items surface as `StructuredData::Generic`
359 // holding their opaque `PyObject*`, which we hand back to SWIG to
360 // recover the SBValue wrapper.
362 StructuredData::Array *arr = structured ? structured->GetAsArray() : nullptr;
363 if (arr) {
364 size_t index = 0;
365 bool aborted = false;
366 arr->ForEach([&](StructuredData::Object *item) {
367 StructuredData::Generic *generic = item ? item->GetAsGeneric() : nullptr;
368 if (!generic) {
370 "ValueObjectList item at index {0} is not a "
371 "StructuredData::Generic",
372 index);
373 aborted = true;
374 return false;
375 }
376 auto *sb_value = reinterpret_cast<lldb::SBValue *>(
378 static_cast<PyObject *>(generic->GetValue())));
379 if (sb_value)
380 if (auto valobj_sp = ScriptInterpreterBridge::GetValueObject(*sb_value))
381 out->Append(valobj_sp);
382 ++index;
383 return true;
384 });
385 if (aborted)
386 return {};
387 return out;
388 }
389
391 "couldn't extract ValueObjectList from Python return value");
392 return {};
393}
394
395template <>
396std::optional<lldb::ValueType>
398 std::optional<lldb::ValueType>>(python::PythonObject &p, Status &error) {
399 if (p.IsNone())
400 return std::nullopt;
401
402 llvm::Expected<unsigned long long> val = p.AsUnsignedLongLong();
403 if (!val) {
404 error = Status::FromError(val.takeError());
405 return std::nullopt;
406 }
407 unsigned long long unmasked = *val & ~kValueTypeFlagsMask;
408 unsigned long long flags = *val & kValueTypeFlagsMask;
409 if (unmasked == eValueTypeInvalid || unmasked > kLastValueType) {
411 "value type invalid or too large (got {0} | {1:x})", unmasked, flags);
412 return std::nullopt;
413 }
414
415 return static_cast<ValueType>(unmasked | flags);
416}
417
418template <>
422 if (lldb::SBDebugger *sb_dbg = reinterpret_cast<lldb::SBDebugger *>(
426 "couldn't cast lldb::SBDebugger to lldb::DebuggerSP.");
427 return {};
428}
429
430template <>
431std::vector<std::string>
434 std::vector<std::string> result;
436 if (!list.IsValid()) {
438 "couldn't extract std::vector<std::string>: not a Python list.");
439 return result;
440 }
441
442 const uint32_t size = list.GetSize();
443 result.reserve(size);
444 for (uint32_t i = 0; i < size; ++i) {
446 list.GetItemAtIndex(i).get());
447 if (!item.IsValid())
448 continue;
449 result.push_back(item.GetString().str());
450 }
451 return result;
452}
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 lldb::ThreadPlanSP GetThreadPlan(const lldb::SBThreadPlan &thread_plan)
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_CastPyObjectToSBThreadPlan(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::ThreadPlan > ThreadPlanSP
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:89
@ 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