Skip to content

Commit

Permalink
CLI should laucnh probe __exec $cmd...
Browse files Browse the repository at this point in the history
This removes a dependence on coreutils `env`.
  • Loading branch information
charmoniumQ committed Jan 21, 2025
1 parent 4e306ad commit 486c2bc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions probe_src/frontend/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn main() -> Result<()> {
/* No more probe dump in Rust.
* See `probe export debug-text` in Python.
* */
Command::new("__gdb-exec-shim").hide(true).arg(
Command::new("__exec").hide(true).arg(
arg!(<CMD> ... "Command to run")
.required(true)
.trailing_var_arg(true)
Expand Down Expand Up @@ -143,7 +143,7 @@ fn main() -> Result<()> {
.and_then(|mut tar| transcribe::transcribe(input, &mut tar))
.wrap_err("Transcribe command failed")
}
Some(("__gdb-exec-shim", sub)) => {
Some(("__exec", sub)) => {
let cmd = sub
.get_many::<OsString>("CMD")
.unwrap()
Expand Down
28 changes: 22 additions & 6 deletions probe_src/frontend/cli/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,36 @@ impl Recorder {
ld_preload.push(&x);
}

let self_bin =
std::env::current_exe().wrap_err("Failed to get path to current executable")?;

let mut child = if self.gdb {
let mut dir_env = OsString::from("--init-eval-command=set environment __PROBE_DIR=");
dir_env.push(self.output.path());
let mut preload_env = OsString::from("--init-eval-command=set environment LD_PRELOAD=");
preload_env.push(ld_preload);

let self_bin =
std::env::current_exe().wrap_err("Failed to get path to current executable")?;
let mut copy_files_env =
OsString::from("--init-eval-command=set environment __PROBE_COPY_FILES=");
if self.copy_files_lazily {
copy_files_env.push("lazy");
} else if self.copy_files_eagerly {
copy_files_env.push("eager")
}
/* Yes, "set environment a=" will work to set a to null value
* in the case where neither copy_file_* option is activated
*
* gdb '--init-eval-command=set environment abcdef=' env -eval-command run -eval-command quit \
* | grep abcdef
*
* */

std::process::Command::new("gdb")
.arg(dir_env)
.arg(preload_env)
.arg(copy_files_env)
.arg("--args")
.arg(self_bin)
.arg("__gdb-exec-shim")
.arg("__exec")
.args(if self.copy_files_eagerly {
std::vec!["--copy-files-eagerly"]
} else if self.copy_files_lazily {
Expand All @@ -177,13 +192,14 @@ impl Recorder {
.spawn()
.wrap_err("Failed to launch gdb")?
} else {
/* We start `env $cmd` instead of `$cmd`
/* We start `probe __exec $cmd` instead of `$cmd`
* This is because PROBE is not able to capture the arguments of the very first process, but it does capture the arguments of any subsequent exec(...).
* Therefore, the "root process" is env, and the user's $cmd is exec(...)-ed.
* We could change this by adding argv and environ to InitProcessOp, but I think this solution is more elegant.
* Since the root process has special quirks, it should not be user's `$cmd`.
* */
std::process::Command::new("env")
std::process::Command::new(self_bin)
.arg("__exec")
.args(self.cmd)
.env_remove("__PROBE_LIB")
.env_remove("__PROBE_LOG")
Expand Down
3 changes: 2 additions & 1 deletion probe_src/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def bash(*cmds: str) -> list[str]:
modes = [
["probe", "record"],
["probe", "record", "--debug"],
["probe", "record", "--copy-files"],
["probe", "record", "--copy-files-lazily"],
#["probe", "record", "--copy-files-eagerly"],
]


Expand Down

0 comments on commit 486c2bc

Please sign in to comment.