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
63llvm::Error
65 return DispatchToOptional("handle_module_loaded", output_sp).takeError();
66}
67
68llvm::Error
70 return DispatchToOptional("handle_module_unloaded", output_sp).takeError();
71}
72
73llvm::Expected<bool>
75 lldb::StreamSP &output_sp) {
76 ExecutionContextRefSP exe_ctx_ref_sp =
77 std::make_shared<ExecutionContextRef>(exe_ctx);
78 llvm::Expected<StructuredData::ObjectSP> obj_or_err =
79 Dispatch("handle_stop", exe_ctx_ref_sp, output_sp);
80 if (!obj_or_err)
81 return obj_or_err.takeError();
82
83 // `handle_stop` is required, so a null object here means the hook returned
84 // None: it expressed no preference, so stay stopped.
85 StructuredData::ObjectSP obj = *obj_or_err;
86 if (!obj || !obj->IsValid())
87 return true;
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)."),
101 {ci_usages, api_usages});
102}
103
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.
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)
llvm::Error HandleModuleUnloaded(lldb::StreamSP &output_sp) override
Called when modules are unloaded from the target. Optional.
llvm::Error HandleModuleLoaded(lldb::StreamSP &output_sp) override
Called when modules are loaded into the target.
static bool CreateInstance(lldb::ScriptLanguage language, ScriptedInterfaceUsages usages)
StructuredData::GenericSP m_object_instance_sp
StructuredData::DictionarySP GetArgsSP() const
ScriptInterpreterPythonImpl & m_interpreter
ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter)
llvm::Expected< T > Dispatch(llvm::StringRef method_name, Args &&...args)
Call method_name on the scripted object.
llvm::Expected< std::optional< T > > DispatchToOptional(llvm::StringRef method_name, Args &&...args)
Call an optional method_name, reporting "the script doesn't implementit" as std::nullopt rather than ...
llvm::Expected< StructuredData::GenericSP > CreatePluginObject(const ScriptedMetadata &scripted_metadata, StructuredData::Generic *script_obj, Args... args)
std::shared_ptr< Object > ObjectSP
bool HasAttribute(llvm::StringRef attribute) const
A class that represents a running process on the host machine.
@ eScriptLanguagePython
@ eScriptedExtensionScriptedHook
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.