Skip to content

Commit a3e44c7

Browse files
authored
Rollup merge of rust-lang#148099 - Zalathar:debuggers, r=jieyouxu
Prepare to move debugger discovery from compiletest to bootstrap For a while I've been wanting to move debugger discovery out of compiletest and into bootstrap, so that bootstrap would be responsible for deciding which debugger to use, and compiletest would faithfully use that debugger with no extra magic (and no horrible config-duplicating hacks). Making that change is complicated, and eventually I had so many intertwined preparatory changes that I split them off into this PR, so that it can be reviewed and tested as a smaller chunk. --- To avoid scope creep, the changes in this PR try to move code as-is as much as possible, even if the moved code could arguably benefit from further cleanups. And in many cases, that code will need to be further overhauled anyway when discovery steps are actually moved out of compiletest.
2 parents 4192223 + 4587ea7 commit a3e44c7

File tree

10 files changed

+128
-57
lines changed

10 files changed

+128
-57
lines changed

src/bootstrap/src/core/build_steps/test.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use crate::core::builder::{
2929
};
3030
use crate::core::config::TargetSelection;
3131
use crate::core::config::flags::{Subcommand, get_completion, top_level_help};
32+
use crate::core::debuggers;
3233
use crate::utils::build_stamp::{self, BuildStamp};
3334
use crate::utils::exec::{BootstrapCommand, command};
3435
use crate::utils::helpers::{
@@ -38,8 +39,6 @@ use crate::utils::helpers::{
3839
use crate::utils::render_tests::{add_flags_and_try_run_tests, try_run_tests};
3940
use crate::{CLang, CodegenBackendKind, DocTests, GitRepo, Mode, PathSet, envify};
4041

41-
const ADB_TEST_DIR: &str = "/data/local/tmp/work";
42-
4342
/// Runs `cargo test` on various internal tools used by bootstrap.
4443
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4544
pub struct CrateBootstrap {
@@ -2078,27 +2077,24 @@ Please disable assertions with `rust.debug-assertions = false`.
20782077

20792078
cmd.arg("--python").arg(builder.python());
20802079

2081-
if let Some(ref gdb) = builder.config.gdb {
2082-
cmd.arg("--gdb").arg(gdb);
2083-
}
2084-
2085-
let lldb_exe = builder.config.lldb.clone().unwrap_or_else(|| PathBuf::from("lldb"));
2086-
let lldb_version = command(&lldb_exe)
2087-
.allow_failure()
2088-
.arg("--version")
2089-
.run_capture(builder)
2090-
.stdout_if_ok()
2091-
.and_then(|v| if v.trim().is_empty() { None } else { Some(v) });
2092-
if let Some(ref vers) = lldb_version {
2093-
cmd.arg("--lldb-version").arg(vers);
2094-
let lldb_python_dir = command(&lldb_exe)
2095-
.allow_failure()
2096-
.arg("-P")
2097-
.run_capture_stdout(builder)
2098-
.stdout_if_ok()
2099-
.map(|p| p.lines().next().expect("lldb Python dir not found").to_string());
2100-
if let Some(ref dir) = lldb_python_dir {
2101-
cmd.arg("--lldb-python-dir").arg(dir);
2080+
if mode == "debuginfo" {
2081+
if let Some(debuggers::Gdb { gdb }) = debuggers::discover_gdb(builder) {
2082+
cmd.arg("--gdb").arg(gdb);
2083+
}
2084+
2085+
if let Some(debuggers::Android { adb_path, adb_test_dir, android_cross_path }) =
2086+
debuggers::discover_android(builder, target)
2087+
{
2088+
cmd.arg("--adb-path").arg(adb_path);
2089+
cmd.arg("--adb-test-dir").arg(adb_test_dir);
2090+
cmd.arg("--android-cross-path").arg(android_cross_path);
2091+
}
2092+
2093+
if let Some(debuggers::Lldb { lldb_version, lldb_python_dir }) =
2094+
debuggers::discover_lldb(builder)
2095+
{
2096+
cmd.arg("--lldb-version").arg(lldb_version);
2097+
cmd.arg("--lldb-python-dir").arg(lldb_python_dir);
21022098
}
21032099
}
21042100

@@ -2332,16 +2328,6 @@ Please disable assertions with `rust.debug-assertions = false`.
23322328

23332329
cmd.env("RUST_TEST_TMPDIR", builder.tempdir());
23342330

2335-
cmd.arg("--adb-path").arg("adb");
2336-
cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR);
2337-
if target.contains("android") && !builder.config.dry_run() {
2338-
// Assume that cc for this target comes from the android sysroot
2339-
cmd.arg("--android-cross-path")
2340-
.arg(builder.cc(target).parent().unwrap().parent().unwrap());
2341-
} else {
2342-
cmd.arg("--android-cross-path").arg("");
2343-
}
2344-
23452331
if builder.config.cmd.rustfix_coverage() {
23462332
cmd.arg("--rustfix-coverage");
23472333
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::path::PathBuf;
2+
3+
use crate::core::builder::Builder;
4+
use crate::core::config::TargetSelection;
5+
6+
pub(crate) struct Android {
7+
pub(crate) adb_path: &'static str,
8+
pub(crate) adb_test_dir: &'static str,
9+
pub(crate) android_cross_path: PathBuf,
10+
}
11+
12+
pub(crate) fn discover_android(builder: &Builder<'_>, target: TargetSelection) -> Option<Android> {
13+
let adb_path = "adb";
14+
// See <https://github.com/rust-lang/rust/pull/102755>.
15+
let adb_test_dir = "/data/local/tmp/work";
16+
17+
let android_cross_path = if target.contains("android") && !builder.config.dry_run() {
18+
builder.cc(target).parent().unwrap().parent().unwrap().to_owned()
19+
} else {
20+
PathBuf::new()
21+
};
22+
23+
Some(Android { adb_path, adb_test_dir, android_cross_path })
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::path::Path;
2+
3+
use crate::core::builder::Builder;
4+
5+
pub(crate) struct Gdb<'a> {
6+
pub(crate) gdb: &'a Path,
7+
}
8+
9+
pub(crate) fn discover_gdb<'a>(builder: &'a Builder<'_>) -> Option<Gdb<'a>> {
10+
let gdb = builder.config.gdb.as_deref()?;
11+
12+
Some(Gdb { gdb })
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::path::PathBuf;
2+
3+
use crate::core::builder::Builder;
4+
use crate::utils::exec::command;
5+
6+
pub(crate) struct Lldb {
7+
pub(crate) lldb_version: String,
8+
pub(crate) lldb_python_dir: String,
9+
}
10+
11+
pub(crate) fn discover_lldb(builder: &Builder<'_>) -> Option<Lldb> {
12+
// FIXME(#148361): We probably should not be picking up whatever arbitrary
13+
// lldb happens to be in the user's path, and instead require some kind of
14+
// explicit opt-in or configuration.
15+
let lldb_exe = builder.config.lldb.clone().unwrap_or_else(|| PathBuf::from("lldb"));
16+
17+
let lldb_version = command(&lldb_exe)
18+
.allow_failure()
19+
.arg("--version")
20+
.run_capture(builder)
21+
.stdout_if_ok()
22+
.and_then(|v| if v.trim().is_empty() { None } else { Some(v) })?;
23+
24+
let lldb_python_dir = command(&lldb_exe)
25+
.allow_failure()
26+
.arg("-P")
27+
.run_capture_stdout(builder)
28+
.stdout_if_ok()
29+
.map(|p| p.lines().next().expect("lldb Python dir not found").to_string())?;
30+
31+
Some(Lldb { lldb_version, lldb_python_dir })
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! Code for discovering debuggers and debugger-related configuration, so that
2+
//! it can be passed to compiletest when running debuginfo tests.
3+
4+
pub(crate) use self::android::{Android, discover_android};
5+
pub(crate) use self::gdb::{Gdb, discover_gdb};
6+
pub(crate) use self::lldb::{Lldb, discover_lldb};
7+
8+
mod android;
9+
mod gdb;
10+
mod lldb;

src/bootstrap/src/core/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub(crate) mod build_steps;
22
pub(crate) mod builder;
33
pub(crate) mod config;
4+
pub(crate) mod debuggers;
45
pub(crate) mod download;
56
pub(crate) mod metadata;
67
pub(crate) mod sanity;

src/tools/compiletest/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ pub struct Config {
560560
///
561561
/// FIXME: take a look at this; this is piggy-backing off of gdb code paths but only for
562562
/// `arm-linux-androideabi` target.
563-
pub android_cross_path: Utf8PathBuf,
563+
pub android_cross_path: Option<Utf8PathBuf>,
564564

565565
/// Extra parameter to run adb on `arm-linux-androideabi`.
566566
///

src/tools/compiletest/src/debuggers.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,19 @@ fn find_cdb(target: &str) -> Option<Utf8PathBuf> {
103103
}
104104

105105
/// Returns Path to CDB
106-
pub(crate) fn analyze_cdb(
107-
cdb: Option<String>,
108-
target: &str,
109-
) -> (Option<Utf8PathBuf>, Option<[u16; 4]>) {
106+
pub(crate) fn discover_cdb(cdb: Option<String>, target: &str) -> Option<Utf8PathBuf> {
110107
let cdb = cdb.map(Utf8PathBuf::from).or_else(|| find_cdb(target));
108+
cdb
109+
}
111110

111+
pub(crate) fn query_cdb_version(cdb: &Utf8Path) -> Option<[u16; 4]> {
112112
let mut version = None;
113-
if let Some(cdb) = cdb.as_ref() {
114-
if let Ok(output) = Command::new(cdb).arg("/version").output() {
115-
if let Some(first_line) = String::from_utf8_lossy(&output.stdout).lines().next() {
116-
version = extract_cdb_version(&first_line);
117-
}
113+
if let Ok(output) = Command::new(cdb).arg("/version").output() {
114+
if let Some(first_line) = String::from_utf8_lossy(&output.stdout).lines().next() {
115+
version = extract_cdb_version(&first_line);
118116
}
119117
}
120-
121-
(cdb, version)
118+
version
122119
}
123120

124121
pub(crate) fn extract_cdb_version(full_version_line: &str) -> Option<[u16; 4]> {
@@ -132,20 +129,19 @@ pub(crate) fn extract_cdb_version(full_version_line: &str) -> Option<[u16; 4]> {
132129
Some([major, minor, patch, build])
133130
}
134131

135-
/// Returns (Path to GDB, GDB Version)
136-
pub(crate) fn analyze_gdb(
132+
pub(crate) fn discover_gdb(
137133
gdb: Option<String>,
138134
target: &str,
139-
android_cross_path: &Utf8Path,
140-
) -> (Option<String>, Option<u32>) {
135+
android_cross_path: Option<&Utf8Path>,
136+
) -> Option<String> {
141137
#[cfg(not(windows))]
142138
const GDB_FALLBACK: &str = "gdb";
143139
#[cfg(windows)]
144140
const GDB_FALLBACK: &str = "gdb.exe";
145141

146142
let fallback_gdb = || {
147143
if is_android_gdb_target(target) {
148-
let mut gdb_path = android_cross_path.to_string();
144+
let mut gdb_path = android_cross_path.unwrap().to_string();
149145
gdb_path.push_str("/bin/gdb");
150146
gdb_path
151147
} else {
@@ -159,6 +155,10 @@ pub(crate) fn analyze_gdb(
159155
Some(ref s) => s.to_owned(),
160156
};
161157

158+
Some(gdb)
159+
}
160+
161+
pub(crate) fn query_gdb_version(gdb: &str) -> Option<u32> {
162162
let mut version_line = None;
163163
if let Ok(output) = Command::new(&gdb).arg("--version").output() {
164164
if let Some(first_line) = String::from_utf8_lossy(&output.stdout).lines().next() {
@@ -168,10 +168,10 @@ pub(crate) fn analyze_gdb(
168168

169169
let version = match version_line {
170170
Some(line) => extract_gdb_version(&line),
171-
None => return (None, None),
171+
None => return None,
172172
};
173173

174-
(Some(gdb), version)
174+
version
175175
}
176176

177177
pub(crate) fn extract_gdb_version(full_version_line: &str) -> Option<u32> {

src/tools/compiletest/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,14 @@ fn parse_config(args: Vec<String>) -> Config {
256256
}
257257

258258
let target = opt_str2(matches.opt_str("target"));
259-
let android_cross_path = opt_path(matches, "android-cross-path");
259+
let android_cross_path = matches.opt_str("android-cross-path").map(Utf8PathBuf::from);
260260
// FIXME: `cdb_version` is *derived* from cdb, but it's *not* technically a config!
261-
let (cdb, cdb_version) = debuggers::analyze_cdb(matches.opt_str("cdb"), &target);
261+
let cdb = debuggers::discover_cdb(matches.opt_str("cdb"), &target);
262+
let cdb_version = cdb.as_deref().and_then(debuggers::query_cdb_version);
262263
// FIXME: `gdb_version` is *derived* from gdb, but it's *not* technically a config!
263-
let (gdb, gdb_version) =
264-
debuggers::analyze_gdb(matches.opt_str("gdb"), &target, &android_cross_path);
264+
let gdb =
265+
debuggers::discover_gdb(matches.opt_str("gdb"), &target, android_cross_path.as_deref());
266+
let gdb_version = gdb.as_deref().and_then(debuggers::query_gdb_version);
265267
// FIXME: `lldb_version` is *derived* from lldb, but it's *not* technically a config!
266268
let lldb_version =
267269
matches.opt_str("lldb-version").as_deref().and_then(debuggers::extract_lldb_version);

src/tools/compiletest/src/runtest/debuginfo.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ impl TestCx<'_> {
153153
// write debugger script
154154
let mut script_str = String::with_capacity(2048);
155155
script_str.push_str(&format!("set charset {}\n", Self::charset()));
156-
script_str.push_str(&format!("set sysroot {}\n", &self.config.android_cross_path));
156+
script_str.push_str(&format!(
157+
"set sysroot {}\n",
158+
self.config.android_cross_path.as_deref().unwrap()
159+
));
157160
script_str.push_str(&format!("file {}\n", exe_file));
158161
script_str.push_str("target remote :5039\n");
159162
script_str.push_str(&format!(

0 commit comments

Comments
 (0)