dbg-cli 0.3.1

A universal debugger CLI that lets AI agents observe runtime state instead of guessing from source code
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
use std::process::Command;
use std::sync::OnceLock;

use regex::Regex;
use serde_json::{Map, Value};

use super::canonical::{BreakId, BreakLoc, CanonicalOps, HitEvent, unsupported};
use super::{Backend, CleanResult, Dependency, DependencyCheck, SpawnConfig};

pub struct NetCoreDbgBackend;

impl Backend for NetCoreDbgBackend {
    fn name(&self) -> &'static str {
        "netcoredbg"
    }

    fn description(&self) -> &'static str {
        ".NET debugger (C#, F#)"
    }

    fn types(&self) -> &'static [&'static str] {
        &["dotnet", "csharp", "fsharp"]
    }

    fn spawn_config(&self, target: &str, args: &[String]) -> anyhow::Result<SpawnConfig> {
        let netcoredbg = std::env::var("NETCOREDBG").unwrap_or_else(|_| "netcoredbg".into());

        let mut spawn_args = vec!["--interpreter=cli".into(), "--".into(), target.into()];
        spawn_args.extend(args.iter().cloned());

        let mut env = vec![];
        if std::env::var("DOTNET_ROOT").is_err() {
            if let Some(root) = detect_dotnet_root() {
                env.push(("DOTNET_ROOT".into(), root));
            }
        }

        Ok(SpawnConfig {
            bin: netcoredbg,
            args: spawn_args,
            env,
            init_commands: vec![],
        })
    }

    fn prompt_pattern(&self) -> &str {
        r"ncdb>"
    }

    fn dependencies(&self) -> Vec<Dependency> {
        vec![
            Dependency {
                name: "dotnet",
                check: DependencyCheck::Binary {
                    name: "dotnet",
                    alternatives: &["dotnet"],
                    version_cmd: None,
                },
                install: "https://dot.net/install",
            },
            Dependency {
                name: "netcoredbg",
                check: DependencyCheck::Binary {
                    name: "netcoredbg",
                    alternatives: &["netcoredbg"],
                    version_cmd: None,
                },
                install: concat!(
                    "mkdir -p ~/.local/share/netcoredbg && ",
                    "curl -sL https://github.com/Samsung/netcoredbg/releases/latest/download/",
                    "netcoredbg-linux-amd64.tar.gz | tar xz -C ~/.local/share/netcoredbg && ",
                    "ln -sf ~/.local/share/netcoredbg/netcoredbg/netcoredbg ~/.local/bin/netcoredbg"
                ),
            },
        ]
    }

    fn format_breakpoint(&self, spec: &str) -> String {
        format!("break {spec}")
    }

    fn run_command(&self) -> &'static str {
        "run"
    }

    fn parse_help(&self, raw: &str) -> String {
        let mut cmds = Vec::new();
        let mut seen = std::collections::HashSet::new();
        for line in raw.lines() {
            let line = line.trim();
            if line.is_empty() || line.starts_with('-') || line.starts_with("command") {
                continue;
            }
            if let Some(tok) = line.split_whitespace().next() {
                if tok.chars().all(|c| c.is_ascii_alphabetic()) && tok.len() < 20 && seen.insert(tok.to_string()) {
                    cmds.push(tok.to_string());
                }
            }
        }
        format!("netcoredbg: {}", cmds.join(", "))
    }

    fn adapters(&self) -> Vec<(&'static str, &'static str)> {
        vec![("dotnet.md", include_str!("../../skills/adapters/dotnet.md"))]
    }

    fn clean(&self, _cmd: &str, output: &str) -> CleanResult {
        let stop_re = Regex::new(r"reason: (.+?)(?:, thread|, stopped|$)").unwrap();
        let frame_re = Regex::new(r"frame=\{(.+?)\}").unwrap();

        let mut events = Vec::new();
        let mut lines = Vec::new();
        for line in output.lines() {
            let trimmed = line.trim();
            if trimmed.is_empty() || trimmed.contains("^running") {
                continue;
            }
            // Emit lifecycle noise as events instead of dropping
            if trimmed.contains("library loaded:") || trimmed.contains("symbols loaded, base") {
                events.push(trimmed.to_string());
                continue;
            }
            if trimmed.contains("no symbols loaded") {
                events.push(trimmed.to_string());
                continue;
            }
            if trimmed.contains("thread created") || trimmed.contains("thread exited") {
                events.push(trimmed.to_string());
                continue;
            }
            if trimmed.contains("breakpoint modified") {
                events.push(trimmed.to_string());
                continue;
            }
            if trimmed.starts_with("stopped,") {
                let reason = stop_re
                    .captures(trimmed)
                    .map(|c| c[1].to_string())
                    .unwrap_or_else(|| "unknown".into());
                let loc = frame_re
                    .captures(trimmed)
                    .map(|c| format!(" @ {}", &c[1]))
                    .unwrap_or_default();
                lines.push(format!("stopped: {reason}{loc}"));
                continue;
            }
            lines.push(trimmed.to_string());
        }

        CleanResult {
            output: lines.join("\n"),
            events,
        }
    }

    fn canonical_ops(&self) -> Option<&dyn CanonicalOps> {
        Some(self)
    }
}

