1#!/bin/bash
2
3# This script generates coverage for bionic.
4#
5# Prereqs: Coverage-enabled build.
6#
7#   $ lunch <target>
8#   $ m NATIVE_COVERAGE_PATHS="bionic" CLANG_COVERAGE=true
9#   $ m NATIVE_COVERAGE_PATHS="bionic" CLANG_COVERAGE=true bionic-unit-tests
10#
11# Do *NOT* then rebuild at the top level, or you'll clobber the
12# coverage-enabled libc!
13#
14# Flash image and set $ANDROID_SERIAL.
15#
16# Usage: $ bionic/build/coverage.sh
17# Output: HTML report is generated to /tmp/bionic-coverage/html/index.html
18#
19
20eval "$(cd ${ANDROID_BUILD_TOP}; build/soong/soong_ui.bash --dumpvars-mode --vars="TARGET_ARCH TARGET_ARCH_VARIANT")"
21
22LLVM_PROFDATA=${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/llvm-binutils-stable/llvm-profdata
23LLVM_COV=${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/llvm-binutils-stable/llvm-cov
24
25DEVICE_TEST_DIR32=/data/local/tmp/bionic-coverage32
26DEVICE_TEST_DIR64=/data/local/tmp/bionic-coverage64
27DEVICE_PROF_DIR=/data/local/tmp/bionic-profraw
28HOST_PROFDATA_DIR=/tmp/bionic-coverage
29
30# Run bionic-unit-tests
31adb shell rm -rf ${DEVICE_TEST_DIR32} ${DEVICE_TEST_DIR64} ${DEVICE_PROF_DIR}
32adb shell mkdir ${DEVICE_TEST_DIR32} ${DEVICE_TEST_DIR64} ${DEVICE_PROF_DIR}
33adb push $OUT/data/nativetest/bionic-loader-test-libs ${DEVICE_TEST_DIR32}
34adb push $OUT/data/nativetest/bionic-unit-tests ${DEVICE_TEST_DIR32}
35adb push $OUT/data/nativetest64/bionic-loader-test-libs ${DEVICE_TEST_DIR64}
36adb push $OUT/data/nativetest64/bionic-unit-tests ${DEVICE_TEST_DIR64}
37adb shell LLVM_PROFILE_FILE=${DEVICE_PROF_DIR}/bionic-%p-%m.profraw \
38  LD_LIBRARY_PATH=${DEVICE_TEST_DIR32}/bionic-loader-test-libs \
39  ${DEVICE_TEST_DIR32}/bionic-unit-tests/bionic-unit-tests
40adb shell LLVM_PROFILE_FILE=${DEVICE_PROF_DIR}/bionic-%p-%m.profraw \
41  LD_LIBRARY_PATH=${DEVICE_TEST_DIR64}/bionic-loader-test-libs \
42  ${DEVICE_TEST_DIR64}/bionic-unit-tests/bionic-unit-tests
43
44# Pull coverage files and post-process
45rm -rf ${HOST_PROFDATA_DIR}
46mkdir ${HOST_PROFDATA_DIR}
47adb pull ${DEVICE_PROF_DIR} ${HOST_PROFDATA_DIR}/profraws
48
49${LLVM_PROFDATA} merge \
50  --output=${HOST_PROFDATA_DIR}/bionic.profdata \
51  ${HOST_PROFDATA_DIR}/profraws/*.profraw
52
53${LLVM_COV} show \
54  --instr-profile=${HOST_PROFDATA_DIR}/bionic.profdata \
55  --format=html \
56  $OUT/symbols/apex/com.android.runtime/lib64/bionic/libc.so \
57  --object=$OUT/symbols/apex/com.android.runtime/lib64/bionic/libm.so \
58  --object=$OUT/symbols/data/nativetest64/bionic-unit-tests/bionic-unit-tests \
59  --object=$OUT/symbols/apex/com.android.runtime/lib/bionic/libc.so \
60  --object=$OUT/symbols/apex/com.android.runtime/lib/bionic/libm.so \
61  --object=$OUT/symbols/data/nativetest/bionic-unit-tests/bionic-unit-tests \
62  /proc/self/cwd/bionic/libc \
63  /proc/self/cwd/bionic/libm \
64  --output-dir=${HOST_PROFDATA_DIR}/html \
65  --show-region-summary=false
66