1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#![warn(clippy::all)]
use anyhow::{anyhow, Error};
use object::Object;
use std::fmt::Write;
use std::fs;
use std::path::{Path, PathBuf};
pub type Uuid = [u8; 16];
cfg_if::cfg_if! {
if #[cfg(unix)] {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
fn path_from_bytes(bytes: &[u8]) -> Result<&OsStr, Error> {
Ok(OsStr::from_bytes(bytes))
}
} else {
use std::str;
fn path_from_bytes(bytes: &[u8]) -> Result<&str, str::Utf8Error> {
str::from_utf8(bytes)
}
}
}
cfg_if::cfg_if! {
if #[cfg(target_os = "macos")] {
mod macos;
use crate::macos::locate_dsym_using_spotlight;
} else {
fn locate_dsym_using_spotlight(_uuid: Uuid) -> Result<PathBuf, Error> {
Err(anyhow!("Could not locate dSYM"))
}
}
}
fn locate_dsym_fastpath(path: &Path, uuid: Uuid) -> Option<PathBuf> {
let path = path.canonicalize().ok()?;
let mut dsym = path.file_name()?.to_owned();
dsym.push(".dSYM");
let dsym_dir = path.with_file_name(&dsym);
if let Some(f) = try_match_dsym(&dsym_dir, uuid) {
return Some(f);
}
let mut target_channel_dir = &*path;
loop {
let parent = target_channel_dir.parent()?;
target_channel_dir = parent;
if target_channel_dir.parent().and_then(Path::file_name)
== Some(std::ffi::OsStr::new("target"))
{
break;
}
}
for dir in fs::read_dir(target_channel_dir.join("deps"))
.unwrap()
.chain(fs::read_dir(target_channel_dir.join("examples")).unwrap())
{
let dir = dir.unwrap().path();
if dir.extension() != Some(std::ffi::OsStr::new("dSYM")) {
continue;
}
if let Some(debug_file_name) = try_match_dsym(&dir, uuid) {
return Some(debug_file_name);
}
}
None
}
fn try_match_dsym(dsym_dir: &Path, uuid: Uuid) -> Option<PathBuf> {
let mut dir_iter = fs::read_dir(dsym_dir.join("Contents/Resources/DWARF")).ok()?;
let debug_file_name = dir_iter.next()?.ok()?.path();
if dir_iter.next().is_some() {
return None;
}
let file = fs::read(&debug_file_name).ok()?;
let dsym = object::File::parse(&file).ok()?;
if dsym.mach_uuid() == Ok(Some(uuid)) {
Some(debug_file_name)
} else {
None
}
}
pub fn locate_debug_symbols<'a, O, T>(object: &'a O, path: T) -> Result<PathBuf, Error>
where
O: Object<'a, 'a>,
T: AsRef<Path>,
{
if let Some(uuid) = object.mach_uuid()? {
return locate_dsym(path.as_ref(), uuid);
}
if let Some(build_id) = object.build_id()? {
let path = locate_debug_build_id(build_id);
if path.is_ok() {
return path;
}
}
if let Some((filename, crc)) = object.gnu_debuglink()? {
let filename = path_from_bytes(filename)?;
return locate_gnu_debuglink(path.as_ref(), filename, crc);
}
Err(anyhow!("Object does not have debug info pointer"))
}
pub fn locate_dsym<T>(path: T, uuid: Uuid) -> Result<PathBuf, Error>
where
T: AsRef<Path>,
{
if let Some(dsym_path) = locate_dsym_fastpath(path.as_ref(), uuid) {
return Ok(dsym_path);
}
locate_dsym_using_spotlight(uuid)
}
pub fn locate_debug_build_id(id: &[u8]) -> Result<PathBuf, Error> {
if id.len() < 2 {
return Err(anyhow!("Build ID is too short"));
}
let mut f = format!("/usr/lib/debug/.build-id/{:02x}/", id[0]);
for x in &id[1..] {
write!(&mut f, "{:02x}", x).ok();
}
write!(&mut f, ".debug").ok();
let f = PathBuf::from(f);
if f.exists() {
return Ok(f);
}
Err(anyhow!("Could not locate file with build ID"))
}
pub fn locate_gnu_debuglink<T, U>(path: T, filename: U, _crc: u32) -> Result<PathBuf, Error>
where
T: AsRef<Path>,
U: AsRef<Path>,
{
let path = fs::canonicalize(path)?;
let parent = path.parent().ok_or_else(|| anyhow!("Bad path"))?;
let filename = filename.as_ref();
let f = parent.join(filename);
if f != path && f.exists() {
return Ok(f);
}
let f = parent.join(".debug").join(filename);
if f.exists() {
return Ok(f);
}
let parent = parent.strip_prefix("/").unwrap();
let f = Path::new("/usr/lib/debug").join(parent).join(filename);
if f.exists() {
return Ok(f);
}
Err(anyhow!("Could not locate GNU debug link file"))
}