visual-studio-2010 - VS2010 的代码覆盖 block

标签 visual-studio-2010 profiling code-coverage profiler

我运行 C++ 代码以获得代码覆盖率结果 this post .

#include <iostream>
using namespace std;

int testfunction(int input)
{
    if (input > 0) {
        return 1;
    }
    else {
        return 0;
    }
}

int main()
{
    testfunction(-1);
    testfunction(1);
}

enter image description here

代码覆盖率结果表明 main() 中有三个块,testfunction() 中有四个块。块是什么意思? main/testfunction 中的 3/4 块如何?

添加

当我修改代码如下时,
int main()
{
    testfunction(1);
    testfunction(1);
}

或如下
int main()
{
    testfunction(-1);
    testfunction(-1);
}

我有这个结果。

enter image description here

而且似乎testfunction()有四个块。
  • 函数入口
  • 如果块
  • 否则块
  • 条件

  • 我收到了来自 this post 的提示.

    最佳答案

    代码覆盖中块的技术术语是基本块。直接从 the Wikipedia entry :

    The code in a basic block has one entry point, meaning no code within it is the destination of a jump instruction anywhere in the program, and it has one exit point, meaning only the last instruction can cause the program to begin executing code in a different basic block. Under these circumstances, whenever the first instruction in a basic block is executed, the rest of the instructions are necessarily executed exactly once, in order.



    基本块在代码覆盖率中很重要,因为我们可以在基本块的开头插入一个探针。当这个探测器被击中时,我们知道该基本块中的所有以下指令都将被执行(由于基本块的属性)。

    不幸的是,对于编译器(尤其是优化),源代码如何映射到基本块并不总是很明显。最简单的判断方法是查看生成的程序集。例如,让我们看看您原来的 main & testfunction :

    对于 main ,我看到下面的程序集(与原始来源交错)。类似于 Peter does here ,我已经注意到基本块的开始位置。
    int main()
    {
    013B2D20  push        ebp                       <--- Block 0 (initial)
    013B2D21  mov         ebp,esp  
    013B2D23  sub         esp,40h  
    013B2D26  push        ebx  
    013B2D27  push        esi  
    013B2D28  push        edi  
        testfunction(-1);
    013B2D29  push        0FFFFFFFFh  
    013B2D2B  call        testfunction (013B10CDh)  
    013B2D30  add         esp,4                     <--- Block 1 (due to call)
        testfunction(1);
    013B2D33  push        1  
    013B2D35  call        testfunction (013B10CDh)  
    013B2D3A  add         esp,4                     <--- Block 2 (due to call)
    }
    013B2D3D  xor         eax,eax  
    013B2D3F  pop         edi  
    013B2D40  pop         esi  
    013B2D41  pop         ebx  
    013B2D42  mov         esp,ebp  
    013B2D44  pop         ebp  
    013B2D45  ret  
    

    我们看到 main具有三个基本块:一个是初始块,另外两个是因为函数调用。查看代码,这似乎是合理的。 testfunction有点硬。单看源码,似乎分为三个块:
  • 功能和逻辑测试入口(input > 0)
  • 条件真分支 ( return 1 )
  • 条件假分支 ( return 0 )

  • 但是,由于实际生成的程序集,有四个块。我假设您在禁用优化的情况下构建了代码。当我在调试配置(禁用优化)中使用 VS2010 进行构建时,我看到以下对 testfunction 的反汇编:
    int testfunction(int input)
    {
    013B2CF0  push        ebp                         <--- Block 0 (initial)
    013B2CF1  mov         ebp,esp  
    013B2CF3  sub         esp,40h  
    013B2CF6  push        ebx  
    013B2CF7  push        esi  
    013B2CF8  push        edi  
        if (input > 0) {
    013B2CF9  cmp         dword ptr [input],0  
    013B2CFD  jle         testfunction+18h (013B2D08h)  
            return 1;
    013B2CFF  mov         eax,1                        <--- Block 1 (due to jle branch)
    013B2D04  jmp         testfunction+1Ah (013B2D0Ah)  
        }
        else {
    013B2D06  jmp         testfunction+1Ah (013B2D0Ah) <--- Not a block (unreachable code)
            return 0;
    013B2D08  xor         eax,eax                      <--- Block 2 (due to jmp branch @ 013B2D04)
        }
    }
    013B2D0A  pop         edi                          <--- Block 3 (due to being jump target from 013B2D04)
    013B2D0B  pop         esi  
    013B2D0C  pop         ebx  
    013B2D0D  mov         esp,ebp  
    013B2D0F  pop         ebp  
    013B2D10  ret  
    

    在这里,我们有四个块:
  • 函数入口
  • 条件真分支
  • 条件假分支
  • 共享 function epilog (清理堆栈并返回)

  • 如果编译器在条件 true 和条件 false 分支中复制了函数结语,您将只会看到三个块。此外,有趣的是,编译器插入了一个虚假的 jmp说明在 013B2D06 .因为它是无法访问的代码,所以它不被视为基本块。

    一般来说,所有这些分析都是多余的,因为整体代码覆盖率指标会告诉您需要了解的内容。这个答案只是为了强调为什么块的数量并不总是显而易见的或预期的。

    关于visual-studio-2010 - VS2010 的代码覆盖 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4958209/

    相关文章:

    c# - 将项目添加到 Visual Studio 解决方案时出现的问题

    entity-framework - 导出大型 Entity Framework 图

    javascript - 分析 javascript 执行的最佳方法是什么?

    ruby - 如何从 Gemfile 安装 gems?

    Visual Studio Code 中的 Java 代码覆盖率

    wpf - 使用属性面板中的 TypeConverter 设置类类型的属性会生成扩展的 XAML,而不是字符串

    C++ 没有正确显示随机数

    performance - 如何使用 Toad 跟踪 SQL

    python - 代码的行分析器是否需要解析树,这就足够了吗?

    c# - 为代码覆盖关闭 Visual Studio 2010 中的 Lambda 表达式