1use core::fmt;
10
11use nix::fcntl::AtFlags;
12use nix::fcntl::OFlag;
13use nix::sched::CloneFlags;
14use nix::sys::epoll::EpollCreateFlags;
15use nix::sys::eventfd::EfdFlags;
16use nix::sys::inotify::InitFlags;
17use nix::sys::mman::MapFlags;
18use nix::sys::mman::ProtFlags;
19use nix::sys::signalfd::SfdFlags;
20use nix::sys::socket::AddressFamily;
21use nix::sys::socket::SockFlag;
22use nix::sys::socket::SockProtocol;
23use nix::sys::stat::Mode;
24use nix::sys::stat::SFlag;
25use nix::sys::timerfd::TimerFlags;
26use nix::sys::wait::WaitPidFlag;
27use nix::unistd::Pid;
28
29use crate::Addr;
30use crate::AddrMut;
31use crate::Errno;
32use crate::MemoryAccess;
33
34pub struct Display<'a, M, T> {
37 memory: &'a M,
39
40 syscall: &'a T,
42
43 outputs: bool,
45}
46
47impl<'a, M, T> Display<'a, M, T> {
48 pub fn new(memory: &'a M, syscall: &'a T, outputs: bool) -> Self {
51 Display {
52 memory,
53 syscall,
54 outputs,
55 }
56 }
57}
58
59impl<'a, M, T> fmt::Display for Display<'a, M, T>
60where
61 M: MemoryAccess,
62 T: Displayable,
63{
64 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65 self.syscall.fmt(self.memory, self.outputs, f)
66 }
67}
68
69pub trait Displayable {
106 fn fmt<M: MemoryAccess>(
108 &self,
109 memory: &M,
110 outputs: bool,
111 f: &mut fmt::Formatter,
112 ) -> fmt::Result;
113
114 fn display<'a, M>(&'a self, memory: &'a M) -> Display<'a, M, Self>
117 where
118 M: MemoryAccess,
119 Self: Sized,
120 {
121 Display::new(memory, self, false)
122 }
123
124 fn display_with_outputs<'a, M>(&'a self, memory: &'a M) -> Display<'a, M, Self>
128 where
129 M: MemoryAccess,
130 Self: Sized,
131 {
132 Display::new(memory, self, true)
133 }
134}
135
136impl<'a, T> Displayable for Option<Addr<'a, T>> {
137 fn fmt<M: MemoryAccess>(
138 &self,
139 _memory: &M,
140 _outputs: bool,
141 f: &mut fmt::Formatter,
142 ) -> fmt::Result {
143 match self {
144 None => write!(f, "NULL"),
145 Some(addr) => write!(f, "{:?}", addr),
146 }
147 }
148}
149
150impl<'a, T> Displayable for Option<AddrMut<'a, T>> {
151 fn fmt<M: MemoryAccess>(
152 &self,
153 _memory: &M,
154 _outputs: bool,
155 f: &mut fmt::Formatter,
156 ) -> fmt::Result {
157 match self {
158 None => write!(f, "NULL"),
159 Some(addr) => write!(f, "{:?}", addr),
160 }
161 }
162}
163
164impl Displayable for OFlag {
165 fn fmt<M: MemoryAccess>(
166 &self,
167 _memory: &M,
168 _outputs: bool,
169 f: &mut fmt::Formatter,
170 ) -> fmt::Result {
171 if self.is_empty() {
172 f.write_str("0")
176 } else {
177 fmt::Debug::fmt(self, f)
178 }
179 }
180}
181
182impl<T> Displayable for Result<T, Errno>
183where
184 T: fmt::Display,
185{
186 fn fmt<M: MemoryAccess>(
187 &self,
188 _memory: &M,
189 _outputs: bool,
190 f: &mut fmt::Formatter,
191 ) -> fmt::Result {
192 match self {
193 Ok(x) => fmt::Display::fmt(x, f),
194 Err(err) => fmt::Display::fmt(err, f),
195 }
196 }
197}
198
199#[macro_export]
202macro_rules! impl_displayable {
203 ($fmt:ident $t:ty) => {
204 impl $crate::Displayable for $t {
205 fn fmt<M: $crate::MemoryAccess>(
206 &self,
207 _memory: &M,
208 _outputs: bool,
209 f: &mut ::core::fmt::Formatter,
210 ) -> ::core::fmt::Result {
211 ::core::fmt::$fmt::fmt(self, f)
212 }
213 }
214 };
215}
216
217#[macro_export]
220macro_rules! displayable_ptr {
221 ($type:ident, $pointer:ident<$value:ident>) => {
222 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
224 #[allow(missing_docs)]
225 pub struct $type<'a>(pub $crate::$pointer<'a, $value>);
226
227 impl<'a> $crate::FromToRaw for std::option::Option<$type<'a>> {
228 fn from_raw(raw: usize) -> Self {
229 $crate::$pointer::from_ptr(raw as *const $value).map($type)
230 }
231
232 fn into_raw(self) -> usize {
233 self.map(|p| p.0).into_raw()
234 }
235 }
236
237 impl<'a> $crate::ReadAddr for $type<'a> {
238 type Target = $value;
239 type Error = $crate::Errno;
240
241 fn read<M: $crate::MemoryAccess>(
242 &self,
243 memory: &M,
244 ) -> Result<Self::Target, Self::Error> {
245 memory.read_value(self.0)
246 }
247 }
248
249 impl<'a> $crate::Displayable for std::option::Option<$type<'a>> {
250 fn fmt<M: $crate::MemoryAccess>(
251 &self,
252 memory: &M,
253 outputs: bool,
254 f: &mut std::fmt::Formatter,
255 ) -> std::fmt::Result {
256 $crate::fmt_nullable_ptr(f, &self.map(|x| x.0), memory, outputs)
257 }
258 }
259
260 impl<'a> From<$type<'a>> for $crate::AddrMut<'a, $value> {
261 fn from(time_ptr: $type<'a>) -> Self {
262 time_ptr.0
263 }
264 }
265
266 impl<'a> From<$type<'a>> for $crate::Addr<'a, $value> {
267 fn from(time_ptr: $type<'a>) -> Self {
268 time_ptr.0.into()
269 }
270 }
271 };
272}
273
274impl_displayable!(Debug AtFlags);
275impl_displayable!(Debug CloneFlags);
276impl_displayable!(Debug Mode);
277impl_displayable!(Debug SFlag);
278impl_displayable!(Debug WaitPidFlag);
279impl_displayable!(Debug MapFlags);
280impl_displayable!(Debug ProtFlags);
281impl_displayable!(Debug EpollCreateFlags);
282impl_displayable!(Debug EfdFlags);
283impl_displayable!(Debug SfdFlags);
284impl_displayable!(Debug InitFlags);
285impl_displayable!(Debug SockFlag);
286impl_displayable!(Debug AddressFamily);
287impl_displayable!(Debug SockProtocol);
288impl_displayable!(Debug Option<SockProtocol>);
289impl_displayable!(Debug TimerFlags);
290
291impl_displayable!(Display Pid);
292impl_displayable!(Display i32);
293impl_displayable!(Display u32);
294impl_displayable!(Display i64);
295impl_displayable!(Display u64);
296impl_displayable!(Display isize);
297impl_displayable!(Display usize);