7

I am trying to use gcov on Linux(Ubuntu) to see frequency of execution for each line of source.

I have added following flags to my gcc compiler and linker flags,

CCFLAGS =  -fprofile-arcs -ftest-coverage
LDFLAGS = -fprofile-arcs -lgcov

but after compiling and running the program, i see no *.gcda file created. As a result of which when i run

gcov  --object-directory <path to the *.gcno/*.gcda files> myfile.cpp 

Shows error:

myfile.gcda:cannot open data file, assuming not executed
File '../../../../../code/myfile.cpp'
Lines executed:0.00% of 2625

Am i missing something. How to fix this?

goldenmean
  • 18,376
  • 54
  • 154
  • 211
  • Did the program run successfully to completion? – Shawn Chin Dec 19 '12 at 17:21
  • Also, what do you get when you run `strings compiled_prog | grep gcda`? – Shawn Chin Dec 19 '12 at 17:25
  • @Shawn - Its a service/daemon that does not return. So essentially I have to CTRL-C the executable in the terminal to stop it. Does this affect the behavior of gcov. I know gprof does not generate profiling data for such processes which do not exit(). Does gcov have that limitation too. – goldenmean Dec 19 '12 at 18:05
  • @Shawn about your second comments strings compiled_prog... but it seems gcov is looking for a separate gcda file rather than the gcda information present in binary. I will still check the output of the strings command and update – goldenmean Dec 19 '12 at 18:08
  • 1
    AFAIK, as with gprof data, coverage data is only output on the `exit` call so you won't get the output if the application is terminated abruptly. As for my second comment, the output path for the data files are often embedded within the compiled binaries so they may not appear where expected if the binary has been moved after compilation. (p.s. these comments are based on experiences in the past. Things may have changed since I last fiddled with gcov.) – Shawn Chin Dec 19 '12 at 18:17
  • Thanks. Will check and update tomorrow. – goldenmean Dec 19 '12 at 18:30

2 Answers2

4

You can use __gcov_flush() method inside your code. You will need to invoke this from registered signal handler.

See:

https://www.osadl.org/fileadmin/dam/interface/docbook/howtos/coverage.pdf

Using this, you can keep your service running and issue "kill" whenever you need to dump coverage data.

Hope that helps....

Icarus3
  • 2,310
  • 14
  • 22
0

make sure the gcov and gcc are of the same version :), this where many of the people fail to check.

$gcc --version
gcc (GCC) 4.1.1
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ find  /usr/lib/gcc -name libgcov.a
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/32/libgcov.a
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/libgcov.a
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/32/libgcov.a
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/libgcov.a

so the gcc version you have loaded should be atleast in the available list of libgcov.a

Aravind Nadumane
  • 170
  • 1
  • 11