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
9#include "../lldb-python.h"
10
13#include "lldb/Target/Target.h"
14#include "lldb/Utility/Log.h"
16
17#include "../SWIGPythonBridge.h"
20
21using namespace lldb;
22using namespace lldb_private;
23using namespace lldb_private::python;
24
28
32 // Qualify through ScriptedPythonInterface to resolve the diamond
33 // inheritance (both ScriptedHookInterface and ScriptedPythonInterface
34 // inherit ScriptedInterface which owns m_object_instance_sp).
36 if (!obj_sp)
37 return methods;
38
42
43 PythonObject implementor(PyRefType::Borrowed, (PyObject *)obj_sp->GetValue());
44 if (!implementor.IsValid())
45 return methods;
46
47 methods.handle_module_loaded =
48 implementor.HasAttribute("handle_module_loaded");
50 implementor.HasAttribute("handle_module_unloaded");
51 methods.handle_stop = implementor.HasAttribute("handle_stop");
52 return methods;
53}
54
55llvm::Expected<StructuredData::GenericSP>
57 const ScriptedMetadata &scripted_metadata, lldb::TargetSP target_sp) {
58 StructuredDataImpl args_sp(scripted_metadata.GetArgsSP());
59 return ScriptedPythonInterface::CreatePluginObject(scripted_metadata, nullptr,
60 target_sp, args_sp);
61}
62
64 lldb::StreamSP &output_sp) {
66 Dispatch("handle_module_loaded", error, output_sp);
67}
68
70 lldb::StreamSP &output_sp) {
72 Dispatch("handle_module_unloaded", error, output_sp);
73}
74
75llvm::Expected<bool>
77 lldb::StreamSP &output_sp) {
78 ExecutionContextRefSP exe_ctx_ref_sp =
79 std::make_shared<ExecutionContextRef>(exe_ctx);
82 Dispatch("handle_stop", error, exe_ctx_ref_sp, output_sp);
83
84 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
85 error)) {
86 if (!obj)
87 return true;
88 return error.ToError();
89 }
90
91 return obj->GetBooleanValue();
92}
93
95 const std::vector<llvm::StringRef> ci_usages = {
96 "target hook add -P <script-name> [-k key -v value ...]"};
97 const std::vector<llvm::StringRef> api_usages = {};
100 llvm::StringRef("Perform actions on target lifecycle events (module "
101 "load/unload, process stop)."),
102 CreateInstance, eScriptLanguagePython, {ci_usages, api_usages});
103}
104
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< StructuredData::GenericSP > CreatePluginObject(const ScriptedMetadata &scripted_metadata, lldb::TargetSP target_sp) override
llvm::Expected< bool > HandleStop(ExecutionContext &exe_ctx, lldb::StreamSP &output_sp) override
Called when the process stops.
ScriptedHookPythonInterface(ScriptInterpreterPythonImpl &interpreter)
static bool CheckStructuredDataObject(llvm::StringRef caller, T obj, Status &error)
static bool CreateInstance(lldb::ScriptLanguage language, ScriptedInterfaceUsages usages)
StructuredData::GenericSP m_object_instance_sp
StructuredData::DictionarySP GetArgsSP() const
ScriptInterpreterPythonImpl & m_interpreter
ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter)
T Dispatch(llvm::StringRef method_name, Status &error, Args &&...args)
llvm::Expected< StructuredData::GenericSP > CreatePluginObject(const ScriptedMetadata &scripted_metadata, StructuredData::Generic *script_obj, 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.