From 3c6b9729d8f87b48ebfa577ce481cacb82aba2e4 Mon Sep 17 00:00:00 2001 From: Steffen Smolka Date: Tue, 28 Oct 2025 02:18:56 -0700 Subject: [PATCH] Make linker backend more portable by adding /usr/sbin to PATH. This can be helpful because certain build system (like Bazel) do not pass on environment variables in order to make builds more hermetic. Without this change, `usdt` is not compatible with such build systems. Context: I ran into this issue while adding Bazel BUILD rules to https://github.com/oxidecomputer/p4. --- usdt-impl/src/linker.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/usdt-impl/src/linker.rs b/usdt-impl/src/linker.rs index 21680368..4e55092d 100644 --- a/usdt-impl/src/linker.rs +++ b/usdt-impl/src/linker.rs @@ -71,6 +71,7 @@ use quote::{format_ident, quote}; use std::{ collections::BTreeMap, convert::TryFrom, + env, io::Write, process::{Command, Stdio}, }; @@ -315,7 +316,17 @@ fn contains_needle2<'a>(line: &'a str, needle: &str) -> Option<(&'a str, &'a str } fn build_header_from_provider(source: &str) -> Result { + // Ensure that `/usr/sbin` is on the PATH, since that is the typical + // location for `dtrace`, but some build systems (such as Bazel) reset + // the environment variables. + let mut path = env::var_os("PATH").unwrap_or_default(); + if !path.is_empty() { + path.push(":"); + } + path.push("/usr/sbin"); + let mut child = Command::new("dtrace") + .env("PATH", path) .arg("-h") .arg("-s") .arg("/dev/stdin")