Skip to content

Commit 3387de3

Browse files
committed
[lldb/Interpreter] Create ScriptedStopHook{,Python}Interface (NFC)
This patch introduces a new scripted interface for ScriptedStopHooks. This will be used in a follow-up patch to call into the script methods. Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 0347c11 commit 3387de3

10 files changed

+177
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- ScriptedStopHookInterface.h -----------------------------*- C++ -*-===//
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+
#ifndef LLDB_INTERPRETER_INTERFACES_SCRIPTEDSTOPHOOKINTERFACE_H
10+
#define LLDB_INTERPRETER_INTERFACES_SCRIPTEDSTOPHOOKINTERFACE_H
11+
12+
#include "lldb/lldb-private.h"
13+
14+
#include "ScriptedInterface.h"
15+
16+
namespace lldb_private {
17+
class ScriptedStopHookInterface : public ScriptedInterface {
18+
public:
19+
virtual llvm::Expected<StructuredData::GenericSP>
20+
CreatePluginObject(llvm::StringRef class_name, lldb::TargetSP target_sp,
21+
const StructuredDataImpl &args_sp) = 0;
22+
23+
/// "handle_stop" will return a bool with the meaning "should_stop"...
24+
/// If nothing is returned, we'll assume we are going to stop.
25+
/// Also any errors should return true, since we should stop on error.
26+
virtual llvm::Expected<bool> HandleStop(ExecutionContext &exe_ctx,
27+
lldb::StreamSP output_sp) {
28+
return true;
29+
}
30+
};
31+
} // namespace lldb_private
32+
33+
#endif // LLDB_INTERPRETER_INTERFACES_SCRIPTEDSTOPHOOKINTERFACE_H

lldb/include/lldb/Interpreter/ScriptInterpreter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,10 @@ class ScriptInterpreter : public PluginInterface {
561561
return {};
562562
}
563563

