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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! Breakpoints, and keeping ratasm's view of them in step with GDB's.
//!
//! # Two sources of truth, reconciled
//!
//! The user sets breakpoints in the editor before a session exists; GDB
//! assigns them numbers only once it has loaded the program, and may move,
//! renumber or reject them. So a breakpoint here has an optional GDB number:
//! it is *pending* until a session adopts it, and pending breakpoints are
//! replayed when one starts.
//!
//! That is what makes it possible to set a breakpoint, build, and run without
//! the breakpoint quietly disappearing — a failure mode that is invisible
//! until the program blows past the line the user was waiting on.

use std::path::{Path, PathBuf};

use crate::debugger::mi::Value;

/// Where a breakpoint is placed.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Location {
    /// A one-based line in a source file.
    Line {
        /// The source file.
        file: PathBuf,
        /// The one-based line number.
        line: usize,
    },
    /// A raw address.
    Address(u64),
    /// A symbol name.
    Symbol(String),
    /// A watchpoint: stop when the expression's value changes.
    Watch(String),
}

impl Location {
    /// The location in the form GDB's `-break-insert` expects.
    pub fn to_gdb_location(&self) -> String {
        match self {
            Location::Line { file, line } => {
                let name = file
                    .file_name()
                    .map(|name| name.to_string_lossy().into_owned())
                    .unwrap_or_else(|| file.display().to_string());
                format!("{name}:{line}")
            }
            Location::Address(address) => format!("*0x{address:x}"),
            Location::Symbol(name) | Location::Watch(name) => name.clone(),
        }
    }

    /// A short rendering for the breakpoint list.
    pub fn display(&self) -> String {
        match self {
            Location::Line { file, line } => {
                let name = file
                    .file_name()
                    .map(|name| name.to_string_lossy().into_owned())
                    .unwrap_or_else(|| file.display().to_string());
                format!("{name}:{line}")
            }
            Location::Address(address) => format!("0x{address:x}"),
            Location::Symbol(name) => name.clone(),
            Location::Watch(expression) => format!("watch {expression}"),
        }
    }

    /// The source line this location refers to, if it names one.
    pub fn source_line(&self) -> Option<(&Path, usize)> {
        match self {
            Location::Line { file, line } => Some((file.as_path(), *line)),
            _ => None,
        }
    }
}

/// One breakpoint.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Breakpoint {
    /// GDB's number, once a session has adopted it.
    pub number: Option<u32>,
    /// Where it is placed.
    pub location: Location,
    /// Whether it is currently active.
    pub enabled: bool,
    /// The address GDB resolved it to, once known.
    pub address: Option<u64>,
    /// The function it landed in, once known.
    pub function: Option<String>,
    /// How many times it has been hit.
    pub hit_count: u32,
    /// Stop only when this expression is true.
    pub condition: Option<String>,
    /// The enabled state and condition GDB was last told.
    pub synced: Option<(bool, Option<String>)>,
}

impl Breakpoint {
    /// Creates a breakpoint that no session has adopted yet.
    pub fn pending(location: Location) -> Self {
        Self {
            number: None,
            location,
            enabled: true,
            address: None,
            function: None,
            hit_count: 0,
            condition: None,
            synced: None,
        }
    }

    /// Whether GDB has adopted this breakpoint.
    pub fn is_active(&self) -> bool {
        self.number.is_some()
    }

    /// A one-line description for the breakpoint list.
    pub fn describe(&self) -> String {
        let mut text = match self.number {
            Some(number) => format!("{number}: "),
            None => "-: ".to_owned(),
        };
        text.push_str(&self.location.display());
        if let Some(function) = &self.function {
            text.push_str(&format!(" in {function}"));
        }
        if let Some(condition) = &self.condition {
            text.push_str(&format!(" if {condition}"));
        }
        if !self.enabled {
            text.push_str(" (disabled)");
        }
        if self.hit_count > 0 {
            text.push_str(&format!(" hit {}", self.hit_count));
        }
        text
    }
}

/// The set of breakpoints the user has asked for.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct BreakpointSet {
    breakpoints: Vec<Breakpoint>,
    removed: Vec<u32>,
}

impl BreakpointSet {
    /// Creates an empty set.
    pub fn new() -> Self {
        Self::default()
    }

    /// Every breakpoint, in the order they were added.
    pub fn all(&self) -> &[Breakpoint] {
        &self.breakpoints
    }

    /// The number of breakpoints.
    pub fn len(&self) -> usize {
        self.breakpoints.len()
    }

    /// Whether there are no breakpoints.
    pub fn is_empty(&self) -> bool {
        self.breakpoints.is_empty()
    }

