ratasm 0.3.0

A terminal IDE and debugger for x86_64 assembly
Documentation
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! GDB/MI output records.
//!
//! Every line GDB writes on its machine interface is one of a handful of
//! record types, distinguished by the first character after an optional
//! numeric token:
//!
//! ```text
//! ^done,key=value      result record: the answer to a command
//! *stopped,reason=...  exec-async: the program's execution state changed
//! =thread-created,...  notify-async: something in GDB's own state changed
//! +download,...        status-async: progress on a long operation
//! ~"text"              console stream: what a human user would have seen
//! @"text"              target stream: output from the program itself
//! &"text"              log stream: GDB's internal logging
//! (gdb)                the prompt, marking the end of a response
//! ```
//!
//! # Why tokens matter
//!
//! Commands may be sent before earlier ones have replied, and async events
//! arrive interleaved with replies at any time. The numeric token that a
//! client puts in front of a command is echoed on its result record, and it is
//! the *only* reliable way to match an answer to its question. Matching on
//! order instead breaks the first time the program hits a breakpoint while a
//! command is in flight.

use super::value::{ParseError, Parser, Value};

/// The class of a result record.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResultClass {
    /// The command completed.
    Done,
    /// The command completed; a synonym for `done` used by some commands.
    Running,
    /// A remote connection was established.
    Connected,
    /// The command failed.
    Error,
    /// GDB is exiting.
    Exit,
}

impl ResultClass {
    /// Parses a result class name.
    pub fn parse(text: &str) -> Option<Self> {
        match text {
            "done" => Some(ResultClass::Done),
            "running" => Some(ResultClass::Running),
            "connected" => Some(ResultClass::Connected),
            "error" => Some(ResultClass::Error),
            "exit" => Some(ResultClass::Exit),
            _ => None,
        }
    }

    /// Whether this class reports failure.
    pub fn is_error(self) -> bool {
        matches!(self, ResultClass::Error)
    }

    /// The name as it appears in the protocol.
    pub const fn as_str(self) -> &'static str {
        match self {
            ResultClass::Done => "done",
            ResultClass::Running => "running",
            ResultClass::Connected => "connected",
            ResultClass::Error => "error",
            ResultClass::Exit => "exit",
        }
    }
}

/// Which asynchronous channel a record arrived on.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AsyncKind {
    /// `*` — the program's execution state changed.
    Exec,
    /// `+` — progress on a long-running operation.
    Status,
    /// `=` — GDB's own state changed.
    Notify,
}

/// Which stream a piece of text came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StreamKind {
    /// `~` — what an interactive user would have seen.
    Console,
    /// `@` — output from the program being debugged.
    Target,
    /// `&` — GDB's internal log.
    Log,
}

/// One line of GDB/MI output.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Record {
    /// The answer to a command.
    Result {
        /// The token echoed from the command, when it had one.
        token: Option<u32>,
        /// Whether the command succeeded.
        class: ResultClass,
        /// The payload.
        results: Vec<(String, Value)>,
    },
    /// An asynchronous notification.
    Async {
        /// The token, when the event was caused by a tokenised command.
        token: Option<u32>,
        /// Which channel it arrived on.
        kind: AsyncKind,
        /// The event name, such as `stopped` or `thread-group-added`.
        class: String,
        /// The payload.
        results: Vec<(String, Value)>,
    },
    /// A line of text on one of the streams.
    Stream {
        /// Which stream it came from.
        kind: StreamKind,
        /// The text, with escapes decoded.
        text: String,
    },
    /// The `(gdb)` prompt marking the end of a response.
    Prompt,
}

impl Record {
    /// The token, when the record carries one.
    pub fn token(&self) -> Option<u32> {
        match self {
            Record::Result { token, .. } | Record::Async { token, .. } => *token,
            _ => None,
        }
    }

    /// The payload, for records that carry one.
    pub fn results(&self) -> &[(String, Value)] {
        match self {
            Record::Result { results, .. } | Record::Async { results, .. } => results,
            _ => &[],
        }
    }

    /// Looks up a key in the record's payload.
    pub fn get(&self, key: &str) -> Option<&Value> {
        self.results()
            .iter()
            .find(|(name, _)| name == key)
            .map(|(_, value)| value)
    }

