Skip to content

Commit ee25b6b

Browse files
debuginfo: Allow to activate GDB pretty printers in autotests.
1 parent 9669c6d commit ee25b6b

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
@@ -323,7 +323,12 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
323323
};
324324

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

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

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

337-
let mut proc_args;
338342
let debugger_run_result;
339343
match config.target.as_slice() {
340344
"arm-linux-androideabi" => {
@@ -454,6 +458,12 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
454458
}
455459

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

479+
if use_gdb_pretty_printer {
480+
// Only emit the gdb auto-loading script if pretty printers
481+
// should actually be loaded
482+
dump_gdb_autoload_script(config, testfile);
483+
}
484+
469485
// run debugger script with gdb
470486
#[cfg(windows)]
471487
fn debugger() -> String {
@@ -483,16 +499,27 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
483499
vec!("-quiet".to_string(),
484500
"-batch".to_string(),
485501
"-nx".to_string(),
502+
// Add the directory containing the pretty printers to
503+
// GDB's script auto loading safe path ...
504+
format!("-iex=add-auto-load-safe-path {}",
505+
rust_pp_module_abs_path.as_slice()),
506+
// ... and also the test directory
507+
format!("-iex=add-auto-load-safe-path {}",
508+
config.build_base.as_str().unwrap()),
486509
format!("-command={}", debugger_script.as_str().unwrap()),
487510
exe_file.as_str().unwrap().to_string());
488-
proc_args = ProcArgs {
511+
512+
let proc_args = ProcArgs {
489513
prog: debugger(),
490514
args: debugger_opts,
491515
};
516+
517+
let environment = vec![("PYTHONPATH".to_string(), rust_pp_module_abs_path)];
518+
492519
debugger_run_result = compose_and_run(config,
493520
testfile,
494521
proc_args,
495-
Vec::new(),
522+
environment,
496523
config.run_lib_path.as_slice(),
497524
None,
498525
None);
@@ -504,6 +531,32 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
504531
}
505532

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

509562
fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path) {
@@ -533,7 +586,8 @@ fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path)
533586
let DebuggerCommands {
534587
commands,
535588
check_lines,
536-
breakpoint_lines
589+
breakpoint_lines,
590+
..
537591
} = parse_debugger_commands(testfile, "lldb");
538592

539593
// Write debugger script:
@@ -619,6 +673,7 @@ struct DebuggerCommands {
619673
commands: Vec<String>,
620674
check_lines: Vec<String>,
621675
breakpoint_lines: Vec<uint>,
676+
use_gdb_pretty_printer: bool
622677
}
623678

624679
fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
@@ -631,6 +686,7 @@ fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
631686
let mut breakpoint_lines = vec!();
632687
let mut commands = vec!();
633688
let mut check_lines = vec!();
689+
let mut use_gdb_pretty_printer = false;
634690
let mut counter = 1;
635691
let mut reader = BufferedReader::new(File::open(file_path).unwrap());
636692
for line in reader.lines() {
@@ -640,6 +696,10 @@ fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
640696
breakpoint_lines.push(counter);
641697
}
642698

699+
if line.as_slice().contains("gdb-use-pretty-printer") {
700+
use_gdb_pretty_printer = true;
701+
}
702+
643703
header::parse_name_value_directive(
644704
line.as_slice(),
645705
command_directive.as_slice()).map(|cmd| {
@@ -663,7 +723,8 @@ fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
663723
DebuggerCommands {
664724
commands: commands,
665725
check_lines: check_lines,
666-
breakpoint_lines: breakpoint_lines
726+
breakpoint_lines: breakpoint_lines,
727+
use_gdb_pretty_printer: use_gdb_pretty_printer,
667728
}
668729
}
669730

0 commit comments

Comments
 (0)