impl CanonicalOps for NetCoreDbgBackend {
    fn tool_name(&self) -> &'static str { "netcoredbg" }
    fn auto_capture_locals(&self) -> bool { false }

    fn tool_version(&self) -> Option<String> {
        static V: OnceLock<Option<String>> = OnceLock::new();
        V.get_or_init(|| {
            let bin = std::env::var("NETCOREDBG").unwrap_or_else(|_| "netcoredbg".into());
            let out = Command::new(&bin).arg("--version").output().ok()?;
            let s = String::from_utf8_lossy(&out.stdout);
            let s = if s.trim().is_empty() {
                String::from_utf8_lossy(&out.stderr).to_string()
            } else {
                s.to_string()
            };
            s.lines().next().map(|l| l.trim().to_string())
        })
        .clone()
    }

    fn op_break(&self, loc: &BreakLoc) -> anyhow::Result<String> {
        Ok(match loc {
            BreakLoc::FileLine { file, line } => format!("break {file}:{line}"),
            BreakLoc::Fqn(name) => format!("break {name}"),
            BreakLoc::ModuleMethod { module, method } => {
                format!("break {module}!{method}")
            }
        })
    }

    fn op_unbreak(&self, id: BreakId) -> anyhow::Result<String> {
        Ok(format!("delete {}", id.0))
    }
    fn op_breaks(&self) -> anyhow::Result<String> { Ok("info breakpoints".into()) }

    fn op_run(&self, _args: &[String]) -> anyhow::Result<String> {
        // netcoredbg launches the target on startup; re-running is `run`.
        Ok("run".into())
    }
    fn op_continue(&self) -> anyhow::Result<String> { Ok("continue".into()) }
    fn op_step(&self) -> anyhow::Result<String> { Ok("step".into()) }
    fn op_next(&self) -> anyhow::Result<String> { Ok("next".into()) }
    fn op_finish(&self) -> anyhow::Result<String> { Ok("finish".into()) }

