miden-debug-engine 0.15.0

Core debugger engine for miden-debug
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
use alloc::{
    borrow::Cow,
    boxed::Box,
    collections::{BTreeMap, BTreeSet, VecDeque},
    string::{String, ToString},
    sync::Arc,
    vec::Vec,
};
use core::{cell::OnceCell, fmt};
#[cfg(feature = "std")]
use std::path::{Path, PathBuf};

use miden_core::operations::AssemblyOp;
use miden_debug_types::{Location, SourceFile, SourceManager, SourceSpan, Uri};
use miden_mast_package::debug_info::{DebugSourceInlineCall, DebugSourceNodeId, PackageDebugInfo};
use miden_processor::{ContextId, SourceInlineCallContext, operation::Operation, trace::RowIndex};
use miden_utils_sync::RwLock;

use crate::Event;

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ControlFlowOp {
    Span,
    Respan,
    Join,
    Split,
    End,
}

pub struct StepInfo<'a> {
    pub op: Option<Operation>,
    pub control: Option<ControlFlowOp>,
    pub asmop: Option<&'a AssemblyOp>,
    pub clk: RowIndex,
    pub ctx: ContextId,
    pub inline_frames: &'a [InlineCallFrame],
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InlineCallFrame {
    name: Arc<str>,
    call_site: Location,
}

impl InlineCallFrame {
    #[cfg(all(test, feature = "dap"))]
    pub(crate) fn new_for_test(name: impl Into<Arc<str>>, call_site: Location) -> Self {
        Self {
            name: name.into(),
            call_site,
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn call_site(&self) -> &Location {
        &self.call_site
    }

    pub fn display_name(&self) -> String {
        demangle(&self.name)
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum LogicalFrameKind {
    Physical,
    Inline,
}

#[derive(Debug, Clone)]
enum LogicalFrameLocation {
    Assembly(Location),
    Resolved(ResolvedLocation),
}

#[derive(Debug, Clone)]
pub struct LogicalStackFrame {
    name: Arc<str>,
    kind: LogicalFrameKind,
    location: Option<LogicalFrameLocation>,
    physical_index: usize,
}

impl LogicalStackFrame {
    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn kind(&self) -> LogicalFrameKind {
        self.kind
    }

    pub fn physical_index(&self) -> usize {
        self.physical_index
    }

    pub fn display_name(&self) -> String {
        match self.kind {
            LogicalFrameKind::Physical => self.name.to_string(),
            LogicalFrameKind::Inline => format!("[inlined] {}", self.name),
        }
    }

    pub fn resolved(&self, source_manager: &dyn SourceManager) -> Option<ResolvedLocation> {
        match self.location.as_ref()? {
            LogicalFrameLocation::Assembly(location) => {
                resolve_assembly_location(source_manager, location)
            }
            LogicalFrameLocation::Resolved(resolved) => Some(resolved.clone()),
        }
    }
}

/// Resolves the inline frames active for an operation.
///
/// Rows owned by the current package come first. Contexts inherited across dynamic/external
/// package boundaries follow in the VM-provided innermost-to-outermost order.
pub fn inline_frames_for_operation<'a>(
    current: Option<(&PackageDebugInfo, DebugSourceNodeId, u32)>,
    inherited: impl IntoIterator<Item = &'a SourceInlineCallContext>,
) -> Vec<InlineCallFrame> {
    let mut frames = Vec::new();
    if let Some((debug_info, source_node, op_idx)) = current {
        append_inline_frames(
            &mut frames,
            debug_info,
            debug_info.inline_calls_for_operation(source_node, op_idx),
        );
    }
    for context in inherited {
        append_inline_frames(&mut frames, context.debug_info(), context.inline_calls());
    }
    frames
}

fn append_inline_frames<'a>(
    frames: &mut Vec<InlineCallFrame>,
    debug_info: &PackageDebugInfo,
    rows: impl IntoIterator<Item = &'a DebugSourceInlineCall>,
) {
    frames.extend(rows.into_iter().filter_map(|row| {
        let function = debug_info.get_function(row.callee_idx)?;
        let name = debug_info.get_string(function.name_idx)?;
        let call_site = debug_info.get_location(row.loc_idx)?;
        Some(InlineCallFrame { name, call_site })
    }));
}

#[derive(Debug, Clone)]
struct SpanContext {
    frame_index: usize,
    location: Option<Location>,
}

pub struct CallStack {
    events: Arc<RwLock<BTreeMap<RowIndex, Event>>>,
    contexts: BTreeSet<Arc<str>>,
    frames: Vec<CallFrame>,
    block_stack: Vec<Option<SpanContext>>,
}
impl CallStack {
    pub fn new(events: Arc<RwLock<BTreeMap<RowIndex, Event>>>) -> Self {
        Self {
            events,
            contexts: BTreeSet::default(),
            frames: vec![],
            block_stack: vec![],
        }
    }

    /// Build a [CallStack] from pre-built frames — used in DAP client mode.
    #[cfg(feature = "dap")]
    pub fn from_remote_frames(frames: Vec<CallFrame>) -> Self {
        Self {
            events: Arc::new(Default::default()),
            contexts: BTreeSet::default(),
            frames,
            block_stack: vec![],
        }
    }

    pub fn stacktrace<'a>(
        &'a self,
        recent: &'a VecDeque<Operation>,
        source_manager: &'a dyn SourceManager,
    ) -> StackTrace<'a> {
        StackTrace::new(self, recent, source_manager)
    }

