Skip to content

Commit 376d2fd

Browse files
Rollup merge of rust-lang#152457 - pmur:murp/mcount-link-pg, r=davidtwco
Pass -pg to linker when using -Zinstrument-mcount This selects a slightly different crt on gnu targets which enables the profiler within glibc. This makes using gprof a little easier with Rust binaries. Otherwise, rustc must be passed `-Clink-args=-pg` to ensure the correct startup code is linked.
2 parents 68978a1 + a4a5208 commit 376d2fd

4 files changed

Lines changed: 33 additions & 0 deletions

File tree

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,6 +2767,10 @@ fn add_order_independent_options(
27672767
cmd.pgo_gen();
27682768
}
27692769

2770+
if sess.opts.unstable_opts.instrument_mcount {
2771+
cmd.enable_profiling();
2772+
}
2773+
27702774
if sess.opts.cg.control_flow_guard != CFGuard::Disabled {
27712775
cmd.control_flow_guard();
27722776
}

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ pub(crate) trait Linker {
352352
fn add_no_exec(&mut self) {}
353353
fn add_as_needed(&mut self) {}
354354
fn reset_per_library_state(&mut self) {}
355+
fn enable_profiling(&mut self) {}
355356
}
356357

357358
impl dyn Linker + '_ {
@@ -732,6 +733,12 @@ impl<'a> Linker for GccLinker<'a> {
732733
self.link_or_cc_args(&["-u", "__llvm_profile_runtime"]);
733734
}
734735

736+
fn enable_profiling(&mut self) {
737+
// This flag is also used when linking to choose target specific
738+
// libraries needed to enable profiling.
739+
self.cc_arg("-pg");
740+
}
741+
735742
fn control_flow_guard(&mut self) {}
736743

737744
fn ehcont_guard(&mut self) {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello World!");
3+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// When building a binary instrumented with mcount, verify the
2+
// binary is linked with the correct crt to enable profiling.
3+
//
4+
//@ only-gnu
5+
//@ ignore-cross-compile
6+
7+
use run_make_support::{path, run, rustc};
8+
9+
fn main() {
10+
// Compile instrumentation enabled binary, and verify -pg is passed
11+
let link_args =
12+
rustc().input("main.rs").arg("-Zinstrument-mcount").print("link-args").run().stdout_utf8();
13+
assert!(link_args.contains("\"-pg\""));
14+
15+
// Run it, and verify gmon.out is created
16+
assert!(!path("gmon.out").exists());
17+
run("main");
18+
assert!(path("gmon.out").exists());
19+
}

0 commit comments

Comments
 (0)