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