Dumping gcov data at runtime

99封情书 提交于 2019-12-08 04:00:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!