Skip to content

Commit

Permalink
Output to file, JSON output draft
Browse files Browse the repository at this point in the history
  • Loading branch information
blaind committed Apr 11, 2021
1 parent 7d881fb commit f07cd5f
Show file tree
Hide file tree
Showing 25 changed files with 279 additions and 80 deletions.
47 changes: 46 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hstrace"
version = "0.0.4"
version = "0.0.5"
authors = ["Mika Vatanen <blaind@blaind.net>"]
repository = "https://github.com/blaind/hstrace"
documentation = "https://docs.rs/hstrace"
Expand Down Expand Up @@ -38,6 +38,8 @@ bitflags = "1.2.1"
crossbeam-utils = "0.8.3"
ctrlc = "3.1.8"
lazy_static = "1.4.0"
serde = { version = "1.0.125", features = ["derive"] }
serde_json = "1.0.64"

[build-dependencies]
bindgen = "0.58.1"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ FLAGS:
OPTIONS:
-e <expr> Expression
-m <mode> Run mode [default: strace]
-o <file> Save output to a file instead of stderr. If suffix is `.json`, will be stored in JSON-format
(format subject to change)
-p <pid> PID to trace
-s <strsize> Maximum length of printable strings [default: 32]
Expand Down
4 changes: 3 additions & 1 deletion src/call/fncntl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;
use serde::Serialize;

