tracexec-core 1.0.0-rc.1

Core crate of tracexec [Internal implementation! DO NOT DEPEND ON!]
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
use std::{
  collections::BTreeMap,
  fmt::Debug,
  sync::{
    Arc,
    atomic::AtomicU64,
  },
};

use chrono::{
  DateTime,
  Local,
};
use clap::ValueEnum;
use crossterm::event::KeyEvent;
use enumflags2::BitFlags;
use filterable_enum::FilterableEnum;
use itertools::Itertools;
use nix::{
  errno::Errno,
  libc::c_int,
  unistd::Pid,
};
use strum::Display;
use tokio::sync::mpsc;

use crate::{
  breakpoint::BreakPointHit,
  cache::ArcStr,
  proc::{
    CgroupInfo,
    Cred,
    CredInspectError,
    EnvDiff,
    FileDescriptorInfoCollection,
    Interpreter,
  },
  timestamp::Timestamp,
  tracer::{
    InspectError,
    ProcessExit,
    Signal,
  },
};

mod id;
mod message;
mod parent;
pub use id::*;
pub use message::*;
pub use parent::*;

#[derive(Debug, Clone, Display, PartialEq, Eq)]
pub enum Event {
  ShouldQuit,
  Key(KeyEvent),
  Tracer(TracerMessage),
  Render,
  Resize { width: u16, height: u16 },
  Init,
  Error,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TracerMessage {
  /// A tracer event is an event that could show in the logs or event list
  Event(TracerEvent),
  /// A state update is any event that doesn't need to show in logs or having
  /// its own line in event list.
  StateUpdate(ProcessStateUpdateEvent),
  FatalError(String),
}

impl From<TracerEvent> for TracerMessage {
  fn from(event: TracerEvent) -> Self {
    Self::Event(event)
  }
}

impl From<ProcessStateUpdateEvent> for TracerMessage {
  fn from(update: ProcessStateUpdateEvent) -> Self {
    Self::StateUpdate(update)
  }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TracerEvent {
  pub details: TracerEventDetails,
  pub id: EventId,
}

/// A global counter for events, though it should only be used by the tracer thread.
static ID: AtomicU64 = AtomicU64::new(0);

impl TracerEvent {
  pub fn allocate_id() -> EventId {
    EventId::new(ID.fetch_add(1, std::sync::atomic::Ordering::SeqCst))
  }
}

impl From<TracerEventDetails> for TracerEvent {
  fn from(details: TracerEventDetails) -> Self {
    Self {
      details,
      id: Self::allocate_id(),
    }
  }
}

#[derive(Debug, Clone, PartialEq, Eq, FilterableEnum)]
#[filterable_enum(kind_extra_derive=ValueEnum, kind_extra_derive=Display, kind_extra_attrs="strum(serialize_all = \"kebab-case\")")]
pub enum TracerEventDetails {
  Info(TracerEventMessage),
  Warning(TracerEventMessage),
  Error(TracerEventMessage),
  NewChild {
    timestamp: Timestamp,
    ppid: Pid,
    pcomm: ArcStr,
    pid: Pid,
  },
  Exec(Box<ExecEvent>),
  TraceeSpawn {
    pid: Pid,
    timestamp: Timestamp,
  },
  TraceeExit {
    timestamp: Timestamp,
    signal: Option<Signal>,
    exit_code: i32,
  },
}

impl TracerEventDetails {
  pub fn into_event_with_id(self, id: EventId) -> TracerEvent {
    TracerEvent { details: self, id }
  }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TracerEventMessage {
  pub pid: Option<Pid>,
  pub timestamp: Option<DateTime<Local>>,
  pub msg: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExecEvent {
  pub syscall: ExecSyscall,
  pub exec_pid: Pid,
  pub pid: Pid,
  pub cwd: OutputMsg,
  pub comm: ArcStr,
  pub filename: OutputMsg,
  pub argv: Arc<Result<Vec<OutputMsg>, InspectError>>,
  pub envp: Arc<Result<BTreeMap<OutputMsg, OutputMsg>, InspectError>>,
  /// There are env var(s) whose key starts with dash
  pub has_dash_env: bool,
  pub cred: Result<Cred, CredInspectError>,
  pub interpreter: Option<Vec<Interpreter>>,
  pub env_diff: Result<EnvDiff, InspectError>,
  pub fdinfo: Arc<FileDescriptorInfoCollection>,
  pub result: i64,
  pub timestamp: Timestamp,
  pub parent: Option<ParentEventId>,
  pub cgroup: CgroupInfo,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Display)]
#[strum(serialize_all = "lowercase")]
pub enum ExecSyscall {
  Execve,
  Execveat,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RuntimeModifier {
  pub show_env: bool,
  pub show_cwd: bool,
}

impl Default for RuntimeModifier {
  fn default() -> Self {
    Self {
      show_env: true,
      show_cwd: true,
    }
  }
}

impl TracerEventDetails {
  pub fn into_tracer_msg(self) -> TracerMessage {
    TracerMessage::Event(self.into())
  }

