Gmon.out does not write after compiling with gcc -pg -g

A C ++ program was compiled using gcc -pg -g (at least these are the arguments I gave to the Makefile and have no convincing evidence of which command was executed). The program went to normal completion with the installation of CWD in my home directory. There is no gmon.out file.

gcc - 4.4.7. OS - centos 6.

My program was launched by the manual Perl daemon using fork / exec. I verified that CWD is my home directory and that it can be written by running daemon touch foo just before running my target program. As far as I have managed to research, this should not affect the profiling of the program or the writing of gmon.out at its completion (usually).

+7
source share
4 answers

Ran to the same problem, g ++ 4.8.2 on CentOS 7. -pg present both for compilation and for linking, started the process and exited normally, there is no gmon.out .

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

Why does it work? On the gprof man page:

A profiled program should call exit (2) or return the profiling data normally, which should be saved in the gmon.out file.

Apparently, the gmon.out entry is output dependent (higher level) (2). So, make sure the code uses exit (2) (from stdlib), not _exit (3) (system call).

+8
source

You may have decided this a few months ago, but today I came across this effect, so I can answer future visitors:

No error message is displayed, gmon.out is simply not created (and the analysis text file will be empty).

One of the reasons why this may be possible is if you do not have the main method or in the case of -mwindows a WinMain . For instance. if you use compiler arguments (gcc) -e or (vc) /entry or using __main .

I looked through the gprof manual but did not find information on how to give it an entry point, so I changed the code.

+1
source

It's very late, but for those of you who struggle, after compiling the code with -pg you need to run the executable to create it gmon.out

0
source

It costs nothing that the -pg flag must be present both at compilation stages and at the linking stage. This usually means that you need to add it to CFLAGS and LDFLAGS to the Makefile.

0
source

Source: https://habr.com/ru/post/978574/


All Articles