How Can We Help?
< All Topics
Print

Generate Code Coverage for makefile Projects

Generate Code Coverage for makefile Projects

To generate code coverage for the makefile-based project in Windows and Linux, you need to prefix rktracer to make command and rebuild the application.

make 

To 

rktracer make  

 

Examples:       

SL/No

Makefile command

with rktracer tool

 

1

make

nmake

rktracer make

rktracer nmake

instruments all the source file in silent mode for multi conditions

2

 

rktracer -v make

rktracer -v nmake

instrument all source files and displays verbose log for multi conditions

3

 

rktracer -v  -branch make

rktracer -v  -branch nmake

instrument all source files and displays verbose log for branch/decision coverage

4

 

rktracer -v -r make

rktracer -v -r nmake

Instrument all source files and display the verbose log for multi conditions in raw mode. RAW mode coverage will be stored in the RK_MEM variable.

RKTracer tool internally checks the compiler in the makefile and instrument all the source files when the build is triggered. By default, rktracer instruments for multiple condition coverage, and you see the following code coverage information in the HTML report.

  • Function Coverage
  • Line Coverage 
  • Statement Coverage
  • Multi-Condition Code Coverage

 

Example makefile

Step 1:Enable RKTracer tool and rebuild the application

make 

TO

rktracer -v make

RKTracer preprocesses the source file with the below command.

RKTracer instruments the preprocessed file with “c:\rktracer/lib/librklic-x86_64-w64-mingw32.exe ” and writes out the instrumented preprocessed file to “prime.i” file. Also, it stores the metadata, structure of instrumentation, and copy of source file content to JSON file.  

RKTracer automatically adds runtime at linking time.

 

Step 2: Testing instrumented application.

Dynamic testing can be unit testing or functional/integration testing, or both. Let us go ahead and input values 2,5 and then quit testing.

Post-testing the RKTracer tool will save coverage data to the rk-coverage.txt file. You can skip and go to third step to generate reports if you see the rk-coverage.txt file.

Coverage data file not found after testing.

RKTracer tool uses the following approaches to save coverage data.

  • Run unit/functional testing, and at the end RKTracer tool will automatically save coverage data on the host/embedded target/server machine, that is, write coverage data to the rk-coverage.txt file.
  • Send a signal to the application running in an infinite loop to trigger and save coverage data. 
  • The application under test has crashed in the middle of testing on the host or target device. RKTracer saves coverage data to rk-coverage.txt and generate reports to check the point of a crash in reports.
  • Embedded devices without a file system or not connected to host while testing. In this case, rktracer RAW mode comes in to picture. You need to save the coverage data from the RKTracer variable. 

Suppose you cannot find the rk-coverage.txt file in the project working folder or target server machine. An application under test has not exited with the proper exit handler, i.e., atexit() handler, or your application might be running in an infinite loop. You can use the following two methods to save coverage data from memory to the file system.

Save coverage data when a particular function is executed at runtime testing.

Identify a function/method name in your application (ensure this function/method is not excluded from RKTracer instrumentation or file with this function). Then copy the function name and edit the rktracer.config in the RKTracer installation folder. 

For C programming language:  Add a new line at the end of rktracer.config file with “c-function-writeout” and function name. Here the function name is case sensitive, and you should also add empty braces, i.e., () at the end of a function name. In case if you have a function with an input parameter, it should still be empty braces. You can add multiple function names with one space to the list, as shown below.

  • c-function-writeout main()
  • c-function-writeout main()  OnCreate() fun()

For C++ programming language: Add a new line at the end of rktracer.config file with “c++-function-writeout” and function name. Here the function name is case sensitive, and you should also add empty braces, i.e., () at the end of a function name. In case if you have a function with an input parameter, it should still be empty braces. You can add multiple function names with one space to the list, as shown below.

  • c++-function-writeout main()
  • c++-function-writeout main()  OnCreate() fun()

After adding function name to rktracer.config file. You need to rebuild the application to re-instrument using rktracer. Coverage data will be saved to the rk-coverage.txt file when this function executes at runtime testing. 

Send a signal to save coverage data.

We can send a signal to the application running in an infinite loop to save coverage data to rk-coverage.txt.

  • Windows: send a kill signal using keys in the keyboard, i.e., Control+Pause/break. Contact support if you would like to add additional signal command to save coverage data at runtime testing.
  • Linux/Unix: Send a SIGWINCH signal: killall -SIGWINCH a.out or kill -SIGWINCH Process ID number. 

Step 3:Generate Code Coverage HTML reports

Once you have the rk-coverage.txt file, you must run the command “rkresults” to generate HTML reports. Ensure that you run the “rkresults” command in the project root folder or where we have the “rktracer” folder.

rkresults command will search coverage data file “rk-coverage.txt” and map with JSON files (generated during instrumentation) in the rktracer folder and generate HTML reports. You can manually open the HTML report using index.html.

Generate code coverage for selected functions/files/folder.

Generate coverage reports  for the selected folders

C:\project\sound\drivers\base\power\

C:\project\sound\drivers\base\power\firmware_loader\

C:\project\sound\drivers\core\

C:\project\sound\security\keys\

C:\project\sound\security\lockdown\

Suppose you need code coverage for source files from three different folders, i.e., core, keys, power, and ignore coverage for folders firmware_loader and lockdown. Edit rktracer.config in the RKTracer installation folder and go to the end of the file add the following information. 

ignore *.c

instrument */power/* */core/* */keys/*

never */firmware_loader/* */lockdown/*

ignore *.c = Ignores all C source files from instrumentation

Instrument = Instrument source files from given folders

never = ignore selected folder  

        

Generate coverage reports only for selected functions from three different files.

fun_X() in source-file-X 

fun_Y() in source-file-Y

fun_Z() in source-file-Z

Suppose you need code coverage for selected functions from three different files. Edit rktracer.config in the RKTracer installation folder and go to the end of the file and set the following variables as shown below.  

ignore *.c

instrument *source-file-X.c *source-file-Y.c *source-file-Z.c

function-ignore *

function-instrument fun_X() fun_Y() fun_Z()

ignore *.c = Ignore all C programming source files

instrument *file-X.c *file-Y.c *file-Z.c   = Instrument only these three source files.

function-ignore * = Then ignore all functions in the above three files.

function-instrument fun_X() fun_Y() fun_Z() = Don’t ignore these three functions from these three files for instrumentation.

Next Generate Code Coverage for Cmake Projects
Table of Contents