阅读:
使用gcov生成增量覆盖率报告
https://blog.csdn.net/Ls4034/article/details/72911389
转自:https://stackoverflow.com/questions/14977285/dumping-gcov-data-at-runtime
待检测程序的main函数加入该函数:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
void __gcov_flush();
static void catch_function(int signal) {
__gcov_flush();
}
int main(void) {
if (signal(SIGINT, catch_function) == SIG_ERR) {
fputs("An error occurred while setting a signal handler.\n", stderr);
return EXIT_FAILURE;
}
while(1);
}
比如:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <signal.h>
void __gcov_flush();
static void catch_function(int signal) {
__gcov_flush();
}
int main(int argc, char *argv[])
{
if (signal(SIGINT, catch_function) == SIG_ERR) {
fputs("An error occurred while setting a signal handler.\n", stderr);
return EXIT_FAILURE;
}
if (argc >=2) {
printf("=====argc>=2\n");
}
printf("helloworld begin\n");
if (argc <2){
printf("=====argc<2\n");
}
while(1){
printf("this is the server body");
sleep(5);
//break;
}
return 0;
}
当在被检测程序的运行界面按ctrl+C按钮,即生成一个.gcda文件。
该文件为追加:
https://stackoverflow.com/questions/15612077/is-there-anyway-to-merge-two-gcov-files-into-one
若需要最新的,需要删除该文件。
附录:
kill命令的使用
先用ps查找进程,然后用kill杀掉:
ps -ef | grep vim
root 19255 19224 1 05:49 pts/1 00:00:00 vim install.py
root 19257 18618 0 05:49 pts/0 00:00:00 grep vim
kill 19255