Skip to content

Commit

Permalink
feat: add PEG based Pest parser
Browse files Browse the repository at this point in the history
feat: initial pest grammar & code

refactor: rename SyscallArg to Expression

refactor: IntegerExpression & BufferExpression types

feat: buffers

feat: macros

feat: array

fix: update/fix legacy regex parser

refactor: rename expressions in grammar

feat: simplify grammar, grab more metadata

feat: multiplication

feat: improve error handling

feat: truncated

feat: unfinished/resumed syscalls

fix: update/fix regex parser

feat: more flexible log level

feat: more tests & fixes

feat: in/out arguments

feat: improve comment handling

feat: named arguments

refactor: move regex parser specific bench

feat: grab more metadata

feat: bit shift

feat: macro dest addr
  • Loading branch information
desbma-s1n committed May 30, 2024
1 parent 0ec360b commit 51d9940
Show file tree
Hide file tree
Showing 10 changed files with 3,009 additions and 1,836 deletions.
150 changes: 150 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ itertools = { version = "0.11.0", default-features = false, features = ["use_std
lazy_static = { version = "1.4.0", default-features = false }
log = { version = "0.4.19", default-features = false, features = ["max_level_trace", "release_max_level_info"] }
nix = { version = "0.26.2", default-features = false, features = ["fs"] }
pest = { version = "2.7.10", default-features = false, features = ["std", "memchr"], optional = true }
pest_derive = { version = "2.7.10", default-features = false, features = ["std", "grammar-extras"], optional = true}
rand = { version = "0.8.5", default-features = false, features = ["std", "std_rng"] }
regex = { version = "1.9.1", default-features = false, features = ["std", "perf"] }
serde = { version = "1.0.193", default-features = false, features = ["std", "derive"] }
Expand All @@ -35,10 +37,11 @@ predicates = { version = "3.0.3", default-features = false, features = ["color"]
pretty_assertions = { version = "1.4.0", default-features = false, features = ["std"] }

[features]
# for benchmarks only
nightly = []
# for tests only
as-root = []
default = ["parser-peg"]
as-root = [] # for tests only
nightly = [] # for benchmarks only
parser-peg = ["dep:pest", "dep:pest_derive"]
parser-regex = []

[lints.rust]
missing_docs = "warn"
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ fn sd_options(
fn main() -> anyhow::Result<()> {
// Init logger
simple_logger::SimpleLogger::new()
.with_level(if cfg!(debug_assertions) {
log::LevelFilter::Debug
} else {
log::LevelFilter::Info
})
.env()
.init()
.context("Failed to init logger")?;
.context("Failed to setup logger")?;

// Get versions
let sd_version = systemd::SystemdVersion::local_system()?;
Expand Down
63 changes: 37 additions & 26 deletions src/strace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Syscall {
pub pid: u32,
pub rel_ts: f64,
pub name: String,
pub args: Vec<SyscallArg>,
pub args: Vec<Expression>,
pub ret_val: SyscallRetVal,
}

Expand All @@ -27,59 +27,70 @@ pub enum BufferType {
}

#[derive(Debug, Clone, PartialEq)]
pub enum SyscallArg {
Buffer {
value: Vec<u8>,
type_: BufferType,
},
Integer {
value: IntegerExpression,
metadata: Option<Vec<u8>>,
pub struct IntegerExpression {
pub value: IntegerExpressionValue,
pub metadata: Option<Vec<u8>>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct BufferExpression {
pub value: Vec<u8>,
pub type_: BufferType,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
Buffer(BufferExpression),
Integer(IntegerExpression),
Struct(HashMap<String, Expression>),
// The strace syntax can be ambiguous between array and set (ie sigset_t in sigprocmask),
// so store both in this, and let the summary interpret
Collection {
complement: bool,
values: Vec<Expression>,
},
Struct(HashMap<String, SyscallArg>),
Array(Vec<SyscallArg>),
Macro {
name: String,
args: Vec<SyscallArg>,
args: Vec<Expression>,
},
// Only used for strace pseudo macro invocations, see `test_macro_addr_arg` for an example
DestinationAddress(String),
}

impl SyscallArg {
impl Expression {
pub fn metadata(&self) -> Option<&[u8]> {
match self {
Self::Integer { metadata, .. } => metadata.as_deref(),
Self::Integer(IntegerExpression { metadata, .. }) => metadata.as_deref(),
_ => None,
}
}
}

#[derive(Debug, Clone, PartialEq)]
pub enum IntegerExpression {
BinaryNot(Box<IntegerExpression>),
BinaryOr(Vec<IntegerExpression>),
Multiplication(Vec<IntegerExpression>),
pub enum IntegerExpressionValue {
BinaryOr(Vec<IntegerExpressionValue>),
Multiplication(Vec<IntegerExpressionValue>),
LeftBitShift {
bits: Box<IntegerExpression>,
shift: Box<IntegerExpression>,
bits: Box<IntegerExpressionValue>,
shift: Box<IntegerExpressionValue>,
},
NamedConst(String),
Literal(i128), // allows holding both signed and unsigned 64 bit integers
}

impl IntegerExpression {
impl IntegerExpressionValue {
pub fn is_flag_set(&self, flag: &str) -> bool {
match self {
IntegerExpression::NamedConst(v) => flag == v,
IntegerExpression::BinaryOr(ces) => ces.iter().any(|ce| ce.is_flag_set(flag)),
IntegerExpression::BinaryNot(ce) => !ce.is_flag_set(flag),
IntegerExpressionValue::NamedConst(v) => flag == v,
IntegerExpressionValue::BinaryOr(ces) => ces.iter().any(|ce| ce.is_flag_set(flag)),
_ => false, // if it was a flag field, strace would have decoded it with named consts
}
}

pub fn flags(&self) -> Vec<String> {
match self {
IntegerExpression::NamedConst(v) => vec![v.clone()],
IntegerExpression::BinaryOr(vs) => vs.iter().flat_map(|v| v.flags()).collect(),
IntegerExpressionValue::NamedConst(v) => vec![v.clone()],
IntegerExpressionValue::BinaryOr(vs) => vs.iter().flat_map(|v| v.flags()).collect(),
_ => vec![],
}
}
Expand Down
Loading

0 comments on commit 51d9940

Please sign in to comment.