anchor_cli/debugger/mod.rs
1//! `anchor debugger` — a foundry-style SBF instruction stepper built on the
2//! same per-test register traces that `--profile` consumes for flamegraphs.
3//!
4//! ## Flow
5//!
6//! 1. The caller invokes the existing `anchor test --profile` pipeline,
7//! which leaves traces under `target/anchor-v2-profile/<test>/`.
8//! 2. [`run`] walks that directory, materializes a [`DebugSession`], and
9//! launches the TUI.
10//!
11//! Keeping the trace-producing half identical to `--profile` means the
12//! debugger and flamegraph don't drift: any improvement to the trace
13//! pipeline is picked up by both.
14
15use {
16 anyhow::Result,
17 std::{
18 collections::BTreeMap,
19 path::{Path, PathBuf},
20 },
21};
22
23pub mod arena;
24pub mod cargo_deps;
25pub mod gdb;
26pub mod highlight;
27pub mod loose;
28pub mod model;
29pub mod path_label;
30pub mod rustc_wrapper;
31pub mod source;
32pub mod tui;
33
34/// Default trace directory name (relative to the workspace root). Mirrors
35/// `crate::profile::DEFAULT_PROFILE_DIR` so the loose-mode flow agrees
36/// with the Anchor.toml-driven flow on where to read/write traces.
37pub fn loose_profile_dir_name() -> &'static str {
38 crate::profile::DEFAULT_PROFILE_DIR
39}
40
41/// Build a [`model::DebugSession`] from the profile trace directory and
42/// launch the TUI. Blocks until the user quits.
43pub fn run(
44 profile_dir: &Path,
45 programs: &BTreeMap<String, PathBuf>,
46 manifest_dir: Option<&Path>,
47 crate_dir: Option<&Path>,
48 test_filter: Option<&str>,
49) -> Result<()> {
50 // Probe the terminal background BEFORE anything that might call into
51 // `highlight::ctx()` (arena pre-highlights disasm). The detection
52 // round-trips an OSC 11 query on a regular TTY; once the TUI raw-mode
53 // takeover starts, the reply might not get back to us cleanly.
54 highlight::detect_theme_mode_once();
55 let session =
56 arena::build_session(profile_dir, programs, manifest_dir, crate_dir, test_filter)?;
57 tui::run(session)
58}