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 void ScriptedPythonInterface::ReverseTransform( 31 lldb_private::Stream *&original_arg, python::PythonObject transformed_arg, 32 Status &error) { 33 Stream *s = ExtractValueFromPythonObject<Stream *>(transformed_arg, error); 34 *original_arg = *s; 35 original_arg->PutCString(static_cast<StreamString *>(s)->GetData()); 36 } 37 38 template <> 39 StructuredData::ArraySP 40 ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>( 41 python::PythonObject &p, Status &error) { 42 python::PythonList result_list(python::PyRefType::Borrowed, p.get()); 43 return result_list.CreateStructuredArray(); 44 } 45 46 template <> 47 StructuredData::DictionarySP 48 ScriptedPythonInterface::ExtractValueFromPythonObject< 49 StructuredData::DictionarySP>(python::PythonObject &p, Status &error) { 50 python::PythonDictionary result_dict(python::PyRefType::Borrowed, p.get()); 51 return result_dict.CreateStructuredDictionary(); 52 } 53 54 template <> 55 Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>( 56 python::PythonObject &p, Status &error) { 57 if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>( 58 python::LLDBSWIGPython_CastPyObjectToSBError(p.get()))) 59 return m_interpreter.GetStatusFromSBError(*sb_error); 60 error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status."); 61 62 return {}; 63 } 64 65 template <> 66 Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>( 67 python::PythonObject &p, Status &error) { 68 if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>( 69 python::LLDBSWIGPython_CastPyObjectToSBEvent(p.get()))) 70 return m_interpreter.GetOpaqueTypeFromSBEvent(*sb_event); 71 error.SetErrorString("Couldn't cast lldb::SBEvent to lldb_private::Event."); 72 73 return nullptr; 74 } 75 76 template <> 77 Stream *ScriptedPythonInterface::ExtractValueFromPythonObject<Stream *>( 78 python::PythonObject &p, Status &error) { 79 if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>( 80 python::LLDBSWIGPython_CastPyObjectToSBStream(p.get()))) 81 return m_interpreter.GetOpaqueTypeFromSBStream(*sb_stream); 82 error.SetErrorString("Couldn't cast lldb::SBStream to lldb_private::Stream."); 83 84 return nullptr; 85 } 86 87 template <> 88 lldb::DataExtractorSP 89 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>( 90 python::PythonObject &p, Status &error) { 91 lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>( 92 python::LLDBSWIGPython_CastPyObjectToSBData(p.get())); 93 94 if (!sb_data) { 95 error.SetErrorString( 96 "Couldn't cast lldb::SBData to lldb::DataExtractorSP."); 97 return nullptr; 98 } 99 100 return m_interpreter.GetDataExtractorFromSBData(*sb_data); 101 } 102 103 template <> 104 lldb::BreakpointSP 105 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>( 106 python::PythonObject &p, Status &error) { 107 lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>( 108 python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get())); 109 110 if (!sb_breakpoint) { 111 error.SetErrorString( 112 "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP."); 113 return nullptr; 114 } 115 116 return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint); 117 } 118 119 template <> 120 lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject< 121 lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) { 122 lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>( 123 python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get())); 124 125 if (!sb_attach_info) { 126 error.SetErrorString( 127 "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP."); 128 return nullptr; 129 } 130 131 return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info); 132 } 133 134 template <> 135 lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject< 136 lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) { 137 lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>( 138 python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get())); 139 140 if (!sb_launch_info) { 141 error.SetErrorString( 142 "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP."); 143 return nullptr; 144 } 145 146 return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info); 147 } 148 149 template <> 150 std::optional<MemoryRegionInfo> 151 ScriptedPythonInterface::ExtractValueFromPythonObject< 152 std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) { 153 154 lldb::SBMemoryRegionInfo *sb_mem_reg_info = 155 reinterpret_cast<lldb::SBMemoryRegionInfo *>( 156 python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get())); 157 158 if (!sb_mem_reg_info) { 159 error.SetErrorString( 160 "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP."); 161 return {}; 162 } 163 164 return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info); 165 } 166 167 #endif 168