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 "SWIGPythonBridge.h" 19 #include "ScriptInterpreterPythonImpl.h" 20 #include "ScriptedPythonInterface.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 ScriptedPythonInterface::ScriptedPythonInterface( 26 ScriptInterpreterPythonImpl &interpreter) 27 : ScriptedInterface(), m_interpreter(interpreter) {} 28 29 Status 30 ScriptedPythonInterface::GetStatusFromMethod(llvm::StringRef method_name) { 31 Status error; 32 Dispatch<Status>(method_name, error); 33 34 return error; 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 LLDBSWIGPython_CastPyObjectToSBError(p.get()))) 50 error = m_interpreter.GetStatusFromSBError(*sb_error); 51 else 52 error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status."); 53 54 return error; 55 } 56 57 template <> 58 lldb::DataExtractorSP 59 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>( 60 python::PythonObject &p, Status &error) { 61 lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>( 62 LLDBSWIGPython_CastPyObjectToSBData(p.get())); 63 64 if (!sb_data) { 65 error.SetErrorString( 66 "Couldn't cast lldb::SBData to lldb::DataExtractorSP."); 67 return nullptr; 68 } 69 70 return m_interpreter.GetDataExtractorFromSBData(*sb_data); 71 } 72 73 template <> 74 llvm::Optional<MemoryRegionInfo> 75 ScriptedPythonInterface::ExtractValueFromPythonObject< 76 llvm::Optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) { 77 78 lldb::SBMemoryRegionInfo *sb_mem_reg_info = 79 reinterpret_cast<lldb::SBMemoryRegionInfo *>( 80 LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get())); 81 82 if (!sb_mem_reg_info) { 83 error.SetErrorString( 84 "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP."); 85 return {}; 86 } 87 88 return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info); 89 } 90 91 #endif 92