rudy-dwarf 0.2.0

DWARF debug information parsing and querying for Rust debugging tools
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
use super::Die;
use crate::{
    die::{navigation::get_roots, utils::get_string_attr, UnitRef},
    file::{DebugFile, DwarfReader, RawDie},
    parser::primitives::rust_cu,
    DwarfDb,
};

use anyhow::Result;

/// Walker that drives the visitor through the DIE tree
pub struct DieWalker<'a, 'db, V> {
    pub visitor: &'a mut V,
    pub db: &'db dyn DwarfDb,

    /// The file being walked
    pub(crate) file: DebugFile,
    /// The compilation unit being walked
    unit_offset: gimli::UnitSectionOffset<usize>,
    pub(crate) unit_ref: &'a UnitRef<'a>,
    /// The entries cursor for the current unit
    // unit_ref: &'a UnitRef<'a>,
    cursor: gimli::EntriesCursor<'a, 'a, DwarfReader>,

    /// The current depth in the DIE tree
    current_depth: isize,
    last_offset: usize,
    /// The next peeked entry, if any
    /// Stored as (depth, RawDie)
    next_entry: Option<(isize, RawDie<'a>)>,
    depth: isize,
}

pub struct VisitorNode<'a> {
    pub(crate) die: RawDie<'a>,
    pub(crate) unit_ref: UnitRef<'a>,
}

impl<'a> VisitorNode<'a> {
    pub fn name(&self) -> Result<Option<String>> {
        get_string_attr(&self.die, gimli::DW_AT_name, &self.unit_ref)
    }

    pub fn tag(&self) -> gimli::DwTag {
        self.die.tag()
    }
}

pub fn walk_file<'db, 'a, V: DieVisitor<'db>>(
    db: &'db dyn DwarfDb,
    file: DebugFile,
    visitor: &'a mut V,
) -> Result<()> {
    tracing::trace!("Walking DWARF for file: {}", file.name(db));
    // Get the root compilation units for this file
    let roots = get_roots(db, file);

    for (unit_offset, unit_ref) in &roots {
        // Create a walker for each unit
        let mut walker = DieWalker {
            db,
            visitor: &mut *visitor,
            file,
            unit_offset: *unit_offset,
            unit_ref,
            cursor: unit_ref.entries(),
            current_depth: 0,
            next_entry: None,
            depth: 0,
            last_offset: unit_offset.as_debug_info_offset().map(|o| o.0).unwrap_or(0),
        };

        // Walk the compilation unit
        tracing::trace!(
            "Walking CU: {:#x}",
            unit_offset.as_debug_info_offset().unwrap().0
        );
        walker.walk_unit()?;
    }
    Ok(())
}

pub fn walk_die<'db, 'a, V: DieVisitor<'db>>(
    db: &'db dyn DwarfDb,
    die: Die<'db>,
    visitor: &'a mut V,
) -> anyhow::Result<()> {
    tracing::trace!("Walking DWARF for DIE: {}", die.print(db));

    die.with_entry_and_unit(db, |entry, unit_ref| {
        let cursor = unit_ref.entries_at_offset(entry.offset())?;
        let mut walker = DieWalker {
            db,
            visitor,
            file: die.file(db),
            unit_offset: die.cu_offset(db),
            unit_ref,
            cursor,
            current_depth: 0,
            next_entry: None,
            depth: 0,
            last_offset: entry.offset().0,
        };
        let Some(die) = walker.next() else {
            tracing::error!("No entries found in DIE: {}", die.print(db));
            return Ok(());
        };
        let node = VisitorNode {
            die,
            unit_ref: *unit_ref,
        };
        V::visit_die(&mut walker, node)
    })?
}

