ghostscope-dwarf 0.1.5

DWARF parser and symbolizer used by GhostScope to resolve variables and types at runtime.
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
use super::LoadedObjfile;
use crate::{
    binary::{
        dwarf_endian_from_object, dwarf_reader_from_arc_with_endian,
        empty_dwarf_reader_with_endian, load_explicit_debug_file, try_load_debug_file, DwarfData,
        MappedFile,
    },
    core::{mapping::ModuleMapping, DebugInfoSource, Result},
    index::{BlockIndex, TypeNameIndex},
    objfile::ModuleUnwindInfo,
    parser::DetailedParser,
};
use ghostscope_debuginfod::{build_id_to_hex, DebuginfodClient};
use object::{Object, ObjectSection, ObjectSymbol, SymbolKind};
use std::{borrow::Cow, collections::HashMap, path::Path, path::PathBuf, sync::Arc, time::Instant};

impl LoadedObjfile {
    /// Parallel loading: debug_info || debug_line || CFI simultaneously.
    pub(crate) async fn load_parallel(
        module_mapping: ModuleMapping,
        debug_search_paths: &[String],
        allow_loose_debug_match: bool,
        explicit_debug_file: Option<PathBuf>,
        debuginfod_client: Option<Arc<DebuginfodClient>>,
    ) -> Result<Self> {
        tracing::info!("Parallel loading for: {}", module_mapping.path.display());
        Self::load_internal_parallel(
            module_mapping,
            debug_search_paths,
            allow_loose_debug_match,
            explicit_debug_file,
            debuginfod_client,
        )
        .await
    }

