-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsiginfo.rs
40 lines (37 loc) · 1.47 KB
/
siginfo.rs
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
//! The `Siginfo` is the struct for Linux signals.
use regex::Regex;
use crate::error;
/// Definition from [siginfo.h](https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/siginfo.h) (not all fields are defined).
#[derive(Copy, Clone, Default)]
pub struct Siginfo {
/// Signal number.
pub si_signo: u32,
pub si_errno: u32,
pub si_code: u32,
/// Address due to access to which an exception was occurred.
pub si_addr: u64,
}
impl Siginfo {
/// Construct `Siginfo` from string (integers are hex).
///
/// # Arguments
///
/// * 'info' - gdb output string with siginfo
pub fn from_gdb<T: AsRef<str>>(info: T) -> error::Result<Siginfo> {
let re =
Regex::new(r"si_signo.*?0x([0-9a-f]+)(?:.|\n)*si_errno.*?0x([0-9a-f]+)(?:.|\n)*si_code.*?0x([0-9a-f]+)(?:.|\n)*si_addr.*?0x([0-9a-f]+)").unwrap();
if let Some(caps) = re.captures(info.as_ref()) {
Ok(Siginfo {
si_signo: u32::from_str_radix(caps.get(1).unwrap().as_str(), 16)?,
si_errno: u32::from_str_radix(caps.get(2).unwrap().as_str(), 16)?,
si_code: u32::from_str_radix(caps.get(3).unwrap().as_str(), 16)?,
si_addr: u64::from_str_radix(caps.get(4).unwrap().as_str(), 16)?,
})
} else {
Err(error::Error::SiginfoParse(format!(
"Siginfo string: {} doesn't match regex template",
info.as_ref()
)))
}
}
}