草庐IT

c - libgcov fork 和 exec 钩子(Hook)

coder 2023-06-21 原文

我的 gcc 手册页声明了关于 --coverage 选项的声明:

Also "fork" calls are detected and correctly handled (double counting will not happen).

我注意到我的/usr/lib/gcc/x86_64-linux-gnu/5.4.0/libgcov.a 包含符号 __gcov_fork__gcov_execl 和其他 __gcov_exec* 变体。网上查了下这些函数的定义,貌似会dump和clear coverage输出,避免数据重复或丢失。

但这似乎对我不起作用:

gcov_test$ rm *.gcno *.gcda
gcov_test$ cat gcov_test.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(void) {
    puts("Before loop");
    for (int i=0; i<5; ++i)
        printf("i=%d\n", i);
    puts("After loop");
    pid_t child1 = fork();
    if (child1<0) {
        perror("fork 1");
        exit(1);
    } else if (child1==0) {
        printf("In child 1: %d\n", (int)getpid());
        execl("/bin/true", "/bin/true", (char*)NULL);
        perror("execl");
        exit(1);
    }
    printf("Parent spawned child 1: %d\n", (int)child1);
    pid_t child2 = fork();
    if (child2<0)
    {
        perror("fork 2");
        exit(1);
    } else if (child2==0) {
        printf("In child 2: %d\n", (int)getpid());
    } else {
        printf("Parent spawned child 2: %d\n", (int)child2);
        if (waitpid(child1, NULL, 0)<0)
            perror("waitpid 1");
        if (waitpid(child2, NULL, 0)<0)
            perror("waitpid 2");
        puts("Parent done");
    }
    return 0;
}

gcov_test$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

gcov_test$ gcc -c -std=c11 -Wall --coverage gcov_test.c 
gcov_test$ gcc --coverage gcov_test.o -o gcov_test
gcov_test$ ./gcov_test
Before loop
i=0
i=1
i=2
i=3
i=4
After loop
Parent spawned child 1: 31569
Parent spawned child 2: 31570
In child 2: 31570
In child 1: 31569
Parent done
gcov_test$ gcov gcov_test.c
File 'gcov_test.c'
Lines executed:64.29% of 28
Creating 'gcov_test.c.gcov'

gcov_test$ cat gcov_test.c.gcov
        -:    0:Source:gcov_test.c
        -:    0:Graph:gcov_test.gcno
        -:    0:Data:gcov_test.gcda
        -:    0:Runs:2
        -:    0:Programs:1
        -:    1:#include <stdlib.h>
        -:    2:#include <stdio.h>
        -:    3:#include <unistd.h>
        -:    4:#include <sys/types.h>
        -:    5:#include <sys/wait.h>
        -:    6:
        2:    7:int main(void) {
        2:    8:    puts("Before loop");
       12:    9:    for (int i=0; i<5; ++i)
       10:   10:        printf("i=%d\n", i);
        2:   11:    puts("After loop");
        2:   12:    pid_t child1 = fork();
        2:   13:    if (child1<0) {
    #####:   14:        perror("fork 1");
    #####:   15:        exit(1);
        2:   16:    } else if (child1==0) {
    #####:   17:        printf("In child 1: %d\n", (int)getpid());
    #####:   18:        execl("/bin/true", "/bin/true", (char*)NULL);
    #####:   19:        perror("execl");
    #####:   20:        exit(1);
        -:   21:    }
        2:   22:    printf("Parent spawned child 1: %d\n", (int)child1);
        2:   23:    pid_t child2 = fork();
        2:   24:    if (child2<0)
        -:   25:    {
    #####:   26:        perror("fork 2");
    #####:   27:        exit(1);
        2:   28:    } else if (child2==0) {
        1:   29:        printf("In child 2: %d\n", (int)getpid());
        -:   30:    } else {
        1:   31:        printf("Parent spawned child 2: %d\n", (int)child2);
        1:   32:        if (waitpid(child1, NULL, 0)<0)
    #####:   33:            perror("waitpid 1");
        1:   34:        if (waitpid(child2, NULL, 0)<0)
    #####:   35:            perror("waitpid 2");
        1:   36:        puts("Parent done");
        -:   37:    }
        2:   38:    return 0;
        -:   39:}
        -:   40:
gcov_test$ 

在我看来,“child 1”进程从未将其覆盖结果写入文件,因为特别是 “In child 1” 行已执行但未显示为已覆盖。并且第二个 fork 之前的所有行似乎都报告了双倍的覆盖率,因此覆盖率结果似乎没有像手册页声称的那样在调用 fork 时重置。

我还需要做些什么来启用那些 libgcov Hook 吗?我不应该只是在覆盖模式下编译时用实际的钩子(Hook)名称替换系统调用,对吗?

最佳答案

解决方案:将c11替换为gnu11

-std=c11 表示纯 C11(没有 GNU 扩展)。 -std=gnu11 还启用 GNU 扩展。我无法解释 -std=--coverage 之间的联系(可能 -std= 通常影响内置函数的使用, __gcov_fork 就是其中之一),但是简单地将标准更改为 gnu11 似乎可以解决问题,即第 17 行和第 29 行现在的执行计数都等于 1。 (我在 GCC 5.4.0 和最近的主干修订版上都试过了)。

附言我建议您提交错误报告。即使这种行为是有意为之,编译器至少应该警告我们潜在的问题。

关于c - libgcov fork 和 exec 钩子(Hook),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46381406/

