LLDB mainline
ScriptedHookPythonInterface.cpp
Go to the documentation of this file.
1//===-- ScriptedHookPythonInterface.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
11#include "lldb/Utility/Log.h"
13
14#include "../SWIGPythonBridge.h"
16#include "../lldb-python.h"
18
19using namespace lldb;
20using namespace lldb_private;
21using namespace lldb_private::python;
22
26
30 // Qualify through ScriptedPythonInterface to resolve the diamond
31 // inheritance (both ScriptedHookInterface and ScriptedPythonInterface
32 // inherit ScriptedInterface which owns m_object_instance_sp).
34 if (!obj_sp)
35 return methods;
36
40
41 PythonObject implementor(PyRefType::Borrowed, (PyObject *)obj_sp->GetValue());
42 if (!implementor.IsValid())
43 return methods;
44
45 methods.handle_module_loaded =
46 implementor.HasAttribute("handle_module_loaded");
48 implementor.HasAttribute("handle_module_unloaded");
49 methods.handle_stop = implementor.HasAttribute("handle_stop");
50 return methods;
51}
52
53llvm::Expected<StructuredData::GenericSP>
55 llvm::StringRef class_name, lldb::TargetSP target_sp,
56 const StructuredDataImpl &args_sp) {
57 return ScriptedPythonInterface::CreatePluginObject(class_name, nullptr,
58 target_sp, args_sp);
59}
60
62 lldb::StreamSP &output_sp) {
64 Dispatch("handle_module_loaded", error, output_sp);
65}
66
68 lldb::StreamSP &output_sp) {
70 Dispatch("handle_module_unloaded", error, output_sp);
71}
72
73llvm::Expected<bool>
75 lldb::StreamSP &output_sp) {
76 ExecutionContextRefSP exe_ctx_ref_sp =
77 std::make_shared<ExecutionContextRef>(exe_ctx);
80 Dispatch("handle_stop", error, exe_ctx_ref_sp, output_sp);
81
82 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
83 error)) {
84 if (!obj)
85 return true;
86 return error.ToError();
87 }
88
89 return obj->GetBooleanValue();
90}
91
93 const std::vector<llvm::StringRef> ci_usages = {
94 "target hook add -P <script-name> [-k key -v value ...]"};
95 const std::vector<llvm::StringRef> api_usages = {};
98 llvm::StringRef("Perform actions on target lifecycle events (module "
99 "load/unload, process stop)."),
100 CreateInstance, eScriptLanguagePython, {ci_usages, api_usages});
101}
102
static llvm::raw_ostream & error(Stream &strm)
ScriptInterpreterPythonImpl::Locker Locker
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
SupportedHookMethods GetSupportedMethods() override
Check which of the three hook methods the Python class implements.
void HandleModuleUnloaded(lldb::StreamSP &output_sp) override
Called when modules are unloaded from the target. Optional.
void HandleModuleLoaded(lldb::StreamSP &output_sp) override
Called when modules are loaded into the target.
llvm::Expected< bool > HandleStop(ExecutionContext &exe_ctx, lldb::StreamSP &output_sp) override
Called when the process stops.
ScriptedHookPythonInterface(ScriptInterpreterPythonImpl &interpreter)
llvm::Expected< StructuredData::GenericSP > CreatePluginObject(llvm::StringRef class_name, lldb::TargetSP target_sp, const StructuredDataImpl &args_sp) override
static bool CheckStructuredDataObject(llvm::StringRef caller, T obj, Status &error)
static bool CreateInstance(lldb::ScriptLanguage language, ScriptedInterfaceUsages usages)
StructuredData::GenericSP m_object_instance_sp
llvm::Expected< StructuredData::GenericSP > CreatePluginObject(llvm::StringRef class_name, StructuredData::Generic *script_obj, Args... args)
ScriptInterpreterPythonImpl & m_interpreter
ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter)
T Dispatch(llvm::StringRef method_name, Status &error, Args &&...args)
An error handling class.
Definition Status.h:118
std::shared_ptr< Object > ObjectSP
bool HasAttribute(llvm::StringRef attribute) const
A class that represents a running process on the host machine.
@ eScriptLanguagePython
std::shared_ptr< lldb_private::Stream > StreamSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::ExecutionContextRef > ExecutionContextRefSP
Describes which hook callback methods the Python class implements.