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
#![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];
mod pdb;
use crate::pdb::locate_pdb;
mod path_utils;
use crate::path_utils::path_from_bytes;
cfg_if::cfg_if! {
if #[cfg(target_os = "macos")] {
mod macos;
use crate::macos::locate_dsym_using_spotlight;
} else {
#[allow(clippy::unnecessary_wraps)]
fn locate_dsym_using_spotlight(_uuid: Uuid) -> Result<Option<PathBuf>, Error> {
Ok(None)
}
}
}
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; }
}
let deps_dir = target_channel_dir.join("deps");
let examples_dir = target_channel_dir.join("examples");
try_match_dsym_in_dir(&deps_dir, uuid).or_else(|| try_match_dsym_in_dir(&examples_dir, uuid))
}
fn try_match_dsym_in_dir(dir: &Path, uuid: Uuid) -> Option<PathBuf> {
for entry in fs::read_dir(dir).ok()? {
let item = entry.ok()?.path();
if item.extension() != Some(std::ffi::OsStr::new("dSYM")) {
continue;
}
if let Some(debug_file_name) = try_match_dsym(&item, 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<Option<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(pdbinfo) = object.pdb_info()? {
return locate_pdb(path.as_ref(), &pdbinfo);
}
if let Some(path) = object
.build_id()?
.and_then(locate_debug_build_id)
{
return Ok(Some(path));
}
if let Some((filename, crc)) = object.gnu_debuglink()? {
let filename = path_from_bytes(filename)?;
return locate_gnu_debuglink(path.as_ref(), filename, crc);
}
Ok(None)
}
pub fn locate_dsym<T>(path: T, uuid: Uuid) -> Result<Option<PathBuf>, Error>
where
T: AsRef<Path>,
{
if let Some(dsym_path) = locate_dsym_fastpath(path.as_ref(), uuid) {
return Ok(Some(dsym_path));
}
locate_dsym_using_spotlight(uuid)
}
pub fn locate_debug_build_id(id: &[u8]) -> Option<PathBuf> {
if id.len() < 2 {
return None;
}
let mut f = format!("/usr/lib/debug/.build-id/{:02x}/", id[0]);
for x in &id[1..] {
let _ = write!(&mut f, "{:02x}", x);
}
let _ = write!(&mut f, ".debug");
let f = PathBuf::from(f);
if f.exists() {
return Some(f);
}
None
}
pub fn locate_gnu_debuglink<T, U>(path: T, filename: U, _crc: u32) -> Result<Option<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(Some(f));
}
let f = parent.join(".debug").join(filename);
if f.exists() {
return Ok(Some(f));
}
let parent = parent.strip_prefix("/").unwrap();
let f = Path::new("/usr/lib/debug").join(parent).join(filename);
if f.exists() {
return Ok(Some(f));
}
Ok(None)
}