13

Is it possible to check if given program was compiled with GNU gprof instrumentation, i.e. with '-pg' flag passed to both compiler and linker, without running it to check if it would generate a gmon.out file?

Caleb
71.9k19 gold badges202 silver badges232 bronze badges
asked Jun 6, 2011 at 10:55

3 Answers 3

13

You could check for references to function mcount (or possibly _mcount or __mcount according to Implementation of Profiling). This function is necessary for profiling to work, and should be absent for non-profiled binaries.

Something like:

$ readelf -s someprog | egrep "\s(_+)?mcount\b" && echo "Profiling is on for someprog"

The above works on a quick test here.

answered Jun 6, 2011 at 11:21
2

The regular expression in the answer above doesn't always work...but the general idea of grepping for "mcount" in the output of 'readelf -s [binary]' is correct, I think

answered May 31, 2012 at 20:30
2

Adding more to the answers:

  1. To check for instrumentation, grep for mcount/gmon:

    $ readelf -s <binary> | egrep "gmon|mcount" 
    20: 0000000000401160 63 FUNC GLOBAL DEFAULT 12 __gmon_start__ 
    28: 0000000000000000 0 FUNC GLOBAL DEFAULT UND mcount@GLIBC_2.2.5 (2) 
    36: 0000000000000000 0 FILE LOCAL DEFAULT ABS gmon-start.c 
    39: 00000000004011a0 0 FUNC LOCAL DEFAULT 12 call_gmon_start 
    100: 0000000000401160 63 FUNC GLOBAL DEFAULT 12 __gmon_start__ 
    114: 0000000000000000 0 FUNC GLOBAL DEFAULT UND mcount@@GLIBC_2.2.5 
    
  2. One needs to compile as well as link with -pg flags, otherwise gmon.out will not be generated. stackoverflow link.

  3. I found that the binary on which I was running gprof didn't generate any gmon.out file, despite compiling/linking with -pg flag. The reason being - I was killing my application, it wasn't a clean exit. gprof generates output only when the program exits normally. stackoverflow link

answered May 17, 2016 at 2:07
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.