ratasm 0.1.2

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
//! The debugger state machine.
//!
//! ```text
//!                 ┌──────────────────────────────┐
//!                 ▼                              │
//!  Idle ──build──► Building ──ok──► Ready ──launch──► Starting
//!                     │                ▲                 │
//!                   failed             │              started
//!                     ▼                │                 ▼
//!                  Failed              │            Running ⇄ Paused
//!                     │                │                 │       │
//!                     └──reset─────────┘                exited───┘
//!                                      │                 ▼
//!                                      └──────────── Exited
//! ```
//!
//! # Why this is explicit
//!
//! The debugger's operations are only valid in particular states: stepping a
//! program that is not running, or setting a register while the target is
//! executing, produce confusing errors from GDB rather than clear ones from
//! us. Encoding the legal transitions in one place means the UI can *ask*
//! whether an action is possible — [`DebuggerState::can_step`] and friends —
//! and a mistake becomes an [`InvalidTransition`] naming both states rather
//! than a mysterious protocol failure.

use std::fmt;

/// Where the debugger currently is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
pub enum DebuggerState {
    /// Nothing is loaded and nothing is running.
    #[default]
    Idle,
    /// The project is being assembled and linked.
    Building,
    /// A debuggable executable exists but no session is running.
    Ready,
    /// GDB is starting and loading the program.
    Starting,
    /// The program is executing; the debugger cannot inspect it.
    Running,
    /// The program is stopped and can be inspected and stepped.
    Paused,
    /// The program finished.
    Exited,
    /// Something went wrong and the session cannot continue.
    Failed,
}

impl DebuggerState {
    /// Every state, for exhaustive testing and display.
    pub const ALL: [DebuggerState; 8] = [
        DebuggerState::Idle,
        DebuggerState::Building,
        DebuggerState::Ready,
        DebuggerState::Starting,
        DebuggerState::Running,
        DebuggerState::Paused,
        DebuggerState::Exited,
        DebuggerState::Failed,
    ];

    /// A short name for the status bar.
    pub const fn label(self) -> &'static str {
        match self {
            DebuggerState::Idle => "idle",
            DebuggerState::Building => "building",
            DebuggerState::Ready => "ready",
            DebuggerState::Starting => "starting",
            DebuggerState::Running => "running",
            DebuggerState::Paused => "paused",
            DebuggerState::Exited => "exited",
            DebuggerState::Failed => "failed",
        }
    }

    /// Whether a debug session exists, whether or not it is stopped.
    pub const fn is_session_active(self) -> bool {
        matches!(
            self,
            DebuggerState::Starting | DebuggerState::Running | DebuggerState::Paused
        )
    }

    /// Whether the program is stopped and inspectable.
    ///
    /// Reading registers, memory, the stack or the disassembly is only
    /// meaningful here: while the program runs, any value read is stale before
    /// it can be displayed.
    pub const fn can_inspect(self) -> bool {
        matches!(self, DebuggerState::Paused)
    }

    /// Whether a step command is currently valid.
    pub const fn can_step(self) -> bool {
        matches!(self, DebuggerState::Paused)
    }

    /// Whether execution can be resumed.
    pub const fn can_continue(self) -> bool {
        matches!(self, DebuggerState::Paused)
    }

    /// Whether the program can be interrupted.
    pub const fn can_interrupt(self) -> bool {
        matches!(self, DebuggerState::Running)
    }

    /// Whether a new session can be launched.
    pub const fn can_launch(self) -> bool {
        matches!(
            self,
            DebuggerState::Ready | DebuggerState::Exited | DebuggerState::Failed
        )
    }

    /// Whether a build can be started.
    ///
    /// Building while a session holds the executable open would replace the
    /// file underneath the running debugger, so it is refused until the
    /// session ends.
    pub const fn can_build(self) -> bool {
        matches!(
            self,
            DebuggerState::Idle
                | DebuggerState::Ready
                | DebuggerState::Exited
                | DebuggerState::Failed
        )
    }

    /// Whether the session can be stopped.
    pub const fn can_stop(self) -> bool {
        self.is_session_active()
    }

    /// Whether breakpoints can be edited right now.
    ///
    /// Before a session starts they are recorded and applied on launch; while
    /// paused GDB accepts them directly. While the program runs they are
    /// deferred, so the UI disables the action rather than appearing to work.
    pub const fn can_edit_breakpoints(self) -> bool {
        !matches!(self, DebuggerState::Running | DebuggerState::Starting)
    }
}

impl fmt::Display for DebuggerState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.label())
    }
}