    /// Parallel internal load implementation - true parallelism for debug_info || debug_line || CFI
    async fn load_internal_parallel(
        module_mapping: ModuleMapping,
        debug_search_paths: &[String],
        allow_loose_debug_match: bool,
        explicit_debug_file: Option<PathBuf>,
        debuginfod_client: Option<Arc<DebuginfodClient>>,
    ) -> Result<Self> {
        let load_started_at = Instant::now();
        tracing::debug!(
            "Loading module in parallel: {}",
            module_mapping.path.display()
        );

        let binary_mapped = Arc::new(MappedFile::open(&module_mapping.path)?);
        let (dwarf, mapped_file_for_dwarf, debug_info_source) = if let Some(debug_file_path) =
            explicit_debug_file
        {
            tracing::info!(
                "Loading DWARF from explicit debug file {} for {}",
                debug_file_path.display(),
                module_mapping.path.display()
            );
            let debug_mapped = Arc::new(load_explicit_debug_file(
                &module_mapping.path,
                &debug_file_path,
                allow_loose_debug_match,
            )?);
            let debug_dwarf = Self::load_dwarf_sections(&debug_mapped)?;
            if !Self::has_debug_info(&debug_dwarf) {
                return Err(anyhow::anyhow!(
                    "Explicit debug file {} for {} contains no .debug_info section",
                    debug_mapped.path.display(),
                    module_mapping.path.display()
                ));
            }
            (
                Arc::new(debug_dwarf),
                debug_mapped,
                DebugInfoSource::Explicit {
                    path: debug_file_path.display().to_string(),
                },
            )
        } else {
            let dwarf_result = Self::load_dwarf_sections(&binary_mapped);
            match dwarf_result {
                Ok(dwarf_data) => {
                    if Self::has_debug_info(&dwarf_data) {
                        tracing::debug!(
                            "Found debug info in binary: {}",
                            module_mapping.path.display()
                        );
                        (
                            Arc::new(dwarf_data),
                            Arc::clone(&binary_mapped),
                            DebugInfoSource::Embedded {
                                path: module_mapping.path.display().to_string(),
                            },
                        )
                    } else {
                        tracing::info!(
                            "No debug info in binary, searching for .gnu_debuglink: {}",
                            module_mapping.path.display()
                        );
                        match try_load_debug_file(
                            &module_mapping.path,
                            debug_search_paths,
                            allow_loose_debug_match,
                        )? {
                            Some(debug_mapped) => {
                                tracing::info!(
                                    "Loading DWARF from separate debug file: {}",
                                    debug_mapped.path.display()
                                );
                                let debug_mapped = Arc::new(debug_mapped);
                                let debug_path = debug_mapped.path.display().to_string();
                                let debug_dwarf = Self::load_dwarf_sections(&debug_mapped)?;
                                if Self::has_debug_info(&debug_dwarf) {
                                    (
                                        Arc::new(debug_dwarf),
                                        debug_mapped,
                                        DebugInfoSource::Debuglink { path: debug_path },
                                    )
                                } else {
                                    tracing::warn!(
                                        "Ignoring separate debug file {} for {} because it contains no .debug_info",
                                        debug_mapped.path.display(),
                                        module_mapping.path.display()
                                    );
                                    Self::load_debuginfod_debug_file_or_missing(
                                        debuginfod_client.as_ref(),
                                        &binary_mapped,
                                        &module_mapping.path,
                                        dwarf_data,
                                    )
                                    .await
                                }
                            }
                            None => {
                                Self::load_debuginfod_debug_file_or_missing(
                                    debuginfod_client.as_ref(),
                                    &binary_mapped,
                                    &module_mapping.path,
                                    dwarf_data,
                                )
                                .await
                            }
                        }
                    }
                }
                Err(e) => {
                    tracing::error!(
                        "Failed to parse DWARF from {}: {}",
                        module_mapping.path.display(),
                        e
                    );
                    return Err(e);
                }
            }
        };

        let mapped_file = mapped_file_for_dwarf;

        tracing::debug!(
            "Starting parallel DWARF parsing with true debug_line || debug_info parallelism..."
        );

        let (pair_result, unwind_info) = tokio::try_join!(
            tokio::task::spawn_blocking({
                let dwarf = Arc::clone(&dwarf);
                let module_path = module_mapping.path.to_string_lossy().to_string();
                move || -> Result<(crate::parser::LineParseResult, crate::parser::DebugParseResult)> {
                    let (line_res, info_res) = rayon::join(
                        || {
                            let parser = crate::parser::DwarfParser::new(&dwarf);
                            parser.parse_line_info(&module_path)
                        },
                        || {
                            let parser = crate::parser::DwarfParser::new(&dwarf);
                            parser.parse_debug_info(&module_path)
                        },
                    );
                    match (line_res, info_res) {
                        (Ok(l), Ok(i)) => Ok((l, i)),
                        (Err(e), _) => Err(e),
                        (_, Err(e)) => Err(e),
                    }
                }
            }),
            tokio::task::spawn_blocking({
                let binary_for_cfi = Arc::clone(&binary_mapped);
                let module_path = module_mapping.path.clone();
                move || ModuleUnwindInfo::from_mapped_file(binary_for_cfi, &module_path)
            })
        )?;

        let (line_result, info_result) = pair_result?;

        let parse_result = crate::parser::DwarfParser::combine_parallel_results(
            line_result,
            info_result,
            module_mapping.path.to_string_lossy().to_string(),
        );
        let parse_elapsed_ms = load_started_at.elapsed().as_millis();

        if let Some(cfi) = unwind_info.cfi_index() {
            let stats = cfi.get_stats();
            tracing::info!(
                "CFI stats: has_eh_frame_hdr={}, fast_lookup={}",
                stats.has_eh_frame_hdr,
                stats.has_fast_lookup
            );
            if cfi.has_fast_lookup() {
                tracing::debug!("CFI fast lookup enabled");
            }
        }

        let crate::parser::DwarfParseResult {
            lightweight_index,
            line_mapping,
            scoped_file_manager,
            compilation_units,
            stats,
        } = parse_result;
        let index_started_at = Instant::now();
        let (lightweight_index, type_name_index) = tokio::task::spawn_blocking(move || {
            let type_name_index = TypeNameIndex::build_from_lightweight(&lightweight_index);
            (lightweight_index, type_name_index)
        })
        .await?;
        let type_name_index = Arc::new(type_name_index);
        let dwarf =
            Arc::try_unwrap(dwarf).map_err(|_| anyhow::anyhow!("Failed to unwrap DWARF Arc"))?;
        let mut detailed_parser = DetailedParser::new();
        detailed_parser.set_type_name_index(Arc::clone(&type_name_index));
        let entry_address = Self::read_entry_address(&binary_mapped);
        let text_symbol_starts_by_name = Self::collect_text_symbol_starts(&binary_mapped);

        let mut warnings = Vec::new();
        if !unwind_info.has_cfi() {
            warnings.push("CFI index failed to initialize".to_string());
        }

        if !warnings.is_empty() {
            for warning in &warnings {
                tracing::warn!(
                    "Module {} loaded with warning: {}",
                    module_mapping.path.display(),
                    warning
                );
            }
        }

        let state_label = if warnings.is_empty() {
            "Success"
        } else {
            "PartialSuccess"
        };
        let index_elapsed_ms = index_started_at.elapsed().as_millis();
        let load_total_ms = load_started_at.elapsed().as_millis() as u64;

        let module = Self {
            module_mapping: module_mapping.clone(),
            lightweight_index,
            line_mapping,
            scoped_file_manager,
            compilation_units,
            unwind_info,
            dwarf,
            detailed_parser,
            block_index: std::sync::RwLock::new(BlockIndex::new()),
            type_name_index,
            _dwarf_mapped_file: mapped_file,
            _binary_mapped_file: binary_mapped,
            debug_info_source,
            entry_address,
            text_symbol_starts_by_name,
            function_ranges_cache: std::sync::RwLock::new(HashMap::new()),
            load_parse_ms: parse_elapsed_ms as u64,
            load_index_ms: index_elapsed_ms as u64,
            load_total_ms,
        };

        tracing::info!(
            "True parallel loading completed for {}: {} functions, {} variables, {} line entries, {} files (state: {}, parse_ms: {}, index_ms: {}, total_ms: {})",
            module.module_mapping.path.display(),
            stats.total_functions,
            stats.total_variables,
            stats.total_line_entries,
            stats.total_files,
            state_label,
            parse_elapsed_ms,
            index_elapsed_ms,
            load_total_ms
        );

        Ok(module)
    }

