Pirama Arumuga Nainar | c41549f | 2021-03-25 16:31:17 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 202121 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # acov-llvm.py is a tool for gathering coverage information from a device and |
| 18 | # generating an LLVM coverage report from that information. To use: |
| 19 | # |
| 20 | # This script would work only when the device image was built with the following |
| 21 | # build variables: |
| 22 | # CLANG_COVERAGE=true NATIVE_COVERAGE_PATHS="<list-of-paths>" |
| 23 | # |
| 24 | # 1. [optional] Reset coverage information on the device |
| 25 | # $ acov-llvm.py clean-device |
| 26 | # |
| 27 | # 2. Run tests |
| 28 | # |
| 29 | # 3. Flush coverage |
| 30 | # from select daemons and system processes on the device |
| 31 | # $ acov-llvm.py flush [list of process names] |
| 32 | # or from all processes on the device: |
| 33 | # $ acov-llvm.py flush |
| 34 | # |
| 35 | # 4. pull coverage from device and generate coverage report |
| 36 | # $ acov-llvm.py report -s <one-or-more-source-paths-in-$ANDROID_BUILD_TOP \ |
| 37 | # -b <one-or-more-binaries-in-$OUT> \ |
| 38 | # E.g.: |
| 39 | # development/scripts/acov-llvm.py report \ |
| 40 | # -s bionic \ |
| 41 | # -b \ |
| 42 | # $OUT/symbols/apex/com.android.runtime/lib/bionic/libc.so \ |
| 43 | # $OUT/symbols/apex/com.android.runtime/lib/bionic/libm.so |
| 44 | |
| 45 | import argparse |
| 46 | import logging |
| 47 | import os |
| 48 | import re |
| 49 | import subprocess |
| 50 | import time |
| 51 | import tempfile |
| 52 | |
| 53 | from pathlib import Path |
| 54 | |
| 55 | FLUSH_SLEEP = 60 |
| 56 | |
| 57 | |
| 58 | def android_build_top(): |
| 59 | return Path(os.environ.get('ANDROID_BUILD_TOP', None)) |
| 60 | |
| 61 | |
| 62 | def _get_clang_revision(): |
Pirama Arumuga Nainar | beb6fb7 | 2021-04-27 21:55:54 -0700 | [diff] [blame^] | 63 | regex = r'ClangDefaultVersion\s+= "(?P<rev>clang-r\d+[a-z]?)"' |
Pirama Arumuga Nainar | c41549f | 2021-03-25 16:31:17 -0700 | [diff] [blame] | 64 | global_go = android_build_top() / 'build/soong/cc/config/global.go' |
| 65 | with open(global_go) as infile: |
| 66 | match = re.search(regex, infile.read()) |
| 67 | |
| 68 | if match is None: |
| 69 | raise RuntimeError(f'Parsing clang info from {global_go} failed') |
| 70 | return match.group('rev') |
| 71 | |
| 72 | |
| 73 | CLANG_TOP = android_build_top() / 'prebuilts/clang/host/linux-x86/' \ |
| 74 | / _get_clang_revision() |
| 75 | LLVM_PROFDATA_PATH = CLANG_TOP / 'bin' / 'llvm-profdata' |
| 76 | LLVM_COV_PATH = CLANG_TOP / 'bin' / 'llvm-cov' |
| 77 | |
| 78 | |
| 79 | def check_output(cmd, *args, **kwargs): |
| 80 | """subprocess.check_output with logging.""" |
| 81 | cmd_str = cmd if isinstance(cmd, str) else ' '.join(cmd) |
| 82 | logging.debug(cmd_str) |
| 83 | return subprocess.run( |
| 84 | cmd, *args, **kwargs, check=True, stdout=subprocess.PIPE).stdout |
| 85 | |
| 86 | |
| 87 | def adb_shell(cmd, *args, **kwargs): |
| 88 | """call 'adb shell <cmd>' with logging.""" |
Pirama Arumuga Nainar | beb6fb7 | 2021-04-27 21:55:54 -0700 | [diff] [blame^] | 89 | return check_output(['adb', 'shell'] + cmd, *args, **kwargs) |
| 90 | |
| 91 | |
| 92 | def send_flush_signal(pids=None): |
| 93 | |
| 94 | def _has_handler_sig37(pid): |
| 95 | try: |
| 96 | status = adb_shell(['cat', f'/proc/{pid}/status'], |
| 97 | text=True, |
| 98 | stderr=subprocess.DEVNULL) |
| 99 | except subprocess.CalledProcessError: |
| 100 | logging.warning(f'Process {pid} is no longer active') |
| 101 | return False |
| 102 | |
| 103 | status = status.split('\n') |
| 104 | sigcgt = [ |
| 105 | line.split(':\t')[1] for line in status if line.startswith('SigCgt') |
| 106 | ] |
| 107 | if not sigcgt: |
| 108 | logging.warning(f'Cannot find \'SigCgt:\' in /proc/{pid}/status') |
| 109 | return False |
| 110 | return int(sigcgt[0], base=16) & (1 << 36) |
| 111 | |
| 112 | if not pids: |
| 113 | output = adb_shell(['ps', '-eo', 'pid'], text=True) |
| 114 | pids = [pid.strip() for pid in output.split()] |
| 115 | pids = pids[1:] # ignore the column header |
| 116 | pids = [pid for pid in pids if _has_handler_sig37(pid)] |
| 117 | |
| 118 | if not pids: |
| 119 | logging.warning( |
| 120 | f'couldn\'t find any process with handler for signal 37') |
| 121 | |
| 122 | adb_shell(['kill', '-37'] + pids) |
Pirama Arumuga Nainar | c41549f | 2021-03-25 16:31:17 -0700 | [diff] [blame] | 123 | |
| 124 | |
| 125 | def do_clean_device(args): |
Pirama Arumuga Nainar | beb6fb7 | 2021-04-27 21:55:54 -0700 | [diff] [blame^] | 126 | adb_shell(['root']) |
| 127 | |
Pirama Arumuga Nainar | c41549f | 2021-03-25 16:31:17 -0700 | [diff] [blame] | 128 | logging.info('resetting coverage on device') |
Pirama Arumuga Nainar | beb6fb7 | 2021-04-27 21:55:54 -0700 | [diff] [blame^] | 129 | send_flush_signal() |
Pirama Arumuga Nainar | c41549f | 2021-03-25 16:31:17 -0700 | [diff] [blame] | 130 | |
| 131 | logging.info( |
| 132 | f'sleeping for {FLUSH_SLEEP} seconds for coverage to be written') |
| 133 | time.sleep(FLUSH_SLEEP) |
| 134 | |
| 135 | logging.info('deleting coverage data from device') |
| 136 | adb_shell(['rm', '-rf', '/data/misc/trace/*.profraw']) |
| 137 | |
| 138 | |
| 139 | def do_flush(args): |
Pirama Arumuga Nainar | beb6fb7 | 2021-04-27 21:55:54 -0700 | [diff] [blame^] | 140 | adb_shell(['root']) |
| 141 | |
Pirama Arumuga Nainar | c41549f | 2021-03-25 16:31:17 -0700 | [diff] [blame] | 142 | if args.procnames: |
| 143 | pids = adb_shell(['pidof'] + args.procnames, text=True).split() |
| 144 | logging.info(f'flushing coverage for pids: {pids}') |
| 145 | else: |
Pirama Arumuga Nainar | beb6fb7 | 2021-04-27 21:55:54 -0700 | [diff] [blame^] | 146 | pids = None |
Pirama Arumuga Nainar | c41549f | 2021-03-25 16:31:17 -0700 | [diff] [blame] | 147 | logging.info('flushing coverage for all processes on device') |
| 148 | |
Pirama Arumuga Nainar | beb6fb7 | 2021-04-27 21:55:54 -0700 | [diff] [blame^] | 149 | send_flush_signal(pids) |
Pirama Arumuga Nainar | c41549f | 2021-03-25 16:31:17 -0700 | [diff] [blame] | 150 | |
| 151 | logging.info( |
| 152 | f'sleeping for {FLUSH_SLEEP} seconds for coverage to be written') |
| 153 | time.sleep(FLUSH_SLEEP) |
| 154 | |
| 155 | |
| 156 | def do_report(args): |
Pirama Arumuga Nainar | beb6fb7 | 2021-04-27 21:55:54 -0700 | [diff] [blame^] | 157 | adb_shell(['root']) |
| 158 | |
Pirama Arumuga Nainar | c41549f | 2021-03-25 16:31:17 -0700 | [diff] [blame] | 159 | temp_dir = tempfile.mkdtemp( |
| 160 | prefix='covreport-', dir=os.environ.get('ANDROID_BUILD_TOP', None)) |
| 161 | logging.info(f'generating coverage report in {temp_dir}') |
| 162 | |
| 163 | # Pull coverage files from /data/misc/trace on the device |
| 164 | compressed = adb_shell(['tar', '-czf', '-', '-C', '/data/misc', 'trace']) |
| 165 | check_output(['tar', 'zxvf', '-', '-C', temp_dir], input=compressed) |
| 166 | |
| 167 | # Call llvm-profdata followed by llvm-cov |
| 168 | profdata = f'{temp_dir}/merged.profdata' |
| 169 | check_output( |
| 170 | f'{LLVM_PROFDATA_PATH} merge --failure-mode=all --output={profdata} {temp_dir}/trace/*.profraw', |
| 171 | shell=True) |
| 172 | |
| 173 | object_flags = [args.binary[0]] + ['--object=' + b for b in args.binary[1:]] |
| 174 | source_dirs = ['/proc/self/cwd/' + s for s in args.source_dir] |
| 175 | |
| 176 | check_output([ |
| 177 | str(LLVM_COV_PATH), 'show', f'--instr-profile={profdata}', |
| 178 | '--format=html', f'--output-dir={temp_dir}/html', |
| 179 | '--show-region-summary=false' |
| 180 | ] + object_flags + source_dirs) |
| 181 | |
| 182 | |
| 183 | def parse_args(): |
| 184 | parser = argparse.ArgumentParser() |
| 185 | parser.add_argument( |
| 186 | '-v', |
| 187 | '--verbose', |
| 188 | action='store_true', |
| 189 | default=False, |
| 190 | help='enable debug logging') |
| 191 | |
| 192 | subparsers = parser.add_subparsers(dest='command', required=True) |
| 193 | |
| 194 | clean_device = subparsers.add_parser( |
| 195 | 'clean-device', help='reset coverage on device') |
| 196 | clean_device.set_defaults(func=do_clean_device) |
| 197 | |
| 198 | flush = subparsers.add_parser( |
| 199 | 'flush', help='flush coverage for processes on device') |
| 200 | flush.add_argument( |
| 201 | 'procnames', |
| 202 | nargs='*', |
| 203 | metavar='PROCNAME', |
| 204 | help='flush coverage for one or more processes with name PROCNAME') |
| 205 | flush.set_defaults(func=do_flush) |
| 206 | |
| 207 | report = subparsers.add_parser( |
| 208 | 'report', help='fetch coverage from device and generate report') |
| 209 | report.add_argument( |
| 210 | '-b', |
| 211 | '--binary', |
| 212 | nargs='+', |
| 213 | metavar='BINARY', |
| 214 | action='extend', |
| 215 | required=True, |
| 216 | help='generate coverage report for BINARY') |
| 217 | report.add_argument( |
| 218 | '-s', |
| 219 | '--source-dir', |
| 220 | nargs='+', |
| 221 | action='extend', |
| 222 | metavar='PATH', |
| 223 | required=True, |
| 224 | help='generate coverage report for source files in PATH') |
| 225 | report.set_defaults(func=do_report) |
| 226 | return parser.parse_args() |
| 227 | |
| 228 | |
| 229 | def main(): |
| 230 | args = parse_args() |
| 231 | if args.verbose: |
| 232 | logging.basicConfig(level=logging.DEBUG) |
| 233 | |
| 234 | args.func(args) |
| 235 | |
| 236 | |
| 237 | if __name__ == '__main__': |
| 238 | main() |