/// Something that happens to the debugger.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Transition {
    /// A build was started.
    BuildStarted,
    /// The build produced an executable.
    BuildSucceeded,
    /// The build failed.
    BuildFailed,
    /// A debug session was requested.
    LaunchRequested,
    /// GDB loaded the program and it began executing.
    LaunchSucceeded,
    /// GDB could not start the session.
    LaunchFailed,
    /// The program stopped, at a breakpoint, a step, or a signal.
    Stopped,
    /// The program resumed.
    Resumed,
    /// The program finished.
    ProgramExited,
    /// The session broke: GDB died, or the protocol desynchronised.
    SessionFailed,
    /// The user dismissed a finished or failed session.
    Reset,
}

impl Transition {
    /// A short description for logs and error messages.
    pub const fn label(self) -> &'static str {
        match self {
            Transition::BuildStarted => "build started",
            Transition::BuildSucceeded => "build succeeded",
            Transition::BuildFailed => "build failed",
            Transition::LaunchRequested => "launch requested",
            Transition::LaunchSucceeded => "launch succeeded",
            Transition::LaunchFailed => "launch failed",
            Transition::Stopped => "program stopped",
            Transition::Resumed => "program resumed",
            Transition::ProgramExited => "program exited",
            Transition::SessionFailed => "session failed",
            Transition::Reset => "reset",
        }
    }
}

/// A transition that is not legal from the current state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("cannot apply '{transition}' while {from}", transition = transition.label())]
pub struct InvalidTransition {
    /// The state the machine was in.
    pub from: DebuggerState,
    /// The transition that was attempted.
    pub transition: Transition,
}

/// The debugger's state, with legal transitions enforced.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct StateMachine {
    state: DebuggerState,
    /// The reason recorded with the most recent failure.
    failure: Option<String>,
    /// The exit status of the most recent run.
    exit_code: Option<i32>,
}

impl StateMachine {
    /// Creates a machine in [`DebuggerState::Idle`].
    pub fn new() -> Self {
        Self::default()
    }

    /// The current state.
    pub fn state(&self) -> DebuggerState {
        self.state
    }

    /// The reason for the most recent failure, if any.
    pub fn failure(&self) -> Option<&str> {
        self.failure.as_deref()
    }

    /// The exit code of the most recent run, if it exited normally.
    pub fn exit_code(&self) -> Option<i32> {
        self.exit_code
    }

    /// Whether `transition` is legal from the current state.
    pub fn allows(&self, transition: Transition) -> bool {
        next_state(self.state, transition).is_some()
    }

    /// Applies `transition`.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidTransition`] when the transition is not legal from the
    /// current state, leaving the state unchanged.
    pub fn apply(&mut self, transition: Transition) -> Result<DebuggerState, InvalidTransition> {
        let next = next_state(self.state, transition).ok_or(InvalidTransition {
            from: self.state,
            transition,
        })?;

        match transition {
            Transition::BuildStarted | Transition::LaunchRequested | Transition::Reset => {
                self.failure = None;
                self.exit_code = None;
            }
            Transition::ProgramExited => self.failure = None,
            _ => {}
        }

        self.state = next;
        Ok(next)
    }

    /// Applies a failing transition, recording why.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidTransition`] when the transition is not legal.
    pub fn fail(
        &mut self,
        transition: Transition,
        reason: impl Into<String>,
    ) -> Result<DebuggerState, InvalidTransition> {
        let next = self.apply(transition)?;
        self.failure = Some(reason.into());
        Ok(next)
    }

    /// Records that the program exited with `code`.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidTransition`] when the program was not running.
    pub fn exited(&mut self, code: Option<i32>) -> Result<DebuggerState, InvalidTransition> {
        let next = self.apply(Transition::ProgramExited)?;
        self.exit_code = code;
        Ok(next)
    }

    /// Forces the machine into [`DebuggerState::Failed`].
    ///
    /// Used when the GDB process dies unexpectedly, which can happen from any
    /// state and must always be representable — refusing the transition would
    /// leave the UI claiming a session that no longer exists.
    pub fn force_failed(&mut self, reason: impl Into<String>) {
        self.state = DebuggerState::Failed;
        self.failure = Some(reason.into());
    }
}

