Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(cymbal): salvage source context when a native address misses the symcache (#67690)

Catalin Irimieaf1b6ddb82c3
diff --git a/rust/cymbal/src/core/symbolication/symbol_store/native.rs b/rust/cymbal/src/core/symbolication/symbol_store/native.rs
index e1b2362e84b3..3fc692d0b221 100644
--- a/rust/cymbal/src/core/symbolication/symbol_store/native.rs
+++ b/rust/cymbal/src/core/symbolication/symbol_store/native.rs
@@ -114,18 +114,21 @@ impl ParsedNativeSymbols {
/// The basename fallback matches by filename and is only applied when
/// there is exactly one candidate (no ambiguity).
pub fn get_source(&self, dwarf_path: &str) -> Option<&str> {
- let sources = self.sources.as_ref()?;
-
// 1. Exact match (fast path, works for physical-function frames)
- if let Some(text) = sources.get(dwarf_path) {
- return Some(text.as_str());
+ if let Some(text) = self.get_source_exact(dwarf_path) {
+ return Some(text);
}
// 2. Basename fallback for inlined cross-file frames where the embedding
// CU's line table records a relative path but the manifest was built
- // from the callee CU's absolute path (Swift WMO common case).
- let basename = std::path::Path::new(dwarf_path).file_name()?.to_str()?;
- let mut candidates = sources.iter().filter(|(k, _)| k.ends_with(basename));
+ // from the callee CU's absolute path (Swift WMO common case). Compares
+ // real file-name components, not string suffixes: "bar.c" must not
+ // match a bundled "/src/foobar.c".
+ let sources = self.sources.as_ref()?;
+ let basename = std::path::Path::new(dwarf_path).file_name()?;
+ let mut candidates = sources
+ .iter()
+ .filter(|(k, _)| std::path::Path::new(k).file_name() == Some(basename));
let first = candidates.next();
// Only use the fallback if there is exactly one candidate (no ambiguity).
if first.is_some() && candidates.next().is_none() {
@@ -135,6 +138,14 @@ impl ParsedNativeSymbols {
None
}
+ /// Exact manifest-key lookup only, with no basename fallback. Used where
+ /// the requested path comes from event input rather than the symcache
+ /// (the symbol-miss context salvage), so fuzzy matching can't attach
+ /// lines from a different file than the one named.
+ pub fn get_source_exact(&self, dwarf_path: &str) -> Option<&str> {
+ self.sources.as_ref()?.get(dwarf_path).map(|v| v.as_str())
+ }
+
fn extract_dwarf_from_zip(
archive: &mut ZipArchive<Cursor<Vec<u8>>>,
) -> Result<Vec<u8>, NativeError> {
@@ -346,3 +357,48 @@ impl Display for NativeRef {
write!(f, "NativeRef")
}
}
+
+#[cfg(test)]
+mod test {
+ use std::collections::HashMap;
+
+ use super::ParsedNativeSymbols;
+
+ fn symbols_with(sources: &[(&str, &str)]) -> ParsedNativeSymbols {
+ ParsedNativeSymbols {
+ symcache_data: Vec::new(),
+ sources: Some(
+ sources
+ .iter()
+ .map(|(k, v)| (k.to_string(), v.to_string()))
+ .collect::<HashMap<_, _>>(),
+ ),
+ decompressed_bytes: 0,
+ }
+ }
+
+ #[test]
+ fn source_lookup_matches_file_name_components_not_suffixes() {
+ let symbols = symbols_with(&[("/src/foobar.c", "foo"), ("/abs/path/Foo.swift", "swift")]);
+
+ // Exact key always wins.
+ assert_eq!(symbols.get_source("/src/foobar.c"), Some("foo"));
+ assert_eq!(symbols.get_source_exact("/src/foobar.c"), Some("foo"));
+
+ // The fallback compares real file-name components: a suffix of a
+ // different file's name must not match.
+ assert_eq!(symbols.get_source("bar.c"), None);
+ // A relative spelling of the same file still matches.
+ assert_eq!(symbols.get_source("sub/dir/Foo.swift"), Some("swift"));
+
+ // The exact-only lookup never falls back.
+ assert_eq!(symbols.get_source_exact("Foo.swift"), None);
+ assert_eq!(symbols.get_source_exact("bar.c"), None);
+ }
+
+ #[test]
+ fn source_fallback_requires_a_unique_candidate() {
+ let symbols = symbols_with(&[("/a/util.c", "a"), ("/b/util.c", "b")]);
+ assert_eq!(symbols.get_source("util.c"), None);
+ }
+}
diff --git a/rust/cymbal/src/core/types/langs/native.rs b/rust/cymbal/src/core/types/langs/native.rs
index a0aa39abfd87..2612a1fa95b4 100644
--- a/rust/cymbal/src/core/types/langs/native.rs
+++ b/rust/cymbal/src/core/types/langs/native.rs
@@ -244,7 +244,25 @@ impl RawNativeFrame {
let symbol_infos = symbols.lookup(lookup_addr)?;
if symbol_infos.is_empty() {
- return Err(NativeError::SymbolNotFound(lookup_addr).into());
+ // The symbol set exists but doesn't cover this address (e.g. a
+ // section outside the symcache). The frame falls back to its
+ // client-side fields — but the set's source bundle can still
+ // supply context for the client-reported file/line. Exact-match
+ // only: the path comes from event input here, so the fuzzy
+ // basename fallback could attach lines from the wrong file.
+ let mut frame = self.handle_resolution_error(NativeError::SymbolNotFound(lookup_addr));
+ if let (Some(filename), Some(lineno)) =
+ (self.filename.as_deref(), self.lineno.filter(|l| *l > 0))
+ {
+ if let Some(source_text) = symbols.get_source_exact(filename) {
+ frame.context = get_context_lines(
+ source_text.lines(),
+ (lineno - 1) as usize,
+ context_lines,
+ );
+ }
+ }
+ return Ok(vec![frame]);
}
// Build one resolved Frame per logical layer. The symcache returns
@@ -1382,4 +1400,71 @@ mod test {
);
}
}
+
+ /// When the uploaded set doesn't cover an address (symbol not found), the
+ /// frame keeps its client-side fields — and the set's source bundle still
+ /// supplies context for the client-reported file/line.
+ #[sqlx::test(migrations = "./tests/test_migrations")]
+ async fn test_symbol_not_found_salvages_context_from_source_bundle(db: sqlx::PgPool) {
+ use crate::frames::RawFrame;
+
+ const ELF: &[u8] = include_bytes!("../../../../tests/static/native/test_binary_inline");
+ const SOURCE: &str = include_str!("../../../../tests/static/native/test_binary_inline.c");
+ let chunk_id = "140ab543-c098-09dc-22b6-11f72e46d6fe";
+ let zip = zip_fixture(
+ ELF,
+ Some(("/cymbal_tests/native/test_binary_inline.c", SOURCE)),
+ );
+ let catalog = catalog_for_chunk(&db, chunk_id, zip).await;
+
+ let slide_base = 0x7f0000000000u64;
+ // 0x10 is inside the image range but outside any symcache-covered
+ // function (ELF header area), so the lookup finds no symbol.
+ let mut raw = native_frame_at(slide_base + 0x10, slide_base);
+ raw.client_resolved = true;
+ raw.function = Some("inlined_leaf".to_string());
+ raw.filename = Some("/cymbal_tests/native/test_binary_inline.c".to_string());
+ raw.lineno = Some(6);
+ let frame = RawFrame::Native(raw);
+ let debug_images = vec![debug_image_at(chunk_id, slide_base)];
+
+ let resolved = frame
+ .resolve(1, &catalog, &debug_images, 3)
+ .await
+ .unwrap()
+ .pop()
+ .unwrap();
+
+ assert!(!resolved.resolved);
+ assert!(resolved
+ .resolve_failure
+ .as_deref()
+ .is_some_and(|f| f.contains("Symbol not found")));
+ assert_eq!(resolved.resolved_name.as_deref(), Some("inlined_leaf"));
+
+ let context = resolved.context.expect("salvaged context from bundle");
+ assert_eq!(context.line.number, 6);
+ assert!(
+ context.line.line.contains("volatile int x = 99"),
+ "expected fixture line 6, got: {:?}",
+ context.line.line
+ );
+
+ // The salvage path is exact-match only: a bare basename (or any other
+ // fuzzy spelling) of a bundled path attaches nothing, since the
+ // filename here is event input rather than symcache output.
+ let mut fuzzy = native_frame_at(slide_base + 0x10, slide_base);
+ fuzzy.client_resolved = true;
+ fuzzy.function = Some("inlined_leaf".to_string());
+ fuzzy.filename = Some("test_binary_inline.c".to_string());
+ fuzzy.lineno = Some(6);
+ let resolved = RawFrame::Native(fuzzy)
+ .resolve(1, &catalog, &debug_images, 3)
+ .await
+ .unwrap()
+ .pop()
+ .unwrap();
+ assert!(!resolved.resolved);
+ assert!(resolved.context.is_none());
+ }
}