gsym-rs 0.1.4

Pure-Rust reader, writer, and Linux ELF/DWARF converter for LLVM GSYM
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
use crate::endian::{Cursor, Endian};
use crate::error::{Error, Result};
use crate::format::function::{InfoType, check_inline_depth};
use crate::format::leb::read_uleb;
use crate::format::line;
use crate::model::{FileIndex, LineEntry, Lookup, LookupFrame};
use smallvec::SmallVec;

use super::function::RawFunction;
use super::{FrameLookupOptions, Gsym, LookupOptions, LookupScratch, RawInlineFrame};

#[derive(Clone, Copy, Debug, Default)]
struct ScannedRecords<'data> {
    line: Option<LineEntry>,
    inline: Option<&'data [u8]>,
    call_sites: Option<&'data [u8]>,
}

#[derive(Clone, Copy, Debug)]
struct FrameRequest<'data> {
    address: u64,
    options: LookupOptions,
    function: RawFunction<'data>,
}

#[derive(Clone, Copy, Debug)]
struct PreparedFrames<'data> {
    function_name: &'data [u8],
    directory: &'data [u8],
    basename: &'data [u8],
    line: u32,
    count: usize,
    call_sites: Option<&'data [u8]>,
}

impl<D: AsRef<[u8]>> Gsym<D> {
    /// Resolves an unslid virtual address with the default lookup options.
    ///
    /// `address` must be an address of the image the file describes. An address
    /// from a running PIE executable or shared object needs its load bias
    /// removed first; see
    /// [`docs::symbolication`](crate::docs::symbolication).
    ///
    /// Returns `Ok(None)` when no function covers the address, which is the
    /// normal answer for padding between functions and for addresses belonging
    /// to another module. The result borrows names and paths from this reader.
    ///
    /// Frames come back innermost first. Use
    /// [`Self::lookup_with_options`] to skip record kinds you do not need, or
    /// [`Self::for_each_frame`] to resolve an address without allocating.
    ///
    /// # Errors
    ///
    /// Returns an error if the matched function's data is malformed.
    ///
    /// ```
    /// use gsym::{AddressRange, FileEntry, Function, Gsym, GsymBuilder, LineEntry};
    ///
    /// let mut builder = GsymBuilder::new();
    /// let file = builder.add_file(FileEntry::new(b"/src", b"main.rs"))?;
    /// builder.add_function(Function {
    ///     lines: vec![LineEntry::new(0x1000, file, 7)],
    ///     ..Function::new(AddressRange::new(0x1000, 0x1010), b"main")
    /// })?;
    /// let bytes = builder.to_bytes()?;
    /// let gsym = Gsym::parse(&bytes)?;
    ///
    /// let hit = gsym.lookup(0x1004)?.expect("covered address");
    /// assert_eq!(hit.frames()[0].name, b"main");
    /// assert_eq!(hit.frames()[0].basename, b"main.rs");
    /// assert_eq!(hit.frames()[0].line, 7);
    ///
    /// assert!(gsym.lookup(0x2000)?.is_none());
    /// # Ok::<(), gsym::Error>(())
    /// ```
    pub fn lookup(&self, address: u64) -> Result<Option<Lookup<'_>>> {
        let mut scratch = LookupScratch::default();
        self.lookup_with_options(address, LookupOptions::default(), &mut scratch)
    }

    /// Resolves an address while reusing caller-owned inline scratch storage.
    ///
    /// Same result as [`Self::lookup`], with control over which optional
    /// records are read and with the scratch buffer supplied by the caller.
    /// The returned [`Lookup`] still owns its frames; use
    /// [`Self::for_each_frame`] to avoid that allocation as well.
    ///
    /// # Errors
    ///
    /// Returns an error if the matched function's data is malformed.
    pub fn lookup_with_options<'data>(
        &'data self,
        address: u64,
        options: LookupOptions,
        scratch: &mut LookupScratch,
    ) -> Result<Option<Lookup<'data>>> {
        scratch.clear();
        let Some(function) = self.matching_function(address)? else {
            return Ok(None);
        };
        let request = FrameRequest {
            address,
            options,
            function,
        };
        let prepared = self.prepare_frames(request, scratch)?;
        let mut frames = Vec::with_capacity(prepared.count);
        self.emit_frames(request, prepared, scratch, |frame| {
            frames.push(frame);
        })?;
        self.finish_lookup(address, function, prepared.call_sites, frames)
            .map(Some)
    }

    /// Visits source frames without allocating an output collection.
    ///
    /// Frames are yielded innermost first and borrow their names and paths from
    /// the reader. Sizing the [`LookupScratch`] with
    /// [`LookupScratch::with_capacity`] keeps repeated lookups allocation-free.
    ///
    /// Returns whether a function covered the address. The visitor is not
    /// called when it returns `false`. Call-site patterns are not reported
    /// here; use [`Self::lookup_with_options`] when they are needed.
    ///
    /// ```
    /// use gsym::{
    ///     AddressRange, FrameLookupOptions, Function, Gsym, GsymBuilder,
    ///     LookupScratch,
    /// };
    ///
    /// let mut builder = GsymBuilder::new();
    /// builder.add_function(Function::new(
    ///     AddressRange::new(0x3000, 0x3010),
    ///     b"visited",
    /// ))?;
    /// let bytes = builder.to_bytes()?;
    /// let gsym = Gsym::parse(bytes)?;
    ///
    /// let mut scratch = LookupScratch::with_capacity(8);
    /// let mut names = Vec::new();
    /// let found = gsym.for_each_frame(
    ///     0x3004,
    ///     FrameLookupOptions::default(),
    ///     &mut scratch,
    ///     |frame| names.push(frame.name.to_vec()),
    /// )?;
    /// assert!(found);
    /// assert_eq!(names, [b"visited".to_vec()]);
    /// # Ok::<(), gsym::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if the matched function's data is malformed.
    pub fn for_each_frame<'data>(
        &'data self,
        address: u64,
        options: FrameLookupOptions,
        scratch: &mut LookupScratch,
        visitor: impl FnMut(LookupFrame<'data>),
    ) -> Result<bool> {
        scratch.clear();
        let Some(function) = self.matching_function(address)? else {
            return Ok(false);
        };
        self.visit_function_frames(
            FrameRequest {
                address,
                options: options.into(),
                function,
            },
            scratch,
            visitor,
        )?;
        Ok(true)
    }

    fn matching_function(&self, address: u64) -> Result<Option<RawFunction<'_>>> {
        let Some(mut index) = self.find_address_index(address)? else {
            return Ok(None);
        };
        let first_start = self.address(index)?;
        while index > 0 && self.address(index.saturating_sub(1))? == first_start {
            index = index.saturating_sub(1);
        }
        let count = self.layout.address_count as usize;
        while index < count {
            let raw = self.raw_function_at(index, first_start)?;
            if raw.range.is_empty() || raw.range.contains(address) {
                return Ok(Some(raw));
            }
            index = index.saturating_add(1);
            if index >= count || self.address(index)? != first_start {
                break;
            }
        }
        Ok(None)
    }

    fn visit_function_frames<'data>(
        &'data self,
        request: FrameRequest<'data>,
        scratch: &mut LookupScratch,
        visitor: impl FnMut(LookupFrame<'data>),
    ) -> Result<Option<&'data [u8]>> {
        let prepared = self.prepare_frames(request, scratch)?;
        self.emit_frames(request, prepared, scratch, visitor)?;
        Ok(prepared.call_sites)
    }

    fn prepare_frames<'data>(
        &'data self,
        request: FrameRequest<'data>,
        scratch: &mut LookupScratch,
    ) -> Result<PreparedFrames<'data>> {
        let records = self.scan_records(request)?;
        let function = request.function;
        let function_name = self.string(function.name)?;
        let (directory, basename, line_number) = if let Some(row) = records.line {
            let (directory, basename) = self.file(row.file)?;
            (directory, basename, row.line)
        } else {
            (&[][..], &[][..], 0)
        };

        if let Some(payload) = records.inline {
            let mut cursor = Cursor::new(payload, self.layout.endian);
            let mut scan = InlineScan {
                string_offset_size: self.layout.string_offset_size,
                address: request.address,
                frames: &mut scratch.inline_frames,
            };
            let (present, _) =
                scan_inline_node(&mut cursor, function.range.start, true, 0, &mut scan)?;
            if !present || !cursor.is_empty() {
                return Err(Error::InvalidFormat("malformed inline-info payload"));
            }
        }

        Ok(PreparedFrames {
            function_name,
            directory,
            basename,
            line: line_number,
            count: scratch.inline_frames.len().max(1),
            call_sites: records.call_sites,
        })
    }

    fn emit_frames<'data>(
        &'data self,
        request: FrameRequest<'data>,
        prepared: PreparedFrames<'data>,
        scratch: &LookupScratch,
        mut visitor: impl FnMut(LookupFrame<'data>),
    ) -> Result<()> {
        let PreparedFrames {
            function_name,
            directory,
            basename,
            line: line_number,
            ..
        } = prepared;
        let address = request.address;
        let function = request.function;

        if scratch.inline_frames.is_empty() {
            visitor(LookupFrame {
                name: function_name,
                directory,
                basename,
                line: line_number,
                offset: address.saturating_sub(function.range.start),
                inlined: false,
            });
        } else {
            let nodes = &scratch.inline_frames;
            for (index, node) in nodes.iter().enumerate().rev() {
                let (frame_directory, frame_basename, frame_line) =
                    if let Some(callee) = nodes.get(index.saturating_add(1)) {
                        let (directory, basename) = self.file(callee.call_file)?;
                        (directory, basename, callee.call_line)
                    } else {
                        (directory, basename, line_number)
                    };
                visitor(LookupFrame {
                    name: if node.name == 0 {
                        function_name
                    } else {
                        self.string(node.name)?
                    },
                    directory: frame_directory,
                    basename: frame_basename,
                    line: frame_line,
                    offset: address.saturating_sub(node.start),
                    inlined: index != 0,
                });
            }
        }
        Ok(())
    }

    fn scan_records<'data>(&self, request: FrameRequest<'data>) -> Result<ScannedRecords<'data>> {
        let FrameRequest {
            address,
            options,
            function,
        } = request;
        if !options.line_information && !options.inline_frames && !options.call_sites {
            return Ok(ScannedRecords::default());
        }
        let mut scanned = ScannedRecords::default();
        for record in function.records(self.layout.endian) {
            let record = record?;
            match record.kind {
                InfoType::LineTable => {
                    if options.line_information {
                        scanned.line = line::lookup(
                            record.payload,
                            self.layout.endian,
                            function.range.start,
                            address,
                        )?;
                    }
                }
                InfoType::Inline => {
                    if options.inline_frames {
                        scanned.inline = Some(record.payload);
                    }
                }
                InfoType::Merged | InfoType::Unknown(_) => {}
                InfoType::CallSite => {
                    if options.call_sites {
                        scanned.call_sites = Some(record.payload);
                    }
                }
            }
        }
        Ok(scanned)
    }

    fn finish_lookup<'data>(
        &'data self,
        address: u64,
        function: RawFunction<'data>,
        call_site_payload: Option<&[u8]>,
        frames: Vec<LookupFrame<'data>>,
    ) -> Result<Lookup<'data>> {
        let mut call_site_patterns = Vec::new();
        if let Some(payload) = call_site_payload {
            read_call_site_patterns(
                payload,
                self.layout.endian,
                self.layout.string_offset_size,
                address.saturating_sub(function.range.start),
                self,
                &mut call_site_patterns,
            )?;
        }
        Ok(Lookup::new(
            address,
            function.range,
            frames.into_boxed_slice(),
            call_site_patterns.into_boxed_slice(),
        ))
    }
}