    pub fn current_frame(&self) -> Option<&CallFrame> {
        self.frames.last()
    }

    pub fn current_frame_mut(&mut self) -> Option<&mut CallFrame> {
        self.frames.last_mut()
    }

    pub fn frames(&self) -> &[CallFrame] {
        self.frames.as_slice()
    }

    pub fn logical_frames(&self, strip_prefix: &str) -> Vec<LogicalStackFrame> {
        let mut logical = Vec::new();
        for (physical_index, frame) in self.frames.iter().enumerate() {
            let current_location = frame.last_logical_location();
            let location = frame
                .inline_frames
                .last()
                .map(|inline| LogicalFrameLocation::Assembly(inline.call_site.clone()))
                .or_else(|| current_location.clone());
            logical.push(LogicalStackFrame {
                name: frame.procedure(strip_prefix).unwrap_or_else(|| Arc::from("<unknown>")),
                kind: LogicalFrameKind::Physical,
                location,
                physical_index,
            });

            for inline_index in (0..frame.inline_frames.len()).rev() {
                let inline = &frame.inline_frames[inline_index];
                let location = if inline_index == 0 {
                    current_location.clone()
                } else {
                    Some(LogicalFrameLocation::Assembly(
                        frame.inline_frames[inline_index - 1].call_site.clone(),
                    ))
                };
                logical.push(LogicalStackFrame {
                    name: Arc::from(inline.display_name().into_boxed_str()),
                    kind: LogicalFrameKind::Inline,
                    location,
                    physical_index,
                });
            }
        }
        logical
    }

    /// Updates the call stack from `info`
    ///
    /// Returns the call frame exited this cycle, if any
    pub fn next(&mut self, info: &StepInfo<'_>) -> Option<CallFrame> {
        let procedure = info.asmop.map(|op| self.cache_procedure_name(op.context_name()));

        let event = {
            let mut events = self.events.write();
            match events.first_key_value() {
                Some((clk, _)) if *clk <= info.clk => events.pop_first().map(|(_, event)| event),
                _ => None,
            }
        };
        log::trace!("handling {:?}/{:?} at cycle {}: {:?}", info.control, info.op, info.clk, event);
        let is_frame_start = event.as_ref().is_some_and(|event| event.is_frame_start());
        let is_frame_end = event.as_ref().is_some_and(|event| event.is_frame_end());
        let popped_frame = self.handle_event(event, procedure.clone(), info.op, info.asmop);

        match info.control {
            Some(ControlFlowOp::Span) => {
                if let Some(asmop) = info.asmop {
                    log::debug!("{asmop:#?}");
                    self.block_stack.push(Some(SpanContext {
                        frame_index: self.frames.len().saturating_sub(1),
                        location: asmop.location().cloned(),
                    }));
                } else {
                    self.block_stack.push(None);
                }
            }
            Some(ControlFlowOp::Join | ControlFlowOp::Split) => {
                self.block_stack.push(None);
            }
            Some(ControlFlowOp::End) => {
                self.block_stack.pop();
            }
            Some(ControlFlowOp::Respan) | None => {}
        }

        if !is_frame_end {
            if self.frames.is_empty() {
                self.frames.push(CallFrame::new(procedure.clone()));
            }
            self.frames.last_mut().unwrap().inline_frames = info.inline_frames.to_vec();
            self.update_current_procedure(procedure.clone());
        }

        if is_frame_start || is_frame_end {
            return popped_frame;
        }

        let Some(op) = info.op else {
            return popped_frame;
        };

        // Attempt to supply procedure context from the current span context, if needed +
        // available
        let (procedure, asmop) = match procedure {
            proc @ Some(_) => (proc, info.asmop.map(Cow::Borrowed)),
            None => match self.block_stack.last() {
                Some(Some(span_ctx)) => {
                    let proc =
                        self.frames.get(span_ctx.frame_index).and_then(|f| f.procedure.clone());
                    let asmop_cow = info.asmop.map(Cow::Borrowed).or_else(|| {
                        let context_name = proc.as_deref().unwrap_or("<unknown>").to_string();
                        let raw_asmop = AssemblyOp::new(
                            span_ctx.location.clone(),
                            context_name,
                            1,
                            op.to_string(),
                        );
                        Some(Cow::Owned(raw_asmop))
                    });
                    (proc, asmop_cow)
                }
                _ => (None, info.asmop.map(Cow::Borrowed)),
            },
        };

        // Use the current frame's procedure context, if no other more precise context is
        // available
        let procedure = procedure.or_else(|| self.frames.last().and_then(|f| f.procedure.clone()));

        // `exec` changes procedure context without creating a physical frame. Keep the physical
        // frame synchronized with the best context available for the current operation.
        self.update_current_procedure(procedure);
        let current_frame = self.frames.last_mut().unwrap();

        // Push op into call frame if this is any op other than `nop` or frame setup
        if !matches!(op, Operation::Noop) {
            let cycle_idx = info.asmop.map(|a| a.num_cycles()).unwrap_or(1);
            current_frame.push(op, cycle_idx, asmop.as_deref());
        }

        popped_frame
    }