564+
virtual lldb::ScriptedStopHookInterfaceSP CreateScriptedStopHookInterface() {
565+
return {};
566+
}
567+
564568
virtual StructuredData::ObjectSP
565569
CreateStructuredDataFromScriptObject(ScriptObject obj) {
566570
return {};

lldb/include/lldb/lldb-forward.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ class ScriptInterpreterLocker;
190190
class ScriptedMetadata;
191191
class ScriptedPlatformInterface;
192192
class ScriptedProcessInterface;
193+
class ScriptedStopHookInterface;
193194
class ScriptedThreadInterface;
194195
class ScriptedThreadPlanInterface;
195196
class ScriptedSyntheticChildren;
@@ -408,6 +409,8 @@ typedef std::unique_ptr<lldb_private::ScriptedPlatformInterface>
408409
ScriptedPlatformInterfaceUP;
409410
typedef std::unique_ptr<lldb_private::ScriptedProcessInterface>
410411
ScriptedProcessInterfaceUP;
412+
typedef std::shared_ptr<lldb_private::ScriptedStopHookInterface>
413+
ScriptedStopHookInterfaceSP;
411414
typedef std::shared_ptr<lldb_private::ScriptedThreadInterface>
412415
ScriptedThreadInterfaceSP;
413416
typedef std::shared_ptr<lldb_private::ScriptedThreadPlanInterface>

lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ add_lldb_library(lldbPluginScriptInterpreterPythonInterfaces PLUGIN
2525
ScriptedPlatformPythonInterface.cpp
2626
ScriptedProcessPythonInterface.cpp
2727
ScriptedPythonInterface.cpp
28+
ScriptedStopHookPythonInterface.cpp
2829
ScriptedThreadPlanPythonInterface.cpp
2930
ScriptedThreadPythonInterface.cpp
3031

lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptInterpreterPythonInterfaces.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ void ScriptInterpreterPythonInterfaces::Initialize() {
2828
OperatingSystemPythonInterface::Initialize();
2929
ScriptedPlatformPythonInterface::Initialize();
3030
ScriptedProcessPythonInterface::Initialize();
31+
ScriptedStopHookPythonInterface::Initialize();
3132
ScriptedThreadPlanPythonInterface::Initialize();
3233
}
3334

3435
void ScriptInterpreterPythonInterfaces::Terminate() {
3536
OperatingSystemPythonInterface::Terminate();
3637
ScriptedPlatformPythonInterface::Terminate();
3738
ScriptedProcessPythonInterface::Terminate();
39+
ScriptedStopHookPythonInterface::Terminate();
3840
ScriptedThreadPlanPythonInterface::Terminate();
3941
}
4042

lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptInterpreterPythonInterfaces.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "OperatingSystemPythonInterface.h"
1919
#include "ScriptedPlatformPythonInterface.h"
2020
#include "ScriptedProcessPythonInterface.h"
21+
#include "ScriptedStopHookPythonInterface.h"
2122
#include "ScriptedThreadPlanPythonInterface.h"
2223

2324
namespace lldb_private {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
30+
ScriptedStopHookPythonInterface::ScriptedStopHookPythonInterface(
31+
ScriptInterpreterPythonImpl &interpreter)
32+
: ScriptedStopHookInterface(), ScriptedPythonInterface(interpreter) {}
33+
34+
llvm::Expected<StructuredData::GenericSP>
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>
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+
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+
71+
void ScriptedStopHookPythonInterface::Terminate() {
72+
PluginManager::UnregisterPlugin(CreateInstance);
73+
}
74+
75+
#endif
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===-- ScriptedStopHookPythonInterface.h -----------------------*- C++ -*-===//
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+
#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDSTOPHOOKPYTHONINTERFACE_H
10+
#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDSTOPHOOKPYTHONINTERFACE_H
11+
12+
#include "lldb/Host/Config.h"
13+
#include "lldb/Interpreter/Interfaces/ScriptedStopHookInterface.h"
14+
15+
#if LLDB_ENABLE_PYTHON
16+
17+
#include "ScriptedPythonInterface.h"
18+
19+
namespace lldb_private {
20+
class ScriptedStopHookPythonInterface : public ScriptedStopHookInterface,
21+
public ScriptedPythonInterface,
22+
public PluginInterface {
23+
public:
24+
ScriptedStopHookPythonInterface(ScriptInterpreterPythonImpl &interpreter);
25+
26+
llvm::Expected<StructuredData::GenericSP>
27+
CreatePluginObject(llvm::StringRef class_name, lldb::TargetSP target_sp,
28+
const StructuredDataImpl &args_sp) override;
29+
30+
llvm::SmallVector<AbstractMethodRequirement>
31+
GetAbstractMethodRequirements() const override {
32+
return llvm::SmallVector<AbstractMethodRequirement>({{"handle_stop", 2}});
33+
}
34+
35+
llvm::Expected<bool> HandleStop(ExecutionContext &exe_ctx,
36+
lldb::StreamSP output_sp) override;
37+
38+
static void Initialize();
39+
40+
static void Terminate();
41+
42+
static llvm::StringRef GetPluginNameStatic() {
43+
return "ScriptedStopHookPythonInterface";
44+
}
45+
46+
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
47+
};
48+
} // namespace lldb_private
49+
50+
#endif // LLDB_ENABLE_PYTHON
51+
#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_INTERFACES_SCRIPTEDSTOPHOOKPYTHONINTERFACE_H

lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,11 @@ ScriptInterpreterPythonImpl::CreateScriptedProcessInterface() {
15591559
return std::make_unique<ScriptedProcessPythonInterface>(*this);
15601560
}
15611561

1562+
ScriptedStopHookInterfaceSP
1563+
ScriptInterpreterPythonImpl::CreateScriptedStopHookInterface() {
1564+
return std::make_shared<ScriptedStopHookPythonInterface>(*this);
1565+
}
1566+
15621567
ScriptedThreadInterfaceSP
15631568
ScriptInterpreterPythonImpl::CreateScriptedThreadInterface() {
15641569
return std::make_shared<ScriptedThreadPythonInterface>(*this);

lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class ScriptInterpreterPythonImpl : public ScriptInterpreterPython {
112112

113113
lldb::ScriptedProcessInterfaceUP CreateScriptedProcessInterface() override;
114114

115+
lldb::ScriptedStopHookInterfaceSP CreateScriptedStopHookInterface() override;
116+
115117
lldb::ScriptedThreadInterfaceSP CreateScriptedThreadInterface() override;
116118

117119
lldb::ScriptedThreadPlanInterfaceSP

0 commit comments

Comments
 (0)