  pub fn timestamp(&self) -> Option<Timestamp> {
    match self {
      Self::Info(m) | Self::Warning(m) | Self::Error(m) => m.timestamp,
      Self::Exec(exec_event) => Some(exec_event.timestamp),
      Self::NewChild { timestamp, .. }
      | Self::TraceeSpawn { timestamp, .. }
      | Self::TraceeExit { timestamp, .. } => Some(*timestamp),
    }
  }
}

impl TracerEventDetails {
  pub fn argv_to_string(argv: &Result<Vec<OutputMsg>, InspectError>) -> String {
    let Ok(argv) = argv else {
      return "[failed to read argv]".into();
    };
    format!("[{}]", argv.iter().format(", "))
  }

  pub fn interpreters_to_string(interpreters: &[Interpreter]) -> String {
    match interpreters {
      [] => Interpreter::None.to_string(),
      [interpreter] => interpreter.to_string(),
      interpreters => format!("[{}]", interpreters.iter().format(", ")),
    }
  }
}

impl FilterableTracerEventDetails {
  pub fn send_if_match(
    self,
    tx: &mpsc::UnboundedSender<TracerMessage>,
    filter: BitFlags<TracerEventDetailsKind>,
  ) -> Result<(), mpsc::error::SendError<TracerMessage>> {
    if let Some(evt) = self.filter_and_take(filter) {
      tx.send(TracerMessage::from(TracerEvent::from(evt)))?;
    }
    Ok(())
  }
}

#[macro_export]
macro_rules! filterable_event {
    ($($t:tt)*) => {
      tracexec_core::event::FilterableTracerEventDetails::from(tracexec_core::event::TracerEventDetails::$($t)*)
    };
}

pub use filterable_event;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProcessStateUpdate {
  Exit {
    status: ProcessExit,
    timestamp: Timestamp,
  },
  BreakPointHit(BreakPointHit),
  Resumed,
  Detached {
    hid: u64,
    timestamp: Timestamp,
  },
  ResumeError {
    hit: BreakPointHit,
    error: Errno,
  },
  DetachError {
    hit: BreakPointHit,
    error: Errno,
  },
}

impl ProcessStateUpdate {
  pub fn termination_timestamp(&self) -> Option<Timestamp> {
    match self {
      Self::Exit { timestamp, .. } | Self::Detached { timestamp, .. } => Some(*timestamp),
      _ => None,
    }
  }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcessStateUpdateEvent {
  pub update: ProcessStateUpdate,
  pub pid: Pid,
  pub ids: Vec<EventId>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventStatus {
  // exec status
  ExecENOENT,
  ExecFailure,
  // process status
  ProcessRunning,
  ProcessExitedNormally,
  ProcessExitedAbnormally(c_int),
  ProcessPaused,
  ProcessDetached,
  // signaled
  ProcessKilled,
  ProcessTerminated,
  ProcessInterrupted,
  ProcessSegfault,
  ProcessAborted,
  ProcessIllegalInstruction,
  ProcessSignaled(Signal),
  // internal failure
  InternalError,
}

impl From<EventStatus> for &'static str {
  fn from(value: EventStatus) -> Self {
    match value {
      EventStatus::ExecENOENT => "⚠️",
      EventStatus::ExecFailure => "",
      EventStatus::ProcessRunning => "🟢",
      EventStatus::ProcessExitedNormally => "😇",
      EventStatus::ProcessExitedAbnormally(_) => "😡",
      EventStatus::ProcessKilled => "😵",
      EventStatus::ProcessTerminated => "🤬",
      EventStatus::ProcessInterrupted => "🥺",
      EventStatus::ProcessSegfault => "💥",
      EventStatus::ProcessAborted => "😱",
      EventStatus::ProcessIllegalInstruction => "👿",
      EventStatus::ProcessSignaled(_) => "💀",
      EventStatus::ProcessPaused => "⏸️",
      EventStatus::ProcessDetached => "🛸",
      EventStatus::InternalError => "",
    }
  }
}

impl std::fmt::Display for EventStatus {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    let icon: &str = <&'static str>::from(*self);
    write!(f, "{icon} ")?;
    use EventStatus::*;
    match self {
      ExecENOENT | ExecFailure => write!(
        f,
        "Exec failed. Further process state is not available for this event."
      )?,
      ProcessRunning => write!(f, "Running")?,
      ProcessTerminated => write!(f, "Terminated")?,
      ProcessAborted => write!(f, "Aborted")?,
      ProcessSegfault => write!(f, "Segmentation fault")?,
      ProcessIllegalInstruction => write!(f, "Illegal instruction")?,
      ProcessKilled => write!(f, "Killed")?,
      ProcessInterrupted => write!(f, "Interrupted")?,
      ProcessExitedNormally => write!(f, "Exited(0)")?,
      ProcessExitedAbnormally(code) => write!(f, "Exited({code})")?,
      ProcessSignaled(signal) => write!(f, "Signaled({signal})")?,
      ProcessPaused => write!(f, "Paused due to breakpoint hit")?,
      ProcessDetached => write!(f, "Detached from tracexec")?,
      InternalError => write!(f, "An internal error occurred in tracexec")?,
    }
    Ok(())
  }
}

#[cfg(test)]
mod tests {
  use std::{
    collections::BTreeMap,
    sync::Arc,
  };

