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
macro_rules! define_modules {
(
$($module_name:ident,)+
) => {
$(
mod $module_name;
#[allow(unused_imports)]
pub use $module_name::*;
)+
lazy_static! {
pub(crate) static ref SYSCALLS: Vec<Definition<'static>> = {
let mut definitions = Definitions::new();
$(
$module_name::get_definitions(&mut definitions);
)+
definitions.into_definitions()
};
}
};
}
macro_rules! define_calls {
(
$($kind:ident,)+
) => {
/// Enum variant of syscall, contains call-specific data
///
/// Currently only a subset of syscalls are resolved to these expanded structures
/// Use this [GitHub issue 3](https://github.com/blaind/hstrace/issues/3) to request a new syscall implementation
#[derive(Debug, PartialEq, Serialize)]
#[allow(non_camel_case_types)]
pub enum SyscallKind {
$($kind($kind),)*
None,
}
impl SyscallKind {
pub fn fmt_human(&self) -> String {
match &self {
$(SyscallKind::$kind(r) => r.hmz(),)*
SyscallKind::None => String::from("__unknown")
}
}
}
impl From<TraceOutput> for Syscall {
fn from(mut sv: TraceOutput) -> Syscall {
let name = FromPrimitive::from_usize(sv.nr).expect("primitive conversion");
let result = match sv.out.take() {
Some(res) => match res {
Ok(_) => Ok(()), // FIXME implement value
Err(e) => Err(e),
}
None => Ok(())
};
let kind: SyscallKind = match name {
$(
Ident::$kind => {
let x: Option<$kind> = sv.into();
SyscallKind::$kind(
match x {
Some(x) => x,
None => {
panic!("kind conversion failed (name: {:?}), check impl From<TraceOutput> for Option<SYSCALL>", name)
}
}
)
},
)*
_ => SyscallKind::None,
};
Syscall::new(name, kind, result)
}
}
};
}
macro_rules! define_structs {
(
$($syscall:ident($kind:ty),)+
) => {
#[derive(Clone)]
#[allow(non_camel_case_types)]
pub enum CStruct {
$($syscall($kind),)+
}
impl fmt::Debug for CStruct {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
$(CStruct::$syscall(c) => write!(f, "{:?}", c),)+
}
}
}
#[derive(Clone, Debug)]
#[allow(non_camel_case_types)]
pub(crate) enum CStructAV {
$($syscall,)+
}
impl CStructAV {
pub fn map_value<'a, T>(&self, direction: &Direction, mut src: &mut ValueTransformer<'a, T>) -> Result<Value, TraceError>
where
T: crate::ptrace::Tracer,
{
let ret = match self {
$(
CStructAV::$syscall => {
if direction == &src.dir {
<$kind as CToCall>::from_src(&mut src)?
} else {
Value::Skip
}
},
)+
};
Ok(ret)
}
}
};
}
macro_rules! define_callnames {
(
$($syscall:ident = $position:tt,)+
) => {
#[allow(dead_code)]
#[derive(FromPrimitive, Debug, PartialEq, Clone, Copy, Serialize)]
pub enum Ident {
/// Unknown call, nr could not be parsed into an enum (missing implementation at hstrace)
Unknown = -1,
$($syscall = $position,)*
}
impl Ident {
pub fn iter() -> Iter<'static, Ident> {
// FIXME: get the number in macro invocation
static CALLNAMES: [Ident; 347] = [
$(Ident::$syscall,)+
];
CALLNAMES.iter()
}
}
impl fmt::Display for Ident {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
};
}