|
1 | 1 | // Copyright (c) Microsoft Corporation. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
| 4 | +use std::process::{Command, Output, Stdio}; |
| 5 | +use std::sync::Arc; |
| 6 | +use std::time::Duration; |
| 7 | + |
| 8 | +use anyhow::Result; |
| 9 | +use debuggable_module::loader::Loader; |
| 10 | + |
| 11 | +use crate::allowlist::TargetAllowList; |
| 12 | +use crate::binary::BinaryCoverage; |
| 13 | + |
4 | 14 | #[cfg(target_os = "linux")] |
5 | 15 | pub mod linux; |
6 | 16 |
|
7 | 17 | #[cfg(target_os = "windows")] |
8 | 18 | pub mod windows; |
9 | 19 |
|
10 | | -#[cfg(target_os = "linux")] |
11 | | -pub use crate::record::linux::record; |
| 20 | +pub struct CoverageRecorder { |
| 21 | + allowlist: TargetAllowList, |
| 22 | + cmd: Command, |
| 23 | + loader: Arc<Loader>, |
| 24 | + timeout: Duration, |
| 25 | +} |
12 | 26 |
|
13 | | -#[cfg(target_os = "windows")] |
14 | | -pub use crate::record::windows::record; |
| 27 | +impl CoverageRecorder { |
| 28 | + pub fn new(mut cmd: Command) -> Self { |
| 29 | + cmd.stdout(Stdio::piped()); |
| 30 | + cmd.stderr(Stdio::piped()); |
| 31 | + |
| 32 | + let allowlist = TargetAllowList::default(); |
| 33 | + let loader = Arc::new(Loader::new()); |
| 34 | + let timeout = Duration::from_secs(5); |
| 35 | + |
| 36 | + Self { |
| 37 | + allowlist, |
| 38 | + cmd, |
| 39 | + loader, |
| 40 | + timeout, |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + pub fn allowlist(mut self, allowlist: TargetAllowList) -> Self { |
| 45 | + self.allowlist = allowlist; |
| 46 | + self |
| 47 | + } |
| 48 | + |
| 49 | + pub fn loader(mut self, loader: impl Into<Arc<Loader>>) -> Self { |
| 50 | + self.loader = loader.into(); |
| 51 | + self |
| 52 | + } |
| 53 | + |
| 54 | + pub fn timeout(mut self, timeout: Duration) -> Self { |
| 55 | + self.timeout = timeout; |
| 56 | + self |
| 57 | + } |
| 58 | + |
| 59 | + #[cfg(target_os = "linux")] |
| 60 | + pub fn record(self) -> Result<Recorded> { |
| 61 | + use linux::debugger::Debugger; |
| 62 | + use linux::LinuxRecorder; |
| 63 | + |
| 64 | + let loader = self.loader.clone(); |
| 65 | + |
| 66 | + crate::timer::timed(self.timeout, move || { |
| 67 | + let mut recorder = LinuxRecorder::new(&loader, self.allowlist); |
| 68 | + let dbg = Debugger::new(&mut recorder); |
| 69 | + let output = dbg.run(self.cmd)?; |
| 70 | + let coverage = recorder.coverage; |
| 71 | + |
| 72 | + Ok(Recorded { coverage, output }) |
| 73 | + })? |
| 74 | + } |
| 75 | + |
| 76 | + #[cfg(target_os = "windows")] |
| 77 | + pub fn record(self) -> Result<Recorded> { |
| 78 | + use debugger::Debugger; |
| 79 | + use windows::WindowsRecorder; |
| 80 | + |
| 81 | + let loader = self.loader.clone(); |
| 82 | + |
| 83 | + crate::timer::timed(self.timeout, move || { |
| 84 | + let mut recorder = WindowsRecorder::new(&loader, self.allowlist); |
| 85 | + let (mut dbg, child) = Debugger::init(self.cmd, &mut recorder)?; |
| 86 | + dbg.run(&mut recorder)?; |
| 87 | + |
| 88 | + let output = child.wait_with_output()?; |
| 89 | + let coverage = recorder.coverage; |
| 90 | + |
| 91 | + Ok(Recorded { coverage, output }) |
| 92 | + })? |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +#[derive(Clone, Debug)] |
| 97 | +pub struct Recorded { |
| 98 | + pub coverage: BinaryCoverage, |
| 99 | + pub output: Output, |
| 100 | +} |
0 commit comments