dump_syms 2.3.7

Dump debug symbols into Breakpad ones
Documentation
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use crossbeam::channel::{bounded, Receiver, Sender};
use hashbrown::HashMap;
use log::{error, info};
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use symbolic::common::Arch;
use symbolic::debuginfo::pdb::PdbObject;
use symbolic::debuginfo::pe::PeObject;
use symbolic::debuginfo::{peek, FileFormat};

use crate::common;
use crate::mapping::PathMappings;
use crate::object_info::ObjectInfo;
use crate::platform::Platform;
use crate::utils;
use crate::windows;

/// Different locations for file output
#[derive(Clone)]
pub enum FileOutput {
    Path(PathBuf),
    Stdout,
    Stderr,
}

impl From<&str> for FileOutput {
    fn from(s: &str) -> Self {
        if s == "-" {
            Self::Stdout
        } else {
            Self::Path(s.into())
        }
    }
}

impl fmt::Display for FileOutput {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Path(p) => write!(f, "{}", p.display()),
            Self::Stdout => f.write_str("stdout"),
            Self::Stderr => f.write_str("stderr"),
        }
    }
}

/// Defines how the final symbols are outputted
#[derive(Clone)]
pub enum Output {
    File(FileOutput),
    /// Store output symbols as FILENAME.\<ext\>/DEBUG_ID/FILENAME.sym in the
    /// specified directory, ie the symbol store format
    Store(PathBuf),
    /// Writes symbols to a file as well as storing them in the symbol store
    /// format to the specified directory
    FileAndStore {
        file: FileOutput,
        store_directory: PathBuf,
    },
}

impl From<PathBuf> for Output {
    fn from(path: PathBuf) -> Self {
        Self::File(FileOutput::Path(path))
    }
}

pub struct Config<'a> {
    pub output: Output,
    pub symbol_server: Option<&'a str>,
    pub debug_id: Option<&'a str>,
    pub code_id: Option<&'a str>,
    pub arch: &'a str,
    pub num_jobs: usize,
    pub check_cfi: bool,
    pub emit_inlines: bool,
    pub mapping_var: Option<Vec<&'a str>>,
    pub mapping_src: Option<Vec<&'a str>>,
    pub mapping_dest: Option<Vec<&'a str>>,
    pub mapping_file: Option<&'a str>,
}

impl Config<'_> {
    /// Create a [`Config`] using the given [`Output`], with all other
    /// fields set to reasonable defaults.
    ///
    /// The architecture will be set to the architecture for which
    /// compilation is happening, the number of jobs will be set to one.
    pub fn with_output(output: Output) -> Self {
        Self {
            output,
            symbol_server: None,
            debug_id: None,
            code_id: None,
            arch: common::get_compile_time_arch(),
            num_jobs: 1,
            check_cfi: true,
            emit_inlines: true,
            mapping_var: None,
            mapping_src: None,
            mapping_dest: None,
            mapping_file: None,
        }
    }
}

fn get_pdb_object_info(
    buf: &[u8],
    path: &Path,
    filename: &str,
    mapping: Option<Arc<PathMappings>>,
    collect_inlines: bool,
) -> common::Result<ObjectInfo> {
    let pdb = PdbObject::parse(buf)?;

    let (pe_name, pe_buf) = match windows::utils::find_pe_for_pdb(path, &pdb.debug_id()) {
        Some((pe_name, pe_buf)) => (Some(pe_name), Some(pe_buf)),
        None => (None, None),
    };
    let pe = pe_buf.as_deref().map(|buf| PeObject::parse(buf).unwrap());

    ObjectInfo::from_pdb(
        pdb,
        filename,
        pe_name.as_deref(),
        pe,
        mapping,
        collect_inlines,
    )
}

#[cfg(feature = "http")]
fn get_pe_pdb_object_info(
    buf: &[u8],
    path: &Path,
    filename: &str,
    mapping: Option<Arc<PathMappings>>,
    symbol_server: Option<&str>,
    emit_inlines: bool,
) -> common::Result<ObjectInfo> {
    let symbol_server = crate::cache::get_sym_servers(symbol_server);
    let res = windows::utils::get_pe_pdb_buf(path, buf, symbol_server.as_ref());

    if let Some((pe, pdb_buf, pdb_name)) = res {
        let pdb = PdbObject::parse(&pdb_buf)?;
        let pdb = ObjectInfo::from_pdb(
            pdb,
            &pdb_name,
            Some(filename),
            Some(pe),
            mapping,
            emit_inlines,
        )?;
        Ok(pdb)
    } else {
        anyhow::bail!("No pdb file found")
    }
}

#[cfg(not(feature = "http"))]
fn get_pe_pdb_object_info<'a>(
    buf: &[u8],
    path: &Path,
    filename: &str,
    mapping: Option<Arc<PathMappings>>,
    symbol_server: Option<&str>,
    emit_inlines: bool,
) -> common::Result<ObjectInfo> {
    anyhow::bail!("HTTP symbol retrieval not enabled")
}