pub(crate) fn get_definitions(inp: &mut Definitions) {
inp.add(
Expand All @@ -16,7 +17,7 @@ pub(crate) fn get_definitions(inp: &mut Definitions) {
);
}

#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("Open a file (dirfd: {}) {} with flags {:?}", self.dirfd, self.pathname, self.flags))]
pub struct Openat {
#[hstrace]
Expand All @@ -31,6 +32,7 @@ pub struct Openat {
}

bitflags! {
#[derive(Serialize)]
pub struct OpenatMode: isize {
const O_ACCMODE = 0o0003;
const O_RDONLY = 0o0;
Expand Down
4 changes: 3 additions & 1 deletion src/call/mman.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;
use serde::Serialize;

pub(crate) fn get_definitions(inp: &mut Definitions) {
inp.add(
Expand Down Expand Up @@ -42,7 +43,7 @@ pub(crate) fn get_definitions(inp: &mut Definitions) {
);
}

#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("Protect memory {:?} - {:?} (len {}) with flags {:?}",
self.addr,
MemoryAddress(self.addr.0 + self.len),
Expand All @@ -61,6 +62,7 @@ pub struct Mprotect {
}

bitflags! {
#[derive(Serialize)]
pub struct Prot: isize {
const PROT_READ = 0x1;
const PROT_WRITE = 0x2;
Expand Down
1 change: 1 addition & 0 deletions src/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//! Use this [GitHub issue 3](https://github.com/blaind/hstrace/issues/3) to request a new syscall implementation
use num_traits::FromPrimitive;
use serde::Serialize;
use std::fmt;

use crate::syscall::{Definition, Direction};
Expand Down
3 changes: 2 additions & 1 deletion src/call/sendfile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;
use serde::Serialize;

pub(crate) fn get_definitions(inp: &mut Definitions) {
inp.add(
Expand All @@ -15,7 +16,7 @@ pub(crate) fn get_definitions(inp: &mut Definitions) {
}

/// Syscall: Transfer data between file descriptors
#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("Transfer data to fd {:?} from fd {:?} offset {:?} len {:?}", self.out_fd, self.in_fd, self.offset, self.count))]
pub struct Sendfile {
/// FD where data is sent
Expand Down
6 changes: 4 additions & 2 deletions src/call/socket.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;
use serde::Serialize;

pub(crate) fn get_definitions(inp: &mut Definitions) {
inp.add(
Expand All @@ -17,7 +18,7 @@ pub(crate) fn get_definitions(inp: &mut Definitions) {
}

/// Syscall: Create an endpoint for communication
#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("Open {:?} domain socket with type {:?} and protocol {:?}", self.domain, self.socket_type, self.protocol))]
pub struct Socket {
#[hstrace]
Expand All @@ -41,7 +42,7 @@ pub enum SockAddr {

/// Argument: Communication domain / protocol family used
#[allow(dead_code, non_camel_case_types)]
#[derive(Debug, Clone, FromPrimitive, PartialEq)]
#[derive(Debug, Clone, FromPrimitive, PartialEq, Serialize)]
#[repr(isize)]
pub enum AddressFamily {
AF_UNSPEC = libc::AF_UNSPEC as isize,
Expand All @@ -53,6 +54,7 @@ pub enum AddressFamily {

bitflags! {
/// Argument: Socket communication semantics
#[derive(Serialize)]
pub struct SocketType: isize {
const SOCK_STREAM = libc::SOCK_STREAM as isize;
const SOCK_DGRAM = libc::SOCK_DGRAM as isize;
Expand Down
5 changes: 3 additions & 2 deletions src/call/stat.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::prelude::*;
use crate::from_c::stat as sys_stat;
use serde::Serialize;

pub(crate) fn get_definitions(inp: &mut Definitions) {
inp.add(
Expand All @@ -17,7 +18,7 @@ pub(crate) fn get_definitions(inp: &mut Definitions) {
);
}

#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("Stat path {:?} returned {:?}", self.pathname, self.stat))]
pub struct Stat {
#[hstrace]
Expand All @@ -27,7 +28,7 @@ pub struct Stat {
pub stat: StatResult,
}

#[derive(Debug, Clone, PartialEq, FromCStruct)]
#[derive(Debug, Clone, PartialEq, FromCStruct, Serialize)]
#[hstrace(c_struct = sys_stat)]
pub struct StatResult {
// st_dev=makedev(0xfd, 0),
Expand Down
5 changes: 3 additions & 2 deletions src/call/swap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;
use serde::Serialize;

pub(crate) fn get_definitions(inp: &mut Definitions) {
inp.add(
Expand All @@ -17,7 +18,7 @@ pub(crate) fn get_definitions(inp: &mut Definitions) {
}

/// Syscall: Start swapping to file/device
#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("Enable swap for path {:?} with flags {:?}", self.path, self.swapflags))]
pub struct Swapon {
#[hstrace]
Expand All @@ -28,7 +29,7 @@ pub struct Swapon {
}

/// Syscall: Stop swap on file/device
#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("Disable swap path {}", self.path))]
pub struct Swapoff {
#[hstrace]
Expand Down
14 changes: 8 additions & 6 deletions src/call/unistd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;
use serde::Serialize;

pub(crate) fn get_definitions(inp: &mut Definitions) {
inp.add(
Expand Down Expand Up @@ -75,7 +76,7 @@ pub(crate) fn get_definitions(inp: &mut Definitions) {
}

/// Syscall: Read value of a symbolic link
#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("{:?} symlink points to {:?}", self.src, self.dst))]
pub struct Readlink {
/// Source file
Expand All @@ -87,7 +88,7 @@ pub struct Readlink {
pub dst: Option<String>,
}

#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("Check path {:?} permissions for mode {:?}", self.pathname, self.mode))]
pub struct Access {
#[hstrace]
Expand All @@ -97,30 +98,30 @@ pub struct Access {
pub mode: AccessMode,
}

#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("Resolved current path to {:?}", self.pathname))]
pub struct Getcwd {
#[hstrace]
pub pathname: Option<String>,
}

#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("Closed file descriptor {:?}", self.fd))]
pub struct Close {
/// File descriptor
#[hstrace]
pub fd: isize,
}

#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("Request memory address expansion to {:?}", self.addr))]
pub struct Brk {
/// Expand to memory address
#[hstrace]
pub addr: MemoryAddress,
}

#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("Change working directory to {:?}", self.path))]
pub struct Chdir {
/// Working directory
Expand All @@ -129,6 +130,7 @@ pub struct Chdir {
}

bitflags! {
#[derive(Serialize)]
pub struct AccessMode: isize {
const R_OK = 4;
const W_OK = 2;
Expand Down
5 changes: 3 additions & 2 deletions src/call/utsname.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::prelude::*;
use crate::from_c::utsname as sys_utsname;
use serde::Serialize;

pub(crate) fn get_definitions(inp: &mut Definitions) {
inp.add(
Expand All @@ -10,14 +11,14 @@ pub(crate) fn get_definitions(inp: &mut Definitions) {
);
}

#[derive(Debug, PartialEq, FromPtrace)]
#[derive(Debug, PartialEq, FromPtrace, Serialize)]
#[hstrace(hmz("Detected uname to be {:?}", self.utsname))]
pub struct Uname {
#[hstrace(c_struct = sys_utsname)]
pub utsname: Utsname,
}

#[derive(Debug, Clone, PartialEq, FromCStruct)]
#[derive(Debug, Clone, PartialEq, FromCStruct, Serialize)]
#[hstrace(c_struct = sys_utsname)]
pub struct Utsname {
#[hstrace(c_char)]
Expand Down
5 changes: 5 additions & 0 deletions src/clap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ args:
help: Run mode
short: m
default_value: strace
- output_file:
help: Save output to a file instead of stderr. If suffix is `.json`, will be stored in JSON-format (format subject to change)
value_name: file
short: o
takes_value: true
- pid:
help: PID to trace
short: p
Expand Down
Loading

0 comments on commit f07cd5f

Please sign in to comment.