Skip to content

Commit

Permalink
Add SyscallNo::name method
Browse files Browse the repository at this point in the history
This makes it easy to get a string representation of the syscall without
having to use `format!("{:?}", syscall)`. This is also more efficient in
that it doesn't require an allocation.
  • Loading branch information
jasonwhite committed Jun 22, 2020
1 parent 7f9a67b commit 781b4c1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
11 changes: 10 additions & 1 deletion src/nr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 +702,20 @@ static SYSCALL_NAMES: [&str; 345] = [
"fsmount",
"fspick",
];

impl SyscallNo {
#[inline]
fn name(&self) -> &'static str {
SYSCALL_NAMES[*self as usize]
}
}

impl fmt::Debug for SyscallNo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", SYSCALL_NAMES[self.clone() as usize])
f.write_str(self.name())
}
}

static SYSCALL_IDS: [SyscallNo; 345] = [
SYS_read,
SYS_write,
Expand Down
29 changes: 19 additions & 10 deletions syscalls-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,28 @@ fn gen_syscall_nrs(dest: &Path) -> Result<()> {
name.chars().skip(5).collect::<String>().as_str()
)?;
}
writeln!(f, "];")?;
writeln!(f, "];\n")?;

writeln!(f, "impl fmt::Debug for SyscallNo {{")?;
writeln!(
f,
" fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {{"
f.write(
br#"impl SyscallNo {
#[inline]
fn name(&self) -> &'static str {
SYSCALL_NAMES[*self as usize]
}
}
"#,
)?;
writeln!(
f,
" write!(f, \"{{}}\", SYSCALL_NAMES[self.clone() as usize])"

f.write(
br#"impl fmt::Debug for SyscallNo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.name())
}
}
"#,
)?;
writeln!(f, " }}")?;
writeln!(f, "}}")?;

writeln!(f, "static SYSCALL_IDS: [SyscallNo; {}] = [", syscalls.len())?;
for (name, _) in &syscalls {
Expand Down

0 comments on commit 781b4c1

Please sign in to comment.