Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPythonInterface.cpp
39688 views
//===-- ScriptedPythonInterface.cpp ---------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Host/Config.h"9#include "lldb/Utility/Log.h"10#include "lldb/lldb-enumerations.h"1112#if LLDB_ENABLE_PYTHON1314// LLDB Python header must be included first15#include "../lldb-python.h"1617#include "../ScriptInterpreterPythonImpl.h"18#include "ScriptedPythonInterface.h"19#include <optional>2021using namespace lldb;22using namespace lldb_private;2324ScriptedPythonInterface::ScriptedPythonInterface(25ScriptInterpreterPythonImpl &interpreter)26: ScriptedInterface(), m_interpreter(interpreter) {}2728template <>29StructuredData::ArraySP30ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>(31python::PythonObject &p, Status &error) {32python::PythonList result_list(python::PyRefType::Borrowed, p.get());33return result_list.CreateStructuredArray();34}3536template <>37StructuredData::DictionarySP38ScriptedPythonInterface::ExtractValueFromPythonObject<39StructuredData::DictionarySP>(python::PythonObject &p, Status &error) {40python::PythonDictionary result_dict(python::PyRefType::Borrowed, p.get());41return result_dict.CreateStructuredDictionary();42}4344template <>45Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>(46python::PythonObject &p, Status &error) {47if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(48python::LLDBSWIGPython_CastPyObjectToSBError(p.get())))49return m_interpreter.GetStatusFromSBError(*sb_error);50error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status.");5152return {};53}5455template <>56Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>(57python::PythonObject &p, Status &error) {58if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>(59python::LLDBSWIGPython_CastPyObjectToSBEvent(p.get())))60return m_interpreter.GetOpaqueTypeFromSBEvent(*sb_event);61error.SetErrorString("Couldn't cast lldb::SBEvent to lldb_private::Event.");6263return nullptr;64}6566template <>67lldb::StreamSP68ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StreamSP>(69python::PythonObject &p, Status &error) {70if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>(71python::LLDBSWIGPython_CastPyObjectToSBStream(p.get())))72return m_interpreter.GetOpaqueTypeFromSBStream(*sb_stream);73error.SetErrorString("Couldn't cast lldb::SBStream to lldb_private::Stream.");7475return nullptr;76}7778template <>79lldb::DataExtractorSP80ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(81python::PythonObject &p, Status &error) {82lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(83python::LLDBSWIGPython_CastPyObjectToSBData(p.get()));8485if (!sb_data) {86error.SetErrorString(87"Couldn't cast lldb::SBData to lldb::DataExtractorSP.");88return nullptr;89}9091return m_interpreter.GetDataExtractorFromSBData(*sb_data);92}9394template <>95lldb::BreakpointSP96ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>(97python::PythonObject &p, Status &error) {98lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(99python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get()));100101if (!sb_breakpoint) {102error.SetErrorString(103"Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");104return nullptr;105}106107return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);108}109110template <>111lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<112lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) {113lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(114python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get()));115116if (!sb_attach_info) {117error.SetErrorString(118"Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");119return nullptr;120}121122return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);123}124125template <>126lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<127lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) {128lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(129python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get()));130131if (!sb_launch_info) {132error.SetErrorString(133"Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");134return nullptr;135}136137return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);138}139140template <>141std::optional<MemoryRegionInfo>142ScriptedPythonInterface::ExtractValueFromPythonObject<143std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {144145lldb::SBMemoryRegionInfo *sb_mem_reg_info =146reinterpret_cast<lldb::SBMemoryRegionInfo *>(147python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));148149if (!sb_mem_reg_info) {150error.SetErrorString(151"Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP.");152return {};153}154155return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);156}157158#endif159160161