    fn update_current_procedure(&mut self, procedure: Option<Arc<str>>) {
        let context_initialized = self
            .frames
            .last_mut()
            .is_some_and(|frame| frame.update_procedure(procedure.clone()));
        let num_frames = self.frames.len();
        if context_initialized && num_frames > 1 {
            let caller_frame = &mut self.frames[num_frames - 2];
            if let Some(OpDetail::Exec { callee }) = caller_frame.context.back_mut()
                && callee.is_none()
            {
                *callee = procedure;
            }
        }
    }

    // Get or cache procedure name/context as `Arc<str>`
    fn cache_procedure_name(&mut self, context_name: &str) -> Arc<str> {
        match self.contexts.get(context_name) {
            Some(name) => Arc::clone(name),
            None => {
                let name = Arc::from(context_name.to_string().into_boxed_str());
                self.contexts.insert(Arc::clone(&name));
                name
            }
        }
    }

    fn handle_event(
        &mut self,
        event: Option<Event>,
        procedure: Option<Arc<str>>,
        op: Option<Operation>,
        asmop: Option<&AssemblyOp>,
    ) -> Option<CallFrame> {
        // Do we need to handle any frame events?
        match event? {
            Event::FrameStart => {
                // Record the fact that we exec'd a new procedure in the op context
                if let Some(current_frame) = self.frames.last_mut() {
                    current_frame.push_exec(procedure.clone());
                }
                // The event is emitted at the start of the callee.
                let mut frame = CallFrame::new(procedure);
                if let Some(op) = op {
                    frame.push(op, 0, asmop);
                }
                self.frames.push(frame);
            }
            Event::Unknown(code) => log::debug!("unknown trace event: {code}"),
            Event::FrameEnd => {
                return self.frames.pop();
            }
            _ => (),
        }
        None
    }
}

pub struct CallFrame {
    procedure: Option<Arc<str>>,
    context: VecDeque<OpDetail>,
    display_name: OnceCell<Arc<str>>,
    finishing: bool,
    inline_frames: Vec<InlineCallFrame>,
}
impl CallFrame {
    pub fn new(procedure: Option<Arc<str>>) -> Self {
        Self {
            procedure,
            context: Default::default(),
            display_name: Default::default(),
            finishing: false,
            inline_frames: Vec::new(),
        }
    }

    /// Build a frame from remote (DAP) data — used in DAP client mode.
    ///
    /// The frame stores the procedure name and an optional [ResolvedLocation]
    /// as a pre-resolved `OpDetail::Full` entry so that `last_resolved()` and
    /// `recent()` work correctly for pane rendering.
    #[cfg(feature = "dap")]
    pub fn from_remote(procedure: Option<Arc<str>>, resolved: Option<ResolvedLocation>) -> Self {
        let mut context = VecDeque::new();
        if let Some(loc) = resolved {
            let cell = OnceCell::new();
            cell.set(Some(loc)).ok();
            context.push_back(OpDetail::Full {
                op: miden_processor::operation::Operation::Noop,
                location: None,
                resolved: cell,
            });
        }
        Self {
            procedure,
            context,
            display_name: Default::default(),
            finishing: false,
            inline_frames: Vec::new(),
        }
    }

