tracexec 0.8.2

Tracer for execve{,at} and pre-exec behavior, launcher for debuggers.
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
use std::{
  collections::{BTreeMap, HashMap},
  process::Stdio,
  sync::Arc,
};

use color_eyre::{eyre::eyre, Section};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use either::Either;
use itertools::{chain, Itertools};
use nix::{sys::signal::Signal, unistd::Pid};
use ratatui::{
  layout::{Alignment, Constraint, Layout},
  prelude::{Buffer, Rect},
  style::{Modifier, Style, Stylize},
  text::{Line, Span},
  widgets::{Block, Borders, Clear, Paragraph, StatefulWidget, Widget, Wrap},
};
use tracing::{debug, trace};
use tui_prompts::{State, TextPrompt, TextState};
use tui_widget_list::{ListBuilder, ListView};

use crate::{
  action::Action,
  tracer::{state::BreakPointStop, BreakPointHit, Tracer},
};

use super::{
  error_popup::InfoPopupState,
  help::{cli_flag, help_item, help_key},
  theme::THEME,
};

#[derive(Debug, Clone)]
struct BreakPointHitEntry {
  bid: u32,
  pid: Pid,
  stop: BreakPointStop,
  breakpoint_pattern: Option<String>,
}

impl BreakPointHitEntry {
  fn paragraph(&self, selected: bool) -> Paragraph<'static> {
    let space = Span::from(" ");
    let line = Line::default()
      .spans(vec![
        Span::styled(self.pid.to_string(), THEME.hit_entry_pid),
        space.clone(),
        Span::styled("hit", THEME.hit_entry_plain_text),
        space.clone(),
        Span::styled(
          format!("breakpoint #{}", self.bid),
          THEME.hit_entry_plain_text,
        ),
        Span::raw("("),
        self.breakpoint_pattern.as_ref().map_or_else(
          || Span::styled("deleted", THEME.hit_entry_no_breakpoint_pattern),
          |pattern| Span::styled(pattern.clone(), THEME.hit_entry_breakpoint_pattern),
        ),
        Span::raw(")"),
        space.clone(),
        Span::styled("at", THEME.hit_entry_plain_text),
        space.clone(),
        Span::styled(
          <&'static str>::from(self.stop),
          THEME.hit_entry_breakpoint_stop,
        ),
      ])
      .style(if selected {
        Style::default().add_modifier(Modifier::REVERSED)
      } else {
        Style::default()
      });
    Paragraph::new(line).wrap(Wrap { trim: true })
  }

  fn hit(&self) -> BreakPointHit {
    BreakPointHit {
      bid: self.bid,
      pid: self.pid,
      stop: self.stop,
    }
  }
}

#[derive(Debug, Clone)]
pub enum DetachReaction {
  LaunchExternal(String),
}

#[derive(PartialEq, Clone, Copy)]
enum EditingTarget {
  DefaultCommand,
  CustomCommand { selection: usize },
}

pub struct HitManagerState {
  tracer: Arc<Tracer>,
  counter: u64,
  hits: BTreeMap<u64, BreakPointHitEntry>,
  pending_detach_reactions: HashMap<u64, DetachReaction>,
  list_state: tui_widget_list::ListState,
  pub visible: bool,
  default_external_command: Option<String>,
  editing: Option<EditingTarget>,
  editor_state: TextState<'static>,
}

impl HitManagerState {
  pub fn new(
    tracer: Arc<Tracer>,
    default_external_command: Option<String>,
  ) -> color_eyre::Result<Self> {
    Ok(Self {
      tracer,
      counter: 0,
      hits: BTreeMap::new(),
      pending_detach_reactions: HashMap::new(),
      list_state: tui_widget_list::ListState::default(),
      visible: false,
      default_external_command,
      editing: None,
      editor_state: TextState::new(),
    })
  }

  pub fn count(&self) -> usize {
    self.hits.len()
  }

  pub fn hide(&mut self) {
    self.visible = false;
    self.editing = None;
  }