  use chrono::Local;
  use nix::unistd::Pid;
  use test_that::prelude::*;

  use super::*;
  use crate::{
    cache::ArcStr,
    timestamp::ts_from_boot_ns,
  };

  #[test]
  fn test_event_tracer_message_conversion() {
    let te = TracerEvent {
      details: TracerEventDetails::Info(TracerEventMessage {
        pid: Some(Pid::from_raw(1)),
        timestamp: Some(Local::now()),
        msg: "info".into(),
      }),
      id: EventId::new(0),
    };

    let tm: TracerMessage = te.clone().into();
    match tm {
      TracerMessage::Event(ev) => assert_eq!(ev, te),
      _ => panic!("Expected Event variant"),
    }
  }

  #[test]
  fn test_tracer_event_allocate_id_increments() {
    let id1 = TracerEvent::allocate_id();
    let id2 = TracerEvent::allocate_id();
    assert_that!(id2.into_inner(), gt(id1.into_inner()));
  }

  #[test]
  fn test_tracer_event_details_timestamp() {
    let ts = ts_from_boot_ns(100000);
    let msg = TracerEventMessage {
      pid: Some(Pid::from_raw(1)),
      timestamp: Some(Local::now()),
      msg: "msg".into(),
    };

    let info_detail = TracerEventDetails::Info(msg.clone());
    assert_eq!(info_detail.timestamp(), msg.timestamp);

    let exec_event = ExecEvent {
      syscall: ExecSyscall::Execve,
      exec_pid: Pid::from_raw(2),
      pid: Pid::from_raw(2),
      cwd: OutputMsg::Ok(ArcStr::from("/")),
      comm: ArcStr::from("comm"),
      filename: OutputMsg::Ok(ArcStr::from("file")),
      argv: Arc::new(Ok(vec![])),
      envp: Arc::new(Ok(BTreeMap::new())),
      has_dash_env: false,
      cred: Ok(Default::default()),
      interpreter: None,
      env_diff: Ok(EnvDiff::empty()),
      fdinfo: Arc::new(FileDescriptorInfoCollection::default()),
      result: 0,
      timestamp: ts,
      parent: None,
      cgroup: CgroupInfo::V2 {
        path: "/".to_string(),
      },
    };
    let exec_detail = TracerEventDetails::Exec(Box::new(exec_event));
    assert_eq!(exec_detail.timestamp(), Some(ts));
  }

