Skip to content

Commit

Permalink
feat: named arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
desbma-s1n committed May 30, 2024
1 parent 47cf861 commit c258b51
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 15 deletions.
52 changes: 52 additions & 0 deletions src/strace/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,58 @@ mod tests {
})
);
}

#[cfg_attr(
feature = "parser-regex",
ignore = "named arguments not supported by regex-parser"
)]
#[test]
fn test_clone() {
let _ = simple_logger::SimpleLogger::new().init();

assert_eq!(
parse_line(
"714433 0.000035 clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f3f3c2f5090) = 714434",
&[]
)
.unwrap(),
ParseResult::Syscall(Syscall {
pid: 714433,
rel_ts: 0.000035,
name: "clone".to_owned(),
args: vec![
Expression::Struct(HashMap::from([
(
"child_stack".to_owned(),
Expression::Integer(IntegerExpression {
value: IntegerExpressionValue::NamedConst("NULL".to_owned()),
metadata: None,
}),
),
(
"flags".to_owned(),
Expression::Integer(IntegerExpression {
value: IntegerExpressionValue::BinaryOr(vec![
IntegerExpressionValue::NamedConst("CLONE_CHILD_CLEARTID".to_owned()),
IntegerExpressionValue::NamedConst("CLONE_CHILD_SETTID".to_owned()),
IntegerExpressionValue::NamedConst("SIGCHLD".to_owned()),
]),
metadata: None
}),
),
(
"child_tidptr".to_owned(),
Expression::Integer(IntegerExpression {
value: IntegerExpressionValue::Literal(0x7f3f3c2f5090),
metadata: None,
}),
),
])),
],
ret_val: 714434
})
);
}
}

#[cfg(all(feature = "nightly", test))]
Expand Down
8 changes: 7 additions & 1 deletion src/strace/parser/peg.pest
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ rel_ts = { ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }

name = { symbol_name }

arguments = { (argument ~ (", " ~ argument)*)? }
arguments = {
#named = named_arguments |
#unnamed = unnamed_arguments
}

ret_val = { int ~ (" " ~ ANY*)? }


// Subrules

named_arguments = { struct_member ~ (", " ~ struct_member)* }
unnamed_arguments = { (argument ~ (", " ~ argument)*)? }

argument = {
#in_out = in_out_argument |
#in = in_argument
Expand Down
47 changes: 33 additions & 14 deletions src/strace/parser/peg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,41 @@ impl TryFrom<Pair<'_, Rule>> for Syscall {
.to_owned();

let args = if pair_tag.as_str() != "end" {
subpairs
let args_pair = subpairs
.next()
.ok_or_else(|| anyhow::anyhow!("Missing arguments node"))?
.into_inner()
.map(|p| {
let p = pair_descend(p, 1)?;
match p.as_node_tag() {
Some("in") => pair_descend(p, 2)?.try_into(),
Some("in_out") => {
// Only take the 'in' part, ignore the rest
pair_descend(p, 2)?.try_into()
.ok_or_else(|| anyhow::anyhow!("Missing arguments node"))?;
let args_pair = pair_descend(args_pair, 1)?;
match args_pair.as_node_tag() {
Some("unnamed") => args_pair
.into_inner()
.map(|p| {
let p = pair_descend(p, 1)?;
match p.as_node_tag() {
Some("in") => pair_descend(p, 2)?.try_into(),
Some("in_out") => {
// Only take the 'in' part, ignore the rest
pair_descend(p, 2)?.try_into()
}
_ => anyhow::bail!("Unhandled pair: {p:?}"),
}
_ => anyhow::bail!("Unhandled pair: {p:?}"),
}
})
.collect::<Result<_, _>>()?
})
.collect::<Result<_, _>>()?,
Some("named") => {
// Handle name arguments as a single struct
vec![Expression::Struct(
args_pair
.into_inner()
.map(|p| -> anyhow::Result<_> {
let (name, val) = p.into_inner().next_tuple().ok_or_else(|| {
anyhow::anyhow!("Missing name arguments nodes")
})?;
Ok((name.as_str().to_owned(), pair_descend(val, 1)?.try_into()?))
})
.collect::<Result<_, _>>()?,
)]
}
_ => anyhow::bail!("Unhandled pair: {args_pair:?}"),
}
} else {
vec![]
};
Expand Down

0 comments on commit c258b51

Please sign in to comment.