debug_worker/gdb/targets/
base.rs

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use super::TargetArch;
use super::VmTarget;
use crate::gdb::targets::ToTargetResult;
use futures::executor::block_on;
use gdbstub::common::Signal;
use gdbstub::common::Tid;
use gdbstub::target::TargetResult;
use gdbstub::target::ext::base::multithread::MultiThreadBase;
use gdbstub::target::ext::base::multithread::MultiThreadResume;
use gdbstub::target::ext::base::multithread::MultiThreadResumeOps;
use gdbstub::target::ext::base::multithread::MultiThreadSingleStep;
use gdbstub::target::ext::base::multithread::MultiThreadSingleStepOps;
use gdbstub::target::ext::base::single_register_access::SingleRegisterAccess;
use mesh::rpc::RpcSend;
use vmm_core_defs::debug_rpc::DebugRequest;
use vmm_core_defs::debug_rpc::DebugState;

impl<T: TargetArch> MultiThreadBase for VmTarget<'_, T> {
    fn read_registers(&mut self, regs: &mut T::Registers, tid: Tid) -> TargetResult<(), Self> {
        let vp_index = self.0.tid_to_vp(tid).fatal()?;

        let state = block_on(
            self.0
                .req_chan
                .call_failable(DebugRequest::GetVpState, vp_index),
        )
        .nonfatal()?;

        T::registers(&state, regs)?;
        Ok(())
    }

    fn write_registers(&mut self, regs: &T::Registers, tid: Tid) -> TargetResult<(), Self> {
        let vp_index = self.0.tid_to_vp(tid).fatal()?;

        let mut state = block_on(
            self.0
                .req_chan
                .call_failable(DebugRequest::GetVpState, vp_index),
        )
        .nonfatal()?;

        T::update_registers(&mut state, regs)?;

        block_on(
            self.0
                .req_chan
                .call_failable(DebugRequest::SetVpState, (vp_index, state)),
        )
        .nonfatal()?;

        Ok(())
    }

    fn read_addrs(
        &mut self,
        start_addr: T::Usize,
        data: &mut [u8],
        tid: Tid,
    ) -> TargetResult<(), Self> {
        self.0
            .read_guest_virtual_memory(self.0.tid_to_vp(tid).fatal()?, start_addr.into(), data)
            .nonfatal()?;
        Ok(())
    }

    fn write_addrs(
        &mut self,
        start_addr: T::Usize,
        data: &[u8],
        tid: Tid,
    ) -> TargetResult<(), Self> {
        self.0
            .write_guest_virtual_memory(self.0.tid_to_vp(tid).fatal()?, start_addr.into(), data)
            .nonfatal()?;
        Ok(())
    }

    fn list_active_threads(
        &mut self,
        thread_is_active: &mut dyn FnMut(Tid),
    ) -> Result<(), Self::Error> {
        for i in 0..self.0.vps.len() as u32 {
            thread_is_active(self.0.vp_to_tid(i));
        }
        Ok(())
    }

    #[inline(always)]
    fn support_resume(&mut self) -> Option<MultiThreadResumeOps<'_, Self>> {
        Some(self)
    }

    #[inline(always)]
    fn support_single_register_access(
        &mut self,
    ) -> Option<
        gdbstub::target::ext::base::single_register_access::SingleRegisterAccessOps<'_, Tid, Self>,
    > {
        Some(self)
    }
}

impl<T: TargetArch> SingleRegisterAccess<Tid> for VmTarget<'_, T> {
    fn read_register(
        &mut self,
        tid: Tid,
        reg_id: T::RegId,
        buf: &mut [u8],
    ) -> TargetResult<usize, Self> {
        let vp_index = self.0.tid_to_vp(tid).fatal()?;

        let state = block_on(
            self.0
                .req_chan
                .call_failable(DebugRequest::GetVpState, vp_index),
        )
        .nonfatal()?;

        Ok(T::register(&state, reg_id, buf)?)
    }

    fn write_register(&mut self, tid: Tid, reg_id: T::RegId, val: &[u8]) -> TargetResult<(), Self> {
        let vp_index = self.0.tid_to_vp(tid).fatal()?;

        let mut state = block_on(
            self.0
                .req_chan
                .call_failable(DebugRequest::GetVpState, vp_index),
        )
        .nonfatal()?;

        T::update_register(&mut state, reg_id, val)?;

        block_on(
            self.0
                .req_chan
                .call_failable(DebugRequest::SetVpState, (vp_index, state)),
        )
        .nonfatal()?;

        Ok(())
    }
}

impl<T: TargetArch> MultiThreadResume for VmTarget<'_, T> {
    fn resume(&mut self) -> Result<(), Self::Error> {
        for (vp_index, vp) in self.0.vps.iter().enumerate() {
            let state = DebugState {
                single_step: vp.single_step,
                breakpoints: self.breakpoints,
            };
            tracing::debug!("resume: vp_index: {}, debug_state: {:?}", vp_index, state);
            self.0.req_chan.send(DebugRequest::SetDebugState {
                vp: vp_index as u32,
                state,
            });
        }

        let (send, recv) = mesh::oneshot();
        self.0
            .req_chan
            .send(DebugRequest::Resume { response: send });
        self.0.stop_chan = Some(recv);
        Ok(())
    }

    fn clear_resume_actions(&mut self) -> Result<(), Self::Error> {
        for vp in self.0.vps.as_mut() {
            vp.single_step = false;
        }
        Ok(())
    }

    fn set_resume_action_continue(
        &mut self,
        tid: Tid,
        _signal: Option<Signal>,
    ) -> Result<(), Self::Error> {
        let vp_index = self.0.tid_to_vp(tid)?;
        self.0.vps[vp_index as usize].single_step = false;
        Ok(())
    }

    #[inline(always)]
    fn support_single_step(&mut self) -> Option<MultiThreadSingleStepOps<'_, Self>> {
        Some(self)
    }
}

impl<T: TargetArch> MultiThreadSingleStep for VmTarget<'_, T> {
    fn set_resume_action_step(
        &mut self,
        tid: Tid,
        _signal: Option<Signal>,
    ) -> Result<(), Self::Error> {
        let vp_index = self.0.tid_to_vp(tid)?;
        self.0.vps[vp_index as usize].single_step = true;
        Ok(())
    }
}