这里需要说明的问题: 为什么 BB1 和 BB2 都有 lineno=9 的代码?
lineno=9 的代码即为 for 语句本身,但实际上, for 循环的执行顺序是:
(0) 初始化 ( 某些场合初始化可以在 for 外面 )
(1) 判断条件,如果条件成立,则执行 for 循环体,再对循环变量进行增 / 减操作;否则,直接进入 for 循环后的块;
因此,这里 BB2 中的 lineno=9 实际上只是 for 里面的循环变量自增语句 (i++) ,这也是为什么 BB2 的代码行信息 lineno=10 写在 lineno=9 前面的原因;而 BB1 中的 lineno=9 实际上只用到判断条件 (i < 10) 。
2.3 含桩点信息的有效基本块图
结合 " Linux 平台代码覆盖率测试 -GCC 插桩前后汇编代码对比分析 " 一文对插入的桩代码的分析,可以很容易地在图 2 的基础上画出带有桩点信息的有效基本块图,如图 3 所示。
图 3 有效基本块 ( 带行号信息和桩点信息 ) 图
若将 " Linux 平台代码覆盖率测试 -GCC 插桩前后汇编代码对比分析 " 一文中前 5 段桩代码按其在文件中的先后顺序依次记为 stub1, stub2, ..., stub5 ,则图中的 stub1 ~ stub5 分别与该文的 stub1 ~ stub5 一一对应。此处为读者方便理解,列出图中的第 1 个桩点 stub1 的代码,如下。关于桩代码的分析已在该文中详细叙述,不再赘述。
movl .LPBX1, %eax # 将 .LPBX1 移到 %eax ,即 %eax=.LPBX1
movl .LPBX1+4, %edx #edx=.LPBX1+4
addl $1, %eax #eax=%eax+1
adcl $0, %edx #edx=%edx+0
movl %eax, .LPBX1 # 将 %eax 移回 .LPBX1
movl %edx, .LPBX1+4 # 将 %edx 移回 .LPBX1+4
关于该段桩代码的解释,可参考 " Linux 平台代码覆盖率测试 -GCC 插桩前后汇编代码对比分析 " 一文的 3.1 节。
2.4 插桩位置及桩代码执行情况分析
根据 2.3 节的叙述及 " Linux 平台代码覆盖率测试 -GCC 插桩前后汇编代码对比分析 " 一文的解释,总结一下该例子的插桩位置。
stub1 : 被插在第 10 行代码之前,被执行 10 次,存于 .LPBX1+0 位置。
stub2 :被插在第 13 行代码之前,被执行 0 次,存于 .LPBX1+8 位置。
stub3 :被插在第 13 行代码之后,被执行 0 次,存于 .LPBX1+24 位置。
stub4 :被插在第 15 行代码之前,被执行 1 次,存于 .LPBX1+16 位置。
stub5 :被插在第 15 行代码之后,被执行 1 次,存于 .LPBX1+32 位置。
上述执行次数就是 " Linux 平台代码覆盖率测试 -GCC 插桩前后汇编代码对比分析 " 一文 3.1 节中的静态数组 .LPBX1 元素的值 ( 注意元素的存放位置 ) 。
+0 +4 +8 +12 +16 +20 +24 +28 +32 +36
|
10 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
1 |
0 |
而这些值就被作为 COUNTERS 写入了 test.gcda 文件, tag=0x 01a10000 ,每个计数值 (counter)8 个字节。请参考 " Linux 平台代码覆盖率测试 -gcov-dump 原理分析 " 和 " Linux 平台代码覆盖率测试 -.gcda/.gcno 文件及其格式分析 " 。
3. 小结
本文简单介绍了基本块 (Basic Block) 和分支的基本概念,并通过例子重点叙述了基本块图 (Basic Block Graph) 、有效基本块图、含桩点信息的有效基本块图,同时分析了插桩位置和桩代码执行情况。
Reference
./gcc/basic_block.h( 本文对 Basic Block 的英文解释即在该文件中 )
http://www.linuxidc.com/Linux/2011-05/36541.htm
http://www.linuxidc.com/Linux/2011-05/36540.htm
http://www.linuxidc.com/Linux/2011-05/36539.htm
http://www.linuxidc.com/Linux/2011-05/36538.htm
http://www.linuxidc.com/Linux/2011-05/36537.htm
http://www.linuxidc.com/Linux/2011-05/36536.htm
http://www.linuxidc.com/Linux/2011-05/36572.htm
Appendix: 源代码中对 Basic Block 的解释
A basic block is a sequence of instructions with only entry and only one exit . If any one of the instructions are executed, they will all be executed, and in sequence from first to last.
There may be COND_EXEC instructions in the basic block. The COND_EXEC *instructions* will be executed -- but if the condition is false the conditionally executed *expressions* will of course not be executed. We don't consider the conditionally executed expression (which might have side-effects) to be in a separate basic block because the program counter will always be at the same location after the COND_EXEC instruction, regardless of whether the condition is true or not.
Basic blocks need not start with a label nor end with a jump insn . For example, a previous basic block may just "conditionally fall" into the succeeding basic block, and the last basic block need not end with a jump insn. Block 0 is a descendant of the entry block .
A basic block beginning with two labels cannot have notes between the labels.
Data for jump tables are stored in jump_insns that occur in no basic block even though these insns can follow or precede insns in basic blocks.
Basic block information indexed by block number .