  pub fn help(&self) -> impl Iterator<Item = Span> {
    if self.editing.is_none() {
      Either::Left(chain!(
        [
          help_item!("Q", "Back"),
          help_item!("R", "Resume\u{00a0}Process"),
          help_item!("D", "Detach\u{00a0}Process"),
          help_item!("E", "Edit\u{00a0}Default\u{00a0}Command")
        ],
        if self.default_external_command.is_some() {
          Some(help_item!(
            "Enter",
            "Detach,\u{00a0}Stop\u{00a0}and\u{00a0}Run\u{00a0}Default\u{00a0}Command"
          ))
        } else {
          None
        },
        [help_item!(
          "Alt+Enter",
          "Detach,\u{00a0}Stop\u{00a0}and\u{00a0}Run\u{00a0}Command"
        ),]
      ))
    } else {
      Either::Right(chain!([
        help_item!("Enter", "Save"),
        help_item!("Ctrl+U", "Clear"),
        help_item!("Esc/Ctrl+C", "Cancel"),
      ],))
    }
    .into_iter()
    .flatten()
  }

  fn close_when_empty(&self) -> Option<Action> {
    if self.hits.is_empty() {
      Some(Action::HideHitManager)
    } else {
      None
    }
  }

  pub fn handle_key_event(&mut self, key: KeyEvent) -> Option<Action> {
    if key.code == KeyCode::F(1) && key.modifiers == KeyModifiers::NONE {
      return Some(Action::SetActivePopup(
        crate::action::ActivePopup::InfoPopup(HitManager::help()),
      ));
    }
    if let Some(editing) = self.editing {
      match key.code {
        KeyCode::Enter => {
          if key.modifiers == KeyModifiers::NONE {
            if self.editor_state.value().trim().is_empty() {
              return Some(Action::show_error_popup(
                "Error".to_string(),
                eyre!("Command cannot be empty or whitespace"),
              ));
            }
            self.editing = None;
            match editing {
              EditingTarget::DefaultCommand => {
                self.default_external_command = Some(self.editor_state.value().to_string())
              }
              EditingTarget::CustomCommand { selection } => {
                self.select_near_by(selection);
                let hid = *self.hits.keys().nth(selection).unwrap();
                if let Err(e) =
                  self.detach_pause_and_launch_external(hid, self.editor_state.value().to_string())
                {
                  return Some(Action::show_error_popup(
                    "Error".to_string(),
                    e.with_note(|| "Failed to detach or launch external command"),
                  ));
                }
                return self.close_when_empty();
              }
            }
            return None;
          }
        }
        KeyCode::Esc => {
          self.editing = None;
          return None;
        }
        KeyCode::Char('c') if key.modifiers == KeyModifiers::CONTROL => {
          self.editing = None;
          return None;
        }
        _ => {
          self.editor_state.handle_key_event(key);
          return None;
        }
      }
      return None;
    }
    if key.modifiers == KeyModifiers::NONE {
      match key.code {
        KeyCode::Char('q') => return Some(Action::HideHitManager),
        KeyCode::Down | KeyCode::Char('j') => {
          self.list_state.next();
        }
        KeyCode::Up | KeyCode::Char('k') => {
          self.list_state.previous();
        }
        KeyCode::Char('d') => {
          if let Some(selected) = self.list_state.selected {
            self.select_near_by(selected);
            let hid = *self.hits.keys().nth(selected).unwrap();
            if let Err(e) = self.detach(hid) {
              return Some(Action::show_error_popup("Detach failed".to_string(), e));
            };
            return self.close_when_empty();
          }
        }
        KeyCode::Char('e') => {
          self.editing = Some(EditingTarget::DefaultCommand);
          if let Some(command) = self.default_external_command.clone() {
            self.editor_state = TextState::new().with_value(command);
            self.editor_state.move_end();
          }
        }
        KeyCode::Enter => {
          if let Some(selected) = self.list_state.selected {
            let external_command = self.default_external_command.clone()?;
            self.select_near_by(selected);
            let hid = *self.hits.keys().nth(selected).unwrap();
            // "konsole --hold -e gdb -p {{PID}}".to_owned()
            if let Err(e) = self.detach_pause_and_launch_external(hid, external_command) {
              return Some(Action::show_error_popup(
                "Error".to_string(),
                e.with_note(|| "Failed to detach or launch external command"),
              ));
            }
            return self.close_when_empty();
          }
        }
        KeyCode::Char('r') => {
          if let Some(selected) = self.list_state.selected {
            debug!("selected: {}", selected);
            self.select_near_by(selected);
            let hid = *self.hits.keys().nth(selected).unwrap();
            if let Err(e) = self.resume(hid) {
              return Some(Action::show_error_popup("Resume failed".to_string(), e));
            }
            return self.close_when_empty();
          }
        }
        _ => {}
      }
    } else if key.code == KeyCode::Enter && key.modifiers == KeyModifiers::ALT {
      if let Some(selected) = self.list_state.selected {
        self.editing = Some(EditingTarget::CustomCommand {
          selection: selected,
        });
      }
    }
    None
  }

