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 
ScriptedPythonInterface(ScriptInterpreterPythonImpl & interpreter)25 ScriptedPythonInterface::ScriptedPythonInterface(
26     ScriptInterpreterPythonImpl &interpreter)
27     : ScriptedInterface(), m_interpreter(interpreter) {}
28 
29 Status
GetStatusFromMethod(llvm::StringRef method_name)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::ArraySP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)39 ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>(
40     python::PythonObject &p, Status &error) {
41   python::PythonList result_list(python::PyRefType::Borrowed, p.get());
42   return result_list.CreateStructuredArray();
43 }
44 
45 template <>
46 StructuredData::DictionarySP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)47 ScriptedPythonInterface::ExtractValueFromPythonObject<
48     StructuredData::DictionarySP>(python::PythonObject &p, Status &error) {
49   python::PythonDictionary result_dict(python::PyRefType::Borrowed, p.get());
50   return result_dict.CreateStructuredDictionary();
51 }
52 
53 template <>
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)54 Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>(
55     python::PythonObject &p, Status &error) {
56   if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(
57           LLDBSWIGPython_CastPyObjectToSBError(p.get())))
58     error = m_interpreter.GetStatusFromSBError(*sb_error);
59   else
60     error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status.");
61 
62   return error;
63 }
64 
65 template <>
66 lldb::DataExtractorSP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)67 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
68     python::PythonObject &p, Status &error) {
69   lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
70       LLDBSWIGPython_CastPyObjectToSBData(p.get()));
71 
72   if (!sb_data) {
73     error.SetErrorString(
74         "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
75     return nullptr;
76   }
77 
78   return m_interpreter.GetDataExtractorFromSBData(*sb_data);
79 }
80 
81 template <>
82 llvm::Optional<MemoryRegionInfo>
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)83 ScriptedPythonInterface::ExtractValueFromPythonObject<
84     llvm::Optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
85 
86   lldb::SBMemoryRegionInfo *sb_mem_reg_info =
87       reinterpret_cast<lldb::SBMemoryRegionInfo *>(
88           LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));
89 
90   if (!sb_mem_reg_info) {
91     error.SetErrorString(
92         "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP.");
93     return {};
94   }
95 
96   return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
97 }
98 
99 #endif
100