Use LLVM for code coverage checking

Code Coverage is a topic in the field of software testing. The source program with high probability Code Coverage is easier to find bugs under the test of the test set than the source program with low probability Code Coverage. LLVM provides a tool LLVM-COV, simply record it Instructions:

step 1: install clang and llvm       

sudo apt install clang llvm

Step 2: Write a test program:  

// coverage.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int loop = atoi(argv[1]);

    for(int idx = 0; idx < loop; idx++)
    {
        printf("%s line %d, %d\n", __func__, __LINE__, idx);
    }

    return 0;
}

Step3: Write Makefile

all:
	clang -o loop-cov -fprofile-instr-generate -fcoverage-mapping coverage.c

 The option -fprofile-instr-generate -fcoverage-mapping is to instrument and compile the source code so that the Code Coverage file will be generated after the program is executed

Step4: Execute the program to generate Code Coverage file

Generated the default.profraw file

Step5: Generate Code Coverage data information

all:
	clang -o loop-cov -fprofile-instr-generate -fcoverage-mapping coverage.c
data:
	llvm-profdata merge -o loop-cov.profdata default.profraw
	llvm-cov show ./loop-cov -instr-profile=loop-cov.profdata loop-cov.profdata

 Make data

 

After obtaining the execution times information of each instruction, llvm-cov performs instrumentation on the original code, and obtains the execution times information of each code of the program at runtime.


end!

Guess you like

Origin blog.csdn.net/tugouxp/article/details/113814431
Recommended