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

4 Answers 4

17

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

1
  • I had a similar issue where I had a program running in an infinite loop and I was terminating it manually with ctrl+c. Removing the infinite part of the loop so the program terminated normally fixed it for me.
    – daniel
    Commented Jul 10, 2021 at 11:12
4

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

1

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.

0

In my case, the issue was that the executable was chdir'ing (changing the current working directory) to elsewhere, and that's where gmon.out ended up.

I ran my program with strace to see system calls and could see that it was writing the gmon output at the end of the trace.

Not the answer you're looking for? Browse other questions tagged or ask your own question.