问题
I'm using gcov to collect code coverage data for a C project I'm working on. I understand that gcov dumps the code coverage data once the program exits after completion. How do I collect gcov data for long running processes. (say, my program is the kernel of an operating system which runs in a server that never shuts down - and I need to collect code coverage data for it). Is there any way to make gcov dump code coverage data periodically (say, every 1 hour) or upon certain event - how can I trigger gcov dump code coverage data (rather than waiting for gcov to do it after the program terminates)?
回答1:
Call __gcov_flush()
periodically.
This can be done by associating a signal handler:
#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);
}
Compile regularly: gcc sig.c -ftest-coverage -fprofile-arcs
Then toggle (periodic) update by kill -2 process_id
来源:https://stackoverflow.com/questions/14977285/dumping-gcov-data-at-runtime