tracexec-core 1.0.0

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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
use std::{
  borrow::Cow,
  fmt,
  num::ParseFloatError,
  path::{
    Path,
    PathBuf,
  },
};

use clap::{
  Args,
  ValueEnum,
};
use color_eyre::eyre::bail;
use enumflags2::BitFlags;
use snafu::{
  ResultExt,
  Snafu,
};

use super::{
  config::{
    DebuggerConfig,
    ExitHandling,
    LogModeConfig,
    ModifierConfig,
    PtraceConfig,
    TuiModeConfig,
  },
  keys::TuiKeyBindingsConfig,
  options::{
    ActivePane,
    AppLayout,
    SeccompBpf,
  },
};
use crate::{
  breakpoint::BreakPoint,
  cli::config::{
    ColorLevel,
    EnvDisplay,
    FileDescriptorDisplay,
  },
  event::TracerEventDetailsKind,
  timestamp::TimestampFormat,
};

#[derive(Args, Debug, Default, Clone)]
pub struct PtraceArgs {
  #[clap(long, help = "Controls whether to enable seccomp-bpf optimization, which greatly improves performance", default_value_t = SeccompBpf::Auto)]
  pub seccomp_bpf: SeccompBpf,
  #[clap(
    long,
    help = "Polling interval, in microseconds. -1(default) disables polling."
  )]
  pub polling_interval: Option<i64>,
}

#[derive(Args, Debug, Default, Clone)]
pub struct ModifierArgs {
  #[clap(long, help = "Only show successful calls", default_value_t = false)]
  pub successful_only: bool,
  #[clap(
    long,
    help = "[Experimental] Try to reproduce file descriptors in commandline. This might result in an unexecutable cmdline if pipes, sockets, etc. are involved.",
    default_value_t = false
  )]
  pub fd_in_cmdline: bool,
  #[clap(
    long,
    help = "[Experimental] Try to reproduce stdio in commandline. This might result in an unexecutable cmdline if pipes, sockets, etc. are involved.",
    default_value_t = false
  )]
  pub stdio_in_cmdline: bool,
  #[clap(long, help = "Resolve /proc/self/exe symlink", default_value_t = false)]
  pub resolve_proc_self_exe: bool,
  #[clap(
    long,
    help = "Do not resolve /proc/self/exe symlink",
    default_value_t = false,
    conflicts_with = "resolve_proc_self_exe"
  )]
  pub no_resolve_proc_self_exe: bool,
  #[clap(long, help = "Hide CLOEXEC fds", default_value_t = false)]
  pub hide_cloexec_fds: bool,
  #[clap(
    long,
    help = "Do not hide CLOEXEC fds",
    default_value_t = false,
    conflicts_with = "hide_cloexec_fds"
  )]
  pub no_hide_cloexec_fds: bool,
  #[clap(long, help = "Show timestamp information", default_value_t = false)]
  pub timestamp: bool,
  #[clap(
    long,
    help = "Do not show timestamp information",
    default_value_t = false,
    conflicts_with = "timestamp"
  )]
  pub no_timestamp: bool,
  #[clap(
    long,
    help = "Set the format of inline timestamp. See https://docs.rs/chrono/latest/chrono/format/strftime/index.html for available options."
  )]
  pub inline_timestamp_format: Option<TimestampFormat>,
  #[clap(long, help = "Collect cgroup information", default_value_t = false)]
  pub collect_cgroup: bool,
  #[clap(
    long,
    help = "Do not collect cgroup information",
    default_value_t = false,
    conflicts_with = "collect_cgroup"
  )]
  pub no_collect_cgroup: bool,
}

impl PtraceArgs {
  pub fn merge_config(&mut self, config: PtraceConfig) {
    // seccomp-bpf
    if let Some(setting) = config.seccomp_bpf
      && self.seccomp_bpf == SeccompBpf::Auto
    {
      self.seccomp_bpf = setting;
    }
  }
}

impl ModifierArgs {
  pub fn processed(mut self) -> Self {
    self.stdio_in_cmdline = self.fd_in_cmdline || self.stdio_in_cmdline;
    self.resolve_proc_self_exe = match (self.resolve_proc_self_exe, self.no_resolve_proc_self_exe) {
      (true, false) => true,
      (false, true) => false,
      _ => true, // default
    };
    self.hide_cloexec_fds = match (self.hide_cloexec_fds, self.no_hide_cloexec_fds) {
      (true, false) => true,
      (false, true) => false,
      _ => true, // default
    };
    self.timestamp = match (self.timestamp, self.no_timestamp) {
      (true, false) => true,
      (false, true) => false,
      _ => false, // default
    };
    self.collect_cgroup = match (self.collect_cgroup, self.no_collect_cgroup) {
      (true, false) => true,
      (false, true) => false,
      _ => false, // default
    };
    self
      .inline_timestamp_format
      .get_or_insert_with(TimestampFormat::default);
    self
  }

