Code Coverage for NDK build system

You will learn how to generate a code coverage for MSBuild system applications using the RKTracer Tool in 3 simple steps. 

You need to prefix “rktracer” command to NDK build command to enable code coverage tool. Runtime libraries will be added to the build system by the RKTracer.  Tests can be run on an RKtracer instrumented application on a native host or on an embedded system, and code coverage can be generated.   

RKTracer tool internally checks the compiler or cross-compilers used by the NDK build system variables and instrument all the source files compiled. By default, rktracer instruments for multi-condition, and you see the following code coverage information in the HTML report.

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

Code Coverage for NDK Build System

Here is an example application to demonstrate how code coverage can be generated with NDK build system.

Enable RKTracer Tool

ndk-build NDK_PROJECT_PATH=.  APP_BUILD_SCRIPT=.\app\Android.mk v=1

TO

rktracer ndk-build NDK_PROJECT_PATH=.  APP_BUILD_SCRIPT=.\app\Android.mk v=1

code coverage for NDK build system
Code coverage for NDK build applications

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 tool automatically adds runtime at linking time. The tool will refer to rktracer.config for internal configuration at the time of instrumentation.

code coverage for NDK build system runtime linking
Code coverage for NDK build at linking

Run tests on instrumented application

If you generate a shared library, push it to the target device or add it to application.apk.

Install the APK in the device.

By default, the RKTracer tool will autosave coverage data from the emulator or device to the app working folder in the build machine, provided the device or emulator is connected to the build machine. If the device/phone is not connected to the build machine at the time of testing, RKTracer coverage data will be saved in the logcat buffer and the testing device. You can later save coverage data from logcat log or pull coverage data from device to app folder in the build machine.

To pull the coverage data, you can execute the command. 

adb pull  /data/data/com.example.widecolor/rk-coverage.txt 

Generate Code Coverage HTML reports

Once you have the rk-coverage.txt file, you need to run the command “rkresults” to generate HTML reports. Ensure that you run the “rkresults” command in the application root folder or where you have the “rktracer” folder generated at the time of build/Instrumentation.

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

Enable coverage for selected files

Generate coverage reports  for the selected folders

C:projectsounddriversbasepower

C:projectsounddriversbasepowerfirmware_loader

C:projectsounddriverscore

C:projectsoundsecuritykeys

C:projectsoundsecuritylockdown

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 from instrumentation

Similar Posts