有关c - libgcov fork 和 exec 钩子(Hook)的更多相关文章

  1. ruby-on-rails - 在所有延迟的作业之前 Hook - 2

    是否可以在所有delayed_job任务之前运行一个方法?基本上,我们试图确保每个运行delayed_job的服务器都有我们代码的最新实例,所以我们想运行一个方法来在每个作业运行之前检查它。(我们已经有了“check”方法并在别处使用它。问题只是关于如何从delayed_job中调用它。) 最佳答案 现在有一种官方方法可以通过插件来做到这一点。这篇博文通过示例清楚地描述了如何执行此操作http://www.salsify.com/blog/delayed-jobs-callbacks-and-hooks-in-rails(本文中描述

  2. ruby - 刚刚分配的变量是否有 ruby 钩子(Hook)? - 2

    这是我理想中想要的。用户做:a="hello"输出为Youjustallocated"a"!=>"Hello"顺序无关紧要,只要我能实现该消息即可。 最佳答案 不,没有直接的方法可以做到这一点,因为在执行代码之前,Ruby字节码编译器会丢弃局部变量名。YARV(MRI1.9.2中使用的RubyVM)提供的关于局部变量的唯一指令是getlocal和setlocal,它们都对整数索引进行操作,而不是变量名。以下是1.9.2源代码中insns.def的摘录:/****************************************

  3. ruby - 如何将 (pre/post)_install_hook 添加到 ruby​​ gems - 2

    我想在安装前使用geminstallsome-gem命令从ruby​​gems.org安装gem时执行一些代码。文档指出您可以使用http://docs.ruby-lang.org/en/2.2.0/Gem.htmlpre_installHook,如下所示:#Filelib/rubygems.rb,line724defself.pre_install(&hook)@pre_install_hooks文档进一步说明:RubyGemsdefaultsarestoredinrubygems/defaults.rb.Ifyou'repackagingRubyGemsorimplementing

  4. ruby - 扩展 ActiveSupport::Notifications.subscribe, instantiation.active_record 钩子(Hook) - 2

    我正在探索ActiveSupport::Notifications,并且想要更多关于'instantiation.active_record'的信息,而不仅仅是:record_count和:类名[1].例如,ActiveSupport::Notifications.subscribe/instantiation.active_record/do|*args|args.status#DatabaseorActiveRecordreturnstatusargs.result#Theactualresultsetreturnedargs.etc..#AnyotherinfoIcancolle

  5. ruby - 为什么在:save callback hook not getting called from FactoryGirl. create()之前? - 2

    这个简单的示例使用DataMapper的before:save回调(又名Hook)来增加callback_count。callback_count初始化为0,回调应该设置为1。当通过以下方式创建TestObject时调用此回调:TestObject.create()但是当FactoryGirl通过以下方式创建时会跳过回调:FactoryGirl.create(:test_object)知道为什么吗?[注意:我正在运行ruby​​1.9.3,factory_girl4.2.0,data_mapper1.2.0]详细信息如下...DataMapper模型#file:models/test_

  6. ruby - 创建/构建 Hook 后 DRY FactoryGirl - 2

    我想在我的工厂中干掉创建/构建后的Hook:FactoryGirl.definedofactory:polldosequence:titledo|n|"MyPollTitle#{n}"endsequence:descriptiondo|n|"MyPollDescription#{n}"enduserfactory:poll_with_answersdoignoredoanswers_count2endafter(:build)do|poll,evaluator|evaluator.answers_count.timesdopoll.answers我面临的问题是我似乎无法在FG中定义方法

  7. ruby - pg: exec_params 不替换参数? - 2

    第一次使用pggem访问postgres数据库。我已成功连接并可以使用#exec运行查询,但现在使用#exec_params构建一个简单的查询似乎没有替换参数。即:get'/databases/:db/tables/:table'do|db_name,table_name|conn=connect(db_name)query_result=conn.exec_params("SELECT*FROM$1;",[table_name])end结果为#这似乎是一个非常简单的示例-我是否从根本上误解了如何使用此方法? 最佳答案 您可以将占位

  8. ruby-on-rails - bundle exec rake 测试抛出错误 - 2

    你好,我是铁路新手。我正在关注MichaelHartl的railstutorial.org。我卡在了第4章的list4.5中:当我点击$bundleexecraketest时,它显示的结果与按照教程应该显示的结果不同。注意:我使用的是Ubuntu15.10作为平台。当我点击$bundleexecraketest时的结果/home/shyambhimani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/minitest-reporters-1.0.5/lib/minitest/minitest_reporter_plugin.rb:8:in

  9. ruby-on-rails - RSpec 中 spec 目录的 Before 和 After 钩子(Hook) - 2

    我们的RSpec测试套件中有相当多的测试。目录结构看起来像-spec/truncation/example1_spec.rbexample2_spec.rb...transaction/example1_spec.rbexample2_spec.rb...我想在transaction/文件夹中的所有规范文件运行之前恢复测试数据库转储,并在所有测试完成后将其清空。有办法吗?有before(:suite)和after(:suite)Hook,但它们适用于单个规范文件。有没有办法在RSpec中为目录提供前后Hook? 最佳答案 你在使用R

  10. ruby - Supervisord:如何获取 rvm 和 bundle exec 来启动 rails 脚本? - 2

    我正在尝试使用supervisord管理一个ruby​​脚本,但是因为我还没有完全理解RVM是如何工作的,所以我无法正确地完成它。通常我会做以下事情:#sourcingofrvmdoneautomaticallyonsshlogincd/var/rails/myappRAILS_ENV="production"bundleexec./script/backgroundrb但是使用下面的配置文件我做不到:[program:owgm]directory=/var/rails/owgmcommand=bundleexec./script/backgroundrbenvironment=RAI

随机推荐