    fn op_stack(&self, n: Option<u32>) -> anyhow::Result<String> {
        Ok(match n {
            Some(k) => format!("backtrace {k}"),
            None => "backtrace".into(),
        })
    }
    fn op_frame(&self, n: u32) -> anyhow::Result<String> {
        Ok(format!("frame {n}"))
    }
    fn op_locals(&self) -> anyhow::Result<String> {
        // netcoredbg's CLI mode has no bulk `info locals` equivalent.
        // `print this` only shows the implicit receiver and doesn't
        // list method-local variables. Phase 2 should switch to MI
        // mode + `-stack-list-variables --all-values`. For now, agents
        // use `dbg print <varname>` for individual variables.
        Err(unsupported("netcoredbg", "bulk locals in CLI mode (use `dbg print <var>` for individual variables)"))
    }
    fn op_print(&self, expr: &str) -> anyhow::Result<String> {
        Ok(format!("print {expr}"))
    }
    fn op_threads(&self) -> anyhow::Result<String> { Ok("info threads".into()) }
    fn op_thread(&self, n: u32) -> anyhow::Result<String> {
        Ok(format!("thread {n}"))
    }
    fn op_list(&self, loc: Option<&str>) -> anyhow::Result<String> {
        Ok(match loc {
            Some(s) => format!("list {s}"),
            None => "list".into(),
        })
    }
    // op_watch inherits the default `unsupported` implementation:
    // netcoredbg's CLI mode does not expose watchpoints.

    /// netcoredbg emits two formats at stop:
    ///   * raw MI: `*stopped,reason="breakpoint-hit",thread-id="1",frame={...}`
    ///   * cleaned (via `Backend::clean`): `stopped: breakpoint N hit @ <frame> at <file>:<line>`
    /// `parse_hit` tries the cleaned form first (it's what the daemon
    /// feeds in after `clean()`); falls back to MI for completeness.
    fn parse_hit(&self, output: &str) -> Option<HitEvent> {
        // Try the cleaned `stopped: ... @ <frame> at <file>:<line>` form.
        let cleaned_re = stop_regex_cleaned();
        for line in output.lines() {
            if let Some(c) = cleaned_re.captures(line) {
                let file = c["file"].to_string();
                let line_no: u32 = c["line"].parse().ok()?;
                let func = c["frame"].to_string();
                return Some(HitEvent {
                    location_key: format!("{file}:{line_no}"),
                    thread: None,
                    frame_symbol: Some(func),
                    file: Some(file),
                    line: Some(line_no),
                });
            }
        }

        // Fall back to the MI form.
        let stop_re = stop_regex_mi();
        let frame_re = frame_regex_mi();
        let file_line_re = file_line_regex_mi();

        let stopped = output
            .lines()
            .any(|l| l.trim_start().starts_with("stopped,") || l.contains("*stopped"));
        if !stopped {
            return None;
        }
        let thread = output.lines().find_map(|l| {
            stop_re
                .captures(l)
                .and_then(|c| c.name("tid").map(|m| m.as_str().to_string()))
        });
        let frame = output.lines().find_map(|l| frame_re.captures(l));
        let (func, file, line) = match frame.as_ref() {
            Some(c) => {
                let frame_blob = &c[1];
                let func = find_mi_field(frame_blob, "func");
                let (file, line) = file_line_re
                    .captures(frame_blob)
                    .map(|m| (m["f"].to_string(), m["l"].parse::<u32>().ok()))
                    .unwrap_or_else(|| (String::new(), None));
                let file = if file.is_empty() {
                    find_mi_field(frame_blob, "file")
                } else {
                    Some(file)
                };
                let line = line.or_else(|| {
                    find_mi_field(frame_blob, "line").and_then(|s| s.parse().ok())
                });
                (func, file, line)
            }
            None => (None, None, None),
        };

        let location_key = match (&file, line, &func) {
            (Some(f), Some(l), _) => format!("{f}:{l}"),
            (_, _, Some(s)) => s.clone(),
            _ => return None,
        };

        Some(HitEvent {
            location_key,
            thread,
            frame_symbol: func,
            file,
            line,
        })
    }

    /// `info locals` emits `name = value` lines similar to gdb.
    fn parse_locals(&self, output: &str) -> Option<Value> {
        let re = locals_regex();
        let mut obj = Map::new();
        for line in output.lines() {
            if let Some(c) = re.captures(line.trim_end()) {
                let name = c[1].to_string();
                let val = c[2].trim().to_string();
                let mut entry = Map::new();
                entry.insert("value".into(), Value::String(val));
                obj.insert(name, Value::Object(entry));
            }
        }
        if obj.is_empty() {
            None
        } else {
            Some(Value::Object(obj))
        }
    }
}