  pub fn merge_config(&mut self, config: ModifierConfig) {
    // false by default flags
    self.successful_only = self.successful_only || config.successful_only.unwrap_or_default();
    self.fd_in_cmdline |= config.fd_in_cmdline.unwrap_or_default();
    self.stdio_in_cmdline |= config.stdio_in_cmdline.unwrap_or_default();
    // flags that have negation counterparts
    if (!self.no_resolve_proc_self_exe) && (!self.resolve_proc_self_exe) {
      self.resolve_proc_self_exe = config.resolve_proc_self_exe.unwrap_or_default();
    }
    if (!self.no_hide_cloexec_fds) && (!self.hide_cloexec_fds) {
      self.hide_cloexec_fds = config.hide_cloexec_fds.unwrap_or_default();
    }
    if let Some(c) = config.timestamp {
      if (!self.timestamp) && (!self.no_timestamp) {
        self.timestamp = c.enable;
      }
      if self.inline_timestamp_format.is_none() {
        self.inline_timestamp_format = c.inline_format;
      }
    }
    if (!self.no_collect_cgroup) && (!self.collect_cgroup) {
      self.collect_cgroup = config.collect_cgroup.unwrap_or_default();
    }
  }
}

#[derive(Args, Debug)]
pub struct TracerEventArgs {
  // TODO:
  //   This isn't really compatible with logging mode
  #[clap(
    long,
    help = "Set the default filter to show all events. This option can be used in combination with --filter-exclude to exclude some unwanted events.",
    conflicts_with = "filter"
  )]
  pub show_all_events: bool,
  #[clap(
    long,
    help = "Set the default filter for events.",
    value_parser = tracer_event_filter_parser,
    default_value = "warning,error,exec,tracee-exit"
  )]
  pub filter: BitFlags<TracerEventDetailsKind>,
  #[clap(
    long,
    help = "Aside from the default filter, also include the events specified here.",
    required = false,
    value_parser = tracer_event_filter_parser,
    default_value_t = BitFlags::empty()
  )]
  pub filter_include: BitFlags<TracerEventDetailsKind>,
  #[clap(
    long,
    help = "Exclude the events specified here from the default filter.",
    value_parser = tracer_event_filter_parser,
    default_value_t = BitFlags::empty()
  )]
  pub filter_exclude: BitFlags<TracerEventDetailsKind>,
}

fn tracer_event_filter_parser(filter: &str) -> Result<BitFlags<TracerEventDetailsKind>, String> {
  let mut result = BitFlags::empty();
  if filter == "<empty>" {
    return Ok(result);
  }
  for f in filter.split(',') {
    let kind = TracerEventDetailsKind::from_str(f, false)?;
    if result.contains(kind) {
      return Err(format!(
        "Event kind '{kind}' is already included in the filter"
      ));
    }
    result |= kind;
  }
  Ok(result)
}

impl TracerEventArgs {
  pub fn all() -> Self {
    Self {
      show_all_events: true,
      filter: Default::default(),
      filter_include: Default::default(),
      filter_exclude: Default::default(),
    }
  }

  pub fn filter(&self) -> color_eyre::Result<BitFlags<TracerEventDetailsKind>> {
    let default_filter = if self.show_all_events {
      BitFlags::all()
    } else {
      self.filter
    };
    if self.filter_include.intersects(self.filter_exclude) {
      bail!("filter_include and filter_exclude cannot contain common events");
    }
    let mut filter = default_filter | self.filter_include;
    filter.remove(self.filter_exclude);
    Ok(filter)
  }
}

