1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::fmt;
use std::os::raw::c_void;
use std::path::Path;
use std::str;
use rustc_demangle::{demangle, Demangle};
pub trait Symbol {
fn name(&self) -> Option<SymbolName> { None }
fn addr(&self) -> Option<*mut c_void> { None }
fn filename(&self) -> Option<&Path> { None }
fn lineno(&self) -> Option<u32> { None }
}
pub struct SymbolName<'a> {
bytes: &'a [u8],
demangled: Option<Demangle<'a>>,
}
pub fn resolve<F: FnMut(&Symbol)>(addr: *mut c_void, mut cb: F) {
resolve_imp(addr, &mut cb)
}
impl fmt::Debug for Symbol {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut d = f.debug_struct("Symbol");
if let Some(name) = self.name() {
d.field("name", &name);
}
if let Some(addr) = self.addr() {
d.field("addr", &addr);
}
if let Some(filename) = self.filename() {
d.field("filename", &filename);
}
if let Some(lineno) = self.lineno() {
d.field("lineno", &lineno);
}
d.finish()
}
}
impl<'a> SymbolName<'a> {
pub fn new(bytes: &'a [u8]) -> SymbolName<'a> {
let demangled = str::from_utf8(bytes).ok().map(demangle);
SymbolName {
bytes: bytes,
demangled: demangled,
}
}
pub fn as_str(&self) -> Option<&'a str> {
self.demangled.as_ref().map(|s| s.as_str())
}
pub fn as_bytes(&self) -> &'a [u8] {
self.bytes
}
}
impl<'a> fmt::Display for SymbolName<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(ref s) = self.demangled {
s.fmt(f)
} else {
String::from_utf8_lossy(self.bytes).fmt(f)
}
}
}
impl<'a> fmt::Debug for SymbolName<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(ref s) = self.demangled {
s.fmt(f)
} else {
String::from_utf8_lossy(self.bytes).fmt(f)
}
}
}
cfg_if! {
if #[cfg(all(windows, feature = "dbghelp"))] {
mod dbghelp;
use self::dbghelp::resolve as resolve_imp;
} else if #[cfg(all(feature = "libbacktrace", unix,
not(target_os = "macos")))] {
mod libbacktrace;
use self::libbacktrace::resolve as resolve_imp;
} else if #[cfg(all(unix, feature = "dladdr"))] {
mod dladdr;
use self::dladdr::resolve as resolve_imp;
} else {
mod noop;
use self::noop::resolve as resolve_imp;
}
}