struct InlineScan<'scratch> {
    string_offset_size: u8,
    address: u64,
    frames: &'scratch mut SmallVec<[RawInlineFrame; 4]>,
}

fn scan_inline_node(
    cursor: &mut Cursor<'_>,
    base: u64,
    collect: bool,
    depth: usize,
    scan: &mut InlineScan<'_>,
) -> Result<(bool, bool)> {
    check_inline_depth(depth)?;
    let count = read_uleb(cursor)?;
    if count == 0 {
        return Ok((false, false));
    }
    if count > cursor.remaining() as u64 / 2 {
        return Err(Error::InvalidFormat(
            "inline range count exceeds remaining payload",
        ));
    }
    let mut contains = false;
    let mut first_start = None;
    for _ in 0..count {
        let start = base
            .checked_add(read_uleb(cursor)?)
            .ok_or(Error::Overflow("inline range start"))?;
        let end = start
            .checked_add(read_uleb(cursor)?)
            .ok_or(Error::Overflow("inline range end"))?;
        first_start.get_or_insert(start);
        if collect {
            contains |= start <= scan.address && scan.address < end;
        }
    }
    let first_start = first_start.ok_or(Error::InvalidFormat(
        "inline node declares no address range",
    ))?;
    let has_children = cursor.read_u8()? != 0;
    let name = cursor.read_uint(scan.string_offset_size)?;
    let call_file = read_uleb(cursor)?;
    let call_line = read_uleb(cursor)?;
    let matched = collect && contains;
    let original_len = scan.frames.len();
    if matched && name != 0 {
        scan.frames.push(RawInlineFrame {
            name,
            call_file: FileIndex::new(u32::try_from(call_file).map_err(|_| Error::OutOfRange {
                field: "inline call-file index",
                value: call_file,
                max: u64::from(u32::MAX),
            })?),
            call_line: u32::try_from(call_line).map_err(|_| Error::OutOfRange {
                field: "inline call-line",
                value: call_line,
                max: u64::from(u32::MAX),
            })?,
            start: first_start,
        });
    }
    if has_children {
        let child_base = first_start;
        let mut found_child = false;
        loop {
            let (present, child_matched) = scan_inline_node(
                cursor,
                child_base,
                matched && !found_child,
                depth.saturating_add(1),
                scan,
            )?;
            if !present {
                break;
            }
            found_child |= child_matched;
        }
    }
    if !matched {
        scan.frames.truncate(original_len);
    }
    Ok((true, matched))
}

fn read_call_site_patterns<'data, D: AsRef<[u8]>>(
    payload: &[u8],
    endian: Endian,
    string_offset_size: u8,
    return_offset: u64,
    gsym: &'data Gsym<D>,
    output: &mut Vec<&'data [u8]>,
) -> Result<()> {
    let mut cursor = Cursor::new(payload, endian);
    let count = cursor.read_u32()?;
    for _ in 0..count {
        let candidate = cursor.read_u64()?;
        let _flags = cursor.read_u8()?;
        let regex_count = cursor.read_u32()?;
        for _ in 0..regex_count {
            let offset = cursor.read_uint(string_offset_size)?;
            if candidate == return_offset {
                output.push(gsym.string(offset)?);
            }
        }
    }
    if !cursor.is_empty() {
        return Err(Error::InvalidFormat("trailing call-site bytes"));
    }
    Ok(())
}