#[derive(Args, Debug, Default, Clone)]
pub struct LogModeArgs {
  #[clap(long, help = "More colors", conflicts_with = "less_colors")]
  pub more_colors: bool,
  #[clap(long, help = "Less colors", conflicts_with = "more_colors")]
  pub less_colors: bool,
  // BEGIN ugly: https://github.com/clap-rs/clap/issues/815
  #[clap(
    long,
    help = "Print commandline that (hopefully) reproduces what was executed. Note: file descriptors are not handled for now.",
    conflicts_with_all = ["show_env", "diff_env", "show_argv", "no_show_cmdline"]
  )]
  pub show_cmdline: bool,
  #[clap(
    long,
    help = "Don't print commandline that (hopefully) reproduces what was executed."
  )]
  pub no_show_cmdline: bool,
  #[clap(
    long,
    help = "Try to show script interpreter indicated by shebang",
    conflicts_with = "no_show_interpreter"
  )]
  pub show_interpreter: bool,
  #[clap(
    long,
    help = "Do not show script interpreter indicated by shebang",
    conflicts_with = "show_interpreter"
  )]
  pub no_show_interpreter: bool,
  #[clap(
    long,
    help = "Set the terminal foreground process group to tracee. This option is useful when tracexec is used interactively. [default]",
    conflicts_with = "no_foreground"
  )]
  pub foreground: bool,
  #[clap(
    long,
    help = "Do not set the terminal foreground process group to tracee",
    conflicts_with = "foreground"
  )]
  pub no_foreground: bool,
  #[clap(
    long,
    help = "Diff file descriptors with the original std{in/out/err}",
    conflicts_with = "no_diff_fd"
  )]
  pub diff_fd: bool,
  #[clap(
    long,
    help = "Do not diff file descriptors",
    conflicts_with = "diff_fd"
  )]
  pub no_diff_fd: bool,
  #[clap(long, help = "Show file descriptors", conflicts_with = "diff_fd")]
  pub show_fd: bool,
  #[clap(
    long,
    help = "Do not show file descriptors",
    conflicts_with = "show_fd"
  )]
  pub no_show_fd: bool,
  #[clap(
    long,
    help = "Diff environment variables with the original environment",
    conflicts_with = "no_diff_env",
    conflicts_with = "show_env",
    conflicts_with = "no_show_env"
  )]
  pub diff_env: bool,
  #[clap(
    long,
    help = "Do not diff environment variables",
    conflicts_with = "diff_env"
  )]
  pub no_diff_env: bool,
  #[clap(
    long,
    help = "Show environment variables",
    conflicts_with = "no_show_env",
    conflicts_with = "diff_env"
  )]
  pub show_env: bool,
  #[clap(
    long,
    help = "Do not show environment variables",
    conflicts_with = "show_env"
  )]
  pub no_show_env: bool,
  #[clap(long, help = "Show comm", conflicts_with = "no_show_comm")]
  pub show_comm: bool,
  #[clap(long, help = "Do not show comm", conflicts_with = "show_comm")]
  pub no_show_comm: bool,
  #[clap(long, help = "Show argv", conflicts_with = "no_show_argv")]
  pub show_argv: bool,
  #[clap(long, help = "Do not show argv", conflicts_with = "show_argv")]
  pub no_show_argv: bool,
  #[clap(long, help = "Show filename", conflicts_with = "no_show_filename")]
  pub show_filename: bool,
  #[clap(long, help = "Do not show filename", conflicts_with = "show_filename")]
  pub no_show_filename: bool,
  #[clap(long, help = "Show cwd", conflicts_with = "no_show_cwd")]
  pub show_cwd: bool,
  #[clap(long, help = "Do not show cwd", conflicts_with = "show_cwd")]
  pub no_show_cwd: bool,
  #[clap(long, help = "Decode errno values", conflicts_with = "no_decode_errno")]
  pub decode_errno: bool,
  #[clap(
    long,
    help = "Do not decode errno values",
    conflicts_with = "decode_errno"
  )]
  pub no_decode_errno: bool,
  // END ugly
}

impl LogModeArgs {
  pub fn foreground(&self) -> bool {
    match (self.foreground, self.no_foreground) {
      (false, true) => false,
      (true, false) => true,
      _ => true,
    }
  }