    /// The breakpoint at a source line, if there is one.
    pub fn at_line(&self, file: &Path, line: usize) -> Option<&Breakpoint> {
        self.breakpoints.iter().find(|breakpoint| {
            breakpoint
                .location
                .source_line()
                .is_some_and(|(path, number)| same_file(path, file) && number == line)
        })
    }

    /// Whether an enabled breakpoint sits on a source line.
    ///
    /// Used by the editor gutter, which shows disabled breakpoints with a
    /// different glyph rather than hiding them.
    pub fn is_set_at(&self, file: &Path, line: usize) -> bool {
        self.at_line(file, line).is_some()
    }

    /// Adds a breakpoint, or removes the one already at that line.
    ///
    /// Returns `true` when a breakpoint was added and `false` when one was
    /// removed, so the caller knows which GDB command to send.
    pub fn toggle_line(&mut self, file: &Path, line: usize) -> bool {
        if let Some(index) = self.index_of_line(file, line) {
            self.remove_index(index);
            false
        } else {
            self.breakpoints.push(Breakpoint::pending(Location::Line {
                file: file.to_path_buf(),
                line,
            }));
            true
        }
    }

    /// Adds a breakpoint at an address, if one is not already there.
    pub fn add_address(&mut self, address: u64) -> bool {
        if self
            .breakpoints
            .iter()
            .any(|breakpoint| breakpoint.location == Location::Address(address))
        {
            return false;
        }
        self.breakpoints
            .push(Breakpoint::pending(Location::Address(address)));
        true
    }

    /// Adds a watchpoint on `expression`, if there is not one already.
    pub fn add_watch(&mut self, expression: &str) -> bool {
        let location = Location::Watch(expression.trim().to_owned());
        if self
            .breakpoints
            .iter()
            .any(|breakpoint| breakpoint.location == location)
        {
            return false;
        }
        self.breakpoints.push(Breakpoint::pending(location));
        true
    }

    /// Sets or clears the condition of the breakpoint at `index`.
    pub fn set_condition(&mut self, index: usize, condition: Option<String>) -> bool {
        match self.breakpoints.get_mut(index) {
            Some(breakpoint) => {
                breakpoint.condition = condition.filter(|text| !text.trim().is_empty());
                true
            }
            None => false,
        }
    }

    /// Adds a breakpoint as it was saved.
    pub fn restore(&mut self, location: Location, enabled: bool, condition: Option<String>) {
        if self
            .breakpoints
            .iter()
            .any(|breakpoint| breakpoint.location == location)
        {
            return;
        }
        let mut breakpoint = Breakpoint::pending(location);
        breakpoint.enabled = enabled;
        breakpoint.condition = condition;
        self.breakpoints.push(breakpoint);
    }

    /// Moves breakpoints in `from` to `to`, or drops them when `to` is `None`.
    pub fn move_file(&mut self, from: &Path, to: Option<&Path>) {
        let mut index = 0;
        while index < self.breakpoints.len() {
            let here = matches!(&self.breakpoints[index].location, Location::Line { file, .. } if same_file(file, from));
            match (here, to) {
                (true, None) => {
                    self.remove_index(index);
                    continue;
                }
                (true, Some(to)) => {
                    if let Location::Line { file, .. } = &mut self.breakpoints[index].location {
                        *file = to.to_path_buf();
                    }
                }
                _ => {}
            }
            index += 1;
        }
    }

    /// GDB numbers of breakpoints removed since GDB was last told.
    pub fn take_removed(&mut self) -> Vec<u32> {
        std::mem::take(&mut self.removed)
    }

    /// Adopted breakpoints whose enabled state or condition GDB has not been told.
    pub fn out_of_sync(&self) -> Vec<(u32, bool, Option<String>)> {
        self.breakpoints
            .iter()
            .filter_map(|breakpoint| {
                let number = breakpoint.number?;
                let wanted = (breakpoint.enabled, breakpoint.condition.clone());
                (breakpoint.synced.as_ref() != Some(&wanted))
                    .then_some((number, wanted.0, wanted.1))
            })
            .collect()
    }

    /// Records that GDB now holds `enabled` and `condition` for breakpoint `number`.
    pub fn mark_synced(&mut self, number: u32, enabled: bool, condition: Option<String>) {
        if let Some(breakpoint) = self
            .breakpoints
            .iter_mut()
            .find(|breakpoint| breakpoint.number == Some(number))
        {
            breakpoint.synced = Some((enabled, condition));
        }
    }

