blob: cbb2d33320668db5f2565fe847d9f4a116b10855 [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
Ian Zerny3af58152023-03-13 10:52:27 +01006from os import path
7
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +01008import argparse
Ian Zernycbcd5032021-10-15 13:34:28 +02009import os
Morten Krogh-Jespersen017a7002019-01-10 14:14:17 +010010import subprocess
11import sys
Ian Zerny9ff31b72021-04-22 11:36:26 +020012
Ian Zernycbcd5032021-10-15 13:34:28 +020013import jdk
Morten Krogh-Jespersen017a7002019-01-10 14:14:17 +010014import utils
15
Ian Zerny9ff31b72021-04-22 11:36:26 +020016
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +010017def parse_arguments():
18 parser = argparse.ArgumentParser(
19 description = 'R8lib wrapper for retrace tool.')
20 parser.add_argument(
21 '-c',
Søren Gjesse8ebfddf2022-04-29 08:47:30 +020022 '--commit-hash',
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +010023 '--commit_hash',
24 help='Commit hash to download r8lib map file for.',
25 default=None)
26 parser.add_argument(
27 '--version',
28 help='Version to download r8lib map file for.',
29 default=None)
30 parser.add_argument(
Morten Krogh-Jespersen08701bf2019-12-02 21:31:04 +010031 '--tag',
32 help='Tag to download r8lib map file for.',
33 default=None)
34 parser.add_argument(
Søren Gjesse8ebfddf2022-04-29 08:47:30 +020035 '--exclude-deps', '--exclude_deps',
36 default=None,
37 action='store_true',
38 help='Use the exclude-deps version of the mapping file.')
39 parser.add_argument(
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +010040 '--map',
41 help='Path to r8lib map.',
Ian Zernycbcd5032021-10-15 13:34:28 +020042 default=None)
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +010043 parser.add_argument(
Morten Krogh-Jespersen3aa4bff82020-09-30 16:43:26 +020044 '--no-r8lib',
Søren Gjesse8ebfddf2022-04-29 08:47:30 +020045 '--no_r8lib',
Morten Krogh-Jespersen3aa4bff82020-09-30 16:43:26 +020046 default=False,
47 action='store_true',
48 help='Use r8.jar and not r8lib.jar.')
49 parser.add_argument(
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +010050 '--stacktrace',
Ian Zernycbcd5032021-10-15 13:34:28 +020051 help='Path to stacktrace file (read from stdin if not passed).',
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +010052 default=None)
Christoffer Quist Adamsen7bf60342020-11-09 14:00:27 +010053 parser.add_argument(
54 '--quiet',
55 default=None,
56 action='store_true',
57 help='Disables diagnostics printing to stdout.')
Morten Krogh-Jespersenc9fd21d2020-12-14 09:25:11 +010058 parser.add_argument(
Morten Krogh-Jespersenb6b2eb52021-08-11 09:33:20 +020059 '--debug-agent',
Søren Gjesse8ebfddf2022-04-29 08:47:30 +020060 '--debug_agent',
Morten Krogh-Jespersenb6b2eb52021-08-11 09:33:20 +020061 default=None,
62 action='store_true',
63 help='Attach a debug-agent to the retracer java process.')
64 parser.add_argument(
65 '--regex',
66 default=None,
67 help='Sets a custom regular expression used for parsing'
68 )
Christoffer Quist Adamsen5e8f04e2021-09-24 08:48:31 +020069 parser.add_argument(
70 '--verbose',
71 default=None,
72 action='store_true',
73 help='Enables verbose retracing.')
Morten Krogh-Jespersenb4713b92022-01-27 13:35:17 +010074 parser.add_argument(
75 '--disable-map-validation',
76 default=None,
77 action='store_true',
78 help='Disable validation of map hash.')
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +010079 return parser.parse_args()
80
81
Ian Zernycbcd5032021-10-15 13:34:28 +020082def get_map_file(args, temp):
83 # default to using the specified map file.
84 if args.map:
85 return args.map
86
87 # next try to extract it from the tag/version options.
88 map_path = utils.find_cloud_storage_file_from_options('r8lib.jar.map', args)
89 if map_path:
90 return map_path
91
92 # next try to extract it from the stack-trace source-file content.
93 if not args.stacktrace:
94 if not args.quiet:
95 print('Waiting for stack-trace input...')
96 args.stacktrace = os.path.join(temp, 'stacktrace.txt')
97 open(args.stacktrace, 'w').writelines(sys.stdin.readlines())
98
99 r8_source_file = None
100 for line in open(args.stacktrace, 'r'):
101 start = line.rfind("(R8_")
102 if start > 0:
103 end = line.find(":", start)
104 content = line[start + 1: end]
105 if r8_source_file:
106 if content != r8_source_file:
107 print('WARNING: there are multiple distinct R8 source files:')
108 print(' ' + r8_source_file)
109 print(' ' + content)
110 else:
111 r8_source_file = content
112
113 if r8_source_file:
114 (header, r8_version_or_hash, maphash) = r8_source_file.split('_')
Ian Zernycf8ef512022-05-04 14:54:16 +0200115 # If the command-line specified --exclude-deps then assume it is as previous
116 # versions will not be marked as such in the source-file line.
117 is_excldeps = args.exclude_deps
118 excldeps_start = r8_version_or_hash.find('+excldeps')
119 if (excldeps_start > 0):
120 is_excldeps = True
121 r8_version_or_hash = r8_version_or_hash[0:excldeps_start]
Ian Zernycbcd5032021-10-15 13:34:28 +0200122 if len(r8_version_or_hash) < 40:
123 args.version = r8_version_or_hash
124 else:
125 args.commit_hash = r8_version_or_hash
126 map_path = None
Ian Zerny3af58152023-03-13 10:52:27 +0100127 if path.exists(utils.R8LIB_MAP) and get_hash_from_map_file(utils.R8LIB_MAP) == maphash:
Rico Wind158ef9f2022-05-19 11:08:30 +0200128 return utils.R8LIB_MAP
129
Ian Zernycbcd5032021-10-15 13:34:28 +0200130 try:
Søren Gjesse8ebfddf2022-04-29 08:47:30 +0200131 map_path = utils.find_cloud_storage_file_from_options(
Ian Zernycf8ef512022-05-04 14:54:16 +0200132 'r8lib' + ('-exclude-deps' if is_excldeps else '') + '.jar.map', args)
Ian Zernycbcd5032021-10-15 13:34:28 +0200133 except Exception as e:
134 print(e)
135 print('WARNING: Falling back to using local mapping file.')
136
Morten Krogh-Jespersenb4713b92022-01-27 13:35:17 +0100137 if map_path and not args.disable_map_validation:
Søren Gjesse8ebfddf2022-04-29 08:47:30 +0200138 check_maphash(map_path, maphash, args)
Ian Zernycbcd5032021-10-15 13:34:28 +0200139 return map_path
140
141 # If no other map file was found, use the local mapping file.
Rico Wind158ef9f2022-05-19 11:08:30 +0200142 return utils.R8LIB_MAP
Ian Zernycbcd5032021-10-15 13:34:28 +0200143
144
Søren Gjesse8ebfddf2022-04-29 08:47:30 +0200145def check_maphash(mapping_path, maphash, args):
Rico Wind158ef9f2022-05-19 11:08:30 +0200146 infile_maphash = get_hash_from_map_file(mapping_path)
147 if infile_maphash != maphash:
148 print('ERROR: The mapping file hash does not match the R8 line')
149 print(' In mapping file: ' + infile_maphash)
150 print(' In source file: ' + maphash)
151 if (not args.exclude_deps):
152 print('If this could be a version without internalized dependencies '
153 + 'try passing --exclude-deps')
154 sys.exit(1)
155
156def get_hash_from_map_file(mapping_path):
Ian Zernycbcd5032021-10-15 13:34:28 +0200157 map_hash_header = "# pg_map_hash: SHA-256 "
158 for line in open(mapping_path, 'r'):
159 if line.startswith(map_hash_header):
Rico Wind158ef9f2022-05-19 11:08:30 +0200160 return line[len(map_hash_header):].strip()
Ian Zernycbcd5032021-10-15 13:34:28 +0200161
Morten Krogh-Jespersen017a7002019-01-10 14:14:17 +0100162def main():
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +0100163 args = parse_arguments()
Ian Zernycbcd5032021-10-15 13:34:28 +0200164 with utils.TempDir() as temp:
165 map_path = get_map_file(args, temp)
166 return run(
Christoffer Quist Adamsen4d38d032021-04-20 12:31:31 +0200167 map_path,
Morten Krogh-Jespersen3aa4bff82020-09-30 16:43:26 +0200168 args.stacktrace,
Christoffer Quist Adamsen7bf60342020-11-09 14:00:27 +0100169 args.no_r8lib,
Morten Krogh-Jespersenc9fd21d2020-12-14 09:25:11 +0100170 quiet=args.quiet,
Morten Krogh-Jespersenb6b2eb52021-08-11 09:33:20 +0200171 debug=args.debug_agent,
Christoffer Quist Adamsen5e8f04e2021-09-24 08:48:31 +0200172 regex=args.regex,
173 verbose=args.verbose)
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100174
Ian Zernycbcd5032021-10-15 13:34:28 +0200175
Christoffer Quist Adamsen5e8f04e2021-09-24 08:48:31 +0200176def run(map_path, stacktrace, no_r8lib, quiet=False, debug=False, regex=None, verbose=False):
Morten Krogh-Jespersenc9fd21d2020-12-14 09:25:11 +0100177 retrace_args = [jdk.GetJavaExecutable()]
178
179 if debug:
Christoffer Quist Adamsen4d38d032021-04-20 12:31:31 +0200180 retrace_args.append(
181 '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005')
Morten Krogh-Jespersenc9fd21d2020-12-14 09:25:11 +0100182
183 retrace_args += [
Morten Krogh-Jespersen89a5b062020-01-07 14:50:40 +0100184 '-cp',
Morten Krogh-Jespersen501efac2021-10-25 10:54:05 +0200185 utils.R8_JAR if no_r8lib else utils.R8RETRACE_JAR,
Morten Krogh-Jespersen89a5b062020-01-07 14:50:40 +0100186 'com.android.tools.r8.retrace.Retrace',
Morten Krogh-Jespersen3aa4bff82020-09-30 16:43:26 +0200187 map_path
Ian Zerny3f54e222019-02-12 10:51:17 +0100188 ]
Morten Krogh-Jespersen89a5b062020-01-07 14:50:40 +0100189
Morten Krogh-Jespersenb6b2eb52021-08-11 09:33:20 +0200190 if regex:
191 retrace_args.append('--regex')
192 retrace_args.append(regex)
193
Morten Krogh-Jespersen691793a2021-01-08 11:36:20 +0100194 if quiet:
195 retrace_args.append('--quiet')
196
Ian Zerny5ffa58f2020-02-26 08:37:14 +0100197 if stacktrace:
198 retrace_args.append(stacktrace)
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +0100199
Christoffer Quist Adamsen5e8f04e2021-09-24 08:48:31 +0200200 if verbose:
201 retrace_args.append('--verbose')
202
Christoffer Quist Adamsen7bf60342020-11-09 14:00:27 +0100203 utils.PrintCmd(retrace_args, quiet=quiet)
Morten Krogh-Jespersena6f0f2f2019-01-17 13:57:39 +0100204 return subprocess.call(retrace_args)
205
Morten Krogh-Jespersen017a7002019-01-10 14:14:17 +0100206
207if __name__ == '__main__':
208 sys.exit(main())