    pub fn procedure(&self, strip_prefix: &str) -> Option<Arc<str>> {
        self.procedure.as_ref()?;
        let name = self.display_name.get_or_init(|| {
            let name = self.procedure.as_deref().unwrap();
            let name = match name.split_once("::") {
                Some((module, rest)) if module == strip_prefix => demangle(rest),
                _ => demangle(name),
            };
            Arc::<str>::from(name.into_boxed_str())
        });
        Some(Arc::clone(name))
    }

    /// Update this physical frame's procedure, returning true only when the context was first
    /// initialized. Later changes arise from `exec` and must invalidate the cached display name,
    /// but must not rewrite the caller's recorded callee.
    fn update_procedure(&mut self, procedure: Option<Arc<str>>) -> bool {
        let Some(procedure) = procedure else {
            return false;
        };
        if self.procedure.as_ref() == Some(&procedure) {
            return false;
        }

        let initialized = self.procedure.is_none();
        self.procedure = Some(procedure);
        self.display_name.take();
        initialized
    }

    pub fn push_exec(&mut self, callee: Option<Arc<str>>) {
        if self.context.len() == 5 {
            self.context.pop_front();
        }

        self.context.push_back(OpDetail::Exec { callee });
    }

    pub fn push(&mut self, opcode: Operation, cycle_idx: u8, op: Option<&AssemblyOp>) {
        if cycle_idx > 1 {
            // Should we ignore this op?
            let skip = self.context.back().map(|detail| matches!(detail, OpDetail::Full { op, .. } | OpDetail::Basic { op } if op == &opcode)).unwrap_or(false);
            if skip {
                return;
            }
        }

        if self.context.len() == 5 {
            self.context.pop_front();
        }

        match op {
            Some(op) => {
                let location = op.location().cloned();
                self.context.push_back(OpDetail::Full {
                    op: opcode,
                    location,
                    resolved: Default::default(),
                });
            }
            None => {
                // If this instruction does not have a location, inherit the location
                // of the previous op in the frame, if one is present
                if let Some(loc) = self.context.back().map(|op| op.location().cloned()) {
                    self.context.push_back(OpDetail::Full {
                        op: opcode,
                        location: loc,
                        resolved: Default::default(),
                    });
                } else {
                    self.context.push_back(OpDetail::Basic { op: opcode });
                }
            }
        }
    }

    pub fn last_location(&self) -> Option<&Location> {
        self.context.iter().rev().find_map(OpDetail::location)
    }

    fn last_logical_location(&self) -> Option<LogicalFrameLocation> {
        self.context.iter().rev().find_map(|detail| {
            detail
                .location()
                .cloned()
                .map(LogicalFrameLocation::Assembly)
                .or_else(|| detail.cached_resolved().cloned().map(LogicalFrameLocation::Resolved))
        })
    }

    pub fn last_resolved(&self, source_manager: &dyn SourceManager) -> Option<&ResolvedLocation> {
        // Search through context in reverse order to find the most recent op with a resolvable
        // location.
        for op in self.context.iter().rev() {
            if let Some(resolved) = op.resolve(source_manager) {
                return Some(resolved);
            }
        }
        None
    }

    pub fn recent(&self) -> &VecDeque<OpDetail> {
        &self.context
    }

    #[inline(always)]
    pub fn should_break_on_exit(&self) -> bool {
        self.finishing
    }

    #[inline(always)]
    pub fn break_on_exit(&mut self) {
        self.finishing = true;
    }
}

#[derive(Debug, Clone)]
pub enum OpDetail {
    Full {
        op: Operation,
        location: Option<Location>,
        resolved: OnceCell<Option<ResolvedLocation>>,
    },
    Exec {
        callee: Option<Arc<str>>,
    },
    Basic {
        op: Operation,
    },
}
impl OpDetail {
    pub fn callee(&self, strip_prefix: &str) -> Option<Box<str>> {
        match self {
            Self::Exec { callee: None } => Some(Box::from("<unknown>")),
            Self::Exec {
                callee: Some(callee),
            } => {
                let name = match callee.split_once("::") {
                    Some((module, rest)) if module == strip_prefix => demangle(rest),
                    _ => demangle(callee),
                };
                Some(name.into_boxed_str())
            }
            _ => None,
        }
    }

