Determine branch coverage information

Hello everybody, I am a beginner in LLVM and need to know how to use LLVM to instrument a C program and execute this instrumented program with different test cases to determine the branch coverage information for each test case. Any suggestion or help is more than welcomed. Thanks in advance. Ahmed Raafat.

Hi Ahmed!

Adventure wrote:

Hello everybody, I am a beginner in LLVM and need to know how to use LLVM to instrument a C program and execute this instrumented program with different test cases to determine the branch coverage information for each test case. Any suggestion or help is more than welcomed. Thanks in advance. Ahmed Raafat.

(In the following instructions you have to insert your own values for the <...> stuff.)

To instrument the C Program you have to compile it into a single bytecode file, I do this by translating each C file to bytecode

$> llvm-gcc -emit-llvm -c <sourcefile> -o <sourcefile>.bc

and then link them all together

$> llvm-ld -stats -time-passes -link-as-library -disable-opt *.bc -o <executable>.1.bc

This gives you an unoptimised bytecode file which is preferable in this case since it retains a somewhat 1:1 relation to your code so you can figure out the results afterwards.

Now its time to instrument the code, I do this by running

$> opt -stats -time-passes -insert-optimal-edge-profiling <executable>.1.bc -o <executable>.2.bc

and add the profiling runtime support

$> llvm-ld -stats -time-passes -link-as-library -disable-opt <path to llvm install>/lib/libprofile_rt.bca <executable>.2.bc -o <executable>.3.bc

You can then create a native executable by

$> llc <executable>.3.bc <executable>.s
$> gcc -g <executable>.s -o <executable>

When you run this executable (with your parameters) it creates a file called llvmprof.out which contains an edge profiling of your code. With

$> llvm-prof -print-all-code <executable>.1.bc

you can dump a bytecode file which is annotated with the recorded profiling information.

If you do several runs of your executable all runs are combined in the single llvmprof.out and the results are accumulated and you can check if every codepath was executed during your tests.

There is a tool in <source tree>/utils/profile.pl which takes a single bytecode file and parameters and does all the work of instrumenting, running and executing llvm-prof in one go, but its a little outdated, I attached a newer version, but I guess its easier to write a script that does exactly the steps you need. YMMV.

Andi

profile.pl (2.79 KB)

Hi Andreas,

Many thanks for the below detailed reply, i will try to follow the below steps and contact you if i have any problems.

Thanks again.

BR,
Ahmed Raafat.