impl<'a, 'db, V: DieVisitor<'db>> DieWalker<'a, 'db, V> {
    pub(crate) fn get_die(&self, raw: RawDie<'a>) -> Die<'db> {
        Die::new(self.db, self.file, self.unit_offset, raw.offset())
    }

    pub(crate) fn peek_next_offset(&mut self) -> Option<usize> {
        let next = self.peek()?.1;
        Some(self.get_die(next).offset(self.db))
    }

    pub fn parse<T>(
        &self,
        node: VisitorNode<'a>,
        parser: impl crate::parser::Parser<'db, T>,
    ) -> Result<T> {
        let die = self.get_die(node.die);
        parser.parse(self.db, die)
    }

    fn peek(&mut self) -> Option<(isize, RawDie<'a>)> {
        if self.next_entry.is_none() {
            match self.cursor.next_dfs() {
                Ok(Some((depth_delta, die))) => {
                    self.depth += depth_delta;
                    // Store the next entry for later use
                    self.next_entry = Some((self.depth, die.clone()));
                }
                Ok(None) => {
                    // No more entries to peek at
                    return None;
                }
                Err(e) => {
                    tracing::error!("Failed to parse DIE: {e}");
                    // we'll stop walking on error
                    return None;
                }
            }
        }

        self.next_entry.clone()
    }

    fn has_children(&mut self) -> bool {
        // The current DIE has children if the next entry is a child of the current DIE
        if let Some((depth, _)) = self.peek() {
            depth == self.current_depth + 1
        } else {
            // if there's no next entry, then there are no children
            false
        }
    }

    fn next(&mut self) -> Option<RawDie<'a>> {
        // if we have a peeked entry, return it
        if let Some((depth, die)) = self.next_entry.take() {
            debug_assert_eq!(
                depth, self.current_depth,
                "`next` must only be called when already at the correct depth"
            );
            self.last_offset = die.offset().0;
            return Some(die);
        }

        // otherwise, get the next entry from the cursor
        match self.cursor.next_dfs() {
            Ok(Some((delta, die))) => {
                debug_assert_eq!(
                    delta, 0,
                    "`next` must only be called when already at the correct depth"
                );
                self.last_offset = die.offset().0;
                Some(die.clone())
            }
            Ok(None) => None,
            Err(e) => {
                tracing::error!("Failed to parse DIE: {e}");
                None
            }
        }
    }

    fn next_sibling(&mut self) -> Option<RawDie<'a>> {
        match self.peek() {
            Some((depth, die)) if depth == self.current_depth => {
                tracing::trace!("got sibling {:#x} at depth {depth}", die.offset().0);
                // we have a sibling at the current depth, return it
                self.last_offset = die.offset().0;
                self.next_entry.take().map(|(_, die)| die)
            }
            // the next entry is a child of the current DIE, so we need to skip it
            Some((depth, _)) if depth > self.current_depth => {
                tracing::trace!(
                    "next entry is a descendent of {} at depth {depth}, skipping (and removing all siblings)",
                    self.current_depth
                );
                self.next_entry.take();
                while self.cursor.next_sibling().ok().flatten().is_some() {
                    // keep skipping siblings until we find one at the current depth
                }
                self.next_sibling()
            }
            // either we have no next entry, or the next entry is not a sibling
            Some((depth, _)) => {
                tracing::trace!(
                    "next entry is a sibling of a parent at depth {depth} < {}, we're out of siblings",
                    self.current_depth
                );
                None
            }
            _ => {
                tracing::trace!("no next entry, returning None");
                None
            }
        }
    }

    pub fn walk_unit(&mut self) -> Result<()> {
        let Some(root) = self.next() else {
            // empty tree -- nothing to walk
            tracing::info!("No entries found in DWARF tree");
            return Ok(());
        };

        // first entry _should_ be the root DIE -- the compilation unit
        let tag = root.tag();
        if tag != gimli::DW_TAG_compile_unit {
            tracing::error!("Expected root DIE to be a compilation unit, found: {tag}");
            return Err(anyhow::anyhow!(
                "Expected root DIE to be a compilation unit, found: {tag}"
            ));
        }

        let unit_ref = *self.unit_ref;
        tracing::trace!("Visiting CU: {:#x}", root.offset().0);
        let node = VisitorNode {
            die: root,
            unit_ref,
        };
        V::visit_cu(self, node)
    }

    pub fn walk_children(&mut self) -> Result<()> {
        let current_offset = self.cursor.current().map_or(0, |c| c.offset().0);

        if !self.has_children() {
            return Ok(());
        }

        self.current_depth += 1;
        tracing::trace!(
            "Walking children of entry: {current_offset:#x} at depth: {}",
            self.current_depth
        );

        // walk the siblings at this depth
        while let Some(next) = self.next_sibling() {
            // continue walking siblings
            let node = VisitorNode {
                die: next,
                unit_ref: *self.unit_ref,
            };
            V::visit_die(self, node)?;
        }
        tracing::trace!("Finished walking children of entry: {current_offset:#x}");
        self.current_depth -= 1;
        Ok(())
    }

    pub fn walk_cu(&mut self) -> Result<()> {
        self.walk_children()
    }

    pub fn walk_namespace(&mut self) -> Result<()> {
        self.walk_children()
    }
}

