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