1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 /// As always, windows has something very different than unix, we mainly want
11 /// to avoid having to depend too much on libunwind for windows.
13 /// If you google around, you'll find a fair bit of references to built-in
14 /// functions to get backtraces on windows. It turns out that most of these are
15 /// in an external library called dbghelp. I was unable to find this library
16 /// via `-ldbghelp`, but it is apparently normal to do the `dlopen` equivalent
19 /// You'll also find that there's a function called CaptureStackBackTrace
20 /// mentioned frequently (which is also easy to use), but sadly I didn't have a
21 /// copy of that function in my mingw install (maybe it was broken?). Instead,
22 /// this takes the route of using StackWalk64 in order to walk the stack.
24 use dynamic_lib::DynamicLibrary;
28 use io::{IoResult, Writer};
32 use option::Option::{Some, None};
35 use result::Result::{Ok, Err};
37 use str::{self, StrExt};
38 use sync::{StaticMutex, MUTEX_INIT};
40 use sys_common::backtrace::*;
42 #[allow(non_snake_case)]
44 fn GetCurrentProcess() -> libc::HANDLE;
45 fn GetCurrentThread() -> libc::HANDLE;
46 fn RtlCaptureContext(ctx: *mut arch::CONTEXT);
50 extern "system" fn(libc::HANDLE, u64, *mut u64,
51 *mut SYMBOL_INFO) -> libc::BOOL;
52 type SymInitializeFn =
53 extern "system" fn(libc::HANDLE, *mut libc::c_void,
54 libc::BOOL) -> libc::BOOL;
56 extern "system" fn(libc::HANDLE) -> libc::BOOL;
59 extern "system" fn(libc::DWORD, libc::HANDLE, libc::HANDLE,
60 *mut STACKFRAME64, *mut arch::CONTEXT,
61 *mut libc::c_void, *mut libc::c_void,
62 *mut libc::c_void, *mut libc::c_void) -> libc::BOOL;
64 const MAX_SYM_NAME: uint = 2000;
65 const IMAGE_FILE_MACHINE_I386: libc::DWORD = 0x014c;
66 const IMAGE_FILE_MACHINE_IA64: libc::DWORD = 0x0200;
67 const IMAGE_FILE_MACHINE_AMD64: libc::DWORD = 0x8664;
71 SizeOfStruct: libc::c_ulong,
72 TypeIndex: libc::c_ulong,
80 Register: libc::c_ulong,
83 NameLen: libc::c_ulong,
84 MaxNameLen: libc::c_ulong,
85 // note that windows has this as 1, but it basically just means that
86 // the name is inline at the end of the struct. For us, we just bump
87 // the struct size up to MAX_SYM_NAME.
88 Name: [libc::c_char; MAX_SYM_NAME],
106 struct STACKFRAME64 {
108 AddrReturn: ADDRESS64,
109 AddrFrame: ADDRESS64,
110 AddrStack: ADDRESS64,
111 AddrBStore: ADDRESS64,
112 FuncTableEntry: *mut libc::c_void,
122 ThCallbackStack: libc::DWORD,
123 ThCallbackBStore: libc::DWORD,
124 NextCallback: libc::DWORD,
125 FramePointer: libc::DWORD,
127 KeUserCallbackDispatcher: u64,
128 SystemRangeStart: u64,
129 KiUserExceptionDispatcher: u64,
135 #[cfg(target_arch = "x86")]
139 const MAXIMUM_SUPPORTED_EXTENSION: uint = 512;
143 ContextFlags: libc::DWORD,
150 FloatSave: FLOATING_SAVE_AREA,
167 ExtendedRegisters: [u8; MAXIMUM_SUPPORTED_EXTENSION],
171 pub struct FLOATING_SAVE_AREA {
172 ControlWord: libc::DWORD,
173 StatusWord: libc::DWORD,
174 TagWord: libc::DWORD,
175 ErrorOffset: libc::DWORD,
176 ErrorSelector: libc::DWORD,
177 DataOffset: libc::DWORD,
178 DataSelector: libc::DWORD,
179 RegisterArea: [u8; 80],
180 Cr0NpxState: libc::DWORD,
183 pub fn init_frame(frame: &mut super::STACKFRAME64,
184 ctx: &CONTEXT) -> libc::DWORD {
185 frame.AddrPC.Offset = ctx.Eip as u64;
186 frame.AddrPC.Mode = super::ADDRESS_MODE::AddrModeFlat;
187 frame.AddrStack.Offset = ctx.Esp as u64;
188 frame.AddrStack.Mode = super::ADDRESS_MODE::AddrModeFlat;
189 frame.AddrFrame.Offset = ctx.Ebp as u64;
190 frame.AddrFrame.Mode = super::ADDRESS_MODE::AddrModeFlat;
191 super::IMAGE_FILE_MACHINE_I386
195 #[cfg(target_arch = "x86_64")]
197 use libc::{c_longlong, c_ulonglong};
198 use libc::types::os::arch::extra::{WORD, DWORD, DWORDLONG};
203 _align_hack: [simd::u64x2; 0], // FIXME align on 16-byte
248 FltSave: FLOATING_SAVE_AREA,
250 VectorRegister: [M128A; 26],
251 VectorControl: DWORDLONG,
253 DebugControl: DWORDLONG,
254 LastBranchToRip: DWORDLONG,
255 LastBranchFromRip: DWORDLONG,
256 LastExceptionToRip: DWORDLONG,
257 LastExceptionFromRip: DWORDLONG,
262 _align_hack: [simd::u64x2; 0], // FIXME align on 16-byte
268 pub struct FLOATING_SAVE_AREA {
269 _align_hack: [simd::u64x2; 0], // FIXME align on 16-byte
270 _Dummy: [u8; 512] // FIXME: Fill this out
273 pub fn init_frame(frame: &mut super::STACKFRAME64,
274 ctx: &CONTEXT) -> DWORD {
275 frame.AddrPC.Offset = ctx.Rip as u64;
276 frame.AddrPC.Mode = super::ADDRESS_MODE::AddrModeFlat;
277 frame.AddrStack.Offset = ctx.Rsp as u64;
278 frame.AddrStack.Mode = super::ADDRESS_MODE::AddrModeFlat;
279 frame.AddrFrame.Offset = ctx.Rbp as u64;
280 frame.AddrFrame.Mode = super::ADDRESS_MODE::AddrModeFlat;
281 super::IMAGE_FILE_MACHINE_AMD64
287 handle: libc::HANDLE,
288 SymCleanup: SymCleanupFn,
291 impl Drop for Cleanup {
292 fn drop(&mut self) { (self.SymCleanup)(self.handle); }
295 pub fn write(w: &mut Writer) -> IoResult<()> {
296 // According to windows documentation, all dbghelp functions are
298 static LOCK: StaticMutex = MUTEX_INIT;
299 let _g = unsafe { LOCK.lock() };
301 // Open up dbghelp.dll, we don't link to it explicitly because it can't
302 // always be found. Additionally, it's nice having fewer dependencies.
303 let path = Path::new("dbghelp.dll");
304 let lib = match DynamicLibrary::open(Some(&path)) {
306 Err(..) => return Ok(()),
309 macro_rules! sym{ ($e:expr, $t:ident) => (unsafe {
310 match lib.symbol($e) {
311 Ok(f) => mem::transmute::<*mut u8, $t>(f),
312 Err(..) => return Ok(())
316 // Fetch the symbols necessary from dbghelp.dll
317 let SymFromAddr = sym!("SymFromAddr", SymFromAddrFn);
318 let SymInitialize = sym!("SymInitialize", SymInitializeFn);
319 let SymCleanup = sym!("SymCleanup", SymCleanupFn);
320 let StackWalk64 = sym!("StackWalk64", StackWalk64Fn);
322 // Allocate necessary structures for doing the stack walk
323 let process = unsafe { GetCurrentProcess() };
324 let thread = unsafe { GetCurrentThread() };
325 let mut context: arch::CONTEXT = unsafe { intrinsics::init() };
326 unsafe { RtlCaptureContext(&mut context); }
327 let mut frame: STACKFRAME64 = unsafe { intrinsics::init() };
328 let image = arch::init_frame(&mut frame, &context);
330 // Initialize this process's symbols
331 let ret = SymInitialize(process, ptr::null_mut(), libc::TRUE);
332 if ret != libc::TRUE { return Ok(()) }
333 let _c = Cleanup { handle: process, SymCleanup: SymCleanup };
335 // And now that we're done with all the setup, do the stack walking!
337 try!(write!(w, "stack backtrace:\n"));
338 while StackWalk64(image, process, thread, &mut frame, &mut context,
342 ptr::null_mut()) == libc::TRUE{
343 let addr = frame.AddrPC.Offset;
344 if addr == frame.AddrReturn.Offset || addr == 0 ||
345 frame.AddrReturn.Offset == 0 { break }
348 try!(write!(w, " {:2}: {:#2$x}", i, addr, HEX_WIDTH));
349 let mut info: SYMBOL_INFO = unsafe { intrinsics::init() };
350 info.MaxNameLen = MAX_SYM_NAME as libc::c_ulong;
351 // the struct size in C. the value is different to
352 // `size_of::<SYMBOL_INFO>() - MAX_SYM_NAME + 1` (== 81)
353 // due to struct alignment.
354 info.SizeOfStruct = 88;
356 let mut displacement = 0u64;
357 let ret = SymFromAddr(process, addr as u64, &mut displacement,
360 if ret == libc::TRUE {
361 try!(write!(w, " - "));
362 let ptr = info.Name.as_ptr() as *const libc::c_char;
363 let bytes = unsafe { ffi::c_str_to_bytes(&ptr) };
364 match str::from_utf8(bytes) {
365 Ok(s) => try!(demangle(w, s)),
366 Err(..) => try!(w.write(&bytes[..bytes.len()-1])),
369 try!(w.write(&['\n' as u8]));