Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gmon.out is not written after compiling with gcc -pg -g

Tags:

c++

linux

gcc

gprof

Compiled a C++ program using gcc -pg -g (at least, those are the args I gave in the Makefile; don't have any hard evidence of what command was executed). Program ran to normal completion with CWD set to my home directory. No gmon.out file written.

gcc is 4.4.7. OS is centos 6.

My program was launched by a hand-rolled Perl daemon using fork/exec. I've verified that the CWD is my home directory, and that it's writeable, by having the daemon execute touch foo just before exec'ing my target program. As far as I've been able to research, this shouldn't have affected the program's profiling or writing gmon.out when it terminated (normally).

like image 872
Chap Avatar asked Sep 22 '13 03:09

Chap


3 Answers

Ran into this same issue, g++ 4.8.2 on CentOS 7. -pg was present for both compiling and linking, run the process & it exits normally, no gmon.out generated.

I fixed this by replacing a call to _exit(status) with exit(status). Note that the former is _exit(3), a system call, and the latter is exit(2), a standard library method.

Why does this work? From the gprof man page:

The profiled program must call "exit"(2) or return normally for the profiling information to be saved in the gmon.out file.

Apparently the writing of gmon.out is dependent on (the higher-level) exit(2). So, check to be sure the code is using exit(2) (from stdlib) and not _exit(3) (system call).

like image 90
coledot Avatar answered Nov 09 '22 13:11

coledot


This is really late, but for those of you who are struggling, after you compile your code with -pg, you need to run the executable for it to generate gmon.out

like image 39
Meme Composer Avatar answered Nov 09 '22 11:11

Meme Composer


Maybe you have solved this months ago but I encountered the effect today so I can answer for future visitors:

No error message is shown, gmon.out just does not get created (and the analysis text-file will be empty).

One reason why this might be is if you have no main method or in the case of -mwindows a WinMain. E.g. if you use the compiler arguments (gcc) -e or (vc) /entry or using __main.

I looked over the gprof manual but did not find info about how to tell it an entry point so I changed the code.

like image 32
Bernd Elkemann Avatar answered Nov 09 '22 13:11

Bernd Elkemann