    /// Adds a breakpoint at a symbol, if one is not already there.
    pub fn add_symbol(&mut self, name: &str) -> bool {
        let location = Location::Symbol(name.to_owned());
        if self
            .breakpoints
            .iter()
            .any(|breakpoint| breakpoint.location == location)
        {
            return false;
        }
        self.breakpoints.push(Breakpoint::pending(location));
        true
    }

    /// Removes the breakpoint with a GDB number.
    pub fn remove_number(&mut self, number: u32) -> bool {
        match self
            .breakpoints
            .iter()
            .position(|breakpoint| breakpoint.number == Some(number))
        {
            Some(index) => self.remove_index(index).is_some(),
            None => false,
        }
    }

    /// Removes the breakpoint at an index in the list.
    pub fn remove_index(&mut self, index: usize) -> Option<Breakpoint> {
        if index >= self.breakpoints.len() {
            return None;
        }
        let breakpoint = self.breakpoints.remove(index);
        self.removed.extend(breakpoint.number);
        Some(breakpoint)
    }

    /// Enables or disables a breakpoint by index.
    pub fn set_enabled(&mut self, index: usize, enabled: bool) -> bool {
        match self.breakpoints.get_mut(index) {
            Some(breakpoint) => {
                breakpoint.enabled = enabled;
                true
            }
            None => false,
        }
    }

    /// Removes every breakpoint.
    pub fn clear(&mut self) {
        self.removed.extend(
            self.breakpoints
                .drain(..)
                .filter_map(|breakpoint| breakpoint.number),
        );
    }

    /// Forgets every GDB number, marking all breakpoints pending again.
    ///
    /// Called when a session ends: the numbers belonged to that session and
    /// mean nothing to the next one.
    pub fn detach(&mut self) {
        for breakpoint in &mut self.breakpoints {
            breakpoint.number = None;
            breakpoint.address = None;
            breakpoint.hit_count = 0;
            breakpoint.synced = None;
        }
        self.removed.clear();
    }

    /// The breakpoints that still need to be sent to GDB.
    pub fn pending(&self) -> Vec<&Breakpoint> {
        self.breakpoints
            .iter()
            .filter(|breakpoint| breakpoint.number.is_none())
            .collect()
    }

    /// Records the number and resolved details GDB assigned to a location.
    ///
    /// Matching is by location rather than by order, because GDB may reject
    /// one breakpoint and still accept later ones.
    pub fn adopt(&mut self, location: &Location, record: &Value) -> bool {
        let Some(index) = self
            .breakpoints
            .iter()
            .position(|breakpoint| &breakpoint.location == location)
        else {
            return false;
        };

        let breakpoint = &mut self.breakpoints[index];
        breakpoint.number = record.get_int("number").and_then(|n| u32::try_from(n).ok());
        breakpoint.address = record.get_address("addr");
        breakpoint.function = record.get_str("func").map(str::to_owned);
        let enabled = record.get_str("enabled") != Some("n");
        let condition = record.get_str("cond").map(str::to_owned);
        breakpoint.synced = Some((enabled, condition));
        true
    }

    /// Records a hit on the breakpoint GDB reported.
    pub fn record_hit(&mut self, number: u32) {
        if let Some(breakpoint) = self
            .breakpoints
            .iter_mut()
            .find(|breakpoint| breakpoint.number == Some(number))
        {
            breakpoint.hit_count = breakpoint.hit_count.saturating_add(1);
        }
    }

    fn index_of_line(&self, file: &Path, line: usize) -> Option<usize> {
        self.breakpoints.iter().position(|breakpoint| {
            breakpoint
                .location
                .source_line()
                .is_some_and(|(path, number)| same_file(path, file) && number == line)
        })
    }
}

