264 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Host/Config.h"10#include "lldb/Utility/Log.h"11#include "lldb/lldb-enumerations.h"12 13#if LLDB_ENABLE_PYTHON14 15// LLDB Python header must be included first16#include "../lldb-python.h"17 18#include "../ScriptInterpreterPythonImpl.h"19#include "ScriptedPythonInterface.h"20#include "lldb/Symbol/SymbolContext.h"21#include <optional>22 23using namespace lldb;24using namespace lldb_private;25 26ScriptedPythonInterface::ScriptedPythonInterface(27 ScriptInterpreterPythonImpl &interpreter)28 : ScriptedInterface(), m_interpreter(interpreter) {}29 30template <>31StructuredData::ArraySP32ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>(33 python::PythonObject &p, Status &error) {34 python::PythonList result_list(python::PyRefType::Borrowed, p.get());35 return result_list.CreateStructuredArray();36}37 38template <>39StructuredData::DictionarySP40ScriptedPythonInterface::ExtractValueFromPythonObject<41 StructuredData::DictionarySP>(python::PythonObject &p, Status &error) {42 python::PythonDictionary result_dict(python::PyRefType::Borrowed, p.get());43 return result_dict.CreateStructuredDictionary();44}45 46template <>47Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>(48 python::PythonObject &p, Status &error) {49 if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(50 python::LLDBSWIGPython_CastPyObjectToSBError(p.get())))51 return m_interpreter.GetStatusFromSBError(*sb_error);52 error =53 Status::FromErrorString("Couldn't cast lldb::SBError to lldb::Status.");54 55 return {};56}57 58template <>59Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>(60 python::PythonObject &p, Status &error) {61 if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>(62 python::LLDBSWIGPython_CastPyObjectToSBEvent(p.get())))63 return m_interpreter.GetOpaqueTypeFromSBEvent(*sb_event);64 error = Status::FromErrorString(65 "Couldn't cast lldb::SBEvent to lldb_private::Event.");66 67 return nullptr;68}69 70template <>71lldb::StreamSP72ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StreamSP>(73 python::PythonObject &p, Status &error) {74 if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>(75 python::LLDBSWIGPython_CastPyObjectToSBStream(p.get())))76 return m_interpreter.GetOpaqueTypeFromSBStream(*sb_stream);77 error = Status::FromErrorString(78 "Couldn't cast lldb::SBStream to lldb_private::Stream.");79 80 return nullptr;81}82 83template <>84lldb::StackFrameSP85ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StackFrameSP>(86 python::PythonObject &p, Status &error) {87 if (lldb::SBFrame *sb_frame = reinterpret_cast<lldb::SBFrame *>(88 python::LLDBSWIGPython_CastPyObjectToSBFrame(p.get())))89 return m_interpreter.GetOpaqueTypeFromSBFrame(*sb_frame);90 error = Status::FromErrorString(91 "Couldn't cast lldb::SBFrame to lldb_private::StackFrame.");92 93 return nullptr;94}95 96template <>97SymbolContext98ScriptedPythonInterface::ExtractValueFromPythonObject<SymbolContext>(99 python::PythonObject &p, Status &error) {100 if (lldb::SBSymbolContext *sb_symbol_context =101 reinterpret_cast<lldb::SBSymbolContext *>(102 python::LLDBSWIGPython_CastPyObjectToSBSymbolContext(p.get())))103 return m_interpreter.GetOpaqueTypeFromSBSymbolContext(*sb_symbol_context);104 error = Status::FromErrorString(105 "Couldn't cast lldb::SBSymbolContext to lldb_private::SymbolContext.");106 107 return {};108}109 110template <>111lldb::DataExtractorSP112ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(113 python::PythonObject &p, Status &error) {114 lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(115 python::LLDBSWIGPython_CastPyObjectToSBData(p.get()));116 117 if (!sb_data) {118 error = Status::FromErrorStringWithFormat(119 "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");120 return nullptr;121 }122 123 return m_interpreter.GetDataExtractorFromSBData(*sb_data);124}125 126template <>127lldb::BreakpointSP128ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>(129 python::PythonObject &p, Status &error) {130 lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(131 python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get()));132 133 if (!sb_breakpoint) {134 error = Status::FromErrorStringWithFormat(135 "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");136 return nullptr;137 }138 139 return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);140}141 142template <>143lldb::BreakpointLocationSP144ScriptedPythonInterface::ExtractValueFromPythonObject<145 lldb::BreakpointLocationSP>(python::PythonObject &p, Status &error) {146 lldb::SBBreakpointLocation *sb_break_loc =147 reinterpret_cast<lldb::SBBreakpointLocation *>(148 python::LLDBSWIGPython_CastPyObjectToSBBreakpointLocation(p.get()));149 150 if (!sb_break_loc) {151 error = Status::FromErrorStringWithFormat(152 "Couldn't cast lldb::SBBreakpointLocation to "153 "lldb::BreakpointLocationSP.");154 return nullptr;155 }156 157 return m_interpreter.GetOpaqueTypeFromSBBreakpointLocation(*sb_break_loc);158}159 160template <>161lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<162 lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) {163 lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(164 python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get()));165 166 if (!sb_attach_info) {167 error = Status::FromErrorStringWithFormat(168 "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");169 return nullptr;170 }171 172 return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);173}174 175template <>176lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<177 lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) {178 lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(179 python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get()));180 181 if (!sb_launch_info) {182 error = Status::FromErrorStringWithFormat(183 "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");184 return nullptr;185 }186 187 return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);188}189 190template <>191std::optional<MemoryRegionInfo>192ScriptedPythonInterface::ExtractValueFromPythonObject<193 std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {194 195 lldb::SBMemoryRegionInfo *sb_mem_reg_info =196 reinterpret_cast<lldb::SBMemoryRegionInfo *>(197 python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));198 199 if (!sb_mem_reg_info) {200 error = Status::FromErrorStringWithFormat(201 "Couldn't cast lldb::SBMemoryRegionInfo to "202 "lldb_private::MemoryRegionInfo.");203 return {};204 }205 206 return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);207}208 209template <>210lldb::ExecutionContextRefSP211ScriptedPythonInterface::ExtractValueFromPythonObject<212 lldb::ExecutionContextRefSP>(python::PythonObject &p, Status &error) {213 214 lldb::SBExecutionContext *sb_exe_ctx =215 reinterpret_cast<lldb::SBExecutionContext *>(216 python::LLDBSWIGPython_CastPyObjectToSBExecutionContext(p.get()));217 218 if (!sb_exe_ctx) {219 error = Status::FromErrorStringWithFormat(220 "Couldn't cast lldb::SBExecutionContext to "221 "lldb::ExecutionContextRefSP.");222 return {};223 }224 225 return m_interpreter.GetOpaqueTypeFromSBExecutionContext(*sb_exe_ctx);226}227 228template <>229lldb::DescriptionLevel230ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DescriptionLevel>(231 python::PythonObject &p, Status &error) {232 lldb::DescriptionLevel ret_val = lldb::eDescriptionLevelBrief;233 llvm::Expected<unsigned long long> unsigned_or_err = p.AsUnsignedLongLong();234 if (!unsigned_or_err) {235 error = (Status::FromError(unsigned_or_err.takeError()));236 return ret_val;237 }238 unsigned long long unsigned_val = *unsigned_or_err;239 if (unsigned_val >= lldb::DescriptionLevel::kNumDescriptionLevels) {240 error = Status("value too large for lldb::DescriptionLevel.");241 return ret_val;242 }243 return static_cast<lldb::DescriptionLevel>(unsigned_val);244}245 246template <>247lldb::StackFrameListSP248ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StackFrameListSP>(249 python::PythonObject &p, Status &error) {250 251 lldb::SBFrameList *sb_frame_list = reinterpret_cast<lldb::SBFrameList *>(252 python::LLDBSWIGPython_CastPyObjectToSBFrameList(p.get()));253 254 if (!sb_frame_list) {255 error = Status::FromErrorStringWithFormat(256 "couldn't cast lldb::SBFrameList to lldb::StackFrameListSP.");257 return {};258 }259 260 return m_interpreter.GetOpaqueTypeFromSBFrameList(*sb_frame_list);261}262 263#endif264