Skip to content

Commit 34d76d4

Browse files
debuginfo: Allow to activate GDB pretty printers in autotests.
1 parent 1a53c00 commit 34d76d4

File tree

1 file changed

+67
-6
lines changed

1 file changed

+67
-6
lines changed

src/compiletest/runtest.rs

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,12 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
322322
};
323323

324324
let config = &mut config;
325-
let DebuggerCommands { commands, check_lines, .. } = parse_debugger_commands(testfile, "gdb");
325+
let DebuggerCommands {
326+
commands,
327+
check_lines,
328+
use_gdb_pretty_printer,
329+
..
330+
} = parse_debugger_commands(testfile, "gdb");
326331
let mut cmds = commands.connect("\n");
327332

328333
// compile test file (it should have 'compile-flags:-g' in the header)
@@ -333,7 +338,6 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
333338

334339
let exe_file = make_exe_name(config, testfile);
335340

336-
let mut proc_args;
337341
let debugger_run_result;
338342
match config.target.as_slice() {
339343
"arm-linux-androideabi" => {
@@ -453,6 +457,12 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
453457
}
454458

455459
_=> {
460+
let rust_src_root = find_rust_src_root(config).expect("Could not find Rust source root");
461+
let rust_pp_module_rel_path = Path::new("./src/etc");
462+
let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
463+
.as_str()
464+
.unwrap()
465+
.to_string();
456466
// write debugger script
457467
let script_str = [
458468
"set charset UTF-8".to_string(),
@@ -465,6 +475,12 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
465475
script_str.as_slice(),
466476
"debugger.script");
467477

478+
if use_gdb_pretty_printer {
479+
// Only emit the gdb auto-loading script if pretty printers
480+
// should actually be loaded
481+
dump_gdb_autoload_script(config, testfile);
482+
}
483+
468484
// run debugger script with gdb
469485
#[cfg(windows)]
470486
fn debugger() -> String {
@@ -482,16 +498,27 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
482498
vec!("-quiet".to_string(),
483499
"-batch".to_string(),
484500
"-nx".to_string(),
501+
// Add the directory containing the pretty printers to
502+
// GDB's script auto loading safe path ...
503+
format!("-iex=add-auto-load-safe-path {}",
504+
rust_pp_module_abs_path.as_slice()),
505+
// ... and also the test directory
506+
format!("-iex=add-auto-load-safe-path {}",
507+
config.build_base.as_str().unwrap()),
485508
format!("-command={}", debugger_script.as_str().unwrap()),
486509
exe_file.as_str().unwrap().to_string());
487-
proc_args = ProcArgs {
510+
511+
let proc_args = ProcArgs {
488512
prog: debugger(),
489513
args: debugger_opts,
490514
};
515+
516+
let environment = vec![("PYTHONPATH".to_string(), rust_pp_module_abs_path)];
517+
491518
debugger_run_result = compose_and_run(config,
492519
testfile,
493520
proc_args,
494-
Vec::new(),
521+
environment,
495522
config.run_lib_path.as_slice(),
496523
None,
497524
None);
@@ -503,6 +530,32 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
503530
}
504531

505532
check_debugger_output(&debugger_run_result, check_lines.as_slice());
533+
534+
fn dump_gdb_autoload_script(config: &Config, testfile: &Path) {
535+
let mut script_path = output_base_name(config, testfile);
536+
let mut script_file_name = script_path.filename().unwrap().to_vec();
537+
script_file_name.push_all("-gdb.py".as_bytes());
538+
script_path.set_filename(script_file_name.as_slice());
539+
540+
let script_content = "import gdb_rust_pretty_printing\n\
541+
gdb_rust_pretty_printing.register_printers(gdb.current_objfile())\n"
542+
.as_bytes();
543+
544+
File::create(&script_path).write(script_content).unwrap();
545+
}
546+
}
547+
548+
fn find_rust_src_root(config: &Config) -> Option<Path> {
549+
let mut path = config.src_base.clone();
550+
let path_postfix = Path::new("src/etc/lldb_batchmode.py");
551+
552+
while path.pop() {
553+
if path.join(path_postfix.clone()).is_file() {
554+
return Some(path);
555+
}
556+
}
557+
558+
return None;
506559
}
507560

508561
fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path) {
@@ -532,7 +585,8 @@ fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path)
532585
let DebuggerCommands {
533586
commands,
534587
check_lines,
535-
breakpoint_lines
588+
breakpoint_lines,
589+
..
536590
} = parse_debugger_commands(testfile, "lldb");
537591

538592
// Write debugger script:
@@ -618,6 +672,7 @@ struct DebuggerCommands {
618672
commands: Vec<String>,
619673
check_lines: Vec<String>,
620674
breakpoint_lines: Vec<uint>,
675+
use_gdb_pretty_printer: bool
621676
}
622677

623678
fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
@@ -630,6 +685,7 @@ fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
630685
let mut breakpoint_lines = vec!();
631686
let mut commands = vec!();
632687
let mut check_lines = vec!();
688+
let mut use_gdb_pretty_printer = false;
633689
let mut counter = 1;
634690
let mut reader = BufferedReader::new(File::open(file_path).unwrap());
635691
for line in reader.lines() {
@@ -639,6 +695,10 @@ fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
639695
breakpoint_lines.push(counter);
640696
}
641697

698+
if line.as_slice().contains("gdb-use-pretty-printer") {
699+
use_gdb_pretty_printer = true;
700+
}
701+
642702
header::parse_name_value_directive(
643703
line.as_slice(),
644704
command_directive.as_slice()).map(|cmd| {
@@ -662,7 +722,8 @@ fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
662722
DebuggerCommands {
663723
commands: commands,
664724
check_lines: check_lines,
665-
breakpoint_lines: breakpoint_lines
725+
breakpoint_lines: breakpoint_lines,
726+
use_gdb_pretty_printer: use_gdb_pretty_printer,
666727
}
667728
}
668729

0 commit comments

Comments
 (0)