Dan Albert | 98e2c64 | 2014-09-19 14:40:57 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # Copyright (C) 2014 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | # |
| 17 | # acov is a tool for gathering coverage information from a device and generating |
| 18 | # a report from that information. To use: |
| 19 | # |
| 20 | # 1. sudo apt-get install lcov |
| 21 | # 2. Build application/library with coverage information. |
Roland Levillain | 0fb7707 | 2020-06-10 12:53:49 +0100 | [diff] [blame] | 22 | # * make NATIVE_COVERAGE=true NATIVE_COVERAGE_PATHS='*' |
Oliver Nguyen | f7421ae | 2019-10-02 12:06:04 -0700 | [diff] [blame] | 23 | # 3. Push the new binaries to the device with adb sync. |
| 24 | # (Optional): Run `acov --clean-device`. This will reset coverage for everything |
| 25 | # on the device. |
| 26 | # 4. Run tests. |
| 27 | # (Optional): Run `acov --flush`. This will dump coverage from all processes |
| 28 | # running on the device. |
Dan Albert | 98e2c64 | 2014-09-19 14:40:57 -0700 | [diff] [blame] | 29 | # 5. Run `acov`. |
| 30 | # |
| 31 | # acov will pull all coverage information from the device, push it to the right |
| 32 | # directories, run lcov, and display the coverage report (currently by opening |
| 33 | # it in your browser). |
| 34 | # |
| 35 | |
Pirama Arumuga Nainar | c41549f | 2021-03-25 16:31:17 -0700 | [diff] [blame] | 36 | if [ "$CLANG_COVERAGE" == "true" ]; then |
| 37 | echo "CLANG_COVERAGE is set. Use development/scripts/acov-llvm.py instead." |
| 38 | exit 0 |
| 39 | fi |
| 40 | |
Oliver Nguyen | f7421ae | 2019-10-02 12:06:04 -0700 | [diff] [blame] | 41 | ANDROID_OUT=${OUT_DIR:-out} |
| 42 | FLUSH_SLEEP=${FLUSH_SLEEP:-60} |
| 43 | |
| 44 | function clearGcdaFiles() { |
| 45 | if [ -d "$ANDROID_OUT" ]; then |
| 46 | find $ANDROID_OUT -name '*.gcda' -delete |
| 47 | fi |
| 48 | } |
| 49 | |
| 50 | function clearGcnoFiles() { |
| 51 | if [ -d "$ANDROID_OUT" ]; then |
| 52 | find $ANDROID_OUT -name '*.gcno' -delete |
| 53 | fi |
| 54 | } |
| 55 | |
Dan Albert | cf347cc | 2015-01-24 13:40:42 -0800 | [diff] [blame] | 56 | if [ "$1" = "--clean" ]; then |
Oliver Nguyen | f7421ae | 2019-10-02 12:06:04 -0700 | [diff] [blame] | 57 | echo "Clearing gcda and gcno files from the local build." |
| 58 | clearGcdaFiles |
| 59 | clearGcnoFiles |
Dan Albert | cf347cc | 2015-01-24 13:40:42 -0800 | [diff] [blame] | 60 | exit 0 |
| 61 | fi |
| 62 | |
| 63 | if [ "$1" = "--prep" ]; then |
Oliver Nguyen | f7421ae | 2019-10-02 12:06:04 -0700 | [diff] [blame] | 64 | echo "Clearing gcda files from the local build." |
| 65 | clearGcdaFiles |
| 66 | exit 0 |
| 67 | fi |
Dan Albert | f1d27e2 | 2015-04-14 13:46:57 -0700 | [diff] [blame] | 68 | |
Oliver Nguyen | f7421ae | 2019-10-02 12:06:04 -0700 | [diff] [blame] | 69 | adb root |
Dan Albert | f1d27e2 | 2015-04-14 13:46:57 -0700 | [diff] [blame] | 70 | |
Oliver Nguyen | f7421ae | 2019-10-02 12:06:04 -0700 | [diff] [blame] | 71 | if [ "$1" = "--clean-device" ]; then |
| 72 | echo "Resetting coverage on the device..." |
| 73 | adb shell kill -37 -1 |
| 74 | echo "Waiting $FLUSH_SLEEP seconds for coverage to be written..." |
| 75 | sleep $FLUSH_SLEEP |
| 76 | adb shell rm -rf /data/misc/trace/* |
| 77 | exit 0 |
| 78 | fi |
| 79 | |
| 80 | if [ "$1" = "--flush" ]; then |
| 81 | shift |
| 82 | if [ -z $@ ]; then |
| 83 | echo "Flushing coverage for all processes on the device..." |
| 84 | adb shell kill -37 -1 |
| 85 | else |
| 86 | echo "Flushing coverage for [$@] on the device..." |
| 87 | adb shell kill -37 $(adb shell pidof $@) |
| 88 | fi |
| 89 | echo "Waiting $FLUSH_SLEEP seconds for coverage to be written..." |
| 90 | sleep $FLUSH_SLEEP |
Dan Albert | cf347cc | 2015-01-24 13:40:42 -0800 | [diff] [blame] | 91 | exit 0 |
| 92 | fi |
| 93 | |
Dan Albert | 7d133d2 | 2014-09-25 11:23:30 -0700 | [diff] [blame] | 94 | which lcov >/dev/null 2>/dev/null |
Dan Albert | 98e2c64 | 2014-09-19 14:40:57 -0700 | [diff] [blame] | 95 | if [ $? -ne 0 ]; then |
| 96 | echo 'lcov not found: running `sudo apt-get install lcov`' |
| 97 | sudo apt-get install lcov |
| 98 | fi |
| 99 | |
| 100 | cd $ANDROID_BUILD_TOP |
Dan Albert | 98e2c64 | 2014-09-19 14:40:57 -0700 | [diff] [blame] | 101 | DIR=$(mktemp -d covreport-XXXXXX) |
Dan Albert | 72324d8 | 2015-01-24 13:47:03 -0800 | [diff] [blame] | 102 | |
Oliver Nguyen | f7421ae | 2019-10-02 12:06:04 -0700 | [diff] [blame] | 103 | # Build a coverage report for each euid that has reported coverage. |
| 104 | COVERAGE_REPORTS= |
| 105 | for USER_ID in $(adb shell ls /data/misc/trace) |
| 106 | do |
| 107 | FILE=cov-$USER_ID.info |
| 108 | adb shell tar -czf - -C /data/misc/trace/$USER_ID/proc/self/cwd $ANDROID_OUT | tar zxvf - |
Dan Albert | 72324d8 | 2015-01-24 13:47:03 -0800 | [diff] [blame] | 109 | |
Oliver Nguyen | f7421ae | 2019-10-02 12:06:04 -0700 | [diff] [blame] | 110 | lcov -c -d $ANDROID_OUT -o $DIR/$FILE --gcov-tool=llvm-gcov $@ |
| 111 | COVERAGE_REPORTS="-a $DIR/$FILE $COVERAGE_REPORTS" |
| 112 | |
| 113 | clearGcdaFiles |
| 114 | done |
| 115 | |
| 116 | FILE=merged.info |
| 117 | lcov $COVERAGE_REPORTS -o $DIR/$FILE |
Dan Albert | 98e2c64 | 2014-09-19 14:40:57 -0700 | [diff] [blame] | 118 | echo "Generating coverage report at $DIR" |
| 119 | genhtml -q -o $DIR $DIR/$FILE |
| 120 | xdg-open $DIR/index.html >/dev/null |