Skip to content

Commit

Permalink
tracer: refactor TracerMode to include foreground and impl it in eBPF…
Browse files Browse the repository at this point in the history
… tracer
  • Loading branch information
kxxt committed Sep 16, 2024
1 parent 09e6e14 commit e4e0d4f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
8 changes: 5 additions & 3 deletions src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ impl EbpfTracer {
.as_ref()
.map(|tx| filterable_event!(TraceeSpawn(child)).send_if_match(tx, self.filter))
.transpose()?;
if let TracerMode::Log | TracerMode::None = &self.mode {
if let TracerMode::Log { foreground: true } = &self.mode {
match tcsetpgrp(stdin(), child) {
Ok(_) => {}
Err(Errno::ENOTTY) => {
Expand All @@ -451,7 +451,7 @@ impl EbpfTracer {
ForkResult::Child => {
let slave_pty = match &self.mode {
TracerMode::Tui(tty) => tty.as_ref(),
TracerMode::Log | TracerMode::None => None,
_ => None,
};

if let Some(pts) = slave_pty {
Expand Down Expand Up @@ -573,7 +573,9 @@ pub async fn run(command: EbpfCommand, user: Option<User>, color: Color) -> colo
baseline,
tx: None,
filter: TracerEventDetailsKind::empty(), // FIXME
mode: TracerMode::Log,
mode: TracerMode::Log {
foreground: log_args.foreground(),
},
};
let running_tracer = tracer.spawn(&mut obj, Some(output))?;
running_tracer.run_until_exit();
Expand Down
6 changes: 1 addition & 5 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,9 @@ pub struct LogModeArgs {
impl LogModeArgs {
pub fn foreground(&self) -> bool {
match (self.foreground, self.no_foreground) {
(true, false) => true,
(false, true) => false,
// Disable foreground mode in test by default
#[cfg(not(test))]
(true, false) => true,
_ => true,
#[cfg(test)]
_ => false,
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ async fn main() -> color_eyre::Result<()> {
let (tracer_tx, mut tracer_rx) = mpsc::unbounded_channel();
let (req_tx, req_rx) = mpsc::unbounded_channel();
let tracer = Arc::new(tracer::Tracer::new(
TracerMode::Log,
TracerMode::Log {
foreground: tracing_args.foreground(),
},
tracing_args,
modifier_args,
ptrace_args,
Expand Down Expand Up @@ -242,7 +244,9 @@ async fn main() -> color_eyre::Result<()> {
let (req_tx, req_rx) = mpsc::unbounded_channel();
let baseline = BaselineInfo::new()?;
let tracer = Arc::new(tracer::Tracer::new(
TracerMode::None,
TracerMode::Log {
foreground: tracing_args.foreground(),
},
tracing_args.clone(),
modifier_args.clone(),
ptrace_args,
Expand Down
26 changes: 11 additions & 15 deletions src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ pub struct Tracer {

pub enum TracerMode {
Tui(Option<UnixSlavePty>),
Log,
None,
Log { foreground: bool },
}

#[derive(Debug, Clone, Copy, PartialEq)]
Expand All @@ -124,7 +123,7 @@ impl PartialEq for TracerMode {
// I think a plain match is more readable here
#[allow(clippy::match_like_matches_macro)]
match (self, other) {
(Self::Log, Self::Log) => true,
(Self::Log { foreground: a }, Self::Log { foreground: b }) => a == b,
_ => false,
}
}
Expand Down Expand Up @@ -162,8 +161,7 @@ impl Tracer {
Ok(Self {
with_tty: match &mode {
TracerMode::Tui(tty) => tty.is_some(),
TracerMode::Log => true,
TracerMode::None => true,
TracerMode::Log { .. } => true,
},
store: RwLock::new(ProcessStateStore::new()),
#[cfg(feature = "seccomp-bpf")]
Expand All @@ -173,7 +171,7 @@ impl Tracer {
filter: {
let mut filter = tracer_event_args.filter()?;
trace!("Event filter: {:?}", filter);
if mode == TracerMode::Log {
if let TracerMode::Log { .. } = mode {
// FIXME: In logging mode, we rely on root child exit event to exit the process
// with the same exit code as the root child. It is not printed in logging mode.
// Ideally we should use another channel to send the exit code to the main thread.
Expand Down Expand Up @@ -245,7 +243,7 @@ impl Tracer {
let seccomp_bpf = self.seccomp_bpf;
let slave_pty = match &self.mode {
TracerMode::Tui(tty) => tty.as_ref(),
TracerMode::Log | TracerMode::None => None,
TracerMode::Log { .. } => None,
};
let with_tty = self.with_tty;
let use_pseudo_term = slave_pty.is_some();
Expand Down Expand Up @@ -338,15 +336,13 @@ impl Tracer {
self.store.write().unwrap().insert(root_child_state);
}
// Set foreground process group of the terminal
if let TracerMode::Log | TracerMode::None = &self.mode {
if self.foreground {
match tcsetpgrp(stdin(), root_child) {
Ok(_) => {}
Err(Errno::ENOTTY) => {
warn!("tcsetpgrp failed: ENOTTY");
}
r => r?,
if let TracerMode::Log { foreground: true } = &self.mode {
match tcsetpgrp(stdin(), root_child) {
Ok(_) => {}
Err(Errno::ENOTTY) => {
warn!("tcsetpgrp failed: ENOTTY");
}
r => r?,
}
}
let mut ptrace_opts = {
Expand Down
2 changes: 1 addition & 1 deletion src/tracer/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn tracer(
UnboundedReceiver<TracerMessage>,
UnboundedReceiver<PendingRequest>,
) {
let tracer_mod = TracerMode::Log;
let tracer_mod = TracerMode::Log { foreground: false };
let tracing_args = LogModeArgs::default();
let tracer_event_args = TracerEventArgs::all();
let (msg_tx, msg_rx) = tokio::sync::mpsc::unbounded_channel();
Expand Down

0 comments on commit e4e0d4f

Please sign in to comment.