/// Visitor trait for walking DWARF DIE trees
pub trait DieVisitor<'db>: Sized {
    fn visit_cu<'a>(walker: &mut DieWalker<'a, 'db, Self>, node: VisitorNode<'a>) -> Result<()> {
        if walker.parse(node, rust_cu())? {
            walker.walk_cu()?;
        }
        Ok(())
    }

    /// Called for each DIE entry
    fn visit_die<'a>(walker: &mut DieWalker<'a, 'db, Self>, node: VisitorNode<'a>) -> Result<()> {
        tracing::trace!("Visiting DIE: {:#010x}", node.die.offset().0);
        // Default: dispatch to specific visit methods based on tag
        match node.die.tag() {
            gimli::DW_TAG_namespace => Self::visit_namespace(walker, node),
            gimli::DW_TAG_subprogram => Self::visit_function(walker, node),
            gimli::DW_TAG_structure_type => Self::visit_struct(walker, node),
            gimli::DW_TAG_enumeration_type => Self::visit_enum(walker, node),
            gimli::DW_TAG_variable => Self::visit_variable(walker, node),
            gimli::DW_TAG_formal_parameter => Self::visit_parameter(walker, node),
            gimli::DW_TAG_base_type => Self::visit_base_type(walker, node),
            gimli::DW_TAG_pointer_type => Self::visit_pointer_type(walker, node),
            gimli::DW_TAG_array_type => Self::visit_array_type(walker, node),
            gimli::DW_TAG_lexical_block => Self::visit_lexical_block(walker, node),
            gimli::DW_TAG_union_type => Self::visit_union_type(walker, node),
            gimli::DW_TAG_subroutine_type => {
                // these don't seem to contain much, so we'll skip
                Ok(())
            }
            gimli::DW_TAG_member
            | gimli::DW_TAG_template_type_parameter
            | gimli::DW_TAG_variant_part
            | gimli::DW_TAG_subrange_type
            | gimli::DW_TAG_enumerator
            | gimli::DW_TAG_inlined_subroutine => {
                // these should typically be visited explicitly
                // as part of visiting the parent
                Ok(())
            }
            _ => {
                tracing::debug!(
                    "Unhandled DIE tag: {} {}",
                    node.die.tag(),
                    walker.get_die(node.die).location(walker.db)
                );
                Ok(())
            }
        }
    }

    /// Visit a namespace
    fn visit_namespace<'a>(
        walker: &mut DieWalker<'a, 'db, Self>,
        _node: VisitorNode<'a>,
    ) -> Result<()> {
        walker.walk_namespace()
    }

    /// Visit a function/subprogram
    fn visit_function<'a>(
        walker: &mut DieWalker<'a, 'db, Self>,
        _node: VisitorNode<'a>,
    ) -> Result<()> {
        walker.walk_children()
    }

    /// Visit a struct type
    fn visit_struct<'a>(
        walker: &mut DieWalker<'a, 'db, Self>,
        _node: VisitorNode<'a>,
    ) -> Result<()> {
        walker.walk_children()
    }

    /// Visit an enum type
    fn visit_enum<'a>(walker: &mut DieWalker<'a, 'db, Self>, _node: VisitorNode<'a>) -> Result<()> {
        walker.walk_children()
    }

    /// Visit a variable
    fn visit_variable<'a>(
        walker: &mut DieWalker<'a, 'db, Self>,
        _node: VisitorNode<'a>,
    ) -> Result<()> {
        walker.walk_children()
    }

    /// Visit a variable
    fn visit_parameter<'a>(
        walker: &mut DieWalker<'a, 'db, Self>,
        _node: VisitorNode<'a>,
    ) -> Result<()> {
        walker.walk_children()
    }

    /// Visit a base type
    fn visit_base_type<'a>(
        walker: &mut DieWalker<'a, 'db, Self>,
        _node: VisitorNode<'a>,
    ) -> Result<()> {
        walker.walk_children()
    }

    /// Visit a pointer type
    fn visit_pointer_type<'a>(
        walker: &mut DieWalker<'a, 'db, Self>,
        _node: VisitorNode<'a>,
    ) -> Result<()> {
        walker.walk_children()
    }

    /// Visit a array type
    fn visit_array_type<'a>(
        walker: &mut DieWalker<'a, 'db, Self>,
        _node: VisitorNode<'a>,
    ) -> Result<()> {
        walker.walk_children()
    }
    fn visit_union_type<'a>(
        walker: &mut DieWalker<'a, 'db, Self>,
        _node: VisitorNode<'a>,
    ) -> Result<()> {
        walker.walk_children()
    }

    fn visit_lexical_block<'a>(
        walker: &mut DieWalker<'a, 'db, Self>,
        _node: VisitorNode<'a>,
    ) -> Result<()> {
        walker.walk_children()
    }
}

#[cfg(test)]
mod test {
    use itertools::Itertools;

    use super::*;

    use anyhow::Result;

    struct TestVisitor {
        pub visited: Vec<String>,
    }