    fn read_entry_address(binary_mapped: &MappedFile) -> Option<u64> {
        let address = binary_mapped.parse_object().ok()?.entry();
        (address != 0).then_some(address)
    }

    fn has_debug_info(dwarf: &DwarfData) -> bool {
        matches!(dwarf.units().next(), Ok(Some(_)))
    }

    async fn load_debuginfod_debug_file_or_missing(
        debuginfod_client: Option<&Arc<DebuginfodClient>>,
        binary_mapped: &Arc<MappedFile>,
        module_path: &Path,
        fallback_dwarf: DwarfData,
    ) -> (Arc<DwarfData>, Arc<MappedFile>, DebugInfoSource) {
        match Self::try_load_debuginfod_debug_file(debuginfod_client, binary_mapped, module_path)
            .await
        {
            Some((debug_dwarf, debug_mapped)) => {
                let debug_path = debug_mapped.path.display().to_string();
                (
                    Arc::new(debug_dwarf),
                    debug_mapped,
                    DebugInfoSource::Debuginfod { path: debug_path },
                )
            }
            None => {
                tracing::warn!(
                    "No usable separate debug file found for: {}",
                    module_path.display()
                );
                (
                    Arc::new(fallback_dwarf),
                    Arc::clone(binary_mapped),
                    DebugInfoSource::Missing,
                )
            }
        }
    }