    pub fn display(&self) -> String {
        match self {
            Self::Full { op, .. } | Self::Basic { op } => format!("{op}"),
            Self::Exec {
                callee: Some(callee),
            } => format!("exec.{callee}"),
            Self::Exec { callee: None } => "exec.<unavailable>".to_string(),
        }
    }

    pub fn opcode(&self) -> Operation {
        match self {
            Self::Full { op, .. } | Self::Basic { op } => *op,
            Self::Exec { .. } => panic!("no opcode associated with execs"),
        }
    }

    pub fn location(&self) -> Option<&Location> {
        match self {
            Self::Full { location, .. } => location.as_ref(),
            Self::Basic { .. } | Self::Exec { .. } => None,
        }
    }

    pub fn resolve(&self, source_manager: &dyn SourceManager) -> Option<&ResolvedLocation> {
        match self {
            Self::Full {
                location, resolved, ..
            } => {
                if let Some(cached) = resolved.get() {
                    return cached.as_ref();
                }
                let loc = location.as_ref()?;
                resolved
                    .get_or_init(|| {
                        let source_file = resolve_source_file_for_location(source_manager, loc)?;
                        let span = SourceSpan::new(source_file.id(), loc.start..loc.end);
                        let file_line_col = source_file.location(span);
                        Some(ResolvedLocation {
                            source_file,
                            line: file_line_col.line.to_u32(),
                            col: file_line_col.column.to_u32(),
                            span,
                        })
                    })
                    .as_ref()
            }
            _ => None,
        }
    }

    fn cached_resolved(&self) -> Option<&ResolvedLocation> {
        match self {
            Self::Full { resolved, .. } => resolved.get().and_then(Option::as_ref),
            Self::Exec { .. } | Self::Basic { .. } => None,
        }
    }
}

/// Resolve a source file for `location`.
///
/// Compiled packages may contain remapped paths such as `src/lib.rs`, while sources loaded by the
/// VM host may be keyed by an absolute path, or may not be loaded yet at all. Prefer the source
/// manager's existing URI table, then fall back to loading the file from disk.
#[cfg(feature = "std")]
pub fn resolve_source_file_for_location(
    source_manager: &dyn SourceManager,
    location: &Location,
) -> Option<Arc<SourceFile>> {
    use miden_assembly_syntax::debuginfo::SourceManagerExt;
    source_manager.get_by_uri(location.uri()).or_else(|| {
        resolve_source_path(location.uri()).and_then(|path| source_manager.load_file(&path).ok())
    })
}

#[cfg(not(feature = "std"))]
pub fn resolve_source_file_for_location(
    source_manager: &dyn SourceManager,
    location: &Location,
) -> Option<Arc<SourceFile>> {
    source_manager.get_by_uri(location.uri())
}

/// Resolve a source URI to an existing local filesystem path.
///
/// Non-file URI schemes are left to the source manager. Relative paths are resolved against the
/// debugger process' current directory, which DAP clients set to the launch `cwd`.
#[cfg(feature = "std")]
pub fn resolve_source_path(uri: &Uri) -> Option<PathBuf> {
    let path = match uri.scheme() {
        None | Some("file") => uri.to_path()?,
        Some(_) => return None,
    };

    fn existing_path(path: &Path) -> Option<PathBuf> {
        path.exists()
            .then(|| path.canonicalize().unwrap_or_else(|_| path.to_path_buf()))
    }

    existing_path(&path).or_else(|| {
        if path.is_relative() {
            std::env::current_dir().ok().and_then(|cwd| existing_path(&cwd.join(path)))
        } else {
            None
        }
    })
}

/// Resolve a source location directly from the filesystem, returning the resolved path and line.
#[cfg(feature = "std")]
pub fn resolve_location_from_filesystem(location: &Location) -> Option<(PathBuf, u32)> {
    let path = resolve_source_path(location.uri())?;
    let bytes = std::fs::read(&path).ok()?;
    let start = location.start.to_usize().min(bytes.len());
    let line = bytes[..start].iter().filter(|byte| **byte == b'\n').count() as u32 + 1;
    Some((path, line))
}

/// Returns true for source paths emitted by compiler/runtime internals rather than user code.
pub fn is_internal_source_uri(uri: &Uri) -> bool {
    let path = uri.as_str().replace('\\', "/");
    path.contains("/codegen/masm/intrinsics/") || path.contains("/rustlib/src/rust/library/")
}