    /// Looks up a key and returns it as a string.
    pub fn get_str(&self, key: &str) -> Option<&str> {
        self.get(key)?.as_str()
    }

    /// The error message, for a failed result record.
    ///
    /// GDB puts the message in a `msg` field; a missing one is reported as a
    /// generic message rather than an empty string so the user always sees
    /// something.
    pub fn error_message(&self) -> Option<String> {
        match self {
            Record::Result {
                class: ResultClass::Error,
                ..
            } => Some(
                self.get_str("msg")
                    .unwrap_or("the debugger reported an error with no message")
                    .to_owned(),
            ),
            _ => None,
        }
    }

    /// Whether this record is a completed answer to a command.
    pub fn is_result(&self) -> bool {
        matches!(self, Record::Result { .. })
    }

    /// Whether this record reports that the program stopped.
    pub fn is_stopped(&self) -> bool {
        matches!(
            self,
            Record::Async {
                kind: AsyncKind::Exec,
                class,
                ..
            } if class == "stopped"
        )
    }

    /// The `reason` field of a stop record.
    pub fn stop_reason(&self) -> Option<&str> {
        if self.is_stopped() {
            self.get_str("reason")
        } else {
            None
        }
    }

    /// Whether this record reports that the program finished.
    ///
    /// Program exit is not a record class of its own: GDB reports it as an
    /// ordinary `*stopped` whose reason is `exited`, `exited-normally` or
    /// `exited-signalled`. Treating "stopped" as "paused and inspectable"
    /// without checking the reason would leave the debugger waiting to step a
    /// process that no longer exists.
    pub fn is_program_exit(&self) -> bool {
        self.stop_reason()
            .is_some_and(|reason| reason.starts_with("exited"))
    }

    /// The program's exit status, when this record reports an exit.
    ///
    /// GDB writes this field in **octal**: a program exiting with status 9 is
    /// reported as `exit-code="011"`. Reading it as decimal is a silent
    /// off-by-a-lot, so the conversion happens here once.
    pub fn exit_code(&self) -> Option<i32> {
        if !self.is_program_exit() {
            return None;
        }
        match self.get_str("exit-code") {
            Some(text) => i32::from_str_radix(text.trim(), 8).ok(),
            // `exited-normally` carries no code and means zero.
            None => (self.stop_reason() == Some("exited-normally")).then_some(0),
        }
    }

    /// Whether this record reports that the program resumed.
    pub fn is_running(&self) -> bool {
        matches!(
            self,
            Record::Async {
                kind: AsyncKind::Exec,
                class,
                ..
            } if class == "running"
        )
    }
}

/// Parses one line of GDB/MI output.
///
/// Returns `Ok(None)` for a blank line, which GDB emits freely and which
/// carries no information.
///
/// # Errors
///
/// Returns [`ParseError`] when the line has a recognised prefix but a
/// malformed payload.
pub fn parse_line(line: &str) -> Result<Option<Record>, ParseError> {
    let line = line.trim_end_matches(['\r', '\n']);
    let trimmed = line.trim();

    if trimmed.is_empty() {
        return Ok(None);
    }
    // The prompt is written as "(gdb)" and often padded with a space.
    if trimmed == "(gdb)" || trimmed == "(gdb) " {
        return Ok(Some(Record::Prompt));
    }

    // A stream record is never preceded by a token.
    if let Some(kind) = stream_kind(trimmed.as_bytes()[0]) {
        let mut parser = Parser::new(&trimmed[1..]);
        let text = match parser.parse_value()? {
            Value::String(text) => text,
            other => other.to_string(),
        };
        return Ok(Some(Record::Stream { kind, text }));
    }

    let (token, rest) = split_token(trimmed);
    let Some(marker) = rest.as_bytes().first().copied() else {
        return Ok(None);
    };
    let body = &rest[1..];

    match marker {
        b'^' => {
            let (class_name, results) = split_class(body);
            let class = ResultClass::parse(class_name).ok_or(ParseError::Unexpected {
                found: class_name.chars().next().unwrap_or('?'),
                expected: "a result class",
                position: 0,
            })?;
            Ok(Some(Record::Result {
                token,
                class,
                results: parse_payload(results)?,
            }))
        }
        b'*' | b'+' | b'=' => {
            let kind = match marker {
                b'*' => AsyncKind::Exec,
                b'+' => AsyncKind::Status,
                _ => AsyncKind::Notify,
            };
            let (class_name, results) = split_class(body);
            Ok(Some(Record::Async {
                token,
                kind,
                class: class_name.to_owned(),
                results: parse_payload(results)?,
            }))
        }
        // GDB occasionally emits plain text, for instance its startup banner
        // when it is not in pure MI mode. Treating it as console output keeps
        // it visible rather than throwing it away.
        _ => Ok(Some(Record::Stream {
            kind: StreamKind::Console,
            text: line.to_owned(),
        })),
    }
}

