lightgui-debug: GdbSession over gdb -q -i=mi2 pipes with hand-written MI parser (mi.rs), settings/breakpoint/variable types, and the 44-check C++ harness ported as Rust tests (37 console + 7 GTK, gdb 17.1). lightgui-support: CodeTheme + C tokenizer/highlighter, FontSettings, CodeView fold/gutter/inspection widget, CMake project builder. Tests: debug 59/59, support 23/23 (incl. theme/font/cmake suites). cargo test clean, zero rustc warnings, clippy clean, cargo fmt clean.
833 lines
29 KiB
Rust
833 lines
29 KiB
Rust
// Console debugger suite — Rust port of console_tests() in
|
|
// tests/debugger/dbg_harness.cpp (37 checks: marks_found + tests 1-36).
|
|
// Each check is its own #[test], named after the C++ report() name.
|
|
// Run with: cargo test -p lightgui-debug --test console
|
|
|
|
#[path = "common/mod.rs"]
|
|
mod common;
|
|
|
|
use common::{has, loc, mark_line, targets, tmp_path, var_value};
|
|
use lightgui_debug::{
|
|
logpoint_to_printf, BpKind, Breakpoint, DbgState, DebuggerSettings, GdbSession,
|
|
};
|
|
use std::path::PathBuf;
|
|
|
|
fn console() -> PathBuf {
|
|
targets().console.clone()
|
|
}
|
|
fn console_src() -> PathBuf {
|
|
targets().console_src.clone()
|
|
}
|
|
fn spin() -> PathBuf {
|
|
targets().spin.clone()
|
|
}
|
|
fn marks() -> (i32, i32, i32, i32, i32, i32) {
|
|
let src = console_src();
|
|
(
|
|
mark_line(&src, "MARK:LOOP"),
|
|
mark_line(&src, "MARK:LOOP_NEXT"),
|
|
mark_line(&src, "MARK:CALL"),
|
|
mark_line(&src, "MARK:AFTER_CALL"),
|
|
mark_line(&src, "MARK:COMPUTE"),
|
|
mark_line(&src, "MARK:HELPER"),
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn marks_found() {
|
|
let (a, b, c, d, e, f) = marks();
|
|
assert!(a > 0 && b > 0 && c > 0 && d > 0 && e > 0 && f > 0);
|
|
}
|
|
|
|
// 1. gdb exists at the default path
|
|
#[test]
|
|
fn gdb_default_path_executable() {
|
|
let s = DebuggerSettings::default();
|
|
assert!(std::fs::metadata(&s.gdb_path).is_ok(), "{}", s.gdb_path);
|
|
}
|
|
|
|
// 2. settings save/load round-trip
|
|
#[test]
|
|
fn settings_save_load() {
|
|
let s = DebuggerSettings {
|
|
gdb_path: "/usr/bin/gdb".to_string(),
|
|
break_on_main: true,
|
|
stop_on_entry: false,
|
|
pretty_print: false,
|
|
default_args: "a b".to_string(),
|
|
..Default::default()
|
|
};
|
|
let p = tmp_path("conf");
|
|
assert!(s.save(p.to_str().unwrap()));
|
|
let mut s2 = DebuggerSettings::default();
|
|
assert!(s2.load(p.to_str().unwrap()));
|
|
assert_eq!(s2.gdb_path, "/usr/bin/gdb");
|
|
assert!(s2.break_on_main);
|
|
assert!(!s2.pretty_print);
|
|
assert_eq!(s2.default_args, "a b");
|
|
}
|
|
|
|
// 3. bad gdb path fails cleanly
|
|
#[test]
|
|
fn bad_gdb_path_fails() {
|
|
let s = DebuggerSettings {
|
|
gdb_path: "/nonexistent/gdb".to_string(),
|
|
..Default::default()
|
|
};
|
|
let mut sess = GdbSession::new(s);
|
|
let err = sess.start(console().to_str().unwrap(), "").unwrap_err();
|
|
assert!(!err.is_empty(), "expected an error message");
|
|
}
|
|
|
|
// 4. run to completion, no breakpoints
|
|
#[test]
|
|
fn run_to_completion() {
|
|
let mut sess = GdbSession::default();
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(10_000));
|
|
assert_eq!(sess.state(), DbgState::Exited);
|
|
assert_eq!(sess.exit_code(), 0);
|
|
assert!(
|
|
sess.target_output().contains("DONE sum=9"),
|
|
"out: {}",
|
|
sess.target_output()
|
|
);
|
|
}
|
|
|
|
// 5. breakpoint by file:line is hit
|
|
#[test]
|
|
fn bp_file_line_hit() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
assert!(sess.stage_breakpoint(&loc(&console_src(), l_loop)));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.state(), DbgState::Stopped);
|
|
assert_eq!(sess.last_stop().reason, "breakpoint-hit");
|
|
assert_eq!(sess.last_stop().frame.line, l_loop);
|
|
assert_eq!(sess.last_stop().frame.function, "main");
|
|
}
|
|
|
|
// 6. locals at the breakpoint (first iteration: i=0, sum=0)
|
|
#[test]
|
|
fn locals_at_bp() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
sess.wait_for_stop(-1);
|
|
sess.refresh_on_stop();
|
|
assert!(
|
|
has(&sess.locals(), "i", "0"),
|
|
"i={}",
|
|
var_value(&sess.locals(), "i")
|
|
);
|
|
assert!(
|
|
has(&sess.locals(), "sum", "0"),
|
|
"sum={}",
|
|
var_value(&sess.locals(), "sum")
|
|
);
|
|
}
|
|
|
|
// 7. watch expression + evaluate
|
|
#[test]
|
|
fn watch_and_evaluate() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
sess.wait_for_stop(-1);
|
|
sess.refresh_on_stop();
|
|
sess.add_watch("sum + 1").expect("add_watch");
|
|
let v = sess.evaluate("i + 5").expect("evaluate i+5");
|
|
assert_eq!(v, "5", "i+5={}", v);
|
|
assert!(has(&sess.watches(), "sum + 1", "1"));
|
|
}
|
|
|
|
// 8. step over: LOOP -> LOOP_NEXT, x == 1 after the call
|
|
#[test]
|
|
fn step_over() {
|
|
let (l_loop, l_next, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
sess.wait_for_stop(-1);
|
|
sess.step_over().expect("step_over");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(
|
|
sess.last_stop().frame.line,
|
|
l_next,
|
|
"line={}",
|
|
sess.last_stop().frame.line
|
|
);
|
|
let v = sess.evaluate("x").expect("evaluate x");
|
|
assert_eq!(v, "1", "x={}", v);
|
|
}
|
|
|
|
// 9. step into: CALL line -> inside compute()
|
|
#[test]
|
|
fn step_into() {
|
|
let (_, _, l_call, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_call));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
sess.wait_for_stop(-1);
|
|
sess.step_into().expect("step_into");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert!(
|
|
sess.last_stop().frame.function.contains("compute"),
|
|
"func={}",
|
|
sess.last_stop().frame.function
|
|
);
|
|
}
|
|
|
|
// 10. step out: from compute() back to main, line after the call
|
|
#[test]
|
|
fn step_out() {
|
|
let (_, _, l_call, l_after, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_call));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
sess.wait_for_stop(-1);
|
|
sess.step_into().expect("step_into");
|
|
sess.wait_for_stop(-1);
|
|
sess.step_out().expect("step_out");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.last_stop().frame.function, "main");
|
|
assert!(
|
|
sess.last_stop().frame.line == l_after || sess.last_stop().frame.line == l_call,
|
|
"line={}",
|
|
sess.last_stop().frame.line
|
|
);
|
|
if sess.last_stop().frame.line == l_call {
|
|
// finish stops on the call-site assignment line; one step over
|
|
// executes the store, landing on the line after.
|
|
sess.step_over().expect("step_over");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.last_stop().frame.line, l_after);
|
|
}
|
|
let v = sess.evaluate("total").expect("evaluate total");
|
|
assert_eq!(v, "61", "total={}", v);
|
|
}
|
|
|
|
// 11. continue to the next breakpoint (loop bp, then helper bp)
|
|
#[test]
|
|
fn continue_to_next_bp() {
|
|
let (l_loop, _, _, _, _, l_helper) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.stage_breakpoint(&loc(&console_src(), l_helper));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.last_stop().frame.line, l_loop);
|
|
sess.cont().expect("cont");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.last_stop().reason, "breakpoint-hit");
|
|
assert!(
|
|
sess.last_stop().frame.function.contains("helper"),
|
|
"func={}",
|
|
sess.last_stop().frame.function
|
|
);
|
|
}
|
|
|
|
// 12. multiple breakpoints listed with ids
|
|
#[test]
|
|
fn multiple_bps_listed() {
|
|
let (l_loop, _, _, _, _, l_helper) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.stage_breakpoint(&loc(&console_src(), l_helper));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
sess.wait_for_stop(-1);
|
|
sess.refresh_breakpoints().expect("refresh");
|
|
assert!(
|
|
sess.breakpoints().len() >= 2,
|
|
"count={}",
|
|
sess.breakpoints().len()
|
|
);
|
|
}
|
|
|
|
// 13. disable a breakpoint: it is not hit
|
|
#[test]
|
|
fn disable_bp_skips() {
|
|
let (l_loop, _, _, _, _, l_helper) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.stage_breakpoint(&loc(&console_src(), l_helper));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
sess.wait_for_stop(-1); // at LOOP
|
|
let loop_id = sess
|
|
.breakpoints()
|
|
.iter()
|
|
.find(|b| b.line == l_loop)
|
|
.map(|b| b.id)
|
|
.unwrap_or(0);
|
|
assert!(loop_id > 0);
|
|
sess.enable_breakpoint(loop_id, false).expect("disable");
|
|
sess.cont().expect("cont");
|
|
assert!(sess.wait_for_stop(-1));
|
|
// Next stop must be the helper bp, not the loop bp.
|
|
assert!(
|
|
sess.last_stop().frame.function.contains("helper"),
|
|
"func={}",
|
|
sess.last_stop().frame.function
|
|
);
|
|
}
|
|
|
|
// 14. re-enable: the breakpoint is hit again
|
|
#[test]
|
|
fn enable_bp_hits_again() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
sess.wait_for_stop(-1);
|
|
let loop_id = sess
|
|
.breakpoints()
|
|
.iter()
|
|
.find(|b| b.line == l_loop)
|
|
.map(|b| b.id)
|
|
.unwrap_or(0);
|
|
sess.enable_breakpoint(loop_id, false).expect("disable");
|
|
sess.cont().expect("cont");
|
|
assert!(sess.wait_for_stop(-1));
|
|
// Disabled: runs to completion (no more bps).
|
|
assert_eq!(sess.state(), DbgState::Exited);
|
|
// Restart with the bp re-enabled: must stop at LOOP again.
|
|
sess.enable_breakpoint(loop_id, true).expect("enable");
|
|
sess.restart().expect("restart");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.state(), DbgState::Stopped);
|
|
assert_eq!(sess.last_stop().frame.line, l_loop);
|
|
}
|
|
|
|
// 15. delete a breakpoint: gone from the list, never hit
|
|
#[test]
|
|
fn delete_bp() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
sess.wait_for_stop(-1);
|
|
let loop_id = sess
|
|
.breakpoints()
|
|
.iter()
|
|
.find(|b| b.line == l_loop)
|
|
.map(|b| b.id)
|
|
.unwrap_or(0);
|
|
sess.remove_breakpoint(loop_id).expect("remove");
|
|
sess.refresh_breakpoints().expect("refresh");
|
|
assert!(
|
|
sess.breakpoints().is_empty(),
|
|
"remaining={}",
|
|
sess.breakpoints().len()
|
|
);
|
|
sess.cont().expect("cont");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.state(), DbgState::Exited);
|
|
}
|
|
|
|
// 16. breakpoint by function name
|
|
#[test]
|
|
fn bp_by_function() {
|
|
let (_, _, _, _, l_compute, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint("compute");
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert!(
|
|
sess.last_stop().frame.function.contains("compute"),
|
|
"func={}",
|
|
sess.last_stop().frame.function
|
|
);
|
|
assert_eq!(sess.last_stop().frame.line, l_compute);
|
|
}
|
|
|
|
// 17. invalid breakpoints fail gracefully
|
|
#[test]
|
|
fn invalid_bp_rejected() {
|
|
let mut sess = GdbSession::default();
|
|
assert!(!sess.stage_breakpoint(""));
|
|
assert!(!sess.stage_breakpoint("not a location!!!"));
|
|
// gdb-side rejection of a nonexistent file
|
|
sess.start_gdb_only().expect("start_gdb_only");
|
|
sess.load_executable(console().to_str().unwrap())
|
|
.expect("load_executable");
|
|
let r = sess.add_breakpoint("no_such_file.cpp:9999");
|
|
assert!(r.is_err(), "expected gdb rejection");
|
|
assert!(!r.unwrap_err().is_empty());
|
|
}
|
|
|
|
// 18. stack frames at a stop
|
|
#[test]
|
|
fn frames_at_stop() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
sess.wait_for_stop(-1);
|
|
sess.refresh_on_stop();
|
|
assert!(!sess.frames().is_empty(), "n={}", sess.frames().len());
|
|
assert_eq!(sess.frames()[0].function, "main");
|
|
assert_eq!(sess.frames()[0].line, l_loop);
|
|
}
|
|
|
|
// 19. stack depth inside nested calls (main > compute > helper)
|
|
#[test]
|
|
fn stack_depth_nested() {
|
|
let (_, _, _, _, _, l_helper) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_helper));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
let mut ok = sess.wait_for_stop(-1);
|
|
// Hits 1-3 are helper() called from the loop (depth 2); hit 4 is
|
|
// helper() called from compute() (depth 3: main > compute > helper).
|
|
for _ in 0..3 {
|
|
if !ok {
|
|
break;
|
|
}
|
|
ok = sess.cont().is_ok() && sess.wait_for_stop(-1);
|
|
}
|
|
sess.refresh_on_stop();
|
|
assert!(ok);
|
|
assert!(sess.frames().len() >= 3, "n={}", sess.frames().len());
|
|
assert!(sess.frames()[0].function.contains("helper"));
|
|
assert!(sess.frames()[1].function.contains("compute"));
|
|
assert_eq!(sess.frames()[2].function, "main");
|
|
}
|
|
|
|
// 20. kill a running (spinning) target
|
|
#[test]
|
|
fn kill_running_target() {
|
|
let mut sess = GdbSession::default();
|
|
sess.start(spin().to_str().unwrap(), "").expect("start");
|
|
std::thread::sleep(std::time::Duration::from_millis(500));
|
|
assert!(sess.target_running());
|
|
sess.kill_target().expect("kill_target");
|
|
assert!(sess.wait_for_stop(5000));
|
|
assert!(!sess.target_running());
|
|
assert!(sess.is_alive());
|
|
}
|
|
|
|
// 21. restart after exit hits the breakpoint again
|
|
#[test]
|
|
fn restart_after_exit() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
let mut ok = sess.wait_for_stop(-1);
|
|
// The loop bp fires once per iteration (i=0,1,2): three continues
|
|
// run the program to completion.
|
|
for _ in 0..3 {
|
|
if !ok {
|
|
break;
|
|
}
|
|
ok = sess.cont().is_ok() && sess.wait_for_stop(-1);
|
|
}
|
|
assert!(ok);
|
|
assert_eq!(sess.state(), DbgState::Exited);
|
|
sess.restart().expect("restart");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.state(), DbgState::Stopped);
|
|
assert_eq!(sess.last_stop().frame.line, l_loop);
|
|
}
|
|
|
|
// 22. restart after kill: target runs again, can be killed again
|
|
#[test]
|
|
fn restart_after_kill() {
|
|
let mut sess = GdbSession::default();
|
|
sess.start(spin().to_str().unwrap(), "").expect("start");
|
|
std::thread::sleep(std::time::Duration::from_millis(300));
|
|
sess.kill_target().expect("kill 1");
|
|
assert!(sess.wait_for_stop(5000));
|
|
sess.restart().expect("restart");
|
|
std::thread::sleep(std::time::Duration::from_millis(300));
|
|
assert!(sess.target_running());
|
|
sess.kill_target().expect("kill 2");
|
|
assert!(sess.wait_for_stop(5000));
|
|
assert!(!sess.target_running());
|
|
}
|
|
|
|
// 23. break_on_main setting stops at main
|
|
#[test]
|
|
fn setting_break_on_main() {
|
|
let s = DebuggerSettings {
|
|
break_on_main: true,
|
|
..Default::default()
|
|
};
|
|
let mut sess = GdbSession::new(s);
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.state(), DbgState::Stopped);
|
|
assert_eq!(sess.last_stop().reason, "breakpoint-hit");
|
|
assert_eq!(sess.last_stop().frame.function, "main");
|
|
}
|
|
|
|
// 24. stop_on_entry setting stops at main
|
|
#[test]
|
|
fn setting_stop_on_entry() {
|
|
let s = DebuggerSettings {
|
|
stop_on_entry: true,
|
|
..Default::default()
|
|
};
|
|
let mut sess = GdbSession::new(s);
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.state(), DbgState::Stopped);
|
|
assert_eq!(sess.last_stop().frame.function, "main");
|
|
}
|
|
|
|
// 25. pretty-print: vector value comes back expanded
|
|
#[test]
|
|
fn setting_pretty_print() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let s = DebuggerSettings {
|
|
pretty_print: true,
|
|
..Default::default()
|
|
};
|
|
let mut sess = GdbSession::new(s);
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(-1));
|
|
sess.refresh_on_stop();
|
|
let v = sess.evaluate("vals").expect("evaluate vals");
|
|
assert!(v.contains('1') && v.contains('3'), "vals={}", v);
|
|
// And the setting is what got saved to disk.
|
|
let s2 = DebuggerSettings {
|
|
pretty_print: true,
|
|
..Default::default()
|
|
};
|
|
let p = tmp_path("conf2");
|
|
assert!(s2.save(p.to_str().unwrap()));
|
|
let mut s3 = DebuggerSettings::default();
|
|
assert!(s3.load(p.to_str().unwrap()) && s3.pretty_print);
|
|
}
|
|
|
|
// 26. settings file is actually read and applied (break_on_main via file)
|
|
#[test]
|
|
fn settings_file_applied() {
|
|
let s = DebuggerSettings {
|
|
gdb_path: "/usr/bin/gdb".to_string(),
|
|
break_on_main: true,
|
|
..Default::default()
|
|
};
|
|
let p = tmp_path("conf3");
|
|
s.save(p.to_str().unwrap());
|
|
let mut loaded = DebuggerSettings::default();
|
|
assert!(loaded.load(p.to_str().unwrap()) && loaded.break_on_main);
|
|
let mut sess = GdbSession::new(loaded);
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.last_stop().frame.function, "main");
|
|
}
|
|
|
|
// 27. target stdout/stderr captured
|
|
#[test]
|
|
fn target_output_captured() {
|
|
let mut sess = GdbSession::default();
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(10_000));
|
|
let out = sess.target_output();
|
|
assert!(out.contains("i=0 x=1 sum=1"), "out: {}", out);
|
|
assert!(out.contains("i=2 x=5 sum=9"), "out: {}", out);
|
|
assert!(out.contains("TOTAL total=61 sum=9"), "out: {}", out);
|
|
}
|
|
|
|
// 28. evaluate arithmetic at a stop
|
|
#[test]
|
|
fn evaluate_arithmetic() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
sess.wait_for_stop(-1);
|
|
let v = sess.evaluate("sum + 100").expect("sum+100");
|
|
assert_eq!(v, "100", "sum+100={}", v);
|
|
let v = sess.evaluate("vals[2]").expect("vals[2]");
|
|
assert_eq!(v, "3");
|
|
let v = sess.evaluate("vals.size()").expect("vals.size()");
|
|
assert_eq!(v, "3");
|
|
}
|
|
|
|
// 29. target args are passed through
|
|
#[test]
|
|
fn target_args_passed() {
|
|
let mut sess = GdbSession::default();
|
|
sess.start(console().to_str().unwrap(), "hello")
|
|
.expect("start");
|
|
assert!(sess.wait_for_stop(10_000));
|
|
assert!(
|
|
sess.target_output().contains("ARG=hello"),
|
|
"out: {}",
|
|
sess.target_output()
|
|
);
|
|
}
|
|
|
|
// 30. breakpoints persist to disk and load back
|
|
#[test]
|
|
fn bps_persist_to_disk() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.stage_breakpoint("compute");
|
|
let p = tmp_path("bps");
|
|
assert!(sess.save_breakpoints(p.to_str().unwrap()));
|
|
let mut sess2 = GdbSession::default();
|
|
assert!(sess2.load_breakpoints(p.to_str().unwrap()));
|
|
assert_eq!(sess2.breakpoints().len(), 2);
|
|
assert_eq!(sess2.breakpoints()[0].location, loc(&console_src(), l_loop));
|
|
assert_eq!(sess2.breakpoints()[1].location, "compute");
|
|
}
|
|
|
|
// 31. conditional breakpoint: fires only when i == 2, never before
|
|
#[test]
|
|
fn bp_conditional_only_when_true() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
let opts = Breakpoint {
|
|
kind: BpKind::Conditional,
|
|
condition: "i == 2".to_string(),
|
|
..Default::default()
|
|
};
|
|
assert!(sess.stage_breakpoint_opts(&loc(&console_src(), l_loop), &opts));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.state(), DbgState::Stopped);
|
|
assert_eq!(sess.last_stop().reason, "breakpoint-hit");
|
|
assert_eq!(sess.last_stop().frame.line, l_loop);
|
|
let v = sess.evaluate("i").expect("evaluate i");
|
|
assert_eq!(v, "2", "i={}", v);
|
|
// It must NOT have stopped at i=0/1: both iterations printed before
|
|
// the first (and only) stop.
|
|
let out = sess.target_output();
|
|
assert!(out.contains("i=0 x=1 sum=1"), "out: {}", out);
|
|
assert!(out.contains("i=1 x=3 sum=4"), "out: {}", out);
|
|
// Hit count from gdb: a conditional breakpoint counts stops only
|
|
// (reaches with a false condition are not hits) -> 1.
|
|
sess.refresh_breakpoints().expect("refresh");
|
|
let hits = sess
|
|
.breakpoints()
|
|
.iter()
|
|
.find(|b| b.line == l_loop)
|
|
.map(|b| b.hits)
|
|
.unwrap_or(-1);
|
|
assert_eq!(hits, 1, "hits={}", hits);
|
|
sess.cont().expect("cont");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.state(), DbgState::Exited);
|
|
}
|
|
|
|
// 32. ignore count: first 2 hits skipped, stops on the 3rd (i == 2)
|
|
#[test]
|
|
fn bp_ignore_count_skips() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
let opts = Breakpoint {
|
|
ignore_count: 2,
|
|
..Default::default()
|
|
};
|
|
assert!(sess.stage_breakpoint_opts(&loc(&console_src(), l_loop), &opts));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.state(), DbgState::Stopped);
|
|
assert_eq!(sess.last_stop().frame.line, l_loop);
|
|
let v = sess.evaluate("i").expect("evaluate i");
|
|
assert_eq!(v, "2", "i={}", v);
|
|
let out = sess.target_output();
|
|
assert!(out.contains("i=0 x=1 sum=1"), "out: {}", out);
|
|
assert!(out.contains("i=1 x=3 sum=4"), "out: {}", out);
|
|
// gdb counts every reach, ignored or not, in `times` -> 3.
|
|
sess.refresh_breakpoints().expect("refresh");
|
|
let hits = sess
|
|
.breakpoints()
|
|
.iter()
|
|
.find(|b| b.line == l_loop)
|
|
.map(|b| b.hits)
|
|
.unwrap_or(-1);
|
|
assert_eq!(hits, 3, "hits={}", hits);
|
|
sess.cont().expect("cont");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.state(), DbgState::Exited);
|
|
}
|
|
|
|
// 33. logpoint: prints its message at every hit, never stops
|
|
#[test]
|
|
fn bp_logpoint_prints_no_stop() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
let opts = Breakpoint {
|
|
kind: BpKind::Logpoint,
|
|
log_message: "LOG i={i}".to_string(),
|
|
..Default::default()
|
|
};
|
|
assert!(sess.stage_breakpoint_opts(&loc(&console_src(), l_loop), &opts));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(10_000));
|
|
// No stop at all: straight to a clean exit.
|
|
assert_eq!(sess.state(), DbgState::Exited);
|
|
assert_eq!(sess.exit_code(), 0);
|
|
// dprintf output arrives on gdb's console stream (-> gdb_log()).
|
|
let log = sess.gdb_log();
|
|
assert!(log.contains("LOG i=0"), "log: {}", log);
|
|
assert!(log.contains("LOG i=1"), "log: {}", log);
|
|
assert!(log.contains("LOG i=2"), "log: {}", log);
|
|
assert!(sess.target_output().contains("DONE sum=9"));
|
|
}
|
|
|
|
// 34. logpoint template with several placeholders and formats
|
|
#[test]
|
|
fn bp_logpoint_template() {
|
|
// Template conversion is pure string work: check it directly.
|
|
let (mut fmt, mut args) = (String::new(), Vec::new());
|
|
assert!(logpoint_to_printf(
|
|
"i={i} s={s:name} x={x:v}",
|
|
&mut fmt,
|
|
&mut args
|
|
));
|
|
assert_eq!(fmt, "i=%d s=%s x=%#x\\n");
|
|
assert_eq!(args.len(), 3);
|
|
assert_eq!(args[0], "i");
|
|
assert_eq!(args[1], "name");
|
|
assert_eq!(args[2], "v");
|
|
assert!(logpoint_to_printf("100% done", &mut fmt, &mut args));
|
|
assert_eq!(fmt, "100%% done\\n");
|
|
assert!(args.is_empty());
|
|
assert!(!logpoint_to_printf("oops {i", &mut fmt, &mut args));
|
|
assert!(!logpoint_to_printf("oops {}", &mut fmt, &mut args));
|
|
// And end to end: two placeholders print at the loop.
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
let opts = Breakpoint {
|
|
kind: BpKind::Logpoint,
|
|
log_message: "LP i={i} sum={sum}".to_string(),
|
|
..Default::default()
|
|
};
|
|
assert!(sess.stage_breakpoint_opts(&loc(&console_src(), l_loop), &opts));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(10_000));
|
|
assert_eq!(sess.state(), DbgState::Exited);
|
|
let log = sess.gdb_log();
|
|
assert!(log.contains("LP i=0 sum=0"), "log: {}", log);
|
|
assert!(log.contains("LP i=2 sum=4"), "log: {}", log);
|
|
}
|
|
|
|
// 35. edit a live breakpoint: plain -> conditional, takes effect
|
|
#[test]
|
|
fn bp_update_plain_to_conditional() {
|
|
let (l_loop, _, _, _, _, _) = marks();
|
|
let mut sess = GdbSession::default();
|
|
sess.stage_breakpoint(&loc(&console_src(), l_loop));
|
|
sess.start(console().to_str().unwrap(), "").expect("start");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.state(), DbgState::Stopped);
|
|
let v = sess.evaluate("i").expect("evaluate i");
|
|
assert_eq!(v, "0", "i={}", v); // plain: stops at 0
|
|
let loop_id = sess
|
|
.breakpoints()
|
|
.iter()
|
|
.find(|b| b.line == l_loop)
|
|
.map(|b| b.id)
|
|
.unwrap_or(0);
|
|
assert!(loop_id > 0);
|
|
let upd = Breakpoint {
|
|
kind: BpKind::Conditional,
|
|
condition: "i == 2".to_string(),
|
|
location: loc(&console_src(), l_loop),
|
|
..Default::default()
|
|
};
|
|
let out = sess.update_breakpoint(loop_id, &upd).expect("update");
|
|
assert_eq!(out.kind, BpKind::Conditional);
|
|
assert_eq!(out.condition, "i == 2");
|
|
sess.cont().expect("cont");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.state(), DbgState::Stopped);
|
|
assert_eq!(sess.last_stop().frame.line, l_loop);
|
|
let v = sess.evaluate("i").expect("evaluate i");
|
|
assert_eq!(v, "2", "i={}", v);
|
|
sess.cont().expect("cont");
|
|
assert!(sess.wait_for_stop(-1));
|
|
assert_eq!(sess.state(), DbgState::Exited);
|
|
}
|
|
|
|
// 36. handler fields persist to disk and load back; old two-field files
|
|
// still load as plain breakpoints.
|
|
#[test]
|
|
fn bp_handlers_persist_roundtrip() {
|
|
let (l_loop, _, _, _, _, l_helper) = marks();
|
|
let mut sess = GdbSession::default();
|
|
let mut c = Breakpoint {
|
|
kind: BpKind::Conditional,
|
|
..Default::default()
|
|
};
|
|
c.condition = "i == 2 || sum > 3".to_string(); // exercises | escaping
|
|
c.group = "loop".to_string();
|
|
let l = Breakpoint {
|
|
kind: BpKind::Logpoint,
|
|
log_message: "LOG i={i} | sum={sum}".to_string(),
|
|
group: "trace".to_string(),
|
|
..Default::default()
|
|
};
|
|
let p = Breakpoint {
|
|
ignore_count: 3,
|
|
..Default::default()
|
|
};
|
|
assert!(sess.stage_breakpoint_opts(&loc(&console_src(), l_loop), &c));
|
|
assert!(sess.stage_breakpoint_opts(&loc(&console_src(), l_helper), &l));
|
|
assert!(sess.stage_breakpoint_opts("compute", &p));
|
|
let path = tmp_path("bps2");
|
|
assert!(sess.save_breakpoints(path.to_str().unwrap()));
|
|
let mut sess2 = GdbSession::default();
|
|
assert!(sess2.load_breakpoints(path.to_str().unwrap()));
|
|
assert_eq!(sess2.breakpoints().len(), 3);
|
|
let b0 = sess2.breakpoints()[0].clone();
|
|
let b1 = sess2.breakpoints()[1].clone();
|
|
let b2 = sess2.breakpoints()[2].clone();
|
|
assert_eq!(b0.kind, BpKind::Conditional);
|
|
assert_eq!(b0.condition, "i == 2 || sum > 3");
|
|
assert_eq!(b0.group, "loop");
|
|
assert_eq!(b0.file, console_src().to_string_lossy());
|
|
assert_eq!(b0.line, l_loop);
|
|
assert_eq!(b1.kind, BpKind::Logpoint);
|
|
assert_eq!(b1.log_message, "LOG i={i} | sum={sum}");
|
|
assert_eq!(b1.group, "trace");
|
|
assert_eq!(b1.file, console_src().to_string_lossy());
|
|
assert_eq!(b1.line, l_helper);
|
|
assert_eq!(b2.kind, BpKind::Plain);
|
|
assert_eq!(b2.ignore_count, 3);
|
|
assert_eq!(b2.location, "compute");
|
|
assert!(b2.file.is_empty());
|
|
assert_eq!(b2.line, 0);
|
|
// Old format: location|enabled, nothing else.
|
|
let old_path = tmp_path("bps_old");
|
|
std::fs::write(
|
|
&old_path,
|
|
format!("{}|1\ncompute|0\n", loc(&console_src(), l_loop)),
|
|
)
|
|
.unwrap();
|
|
let mut sess3 = GdbSession::default();
|
|
assert!(sess3.load_breakpoints(old_path.to_str().unwrap()));
|
|
assert_eq!(sess3.breakpoints().len(), 2);
|
|
assert_eq!(sess3.breakpoints()[0].kind, BpKind::Plain);
|
|
assert!(sess3.breakpoints()[0].enabled);
|
|
assert_eq!(sess3.breakpoints()[0].file, console_src().to_string_lossy());
|
|
assert_eq!(sess3.breakpoints()[0].line, l_loop);
|
|
assert_eq!(sess3.breakpoints()[1].kind, BpKind::Plain);
|
|
assert!(!sess3.breakpoints()[1].enabled);
|
|
assert!(sess3.breakpoints()[1].file.is_empty());
|
|
// A bad condition is rejected and leaves no breakpoint behind.
|
|
let mut s4 = GdbSession::default();
|
|
s4.start_gdb_only().expect("start_gdb_only");
|
|
s4.load_executable(console().to_str().unwrap())
|
|
.expect("load_executable");
|
|
let bad = Breakpoint {
|
|
kind: BpKind::Conditional,
|
|
condition: "nosuchvar == 2".to_string(),
|
|
..Default::default()
|
|
};
|
|
let r = s4.add_breakpoint_opts(&loc(&console_src(), l_loop), &bad);
|
|
assert!(r.is_err(), "expected rejection");
|
|
assert!(!r.unwrap_err().is_empty());
|
|
assert!(s4.breakpoints().is_empty());
|
|
}
|