]> git.proxmox.com Git - rustc.git/blob - vendor/addr2line-0.18.0/tests/correctness.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / vendor / addr2line-0.18.0 / tests / correctness.rs
1 extern crate addr2line;
2 extern crate fallible_iterator;
3 extern crate findshlibs;
4 extern crate gimli;
5 extern crate memmap2;
6 extern crate object;
7
8 use addr2line::Context;
9 use fallible_iterator::FallibleIterator;
10 use findshlibs::{IterationControl, SharedLibrary, TargetSharedLibrary};
11 use object::Object;
12 use std::fs::File;
13
14 fn find_debuginfo() -> memmap2::Mmap {
15 let path = std::env::current_exe().unwrap();
16 let file = File::open(&path).unwrap();
17 let map = unsafe { memmap2::Mmap::map(&file).unwrap() };
18 let file = &object::File::parse(&*map).unwrap();
19 if let Ok(uuid) = file.mach_uuid() {
20 for candidate in path.parent().unwrap().read_dir().unwrap() {
21 let path = candidate.unwrap().path();
22 if !path.to_str().unwrap().ends_with(".dSYM") {
23 continue;
24 }
25 for candidate in path.join("Contents/Resources/DWARF").read_dir().unwrap() {
26 let path = candidate.unwrap().path();
27 let file = File::open(&path).unwrap();
28 let map = unsafe { memmap2::Mmap::map(&file).unwrap() };
29 let file = &object::File::parse(&*map).unwrap();
30 if file.mach_uuid().unwrap() == uuid {
31 return map;
32 }
33 }
34 }
35 }
36
37 return map;
38 }
39
40 #[test]
41 fn correctness() {
42 let map = find_debuginfo();
43 let file = &object::File::parse(&*map).unwrap();
44 let module_base = file.relative_address_base();
45 let ctx = Context::new(file).unwrap();
46
47 let mut bias = None;
48 TargetSharedLibrary::each(|lib| {
49 bias = Some((lib.virtual_memory_bias().0 as u64).wrapping_sub(module_base));
50 IterationControl::Break
51 });
52
53 let test = |sym: u64, expected_prefix: &str| {
54 let ip = sym.wrapping_sub(bias.unwrap());
55
56 let frames = ctx.find_frames(ip).unwrap();
57 let frame = frames.last().unwrap().unwrap();
58 let name = frame.function.as_ref().unwrap().demangle().unwrap();
59 // Old rust versions generate DWARF with wrong linkage name,
60 // so only check the start.
61 if !name.starts_with(expected_prefix) {
62 panic!("incorrect name '{}', expected {:?}", name, expected_prefix);
63 }
64 };
65
66 test(test_function as u64, "correctness::test_function");
67 test(
68 small::test_function as u64,
69 "correctness::small::test_function",
70 );
71 test(auxiliary::foo as u64, "auxiliary::foo");
72 }
73
74 mod small {
75 pub fn test_function() {
76 println!("y");
77 }
78 }
79
80 fn test_function() {
81 println!("x");
82 }
83
84 #[test]
85 fn zero_function() {
86 let map = find_debuginfo();
87 let file = &object::File::parse(&*map).unwrap();
88 let ctx = Context::new(file).unwrap();
89 for probe in 0..10 {
90 assert!(ctx.find_frames(probe).unwrap().count().unwrap() < 10);
91 }
92 }