Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make blaze_sym::path member optional #293

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/blazesym.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ typedef struct blaze_sym {
size_t offset;
/**
* The path of the source file defining the symbol.
*
* This attribute is optional and may be NULL.
*/
const char *path;
/**
Expand Down
8 changes: 7 additions & 1 deletion src/c_api/symbolize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ pub struct blaze_sym {
/// applied).
pub offset: usize,
/// The path of the source file defining the symbol.
///
/// This attribute is optional and may be NULL.
pub path: *const c_char,
/// The line number on which the symbol is located in the source
/// code.
Expand Down Expand Up @@ -369,7 +371,11 @@ unsafe fn convert_symbolizedresults_to_c(results: Vec<Vec<Sym>>) -> *const blaze

for r in entry {
let name_ptr = make_cstr(OsStr::new(&r.name));
let path_ptr = make_cstr(r.path.as_ref().map(|p| p.as_os_str()).unwrap_or_default());
let path_ptr = r
.path
.as_ref()
.map(|p| make_cstr(p.as_os_str()))
.unwrap_or_else(ptr::null_mut);

let csym_ref = unsafe { &mut *csym_last };
csym_ref.name = name_ptr;
Expand Down
92 changes: 62 additions & 30 deletions tests/blazesym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,37 +104,14 @@ fn symbolize_gsym() {
test(src);
}

/// Check that we can symbolize an address using DWARF.
/// Check that we can symbolize an address using ELF or DWARF.
#[test]
fn symbolize_dwarf() {
let test_dwarf = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("data")
.join("test-stable-addresses-dwarf-only.bin");
let src = symbolize::Source::Elf(symbolize::Elf::new(&test_dwarf));
let symbolizer = Symbolizer::new();
let results = symbolizer
.symbolize(&src, &[0x2000100])
.unwrap()
.into_iter()
.flatten()
.collect::<Vec<_>>();
assert_eq!(results.len(), 1);

let result = results.first().unwrap();
assert_eq!(result.name, "factorial");
assert_eq!(result.addr, 0x2000100);
assert_eq!(result.offset, 0);
assert_eq!(result.line, Some(8));

// Inquire symbol size.
let size = find_function_size("factorial", &test_dwarf);
assert_ne!(size, 0);

// Now check that we can symbolize addresses at a positive offset from the
// start of the function.
for offset in 1..size {
fn symbolize_elf_dwarf() {
fn test(path: &Path, dwarf: bool) {
let src = symbolize::Source::Elf(symbolize::Elf::new(path));
let symbolizer = Symbolizer::new();
let results = symbolizer
.symbolize(&src, &[0x2000100 + offset])
.symbolize(&src, &[0x2000100])
.unwrap()
.into_iter()
.flatten()
Expand All @@ -144,8 +121,63 @@ fn symbolize_dwarf() {
let result = results.first().unwrap();
assert_eq!(result.name, "factorial");
assert_eq!(result.addr, 0x2000100);
assert_eq!(result.offset, offset);
assert_eq!(result.offset, 0);

if dwarf {
assert!(result
.path
.as_ref()
.unwrap()
.ends_with("test-stable-addresses.c"));
assert_eq!(result.line, Some(8));
} else {
assert_eq!(result.path, None);
assert_eq!(result.line, None);
}

// Inquire symbol size.
let size = find_function_size("factorial", path);
assert_ne!(size, 0);

// Now check that we can symbolize addresses at a positive offset from the
// start of the function.
for offset in 1..size {
let results = symbolizer
.symbolize(&src, &[0x2000100 + offset])
.unwrap()
.into_iter()
.flatten()
.collect::<Vec<_>>();
assert_eq!(results.len(), 1);

let result = results.first().unwrap();
assert_eq!(result.name, "factorial");
assert_eq!(result.addr, 0x2000100);
assert_eq!(result.offset, offset);

if dwarf {
assert!(result
.path
.as_ref()
.unwrap()
.ends_with("test-stable-addresses.c"));
assert!(result.line.is_some());
} else {
assert_eq!(result.path, None);
assert_eq!(result.line, None);
}
}
}

let path = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("data")
.join("test-stable-addresses-no-dwarf.bin");
test(&path, false);

let path = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("data")
.join("test-stable-addresses-dwarf-only.bin");
test(&path, true);
}

/// Check that we can symbolize the `abort_creds` function inside a
Expand Down
80 changes: 52 additions & 28 deletions tests/c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,61 @@ fn symbolizer_creation_with_opts() {

/// Make sure that we can symbolize an address in an ELF file.
#[test]
fn symbolize_from_elf() {
let test_dwarf = Path::new(&env!("CARGO_MANIFEST_DIR"))
fn symbolize_elf_dwarf() {
fn test(path: &CStr, dwarf: bool) {
let elf_src = blaze_symbolize_src_elf {
path: path.as_ptr(),
};

let symbolizer = blaze_symbolizer_new();
let addrs = [0x2000100];
let result =
unsafe { blaze_symbolize_elf(symbolizer, &elf_src, addrs.as_ptr(), addrs.len()) };

assert!(!result.is_null());

let result = unsafe { &*result };
assert_eq!(result.size, 1);
let entries = unsafe { slice::from_raw_parts(result.entries.as_ptr(), result.size) };
let entry = &entries[0];
assert_eq!(entry.size, 1);

let syms = unsafe { slice::from_raw_parts(entry.syms, entry.size) };
let sym = &syms[0];
assert_eq!(
unsafe { CStr::from_ptr(sym.name) },
CStr::from_bytes_with_nul(b"factorial\0").unwrap()
);
assert_eq!(sym.addr, 0x2000100);
assert_eq!(sym.offset, 0);

if dwarf {
assert!(!sym.path.is_null());
assert!(unsafe { CStr::from_ptr(sym.path) }
.to_str()
.unwrap()
.ends_with("test-stable-addresses.c"));
assert_eq!(sym.line, 8);
} else {
assert!(sym.path.is_null());
assert_eq!(sym.line, 0);
}

let () = unsafe { blaze_result_free(result) };
let () = unsafe { blaze_symbolizer_free(symbolizer) };
}

let path = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("data")
.join("test-stable-addresses-no-dwarf.bin");
let test_dwarf_c = CString::new(test_dwarf.to_str().unwrap()).unwrap();

let elf_src = blaze_symbolize_src_elf {
path: test_dwarf_c.as_ptr(),
};
let path_c = CString::new(path.to_str().unwrap()).unwrap();
test(&path_c, false);

let symbolizer = blaze_symbolizer_new();
let addrs = [0x2000100];
let result = unsafe { blaze_symbolize_elf(symbolizer, &elf_src, addrs.as_ptr(), addrs.len()) };

assert!(!result.is_null());

let result = unsafe { &*result };
assert_eq!(result.size, 1);
let entries = unsafe { slice::from_raw_parts(result.entries.as_ptr(), result.size) };
let entry = &entries[0];
assert_eq!(entry.size, 1);

let syms = unsafe { slice::from_raw_parts(entry.syms, entry.size) };
let sym = &syms[0];
assert_eq!(
unsafe { CStr::from_ptr(sym.name) },
CStr::from_bytes_with_nul(b"factorial\0").unwrap()
);

let () = unsafe { blaze_result_free(result) };
let () = unsafe { blaze_symbolizer_free(symbolizer) };
let path = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("data")
.join("test-stable-addresses-dwarf-only.bin");
let path_c = CString::new(path.to_str().unwrap()).unwrap();
test(&path_c, true);
}


Expand Down