#[derive(Debug, Clone)]
pub struct ResolvedLocation {
    pub source_file: Arc<SourceFile>,
    // TODO(fabrio): Use LineNumber and ColumnNumber instead of raw `u32`.
    pub line: u32,
    pub col: u32,
    pub span: SourceSpan,
}
impl fmt::Display for ResolvedLocation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}:{}", self.source_file.uri().as_str(), self.line, self.col)
    }
}

pub struct CurrentFrame {
    pub procedure: Option<Arc<str>>,
    pub location: Option<ResolvedLocation>,
}

pub struct StackTrace<'a> {
    callstack: &'a CallStack,
    recent: &'a VecDeque<Operation>,
    source_manager: &'a dyn SourceManager,
    current_frame: Option<CurrentFrame>,
}

impl<'a> StackTrace<'a> {
    pub fn new(
        callstack: &'a CallStack,
        recent: &'a VecDeque<Operation>,
        source_manager: &'a dyn SourceManager,
    ) -> Self {
        let current_frame = callstack.logical_frames("").last().map(|frame| {
            let location = frame.resolved(source_manager);
            let procedure = Some(Arc::from(frame.display_name().into_boxed_str()));
            CurrentFrame {
                procedure,
                location,
            }
        });
        Self {
            callstack,
            recent,
            source_manager,
            current_frame,
        }
    }

    pub fn current_frame(&self) -> Option<&CurrentFrame> {
        self.current_frame.as_ref()
    }
}

impl fmt::Display for StackTrace<'_> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use core::fmt::Write;

        let frames = self.callstack.logical_frames("");
        let num_frames = frames.len();

        writeln!(f, "\nStack Trace:")?;

        for (i, frame) in frames.iter().enumerate() {
            let is_top = i + 1 == num_frames;
            let name = frame.display_name();
            if is_top {
                write!(f, " `-> {name}")?;
            } else {
                write!(f, " |-> {name}")?;
            }
            if let Some(resolved) = frame.resolved(self.source_manager) {
                write!(f, " in {resolved}")?;
            } else {
                write!(f, " in <unavailable>")?;
            }
            if is_top {
                let physical_frame = &self.callstack.frames[frame.physical_index()];
                // Print op context
                let context_size = physical_frame.context.len();
                writeln!(f, ":\n\nLast {context_size} Instructions (of current frame):")?;
                for (i, op) in physical_frame.context.iter().enumerate() {
                    let is_last = i + 1 == context_size;
                    if let Some(callee) = op.callee("") {
                        write!(f, " |   exec.{callee}")?;
                    } else {
                        write!(f, " |   {}", op.opcode())?;
                    }
                    if is_last {
                        writeln!(f, "\n `-> <error occurred here>")?;
                    } else {
                        f.write_char('\n')?;
                    }
                }

                let context_size = self.recent.len();
                writeln!(f, "\n\nLast {context_size} Instructions (any frame):")?;
                for (i, op) in self.recent.iter().enumerate() {
                    let is_last = i + 1 == context_size;
                    if is_last {
                        writeln!(f, " |   {}", op)?;
                        writeln!(f, " `-> <error occurred here>")?;
                    } else {
                        writeln!(f, " |   {}", op)?;
                    }
                }
            } else {
                f.write_char('\n')?;
            }
        }

        Ok(())
    }
}

fn resolve_assembly_location(
    source_manager: &dyn SourceManager,
    location: &Location,
) -> Option<ResolvedLocation> {
    let source_file = resolve_source_file_for_location(source_manager, location)?;
    let span = SourceSpan::new(source_file.id(), location.start..location.end);
    let file_line_col = source_file.location(span);
    Some(ResolvedLocation {
        source_file,
        line: file_line_col.line.to_u32(),
        col: file_line_col.column.to_u32(),
        span,
    })
}

#[cfg(feature = "std")]
fn demangle(name: &str) -> String {
    let mut input = name.as_bytes();
    let mut demangled = Vec::with_capacity(input.len() * 2);
    rustc_demangle::demangle_stream(&mut input, &mut demangled, /* include_hash= */ false)
        .expect("failed to write demangled identifier");
    String::from_utf8(demangled).expect("demangled identifier contains invalid utf-8")
}

#[cfg(not(feature = "std"))]
fn demangle(name: &str) -> String {
    rustc_demangle::demangle(name).to_string()
}

#[cfg(test)]
mod tests {
    use std::{cell::OnceCell, fs, path::PathBuf};

