linker_trace/
lib.rs

1//! This crate defines a format for storing debug traces associated with particular addresses in the
2//! linker output.
3
4use anyhow::Context;
5use anyhow::Result;
6use serde::Deserialize;
7use serde::Serialize;
8use std::io::Write;
9use std::ops::Range;
10use std::path::Path;
11use std::path::PathBuf;
12
13#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Default)]
14pub struct TraceData {
15    pub traces: Vec<AddressTrace>,
16}
17
18#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
19pub struct AddressTrace {
20    pub address: u64,
21    pub messages: Vec<String>,
22}
23
24#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
25pub struct Section {
26    pub mem_range: Range<u64>,
27}
28
29impl TraceData {
30    pub fn write(&self, writer: &mut impl Write) -> Result<()> {
31        let r = postcard::to_io(self, writer);
32        let r: Result<()> = r.map(|_| ()).map_err(|e| e.into());
33        r?;
34        Ok(())
35    }
36
37    pub fn to_bytes(&self) -> Result<Vec<u8>> {
38        Ok(postcard::to_stdvec(self)?)
39    }
40
41    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
42        postcard::from_bytes(bytes).context("Invalid linker trace")
43    }
44}
45
46#[must_use]
47pub fn trace_path(base_path: &Path) -> PathBuf {
48    let mut new_extension = base_path.extension().unwrap_or_default().to_owned();
49    new_extension.push(".trace");
50    base_path.with_extension(new_extension)
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_round_trip() {
59        let layout = TraceData {
60            traces: vec![AddressTrace {
61                address: 100,
62                messages: vec!["Test".to_owned()],
63            }],
64        };
65        let bytes = layout.to_bytes().unwrap();
66        let layout2 = TraceData::from_bytes(&bytes).unwrap();
67        assert_eq!(layout, layout2);
68    }
69}