Understanding blocks in gcov files

自作多情 提交于 2021-01-29 05:34:44

问题


I'm trying to understand the output of the gcov tool. Running it with -a options makes sense, and want to understand the block coverage options. Unfortunately it's hard to make sense of what the blocks do and why they aren't taken. Below is the output.

I have run add function in my calculator program once. I have no clue why it shows block0.

        -:    0:Source:calculator.c
        -:    0:Graph:calculator.gcno
        -:    0:Data:calculator.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:#include "calculator.h"
        -:    2:#include <stdio.h>
        -:    3:#include <stdlib.h>
        -:    4:
        1:    5:int main(int argc, char *argv[])
        1:    5-block  0
        -:    6:{
        -:    7:    int a,b, result;
        -:    8:    char opr;
        -:    9:
        1:   10:    if(argc!=4)
        1:   10-block  0
        -:   11:    {
    #####:   12:        printf("Invalid arguments...\n");
    $$$$$:   12-block  0
    #####:   13:        return -1;
        -:   14:    }
        -:   15:
        -:   16:    //get values
        1:   17:    a = atoi(argv[1]);
        1:   18:    b = atoi(argv[3]);
        -:   19:
        -:   20:    //get operator
        1:   21:    opr=argv[2][0];
        -:   22:
        -:   23:    //calculate according to operator
        1:   24:    switch(opr)
        1:   24-block  0
        -:   25:    {
        1:   26:        case '+':
        1:   27:            result = add_(a, b);
        1:   27-block  0
        -:   28:
        1:   29:            break;
    #####:   30:        case '-':
    #####:   31:            result=sub_(a,b);
    $$$$$:   31-block  0
    #####:   32:            break;
    #####:   33:        case '_':
    #####:   34:            result=multiply_(a,b);
    $$$$$:   34-block  0
    #####:   35:            break;
    #####:   36:        case '/':
    #####:   37:            result = div_(a,b);
    $$$$$:   37-block  0
    #####:   38:        default:
    #####:   39:            result=0;
    #####:   40:            break;
    $$$$$:   40-block  0
        -:   41:    }
        -:   42:
        1:   43:    if(opr=='+' || opr=='-' || opr=='_'|| opr== '/')
        1:   43-block  0
    $$$$$:   43-block  1
    $$$$$:   43-block  2
    $$$$$:   43-block  3
        1:   44:        printf("Result: %d %c %d = %d\n",a,opr,b,result);
        1:   44-block  0
        -:   45:    else
    #####:   46:        printf("Undefined Operator...\n");
    $$$$$:   46-block  0
        -:   47:
        1:   48:    return 0;
        1:   48-block  0
        -:   49:}
        -:   50:
        -:   51:/**
        -:   52: * Function to add two numbers
        -:   53: */
        1:   54:float add_(float num1, float num2)
        1:   54-block  0
        -:   55:{
        1:   56:    return num1 + num2;
        1:   56-block  0
        -:   57:}
        -:   58:
        -:   59:/**
        -:   60: * Function to subtract two numbers
        -:   61: */
    #####:   62:float sub_(float num1, float num2)
    $$$$$:   62-block  0
        -:   63:{
    #####:   64:    return num1 - num2;
    $$$$$:   64-block  0
        -:   65:}
        -:   66:
        -:   67:/**
        -:   68: * Function to multiply two numbers
        -:   69: */
    #####:   70:float multiply_(float num1, float num2)
    $$$$$:   70-block  0
        -:   71:{
    #####:   72:    return num1 * num2;
    $$$$$:   72-block  0
        -:   73:}
        -:   74:
        -:   75:/**
        -:   76: * Function to divide two numbers
        -:   77: */
    #####:   78:float div_(float num1, float num2)
    $$$$$:   78-block  0
        -:   79:{
    #####:   80:    return num1 / num2;
    $$$$$:   80-block  0
        -:   81:}

If anyone knows how to decipher the block info, specially lines 5,12,13,43 ,64 or knows of any detailed documentation on what it all means, I'd appreciate the help.


回答1:


Each block is marked by a line with the same line number as the last line of the block and the number of branch and calls in the block. A block is created using a pair of curly braces({}). Line 5 marks the beginning of main block...then as I mentioned for every branch or function call block number is mentioned...Now your if statement has four conditions that means there will be 4 additional blocks which are labeled as 0,1,2,3..All the blocks which are not executed are marked $$$$$, which is true here as you must have passed '+' as the argument so the program never takes the path of other operators and hence block 1,2,3 are marked as $$$$$.

Hope this helps.



来源:https://stackoverflow.com/questions/55906426/understanding-blocks-in-gcov-files

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