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 Some(Arc::new(Config { debugger: Some(Debugger::Lldb), ..config.clone() }))
55}
56
57pub(crate) fn is_android_gdb_target(target: &str) -> bool {
60 matches!(
61 &target[..],
62 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android"
63 )
64}
65
66fn is_pc_windows_msvc_target(target: &str) -> bool {
68 target.ends_with("-pc-windows-msvc")
69}
70
71fn find_cdb(target: &str) -> Option<Utf8PathBuf> {
73 if !(cfg!(windows) && is_pc_windows_msvc_target(target)) {
74 return None;
75 }
76
77 let pf86 = Utf8PathBuf::from_path_buf(
78 env::var_os("ProgramFiles(x86)").or_else(|| env::var_os("ProgramFiles"))?.into(),
79 )
80 .unwrap();
81 let cdb_arch = if cfg!(target_arch = "x86") {
82 "x86"
83 } else if cfg!(target_arch = "x86_64") {
84 "x64"
85 } else if cfg!(target_arch = "aarch64") {
86 "arm64"
87 } else if cfg!(target_arch = "arm") {
88 "arm"
89 } else {
90 return None; };
92
93 let mut path = pf86;
94 path.push(r"Windows Kits\10\Debuggers"); path.push(cdb_arch);
96 path.push(r"cdb.exe");
97
98 if !path.exists() {
99 return None;
100 }
101
102 Some(path)
103}
104
105pub(crate) fn analyze_cdb(
107 cdb: Option<String>,
108 target: &str,
109) -> (Option<Utf8PathBuf>, Option<[u16; 4]>) {
110 let cdb = cdb.map(Utf8PathBuf::from).or_else(|| find_cdb(target));
111
112 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 }
118 }
119 }
120
121 (cdb, version)
122}
123
124pub(crate) fn extract_cdb_version(full_version_line: &str) -> Option<[u16; 4]> {
125 let version = full_version_line.rsplit(' ').next()?;
127 let mut components = version.split('.');
128 let major: u16 = components.next().unwrap().parse().unwrap();
129 let minor: u16 = components.next().unwrap().parse().unwrap();
130 let patch: u16 = components.next().unwrap_or("0").parse().unwrap();
131 let build: u16 = components.next().unwrap_or("0").parse().unwrap();
132 Some([major, minor, patch, build])
133}
134
135pub(crate) fn analyze_gdb(
137 gdb: Option<String>,
138 target: &str,
139 android_cross_path: &Utf8Path,
140) -> (Option<String>, Option<u32>) {
141 #[cfg(not(windows))]
142 const GDB_FALLBACK: &str = "gdb";
143 #[cfg(windows)]
144 const GDB_FALLBACK: &str = "gdb.exe";
145
146 let fallback_gdb = || {
147 if is_android_gdb_target(target) {
148 let mut gdb_path = android_cross_path.to_string();
149 gdb_path.push_str("/bin/gdb");
150 gdb_path
151 } else {
152 GDB_FALLBACK.to_owned()
153 }
154 };
155
156 let gdb = match gdb {
157 None => fallback_gdb(),
158 Some(ref s) if s.is_empty() => fallback_gdb(), Some(ref s) => s.to_owned(),
160 };
161
162 let mut version_line = None;
163 if let Ok(output) = Command::new(&gdb).arg("--version").output() {
164 if let Some(first_line) = String::from_utf8_lossy(&output.stdout).lines().next() {
165 version_line = Some(first_line.to_string());
166 }
167 }
168
169 let version = match version_line {
170 Some(line) => extract_gdb_version(&line),
171 None => return (None, None),
172 };
173
174 (Some(gdb), version)
175}
176
177pub(crate) fn extract_gdb_version(full_version_line: &str) -> Option<u32> {
178 let full_version_line = full_version_line.trim();
179
180 let unbracketed_part = full_version_line.split('[').next().unwrap();
192 let mut splits = unbracketed_part.trim_end().rsplit(' ');
193 let version_string = splits.next().unwrap();
194
195 let mut splits = version_string.split('.');
196 let major = splits.next().unwrap();
197 let minor = splits.next().unwrap();
198 let patch = splits.next();
199
200 let major: u32 = major.parse().unwrap();
201 let (minor, patch): (u32, u32) = match minor.find(not_a_digit) {
202 None => {
203 let minor = minor.parse().unwrap();
204 let patch: u32 = match patch {
205 Some(patch) => match patch.find(not_a_digit) {
206 None => patch.parse().unwrap(),
207 Some(idx) if idx > 3 => 0,
208 Some(idx) => patch[..idx].parse().unwrap(),
209 },
210 None => 0,
211 };
212 (minor, patch)
213 }
214 Some(idx) => {
216 let minor = minor[..idx].parse().unwrap();
217 (minor, 0)
218 }
219 };
220
221 Some(((major * 1000) + minor) * 1000 + patch)
222}
223
224pub(crate) fn extract_lldb_version(full_version_line: &str) -> Option<u32> {
226 let full_version_line = full_version_line.trim();
245
246 if let Some(apple_ver) =
247 full_version_line.strip_prefix("LLDB-").or_else(|| full_version_line.strip_prefix("lldb-"))
248 {
249 if let Some(idx) = apple_ver.find(not_a_digit) {
250 let version: u32 = apple_ver[..idx].parse().unwrap();
251 return Some(version);
252 }
253 } else if let Some(lldb_ver) = full_version_line.strip_prefix("lldb version ") {
254 if let Some(idx) = lldb_ver.find(not_a_digit) {
255 let version: u32 = lldb_ver[..idx].parse().ok()?;
256 return Some(version * 100);
257 }
258 }
259 None
260}
261
262fn not_a_digit(c: char) -> bool {
263 !c.is_ascii_digit()
264}