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.
13 use dynamic_lib
::DynamicLibrary
;
17 use libc
::{c_ulong, c_int, c_char, c_void}
;
19 use super::{SymFromAddrFn, SymGetLineFromAddr64Fn}
;
21 use sys_common
::backtrace
::{output, output_fileline}
;
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
);
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;
35 let mut displacement
= 0u64;
36 let ret
= SymFromAddr(process
, addr
, &mut displacement
, &mut info
);
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() }
)
45 try
!(output(w
, i
, addr
as usize as *mut c_void
, name
));
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;
51 let mut displacement
= 0u32;
52 let ret
= SymGetLineFromAddr64(process
, addr
, &mut displacement
, &mut line
);
55 unsafe { CStr::from_ptr(line.Filename).to_bytes() }
,
56 line
.LineNumber
as c_int
,