  pub fn merge_config(&mut self, config: LogModeConfig) {
    /// fallback to config value if both --x and --no-x are not set
    macro_rules! fallback {
      ($x:ident) => {
        ::paste::paste! {
          if (!self.$x) && (!self.[<no_ $x>]) {
            if let Some(x) = config.$x {
              if x {
                self.$x = true;
              } else {
                self.[<no_ $x>] = true;
              }
            }
          }
        }
      };
    }
    fallback!(show_interpreter);
    fallback!(foreground);
    fallback!(show_comm);
    fallback!(show_filename);
    fallback!(show_cwd);
    fallback!(decode_errno);
    match config.fd_display {
      Some(FileDescriptorDisplay::Show) => {
        if (!self.no_show_fd) && (!self.diff_fd) {
          self.show_fd = true;
        }
      }
      Some(FileDescriptorDisplay::Diff) => {
        if (!self.show_fd) && (!self.no_diff_fd) {
          self.diff_fd = true;
        }
      }
      Some(FileDescriptorDisplay::Hide) if (!self.diff_fd) && (!self.show_fd) => {
        self.no_diff_fd = true;
        self.no_show_fd = true;
      }
      _ => (),
    }
    fallback!(show_cmdline);
    if !self.show_cmdline {
      fallback!(show_argv);
      tracing::warn!("{}", self.show_argv);
      match config.env_display {
        Some(EnvDisplay::Show) => {
          if (!self.diff_env) && (!self.no_show_env) {
            self.show_env = true;
          }
        }
        Some(EnvDisplay::Diff) => {
          if (!self.show_env) && (!self.no_diff_env) {
            self.diff_env = true;
          }
        }
        Some(EnvDisplay::Hide) if (!self.show_env) && (!self.diff_env) => {
          self.no_diff_env = true;
          self.no_show_env = true;
        }
        _ => (),
      }
    }
    match config.color_level {
      Some(ColorLevel::Less) => {
        if !self.more_colors {
          self.less_colors = true;
        }
      }
      Some(ColorLevel::More) if !self.less_colors => {
        self.more_colors = true;
      }
      _ => (),
    }
  }
}

#[derive(Args, Debug, Default, Clone)]
pub struct TuiModeArgs {
  #[clap(
    long,
    help = "Do not allocate a pseudo terminal; redirect stdin/out/err to /dev/null"
  )]
  pub no_tty: bool,
  #[clap(long, short, help = "Keep the event list scrolled to the bottom")]
  pub follow: bool,
  #[clap(
    long,
    help = "Instead of waiting for the root child to exit, terminate when the TUI exits",
    conflicts_with = "kill_on_exit"
  )]
  pub terminate_on_exit: bool,
  #[clap(
    long,
    help = "Instead of waiting for the root child to exit, kill when the TUI exits"
  )]
  pub kill_on_exit: bool,
  #[clap(
    long,
    short = 'A',
    help = "Set the default active pane to use when TUI launches",
    conflicts_with = "no_tty"
  )]
  pub active_pane: Option<ActivePane>,
  #[clap(
    long,
    short = 'L',
    help = "Set the layout of the TUI when it launches",
    conflicts_with = "no_tty"
  )]
  pub layout: Option<AppLayout>,
  #[clap(
    long,
    short = 'F',
    help = "Set the frame rate of the TUI (60 by default)",
    value_parser = frame_rate_parser
  )]
  pub frame_rate: Option<f64>,
  #[clap(
    long,
    short = 'm',
    help = "Max number of events to keep in TUI (0=unlimited)"
  )]
  pub max_events: Option<u64>,
  #[clap(
    long,
    help = "Number of scrollback lines to keep in the pseudo terminal (1000 by default)",
    conflicts_with = "no_tty"
  )]
  pub scrollback_lines: Option<usize>,
  #[clap(
    long = "theme",
    help = "Path to a theme file to use for the TUI.",
    value_parser = theme_file_cli_parser,
  )]
  /// Tri-state source of theme file path: unset, CLI, or config.
  pub theme_file: Option<ThemeFileValue>,
  #[clap(skip)]
  pub theme: Option<Box<crate::cli::tui_theme::ThemeSpec>>,
  #[clap(skip)]
  pub keys: Option<Box<TuiKeyBindingsConfig>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ThemeFileValue {
  Cli(PathBuf),
  Config(PathBuf),
}

impl ThemeFileValue {
  pub fn as_deref(&self) -> &Path {
    match self {
      Self::Cli(path) | Self::Config(path) => path.as_path(),
    }
  }

  pub fn is_from_cli(&self) -> bool {
    matches!(self, Self::Cli(_))
  }
}

impl fmt::Display for ThemeFileValue {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    match self {
      Self::Cli(path) | Self::Config(path) => write!(f, "{}", path.display()),
    }
  }
}

#[derive(Args, Debug, Default, Clone)]
pub struct DebuggerArgs {
  #[clap(
    long,
    short = 'D',
    help = "Set the default external command to run when using \"Detach, Stop and Run Command\" feature in Hit Manager"
  )]
  pub default_external_command: Option<String>,
  #[clap(
    long = "add-breakpoint",
    short = 'b',
    value_parser = breakpoint_parser,
    help = "Add a new breakpoint to the tracer. This option can be used multiple times. The format is <syscall-stop>:<pattern-type>:<pattern>, where syscall-stop can be sysenter or sysexit, pattern-type can be argv-regex, in-filename or exact-filename. For example, sysexit:in-filename:/bash",
  )]
  pub breakpoints: Vec<BreakPoint>,
}

impl TuiModeArgs {
  pub const fn tty(&self) -> bool {
    !self.no_tty
  }

