Skip to content

Commit

Permalink
Address #229: Improve documentation and error output on MacOS when no…
Browse files Browse the repository at this point in the history
…t running as root (#303)
  • Loading branch information
zbentley authored Feb 12, 2024
1 parent 10dc842 commit 651bfbf
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
**/*.rs.bk
*svg
perf*

# IntelliJ IDE files
.idea/
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ echo -1 | sudo tee /proc/sys/kernel/perf_event_paranoid
### DTrace on macOS

On macOS, there is no alternative to running as superuser in order to
enable dtrace. The simplest way is to use `--root`, this way `rustc`
will be run normally but the binary will get run as superuser.
enable DTrace. This should be done by invoking `sudo flamegraph ...` or
`cargo flamegraph --root ...`. Do not do `sudo cargo flamegraph ...`;
this can cause problems due to Cargo's build system being run as root.

Be aware that if the binary being tested is user-aware, this does
change its behaviour.
Expand Down
7 changes: 7 additions & 0 deletions src/bin/cargo-flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,13 @@ fn main() -> anyhow::Result<()> {
Vec::new()
};

#[cfg(target_os = "macos")]
if let None = opt.graph.root {
return Err(anyhow!(
"DTrace requires elevated permissions on MacOS; re-invoke using 'cargo flamegraph --root ...'",
));
}

let artifacts = build(&opt, kind)?;
let workload = workload(&opt, &artifacts)?;
flamegraph::generate_flamegraph_for_workload(Workload::Command(workload), opt.graph)
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ pub struct Options {

/// Run with root privileges (using `sudo`). Accepts an optional argument containing command line options which will be passed to sudo
#[clap(long, value_name = "SUDO FLAGS")]
root: Option<Option<String>>,
pub root: Option<Option<String>>,

/// Sampling frequency
#[clap(short = 'F', long = "freq")]
Expand Down

0 comments on commit 651bfbf

Please sign in to comment.