/// Parses a payload, tolerating an empty one.
fn parse_payload(text: &str) -> Result<Vec<(String, Value)>, ParseError> {
    if text.is_empty() {
        return Ok(Vec::new());
    }
    let mut parser = Parser::new(text);
    parser.parse_results()
}

/// Splits the leading numeric token from a line.
fn split_token(line: &str) -> (Option<u32>, &str) {
    let digits = line
        .as_bytes()
        .iter()
        .take_while(|byte| byte.is_ascii_digit())
        .count();
    if digits == 0 {
        return (None, line);
    }
    let token = line[..digits].parse().ok();
    (token, &line[digits..])
}

/// Splits the class name from the results that follow it.
fn split_class(body: &str) -> (&str, &str) {
    match body.find(',') {
        Some(index) => (&body[..index], &body[index + 1..]),
        None => (body, ""),
    }
}

/// Maps a stream marker byte to its kind.
fn stream_kind(marker: u8) -> Option<StreamKind> {
    match marker {
        b'~' => Some(StreamKind::Console),
        b'@' => Some(StreamKind::Target),
        b'&' => Some(StreamKind::Log),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse(line: &str) -> Record {
        parse_line(line)
            .unwrap_or_else(|error| panic!("{line:?} failed to parse: {error}"))
            .unwrap_or_else(|| panic!("{line:?} produced no record"))
    }

    #[test]
    fn a_bare_done_is_a_result_record() {
        let record = parse("^done");
        assert!(record.is_result());
        assert_eq!(record.token(), None);
        assert!(record.results().is_empty());
        match record {
            Record::Result { class, .. } => assert_eq!(class, ResultClass::Done),
            other => panic!("unexpected record: {other:?}"),
        }
    }

    #[test]
    fn a_token_is_carried_through() {
        // Without this, replies cannot be matched to their commands.
        let record = parse("0042^done");
        assert_eq!(record.token(), Some(42));
    }

    #[test]
    fn a_result_payload_is_parsed() {
        let record = parse(r#"^done,value="0x3c""#);
        assert_eq!(record.get_str("value"), Some("0x3c"));
    }

    #[test]
    fn an_error_record_exposes_its_message() {
        let record = parse(r#"^error,msg="No symbol \"nope\" in current context.""#);
        assert_eq!(
            record.error_message().as_deref(),
            Some("No symbol \"nope\" in current context.")
        );
        match record {
            Record::Result { class, .. } => assert!(class.is_error()),
            other => panic!("unexpected record: {other:?}"),
        }
    }

    #[test]
    fn an_error_without_a_message_still_reports_something() {
        let record = parse("^error");
        assert!(record.error_message().is_some());
        assert!(!record.error_message().unwrap_or_default().is_empty());
    }

    #[test]
    fn a_successful_record_has_no_error_message() {
        assert!(parse("^done").error_message().is_none());
        assert!(parse("*stopped").error_message().is_none());
    }

    #[test]
    fn the_running_class_is_recognised() {
        match parse("^running") {
            Record::Result { class, .. } => assert_eq!(class, ResultClass::Running),
            other => panic!("unexpected record: {other:?}"),
        }
    }

    #[test]
    fn an_unknown_result_class_is_an_error() {
        assert!(parse_line("^nonsense").is_err());
    }

    #[test]
    fn a_stopped_event_is_recognised_with_its_payload() {
        let line = r#"*stopped,reason="breakpoint-hit",bkptno="1",frame={addr="0x004000b0",func="_start",file="main.asm",line="9"},thread-id="1""#;
        let record = parse(line);
        assert!(record.is_stopped());
        assert!(!record.is_running());
        assert_eq!(record.get_str("reason"), Some("breakpoint-hit"));

        let frame = record.get("frame").expect("frame");
        assert_eq!(frame.get_int("line"), Some(9));
        assert_eq!(frame.get_address("addr"), Some(0x0040_00b0));
    }

    #[test]
    fn a_running_event_is_recognised() {
        let record = parse(r#"*running,thread-id="all""#);
        assert!(record.is_running());
        assert!(!record.is_stopped());
    }

    #[test]
    fn notify_and_status_records_are_distinguished_from_exec() {
        match parse(r#"=thread-group-added,id="i1""#) {
            Record::Async { kind, class, .. } => {
                assert_eq!(kind, AsyncKind::Notify);
                assert_eq!(class, "thread-group-added");
            }
            other => panic!("unexpected record: {other:?}"),
        }
        match parse(r#"+download,section=".text""#) {
            Record::Async { kind, .. } => assert_eq!(kind, AsyncKind::Status),
            other => panic!("unexpected record: {other:?}"),
        }
    }

    #[test]
    fn a_notify_record_is_not_treated_as_a_stop() {
        // `=` records describe GDB's state, never the program's execution.
        let record = parse(r#"=breakpoint-modified,bkpt={number="1"}"#);
        assert!(!record.is_stopped());
        assert!(!record.is_result());
    }

    #[test]
    fn the_three_stream_kinds_are_distinguished() {
        for (line, expected) in [
            (r#"~"console text\n""#, StreamKind::Console),
            (r#"@"program output\n""#, StreamKind::Target),
            (r#"&"log line\n""#, StreamKind::Log),
        ] {
            match parse(line) {
                Record::Stream { kind, text } => {
                    assert_eq!(kind, expected, "wrong kind for {line}");
                    assert!(text.ends_with('\n'), "escape not decoded in {line}");
                }
                other => panic!("unexpected record: {other:?}"),
            }
        }
    }

    #[test]
    fn stream_text_keeps_embedded_punctuation() {
        // Program output legitimately contains commas, braces and quotes.
        let record = parse(r#"@"mov rax, {1} \"quoted\"\n""#);
        match record {
            Record::Stream { text, .. } => {
                assert_eq!(text, "mov rax, {1} \"quoted\"\n");
            }
            other => panic!("unexpected record: {other:?}"),
        }
    }

    #[test]
    fn the_prompt_is_recognised() {
        assert_eq!(parse("(gdb)"), Record::Prompt);
        assert_eq!(parse("(gdb) "), Record::Prompt);
    }

    #[test]
    fn blank_lines_produce_nothing() {
        assert_eq!(parse_line(""), Ok(None));
        assert_eq!(parse_line("   "), Ok(None));
        assert_eq!(parse_line("\n"), Ok(None));
    }

    #[test]
    fn trailing_line_endings_are_ignored() {
        assert!(parse("^done\r\n").is_result());
        assert!(parse("^done\n").is_result());
    }

    #[test]
    fn unrecognised_text_is_kept_as_console_output() {
        // GDB's startup banner, for instance. Discarding it would hide
        // messages the user may need.
        match parse("GNU gdb (GDB) 17.2") {
            Record::Stream { kind, text } => {
                assert_eq!(kind, StreamKind::Console);
                assert!(text.contains("GNU gdb"));
            }
            other => panic!("unexpected record: {other:?}"),
        }
    }

    #[test]
    fn a_malformed_payload_is_an_error_not_a_panic() {
        assert!(parse_line(r#"^done,value="unterminated"#).is_err());
        assert!(parse_line("*stopped,=").is_err());
    }

    #[test]
    fn a_real_session_transcript_parses_completely() {
        // A verbatim exchange from starting a program and hitting a breakpoint.
        const TRANSCRIPT: &str = r#"=thread-group-added,id="i1"
~"GNU gdb (GDB) 17.2\n"
(gdb)
-break-insert _start
^done,bkpt={number="1",type="breakpoint",disp="keep",enabled="y",addr="0x00000000004000b0",func="_start",file="main.asm",fullname="/tmp/p/main.asm",line="9",thread-groups=["i1"],times="0",original-location="_start"}
(gdb)
=thread-created,id="1",group-id="i1"
^running
*running,thread-id="all"
(gdb)
@"Hello, world!\n"
*stopped,reason="breakpoint-hit",disp="keep",bkptno="1",frame={addr="0x00000000004000b0",func="_start",args=[],file="main.asm",line="9"},thread-id="1",stopped-threads="all",core="2"
(gdb)
"#;

        let mut results = 0;
        let mut stops = 0;
        let mut prompts = 0;
        let mut target_output = String::new();

        for line in TRANSCRIPT.lines() {
            // The command echo is not MI output; skip it as GDB would not
            // send it back.
            if line.starts_with('-') {
                continue;
            }
            let record =
                parse_line(line).unwrap_or_else(|error| panic!("{line:?} failed: {error}"));
            match record {
                Some(Record::Result { .. }) => results += 1,
                Some(Record::Prompt) => prompts += 1,
                Some(ref record) if record.is_stopped() => stops += 1,
                Some(Record::Stream {
                    kind: StreamKind::Target,
                    ref text,
                }) => target_output.push_str(text),
                _ => {}
            }
        }

        assert_eq!(results, 2, "^done and ^running");
        assert_eq!(stops, 1);
        assert_eq!(prompts, 4);
        assert_eq!(target_output, "Hello, world!\n");
    }

    #[test]
    fn a_breakpoint_record_exposes_its_fields() {
        let line = r#"^done,bkpt={number="2",type="breakpoint",enabled="y",addr="0x004000b4",func="_start",file="main.asm",line="12",times="0"}"#;
        let record = parse(line);
        let bkpt = record.get("bkpt").expect("bkpt");
        assert_eq!(bkpt.get_int("number"), Some(2));
        assert_eq!(bkpt.get_str("enabled"), Some("y"));
        assert_eq!(bkpt.get_address("addr"), Some(0x0040_00b4));
        assert_eq!(bkpt.get_int("line"), Some(12));
    }

    #[test]
    fn a_normal_exit_is_recognised_as_an_exit_not_a_pause() {
        // Verbatim from GDB 17.2.
        let record = parse(r#"*stopped,reason="exited-normally""#);
        assert!(record.is_stopped(), "it is still a stop record");
        assert!(record.is_program_exit(), "but the program has finished");
        assert_eq!(record.exit_code(), Some(0));
    }

    #[test]
    fn an_exit_code_is_decoded_from_octal() {
        // GDB writes the status in octal: 011 is 9, not 11. Verified against
        // GDB 17.2 with a program exiting with status 9.
        let record = parse(r#"*stopped,reason="exited",exit-code="011""#);
        assert!(record.is_program_exit());
        assert_eq!(record.exit_code(), Some(9));
    }

    #[test]
    fn a_breakpoint_stop_is_not_an_exit() {
        let record = parse(r#"*stopped,reason="breakpoint-hit",bkptno="1""#);
        assert!(record.is_stopped());
        assert!(!record.is_program_exit());
        assert_eq!(record.exit_code(), None);
        assert_eq!(record.stop_reason(), Some("breakpoint-hit"));
    }

    #[test]
    fn a_signalled_exit_is_recognised() {
        let record = parse(
            r#"*stopped,reason="exited-signalled",signal-name="SIGSEGV",signal-meaning="Segmentation fault""#,
        );
        assert!(record.is_program_exit());
        assert_eq!(record.get_str("signal-name"), Some("SIGSEGV"));
    }

    #[test]
    fn a_signal_stop_is_a_pause_not_an_exit() {
        // A caught SIGSEGV stops the program without ending it, which is
        // exactly the case a debugger exists to inspect.
        let record = parse(
            r#"*stopped,reason="signal-received",signal-name="SIGSEGV",frame={addr="0x004000b4"}"#,
        );
        assert!(!record.is_program_exit());
        assert_eq!(record.stop_reason(), Some("signal-received"));
        assert_eq!(
            record.get("frame").and_then(|f| f.get_address("addr")),
            Some(0x0040_00b4)
        );
    }

    #[test]
    fn a_non_stop_record_has_no_stop_reason() {
        assert_eq!(parse("^done").stop_reason(), None);
        assert!(!parse("^done").is_program_exit());
    }

    #[test]
    fn result_class_names_round_trip() {
        for class in [
            ResultClass::Done,
            ResultClass::Running,
            ResultClass::Connected,
            ResultClass::Error,
            ResultClass::Exit,
        ] {
            assert_eq!(ResultClass::parse(class.as_str()), Some(class));
        }
        assert_eq!(ResultClass::parse("nonsense"), None);
    }
}