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};
34 use result::Result::{Ok, Err};
36 use str::{self, StrExt};
37 use sync::{StaticMutex, MUTEX_INIT};
39 use sys_common::backtrace::*;
41 #[allow(non_snake_case)]
43 fn GetCurrentProcess() -> libc::HANDLE;
44 fn GetCurrentThread() -> libc::HANDLE;
45 fn RtlCaptureContext(ctx: *mut arch::CONTEXT);
49 extern "system" fn(libc::HANDLE, u64, *mut u64,
50 *mut SYMBOL_INFO) -> libc::BOOL;
51 type SymInitializeFn =
52 extern "system" fn(libc::HANDLE, *mut libc::c_void,
53 libc::BOOL) -> libc::BOOL;
55 extern "system" fn(libc::HANDLE) -> libc::BOOL;
58 extern "system" fn(libc::DWORD, libc::HANDLE, libc::HANDLE,
59 *mut STACKFRAME64, *mut arch::CONTEXT,
60 *mut libc::c_void, *mut libc::c_void,
61 *mut libc::c_void, *mut libc::c_void) -> libc::BOOL;
63 const MAX_SYM_NAME: uint = 2000;
64 const IMAGE_FILE_MACHINE_I386: libc::DWORD = 0x014c;
65 const IMAGE_FILE_MACHINE_IA64: libc::DWORD = 0x0200;
66 const IMAGE_FILE_MACHINE_AMD64: libc::DWORD = 0x8664;
70 SizeOfStruct: libc::c_ulong,
71 TypeIndex: libc::c_ulong,
79 Register: libc::c_ulong,
82 NameLen: libc::c_ulong,
83 MaxNameLen: libc::c_ulong,
84 // note that windows has this as 1, but it basically just means that
85 // the name is inline at the end of the struct. For us, we just bump
86 // the struct size up to MAX_SYM_NAME.
87 Name: [libc::c_char; MAX_SYM_NAME],
105 struct STACKFRAME64 {
107 AddrReturn: ADDRESS64,
108 AddrFrame: ADDRESS64,
109 AddrStack: ADDRESS64,
110 AddrBStore: ADDRESS64,
111 FuncTableEntry: *mut libc::c_void,
121 ThCallbackStack: libc::DWORD,
122 ThCallbackBStore: libc::DWORD,
123 NextCallback: libc::DWORD,
124 FramePointer: libc::DWORD,
126 KeUserCallbackDispatcher: u64,
127 SystemRangeStart: u64,
128 KiUserExceptionDispatcher: u64,
134 #[cfg(target_arch = "x86")]
138 const MAXIMUM_SUPPORTED_EXTENSION: uint = 512;
142 ContextFlags: libc::DWORD,
149 FloatSave: FLOATING_SAVE_AREA,
166 ExtendedRegisters: [u8; MAXIMUM_SUPPORTED_EXTENSION],
170 pub struct FLOATING_SAVE_AREA {
171 ControlWord: libc::DWORD,
172 StatusWord: libc::DWORD,
173 TagWord: libc::DWORD,
174 ErrorOffset: libc::DWORD,
175 ErrorSelector: libc::DWORD,
176 DataOffset: libc::DWORD,
177 DataSelector: libc::DWORD,
178 RegisterArea: [u8; 80],
179 Cr0NpxState: libc::DWORD,
182 pub fn init_frame(frame: &mut super::STACKFRAME64,
183 ctx: &CONTEXT) -> libc::DWORD {
184 frame.AddrPC.Offset = ctx.Eip as u64;
185 frame.AddrPC.Mode = super::ADDRESS_MODE::AddrModeFlat;
186 frame.AddrStack.Offset = ctx.Esp as u64;
187 frame.AddrStack.Mode = super::ADDRESS_MODE::AddrModeFlat;
188 frame.AddrFrame.Offset = ctx.Ebp as u64;
189 frame.AddrFrame.Mode = super::ADDRESS_MODE::AddrModeFlat;
190 super::IMAGE_FILE_MACHINE_I386
194 #[cfg(target_arch = "x86_64")]
196 use libc::{c_longlong, c_ulonglong};
197 use libc::types::os::arch::extra::{WORD, DWORD, DWORDLONG};
202 _align_hack: [simd::u64x2; 0], // FIXME align on 16-byte
247 FltSave: FLOATING_SAVE_AREA,
249 VectorRegister: [M128A; 26],
250 VectorControl: DWORDLONG,
252 DebugControl: DWORDLONG,
253 LastBranchToRip: DWORDLONG,
254 LastBranchFromRip: DWORDLONG,
255 LastExceptionToRip: DWORDLONG,
256 LastExceptionFromRip: DWORDLONG,
261 _align_hack: [simd::u64x2; 0], // FIXME align on 16-byte
267 pub struct FLOATING_SAVE_AREA {
268 _align_hack: [simd::u64x2; 0], // FIXME align on 16-byte
269 _Dummy: [u8; 512] // FIXME: Fill this out
272 pub fn init_frame(frame: &mut super::STACKFRAME64,
273 ctx: &CONTEXT) -> DWORD {
274 frame.AddrPC.Offset = ctx.Rip as u64;
275 frame.AddrPC.Mode = super::ADDRESS_MODE::AddrModeFlat;
276 frame.AddrStack.Offset = ctx.Rsp as u64;
277 frame.AddrStack.Mode = super::ADDRESS_MODE::AddrModeFlat;
278 frame.AddrFrame.Offset = ctx.Rbp as u64;
279 frame.AddrFrame.Mode = super::ADDRESS_MODE::AddrModeFlat;
280 super::IMAGE_FILE_MACHINE_AMD64
286 handle: libc::HANDLE,
287 SymCleanup: SymCleanupFn,
290 impl Drop for Cleanup {
291 fn drop(&mut self) { (self.SymCleanup)(self.handle); }
294 pub fn write(w: &mut Writer) -> IoResult<()> {
295 // According to windows documentation, all dbghelp functions are
297 static LOCK: StaticMutex = MUTEX_INIT;
298 let _g = unsafe { LOCK.lock() };
300 // Open up dbghelp.dll, we don't link to it explicitly because it can't
301 // always be found. Additionally, it's nice having fewer dependencies.
302 let path = Path::new("dbghelp.dll");
303 let lib = match DynamicLibrary::open(Some(&path)) {
305 Err(..) => return Ok(()),
308 macro_rules! sym{ ($e:expr, $t:ident) => (unsafe {
309 match lib.symbol($e) {
310 Ok(f) => mem::transmute::<*mut u8, $t>(f),
311 Err(..) => return Ok(())
315 // Fetch the symbols necessary from dbghelp.dll
316 let SymFromAddr = sym!("SymFromAddr", SymFromAddrFn);
317 let SymInitialize = sym!("SymInitialize", SymInitializeFn);
318 let SymCleanup = sym!("SymCleanup", SymCleanupFn);
319 let StackWalk64 = sym!("StackWalk64", StackWalk64Fn);
321 // Allocate necessary structures for doing the stack walk
322 let process = unsafe { GetCurrentProcess() };
323 let thread = unsafe { GetCurrentThread() };
324 let mut context: arch::CONTEXT = unsafe { intrinsics::init() };
325 unsafe { RtlCaptureContext(&mut context); }
326 let mut frame: STACKFRAME64 = unsafe { intrinsics::init() };
327 let image = arch::init_frame(&mut frame, &context);
329 // Initialize this process's symbols
330 let ret = SymInitialize(process, 0 as *mut libc::c_void, libc::TRUE);
331 if ret != libc::TRUE { return Ok(()) }
332 let _c = Cleanup { handle: process, SymCleanup: SymCleanup };
334 // And now that we're done with all the setup, do the stack walking!
336 try!(write!(w, "stack backtrace:\n"));
337 while StackWalk64(image, process, thread, &mut frame, &mut context,
338 0 as *mut libc::c_void,
339 0 as *mut libc::c_void,
340 0 as *mut libc::c_void,
341 0 as *mut libc::c_void) == libc::TRUE{
342 let addr = frame.AddrPC.Offset;
343 if addr == frame.AddrReturn.Offset || addr == 0 ||
344 frame.AddrReturn.Offset == 0 { break }
347 try!(write!(w, " {:2}: {:#2$x}", i, addr, HEX_WIDTH));
348 let mut info: SYMBOL_INFO = unsafe { intrinsics::init() };
349 info.MaxNameLen = MAX_SYM_NAME as libc::c_ulong;
350 // the struct size in C. the value is different to
351 // `size_of::<SYMBOL_INFO>() - MAX_SYM_NAME + 1` (== 81)
352 // due to struct alignment.
353 info.SizeOfStruct = 88;
355 let mut displacement = 0u64;
356 let ret = SymFromAddr(process, addr as u64, &mut displacement,
359 if ret == libc::TRUE {
360 try!(write!(w, " - "));
361 let ptr = info.Name.as_ptr() as *const libc::c_char;
362 let bytes = unsafe { ffi::c_str_to_bytes(&ptr) };
363 match str::from_utf8(bytes) {
364 Ok(s) => try!(demangle(w, s)),
365 Err(..) => try!(w.write(&bytes[..(bytes.len()-1)])),
368 try!(w.write(&['\n' as u8]));