GCC's coverage (--coverage, .gcno beside each object at build time, .gcda written at exit) works too, with two
differences:
No continuous mode. The counters are written by an atexit handler, which _exit skips. A preloaded shim that
calls __gcov_dump before _exit writes them:
/* dump_on_exit.c: cc -shared -fPIC -O2 dump_on_exit.c -o dump_on_exit.so -ldl */
#define _GNU_SOURCE
#include <dlfcn.h>
#include <unistd.h>
void _exit(int code) {
void (*dump)(void) = (void (*)(void))dlsym(RTLD_DEFAULT, "__gcov_dump");
void (*real)(int) = (void (*)(int))dlsym(RTLD_NEXT, "_exit");
if (dump) dump();
real(code);
for (;;) {}
}
dlsym finds __gcov_dump only in a program that exports its symbols: link with -rdynamic
(-DCMAKE_EXE_LINKER_FLAGS="--coverage -rdynamic"). Run with LD_PRELOAD=$PWD/dump_on_exit.so. A process that
is killed or crashes still writes nothing. The same shim does not work for LLVM's __llvm_profile_write_file,
which is hidden; use continuous mode there.
One set of files. Every process writes into the same .gcda files in the build folder (their absolute
paths are compiled in), merging under a lock, and a later run adds to an earlier one. Delete them before each
campaign (find build/gcov -name '*.gcda' -delete), and use GCOV_PREFIX/GCOV_PREFIX_STRIP to send one run's
files elsewhere.
cmake -S . -B build/gcov -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ \
-DSNX_DEPLOY=OFF -DSNX_JEMALLOC=OFF -DCMAKE_C_FLAGS=--coverage -DCMAKE_CXX_FLAGS=--coverage \
"-DCMAKE_EXE_LINKER_FLAGS=--coverage -rdynamic"
cmake --build build/gcov --target scarletnx
LD_PRELOAD=$PWD/dump_on_exit.so python3 scripts/amberlith.py run --runtime build/gcov/scarletnx ...
gcovr -r . build/gcov --exclude '.cache/' --html-details work/cov/gcov/index.html
lcov -c -d build/gcov -o work/cov/gcov.info && genhtml work/cov/gcov.info -o work/cov/gcov-html
clang --coverage writes the same gcov format, read with llvm-cov gcov. Prefer source-based coverage with
Clang: its regions are exact to the expression, it survives _exit and crashes, and each process gets its own
file.
Made from docs/dev/coverage.md at 69237645142c. GPL-3.0-or-later.