/// The state `transition` leads to, or `None` when it is not legal.
///
/// This function *is* the state diagram; everything else defers to it.
fn next_state(from: DebuggerState, transition: Transition) -> Option<DebuggerState> {
    use DebuggerState as S;
    use Transition as T;

    match (from, transition) {
        // Building can begin whenever no session holds the executable.
        (state, T::BuildStarted) if state.can_build() => Some(S::Building),
        (S::Building, T::BuildSucceeded) => Some(S::Ready),
        (S::Building, T::BuildFailed) => Some(S::Failed),

        // Launching.
        (state, T::LaunchRequested) if state.can_launch() => Some(S::Starting),
        (S::Starting, T::LaunchSucceeded) => Some(S::Running),
        (S::Starting, T::LaunchFailed) => Some(S::Failed),

        // GDB reports the first stop before the program has really run, so a
        // stop directly out of Starting is normal rather than an error.
        (S::Starting | S::Running, T::Stopped) => Some(S::Paused),
        (S::Paused, T::Resumed) => Some(S::Running),

        // A program can finish from any live state.
        (S::Starting | S::Running | S::Paused, T::ProgramExited) => Some(S::Exited),

        // A session can break at any point while it exists.
        (state, T::SessionFailed) if state.is_session_active() => Some(S::Failed),

        // Dismissing a finished or failed session returns to a usable state.
        (S::Exited | S::Failed, T::Reset) => Some(S::Idle),

        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use DebuggerState as S;
    use Transition as T;

    fn machine_in(state: S) -> StateMachine {
        StateMachine {
            state,
            failure: None,
            exit_code: None,
        }
    }

    #[test]
    fn a_new_machine_is_idle() {
        let machine = StateMachine::new();
        assert_eq!(machine.state(), S::Idle);
        assert!(machine.failure().is_none());
        assert!(machine.exit_code().is_none());
    }

    #[test]
    fn the_happy_path_runs_end_to_end() {
        let mut machine = StateMachine::new();
        assert_eq!(machine.apply(T::BuildStarted), Ok(S::Building));
        assert_eq!(machine.apply(T::BuildSucceeded), Ok(S::Ready));
        assert_eq!(machine.apply(T::LaunchRequested), Ok(S::Starting));
        assert_eq!(machine.apply(T::LaunchSucceeded), Ok(S::Running));
        assert_eq!(machine.apply(T::Stopped), Ok(S::Paused));
        assert_eq!(machine.apply(T::Resumed), Ok(S::Running));
        assert_eq!(machine.exited(Some(0)), Ok(S::Exited));
        assert_eq!(machine.exit_code(), Some(0));
    }

    #[test]
    fn stepping_repeatedly_alternates_between_running_and_paused() {
        let mut machine = machine_in(S::Paused);
        for _ in 0..5 {
            assert_eq!(machine.apply(T::Resumed), Ok(S::Running));
            assert_eq!(machine.apply(T::Stopped), Ok(S::Paused));
        }
    }

    #[test]
    fn a_stop_arriving_during_startup_is_accepted() {
        // GDB reports the initial stop before the program has really begun.
        let mut machine = machine_in(S::Starting);
        assert_eq!(machine.apply(T::Stopped), Ok(S::Paused));
    }

    #[test]
    fn stepping_a_program_that_is_not_paused_is_refused() {
        for state in [S::Idle, S::Building, S::Ready, S::Running, S::Exited] {
            assert!(!state.can_step(), "{state} must not allow stepping");
            let mut machine = machine_in(state);
            assert!(machine.apply(T::Resumed).is_err(), "{state}");
        }
    }

    #[test]
    fn an_invalid_transition_leaves_the_state_untouched() {
        let mut machine = machine_in(S::Idle);
        let error = machine.apply(T::Stopped).expect_err("must be refused");
        assert_eq!(error.from, S::Idle);
        assert_eq!(error.transition, T::Stopped);
        assert_eq!(machine.state(), S::Idle, "state must not have moved");
    }

    #[test]
    fn the_error_message_names_the_state_and_the_action() {
        let error = InvalidTransition {
            from: S::Running,
            transition: T::Stopped,
        };
        let text = error.to_string();
        assert!(text.contains("running"), "{text}");
        assert!(text.contains("program stopped"), "{text}");
    }

    #[test]
    fn building_is_refused_while_a_session_holds_the_executable() {
        // Rebuilding under a live session would replace the file GDB has open.
        for state in [S::Starting, S::Running, S::Paused] {
            assert!(!state.can_build(), "{state} must not allow a build");
            let mut machine = machine_in(state);
            assert!(machine.apply(T::BuildStarted).is_err(), "{state}");
        }
    }

    #[test]
    fn building_is_allowed_once_a_session_has_finished() {
        for state in [S::Idle, S::Ready, S::Exited, S::Failed] {
            assert!(state.can_build(), "{state} should allow a build");
            let mut machine = machine_in(state);
            assert_eq!(machine.apply(T::BuildStarted), Ok(S::Building));
        }
    }

    #[test]
    fn a_failed_build_records_its_reason() {
        let mut machine = StateMachine::new();
        machine.apply(T::BuildStarted).expect("build starts");
        machine
            .fail(T::BuildFailed, "nasm: 3 errors")
            .expect("build fails");
        assert_eq!(machine.state(), S::Failed);
        assert_eq!(machine.failure(), Some("nasm: 3 errors"));
    }

    #[test]
    fn starting_a_new_build_clears_the_previous_failure() {
        let mut machine = StateMachine::new();
        machine.apply(T::BuildStarted).expect("start");
        machine.fail(T::BuildFailed, "old error").expect("fail");
        machine.apply(T::BuildStarted).expect("rebuild");
        assert!(machine.failure().is_none(), "stale failure must be cleared");
    }

    #[test]
    fn a_failed_session_can_be_relaunched_after_a_reset() {
        let mut machine = machine_in(S::Failed);
        assert_eq!(machine.apply(T::Reset), Ok(S::Idle));
        assert_eq!(machine.apply(T::BuildStarted), Ok(S::Building));
    }

    #[test]
    fn a_finished_program_can_be_relaunched_without_rebuilding() {
        let mut machine = machine_in(S::Exited);
        assert!(machine.state().can_launch());
        assert_eq!(machine.apply(T::LaunchRequested), Ok(S::Starting));
    }

    #[test]
    fn the_program_can_exit_from_any_live_state() {
        for state in [S::Starting, S::Running, S::Paused] {
            let mut machine = machine_in(state);
            assert_eq!(machine.exited(Some(1)), Ok(S::Exited), "{state}");
        }
    }

    #[test]
    fn a_session_failure_is_accepted_only_while_a_session_exists() {
        for state in [S::Starting, S::Running, S::Paused] {
            let mut machine = machine_in(state);
            assert_eq!(machine.apply(T::SessionFailed), Ok(S::Failed), "{state}");
        }
        for state in [S::Idle, S::Ready, S::Building, S::Exited] {
            let mut machine = machine_in(state);
            assert!(machine.apply(T::SessionFailed).is_err(), "{state}");
        }
    }

    #[test]
    fn a_dead_debugger_can_always_be_recorded() {
        // The GDB process can die at any moment; the UI must never be left
        // claiming a session that no longer exists.
        for state in S::ALL {
            let mut machine = machine_in(state);
            machine.force_failed("gdb exited unexpectedly");
            assert_eq!(machine.state(), S::Failed, "from {state}");
            assert_eq!(machine.failure(), Some("gdb exited unexpectedly"));
        }
    }

    #[test]
    fn inspection_is_only_possible_while_paused() {
        for state in S::ALL {
            assert_eq!(
                state.can_inspect(),
                state == S::Paused,
                "{state} reported the wrong inspectability"
            );
        }
    }

    #[test]
    fn interruption_is_only_possible_while_running() {
        for state in S::ALL {
            assert_eq!(state.can_interrupt(), state == S::Running, "{state}");
        }
    }

    #[test]
    fn breakpoints_can_be_edited_except_while_the_program_is_moving() {
        assert!(S::Idle.can_edit_breakpoints(), "before a session");
        assert!(S::Paused.can_edit_breakpoints(), "while stopped");
        assert!(!S::Running.can_edit_breakpoints(), "while running");
        assert!(!S::Starting.can_edit_breakpoints(), "while starting");
    }

    #[test]
    fn allows_agrees_with_apply_for_every_pair() {
        // The query the UI uses to enable buttons must never disagree with
        // what actually happens.
        let transitions = [
            T::BuildStarted,
            T::BuildSucceeded,
            T::BuildFailed,
            T::LaunchRequested,
            T::LaunchSucceeded,
            T::LaunchFailed,
            T::Stopped,
            T::Resumed,
            T::ProgramExited,
            T::SessionFailed,
            T::Reset,
        ];
        for state in S::ALL {
            for transition in transitions {
                let machine = machine_in(state);
                let allowed = machine.allows(transition);
                let mut probe = machine_in(state);
                assert_eq!(
                    allowed,
                    probe.apply(transition).is_ok(),
                    "{state} + {transition:?} disagreed"
                );
            }
        }
    }

    #[test]
    fn every_state_is_reachable_from_idle() {
        // A state nothing can reach is dead code pretending to be a feature.
        let mut reached = vec![S::Idle];
        let transitions = [
            T::BuildStarted,
            T::BuildSucceeded,
            T::BuildFailed,
            T::LaunchRequested,
            T::LaunchSucceeded,
            T::LaunchFailed,
            T::Stopped,
            T::Resumed,
            T::ProgramExited,
            T::SessionFailed,
            T::Reset,
        ];

        let mut changed = true;
        while changed {
            changed = false;
            for state in reached.clone() {
                for transition in transitions {
                    if let Some(next) = next_state(state, transition) {
                        if !reached.contains(&next) {
                            reached.push(next);
                            changed = true;
                        }
                    }
                }
            }
        }

        for state in S::ALL {
            assert!(reached.contains(&state), "{state} is unreachable");
        }
    }

    #[test]
    fn state_labels_are_unique() {
        let mut labels: Vec<&str> = S::ALL.iter().map(|state| state.label()).collect();
        labels.sort_unstable();
        let count = labels.len();
        labels.dedup();
        assert_eq!(labels.len(), count);
    }
}