/// Whether two paths refer to the same source file.
///
/// GDB reports the file name it was given, which may be relative where the
/// editor holds an absolute path. Comparing the file names as a fallback keeps
/// breakpoints attached in the common single-directory project; comparing only
/// the full paths would silently fail to match.
fn same_file(left: &Path, right: &Path) -> bool {
    if left == right {
        return true;
    }
    match (left.file_name(), right.file_name()) {
        (Some(left_name), Some(right_name)) => left_name == right_name,
        _ => false,
    }
}

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

    fn main_asm() -> PathBuf {
        PathBuf::from("/project/src/main.asm")
    }

    #[test]
    fn a_new_set_is_empty() {
        let set = BreakpointSet::new();
        assert!(set.is_empty());
        assert_eq!(set.len(), 0);
    }

    #[test]
    fn toggling_adds_then_removes() {
        let mut set = BreakpointSet::new();
        assert!(set.toggle_line(&main_asm(), 12), "first toggle adds");
        assert_eq!(set.len(), 1);
        assert!(set.is_set_at(&main_asm(), 12));

        assert!(!set.toggle_line(&main_asm(), 12), "second toggle removes");
        assert!(set.is_empty());
        assert!(!set.is_set_at(&main_asm(), 12));
    }

    #[test]
    fn breakpoints_on_different_lines_coexist() {
        let mut set = BreakpointSet::new();
        set.toggle_line(&main_asm(), 5);
        set.toggle_line(&main_asm(), 12);
        assert_eq!(set.len(), 2);
        assert!(set.is_set_at(&main_asm(), 5));
        assert!(set.is_set_at(&main_asm(), 12));
        assert!(!set.is_set_at(&main_asm(), 7));
    }

    #[test]
    fn a_relative_path_matches_an_absolute_one() {
        // GDB reports "main.asm" where the editor holds the full path.
        let mut set = BreakpointSet::new();
        set.toggle_line(&main_asm(), 9);
        assert!(set.is_set_at(Path::new("main.asm"), 9));
        assert!(!set.is_set_at(Path::new("other.asm"), 9));
    }

    #[test]
    fn a_new_breakpoint_starts_pending_and_enabled() {
        let mut set = BreakpointSet::new();
        set.toggle_line(&main_asm(), 3);
        let breakpoint = &set.all()[0];
        assert!(!breakpoint.is_active(), "no session has adopted it");
        assert!(breakpoint.enabled);
        assert_eq!(breakpoint.hit_count, 0);
        assert_eq!(set.pending().len(), 1);
    }

    #[test]
    fn adopting_records_the_details_gdb_resolved() {
        let mut set = BreakpointSet::new();
        let location = Location::Line {
            file: main_asm(),
            line: 9,
        };
        set.toggle_line(&main_asm(), 9);

        let record = Value::Tuple(vec![
            ("number".to_owned(), Value::String("1".to_owned())),
            ("enabled".to_owned(), Value::String("y".to_owned())),
            ("addr".to_owned(), Value::String("0x004000b0".to_owned())),
            ("func".to_owned(), Value::String("_start".to_owned())),
        ]);
        assert!(set.adopt(&location, &record));

        let breakpoint = &set.all()[0];
        assert_eq!(breakpoint.number, Some(1));
        assert_eq!(breakpoint.address, Some(0x0040_00b0));
        assert_eq!(breakpoint.function.as_deref(), Some("_start"));
        assert!(breakpoint.is_active());
        assert!(set.pending().is_empty());
    }

    #[test]
    fn adopting_an_unknown_location_changes_nothing() {
        let mut set = BreakpointSet::new();
        set.toggle_line(&main_asm(), 9);
        let elsewhere = Location::Line {
            file: PathBuf::from("other.asm"),
            line: 1,
        };
        assert!(!set.adopt(&elsewhere, &Value::Tuple(Vec::new())));
        assert!(!set.all()[0].is_active());
    }

    #[test]
    fn what_gdb_holds_is_compared_with_what_was_asked_for() {
        let mut set = BreakpointSet::new();
        let location = Location::Symbol("_start".to_owned());
        set.add_symbol("_start");
        let record = Value::Tuple(vec![
            ("number".to_owned(), Value::String("1".to_owned())),
            ("enabled".to_owned(), Value::String("n".to_owned())),
        ]);
        set.adopt(&location, &record);
        assert_eq!(set.out_of_sync(), vec![(1, true, None)]);
        set.mark_synced(1, true, None);
        assert!(set.out_of_sync().is_empty());

        set.set_enabled(0, false);
        set.set_condition(0, Some("$rax == 1".into()));
        assert_eq!(
            set.out_of_sync(),
            vec![(1, false, Some("$rax == 1".into()))]
        );
        assert!(set.all()[0].describe().contains("disabled"));
        assert!(set.all()[0].describe().contains("if $rax == 1"));

        set.clear();
        assert_eq!(set.take_removed(), vec![1]);
        assert!(set.take_removed().is_empty());
    }

    #[test]
    fn watchpoints_and_moved_files() {
        let mut set = BreakpointSet::new();
        assert!(set.add_watch(" $rax "));
        assert!(!set.add_watch("$rax"));
        assert_eq!(set.all()[0].location.display(), "watch $rax");
        set.toggle_line(Path::new("/p/a.asm"), 3);
        set.move_file(Path::new("/p/a.asm"), Some(Path::new("/p/b.asm")));
        assert!(set.is_set_at(Path::new("/p/b.asm"), 3));
        set.move_file(Path::new("/p/b.asm"), None);
        assert_eq!(set.len(), 1);
    }

    #[test]
    fn ending_a_session_returns_every_breakpoint_to_pending() {
        // GDB's numbers belong to one session and must not be reused.
        let mut set = BreakpointSet::new();
        set.toggle_line(&main_asm(), 9);
        set.adopt(
            &Location::Line {
                file: main_asm(),
                line: 9,
            },
            &Value::Tuple(vec![("number".to_owned(), Value::String("1".to_owned()))]),
        );
        set.record_hit(1);
        assert_eq!(set.all()[0].hit_count, 1);

        set.detach();
        let breakpoint = &set.all()[0];
        assert!(!breakpoint.is_active());
        assert_eq!(breakpoint.hit_count, 0);
        assert_eq!(breakpoint.address, None);
        assert_eq!(set.pending().len(), 1, "it must be replayed next time");
    }

    #[test]
    fn hits_are_counted_for_the_right_breakpoint() {
        let mut set = BreakpointSet::new();
        set.add_symbol("first");
        set.add_symbol("second");
        set.adopt(
            &Location::Symbol("first".to_owned()),
            &Value::Tuple(vec![("number".to_owned(), Value::String("1".to_owned()))]),
        );
        set.adopt(
            &Location::Symbol("second".to_owned()),
            &Value::Tuple(vec![("number".to_owned(), Value::String("2".to_owned()))]),
        );

        set.record_hit(2);
        set.record_hit(2);
        assert_eq!(set.all()[0].hit_count, 0);
        assert_eq!(set.all()[1].hit_count, 2);
    }

    #[test]
    fn recording_a_hit_on_an_unknown_number_is_ignored() {
        let mut set = BreakpointSet::new();
        set.add_symbol("_start");
        set.record_hit(99);
        assert_eq!(set.all()[0].hit_count, 0);
    }

    #[test]
    fn address_and_symbol_breakpoints_are_not_duplicated() {
        let mut set = BreakpointSet::new();
        assert!(set.add_address(0x4000));
        assert!(!set.add_address(0x4000), "the same address twice");
        assert!(set.add_address(0x4008));
        assert!(set.add_symbol("_start"));
        assert!(!set.add_symbol("_start"));
        assert_eq!(set.len(), 3);
    }

    #[test]
    fn gdb_locations_use_the_expected_syntax() {
        assert_eq!(
            Location::Line {
                file: main_asm(),
                line: 12
            }
            .to_gdb_location(),
            "main.asm:12"
        );
        assert_eq!(Location::Address(0x4000b0).to_gdb_location(), "*0x4000b0");
        assert_eq!(
            Location::Symbol("_start".into()).to_gdb_location(),
            "_start"
        );
    }

    #[test]
    fn removing_by_number_and_index_both_work() {
        let mut set = BreakpointSet::new();
        set.add_symbol("a");
        set.add_symbol("b");
        set.adopt(
            &Location::Symbol("a".to_owned()),
            &Value::Tuple(vec![("number".to_owned(), Value::String("7".to_owned()))]),
        );

        assert!(set.remove_number(7));
        assert_eq!(set.len(), 1);
        assert!(!set.remove_number(7), "already gone");

        assert!(set.remove_index(0).is_some());
        assert!(set.is_empty());
        assert!(set.remove_index(0).is_none());
    }

    #[test]
    fn enabling_and_disabling_by_index_is_bounds_checked() {
        let mut set = BreakpointSet::new();
        set.add_symbol("_start");
        assert!(set.set_enabled(0, false));
        assert!(!set.all()[0].enabled);
        assert!(!set.set_enabled(9, true), "out of range is refused");
    }

    #[test]
    fn clearing_removes_everything() {
        let mut set = BreakpointSet::new();
        set.add_symbol("a");
        set.toggle_line(&main_asm(), 1);
        set.clear();
        assert!(set.is_empty());
    }

    #[test]
    fn descriptions_are_readable_in_every_state() {
        let mut set = BreakpointSet::new();
        set.toggle_line(&main_asm(), 9);
        assert_eq!(set.all()[0].describe(), "-: main.asm:9");

        set.adopt(
            &Location::Line {
                file: main_asm(),
                line: 9,
            },
            &Value::Tuple(vec![
                ("number".to_owned(), Value::String("1".to_owned())),
                ("func".to_owned(), Value::String("_start".to_owned())),
            ]),
        );
        set.record_hit(1);
        assert_eq!(set.all()[0].describe(), "1: main.asm:9 in _start hit 1");
    }
}