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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use alloc::collections::BTreeSet;
use alloc::vec::Vec;
use core::marker::PhantomData;

use log::*;
use num_traits::ops::saturating::Saturating;

use crate::{
    protocol::{Command, Packet, ResponseWriter},
    Access, AccessKind, Connection, Error, Target, TargetState,
};

enum ExecState {
    Paused,
    Running { single_step: bool },
    Exit,
}

/// Facilitates the remote debugging of a [`Target`](trait.Target.html) using
/// the GDB Remote Serial Protocol over a given
/// [`Connection`](trait.Connection.html).
pub struct GdbStub<T: Target, C: Connection> {
    conn: C,
    exec_state: ExecState,
    swbreak: BTreeSet<T::Usize>,
    hwbreak: BTreeSet<T::Usize>,
    wwatch: BTreeSet<T::Usize>,
    rwatch: BTreeSet<T::Usize>,
    awatch: BTreeSet<T::Usize>,
    _target: PhantomData<T>,
}

impl<T: Target, C: Connection> GdbStub<T, C> {
    /// Create a new `GdbStub` using the provided connection.
    pub fn new(conn: C) -> GdbStub<T, C> {
        GdbStub {
            conn,
            swbreak: BTreeSet::new(),
            hwbreak: BTreeSet::new(),
            wwatch: BTreeSet::new(),
            rwatch: BTreeSet::new(),
            awatch: BTreeSet::new(),
            exec_state: ExecState::Paused,
            _target: PhantomData,
        }
    }

    fn handle_command(&mut self, target: &mut T, command: Command) -> Result<(), Error<T, C>> {
        // Acknowledge the command
        self.conn.write(b'+').map_err(Error::ConnectionRead)?;

        let mut res = ResponseWriter::new(&mut self.conn);

        match command {
            // ------------------ Handshaking and Queries ------------------- //
            Command::qSupported(_features) => {
                // TODO: enumerate qSupported features better
                res.write_str("swbreak+;")?;
                res.write_str("hwbreak+;")?;
                res.write_str("vContSupported+;")?;

                if T::target_description_xml().is_some() {
                    res.write_str("qXfer:features:read+;")?;
                }
            }
            Command::vContQuestionMark(_) => res.write_str("vCont;c;C;s;S;t")?,
            Command::qXferFeaturesRead(cmd) => {
                let _annex = cmd.annex; // This _should_ always be target.xml...
                match T::target_description_xml() {
                    Some(xml) => {
                        let xml = xml.trim();
                        if cmd.offset >= xml.len() {
                            // no more data
                            res.write_str("l")?;
                        } else if cmd.offset + cmd.len >= xml.len() {
                            // last little bit of data
                            res.write_str("l")?;
                            res.write_binary(&xml.as_bytes()[cmd.offset..])?
                        } else {
                            // still more data
                            res.write_str("m")?;
                            res.write_binary(&xml.as_bytes()[cmd.offset..(cmd.offset + cmd.len)])?
                        }
                    }
                    // If the target hasn't provided their own XML, then the initial response to
                    // "qSupported" wouldn't have included  "qXfer:features:read", and gdb wouldn't
                    // send this packet unless it was explicitly marked as supported.
                    None => unreachable!(),
                }
            }

            // -------------------- "Core" Functionality -------------------- //
            // TODO: Improve the '?' response...
            Command::QuestionMark(_) => res.write_str("S05")?,
            Command::qAttached(_) => res.write_str("1")?,
            Command::g(_) => {
                let mut err = Ok(());
                target.read_registers(|reg| {
                    if let Err(e) = res.write_hex_buf(reg) {
                        err = Err(e)
                    }
                });
                err?;
            }
            Command::G(cmd) => {
                // TODO: use the length of the slice returned by `target.read_registers` to
                // validate that the server sent the correct amount of data
                target.write_registers(cmd.vals.as_slice());
                res.write_str("OK")?;
            }
            Command::m(cmd) => {
                let mut err = Ok(());
                // XXX: get rid of these unwraps ahhh
                let start: T::Usize = num_traits::NumCast::from(cmd.addr).unwrap();
                // XXX: on overflow, this _should_ wrap around to low addresses (maybe?)
                let end = start.saturating_add(num_traits::NumCast::from(cmd.len).unwrap());

                target.read_addrs(start..end, |val| {
                    // TODO: assert the length is correct
                    if let Err(e) = res.write_hex(val) {
                        err = Err(e)
                    }
                });
                err?;
            }
            Command::M(cmd) => {
                let addr = cmd.addr;
                let mut val = cmd
                    .val
                    .into_iter()
                    .enumerate()
                    .map(|(i, v)| (addr + i as u64, v))
                    // XXX: get rid of this unwrap ahhh
                    .map(|(i, v)| (num_traits::NumCast::from(i).unwrap(), v));

                target.write_addrs(|| val.next());
            }
            Command::D(_) => {
                res.write_str("OK")?;
                self.exec_state = ExecState::Exit
            }
            Command::Z(cmd) => {
                // XXX: get rid of this unwrap ahhh
                let addr = num_traits::NumCast::from(cmd.addr).unwrap();
                let supported = match cmd.type_ {
                    // TODO: defer implementation of hardware and software breakpoints to target
                    0 => Some(self.swbreak.insert(addr)),
                    1 => Some(self.hwbreak.insert(addr)),
                    2 => Some(self.wwatch.insert(addr)),
                    3 => Some(self.rwatch.insert(addr)),
                    4 => Some(self.awatch.insert(addr)),
                    _ => None,
                };

                if supported.is_some() {
                    res.write_str("OK")?;
                }
            }
            Command::z(cmd) => {
                // XXX: get rid of this unwrap ahhh
                let addr = num_traits::NumCast::from(cmd.addr).unwrap();
                let supported = match cmd.type_ {
                    // TODO: defer implementation of hardware and software breakpoints to target
                    0 => Some(self.swbreak.remove(&addr)),
                    1 => Some(self.hwbreak.remove(&addr)),
                    2 => Some(self.wwatch.remove(&addr)),
                    3 => Some(self.rwatch.remove(&addr)),
                    4 => Some(self.awatch.remove(&addr)),
                    _ => None,
                };

                if supported.is_some() {
                    res.write_str("OK")?;
                }
            }
            Command::vCont(cmd) => {
                use crate::protocol::_vCont::VContKind;
                let action = &cmd.actions[0];
                self.exec_state = match action.kind {
                    VContKind::Step => ExecState::Running { single_step: true },
                    VContKind::Continue => ExecState::Running { single_step: false },
                    _ => unimplemented!("unsupported vCont action"),
                };
                // no immediate response
                return Ok(());
            }
            // TODO?: support custom resume addr in 'c' and 's'
            Command::c(_) => {
                self.exec_state = ExecState::Running { single_step: false };
                // no immediate response
                return Ok(());
            }
            Command::s(_) => {
                self.exec_state = ExecState::Running { single_step: true };
                // no immediate response
                return Ok(());
            }

            // ------------------- Stubbed Functionality -------------------- //
            // TODO: add proper support for >1 "thread"
            // for now, just hard-code a single thread with id 1
            Command::H(_) => res.write_str("OK")?,
            Command::qfThreadInfo(_) => res.write_str("m1")?,
            Command::qsThreadInfo(_) => res.write_str("l")?,
            Command::qC(_) => res.write_str("QC1")?,

            // -------------------------------------------------------------- //
            Command::Unknown(cmd) => warn!("Unknown command: {}", cmd),
            #[allow(unreachable_patterns)]
            c => warn!("Unimplemented command: {:?}", c),
        }

        res.flush().map_err(Error::ConnectionWrite)
    }