    use miden_assembly_syntax::debuginfo::{DefaultSourceManager, SourceManagerExt};
    use miden_debug_types::{ByteIndex, Location, Uri};

    use super::*;

    #[test]
    fn resolves_relative_source_locations_from_filesystem() {
        let path = test_source_path("relative");
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(&path, "fn main() {\n    let x = 1;\n}\n").unwrap();

        let start = "fn main() {\n    ".len() as u32;
        let location = Location::new(
            Uri::from(path.display().to_string()),
            ByteIndex::new(start),
            ByteIndex::new(start + 5),
        );
        let detail = OpDetail::Full {
            op: Operation::Noop,
            location: Some(location),
            resolved: OnceCell::new(),
        };
        let source_manager = DefaultSourceManager::default();

        let resolved = detail.resolve(&source_manager).expect("source should resolve");
        assert_eq!(resolved.line, 2);
        assert!(resolved.source_file.uri().as_str().ends_with("src/lib.rs"));

        fs::remove_dir_all(path.parent().unwrap().parent().unwrap()).ok();
    }

    #[test]
    fn logical_frames_place_innermost_inline_frame_on_top() {
        let path = test_source_path("inline-frames");
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        let source = "physical call\nouter call\ninner body\n";
        fs::write(&path, source).unwrap();
        let uri = Uri::from(path.display().to_string());

        let mut frame = CallFrame::new(Some(Arc::from("crate::physical")));
        let outer_start = "physical call\n".len() as u32;
        frame.inline_frames = vec![
            InlineCallFrame {
                name: Arc::from("crate::inner"),
                call_site: Location::new(
                    uri.clone(),
                    ByteIndex::new(outer_start),
                    ByteIndex::new(outer_start + "outer call".len() as u32),
                ),
            },
            InlineCallFrame {
                name: Arc::from("crate::outer"),
                call_site: Location::new(
                    uri.clone(),
                    ByteIndex::new(0),
                    ByteIndex::new("physical call".len() as u32),
                ),
            },
        ];
        let inner_start = "physical call\nouter call\n".len() as u32;
        let asmop = AssemblyOp::new(
            Some(Location::new(
                uri,
                ByteIndex::new(inner_start),
                ByteIndex::new(inner_start + "inner body".len() as u32),
            )),
            "crate::physical".to_string(),
            1,
            "add".to_string(),
        );
        frame.push(Operation::Add, 1, Some(&asmop));

        let mut callstack = CallStack::new(Arc::new(RwLock::new(BTreeMap::new())));
        callstack.frames.push(frame);
        let source_manager = DefaultSourceManager::default();
        let logical = callstack.logical_frames("");

        assert_eq!(logical.len(), 3);
        assert_eq!(logical[0].name(), "crate::physical");
        assert_eq!(logical[0].kind(), LogicalFrameKind::Physical);
        assert_eq!(logical[0].resolved(&source_manager).unwrap().line, 1);
        assert_eq!(logical[1].name(), "crate::outer");
        assert_eq!(logical[1].resolved(&source_manager).unwrap().line, 2);
        assert_eq!(logical[2].name(), "crate::inner");
        assert_eq!(logical[2].kind(), LogicalFrameKind::Inline);
        assert_eq!(logical[2].resolved(&source_manager).unwrap().line, 3);

        fs::remove_dir_all(path.parent().unwrap().parent().unwrap()).ok();
    }

    #[test]
    fn control_cycles_replace_and_clear_inline_frames() {
        let inline = InlineCallFrame {
            name: Arc::from("crate::inline"),
            call_site: Location::new(Uri::new("test.masm"), ByteIndex::new(0), ByteIndex::new(1)),
        };
        let mut callstack = CallStack::new(Arc::new(RwLock::new(BTreeMap::new())));

        callstack.next(&StepInfo {
            op: None,
            control: Some(ControlFlowOp::Split),
            asmop: None,
            clk: RowIndex::from(0u32),
            ctx: ContextId::root(),
            inline_frames: std::slice::from_ref(&inline),
        });

        let logical = callstack.logical_frames("");
        assert_eq!(logical.len(), 2);
        assert_eq!(logical[0].name(), "<unknown>");
        assert_eq!(logical[1].name(), "crate::inline");

        callstack.next(&StepInfo {
            op: None,
            control: Some(ControlFlowOp::Respan),
            asmop: None,
            clk: RowIndex::from(1u32),
            ctx: ContextId::root(),
            inline_frames: &[],
        });

        let logical = callstack.logical_frames("");
        assert_eq!(logical.len(), 1);
        assert_eq!(logical[0].name(), "<unknown>");
    }

