Skip to content

Commit 148366d

Browse files
debuginfo: Allow to activate GDB pretty printers in autotests.
1 parent 6de570f commit 148366d

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

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

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

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

338-
let mut proc_args;
339343
let debugger_run_result;
340344
match config.target.as_slice() {
341345
"arm-linux-androideabi" => {
@@ -455,6 +459,12 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
455459
}
456460

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

480+
if use_gdb_pretty_printer {
481+
// Only emit the gdb auto-loading script if pretty printers
482+
// should actually be loaded
483+
dump_gdb_autoload_script(config, testfile);
484+
}
485+
470486
// run debugger script with gdb
471487
#[cfg(windows)]
472488
fn debugger() -> String {
@@ -484,16 +500,27 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
484500
vec!("-quiet".to_string(),
485501
"-batch".to_string(),
486502
"-nx".to_string(),
503+
// Add the directory containing the pretty printers to
504+
// GDB's script auto loading safe path ...
505+
format!("-iex=add-auto-load-safe-path {}",
506+
rust_pp_module_abs_path.as_slice()),
507+
// ... and also the test directory
508+
format!("-iex=add-auto-load-safe-path {}",
509+
config.build_base.as_str().unwrap()),
487510
format!("-command={}", debugger_script.as_str().unwrap()),
488511
exe_file.as_str().unwrap().to_string());
489-
proc_args = ProcArgs {
512+
513+
let proc_args = ProcArgs {
490514
prog: debugger(),
491515
args: debugger_opts,
492516
};
517+
518+
let environment = vec![("PYTHONPATH".to_string(), rust_pp_module_abs_path)];
519+
493520
debugger_run_result = compose_and_run(config,
494521
testfile,
495522
proc_args,
496-
Vec::new(),
523+
environment,
497524
config.run_lib_path.as_slice(),
498525
None,
499526
None);
@@ -505,6 +532,32 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
505532
}
506533

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

510563
fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path) {
@@ -534,7 +587,8 @@ fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path)
534587
let DebuggerCommands {
535588
commands,
536589
check_lines,
537-
breakpoint_lines
590+
breakpoint_lines,
591+
..
538592
} = parse_debugger_commands(testfile, "lldb");
539593

540594
// Write debugger script:
@@ -620,6 +674,7 @@ struct DebuggerCommands {
620674
commands: Vec<String>,
621675
check_lines: Vec<String>,
622676
breakpoint_lines: Vec<uint>,
677+
use_gdb_pretty_printer: bool
623678
}
624679

625680
fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
@@ -632,6 +687,7 @@ fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
632687
let mut breakpoint_lines = vec!();
633688
let mut commands = vec!();
634689
let mut check_lines = vec!();
690+
let mut use_gdb_pretty_printer = false;
635691
let mut counter = 1;
636692
let mut reader = BufferedReader::new(File::open(file_path).unwrap());
637693
for line in reader.lines() {
@@ -641,6 +697,10 @@ fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
641697
breakpoint_lines.push(counter);
642698
}
643699

700+
if line.as_slice().contains("gdb-use-pretty-printer") {
701+
use_gdb_pretty_printer = true;
702+
}
703+
644704
header::parse_name_value_directive(
645705
line.as_slice(),
646706
command_directive.as_slice()).map(|cmd| {
@@ -664,7 +724,8 @@ fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
664724
DebuggerCommands {
665725
commands: commands,
666726
check_lines: check_lines,
667-
breakpoint_lines: breakpoint_lines
727+
breakpoint_lines: breakpoint_lines,
728+
use_gdb_pretty_printer: use_gdb_pretty_printer,
668729
}
669730
}
670731

0 commit comments

Comments
 (0)