  pub fn add_hit(&mut self, hit: BreakPointHit) -> u64 {
    let id = self.counter;
    let BreakPointHit { bid, pid, stop } = hit;
    self.hits.insert(
      id,
      BreakPointHitEntry {
        bid,
        pid,
        stop,
        breakpoint_pattern: self.tracer.get_breakpoint_pattern_string(bid),
      },
    );
    self.counter += 1;
    id
  }

  fn select_near_by(&mut self, old: usize) {
    if old > 0 {
      self.list_state.select(Some(old - 1));
    } else if old + 1 < self.hits.len() {
      self.list_state.select(Some(old));
    } else {
      self.list_state.select(None);
    }
  }

  pub fn detach(&mut self, hid: u64) -> color_eyre::Result<()> {
    if let Some(hit) = self.hits.remove(&hid) {
      self.tracer.request_process_detach(hit.hit(), None, hid)?;
    }
    Ok(())
  }

  pub fn resume(&mut self, hid: u64) -> color_eyre::Result<()> {
    if let Some(hit) = self.hits.remove(&hid) {
      self.tracer.request_process_resume(hit.hit())?;
    }
    Ok(())
  }

  pub fn detach_pause_and_launch_external(
    &mut self,
    hid: u64,
    cmdline_template: String,
  ) -> color_eyre::Result<()> {
    trace!("detaching, pausing and launching external command for hit={hid}");
    if let Some(hit) = self.hits.remove(&hid) {
      trace!(
        "detaching, pausing and launching external command for hit={hid}, pid={}",
        hit.pid
      );
      self
        .pending_detach_reactions
        .insert(hid, DetachReaction::LaunchExternal(cmdline_template));
      self
        .tracer
        .request_process_detach(hit.hit(), Some(Signal::SIGSTOP.into()), hid)?;
    }
    Ok(())
  }

  pub fn react_on_process_detach(&mut self, hid: u64, pid: Pid) -> color_eyre::Result<()> {
    debug!(
      "reacting on process {pid}(hid: {hid}) detach, reactions: {:?}",
      self.pending_detach_reactions
    );
    if let Some(reaction) = self.pending_detach_reactions.remove(&hid) {
      trace!("reacting on process {pid} detach: {reaction:?}");
      match reaction {
        DetachReaction::LaunchExternal(cmd) => {
          let cmd = shell_words::split(&cmd.replace("{{PID}}", &pid.to_string()))?;
          tokio::process::Command::new(&cmd[0])
            .args(&cmd[1..])
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()?;
        }
      }
    }
    Ok(())
  }

  fn seccomp_bpf_warning(&self) -> Paragraph<'static> {
    let space = Span::raw(" ");
    let line1 = Line::default().spans(vec![
      " WARNING ".on_light_red().white().bold(),
      space.clone(),
      "seccomp-bpf optimization is enabled. ".into(),
      "Detached tracees and their children will not be able to use execve{,at} syscall. "
        .light_red(),
      "If the tracee to be detached need to exec other programs, ".into(),
      "please run tracexec with ".cyan().bold(),
      cli_flag("--seccomp-bpf=off"),
      ".".into(),
    ]);
    Paragraph::new(vec![line1]).wrap(Wrap { trim: false })
  }

  pub fn cursor(&self) -> Option<(u16, u16)> {
    if self.editing.is_some() {
      Some(self.editor_state.cursor())
    } else {
      None
    }
  }
}

pub struct HitManager;

impl HitManager {}

impl StatefulWidget for HitManager {
  type State = HitManagerState;

  fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
    let help_area = Rect {
      x: buf.area.width.saturating_sub(10),
      y: 0,
      width: 10.min(buf.area.width),
      height: 1,
    };
    Clear.render(help_area, buf);
    Line::default()
      .spans(help_item!("F1", "Help"))
      .render(help_area, buf);
    let editor_area = Rect {
      x: 0,
      y: 1,
      width: buf.area.width,
      height: 1,
    };
    Clear.render(editor_area, buf);
    if let Some(editing) = state.editing {
      let editor = TextPrompt::new(
        match editing {
          EditingTarget::DefaultCommand => "default command",
          EditingTarget::CustomCommand { .. } => "command",
        }
        .into(),
      );
      editor.render(editor_area, buf, &mut state.editor_state);
    } else if let Some(command) = state.default_external_command.as_deref() {
      let line = Line::default().spans(vec![
        Span::raw("🚀 default command: "),
        Span::styled(command, THEME.hit_manager_default_command),
      ]);
      line.render(editor_area, buf);
    } else {
      let line = Line::default().spans(vec![
        Span::styled(
          "default command not set. Press ",
          THEME.hit_manager_no_default_command,
        ),
        help_key("E"),
        Span::styled(" to set", THEME.hit_manager_no_default_command),
      ]);
      line.render(editor_area, buf);
    }

    Clear.render(area, buf);
    let block = Block::new()
      .title(" Hit Manager ")
      .borders(Borders::ALL)
      .title_alignment(Alignment::Center);
    let items = state.hits.values().cloned().collect_vec();
    let builder = ListBuilder::new(move |ctx| {
      let item = &items[ctx.index];
      let paragraph = item.paragraph(ctx.is_selected);
      let line_count = paragraph
        .line_count(ctx.cross_axis_size)
        .try_into()
        .unwrap_or(u16::MAX);
      (paragraph, line_count)
    });
    let list = ListView::new(builder, state.hits.len());

    if !state.hits.is_empty() && state.list_state.selected.is_none() {
      state.list_state.select(Some(0));
    }

    if state.tracer.seccomp_bpf() {
      let warning = state.seccomp_bpf_warning();
      let warning_height = warning.line_count(area.width) as u16;
      let [warning_area, list_area] =
        Layout::vertical([Constraint::Length(warning_height), Constraint::Min(0)]).areas(area);
      warning.render(warning_area, buf);
      let inner = block.inner(list_area);
      block.render(list_area, buf);
      list.render(inner, buf, &mut state.list_state);
    } else {
      let inner = block.inner(area);
      block.render(area, buf);
      list.render(inner, buf, &mut state.list_state);
    }
  }
}

impl HitManager {
  fn help() -> InfoPopupState {
    InfoPopupState::info(
      "Help".to_string(),
      vec![
        Line::default().spans(vec![
          "The Hit Manager shows the processes that hit breakpoints and become stopped by tracexec. A process can stop at "
            .into(),
          "syscall-enter(right before exec)".cyan().bold(),
          " or ".into(),
          "syscall-exit(right after exec)".cyan().bold(),
          ". ".into(),
        ]),
        #[cfg(feature = "seccomp-bpf")]
        Line::default().spans(vec![
          "By default, tracexec uses seccomp-bpf to speed up ptrace operations so that there is minimal overhead \
          when running programs inside tracexec. ".into(),
          "However, this comes with a limitation that detached tracees and their children will not be able to use \
          execve{,at} syscall. Usually it is shown as the following error: ".red(),
          "Function not implemented".light_red().bold(),
          ". To workaround this problem, run tracexec with ".into(),
          cli_flag("--seccomp-bpf=off"),
          " flag. ".into(),
        ]),
        Line::default().spans(vec![
          "You can detach, resume or detach and launch external commands for the stopped processes. The ".into(),
          "{{PID}}".cyan().bold(),
          " parameter in the external command will be replaced with the PID of the detached and stopped process. ".into(),
          "For example, you can detach a process and launch a debugger to attach to it. \
          Usually you would want to open a new terminal emulator like: ".into(),
          "konsole --hold -e gdb -p {{PID}}".cyan().bold(),
          ". ".into(),
          "This feature is especially useful when you want to debug a subprocess that is executed from a shell script\
          (which might use pipes as stdio) or another complex software.".into(),
          "It is worth mentioning that even if the process is stopped at syscall-enter stop, by the time a debugger \
          attaches to the process, the process should be already past the syscall-exit stop.".into(),
        ]),
      ],
    )
  }
}