1. Preparation
linux PC development board
1) Install the cross-compilation tool mipsel-unknown-linux-gnu on the linux PC, and configure the environment variables
2) Download and install Cunit and lcov. The installation method is as follows:
tar -zxvf CUnit-2.1.0-src.tar.gz
cd CUnit-2.1.0
./configure --host=mipsel-unknown-linux-gnu --prefix=/usr/local
make
Take exe as an example, the src directory is located at: /data/home/carlos/trunk/src/XX/exe/, compile with -fprofile-arcs -ftest-coverage (the first parameter is to use two GCOV environment variables, Instructions are below; the second parameter is to add a code coverage switch).
The makefile directory is located at: /data/home/carlos/trunk/src/YY/YY/exe/.
Package the content of the generated directory and copy it to the test machine directory: /data/ZZ/ZZ/exe/,
And copy the preload library signal.so to this directory (in order to process the signal without modifying the source code, execute gcov_flush when the program exits)
Configure environment variables, and use these two variables to easily specify the output location of the gcda file:
export GCOV_PREFIX_STRIP=8 (the original directory generated by compilation has 8 layers)
export GCOV_PREFIX=/data/ZZ/ZZ/exe/
Excuting an order:
LD_PRELOAD=./signal.so ./exe -- daemon=false&
Note: The daemon method cannot be used here, because the daemon will fork the child process to execute the program, and the __attribute__ ((constructor)) in the preloaded library cannot take effect
After testing, kill the process with killall -6 exe (the process must be terminated with a signal that can be caught and handled in the preloaded library)
Execute gcov –o ./*.gcno to generate *.gcda files in the current directory
Copy this directory back to the compilation target directory of the development machine, namely:
/data/home/carlos/trunk/src/YY/YY/exe/ Because when generating reports later, the lcov tool must point to the compilation directory (depending on other source code in different hierarchical directories), not the installation or running directory
Execute in /data/home/carlos/trunk/src/YY/YY/exe/:
lcov –c –d ./ -b../../../ -o exe.info
genhtml –o ./html_result/ --no-branch-coverage exe.info
The code coverage report is available in the generated html_result directory, where XX/exe/index.html is the target page
Among them, there are examples of signal.so with code on the Internet. In fact, it is to use __attribute__ ((constructor)) to define a message corresponding function.