xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedPythonInterface.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
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 #include <optional>
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
ScriptedPythonInterface(ScriptInterpreterPythonImpl & interpreter)26 ScriptedPythonInterface::ScriptedPythonInterface(
27     ScriptInterpreterPythonImpl &interpreter)
28     : ScriptedInterface(), m_interpreter(interpreter) {}
29 
30 template <>
31 StructuredData::ArraySP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)32 ScriptedPythonInterface::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 
38 template <>
39 StructuredData::DictionarySP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)40 ScriptedPythonInterface::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 
46 template <>
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)47 Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>(
48     python::PythonObject &p, Status &error) {
49   if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(
50           LLDBSWIGPython_CastPyObjectToSBError(p.get())))
51     return m_interpreter.GetStatusFromSBError(*sb_error);
52   else
53     error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status.");
54 
55   return {};
56 }
57 
58 template <>
59 lldb::DataExtractorSP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)60 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
61     python::PythonObject &p, Status &error) {
62   lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
63       LLDBSWIGPython_CastPyObjectToSBData(p.get()));
64 
65   if (!sb_data) {
66     error.SetErrorString(
67         "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
68     return nullptr;
69   }
70 
71   return m_interpreter.GetDataExtractorFromSBData(*sb_data);
72 }
73 
74 template <>
75 std::optional<MemoryRegionInfo>
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)76 ScriptedPythonInterface::ExtractValueFromPythonObject<
77     std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
78 
79   lldb::SBMemoryRegionInfo *sb_mem_reg_info =
80       reinterpret_cast<lldb::SBMemoryRegionInfo *>(
81           LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));
82 
83   if (!sb_mem_reg_info) {
84     error.SetErrorString(
85         "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP.");
86     return {};
87   }
88 
89   return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
90 }
91 
92 #endif
93