1use std::ffi::{OsStr, OsString};
2use std::io::{BufRead, BufReader};
3use std::process::{Command, Output, Stdio};
4
5use camino::Utf8Path;
6use tracing::debug;
7
8use super::debugger::DebuggerCommands;
9use super::{Debugger, Emit, ProcRes, TestCx, Truncated, WillExecute};
10use crate::debuggers::extract_gdb_version;
11use crate::util::ArgFileCommand;
12
13impl TestCx<'_> {
14 pub(super) fn run_debuginfo_test(&self) {
15 match self.variant.debugger.as_ref().unwrap() {
16 Debugger::Cdb => self.run_debuginfo_cdb_test(),
17 Debugger::Gdb => self.run_debuginfo_gdb_test(),
18 Debugger::Lldb => self.run_debuginfo_lldb_test(),
19 }
20 }
21
22 fn run_debuginfo_cdb_test(&self) {
23 let exe_file = self.make_exe_name();
24
25 let pdb_file = exe_file.with_extension(".pdb");
34 if pdb_file.exists() {
35 std::fs::remove_file(pdb_file).unwrap();
36 }
37
38 let should_run = self.run_if_enabled();
40 let compile_result = self.compile_test(should_run, Emit::None);
41 if !compile_result.status.success() {
42 self.fatal_proc_rec("compilation failed!", &compile_result);
43 }
44 if let WillExecute::Disabled = should_run {
45 return;
46 }
47
48 let dbg_cmds =
50 DebuggerCommands::parse_from(&self.testpaths.file, "cdb", self.variant.revision())
51 .unwrap_or_else(|e| self.fatal(&e));
52
53 let mut script_str = String::with_capacity(2048);
55 script_str.push_str("version\n"); script_str.push_str(".nvlist\n"); let mut js_extension = self.testpaths.file.clone();
61 js_extension.set_extension("cdb.js");
62 if js_extension.exists() {
63 script_str.push_str(&format!(".scriptload \"{}\"\n", js_extension));
64 }
65
66 let source_file_name = self.testpaths.file.file_name().unwrap();
68 for line in &dbg_cmds.breakpoint_lines {
69 script_str.push_str(&format!("bp `{}:{}`\n", source_file_name, line));
70 }
71
72 for line in &dbg_cmds.commands {
74 script_str.push_str(line);
75 script_str.push('\n');
76 }
77
78 script_str.push_str("qq\n"); debug!("script_str = {}", script_str);
82 self.dump_output_file(&script_str, "debugger.script");
83 let debugger_script = self.make_out_name("debugger.script");
84
85 let cdb_path = &self.config.cdb.as_ref().unwrap();
86 let mut cdb = Command::new(cdb_path);
87 cdb.arg("-lines") .arg("-cf")
89 .arg(&debugger_script)
90 .arg(&exe_file);
91
92 let debugger_run_result = self.compose_and_run(
93 cdb,
94 self.config.target_run_lib_path.as_path(),
95 None, None, );
98
99 if !debugger_run_result.status.success() {
100 self.fatal_proc_rec("Error while running CDB", &debugger_run_result);
101 }
102
103 if let Err(e) = dbg_cmds.check_output(&debugger_run_result) {
104 self.fatal_proc_rec(&e, &debugger_run_result);
105 }
106 }
107
108 fn run_debuginfo_gdb_test(&self) {
109 let dbg_cmds =
110 DebuggerCommands::parse_from(&self.testpaths.file, "gdb", self.variant.revision())
111 .unwrap_or_else(|e| self.fatal(&e));
112 let mut cmds = dbg_cmds.commands.join("\n");
113
114 let should_run = self.run_if_enabled();
116 let compiler_run_result = self.compile_test(should_run, Emit::None);
117 if !compiler_run_result.status.success() {
118 self.fatal_proc_rec("compilation failed!", &compiler_run_result);
119 }
120 if let WillExecute::Disabled = should_run {
121 return;
122 }
123
124 let exe_file = self.make_exe_name();
125
126 let debugger_run_result;
127 if let Some(android_cross_path) = self.config.android_cross_path.as_deref() {
130 cmds = cmds.replace("run", "continue");
131
132 let mut script_str = String::with_capacity(2048);
134 script_str.push_str("py import debugger_tester\n");
135 script_str.push_str("set charset UTF-8\n");
136 script_str.push_str(&format!("set sysroot {android_cross_path}\n"));
137 script_str.push_str(&format!("file {}\n", exe_file));
138 script_str.push_str("target remote :5039\n");
139 script_str.push_str(&format!(
140 "set solib-search-path \
141 ./{}/stage2/lib/rustlib/{}/lib/\n",
142 self.config.host, self.config.target
143 ));
144 for line in &dbg_cmds.breakpoint_lines {
145 script_str.push_str(
146 format!("break {}:{}\n", self.testpaths.file.file_name().unwrap(), *line)
147 .as_str(),
148 );
149 }
150 script_str.push_str(&cmds);
151 script_str.push_str("\nquit\n");
152
153 debug!("script_str = {}", script_str);
154 self.dump_output_file(&script_str, "debugger.script");
155
156 let adb_path = self.config.adb_path.as_ref().expect("`adb_path` must be specified");
159 let adb_test_dir =
160 self.config.adb_test_dir.as_ref().expect("`adb_test_dir` must be specified");
161
162 Command::new(adb_path)
163 .arg("push")
164 .arg(&exe_file)
165 .arg(adb_test_dir)
166 .status()
167 .unwrap_or_else(|e| panic!("failed to exec `{adb_path:?}`: {e:?}"));
168
169 Command::new(adb_path)
170 .args(&["forward", "tcp:5039", "tcp:5039"])
171 .status()
172 .unwrap_or_else(|e| panic!("failed to exec `{adb_path:?}`: {e:?}"));
173
174 let adb_arg = format!(
175 "export LD_LIBRARY_PATH={}; \
176 gdbserver{} :5039 {}/{}",
177 adb_test_dir,
178 if self.config.target.contains("aarch64") { "64" } else { "" },
179 adb_test_dir,
180 exe_file.file_name().unwrap()
181 );
182
183 debug!("adb arg: {}", adb_arg);
184 let mut adb = Command::new(adb_path)
185 .args(&["shell", &adb_arg])
186 .stdout(Stdio::piped())
187 .stderr(Stdio::inherit())
188 .spawn()
189 .unwrap_or_else(|e| panic!("failed to exec `{adb_path:?}`: {e:?}"));
190
191 let mut stdout = BufReader::new(adb.stdout.take().unwrap());
195 let mut line = String::new();
196 loop {
197 line.clear();
198 stdout.read_line(&mut line).unwrap();
199 if line.starts_with("Listening on port 5039") {
200 break;
201 }
202 }
203 drop(stdout);
204
205 let mut debugger_script = OsString::from("-command=");
206 debugger_script.push(self.make_out_name("debugger.script"));
207 let debugger_opts: &[&OsStr] =
208 &["-quiet".as_ref(), "-batch".as_ref(), "-nx".as_ref(), &debugger_script];
209
210 let gdb_path = self.config.gdb.as_ref().unwrap();
211 let mut gdb = Command::new(&gdb_path);
212 gdb.args(debugger_opts);
213
214 let cmdline = format!("{gdb:?}");
215 self.logv(format_args!("executing {cmdline}"));
216
217 let Output { status, stdout, stderr } =
218 gdb.output().unwrap_or_else(|e| panic!("failed to exec `{gdb_path:?}`: {e:?}"));
219
220 debugger_run_result = ProcRes {
221 status,
222 stdout: String::from_utf8(stdout).unwrap(),
223 stderr: String::from_utf8(stderr).unwrap(),
224 truncated: Truncated::No,
225 cmdline,
226 };
227 if adb.kill().is_err() {
228 writeln!(self.stdout, "Adb process is already finished.");
229 }
230 } else {
231 let rust_pp_module_abs_path = self.config.src_root.join("src").join("etc");
232 let mut script_str = String::with_capacity(2048);
234 script_str.push_str("py import debugger_tester\n");
235 script_str.push_str("set charset UTF-8\n");
236 script_str.push_str("show version\n");
237
238 match self.config.gdb_version {
239 Some(version) => {
240 writeln!(
241 self.stdout,
242 "NOTE: compiletest thinks it is using GDB version {}",
243 version
244 );
245
246 if !self.props.disable_gdb_pretty_printers
247 && version > extract_gdb_version("7.4").unwrap()
248 {
249 script_str.push_str(&format!(
252 "add-auto-load-safe-path {}\n",
253 rust_pp_module_abs_path.as_str().replace(r"\", r"\\")
254 ));
255
256 script_str.push_str(&format!(
260 "add-auto-load-safe-path {}\n",
261 self.output_base_dir().as_str().replace(r"\", r"\\")
262 ));
263
264 #[cfg(target_os = "windows")]
268 {
269 script_str.push_str(&format!(
270 "source {}\n",
271 self.config
272 .src_root
273 .join("src/etc/gdb_load_rust_pretty_printers.py")
274 ));
275 }
276 }
277 }
278 _ => {
279 writeln!(
280 self.stdout,
281 "NOTE: compiletest does not know which version of \
282 GDB it is using"
283 );
284 }
285 }
286
287 script_str.push_str("set print pretty off\n");
290
291 script_str.push_str(&format!(
293 "directory {}\n",
294 rust_pp_module_abs_path.as_str().replace(r"\", r"\\")
295 ));
296
297 script_str.push_str(&format!("file {}\n", exe_file.as_str().replace(r"\", r"\\")));
299
300 script_str.push_str("set language rust\n");
302
303 for line in &dbg_cmds.breakpoint_lines {
305 script_str.push_str(&format!(
306 "break '{}':{}\n",
307 self.testpaths.file.file_name().unwrap(),
308 *line
309 ));
310 }
311
312 script_str.push_str(&cmds);
313 script_str.push_str("\nrepr_finalize\n");
315
316 script_str.push_str("\nquit\n");
317
318 debug!("script_str = {}", script_str);
319 self.dump_output_file(&script_str, "debugger.script");
320
321 let mut debugger_script = OsString::from("-command=");
322 debugger_script.push(self.make_out_name("debugger.script"));
323
324 let debugger_opts: &[&OsStr] =
325 &["-quiet".as_ref(), "-batch".as_ref(), "-nx".as_ref(), &debugger_script];
326
327 let mut gdb = Command::new(self.config.gdb.as_ref().unwrap());
328
329 let gdb_input_data_path = self.config.src_root.join(format!(
330 "{}/gdb_input/{}.json",
331 self.testpaths.file.parent().unwrap(),
332 get_target_file_name(&self.config.target)
333 ));
334
335 let pythonpath = with_pythonpath_prepended(&rust_pp_module_abs_path);
336 gdb.args(debugger_opts)
337 .env("PYTHONPATH", pythonpath)
338 .env("DEBUGGER_TESTER_DEBUGGER", "gdb")
339 .env("DEBUGGER_TESTER_BLESS_TEST_DATA", if self.config.bless { "1" } else { "0" })
340 .env("DEBUGGER_TESTER_TARGET_TRIPLE", &self.config.target)
341 .env("DEBUGGER_TESTER_INPUT_DATA_PATH", gdb_input_data_path);
342
343 debugger_run_result =
344 self.compose_and_run(gdb, self.config.target_run_lib_path.as_path(), None, None);
345 }
346
347 if !debugger_run_result.status.success() {
348 self.fatal_proc_rec("gdb failed to execute", &debugger_run_result);
349 }
350
351 if let Err(e) = dbg_cmds.check_output(&debugger_run_result) {
352 self.fatal_proc_rec(&e, &debugger_run_result);
353 }
354 }
355
356 fn run_debuginfo_lldb_test(&self) {
357 let Some(ref lldb) = self.config.lldb else {
358 self.fatal("Can't run LLDB test because LLDB's path is not set.");
359 };
360
361 let should_run = self.run_if_enabled();
363 let compile_result = self.compile_test(should_run, Emit::None);
364 if !compile_result.status.success() {
365 self.fatal_proc_rec("compilation failed!", &compile_result);
366 }
367 if let WillExecute::Disabled = should_run {
368 return;
369 }
370
371 let exe_file = self.make_exe_name();
372
373 match self.config.lldb_version {
374 Some(ref version) => {
375 writeln!(
376 self.stdout,
377 "NOTE: compiletest thinks it is using LLDB version: {:?}",
378 version
379 );
380 }
381 _ => {
382 writeln!(
383 self.stdout,
384 "NOTE: compiletest does not know which version of \
385 LLDB it is using"
386 );
387 }
388 }
389
390 let dbg_cmds =
392 DebuggerCommands::parse_from(&self.testpaths.file, "lldb", self.variant.revision())
393 .unwrap_or_else(|e| self.fatal(&e));
394
395 let mut script_str = String::from("settings set auto-confirm true\n");
398
399 if self.config.host.contains("darwin") {
425 script_str.push_str("settings set target.inherit-tcc true\n");
426 }
427
428 script_str.push_str("version\n");
430
431 let rust_pp_module_abs_path = self.config.src_root.join("src/etc");
433
434 script_str.push_str(&format!(
435 "command script import {}/lldb_lookup.py\n",
436 rust_pp_module_abs_path
437 ));
438 script_str.push_str("script print(lldb_lookup.FEATURE_FLAGS)\n");
439
440 let source_file_name = self.testpaths.file.file_name().unwrap();
442 for line in &dbg_cmds.breakpoint_lines {
443 script_str.push_str(&format!(
444 "breakpoint set --file '{}' --line {}\n",
445 source_file_name, line
446 ));
447 }
448
449 for line in &dbg_cmds.commands {
451 script_str.push_str(line);
452 script_str.push('\n');
453 }
454
455 script_str.push_str("\nquit\n");
457
458 debug!("script_str = {}", script_str);
460 self.dump_output_file(&script_str, "debugger.script");
461 let debugger_script = self.make_out_name("debugger.script");
462
463 let debugger_run_result = self.run_lldb(lldb, &exe_file, &debugger_script);
465
466 if !debugger_run_result.status.success() {
467 self.fatal_proc_rec("Error while running LLDB", &debugger_run_result);
468 }
469
470 if let Err(e) = dbg_cmds.check_output(&debugger_run_result) {
471 self.fatal_proc_rec(&e, &debugger_run_result);
472 }
473 }
474
475 fn run_lldb(
476 &self,
477 lldb: &Utf8Path,
478 test_executable: &Utf8Path,
479 debugger_script: &Utf8Path,
480 ) -> ProcRes {
481 let rust_pp_module_abs_path = self.config.src_root.join("src/etc");
483 let pythonpath = with_pythonpath_prepended(&rust_pp_module_abs_path);
484 let path = prepend_to_path(&self.config.target_run_lib_path);
486
487 let lldb_input_data_path = self.config.src_root.join(format!(
489 "{}/lldb_input/{}.json",
490 self.testpaths.file.parent().unwrap(),
491 get_target_file_name(&self.config.target)
492 ));
493
494 let mut cmd = ArgFileCommand::new(lldb);
495 cmd.arg("--batch") .arg("--one-line")
497 .arg("script --language python -- import debugger_tester; debugger_tester.main()")
498 .env("DEBUGGER_TESTER_TARGET_PATH", test_executable)
499 .env("DEBUGGER_TESTER_SCRIPT_PATH", debugger_script)
500 .env("DEBUGGER_TESTER_INPUT_DATA_PATH", lldb_input_data_path)
501 .env("DEBUGGER_TESTER_BLESS_TEST_DATA", if self.config.bless { "1" } else { "0" })
502 .env("DEBUGGER_TESTER_TARGET_TRIPLE", &self.config.target)
503 .env("DEBUGGER_TESTER_DEBUGGER", "lldb")
504 .env("PYTHONUNBUFFERED", "1") .env("PYTHONPATH", pythonpath)
506 .env("PATH", path);
507
508 self.run_command_to_procres(cmd)
509 }
510}
511
512fn with_pythonpath_prepended(some_path: &Utf8Path) -> String {
513 if let Ok(pp) = std::env::var("PYTHONPATH") {
515 #[cfg(target_os = "windows")]
516 {
517 format!("{pp};{some_path}")
518 }
519 #[cfg(not(target_os = "windows"))]
520 {
521 format!("{pp}:{some_path}")
522 }
523 } else {
524 some_path.to_string()
525 }
526}
527
528fn prepend_to_path(some_path: &Utf8Path) -> String {
529 if let Ok(path) = std::env::var("PATH") {
530 #[cfg(target_os = "windows")]
531 {
532 format!("{some_path};{path}")
533 }
534 #[cfg(not(target_os = "windows"))]
535 {
536 format!("{some_path}:{path}")
537 }
538 } else {
539 some_path.to_string()
540 }
541}
542
543fn get_target_file_name(target_name: &str) -> &'static str {
546 if target_name.ends_with("windows-msvc") {
547 "windows_msvc"
548 } else if target_name.ends_with("windows-gnu") || target_name.ends_with("windows-gnullvm") {
549 "windows_gnu"
550 } else {
551 "non_windows"
552 }
553}