  /// Reject options that only make sense when a pseudo terminal is allocated.
  ///
  /// The explicit `--no-tty` flag is already rejected by clap's
  /// `conflicts_with`, but the effective mode can lack a pseudo terminal
  /// without that flag, e.g. in eBPF TUI mode when no command is given and
  /// the TUI traces all execs on the system. `--active-pane events` and
  /// `--layout` are not checked here: they are valid (and the former is
  /// what the TUI falls back to) without a pseudo terminal.
  pub fn validate_pty_options(&self, pty_allocated: bool) -> color_eyre::Result<()> {
    if pty_allocated {
      return Ok(());
    }
    if self.active_pane == Some(ActivePane::Terminal) {
      bail!(
        "--active-pane terminal requires a pseudo terminal, which is not allocated in this mode"
      );
    }
    if self.scrollback_lines.is_some() {
      bail!("--scrollback-lines requires a pseudo terminal, which is not allocated in this mode");
    }
    Ok(())
  }

  pub fn merge_config(&mut self, config: TuiModeConfig) {
    self.active_pane = self.active_pane.or(config.active_pane);
    self.layout = self.layout.or(config.layout);
    self.frame_rate = self.frame_rate.or(config.frame_rate);
    self.max_events = self.max_events.or(config.max_events);
    self.scrollback_lines = self.scrollback_lines.or(config.scrollback_lines);
    if self.theme_file.is_none()
      && let Some(path) = config.theme_file
    {
      self.theme_file = Some(ThemeFileValue::Config(path));
    }
    if self.theme.is_none() {
      self.theme = config.theme.map(Box::new);
    }
    self.follow |= config.follow.unwrap_or_default();
    if self.keys.is_none() {
      self.keys = config.keys.map(Box::new);
    }
    if (!self.terminate_on_exit) && (!self.kill_on_exit) {
      match config.exit_handling {
        Some(ExitHandling::Kill) => self.kill_on_exit = true,
        Some(ExitHandling::Terminate) => self.terminate_on_exit = true,
        _ => (),
      }
    }
  }
}

fn theme_file_cli_parser(s: &str) -> Result<ThemeFileValue, String> {
  if s.is_empty() {
    Err("theme file path cannot be empty".to_string())
  } else {
    Ok(ThemeFileValue::Cli(PathBuf::from(s)))
  }
}

impl DebuggerArgs {
  pub fn merge_config(&mut self, config: DebuggerConfig) {
    if self.default_external_command.is_none() {
      self.default_external_command = config.default_external_command;
    }
  }
}

fn frame_rate_parser(s: &str) -> Result<f64, ParseFrameRateError> {
  let v = s.parse::<f64>().with_context(|_| ParseFloatSnafu {
    value: s.to_string(),
  })?;
  if v < 0.0 || v.is_nan() || v.is_infinite() {
    Err(ParseFrameRateError::Invalid)
  } else if v < 5.0 {
    Err(ParseFrameRateError::TooLow)
  } else {
    Ok(v)
  }
}

fn breakpoint_parser(s: &str) -> Result<BreakPoint, Cow<'static, str>> {
  BreakPoint::try_from(s)
}

#[derive(Snafu, Debug)]
enum ParseFrameRateError {
  #[snafu(display("Failed to parse frame rate {value} as a floating point number"))]
  ParseFloat {
    source: ParseFloatError,
    value: String,
  },
  #[snafu(display("Invalid frame rate"))]
  Invalid,
  #[snafu(display("Frame rate too low, must be at least 5.0"))]
  TooLow,
}

#[derive(Args, Debug, Default, Clone)]
pub struct ExporterArgs {
  #[clap(short, long, help = "prettify the output if supported")]
  pub pretty: bool,
}

#[cfg(test)]
mod tests {
  use clap::Parser;
  use test_that::prelude::*;

  use super::*;

  // Helper wrapper so we can test clap parsing easily
  #[derive(Parser, Debug)]
  struct TestCli<T: Args + Clone + std::fmt::Debug> {
    #[clap(flatten)]
    args: T,
  }

  /* ----------------------------- PtraceArgs ----------------------------- */

  #[test]
  fn test_ptrace_args_merge_config() {
    let mut args = PtraceArgs {
      seccomp_bpf: SeccompBpf::Auto,
      polling_interval: None,
    };

    let cfg = PtraceConfig {
      seccomp_bpf: Some(SeccompBpf::On),
    };

    args.merge_config(cfg);
    assert_eq!(args.seccomp_bpf, SeccompBpf::On);
  }