    fn recv_packet<'a, 'b>(
        &'a mut self,
        packet_buffer: &'b mut Vec<u8>,
    ) -> Result<Option<Packet<'b>>, Error<T, C>> {
        let header_byte = match self.exec_state {
            // block waiting for a gdb command
            ExecState::Paused => self.conn.read().map(Some),
            ExecState::Running { .. } => self.conn.read_nonblocking(),
            ExecState::Exit => unreachable!(),
        };

        match header_byte {
            Ok(None) => Ok(None), // no incoming message
            Ok(Some(header_byte)) => {
                packet_buffer.clear();
                packet_buffer.push(header_byte);
                if header_byte == b'$' {
                    // read the packet body
                    loop {
                        match self.conn.read().map_err(Error::ConnectionRead)? {
                            b'#' => break,
                            x => packet_buffer.push(x),
                        }
                    }
                    // append the # char
                    packet_buffer.push(b'#');
                    // and finally, read the checksum as well
                    packet_buffer.push(self.conn.read().map_err(Error::ConnectionRead)?);
                    packet_buffer.push(self.conn.read().map_err(Error::ConnectionRead)?);
                }

                match Packet::from_buf(packet_buffer) {
                    Ok(packet) => Ok(Some(packet)),
                    Err(e) => {
                        // TODO: preserve this context within Error::PacketParse
                        error!("Could not parse packet: {:?}", e);
                        Err(Error::PacketParse)
                    }
                }
            }
            Err(e) => Err(Error::ConnectionRead(e)),
        }
    }

    fn send_breakpoint_stop_response(
        &mut self,
        stop_reason: StopReason<T::Usize>,
    ) -> Result<(), Error<T, C>> {
        let mut res = ResponseWriter::new(&mut self.conn);

        res.write_str("T")?;
        res.write_hex(5)?;

        macro_rules! write_addr {
            ($addr:expr) => {
                // XXX: get rid of this unwrap ahhh
                let addr: u64 = num_traits::NumCast::from($addr).unwrap();
                res.write_hex_buf(&addr.to_ne_bytes())?;
            };
        }

        match stop_reason {
            StopReason::WWatch(addr) => {
                res.write_str("watch:")?;
                write_addr!(addr);
            }
            StopReason::RWatch(addr) => {
                res.write_str("rwatch:")?;
                write_addr!(addr);
            }
            StopReason::AWatch(addr) => {
                res.write_str("awatch:")?;
                write_addr!(addr);
            }
            StopReason::SwBreak => res.write_str("swbreak:")?,
            StopReason::HwBreak => res.write_str("hwbreak:")?,
        };

        res.write_str(";")?;

        Ok(res.flush()?)
    }

    /// Starts a GDB remote debugging session.
    ///
    /// Returns once the GDB client cleanly closes the debugging session (via
    /// the GDB `quit` command).
    pub fn run(&mut self, target: &mut T) -> Result<TargetState, Error<T, C>> {
        let mut packet_buffer = Vec::new();

        loop {
            // Handle any incoming GDB packets
            match self.recv_packet(&mut packet_buffer)? {
                None => {}
                Some(packet) => match packet {
                    Packet::Ack => {}
                    Packet::Nack => unimplemented!(),
                    Packet::Interrupt => {
                        self.exec_state = ExecState::Paused;
                        let mut res = ResponseWriter::new(&mut self.conn);
                        res.write_str("S05")?;
                        res.flush()?;
                    }
                    Packet::Command(command) => {
                        self.handle_command(target, command)?;
                    }
                },
            };

            match self.exec_state {
                ExecState::Paused => {}
                ExecState::Exit => {
                    return Ok(TargetState::Running);
                }
                ExecState::Running { single_step } => {
                    let mut stop_reason = None;

                    // check for memory breakpoints on each access
                    let on_access = |access: Access<T::Usize>| {
                        if self.awatch.contains(&access.addr) {
                            stop_reason = Some(StopReason::AWatch(access.addr));
                            return;
                        }

                        match access.kind {
                            AccessKind::Read => {
                                if self.rwatch.contains(&access.addr) {
                                    stop_reason = Some(StopReason::RWatch(access.addr))
                                }
                            }
                            AccessKind::Write => {
                                if self.wwatch.contains(&access.addr) {
                                    stop_reason = Some(StopReason::WWatch(access.addr))
                                }
                            }
                        }
                    };

                    let target_state = target.step(on_access).map_err(Error::TargetError)?;
                    if target_state == TargetState::Halted {
                        // "The process exited with status code 00"
                        let mut res = ResponseWriter::new(&mut self.conn);
                        res.write_str("W00")?;
                        res.flush()?;
                        return Ok(TargetState::Halted);
                    };

                    // TODO: defer implementation of hardware and software breakpoints to target
                    let target_pc = target.read_pc();
                    if target_state == TargetState::SoftwareBreakpoint
                        || self.swbreak.contains(&target_pc)
                    {
                        stop_reason = Some(StopReason::SwBreak)
                    } else if self.hwbreak.contains(&target_pc) {
                        stop_reason = Some(StopReason::HwBreak)
                    }

                    // if something interesting happened, send the stop response
                    if let Some(stop_reason) = stop_reason {
                        warn!("{:x?}", stop_reason);
                        self.send_breakpoint_stop_response(stop_reason)?;
                        self.exec_state = ExecState::Paused;
                    } else if single_step {
                        self.exec_state = ExecState::Paused;
                        let mut res = ResponseWriter::new(&mut self.conn);
                        res.write_str("S05")?;
                        res.flush()?;
                    }
                }
            }
        }
    }
}

#[derive(Debug)]
enum StopReason<U> {
    WWatch(U),
    RWatch(U),
    AWatch(U),
    SwBreak,
    HwBreak,
    // TODO: add more stop reasons
}

// enum SignalMetadata {
//     Register(u8, Vec<u8>),
//     Thread { tid: isize },
//     Core(usize),
//     StopReason(StopReason),
// }

// enum StopReply<'a> {
//     Signal(u8),                              // S
//     SignalWithMeta(u8, Vec<SignalMetadata>), // T
//     Exited {
//         status: u8,
//         pid: Option<isize>,
//     }, // W
//     Terminated {
//         status: u8,
//         pid: Option<isize>,
//     }, // X
//     ThreadExit {
//         status: u8,
//         tid: isize,
//     }, // w
//     NoResumedThreads,                        // N
//     ConsoleOutput(&'a [u8]),                 // O
//     FileIOSyscall {
//         call_id: &'a str,
//         params: Vec<&'a str>,
//     },
// }