fn get_pe_object_info(buf: &[u8], path: &Path, filename: &str) -> common::Result<ObjectInfo> {
    let pe = PeObject::parse(buf)
        .unwrap_or_else(|_| panic!("Unable to parse the PE file {}", path.to_str().unwrap()));
    let pe = ObjectInfo::from_pe(filename, pe)?;
    Ok(pe)
}

#[inline]
pub fn get_writer_for_sym(fo: &FileOutput) -> std::io::BufWriter<Box<dyn std::io::Write>> {
    let output: Box<dyn std::io::Write> = match fo {
        FileOutput::Stdout => Box::new(std::io::stdout()),
        FileOutput::Stderr => Box::new(std::io::stderr()),
        FileOutput::Path(path) => {
            let output = std::fs::File::create(path)
                .unwrap_or_else(|_| panic!("Cannot open file {} for writing", path.display()));
            Box::new(output)
        }
    };

    std::io::BufWriter::new(output)
}

fn store(output: &Output, check_cfi: bool, object_info: ObjectInfo) -> common::Result<()> {
    anyhow::ensure!(!check_cfi || object_info.has_stack(), "No CFI data");

    let sym_store_path = |dir: &Path| -> Option<PathBuf> {
        if dir.to_str()?.is_empty() {
            return None;
        }

        let mut pb = PathBuf::new();
        pb.push(dir);
        pb.push(utils::get_path_for_sym(
            object_info.get_name(),
            object_info.get_debug_id(),
        ));
        Some(pb)
    };

    let (foutput, store) = match output {
        Output::File(fo) => (Some(fo), None),
        Output::Store(store) => (None, sym_store_path(store)),
        Output::FileAndStore {
            file,
            store_directory,
        } => (Some(file), sym_store_path(store_directory)),
    };

    if let Some(store) = store {
        fs::create_dir_all(store.parent().unwrap())?;

        let fo = FileOutput::Path(store);
        let output = get_writer_for_sym(&fo);
        object_info.dump(output)?;

        info!("Store symbols at {fo}");
    }

    if let Some(file) = foutput {
        let writer = get_writer_for_sym(file);
        object_info.dump(writer)?;

        info!("Write symbols at {file}");
    }
    Ok(())
}

#[cfg(feature = "http")]
fn get_from_id(
    config: &Config,
    path: &Path,
    filename: String,
) -> common::Result<(Vec<u8>, String)> {
    if let Some(id) = config.debug_id.or(config.code_id) {
        let symbol_server = crate::cache::get_sym_servers(config.symbol_server);
        let (buf, filename) = crate::cache::search_file(filename, id, symbol_server.as_ref());
        return if let Some(buf) = buf {
            Ok((buf, filename))
        } else {
            anyhow::bail!("Impossible to get file {} with id {}", filename, id)
        };
    }

    Ok((utils::read_file(path), filename))
}

#[cfg(not(feature = "http"))]
fn get_from_id(
    _config: &Config,
    path: &Path,
    filename: String,
) -> common::Result<(Vec<u8>, String)> {
    Ok((utils::read_file(path), filename))
}

pub fn single_file(config: &Config, filename: &str) -> common::Result<()> {
    let path = Path::new(filename);
    let filename = utils::get_filename(path);

    let (buf, filename) = get_from_id(config, path, filename)?;

    let path_mappings = PathMappings::new(
        &config.mapping_var,
        &config.mapping_src,
        &config.mapping_dest,
        &config.mapping_file,
    )?
    .map(Arc::new);
    let arch = Arch::from_str(config.arch)?;
    let object_info = get_object_info(
        buf,
        path,
        &filename,
        path_mappings,
        arch,
        config.symbol_server,
        config.emit_inlines,
    )?;
    store(&config.output, config.check_cfi, object_info)
}

/// Detects the object format based on the bytes in the file.
fn get_object_info(
    buf: Vec<u8>,
    path: &Path,
    filename: &str,
    file_mapping: Option<Arc<PathMappings>>,
    arch: Arch,
    symbol_server: Option<&str>,
    emit_inlines: bool,
) -> common::Result<ObjectInfo> {
    let object_info = match peek(&buf, true /* check for fat binary */) {
        FileFormat::Elf => {
            ObjectInfo::from_elf(&buf, filename, Platform::Linux, file_mapping, emit_inlines)?
        }
        FileFormat::Pdb => get_pdb_object_info(&buf, path, filename, file_mapping, emit_inlines)?,
        FileFormat::Pe => {
            if let Ok(pdb_info) = get_pe_pdb_object_info(
                &buf,
                path,
                filename,
                file_mapping,
                symbol_server,
                emit_inlines,
            ) {
                pdb_info
            } else {
                get_pe_object_info(&buf, path, filename)?
            }
        }
        FileFormat::MachO => {
            ObjectInfo::from_macho(&buf, filename, arch, file_mapping, emit_inlines)?
        }
        _ => anyhow::bail!("Unknown file format"),
    };
    Ok(object_info)
}