  #[test]
  fn test_ptrace_args_cli_parse() {
    let cli = TestCli::<PtraceArgs>::parse_from(["test", "--polling-interval", "100"]);
    assert_eq!(cli.args.polling_interval, Some(100));
  }

  /* ---------------------------- ModifierArgs ----------------------------- */

  #[test]
  fn test_modifier_processed_defaults() {
    let args = ModifierArgs::default().processed();
    assert!(args.resolve_proc_self_exe);
    assert!(args.hide_cloexec_fds);
    assert!(!args.timestamp);
    assert!(args.inline_timestamp_format.is_some());
  }

  #[test]
  fn test_modifier_processed_fd_implies_stdio() {
    let args = ModifierArgs {
      fd_in_cmdline: true,
      ..Default::default()
    }
    .processed();

    assert!(args.stdio_in_cmdline);
  }

  #[test]
  fn test_modifier_merge_config() {
    let mut args = ModifierArgs::default();

    let cfg = ModifierConfig {
      successful_only: Some(true),
      fd_in_cmdline: Some(true),
      stdio_in_cmdline: None,
      resolve_proc_self_exe: Some(false),
      hide_cloexec_fds: Some(false),
      timestamp: None,
      seccomp_bpf: None,
      collect_cgroup: None,
    };

    args.merge_config(cfg);

    assert!(args.successful_only);
    assert!(args.fd_in_cmdline);
    assert!(!args.resolve_proc_self_exe);
    assert!(!args.hide_cloexec_fds);
  }

  #[test]
  fn test_modifier_args_cli_overrides_config_positive() {
    let mut args = ModifierArgs {
      resolve_proc_self_exe: true, // CLI explicitly enables
      ..Default::default()
    };

    let cfg = ModifierConfig {
      resolve_proc_self_exe: Some(false),
      ..Default::default()
    };

    args.merge_config(cfg);

    assert!(args.resolve_proc_self_exe);
  }

  #[test]
  fn test_modifier_args_cli_no_flag_blocks_config() {
    let mut args = ModifierArgs {
      no_hide_cloexec_fds: true, // CLI explicitly disables
      ..Default::default()
    };

    let cfg = ModifierConfig {
      hide_cloexec_fds: Some(true),
      ..Default::default()
    };

    args.merge_config(cfg);

    assert!(!args.hide_cloexec_fds);
  }

  #[test]
  fn test_modifier_cli_parse_conflicts() {
    let cli = TestCli::<ModifierArgs>::parse_from(["test", "--no-timestamp"]);

    let processed = cli.args.processed();
    assert!(!processed.timestamp);
  }

  #[test]
  fn test_modifier_args_timestamp_cli_overrides_config() {
    let mut args = ModifierArgs {
      timestamp: true,
      ..Default::default()
    };

    let cfg = ModifierConfig {
      timestamp: Some(crate::cli::config::TimestampConfig {
        enable: false,
        inline_format: None,
      }),
      ..Default::default()
    };

    args.merge_config(cfg);

    assert!(args.timestamp);
  }

  /* -------------------------- TracerEventArgs ---------------------------- */

  #[test]
  fn test_tracer_event_filter_parser_basic() {
    let f = tracer_event_filter_parser("warning,error").unwrap();
    assert!(f.contains(TracerEventDetailsKind::Warning));
    assert!(f.contains(TracerEventDetailsKind::Error));
  }

  #[test]
  fn test_tracer_event_filter_duplicate() {
    let err = tracer_event_filter_parser("warning,warning").unwrap_err();
    assert_that!(err, contains_substring("already included"));
  }

  #[test]
  fn test_tracer_event_args_all() {
    let args = TracerEventArgs::all();
    let f = args.filter().unwrap();
    assert_eq!(f, BitFlags::all());
  }

  #[test]
  fn test_tracer_event_include_exclude_conflict() {
    let args = TracerEventArgs {
      show_all_events: false,
      filter: BitFlags::empty(),
      filter_include: TracerEventDetailsKind::Error.into(),
      filter_exclude: TracerEventDetailsKind::Error.into(),
    };

    assert_that!(args.filter(), err(anything()));
  }

  /* ---------------------------- LogModeArgs ------------------------------ */

  #[test]
  fn test_logmode_foreground_logic() {
    let args = LogModeArgs {
      foreground: false,
      no_foreground: true,
      ..Default::default()
    };
    assert!(!args.foreground());

    let args = LogModeArgs {
      foreground: true,
      no_foreground: false,
      ..Default::default()
    };
    assert!(args.foreground());
  }