    impl<'db> super::DieVisitor<'db> for TestVisitor {
        fn visit_cu<'a>(
            walker: &mut super::DieWalker<'a, 'db, Self>,
            node: VisitorNode<'a>,
        ) -> Result<()> {
            Self::visit_die(walker, node)
        }

        fn visit_die<'a>(
            walker: &mut super::DieWalker<'a, 'db, Self>,
            node: VisitorNode<'a>,
        ) -> Result<()> {
            let offset = node.die.offset().0;
            let padding = std::iter::repeat_n(" ", 2 * walker.current_depth as usize).join("");

            let tag = node.die.tag();
            walker
                .visitor
                .visited
                .push(format!("{offset:#010x}: {padding}{tag}"));
            // Default: dispatch to specific visit methods based on tag
            match tag {
                gimli::DW_TAG_namespace => Self::visit_namespace(walker, node)?,
                gimli::DW_TAG_subprogram => Self::visit_function(walker, node)?,
                gimli::DW_TAG_structure_type => Self::visit_struct(walker, node)?,
                gimli::DW_TAG_enumeration_type => Self::visit_enum(walker, node)?,
                gimli::DW_TAG_variable => Self::visit_variable(walker, node)?,
                gimli::DW_TAG_base_type => Self::visit_base_type(walker, node)?,
                gimli::DW_TAG_compile_unit => {}
                _ => {}
            }
            walker.walk_children()
        }
    }

    #[test]
    fn test_visitor() {
        let _guard = crate::test_utils::init_tracing_and_insta();

        let small_file = crate::test_utils::root_artifacts_dir()
            .join("x86_64-unknown-linux-gnu")
            .join("small");
        let db = crate::test_utils::test_db(None);
        let db = &db;
        let binary = crate::test_utils::load_binary(db, small_file);
        let mut visitor = TestVisitor {
            visited: Vec::new(),
        };

        let debug_file = crate::file::DebugFile::new(db, binary.file(db), false);

        super::walk_file(db, debug_file, &mut visitor).unwrap();

        // Check that we visited the expected entries
        assert!(!visitor.visited.is_empty(), "No entries were visited");
        insta::assert_snapshot!(visitor.visited.join("\n"));
    }

    #[derive(Default)]
    struct ModuleFunctionVisitor {
        pub path: Vec<String>,
        pub functions: Vec<String>,
    }

    impl<'db> super::DieVisitor<'db> for ModuleFunctionVisitor {
        fn visit_struct<'a>(
            walker: &mut super::DieWalker<'a, 'db, Self>,
            node: VisitorNode<'a>,
        ) -> Result<()> {
            let name = node
                .name()
                .unwrap()
                .unwrap_or_else(|| "<default>".to_string());
            walker.visitor.path.push(name);
            walker.walk_children()?;
            walker.visitor.path.pop();
            Ok(())
        }

        fn visit_namespace<'a>(
            walker: &mut super::DieWalker<'a, 'db, Self>,
            node: VisitorNode<'a>,
        ) -> Result<()> {
            let name = node
                .name()
                .unwrap()
                .unwrap_or_else(|| "<unnamed>".to_string());
            walker.visitor.path.push(name);
            walker.walk_namespace()?;
            walker.visitor.path.pop();
            Ok(())
        }

        fn visit_function<'a>(
            walker: &mut super::DieWalker<'a, 'db, Self>,
            node: VisitorNode<'a>,
        ) -> Result<()> {
            let name = node
                .name()
                .unwrap()
                .unwrap_or_else(|| "<unnamed>".to_string());
            let mut path = walker.visitor.path.clone();
            path.push(name);
            walker.visitor.functions.push(path.join("::"));
            Ok(())
        }
    }

    #[test]
    fn methods_and_functions() {
        let _guard = crate::test_utils::init_tracing_and_insta();

        let small_file = crate::test_utils::root_artifacts_dir()
            .join("x86_64-unknown-linux-gnu")
            .join("small");

        // Create a test database
        let db = crate::test_utils::test_db(None);
        let db = &db;

        // Create a test binary
        let binary = crate::test_utils::load_binary(db, small_file);

        let (debug_files, _) = crate::symbols::index_symbol_map(db, binary).unwrap();

        let mut visitor = ModuleFunctionVisitor::default();
        for (_, file) in debug_files {
            super::walk_file(db, file, &mut visitor).unwrap();
        }

        visitor.functions.retain(|f| f != "<unnamed>");
        visitor.functions.dedup();
        visitor.functions.sort();

        // Check that we visited the expected entries
        assert!(!visitor.functions.is_empty(), "No functions were visited");
        insta::assert_snapshot!(visitor.functions.join("\n"));
    }
}