#[allow(clippy::large_enum_variant)]
enum JobType {
    Get,
    Dump(ObjectInfo),
}

struct JobItem {
    file: String,
    typ: JobType,
    mapping: Option<Arc<PathMappings>>,
    collect_inlines: bool,
}

fn send_store_jobs(
    sender: &Sender<Option<JobItem>>,
    results: &mut HashMap<String, ObjectInfo>,
    num_threads: usize,
    output: Output,
    check_cfi: bool,
    collect_inlines: bool,
) -> common::Result<()> {
    if results.len() == 1 {
        let (_, d) = results.drain().take(1).next().unwrap();
        self::store(&output, check_cfi, d)?;
    } else {
        for (_, d) in results.drain() {
            sender
                .send(Some(JobItem {
                    file: "".to_string(),
                    typ: JobType::Dump(d),
                    mapping: None,
                    collect_inlines,
                }))
                .unwrap();
        }
    }

    poison_queue(sender, num_threads);
    Ok(())
}

fn poison_queue(sender: &Sender<Option<JobItem>>, num_threads: usize) {
    // Poison the receiver.
    for _ in 0..num_threads {
        sender.send(None).unwrap();
    }
}

#[allow(clippy::too_many_arguments)]
fn consumer(
    arch: Arch,
    sender: Sender<Option<JobItem>>,
    receiver: Receiver<Option<JobItem>>,
    results: Arc<Mutex<HashMap<String, ObjectInfo>>>,
    counter: Arc<AtomicUsize>,
    num_threads: usize,
    output: Output,
    check_cfi: bool,
) -> common::Result<()> {
    while let Ok(job) = receiver.recv() {
        if job.is_none() {
            return Ok(());
        }

        let JobItem {
            file,
            typ,
            mapping,
            collect_inlines,
        } = job.unwrap();

        match typ {
            JobType::Get => {
                let path = PathBuf::from(file);
                let filename = utils::get_filename(&path);
                let buf = utils::read_file(&path);

                let info =
                    get_object_info(buf, &path, &filename, mapping, arch, None, collect_inlines)?;

                let mut results = results.lock().unwrap();
                let info = if let Some(prev) = results.remove(info.get_debug_id()) {
                    ObjectInfo::merge(info, prev).inspect_err(|_e| {
                        poison_queue(&sender, num_threads);
                    })?
                } else {
                    info
                };
                results.insert(info.get_debug_id().to_string(), info);
            }
            JobType::Dump(d) => {
                self::store(&output, check_cfi, d)?;
                continue;
            }
        }

        if counter.load(Ordering::SeqCst) == 1 {
            // it was the last file: so we just have to add jobs to dump & store
            // and then poison the queue
            let mut results = results.lock().unwrap();
            send_store_jobs(
                &sender,
                &mut results,
                num_threads,
                output.clone(),
                check_cfi,
                collect_inlines,
            )?;
        } else {
            counter.fetch_sub(1, Ordering::SeqCst);
        }
    }

    Ok(())
}

pub fn several_files(config: &Config, filenames: &[&str]) -> common::Result<()> {
    let file_mapping = PathMappings::new(
        &config.mapping_var,
        &config.mapping_src,
        &config.mapping_dest,
        &config.mapping_file,
    )?
    .map(Arc::new);
    let arch = Arch::from_str(config.arch)?;
    let results = Arc::new(Mutex::new(HashMap::default()));
    let num_jobs = config.num_jobs.min(filenames.len());
    let counter = Arc::new(AtomicUsize::new(filenames.len()));

    let (sender, receiver) = bounded(num_jobs + 1);

    let mut receivers = Vec::with_capacity(num_jobs);
    for i in 0..num_jobs {
        let sender = sender.clone();
        let receiver = receiver.clone();
        let results = Arc::clone(&results);
        let counter = Arc::clone(&counter);
        let output = config.output.clone();

        let check_cfi = config.check_cfi;

        let t = thread::Builder::new()
            .name(format!("dump-syms {i}"))
            .spawn(move || {
                consumer(
                    arch, sender, receiver, results, counter, num_jobs, output, check_cfi,
                )
            })
            .unwrap();

        receivers.push(t);
    }

    for f in filenames {
        sender
            .send(Some(JobItem {
                file: f.to_string(),
                typ: JobType::Get,
                mapping: file_mapping.clone(),
                collect_inlines: config.emit_inlines,
            }))
            .unwrap();
    }

    for receiver in receivers {
        if let Err(e) = receiver.join().unwrap() {
            error!("{e}");
        }
    }

    Ok(())
}