  #[test]
  fn test_argv_to_string() {
    let argv_ok = Ok(vec![
      OutputMsg::Ok(ArcStr::from("arg1")),
      OutputMsg::Ok(ArcStr::from("arg2")),
    ]);
    let argv_err: Result<Vec<OutputMsg>, InspectError> = Err(InspectError::EPERM);

    let s = TracerEventDetails::argv_to_string(&argv_ok);
    assert_that!(s, contains_substring("arg1"));
    assert_that!(s, contains_substring("arg2"));

    let s_err = TracerEventDetails::argv_to_string(&argv_err);
    assert_eq!(s_err, "[failed to read argv]");
  }

  #[test]
  fn test_interpreters_to_string() {
    let none: Vec<Interpreter> = vec![];
    let one: Vec<Interpreter> = vec![Interpreter::None];
    let many: Vec<Interpreter> = vec![Interpreter::None, Interpreter::None];

    owo_colors::control::set_should_colorize(false);

    let s_none = TracerEventDetails::interpreters_to_string(&none);
    assert_eq!(s_none, "none");

    let s_one = TracerEventDetails::interpreters_to_string(&one);
    assert_eq!(s_one, "none");

    let s_many = TracerEventDetails::interpreters_to_string(&many);
    assert_that!(s_many, contains_substring("none"));
    assert_that!(s_many, contains_substring(","));
  }

  #[test]
  fn test_process_state_update_termination_timestamp() {
    let ts = ts_from_boot_ns(1000000);
    let exit = ProcessStateUpdate::Exit {
      status: ProcessExit::Code(0),
      timestamp: ts,
    };
    let detached = ProcessStateUpdate::Detached {
      hid: 1,
      timestamp: ts,
    };
    let resumed = ProcessStateUpdate::Resumed;

    assert_eq!(exit.termination_timestamp(), Some(ts));
    assert_eq!(detached.termination_timestamp(), Some(ts));
    assert_eq!(resumed.termination_timestamp(), None);
  }

  #[test]
  fn test_exec_syscall_display() {
    assert_eq!(ExecSyscall::Execve.to_string(), "execve");
    assert_eq!(ExecSyscall::Execveat.to_string(), "execveat");
  }

  #[test]
  fn test_event_status_display() {
    let cases = [
      (EventStatus::ExecENOENT, "⚠️ Exec failed"),
      (EventStatus::ProcessRunning, "🟢 Running"),
      (EventStatus::ProcessExitedNormally, "😇 Exited(0)"),
      (EventStatus::ProcessSegfault, "💥 Segmentation fault"),
    ];

    for (status, prefix) in cases {
      let s = format!("{}", status);
      assert!(s.starts_with(prefix.split_whitespace().next().unwrap()));
    }
  }
}