1use std::env;
2use std::process::Command;
3use std::sync::Arc;
4
5use camino::{Utf8Path, Utf8PathBuf};
6
7use crate::common::{Config, Debugger};
8
9pub(crate) fn configure_cdb(config: &Config) -> Option<Arc<Config>> {
10 config.cdb.as_ref()?;
11
12 Some(Arc::new(Config { debugger: Some(Debugger::Cdb), ..config.clone() }))
13}
14
15pub(crate) fn configure_gdb(config: &Config) -> Option<Arc<Config>> {
16 config.gdb_version?;
17
18 if config.matches_env("msvc") {
19 return None;
20 }
21
22 if config.remote_test_client.is_some() && !config.target.contains("android") {
23 println!(
24 "WARNING: debuginfo tests are not available when \
25 testing with remote"
26 );
27 return None;
28 }
29
30 if config.target.contains("android") {
31 println!(
32 "{} debug-info test uses tcp 5039 port.\
33 please reserve it",
34 config.target
35 );
36
37 unsafe { env::set_var("RUST_TEST_THREADS", "1") };
46 }
47
48 Some(Arc::new(Config { debugger: Some(Debugger::Gdb), ..config.clone() }))
49}
50
51pub(crate) fn configure_lldb(config: &Config) -> Option<Arc<Config>> {
52 config.lldb_python_dir.as_ref()?;
53
54 if let Some(350) = config.lldb_version {
55 println!(
56 "WARNING: The used version of LLDB (350) has a \
57 known issue that breaks debuginfo tests. See \
58 issue #32520 for more information. Skipping all \
59 LLDB-based tests!",
60 );
61 return None;
62 }
63
64 Some(Arc::new(Config { debugger: Some(Debugger::Lldb), ..config.clone() }))
65}
66
67pub(crate) fn is_android_gdb_target(target: &str) -> bool {
70 matches!(
71 &target[..],
72 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android"
73 )
74}
75
76fn is_pc_windows_msvc_target(target: &str) -> bool {
78 target.ends_with("-pc-windows-msvc")
79}
80
81fn find_cdb(target: &str) -> Option<Utf8PathBuf> {
82 if !(cfg!(windows) && is_pc_windows_msvc_target(target)) {
83 return None;
84 }
85
86 let pf86 = Utf8PathBuf::from_path_buf(
87 env::var_os("ProgramFiles(x86)").or_else(|| env::var_os("ProgramFiles"))?.into(),
88 )
89 .unwrap();
90 let cdb_arch = if cfg!(target_arch = "x86") {
91 "x86"
92 } else if cfg!(target_arch = "x86_64") {
93 "x64"
94 } else if cfg!(target_arch = "aarch64") {
95 "arm64"
96 } else if cfg!(target_arch = "arm") {
97 "arm"
98 } else {
99 return None; };
101
102 let mut path = pf86;
103 path.push(r"Windows Kits\10\Debuggers"); path.push(cdb_arch);
105 path.push(r"cdb.exe");
106
107 if !path.exists() {
108 return None;
109 }
110
111 Some(path)
112}
113
114pub(crate) fn analyze_cdb(
116 cdb: Option<String>,
117 target: &str,
118) -> (Option<Utf8PathBuf>, Option<[u16; 4]>) {
119 let cdb = cdb.map(Utf8PathBuf::from).or_else(|| find_cdb(target));
120
121 let mut version = None;
122 if let Some(cdb) = cdb.as_ref() {
123 if let Ok(output) = Command::new(cdb).arg("/version").output() {
124 if let Some(first_line) = String::from_utf8_lossy(&output.stdout).lines().next() {
125 version = extract_cdb_version(&first_line);
126 }
127 }
128 }
129
130 (cdb, version)
131}
132
133pub(crate) fn extract_cdb_version(full_version_line: &str) -> Option<[u16; 4]> {
134 let version = full_version_line.rsplit(' ').next()?;
136 let mut components = version.split('.');
137 let major: u16 = components.next().unwrap().parse().unwrap();
138 let minor: u16 = components.next().unwrap().parse().unwrap();
139 let patch: u16 = components.next().unwrap_or("0").parse().unwrap();
140 let build: u16 = components.next().unwrap_or("0").parse().unwrap();
141 Some([major, minor, patch, build])
142}
143
144pub(crate) fn analyze_gdb(
146 gdb: Option<String>,
147 target: &str,
148 android_cross_path: &Utf8Path,
149) -> (Option<String>, Option<u32>) {
150 #[cfg(not(windows))]
151 const GDB_FALLBACK: &str = "gdb";
152 #[cfg(windows)]
153 const GDB_FALLBACK: &str = "gdb.exe";
154
155 let fallback_gdb = || {
156 if is_android_gdb_target(target) {
157 let mut gdb_path = android_cross_path.to_string();
158 gdb_path.push_str("/bin/gdb");
159 gdb_path
160 } else {
161 GDB_FALLBACK.to_owned()
162 }
163 };
164
165 let gdb = match gdb {
166 None => fallback_gdb(),
167 Some(ref s) if s.is_empty() => fallback_gdb(), Some(ref s) => s.to_owned(),
169 };
170
171 let mut version_line = None;
172 if let Ok(output) = Command::new(&gdb).arg("--version").output() {
173 if let Some(first_line) = String::from_utf8_lossy(&output.stdout).lines().next() {
174 version_line = Some(first_line.to_string());
175 }
176 }
177
178 let version = match version_line {
179 Some(line) => extract_gdb_version(&line),
180 None => return (None, None),
181 };
182
183 (Some(gdb), version)
184}
185
186pub(crate) fn extract_gdb_version(full_version_line: &str) -> Option<u32> {
187 let full_version_line = full_version_line.trim();
188
189 let unbracketed_part = full_version_line.split('[').next().unwrap();
201 let mut splits = unbracketed_part.trim_end().rsplit(' ');
202 let version_string = splits.next().unwrap();
203
204 let mut splits = version_string.split('.');
205 let major = splits.next().unwrap();
206 let minor = splits.next().unwrap();
207 let patch = splits.next();
208
209 let major: u32 = major.parse().unwrap();
210 let (minor, patch): (u32, u32) = match minor.find(not_a_digit) {
211 None => {
212 let minor = minor.parse().unwrap();
213 let patch: u32 = match patch {
214 Some(patch) => match patch.find(not_a_digit) {
215 None => patch.parse().unwrap(),
216 Some(idx) if idx > 3 => 0,
217 Some(idx) => patch[..idx].parse().unwrap(),
218 },
219 None => 0,
220 };
221 (minor, patch)
222 }
223 Some(idx) => {
225 let minor = minor[..idx].parse().unwrap();
226 (minor, 0)
227 }
228 };
229
230 Some(((major * 1000) + minor) * 1000 + patch)
231}
232
233pub(crate) fn extract_lldb_version(full_version_line: &str) -> Option<u32> {
235 let full_version_line = full_version_line.trim();
254
255 if let Some(apple_ver) =
256 full_version_line.strip_prefix("LLDB-").or_else(|| full_version_line.strip_prefix("lldb-"))
257 {
258 if let Some(idx) = apple_ver.find(not_a_digit) {
259 let version: u32 = apple_ver[..idx].parse().unwrap();
260 return Some(version);
261 }
262 } else if let Some(lldb_ver) = full_version_line.strip_prefix("lldb version ") {
263 if let Some(idx) = lldb_ver.find(not_a_digit) {
264 let version: u32 = lldb_ver[..idx].parse().ok()?;
265 return Some(version * 100);
266 }
267 }
268 None
269}
270
271fn not_a_digit(c: char) -> bool {
272 !c.is_ascii_digit()
273}