• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright (C) 2021 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# 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
45import argparse
46import logging
47import os
48import re
49import subprocess
50import time
51import tempfile
52
53from pathlib import Path
54
55FLUSH_SLEEP = 60
56
57
58def android_build_top():
59    return Path(os.environ.get('ANDROID_BUILD_TOP', None))
60
61
62def _get_clang_revision():
63    version_output = subprocess.check_output(
64        android_build_top() / 'build/soong/scripts/get_clang_version.py',
65        text=True)
66    return version_output.strip()
67
68
69CLANG_TOP = android_build_top() / 'prebuilts/clang/host/linux-x86/' \
70        / _get_clang_revision()
71LLVM_PROFDATA_PATH = CLANG_TOP / 'bin' / 'llvm-profdata'
72LLVM_COV_PATH = CLANG_TOP / 'bin' / 'llvm-cov'
73
74
75def check_output(cmd, *args, **kwargs):
76    """subprocess.check_output with logging."""
77    cmd_str = cmd if isinstance(cmd, str) else ' '.join(cmd)
78    logging.debug(cmd_str)
79    return subprocess.run(
80        cmd, *args, **kwargs, check=True, stdout=subprocess.PIPE).stdout
81
82
83def adb(cmd, *args, **kwargs):
84    """call 'adb <cmd>' with logging."""
85    return check_output(['adb'] + cmd, *args, **kwargs)
86
87
88def adb_root(*args, **kwargs):
89    """call 'adb root' with logging."""
90    return adb(['root'], *args, **kwargs)
91
92
93def adb_shell(cmd, *args, **kwargs):
94    """call 'adb shell <cmd>' with logging."""
95    return adb(['shell'] + cmd, *args, **kwargs)
96
97
98def send_flush_signal(pids=None):
99
100    def _has_handler_sig37(pid):
101        try:
102            status = adb_shell(['cat', f'/proc/{pid}/status'],
103                               text=True,
104                               stderr=subprocess.DEVNULL)
105        except subprocess.CalledProcessError:
106            logging.warning(f'Process {pid} is no longer active')
107            return False
108
109        status = status.split('\n')
110        sigcgt = [
111            line.split(':\t')[1] for line in status if line.startswith('SigCgt')
112        ]
113        if not sigcgt:
114            logging.warning(f'Cannot find \'SigCgt:\' in /proc/{pid}/status')
115            return False
116        return int(sigcgt[0], base=16) & (1 << 36)
117
118    if not pids:
119        output = adb_shell(['ps', '-eo', 'pid'], text=True)
120        pids = [pid.strip() for pid in output.split()]
121        pids = pids[1:]  # ignore the column header
122    pids = [pid for pid in pids if _has_handler_sig37(pid)]
123
124    if not pids:
125        logging.warning(
126            f'couldn\'t find any process with handler for signal 37')
127
128    # Some processes may have exited after we run `ps` command above - ignore failures when
129    # sending flush signal.
130    # We rely on kill(1) sending the signal to all pids on the command line even if some don't
131    # exist.  This is true of toybox and "probably implied" by POSIX, even if not explicitly called
132    # out [https://pubs.opengroup.org/onlinepubs/9699919799/utilities/kill.html].
133    try:
134        adb_shell(['kill', '-37'] + pids)
135    except subprocess.CalledProcessError:
136        logging.warning('Sending flush signal failed - some pids no longer active')
137
138
139def do_clean_device(args):
140    adb_root()
141
142    logging.info('resetting coverage on device')
143    send_flush_signal()
144
145    logging.info(
146        f'sleeping for {FLUSH_SLEEP} seconds for coverage to be written')
147    time.sleep(FLUSH_SLEEP)
148
149    logging.info('deleting coverage data from device')
150    adb_shell(['rm', '-rf', '/data/misc/trace/*.profraw'])
151
152
153def do_flush(args):
154    adb_root()
155
156    if args.procnames:
157        pids = adb_shell(['pidof'] + args.procnames, text=True).split()
158        logging.info(f'flushing coverage for pids: {pids}')
159    else:
160        pids = None
161        logging.info('flushing coverage for all processes on device')
162
163    send_flush_signal(pids)
164
165    logging.info(
166        f'sleeping for {FLUSH_SLEEP} seconds for coverage to be written')
167    time.sleep(FLUSH_SLEEP)
168
169
170def do_report(args):
171    adb_root()
172
173    temp_dir = tempfile.mkdtemp(
174        prefix='covreport-', dir=os.environ.get('ANDROID_BUILD_TOP', None))
175    logging.info(f'generating coverage report in {temp_dir}')
176
177    # Pull coverage files from /data/misc/trace on the device
178    compressed = adb_shell(['tar', '-czf', '-', '-C', '/data/misc', 'trace'])
179    check_output(['tar', 'zxvf', '-', '-C', temp_dir], input=compressed)
180
181    # Call llvm-profdata followed by llvm-cov
182    profdata = f'{temp_dir}/merged.profdata'
183    check_output(
184        f'{LLVM_PROFDATA_PATH} merge --failure-mode=all --output={profdata} {temp_dir}/trace/*.profraw',
185        shell=True)
186
187    object_flags = [args.binary[0]] + ['--object=' + b for b in args.binary[1:]]
188    source_dirs = ['/proc/self/cwd/' + s for s in args.source_dir]
189
190    output_dir = f'{temp_dir}/html'
191
192    check_output([
193        str(LLVM_COV_PATH), 'show', f'--instr-profile={profdata}',
194        '--format=html', f'--output-dir={output_dir}',
195        '--show-region-summary=false'
196    ] + object_flags + source_dirs)
197
198    print(f'Coverage report data written in {output_dir}')
199
200
201def parse_args():
202    parser = argparse.ArgumentParser()
203    parser.add_argument(
204        '-v',
205        '--verbose',
206        action='store_true',
207        default=False,
208        help='enable debug logging')
209
210    subparsers = parser.add_subparsers(dest='command', required=True)
211
212    clean_device = subparsers.add_parser(
213        'clean-device', help='reset coverage on device')
214    clean_device.set_defaults(func=do_clean_device)
215
216    flush = subparsers.add_parser(
217        'flush', help='flush coverage for processes on device')
218    flush.add_argument(
219        'procnames',
220        nargs='*',
221        metavar='PROCNAME',
222        help='flush coverage for one or more processes with name PROCNAME')
223    flush.set_defaults(func=do_flush)
224
225    report = subparsers.add_parser(
226        'report', help='fetch coverage from device and generate report')
227    report.add_argument(
228        '-b',
229        '--binary',
230        nargs='+',
231        metavar='BINARY',
232        action='extend',
233        required=True,
234        help='generate coverage report for BINARY')
235    report.add_argument(
236        '-s',
237        '--source-dir',
238        nargs='+',
239        action='extend',
240        metavar='PATH',
241        required=True,
242        help='generate coverage report for source files in PATH')
243    report.set_defaults(func=do_report)
244    return parser.parse_args()
245
246
247def main():
248    args = parse_args()
249    if args.verbose:
250        logging.basicConfig(level=logging.DEBUG)
251
252    args.func(args)
253
254
255if __name__ == '__main__':
256    main()
257