    async fn try_load_debuginfod_debug_file(
        debuginfod_client: Option<&Arc<DebuginfodClient>>,
        binary_mapped: &Arc<MappedFile>,
        module_path: &Path,
    ) -> Option<(DwarfData, Arc<MappedFile>)> {
        let client = debuginfod_client?;
        let build_id = match Self::build_id_for_debuginfod(binary_mapped, module_path) {
            Some(build_id) => build_id,
            None => return None,
        };
        let build_id_hex = build_id_to_hex(&build_id);

        tracing::info!(
            "Trying debuginfod debug info for {} (build-id={})",
            module_path.display(),
            build_id_hex
        );

        let fetched = match client.fetch_debuginfo(&build_id).await {
            Ok(Some(fetched)) => fetched,
            Ok(None) => {
                tracing::debug!(
                    "debuginfod had no debug info for {} (build-id={})",
                    module_path.display(),
                    build_id_hex
                );
                return None;
            }
            Err(err) => {
                tracing::warn!(
                    "debuginfod lookup failed for {} (build-id={}): {}",
                    module_path.display(),
                    build_id_hex,
                    err
                );
                return None;
            }
        };

        tracing::info!(
            "debuginfod returned debug info for {} from {} (path={}, from_cache={})",
            module_path.display(),
            fetched.url.as_deref().unwrap_or("<cache>"),
            fetched.path.display(),
            fetched.from_cache
        );

        let debug_mapped = match MappedFile::open(&fetched.path) {
            Ok(mapped) => Arc::new(mapped),
            Err(err) => {
                tracing::warn!(
                    "Failed to open debuginfod debug file {} for {}: {}",
                    fetched.path.display(),
                    module_path.display(),
                    err
                );
                return None;
            }
        };

        if !Self::debuginfod_debug_file_matches_build_id(&debug_mapped, &build_id, module_path) {
            return None;
        }

        let debug_dwarf = match Self::load_dwarf_sections(&debug_mapped) {
            Ok(dwarf) => dwarf,
            Err(err) => {
                tracing::warn!(
                    "Failed to load DWARF sections from debuginfod debug file {} for {}: {}",
                    debug_mapped.path.display(),
                    module_path.display(),
                    err
                );
                return None;
            }
        };

        if !Self::has_debug_info(&debug_dwarf) {
            tracing::warn!(
                "Ignoring debuginfod debug file {} for {} because it contains no .debug_info",
                debug_mapped.path.display(),
                module_path.display()
            );
            return None;
        }

        tracing::info!(
            "Loading DWARF from debuginfod debug file: {}",
            debug_mapped.path.display()
        );
        Some((debug_dwarf, debug_mapped))
    }

    fn build_id_for_debuginfod(binary_mapped: &MappedFile, module_path: &Path) -> Option<Vec<u8>> {
        let object = match binary_mapped.parse_object() {
            Ok(object) => object,
            Err(err) => {
                tracing::warn!(
                    "Failed to parse object while reading build-id for {}: {}",
                    module_path.display(),
                    err
                );
                return None;
            }
        };

        match object.build_id() {
            Ok(Some(build_id)) => Some(build_id.to_vec()),
            Ok(None) => {
                tracing::debug!(
                    "No build-id in {}; skipping debuginfod",
                    module_path.display()
                );
                None
            }
            Err(err) => {
                tracing::warn!(
                    "Failed to read build-id from {}; skipping debuginfod: {}",
                    module_path.display(),
                    err
                );
                None
            }
        }
    }