fn stop_regex_cleaned() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| {
        // `stopped: breakpoint 1 hit @ Ns.Class.Method() at /path/file.cs:42`
        Regex::new(
            r"^stopped:[^@]*@\s+(?P<frame>\S+?)\s+at\s+(?P<file>\S+):(?P<line>\d+)",
        )
        .unwrap()
    })
}

fn stop_regex_mi() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| {
        Regex::new(r#"thread-id[:=]\s*["]?(?P<tid>\d+)"#).unwrap()
    })
}

fn frame_regex_mi() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| Regex::new(r"frame=\{(.+?)\}").unwrap())
}

fn file_line_regex_mi() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| Regex::new(r"at\s+(?P<f>[^:}\s]+):(?P<l>\d+)").unwrap())
}

fn find_mi_field(blob: &str, key: &str) -> Option<String> {
    // Match `key="value"` (MI-style) or `key=value` (un-quoted) inside
    // the frame blob. Values may contain spaces but not `}` or `,`.
    let re_quoted = Regex::new(&format!(r#"{key}="([^"]*)""#)).ok()?;
    if let Some(c) = re_quoted.captures(blob) {
        return Some(c[1].to_string());
    }
    let re_unq = Regex::new(&format!(r"{key}=([^,}}]+)")).ok()?;
    re_unq.captures(blob).map(|c| c[1].trim().to_string())
}

fn locals_regex() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| Regex::new(r"^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.+)$").unwrap())
}

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

    #[test]
    fn format_breakpoint() {
        assert_eq!(
            NetCoreDbgBackend.format_breakpoint("Program.cs:10"),
            "break Program.cs:10"
        );
    }

    #[test]
    fn clean_parses_stopped_with_reason_and_frame() {
        let input = "stopped, reason: breakpoint 1 hit, thread-id: 1, frame={Program.Main() at Program.cs:4}";
        let r = NetCoreDbgBackend.clean("run", input);
        assert!(r.output.contains("stopped: breakpoint 1 hit"));
        assert!(r.output.contains("@ Program.Main() at Program.cs:4"));
    }

    #[test]
    fn clean_emits_library_events() {
        let input = "library loaded: System.dll, symbols loaded, base address: 0x1000\nthread created, id: 123\nbreakpoint modified, Breakpoint 1";
        let r = NetCoreDbgBackend.clean("run", input);
        assert!(r.output.is_empty());
        assert_eq!(r.events.len(), 3);
    }

    #[test]
    fn clean_skips_empty_and_running() {
        let input = "\n^running\nactual output";
        let r = NetCoreDbgBackend.clean("continue", input);
        assert_eq!(r.output, "actual output");
    }

    #[test]
    fn parse_help_filters_dashes_and_command() {
        let raw = "command list:\n-h  show help\nbreak  Set breakpoint\ncontinue  Resume";
        let result = NetCoreDbgBackend.parse_help(raw);
        assert!(result.contains("break"));
        assert!(result.contains("continue"));
        assert!(!result.contains("command"));
    }

    // --------------------------------------------------------------
    // CanonicalOps
    // --------------------------------------------------------------

    #[test]
    fn canonical_break_ops() {
        let ops: &dyn CanonicalOps = &NetCoreDbgBackend;
        assert_eq!(
            ops.op_break(&BreakLoc::FileLine { file: "Program.cs".into(), line: 10 }).unwrap(),
            "break Program.cs:10"
        );
        assert_eq!(
            ops.op_break(&BreakLoc::Fqn("Foo.Bar.Baz".into())).unwrap(),
            "break Foo.Bar.Baz"
        );
        assert_eq!(
            ops.op_break(&BreakLoc::ModuleMethod { module: "MyLib".into(), method: "Baz".into() }).unwrap(),
            "break MyLib!Baz"
        );
    }

    #[test]
    fn canonical_exec_ops() {
        let ops: &dyn CanonicalOps = &NetCoreDbgBackend;
        assert_eq!(ops.op_continue().unwrap(), "continue");
        assert_eq!(ops.op_step().unwrap(), "step");
        assert_eq!(ops.op_next().unwrap(), "next");
        assert_eq!(ops.op_finish().unwrap(), "finish");
    }

    #[test]
    fn canonical_thread_ops() {
        let ops: &dyn CanonicalOps = &NetCoreDbgBackend;
        assert_eq!(ops.op_threads().unwrap(), "info threads");
        assert_eq!(ops.op_thread(2).unwrap(), "thread 2");
    }

    #[test]
    fn canonical_watch_unsupported() {
        let ops: &dyn CanonicalOps = &NetCoreDbgBackend;
        let err = ops.op_watch("x").unwrap_err().to_string();
        assert!(err.contains("netcoredbg"));
        assert!(err.contains("dbg raw"));
    }

    #[test]
    fn parse_hit_from_cleaned_stop_line() {
        // This is what Backend::clean produces out of the MI record —
        // it's also what the daemon feeds into parse_hit.
        let out = "stopped: breakpoint 1 hit @ DbgExample.Algos.Fibonacci() at /app/Program.cs:22";
        let hit = NetCoreDbgBackend.parse_hit(out).expect("should parse");
        assert_eq!(hit.file.as_deref(), Some("/app/Program.cs"));
        assert_eq!(hit.line, Some(22));
        assert_eq!(hit.frame_symbol.as_deref(), Some("DbgExample.Algos.Fibonacci()"));
    }

    #[test]
    fn parse_hit_from_mi_stopped_record() {
        let out = r#"stopped, reason: breakpoint 1 hit, thread-id: 1, frame={Program.Main() at Program.cs:4}"#;
        let hit = NetCoreDbgBackend.parse_hit(out).expect("should parse");
        assert_eq!(hit.file.as_deref(), Some("Program.cs"));
        assert_eq!(hit.line, Some(4));
        assert_eq!(hit.thread.as_deref(), Some("1"));
    }

    #[test]
    fn parse_hit_from_quoted_mi_record() {
        let out = r#"*stopped,reason="breakpoint-hit",thread-id="1",frame={func="Foo.Bar.Baz",file="Program.cs",line="42"}"#;
        let hit = NetCoreDbgBackend.parse_hit(out).expect("should parse");
        assert_eq!(hit.file.as_deref(), Some("Program.cs"));
        assert_eq!(hit.line, Some(42));
        assert_eq!(hit.frame_symbol.as_deref(), Some("Foo.Bar.Baz"));
    }

    #[test]
    fn parse_hit_none_without_stopped() {
        assert!(NetCoreDbgBackend.parse_hit("random output").is_none());
    }

    #[test]
    fn parse_locals_simple_values() {
        let out = "x = 42\nname = \"hello\"\nempty = {}";
        let v = NetCoreDbgBackend.parse_locals(out).expect("should parse");
        let obj = v.as_object().unwrap();
        assert_eq!(obj.get("x").unwrap().get("value").unwrap().as_str().unwrap(), "42");
        assert_eq!(obj.get("name").unwrap().get("value").unwrap().as_str().unwrap(), "\"hello\"");
    }

    #[test]
    fn backend_canonical_ops_hook_returns_self() {
        let b: Box<dyn Backend> = Box::new(NetCoreDbgBackend);
        assert_eq!(b.canonical_ops().unwrap().tool_name(), "netcoredbg");
    }
}

fn detect_dotnet_root() -> Option<String> {
    // Homebrew layout: .../dotnet/<ver>/bin/dotnet → sibling libexec/.
    // Standard layout: dotnet binary lives directly in the root.
    dbg_cli::deps::find_tool_root("dotnet", Some("libexec"), None, 2)
        .and_then(|p| p.to_str().map(str::to_string))
}