aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/commands/_vCont.rs
blob: 437fa4c748596375649e30e5bee26def26a277d6 (plain)
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
use super::prelude::*;
use crate::common::Signal;
use crate::protocol::common::hex::HexString;
use crate::protocol::common::thread_id::SpecificThreadId;
use crate::protocol::common::thread_id::ThreadId;

// TODO?: instead of lazily parsing data, parse the strings into a compressed
// binary representations that can be stuffed back into the packet buffer and
// return an iterator over the binary data that's _guaranteed_ to be valid. This
// would clean up some of the code in the vCont handler.
//
// The interesting part would be to see whether or not the simplified error
// handing code will compensate for all the new code required to pre-validate
// the data...
#[derive(Debug)]
pub enum vCont<'a> {
    Query,
    Actions(Actions<'a>),
}

impl<'a> ParseCommand<'a> for vCont<'a> {
    #[inline(always)]
    fn from_packet(buf: PacketBuf<'a>) -> Option<Self> {
        let body = buf.into_body();
        match body as &[u8] {
            b"?" => Some(vCont::Query),
            _ => Some(vCont::Actions(Actions::new_from_buf(body))),
        }
    }
}

#[derive(Debug)]
pub enum Actions<'a> {
    Buf(ActionsBuf<'a>),
    FixedStep(SpecificThreadId),
    FixedCont(SpecificThreadId),
}

impl<'a> Actions<'a> {
    fn new_from_buf(buf: &'a [u8]) -> Actions<'a> {
        Actions::Buf(ActionsBuf(buf))
    }

    #[inline(always)]
    pub fn new_step(tid: SpecificThreadId) -> Actions<'a> {
        Actions::FixedStep(tid)
    }

    #[inline(always)]
    pub fn new_continue(tid: SpecificThreadId) -> Actions<'a> {
        Actions::FixedCont(tid)
    }

    pub fn iter(&self) -> impl Iterator<Item = Option<VContAction<'a>>> + '_ {
        match self {
            Actions::Buf(x) => EitherIter::A(x.iter()),
            Actions::FixedStep(x) => EitherIter::B(core::iter::once(Some(VContAction {
                kind: VContKind::Step,
                thread: Some(*x),
            }))),
            Actions::FixedCont(x) => EitherIter::B(core::iter::once(Some(VContAction {
                kind: VContKind::Continue,
                thread: Some(*x),
            }))),
        }
    }
}

#[derive(Debug)]
pub struct ActionsBuf<'a>(&'a [u8]);

impl<'a> ActionsBuf<'a> {
    fn iter(&self) -> impl Iterator<Item = Option<VContAction<'a>>> + '_ {
        self.0.split(|b| *b == b';').skip(1).map(|act| {
            let mut s = act.split(|b| *b == b':');
            let kind = s.next()?;
            let thread = match s.next() {
                Some(s) => Some(SpecificThreadId::try_from(ThreadId::try_from(s).ok()?).ok()?),
                None => None,
            };

            Some(VContAction {
                kind: VContKind::from_bytes(kind)?,
                thread,
            })
        })
    }
}

#[derive(Debug, Copy, Clone)]
pub struct VContAction<'a> {
    pub kind: VContKind<'a>,
    pub thread: Option<SpecificThreadId>,
}

#[derive(Debug, Copy, Clone)]
pub enum VContKind<'a> {
    Continue,
    ContinueWithSig(Signal),
    RangeStep(HexString<'a>, HexString<'a>),
    Step,
    StepWithSig(Signal),
    Stop,
}

impl<'a> VContKind<'a> {
    #[inline(always)]
    fn from_bytes(s: &[u8]) -> Option<VContKind<'_>> {
        use self::VContKind::*;

        let res = match s {
            [b'c'] => Continue,
            [b's'] => Step,
            [b't'] => Stop,
            [b'C', sig @ ..] => ContinueWithSig(Signal(decode_hex(sig).ok()?)),
            [b'S', sig @ ..] => StepWithSig(Signal(decode_hex(sig).ok()?)),
            [b'r', range @ ..] => {
                let mut range = range.split(|b| *b == b',');
                RangeStep(HexString(range.next()?), HexString(range.next()?))
            }
            _ => return None,
        };

        Some(res)
    }
}

/// Helper type to unify iterators that output the same type. Returned as an
/// opaque type from `Actions::iter()`.
enum EitherIter<A, B> {
    A(A),
    B(B),
}

impl<A, B, T> Iterator for EitherIter<A, B>
where
    A: Iterator<Item = T>,
    B: Iterator<Item = T>,
{
    type Item = T;

    #[inline(always)]
    fn next(&mut self) -> Option<T> {
        match self {
            EitherIter::A(a) => a.next(),
            EitherIter::B(b) => b.next(),
        }
    }
}