    #[test]
    fn logical_physical_frame_tracks_exec_procedure_changes() {
        let mut callstack = CallStack::new(Arc::new(RwLock::new(BTreeMap::new())));
        let main = AssemblyOp::new(None, "program::main".to_string(), 1, "add".to_string());
        callstack.next(&StepInfo {
            op: Some(Operation::Add),
            control: None,
            asmop: Some(&main),
            clk: RowIndex::from(0u32),
            ctx: ContextId::root(),
            inline_frames: &[],
        });

        let logical = callstack.logical_frames("");
        assert_eq!(logical[0].name(), "program::main");
        assert_eq!(logical[0].display_name(), "program::main");

        let inline = InlineCallFrame {
            name: Arc::from("source::inline"),
            call_site: Location::new(Uri::new("test.masm"), ByteIndex::new(0), ByteIndex::new(1)),
        };
        let exec = AssemblyOp::new(None, "program::double".to_string(), 1, "mul".to_string());
        callstack.next(&StepInfo {
            op: Some(Operation::Mul),
            control: None,
            asmop: Some(&exec),
            clk: RowIndex::from(1u32),
            ctx: ContextId::root(),
            inline_frames: std::slice::from_ref(&inline),
        });

        let logical = callstack.logical_frames("");
        assert_eq!(logical.len(), 2);
        assert_eq!(logical[0].kind(), LogicalFrameKind::Physical);
        assert_eq!(logical[0].name(), "program::double");
        assert_eq!(logical[0].display_name(), "program::double");
        assert_eq!(logical[1].kind(), LogicalFrameKind::Inline);
    }

    #[test]
    fn control_cycle_tracks_exec_procedure_change_before_first_operation() {
        let mut callstack = CallStack::new(Arc::new(RwLock::new(BTreeMap::new())));
        let main = AssemblyOp::new(None, "program::main".to_string(), 1, "add".to_string());
        callstack.next(&StepInfo {
            op: Some(Operation::Add),
            control: None,
            asmop: Some(&main),
            clk: RowIndex::from(0u32),
            ctx: ContextId::root(),
            inline_frames: &[],
        });

        let exec = AssemblyOp::new(None, "program::double".to_string(), 1, "if.true".to_string());
        callstack.next(&StepInfo {
            op: None,
            control: Some(ControlFlowOp::Split),
            asmop: Some(&exec),
            clk: RowIndex::from(1u32),
            ctx: ContextId::root(),
            inline_frames: &[],
        });

        let logical = callstack.logical_frames("");
        assert_eq!(logical[0].name(), "program::double");
    }

    #[cfg(feature = "dap")]
    #[test]
    fn remote_logical_frames_preserve_pre_resolved_locations() {
        let path = test_source_path("remote-logical-frame");
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(&path, "first line\nsecond line\n").unwrap();

        let source_manager = DefaultSourceManager::default();
        let source_file = source_manager.load_file(&path).expect("source should load");
        let span = SourceSpan::new(source_file.id(), ByteIndex::new(11)..ByteIndex::new(17));
        let remote = ResolvedLocation {
            source_file,
            line: 77,
            col: 13,
            span,
        };
        let callstack = CallStack::from_remote_frames(vec![CallFrame::from_remote(
            Some(Arc::from("remote::procedure")),
            Some(remote.clone()),
        )]);

        let recent = callstack
            .current_frame()
            .unwrap()
            .last_resolved(&source_manager)
            .expect("remote frame should retain its cached location");
        assert_eq!(recent.line, remote.line);
        assert_eq!(recent.col, remote.col);
        assert_eq!(recent.span, remote.span);

        let logical = callstack.logical_frames("");
        let resolved = logical[0]
            .resolved(&source_manager)
            .expect("logical frame should retain its cached location");
        assert_eq!(resolved.source_file.uri(), remote.source_file.uri());
        assert_eq!(resolved.line, remote.line);
        assert_eq!(resolved.col, remote.col);
        assert_eq!(resolved.span, remote.span);

        fs::remove_dir_all(path.parent().unwrap().parent().unwrap()).ok();
    }

    fn test_source_path(test_name: &str) -> PathBuf {
        PathBuf::from("target")
            .join("debugger-source-tests")
            .join(format!("{}-{}", test_name, std::process::id()))
            .join("src")
            .join("lib.rs")
    }
}