    fn debuginfod_debug_file_matches_build_id(
        debug_mapped: &MappedFile,
        expected_build_id: &[u8],
        module_path: &Path,
    ) -> bool {
        let expected = build_id_to_hex(expected_build_id);
        let object = match debug_mapped.parse_object() {
            Ok(object) => object,
            Err(err) => {
                tracing::warn!(
                    "Ignoring debuginfod debug file {} for {}: failed to parse object: {}",
                    debug_mapped.path.display(),
                    module_path.display(),
                    err
                );
                return false;
            }
        };

        match object.build_id() {
            Ok(Some(actual_build_id)) if actual_build_id == expected_build_id => {
                tracing::info!(
                    "debuginfod build-id verification passed for {}: {}",
                    debug_mapped.path.display(),
                    expected
                );
                true
            }
            Ok(Some(actual_build_id)) => {
                tracing::warn!(
                    "Ignoring debuginfod debug file {} for {}: build-id mismatch expected={}, actual={}",
                    debug_mapped.path.display(),
                    module_path.display(),
                    expected,
                    build_id_to_hex(actual_build_id)
                );
                false
            }
            Ok(None) => {
                tracing::warn!(
                    "Ignoring debuginfod debug file {} for {}: missing build-id, expected={}",
                    debug_mapped.path.display(),
                    module_path.display(),
                    expected
                );
                false
            }
            Err(err) => {
                tracing::warn!(
                    "Ignoring debuginfod debug file {} for {}: failed to read build-id: {}",
                    debug_mapped.path.display(),
                    module_path.display(),
                    err
                );
                false
            }
        }
    }

    fn collect_text_symbol_starts(binary_mapped: &MappedFile) -> HashMap<String, Vec<u64>> {
        let object = match binary_mapped.parse_object() {
            Ok(object) => object,
            Err(err) => {
                tracing::warn!(
                    "Failed to parse object while building text symbol cache for {}: {}",
                    binary_mapped.path.display(),
                    err
                );
                return HashMap::new();
            }
        };

        let mut by_name: HashMap<String, Vec<u64>> = HashMap::new();
        let mut collect_symbol = |symbol: object::Symbol<'_, '_, &[u8]>| {
            if symbol.kind() != SymbolKind::Text {
                return;
            }

            let Ok(name) = symbol.name() else {
                return;
            };
            by_name
                .entry(name.to_string())
                .or_default()
                .push(symbol.address());
        };

        for symbol in object.symbols() {
            collect_symbol(symbol);
        }

        for symbol in object.dynamic_symbols() {
            collect_symbol(symbol);
        }

        for starts in by_name.values_mut() {
            starts.sort_unstable();
            starts.dedup();
        }

        by_name
    }

    fn load_dwarf_sections(file_data: &Arc<MappedFile>) -> Result<DwarfData> {
        let object = file_data.parse_object()?;
        let endian = dwarf_endian_from_object(&object);

        let load_section = |id: gimli::SectionId| -> Result<_> {
            if let Some(section) = object.section_by_name(id.name()) {
                let compressed_range = section.compressed_file_range()?;
                if compressed_range.format != object::read::CompressionFormat::None {
                    let data = section.uncompressed_data().map_err(|err| {
                        anyhow::anyhow!(
                            "Failed to decompress DWARF section {} in {}: {}",
                            id.name(),
                            file_data.path.display(),
                            err
                        )
                    })?;
                    let bytes: Arc<[u8]> = match data {
                        Cow::Borrowed(bytes) => Arc::from(bytes),
                        Cow::Owned(bytes) => Arc::from(bytes),
                    };
                    Ok(dwarf_reader_from_arc_with_endian(bytes, endian))
                } else if let Some((start, size)) = section.file_range() {
                    MappedFile::dwarf_reader_range(Arc::clone(file_data), start, size, endian)
                        .ok_or_else(|| {
                            anyhow::anyhow!("Invalid DWARF section range for {}", id.name())
                        })
                } else {
                    Ok(empty_dwarf_reader_with_endian(endian))
                }
            } else {
                Ok(empty_dwarf_reader_with_endian(endian))
            }
        };

        let dwarf = gimli::Dwarf::load(load_section)?;
        Ok(dwarf)
    }
}