gmon.out is not written after compiling with gcc -

2019-04-24 14:12发布

问题:

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).

回答1:

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).



回答2:

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.



回答3:

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