1 //===-- ScriptedStopHookPythonInterface.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/Core/PluginManager.h"
10 #include "lldb/Host/Config.h"
11 #include "lldb/Target/ExecutionContext.h"
12 #include "lldb/Utility/Log.h"
13 #include "lldb/lldb-enumerations.h"
14
15 #if LLDB_ENABLE_PYTHON
16
17 // clang-format off
18 // LLDB Python header must be included first
19 #include "../lldb-python.h"
20 //clang-format on
21
22 #include "../SWIGPythonBridge.h"
23 #include "../ScriptInterpreterPythonImpl.h"
24 #include "ScriptedStopHookPythonInterface.h"
25
26 using namespace lldb;
27 using namespace lldb_private;
28 using namespace lldb_private::python;
29
ScriptedStopHookPythonInterface(ScriptInterpreterPythonImpl & interpreter)30 ScriptedStopHookPythonInterface::ScriptedStopHookPythonInterface(
31 ScriptInterpreterPythonImpl &interpreter)
32 : ScriptedStopHookInterface(), ScriptedPythonInterface(interpreter) {}
33
34 llvm::Expected<StructuredData::GenericSP>
CreatePluginObject(llvm::StringRef class_name,lldb::TargetSP target_sp,const StructuredDataImpl & args_sp)35 ScriptedStopHookPythonInterface::CreatePluginObject(llvm::StringRef class_name,
36 lldb::TargetSP target_sp,
37 const StructuredDataImpl &args_sp) {
38 return ScriptedPythonInterface::CreatePluginObject(class_name, nullptr,
39 target_sp, args_sp);
40 }
41
42 llvm::Expected<bool>
HandleStop(ExecutionContext & exe_ctx,lldb::StreamSP & output_sp)43 ScriptedStopHookPythonInterface::HandleStop(ExecutionContext &exe_ctx,
44 lldb::StreamSP& output_sp) {
45 ExecutionContextRefSP exe_ctx_ref_sp =
46 std::make_shared<ExecutionContextRef>(exe_ctx);
47 Status error;
48 StructuredData::ObjectSP obj = Dispatch("handle_stop", error, exe_ctx_ref_sp, output_sp);
49
50 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
51 error)) {
52 if (!obj)
53 return true;
54 return error.ToError();
55 }
56
57 return obj->GetBooleanValue();
58 }
59
60
Initialize()61 void ScriptedStopHookPythonInterface::Initialize() {
62 const std::vector<llvm::StringRef> ci_usages = {
63 "target stop-hook add -P <script-name> [-k key -v value ...]"};
64 const std::vector<llvm::StringRef> api_usages = {};
65 PluginManager::RegisterPlugin(
66 GetPluginNameStatic(),
67 llvm::StringRef("Perform actions whenever the process stops, before control is returned to the user."),
68 CreateInstance, eScriptLanguagePython, {ci_usages, api_usages});
69 }
70
Terminate()71 void ScriptedStopHookPythonInterface::Terminate() {
72 PluginManager::UnregisterPlugin(CreateInstance);
73 }
74
75 #endif
76