blob: 22753fb05578e2ae7f41d44e6e4776fb01bcc3f0 [file] [log] [blame]
Ian Zernycbcd5032021-10-15 13:34:28 +02001#!/usr/bin/env python3
Morten Krogh-Jespersen017a7002019-01-10 14:14:17 +01002# Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +01006import argparse
Ian Zernycbcd5032021-10-15 13:34:28 +02007import os
Morten Krogh-Jespersen017a7002019-01-10 14:14:17 +01008import subprocess
9import sys
Ian Zerny9ff31b72021-04-22 11:36:26 +020010
Ian Zernycbcd5032021-10-15 13:34:28 +020011import jdk
Morten Krogh-Jespersen017a7002019-01-10 14:14:17 +010012import utils
13
Ian Zerny9ff31b72021-04-22 11:36:26 +020014
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +010015def parse_arguments():
16 parser = argparse.ArgumentParser(
17 description = 'R8lib wrapper for retrace tool.')
18 parser.add_argument(
19 '-c',
20 '--commit_hash',
21 help='Commit hash to download r8lib map file for.',
22 default=None)
23 parser.add_argument(
24 '--version',
25 help='Version to download r8lib map file for.',
26 default=None)
27 parser.add_argument(
Morten Krogh-Jespersen08701bf2019-12-02 21:31:04 +010028 '--tag',
29 help='Tag to download r8lib map file for.',
30 default=None)
31 parser.add_argument(
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +010032 '--map',
33 help='Path to r8lib map.',
Ian Zernycbcd5032021-10-15 13:34:28 +020034 default=None)
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +010035 parser.add_argument(
Morten Krogh-Jespersen3aa4bff82020-09-30 16:43:26 +020036 '--no-r8lib',
37 default=False,
38 action='store_true',
39 help='Use r8.jar and not r8lib.jar.')
40 parser.add_argument(
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +010041 '--stacktrace',
Ian Zernycbcd5032021-10-15 13:34:28 +020042 help='Path to stacktrace file (read from stdin if not passed).',
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +010043 default=None)
Christoffer Quist Adamsen7bf60342020-11-09 14:00:27 +010044 parser.add_argument(
45 '--quiet',
46 default=None,
47 action='store_true',
48 help='Disables diagnostics printing to stdout.')
Morten Krogh-Jespersenc9fd21d2020-12-14 09:25:11 +010049 parser.add_argument(
Morten Krogh-Jespersenb6b2eb52021-08-11 09:33:20 +020050 '--debug-agent',
51 default=None,
52 action='store_true',
53 help='Attach a debug-agent to the retracer java process.')
54 parser.add_argument(
55 '--regex',
56 default=None,
57 help='Sets a custom regular expression used for parsing'
58 )
Christoffer Quist Adamsen5e8f04e2021-09-24 08:48:31 +020059 parser.add_argument(
60 '--verbose',
61 default=None,
62 action='store_true',
63 help='Enables verbose retracing.')
Morten Krogh-Jespersenb4713b92022-01-27 13:35:17 +010064 parser.add_argument(
65 '--disable-map-validation',
66 default=None,
67 action='store_true',
68 help='Disable validation of map hash.')
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +010069 return parser.parse_args()
70
71
Ian Zernycbcd5032021-10-15 13:34:28 +020072def get_map_file(args, temp):
73 # default to using the specified map file.
74 if args.map:
75 return args.map
76
77 # next try to extract it from the tag/version options.
78 map_path = utils.find_cloud_storage_file_from_options('r8lib.jar.map', args)
79 if map_path:
80 return map_path
81
82 # next try to extract it from the stack-trace source-file content.
83 if not args.stacktrace:
84 if not args.quiet:
85 print('Waiting for stack-trace input...')
86 args.stacktrace = os.path.join(temp, 'stacktrace.txt')
87 open(args.stacktrace, 'w').writelines(sys.stdin.readlines())
88
89 r8_source_file = None
90 for line in open(args.stacktrace, 'r'):
91 start = line.rfind("(R8_")
92 if start > 0:
93 end = line.find(":", start)
94 content = line[start + 1: end]
95 if r8_source_file:
96 if content != r8_source_file:
97 print('WARNING: there are multiple distinct R8 source files:')
98 print(' ' + r8_source_file)
99 print(' ' + content)
100 else:
101 r8_source_file = content
102
103 if r8_source_file:
104 (header, r8_version_or_hash, maphash) = r8_source_file.split('_')
105 if len(r8_version_or_hash) < 40:
106 args.version = r8_version_or_hash
107 else:
108 args.commit_hash = r8_version_or_hash
109 map_path = None
110 try:
111 map_path = utils.find_cloud_storage_file_from_options('r8lib.jar.map', args)
112 except Exception as e:
113 print(e)
114 print('WARNING: Falling back to using local mapping file.')
115
Morten Krogh-Jespersenb4713b92022-01-27 13:35:17 +0100116 if map_path and not args.disable_map_validation:
Ian Zernycbcd5032021-10-15 13:34:28 +0200117 check_maphash(map_path, maphash)
118 return map_path
119
120 # If no other map file was found, use the local mapping file.
121 return utils.R8LIB_JAR + '.map'
122
123
124def check_maphash(mapping_path, maphash):
125 map_hash_header = "# pg_map_hash: SHA-256 "
126 for line in open(mapping_path, 'r'):
127 if line.startswith(map_hash_header):
128 infile_maphash = line[len(map_hash_header):].strip()
129 if infile_maphash != maphash:
130 print('ERROR: The mapping file hash does not match the R8 line')
131 print(' In mapping file: ' + infile_maphash)
132 print(' In source file: ' + maphash)
133 sys.exit(1)
134
135
Morten Krogh-Jespersen017a7002019-01-10 14:14:17 +0100136def main():
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +0100137 args = parse_arguments()
Ian Zernycbcd5032021-10-15 13:34:28 +0200138 with utils.TempDir() as temp:
139 map_path = get_map_file(args, temp)
140 return run(
Christoffer Quist Adamsen4d38d032021-04-20 12:31:31 +0200141 map_path,
Morten Krogh-Jespersen3aa4bff82020-09-30 16:43:26 +0200142 args.stacktrace,
Christoffer Quist Adamsen7bf60342020-11-09 14:00:27 +0100143 args.no_r8lib,
Morten Krogh-Jespersenc9fd21d2020-12-14 09:25:11 +0100144 quiet=args.quiet,
Morten Krogh-Jespersenb6b2eb52021-08-11 09:33:20 +0200145 debug=args.debug_agent,
Christoffer Quist Adamsen5e8f04e2021-09-24 08:48:31 +0200146 regex=args.regex,
147 verbose=args.verbose)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100148
Ian Zernycbcd5032021-10-15 13:34:28 +0200149
Christoffer Quist Adamsen5e8f04e2021-09-24 08:48:31 +0200150def run(map_path, stacktrace, no_r8lib, quiet=False, debug=False, regex=None, verbose=False):
Morten Krogh-Jespersenc9fd21d2020-12-14 09:25:11 +0100151 retrace_args = [jdk.GetJavaExecutable()]
152
153 if debug:
Christoffer Quist Adamsen4d38d032021-04-20 12:31:31 +0200154 retrace_args.append(
155 '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005')
Morten Krogh-Jespersenc9fd21d2020-12-14 09:25:11 +0100156
157 retrace_args += [
Morten Krogh-Jespersen89a5b062020-01-07 14:50:40 +0100158 '-cp',
Morten Krogh-Jespersen501efac2021-10-25 10:54:05 +0200159 utils.R8_JAR if no_r8lib else utils.R8RETRACE_JAR,
Morten Krogh-Jespersen89a5b062020-01-07 14:50:40 +0100160 'com.android.tools.r8.retrace.Retrace',
Morten Krogh-Jespersen3aa4bff82020-09-30 16:43:26 +0200161 map_path
Ian Zerny3f54e222019-02-12 10:51:17 +0100162 ]
Morten Krogh-Jespersen89a5b062020-01-07 14:50:40 +0100163
Morten Krogh-Jespersenb6b2eb52021-08-11 09:33:20 +0200164 if regex:
165 retrace_args.append('--regex')
166 retrace_args.append(regex)
167
Morten Krogh-Jespersen691793a2021-01-08 11:36:20 +0100168 if quiet:
169 retrace_args.append('--quiet')
170
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100171 if stacktrace:
172 retrace_args.append(stacktrace)
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +0100173
Christoffer Quist Adamsen5e8f04e2021-09-24 08:48:31 +0200174 if verbose:
175 retrace_args.append('--verbose')
176
Christoffer Quist Adamsen7bf60342020-11-09 14:00:27 +0100177 utils.PrintCmd(retrace_args, quiet=quiet)
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +0100178 return subprocess.call(retrace_args)
179
Morten Krogh-Jespersen017a7002019-01-10 14:14:17 +0100180
181if __name__ == '__main__':
182 sys.exit(main())