-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathdebug_api.rs
More file actions
108 lines (93 loc) · 3.18 KB
/
debug_api.rs
File metadata and controls
108 lines (93 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::sync::Arc;
use multiversx_chain_vm::{
executor::BreakpointValue,
tx_mock::{TxContext, TxContextRef, TxContextStack, TxPanic},
vm_hooks::{DebugApiVMHooksHandler, VMHooksDispatcher},
};
use multiversx_sc::{chain_core::types::ReturnCode, err_msg};
use crate::debug_executor::{StaticVarData, StaticVarStack, VMHooksDebugger};
use super::{DebugHandle, VMHooksApi, VMHooksApiBackend};
#[derive(Clone)]
pub struct DebugApiBackend;
impl VMHooksApiBackend for DebugApiBackend {
type HandleType = DebugHandle;
fn with_vm_hooks<R, F>(f: F) -> R
where
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
let top_context = TxContextStack::static_peek();
let wrapper = DebugApiVMHooksHandler::new(top_context);
let dispatcher = VMHooksDispatcher::new(Box::new(wrapper));
f(&dispatcher)
}
fn with_vm_hooks_ctx_1<R, F>(handle: Self::HandleType, f: F) -> R
where
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
let wrapper = DebugApiVMHooksHandler::new(handle.context);
let dispatcher = VMHooksDispatcher::new(Box::new(wrapper));
f(&dispatcher)
}
fn with_vm_hooks_ctx_2<R, F>(handle1: Self::HandleType, handle2: Self::HandleType, f: F) -> R
where
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
assert_handles_on_same_context(&handle1, &handle2);
Self::with_vm_hooks_ctx_1(handle1, f)
}
fn with_vm_hooks_ctx_3<R, F>(
handle1: Self::HandleType,
handle2: Self::HandleType,
handle3: Self::HandleType,
f: F,
) -> R
where
F: FnOnce(&dyn VMHooksDebugger) -> R,
{
assert_handles_on_same_context(&handle1, &handle2);
assert_handles_on_same_context(&handle1, &handle3);
Self::with_vm_hooks_ctx_1(handle1, f)
}
fn assert_live_handle(handle: &Self::HandleType) {
if !handle.is_on_current_context() {
debugger_panic(
ReturnCode::DebugApiError,
err_msg::DEBUG_API_ERR_HANDLE_STALE,
);
}
}
fn with_static_data<R, F>(f: F) -> R
where
F: FnOnce(&StaticVarData) -> R,
{
let top_context = StaticVarStack::static_peek();
f(&top_context)
}
}
pub type DebugApi = VMHooksApi<DebugApiBackend>;
impl DebugApi {
pub fn dummy() {
let tx_context = TxContext::dummy();
let tx_context_arc = Arc::new(tx_context);
// TODO: WARNING: this does not clean up after itself, must fix!!!
TxContextStack::static_push(tx_context_arc);
StaticVarStack::static_push();
}
}
impl std::fmt::Debug for DebugApi {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("DebugApi")
}
}
fn debugger_panic(status: ReturnCode, message: &str) {
TxContextRef::new_from_static().replace_tx_result_with_error(TxPanic::new(status, message));
std::panic::panic_any(BreakpointValue::SignalError);
}
fn assert_handles_on_same_context(handle1: &DebugHandle, handle2: &DebugHandle) {
if !handle1.is_on_same_context(handle2) {
debugger_panic(
ReturnCode::DebugApiError,
err_msg::DEBUG_API_ERR_HANDLE_CONTEXT_MISMATCH,
);
}
}