本文介绍了在运行时转储gcov数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用gcov收集我正在从事的C项目的代码覆盖率数据。我知道,一旦完成后程序退出,gcov将转储代码覆盖率数据。如何收集长时间运行的进程的gcov数据。 (例如,我的程序是操作系统的内核,该操作系统在永不关闭的服务器中运行-我需要为其收集代码覆盖率数据)。有什么方法可以定期(例如,每1小时一次)或在某些事件下制作gcov转储代码覆盖率数据-如何触发gcov转储代码覆盖率数据(而不是在程序终止后等待gcov这样做)? p>

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)?

推荐答案

定期调用 __ gcov_flush()

这可以通过关联来完成:

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);
}

定期编译: gcc sig.c -ftest- Coverage -fprofile-arcs

然后切换(定期)更新为 kill -2 process_id

这篇关于在运行时转储gcov数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 02:01