-
-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathfind.rs
More file actions
391 lines (356 loc) · 11.4 KB
/
find.rs
File metadata and controls
391 lines (356 loc) · 11.4 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
use std::collections::HashSet;
use std::env;
use std::ffi::OsStr;
use std::fmt::Debug;
use std::io;
use std::path::PathBuf;
use std::str::FromStr as _;
use anyhow::Result;
use clap::{builder::PossibleValuesParser, Arg, ArgAction, ArgMatches, Command};
use console::style;
use if_chain::if_chain;
use proguard::ProguardMapping;
use serde::Serialize;
use symbolic::common::{ByteView, DebugId};
use uuid::{Uuid, Version as UuidVersion};
use walkdir::{DirEntry, WalkDir};
use crate::utils::dif::{DifFile, DifType};
use crate::utils::progress::{ProgressBar, ProgressStyle};
use crate::utils::system::QuietExit;
// text files larger than 32 megabytes are not considered to be
// valid mapping files when scanning
const MAX_MAPPING_FILE: u64 = 32 * 1024 * 1024;
#[derive(Serialize, Debug)]
struct DifMatch {
#[serde(rename = "type")]
pub ty: DifType,
pub id: DebugId,
pub path: PathBuf,
}
pub fn make_command(command: Command) -> Command {
command
.about("Locate debug information files for given debug identifiers.")
.arg(
Arg::new("ids")
.value_name("ID")
.help("The debug identifiers of the files to search for.")
.value_parser(DebugId::from_str)
.num_args(1..)
.action(ArgAction::Append),
)
.arg(
Arg::new("types")
.long("type")
.short('t')
.value_name("TYPE")
.action(ArgAction::Append)
.value_parser(PossibleValuesParser::new(DifType::all_names()))
.help(
"Only consider debug information files of the given \
type. By default all types are considered.",
),
)
.arg(
Arg::new("no_well_known")
.long("no-well-known")
.action(ArgAction::SetTrue)
.help("Do not look for debug symbols in well known locations."),
)
.arg(
Arg::new("no_cwd")
.long("no-cwd")
.action(ArgAction::SetTrue)
.help("Do not look for debug symbols in the current working directory."),
)
.arg(
Arg::new("paths")
.long("path")
.short('p')
.value_name("PATH")
.action(ArgAction::Append)
.help("Add a path to search recursively for debug info files."),
)
.arg(
Arg::new("json")
.long("json")
.action(ArgAction::SetTrue)
.help("Format outputs as JSON."),
)
}
fn id_hint(id: &DebugId) -> &'static str {
if id.appendix() > 0 {
return "likely PDB";
}
match id.uuid().get_version() {
Some(UuidVersion::Sha1) => "likely Proguard",
Some(UuidVersion::Md5) => "likely dSYM",
None => "likely ELF Debug",
_ => "unknown",
}
}
fn find_ids(
paths: &HashSet<PathBuf>,
types: &HashSet<DifType>,
ids: &HashSet<DebugId>,
as_json: bool,
) -> Result<bool> {
let mut remaining = ids.clone();
let mut breakpad_found = HashSet::new();
let mut proguard_uuids: HashSet<_> = ids
.iter()
.map(DebugId::uuid)
.filter(|&x| x.get_version() == Some(UuidVersion::Sha1))
.collect();
let iter = paths
.iter()
.flat_map(WalkDir::new)
.filter_map(Result::ok)
.filter(|e| e.file_type().is_file());
let mut found_files = vec![];
let pb = ProgressBar::new_spinner();
pb.set_style(
ProgressStyle::default_spinner()
.tick_chars("/|\\- ")
.template(
"{spinner} Looking for debug info files... {msg:.dim}\
\n debug info files found: {prefix:.yellow}",
),
);
for dirent in iter {
if remaining.is_empty() {
break;
}
if let Some(p) = dirent.file_name().to_str() {
pb.set_message(p);
}
pb.tick();
pb.set_prefix(&format!("{}", found_files.len()));
let found: Vec<_> = types
.iter()
.filter_map(|t| match t {
DifType::Dsym => find_ids_for_dsym(&dirent, &remaining),
DifType::Elf => find_ids_for_elf(&dirent, &remaining),
DifType::Pe => find_ids_for_pe(&dirent, &remaining),
DifType::Pdb => find_ids_for_pdb(&dirent, &remaining),
DifType::PortablePdb => find_ids_for_portablepdb(&dirent, &remaining),
DifType::SourceBundle => find_ids_for_sourcebundle(&dirent, &remaining),
DifType::Breakpad => find_ids_for_breakpad(&dirent, &remaining),
DifType::Proguard => find_ids_for_proguard(&dirent, &proguard_uuids),
DifType::Jvm => find_ids_for_sourcebundle(&dirent, &remaining),
DifType::Wasm => None,
})
.flatten()
.collect();
for (id, ty) in found {
let path = dirent.path().to_path_buf();
found_files.push(DifMatch { ty, id, path });
if ty == DifType::Breakpad {
breakpad_found.insert(id);
} else {
remaining.remove(&id);
}
proguard_uuids.remove(&id.uuid());
}
}
pb.finish_and_clear();
if as_json {
serde_json::to_writer_pretty(&mut io::stdout(), &found_files)?;
println!();
} else {
for m in found_files {
println!(
"{} {} [{}]",
style(m.id).dim(),
m.path.display(),
style(m.ty).yellow()
);
}
remaining.extend(breakpad_found);
if !remaining.is_empty() {
eprintln!();
eprintln!("missing debug information files:");
for id in &remaining {
eprintln!(" {id} ({})", id_hint(id),);
}
}
}
Ok(remaining.is_empty())
}
fn find_ids_for_proguard(
dirent: &DirEntry,
proguard_uuids: &HashSet<Uuid>,
) -> Option<Vec<(DebugId, DifType)>> {
if_chain! {
if !proguard_uuids.is_empty();
if dirent.path().extension() == Some(OsStr::new("txt"));
if let Ok(md) = dirent.metadata();
if md.len() < MAX_MAPPING_FILE;
if let Ok(byteview) = ByteView::open(dirent.path());
let mapping = ProguardMapping::new(&byteview);
if mapping.is_valid();
if proguard_uuids.contains(&mapping.uuid());
then {
return Some(vec![(mapping.uuid().into(), DifType::Proguard)]);
}
}
None
}
fn find_ids_for_dsym(
dirent: &DirEntry,
remaining: &HashSet<DebugId>,
) -> Option<Vec<(DebugId, DifType)>> {
if_chain! {
// we regularly match on .class files but the will never be
// dsyms, so we can quickly skip them here
if dirent.path().extension() != Some(OsStr::new("class"));
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::Dsym));
then {
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::Dsym))
}
}
None
}
fn find_ids_for_elf(
dirent: &DirEntry,
remaining: &HashSet<DebugId>,
) -> Option<Vec<(DebugId, DifType)>> {
if_chain! {
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::Elf));
then {
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::Elf))
}
}
None
}
fn find_ids_for_pe(
dirent: &DirEntry,
remaining: &HashSet<DebugId>,
) -> Option<Vec<(DebugId, DifType)>> {
if_chain! {
if dirent.path().extension() == Some(OsStr::new("exe")) ||
dirent.path().extension() == Some(OsStr::new("dll"));
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::Pe));
then {
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::Pe))
}
}
None
}
fn find_ids_for_pdb(
dirent: &DirEntry,
remaining: &HashSet<DebugId>,
) -> Option<Vec<(DebugId, DifType)>> {
if_chain! {
if dirent.path().extension() == Some(OsStr::new("pdb"));
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::Pdb));
then {
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::Pdb))
}
}
None
}
fn find_ids_for_portablepdb(
dirent: &DirEntry,
remaining: &HashSet<DebugId>,
) -> Option<Vec<(DebugId, DifType)>> {
if_chain! {
if dirent.path().extension() == Some(OsStr::new("pdb"));
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::PortablePdb));
then {
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::PortablePdb))
}
}
None
}
fn find_ids_for_sourcebundle(
dirent: &DirEntry,
remaining: &HashSet<DebugId>,
) -> Option<Vec<(DebugId, DifType)>> {
if_chain! {
if dirent.path().extension() == Some(OsStr::new("zip"));
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::SourceBundle));
then {
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::SourceBundle))
}
}
None
}
fn find_ids_for_breakpad(
dirent: &DirEntry,
remaining: &HashSet<DebugId>,
) -> Option<Vec<(DebugId, DifType)>> {
if_chain! {
if dirent.path().extension() == Some(OsStr::new("sym"));
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::Breakpad));
then {
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::Breakpad))
}
}
None
}
fn extract_remaining_ids(
ids: &[DebugId],
remaining: &HashSet<DebugId>,
t: DifType,
) -> Vec<(DebugId, DifType)> {
ids.iter()
.filter_map(|id| {
if remaining.contains(id) {
return Some((id.to_owned(), t));
}
None
})
.collect()
}
pub fn execute(matches: &ArgMatches) -> Result<()> {
let mut paths = HashSet::new();
let mut types = HashSet::new();
let mut ids = HashSet::new();
// which types should we consider?
if let Some(t) = matches.get_many::<String>("types") {
for ty in t {
#[expect(clippy::unwrap_used, reason = "legacy code")]
types.insert(ty.parse().unwrap());
}
} else {
types.extend(DifType::all());
}
let with_well_known = !matches.get_flag("no_well_known");
let with_cwd = !matches.get_flag("no_cwd");
// start adding well known locations
if_chain! {
if with_well_known;
if types.contains(&DifType::Dsym);
if let Some(path) = dirs::home_dir().map(|x| x.join("Library/Developer/Xcode/DerivedData"));
if path.is_dir();
then {
paths.insert(path);
}
}
// current folder if wanted
if_chain! {
if with_cwd;
if let Ok(path) = env::current_dir();
then {
paths.insert(path);
}
}
// extra paths
if let Some(p) = matches.get_many::<String>("paths") {
for path in p {
paths.insert(PathBuf::from(path));
}
}
// which ids are we looking for?
if let Some(i) = matches.get_many::<DebugId>("ids") {
for id in i {
ids.insert(*id);
}
} else {
return Ok(());
}
if !find_ids(&paths, &types, &ids, matches.get_flag("json"))? {
return Err(QuietExit(1).into());
}
Ok(())
}