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/Host/Config.h" 10 #include "lldb/Utility/Log.h" 11 #include "lldb/lldb-enumerations.h" 12 13 #if LLDB_ENABLE_PYTHON 14 15 // LLDB Python header must be included first 16 #include "../lldb-python.h" 17 18 #include "../ScriptInterpreterPythonImpl.h" 19 #include "ScriptedPythonInterface.h" 20 #include <optional> 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 ScriptedPythonInterface::ScriptedPythonInterface( 26 ScriptInterpreterPythonImpl &interpreter) 27 : ScriptedInterface(), m_interpreter(interpreter) {} 28 29 template <> 30 StructuredData::ArraySP 31 ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>( 32 python::PythonObject &p, Status &error) { 33 python::PythonList result_list(python::PyRefType::Borrowed, p.get()); 34 return result_list.CreateStructuredArray(); 35 } 36 37 template <> 38 StructuredData::DictionarySP 39 ScriptedPythonInterface::ExtractValueFromPythonObject< 40 StructuredData::DictionarySP>(python::PythonObject &p, Status &error) { 41 python::PythonDictionary result_dict(python::PyRefType::Borrowed, p.get()); 42 return result_dict.CreateStructuredDictionary(); 43 } 44 45 template <> 46 Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>( 47 python::PythonObject &p, Status &error) { 48 if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>( 49 python::LLDBSWIGPython_CastPyObjectToSBError(p.get()))) 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 57 template <> 58 Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>( 59 python::PythonObject &p, Status &error) { 60 if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>( 61 python::LLDBSWIGPython_CastPyObjectToSBEvent(p.get()))) 62 return m_interpreter.GetOpaqueTypeFromSBEvent(*sb_event); 63 error = Status::FromErrorString( 64 "Couldn't cast lldb::SBEvent to lldb_private::Event."); 65 66 return nullptr; 67 } 68 69 template <> 70 lldb::StreamSP 71 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StreamSP>( 72 python::PythonObject &p, Status &error) { 73 if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>( 74 python::LLDBSWIGPython_CastPyObjectToSBStream(p.get()))) 75 return m_interpreter.GetOpaqueTypeFromSBStream(*sb_stream); 76 error = Status::FromErrorString( 77 "Couldn't cast lldb::SBStream to lldb_private::Stream."); 78 79 return nullptr; 80 } 81 82 template <> 83 lldb::DataExtractorSP 84 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>( 85 python::PythonObject &p, Status &error) { 86 lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>( 87 python::LLDBSWIGPython_CastPyObjectToSBData(p.get())); 88 89 if (!sb_data) { 90 error = Status::FromErrorStringWithFormat( 91 "Couldn't cast lldb::SBData to lldb::DataExtractorSP."); 92 return nullptr; 93 } 94 95 return m_interpreter.GetDataExtractorFromSBData(*sb_data); 96 } 97 98 template <> 99 lldb::BreakpointSP 100 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>( 101 python::PythonObject &p, Status &error) { 102 lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>( 103 python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get())); 104 105 if (!sb_breakpoint) { 106 error = Status::FromErrorStringWithFormat( 107 "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP."); 108 return nullptr; 109 } 110 111 return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint); 112 } 113 114 template <> 115 lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject< 116 lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) { 117 lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>( 118 python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get())); 119 120 if (!sb_attach_info) { 121 error = Status::FromErrorStringWithFormat( 122 "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP."); 123 return nullptr; 124 } 125 126 return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info); 127 } 128 129 template <> 130 lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject< 131 lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) { 132 lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>( 133 python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get())); 134 135 if (!sb_launch_info) { 136 error = Status::FromErrorStringWithFormat( 137 "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP."); 138 return nullptr; 139 } 140 141 return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info); 142 } 143 144 template <> 145 std::optional<MemoryRegionInfo> 146 ScriptedPythonInterface::ExtractValueFromPythonObject< 147 std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) { 148 149 lldb::SBMemoryRegionInfo *sb_mem_reg_info = 150 reinterpret_cast<lldb::SBMemoryRegionInfo *>( 151 python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get())); 152 153 if (!sb_mem_reg_info) { 154 error = Status::FromErrorStringWithFormat( 155 "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP."); 156 return {}; 157 } 158 159 return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info); 160 } 161 162 #endif 163