Native Code coverage with android soong build system

Posting answer to my question for other users on SO.

Install coverage tool :

  1. sudo apt-get install lcov (This should install lcov-1.12)
  2. sudo apt-get install gcc-4.6 (Clang generates .gcno approximately equal to gcc 4.2 that aren't compatible with gcov-4.8. Installing gcc-4.6 to get gcov-4.6 and invoking lcov with '--gcov-tool /usr/bin/gcov-4.6')
  3. Download LLVM 3.8 for llvm-cov to work : http://releases.llvm.org/download.html

All native unit test cases i.e instrumented binary needs to be executed on target. To build and emit clang's instrumentation based profiling. Example: http://androidxref.com/9.0.0_r3/xref/hardware/interfaces/automotive/vehicle/2.0/default/Android.bp#82 (Renamed to vehicle-manager-unit-test for shorter name)

  • export NATIVE_COVERAGE=true
  • Add native_coverage: true to test module in Android.bp
  • Go to: module-name/test
  • Use mm or make command to build native binary
  • Ex: For hardware/interfaces/automotive/vehicle/2.0/default/tests/ : mma or make vehicle-manager-unit-test -j32
  • Copy coverage enabled instrumented binary to target
  • adb push out/target/product/product_name/data/nativetest64/vendor/vehicle-manager-unit-test /data/nativetest64/vehicle-manager-unit-test adb shell chmod +x /data/nativetest64/vehicle-manager-unit-test

  • Run test cases and generate .gcda files

    adb shell \ GCOV_PREFIX=/data/local/tmp \ GCOV_PREFIX_STRIP=echo $ANDROID_BUILD_TOP | grep -o / | wc -l \ /data/nativetest64/vehicle-manager-unit-test

  • adb shell find -iname *.gcda

  • adb pull /data/local/tmp/proc/self/cwd/out/soong/.intermediates/hardware/interfaces/automotive/vehicle/2.0/default/vehicle-manager-unit-test/android_x86_64_silvermont_vendor_cov/obj/hardware/interfaces/automotive/vehicle/2.0/default/tests/ .(Destination folder)

  • Extract GCNO files from GCNODIR (archive file generated at out/overage/data/nativetest64/vendor/vehicle-manager-unit-test ) to same folder with GCDA files

  • llvm-cov gcov -f -b *.gcda (https://llvm.org/docs/CommandGuide/llvm-cov.html )

  • lcov --directory . --base-directory . --gcov-tool /usr/bin/gcov-4.6 --capture -o cov.info (http://ltp.sourceforge.net/coverage/lcov.php)

  • genhtml cov.info -o output

sample coverage report for vhal 2.0 Here's the script which wraps all these commands: https://gist.github.com/pankajgangwar/f070b8b54e83543f8e3638dcd2cae1b8