]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/backtrace.rs
Rollup merge of #60609 - spastorino:be-explicit-on-mem-replace-doc, r=Centril
[rust.git] / src / libstd / sys / sgx / backtrace.rs
1 use crate::io;
2 use crate::error::Error;
3 use crate::fmt;
4 use crate::sys_common::backtrace::Frame;
5 use crate::sys::sgx::abi::mem::image_base;
6
7 use unwind as uw;
8
9 pub struct BacktraceContext;
10
11 struct Context<'a> {
12     idx: usize,
13     frames: &'a mut [Frame],
14 }
15
16 #[derive(Debug)]
17 struct UnwindError(uw::_Unwind_Reason_Code);
18
19 impl Error for UnwindError {
20     fn description(&self) -> &'static str {
21         "unexpected return value while unwinding"
22     }
23 }
24
25 impl fmt::Display for UnwindError {
26     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27         write!(f, "{}: {:?}", self.description(), self.0)
28     }
29 }
30
31 #[inline(never)] // this function call can be skipped it when tracing.
32 pub fn unwind_backtrace(frames: &mut [Frame]) -> io::Result<(usize, BacktraceContext)> {
33     let mut cx = Context { idx: 0, frames };
34     let result_unwind = unsafe {
35         uw::_Unwind_Backtrace(trace_fn, &mut cx as *mut Context<'_> as *mut libc::c_void)
36     };
37     // See libunwind:src/unwind/Backtrace.c for the return values.
38     // No, there is no doc.
39     let res = match result_unwind {
40         // These return codes seem to be benign and need to be ignored for backtraces
41         // to show up properly on all tested platforms.
42         uw::_URC_END_OF_STACK | uw::_URC_FATAL_PHASE1_ERROR | uw::_URC_FAILURE => {
43             Ok((cx.idx, BacktraceContext))
44         }
45         _ => Err(io::Error::new(
46             io::ErrorKind::Other,
47             UnwindError(result_unwind),
48         )),
49     };
50     res
51 }
52
53 extern "C" fn trace_fn(
54     ctx: *mut uw::_Unwind_Context,
55     arg: *mut libc::c_void,
56 ) -> uw::_Unwind_Reason_Code {
57     let cx = unsafe { &mut *(arg as *mut Context<'_>) };
58     if cx.idx >= cx.frames.len() {
59         return uw::_URC_NORMAL_STOP;
60     }
61
62     let mut ip_before_insn = 0;
63     let mut ip = unsafe { uw::_Unwind_GetIPInfo(ctx, &mut ip_before_insn) as *mut libc::c_void };
64     if !ip.is_null() && ip_before_insn == 0 {
65         // this is a non-signaling frame, so `ip` refers to the address
66         // after the calling instruction. account for that.
67         ip = (ip as usize - 1) as *mut _;
68     }
69
70     let symaddr = unsafe { uw::_Unwind_FindEnclosingFunction(ip) };
71     cx.frames[cx.idx] = Frame {
72         symbol_addr: symaddr as *mut u8,
73         exact_position: ip as *mut u8,
74         inline_context: 0,
75     };
76     cx.idx += 1;
77
78     uw::_URC_NO_REASON
79 }
80
81 // To reduce TCB size in Sgx enclave, we do not want to implement resolve_symname functionality.
82 // Rather, we print the offset of the address here, which could be later mapped to correct function.
83 pub fn resolve_symname<F>(frame: Frame,
84                           callback: F,
85                           _: &BacktraceContext) -> io::Result<()>
86     where F: FnOnce(Option<&str>) -> io::Result<()>
87 {
88     callback(Some(&format!("0x{:x}",
89             (frame.symbol_addr.wrapping_offset_from(image_base() as _)))))
90 }
91
92 pub fn foreach_symbol_fileline<F>(_: Frame,
93                                   _: F,
94                                   _: &BacktraceContext) -> io::Result<bool>
95     where F: FnMut(&[u8], u32) -> io::Result<()>
96 {
97     Ok(false)
98 }