1 //===-- ScriptedThreadPlanPythonInterface.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 "ScriptedThreadPlanPythonInterface.h"
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 using namespace lldb_private::python;
25 
26 ScriptedThreadPlanPythonInterface::ScriptedThreadPlanPythonInterface(
27     ScriptInterpreterPythonImpl &interpreter)
28     : ScriptedThreadPlanInterface(), ScriptedPythonInterface(interpreter) {}
29 
30 llvm::Expected<StructuredData::GenericSP>
31 ScriptedThreadPlanPythonInterface::CreatePluginObject(
32     const llvm::StringRef class_name, lldb::ThreadPlanSP thread_plan_sp,
33     const StructuredDataImpl &args_sp) {
34   return ScriptedPythonInterface::CreatePluginObject(class_name, nullptr,
35                                                      thread_plan_sp, args_sp);
36 }
37 
38 llvm::Expected<bool>
39 ScriptedThreadPlanPythonInterface::ExplainsStop(Event *event) {
40   Status error;
41   StructuredData::ObjectSP obj = Dispatch("explains_stop", error, event);
42 
43   if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
44                                                     error)) {
45     if (!obj)
46       return false;
47     return error.ToError();
48   }
49 
50   return obj->GetBooleanValue();
51 }
52 
53 llvm::Expected<bool>
54 ScriptedThreadPlanPythonInterface::ShouldStop(Event *event) {
55   Status error;
56   StructuredData::ObjectSP obj = Dispatch("should_stop", error, event);
57 
58   if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
59                                                     error)) {
60     if (!obj)
61       return false;
62     return error.ToError();
63   }
64 
65   return obj->GetBooleanValue();
66 }
67 
68 llvm::Expected<bool> ScriptedThreadPlanPythonInterface::IsStale() {
69   Status error;
70   StructuredData::ObjectSP obj = Dispatch("is_stale", error);
71 
72   if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
73                                                     error)) {
74     if (!obj)
75       return false;
76     return error.ToError();
77   }
78 
79   return obj->GetBooleanValue();
80 }
81 
82 lldb::StateType ScriptedThreadPlanPythonInterface::GetRunState() {
83   Status error;
84   StructuredData::ObjectSP obj = Dispatch("should_step", error);
85 
86   if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
87                                                     error))
88     return lldb::eStateStepping;
89 
90   return static_cast<lldb::StateType>(obj->GetUnsignedIntegerValue(
91       static_cast<uint32_t>(lldb::eStateStepping)));
92 }
93 
94 llvm::Expected<bool>
95 ScriptedThreadPlanPythonInterface::GetStopDescription(lldb_private::Stream *s) {
96   Status error;
97   Dispatch("stop_description", error, s);
98 
99   if (error.Fail())
100     return error.ToError();
101 
102   return true;
103 }
104 
105 #endif
106