Search code examples
ctestinggccgcov

Getting the sequence of Gcov's executed lines information


I'm trying to get the sequence of line of code executed by a test case. By using Gcov, the output .gcov file shows which line of codes are executed and how many times they are executed but no information about the sequence.

The problem is that I can't get the sequence of the executed lines. Let's use this program as an example:

//...
for(i = 0; i < n; i++) { // line 8
    if(a > b) { // line 9
        // do something (line 10 - 15)
    } else { // line 16
        // do something (line 17 - 20)
    }
}
//...

For example with input x, the loop body will be iterated twice where the true branch of the if executed on the first iteration and then the false branch on the second iteration. On this example, the information about the sequence of executed lines that I want to get would be something like this ...-8-9-10-11-12-13-14-15-8-9-17-18-19-20-....

With Gcov, I can't get the information mentioned above because the .gcov file will only show that all the lines from both branch are executed. There is no way I can figure out the sequence whether line 10-15 or line 17-20 are executed first.

Is there a way of getting the sequence of executed lines with Gcov? Or is there any alternatives of doing this? Thank you


Solution

  • gcov (and -fprofile-arcs) only store and process raw numbers, there is no branch history information. The data you are looking for is simply not available.

    American Fuzzy Lop has a different form of instrumentation which covers some path-related information. See the technical description. You do not say what you actually want to achieve, so perhaps this is sufficient.

    If you need to capture any form of execution trace, you could use a GDB script like this:

    b main
    r
    while 1
    s
    end
    

    It will need some post-processing because it is quite difficult to suppress unwanted GDB output. For more elaborate data capture, you could use the GDB Python interface.