Updated: October 28, 2024 |
Gather code coverage data (GNU)
gcov_variant [options] sourcefile
where gcov_variant depends on the target platform, as follows:
Target platform | gcov_variant |
---|---|
ARMv7 | ntoarmv7-gcov |
AArch64 | ntoaarch64-gcov |
x86 64-bit | ntox86_64-gcov |
Linux, Mac, Microsoft Windows
For example, if x.h was included in a.c, then running gcov -l on a.c will produce a.c##x.h.gcov instead of x.h.gcov. This can be useful if x.h is included in multiple source files.
The gcov utility searches for the .bb, .bbg, and .da data files using this option. If a directory is specified, the data files are in that directory and named after the source file name without its extension. If a file is specified, the data files are named after that file without its extension. If this option isn't specified, it defaults to the current directory.
Without -p only the filename component is used. With -p, all directories are used, with / characters translated to # characters, . directory components removed and .. components renamed to ^. This is useful if source files are in several different directories.
The -p option also affects the -l option.
You can use the gcov utility to test code coverage in your programs. Gathering code-coverage data involves the following steps:
You can use the Code Coverage perspective in the Integrated Development Environment (IDE) to gather and visually interpret this information. For more information, see Code Coverage in the IDE User's Guide. For detailed documentation about gcov, see https://gcc.gnu.org/onlinedocs//gcc/Gcov.html#Gcov in the GCC documentation at https://gcc.gnu.org/.
When compiling for coverage on a host operating system such as Windows which can use a file path separator other than ' / ' (forward slash), the .gcda files when generated on a nto target may have unexpected naming. The directory name of the .gcda file in these instances may contain elements of the directory path structure on the compiling host. The nto file system will only recognize the forward slash as a file path separator.
A .gcda file could be created such as:
c:\win_dir1\win_dir2/c_file.gcda
The generated directory name on the nto target would be c:\win_dir1\win_dir2 and GCOV_PREFIX_STRIP=1 with GCOV_PREFIX= would produce a file named c_file.gcda.
If you want to use lcov (which we don't provide) on a Linux host to view coverage data, configure it to work with our version of gcov by adding a line such as the following to your .lcovrc file:
geninfo_gcov_tool=ntox86_64-gcov
Gathering data from the command line
If you want to gether code-coverage data from the command line, you may have to adjust some paths, because the tools create the gcda files based on the current working directory when the corresponding source file was compiled. For example:
qcc -fprofile-arcs -ftest-coverage -o my_program my_program.c -lm
GCOV_PREFIX_STRIP=9990 GCOV_PREFIX=/tmp/ /tmp/my_program
To send the output to your current directory, run:
GCOV_PREFIX_STRIP=9999 ./my_program
Here's another example. Suppose you do the following:
cd /build/my_project/objects qcc -fprofile-arcs -ftest-coverage -c -o fileA.o ../source/fileA.c qcc -fprofile-arcs -ftest-coverage -c -o fileB.o ../source/fileB.c qcc -fprofile-arcs -ftest-coverage -c -o fileB.o ../source/fileC.c cd /build/my_project/ qcc -fprofile-arcs -ftest-coverage -o my_program objects/fileA.o objects/fileB.o objects/fileC.o
and you transfer the binary to your target system, and then run your program. Let's examine how GCOV_PREFIX and GCOV_PREFIX_STRIP affect where the code-coverage files go:
For more information, see https://gcc.gnu.org/onlinedocs/gcc/Cross-profiling.html in the GNU documentation.
GNU