  #[test]
  fn test_logmode_merge_color_config() {
    let mut args = LogModeArgs::default();

    let cfg = LogModeConfig {
      color_level: Some(ColorLevel::Less),
      ..Default::default()
    };

    args.merge_config(cfg);
    assert!(args.less_colors);
  }

  #[test]
  fn test_logmode_fd_display_config() {
    let mut args = LogModeArgs::default();

    let cfg = LogModeConfig {
      fd_display: Some(FileDescriptorDisplay::Show),
      ..Default::default()
    };

    args.merge_config(cfg);
    assert!(args.show_fd);
  }

  #[test]
  fn test_logmode_cli_parse() {
    let cli = TestCli::<LogModeArgs>::parse_from(["test", "--show-cmdline", "--show-interpreter"]);

    assert!(cli.args.show_cmdline);
    assert!(cli.args.show_interpreter);
  }

  #[test]
  fn test_logmode_cli_no_foreground_overrides_config() {
    let mut args = LogModeArgs {
      no_foreground: true,
      ..Default::default()
    };

    let cfg = LogModeConfig {
      foreground: Some(true),
      ..Default::default()
    };

    args.merge_config(cfg);

    assert!(!args.foreground());
  }

  #[test]
  fn test_logmode_cli_show_fd_overrides_config_hide() {
    let mut args = LogModeArgs {
      show_fd: true,
      ..Default::default()
    };

    let cfg = LogModeConfig {
      fd_display: Some(FileDescriptorDisplay::Hide),
      ..Default::default()
    };

    args.merge_config(cfg);

    assert!(args.show_fd);
    assert!(!args.no_show_fd);
  }

  #[test]
  fn test_logmode_cli_color_overrides_config() {
    let mut args = LogModeArgs {
      more_colors: true,
      ..Default::default()
    };

    let cfg = LogModeConfig {
      color_level: Some(ColorLevel::Less),
      ..Default::default()
    };

    args.merge_config(cfg);

    assert!(args.more_colors);
    assert!(!args.less_colors);
  }

  /* ----------------------------- TuiModeArgs ----------------------------- */

  #[test]
  fn test_tui_merge_config_exit_handling() {
    let mut args = TuiModeArgs::default();

    let cfg = TuiModeConfig {
      exit_handling: Some(ExitHandling::Kill),
      follow: Some(true),
      theme_file: Some(PathBuf::from("high-contrast.toml")),
      ..Default::default()
    };

    args.merge_config(cfg);

    assert!(args.kill_on_exit);
    assert!(args.follow);
    assert_eq!(
      args.theme_file,
      Some(ThemeFileValue::Config(PathBuf::from("high-contrast.toml")))
    );
  }

  #[test]
  fn test_tui_merge_config_theme_file_from_cli() {
    let mut args = TuiModeArgs {
      theme_file: Some(ThemeFileValue::Cli(PathBuf::from("cli.toml"))),
      ..Default::default()
    };
    let cfg = TuiModeConfig {
      theme_file: Some(PathBuf::from("config.toml")),
      ..Default::default()
    };

    args.merge_config(cfg);

    // CLI value wins and keeps CLI provenance.
    assert_eq!(
      args.theme_file,
      Some(ThemeFileValue::Cli(PathBuf::from("cli.toml")))
    );
  }

  #[test]
  fn test_tui_parse_theme_file_from_cli() {
    let args = TestCli::<TuiModeArgs>::parse_from(["test", "--theme", "cli.toml"]).args;
    assert_eq!(
      args.theme_file,
      Some(ThemeFileValue::Cli(PathBuf::from("cli.toml")))
    );
  }

  #[test]
  fn test_tui_parse_theme_file_unset_by_default() {
    let args = TestCli::<TuiModeArgs>::parse_from(["test"]).args;
    assert_eq!(args.theme_file, None);
  }

  #[test]
  fn test_tui_merge_config_inline_theme() {
    use crate::cli::tui_theme::{
      StyleSpec,
      ThemeColor,
      ThemeSpec,
    };
    let mut args = TuiModeArgs::default();
    let cfg = TuiModeConfig {
      theme: Some(ThemeSpec {
        app_title: Some(StyleSpec {
          fg: Some(ThemeColor::Named("cyan".into())),
          ..Default::default()
        }),
        ..Default::default()
      }),
      ..Default::default()
    };

    args.merge_config(cfg);

    assert!(matches!(
      args
        .theme
        .as_ref()
        .and_then(|s| s.app_title.as_ref())
        .and_then(|a| a.fg.as_ref()),
      Some(ThemeColor::Named(s)) if s == "cyan"
    ));
  }

