]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/windows/printing/msvc.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / libstd / sys / windows / printing / msvc.rs
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.
4 //
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
11 use ffi::CStr;
12 use io::prelude::*;
13 use io;
14 use libc::{c_ulong, c_int, c_char, c_void};
15 use mem;
16 use sys::c;
17 use sys::dynamic_lib::DynamicLibrary;
18 use sys_common::backtrace::{output, output_fileline};
19
20 type SymFromAddrFn =
21 unsafe extern "system" fn(c::HANDLE, u64, *mut u64,
22 *mut c::SYMBOL_INFO) -> c::BOOL;
23 type SymGetLineFromAddr64Fn =
24 unsafe extern "system" fn(c::HANDLE, u64, *mut u32,
25 *mut c::IMAGEHLP_LINE64) -> c::BOOL;
26
27 pub fn print(w: &mut Write,
28 i: isize,
29 addr: u64,
30 process: c::HANDLE,
31 dbghelp: &DynamicLibrary)
32 -> io::Result<()> {
33 unsafe {
34 let SymFromAddr = sym!(dbghelp, "SymFromAddr", SymFromAddrFn);
35 let SymGetLineFromAddr64 = sym!(dbghelp,
36 "SymGetLineFromAddr64",
37 SymGetLineFromAddr64Fn);
38
39 let mut info: c::SYMBOL_INFO = mem::zeroed();
40 info.MaxNameLen = c::MAX_SYM_NAME as c_ulong;
41 // the struct size in C. the value is different to
42 // `size_of::<SYMBOL_INFO>() - MAX_SYM_NAME + 1` (== 81)
43 // due to struct alignment.
44 info.SizeOfStruct = 88;
45
46 let mut displacement = 0u64;
47 let ret = SymFromAddr(process, addr, &mut displacement, &mut info);
48
49 let name = if ret == c::TRUE {
50 let ptr = info.Name.as_ptr() as *const c_char;
51 Some(CStr::from_ptr(ptr).to_bytes())
52 } else {
53 None
54 };
55
56 output(w, i, addr as usize as *mut c_void, name)?;
57
58 // Now find out the filename and line number
59 let mut line: c::IMAGEHLP_LINE64 = mem::zeroed();
60 line.SizeOfStruct = ::mem::size_of::<c::IMAGEHLP_LINE64>() as u32;
61
62 let mut displacement = 0u32;
63 let ret = SymGetLineFromAddr64(process, addr, &mut displacement, &mut line);
64 if ret == c::TRUE {
65 output_fileline(w,
66 CStr::from_ptr(line.Filename).to_bytes(),
67 line.LineNumber as c_int,
68 false)
69 } else {
70 Ok(())
71 }
72 }
73 }