  #[test]
  fn test_tui_validate_pty_options() {
    // Options that require a pseudo terminal are accepted when one is allocated.
    let args = TuiModeArgs {
      active_pane: Some(crate::cli::options::ActivePane::Terminal),
      layout: Some(crate::cli::options::AppLayout::Vertical),
      scrollback_lines: Some(2000),
      ..Default::default()
    };
    assert!(args.validate_pty_options(true).is_ok());

    // `--active-pane terminal` is rejected when the effective mode allocates none.
    assert_that!(args.validate_pty_options(false), err(anything()));
    assert_that!(
      TuiModeArgs {
        scrollback_lines: Some(1000),
        ..Default::default()
      }
      .validate_pty_options(false),
      err(anything())
    );
    // `--active-pane events`, `--layout`, and defaults stay valid without a
    // pseudo terminal.
    assert!(
      TuiModeArgs {
        active_pane: Some(crate::cli::options::ActivePane::Events),
        ..Default::default()
      }
      .validate_pty_options(false)
      .is_ok()
    );
    assert!(
      TuiModeArgs {
        layout: Some(crate::cli::options::AppLayout::Vertical),
        ..Default::default()
      }
      .validate_pty_options(false)
      .is_ok()
    );
    assert!(TuiModeArgs::default().validate_pty_options(false).is_ok());
  }

  #[test]
  fn test_tui_cli_parse() {
    let cli = TestCli::<TuiModeArgs>::parse_from(["test", "--follow", "--frame-rate", "30"]);

    assert!(cli.args.tty());
    assert!(!cli.args.no_tty);
    assert!(cli.args.follow);
    assert_eq!(cli.args.frame_rate, Some(30.0));
  }

  #[test]
  fn test_tui_cli_parse_no_tty() {
    let cli = TestCli::<TuiModeArgs>::parse_from(["test", "--no-tty"]);

    assert!(!cli.args.tty());
    assert!(cli.args.no_tty);
  }

  #[test]
  fn test_tui_cli_no_tty_conflicts_with_terminal_options() {
    let result =
      TestCli::<TuiModeArgs>::try_parse_from(["test", "--no-tty", "--active-pane", "terminal"]);

    assert_that!(result, err(anything()));
  }

  #[test]
  fn test_tui_cli_exit_handling_overrides_config() {
    let mut args = TuiModeArgs {
      terminate_on_exit: true,
      ..Default::default()
    };

    let cfg = TuiModeConfig {
      exit_handling: Some(ExitHandling::Kill),
      ..Default::default()
    };

    args.merge_config(cfg);

    assert!(args.terminate_on_exit);
    assert!(!args.kill_on_exit);
  }

  /* --------------------------- DebuggerArgs ------------------------------ */

  #[test]
  fn test_debugger_merge_config() {
    let mut args = DebuggerArgs::default();

    let cfg = DebuggerConfig {
      default_external_command: Some("echo hi".into()),
    };

    args.merge_config(cfg);
    assert_eq!(args.default_external_command.as_deref(), Some("echo hi"));
  }

  #[test]
  fn test_debugger_cli_parse_breakpoint() {
    let cli = TestCli::<DebuggerArgs>::parse_from([
      "test",
      "--add-breakpoint",
      "sysenter:exact-filename:/bin/ls",
    ]);

    assert_eq!(cli.args.breakpoints.len(), 1);
  }

  #[test]
  fn test_debugger_cli_command_overrides_config() {
    let mut args = DebuggerArgs {
      default_external_command: Some("cli-cmd".into()),
      ..Default::default()
    };

    let cfg = DebuggerConfig {
      default_external_command: Some("config-cmd".into()),
    };

    args.merge_config(cfg);

    assert_eq!(args.default_external_command.as_deref(), Some("cli-cmd"));
  }

  /* ------------------------- frame_rate_parser --------------------------- */

  #[test]
  fn test_frame_rate_parser_valid() {
    assert_eq!(frame_rate_parser("60").unwrap(), 60.0);
  }

  #[test]
  fn test_frame_rate_parser_too_low() {
    let err = frame_rate_parser("1").unwrap_err();
    let msg = err.to_string();
    assert_that!(msg, contains_substring("too low"));
  }

  #[test]
  fn test_frame_rate_parser_invalid() {
    let err = frame_rate_parser("-1").unwrap_err();
    assert_that!(err.to_string(), contains_substring("Invalid"));
  }

  /* ----------------------------- ExporterArgs ---------------------------- */

  #[test]
  fn test_exporter_cli_parse() {
    let cli = TestCli::<ExporterArgs>::parse_from(["test", "--pretty"]);

    assert!(cli.args.pretty);
  }
}