Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Morten Krogh-Jespersen | 017a700 | 2019-01-10 14:14:17 +0100 | [diff] [blame] | 2 | # 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 Zerny | 3af5815 | 2023-03-13 10:52:27 +0100 | [diff] [blame] | 6 | from os import path |
| 7 | |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 8 | import argparse |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 9 | import os |
Morten Krogh-Jespersen | 017a700 | 2019-01-10 14:14:17 +0100 | [diff] [blame] | 10 | import subprocess |
| 11 | import sys |
Ian Zerny | 9ff31b7 | 2021-04-22 11:36:26 +0200 | [diff] [blame] | 12 | |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 13 | import jdk |
Morten Krogh-Jespersen | 017a700 | 2019-01-10 14:14:17 +0100 | [diff] [blame] | 14 | import utils |
| 15 | |
Ian Zerny | 9ff31b7 | 2021-04-22 11:36:26 +0200 | [diff] [blame] | 16 | |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 17 | def parse_arguments(): |
| 18 | parser = argparse.ArgumentParser( |
| 19 | description = 'R8lib wrapper for retrace tool.') |
| 20 | parser.add_argument( |
| 21 | '-c', |
Søren Gjesse | 8ebfddf | 2022-04-29 08:47:30 +0200 | [diff] [blame] | 22 | '--commit-hash', |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 23 | '--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-Jespersen | 08701bf | 2019-12-02 21:31:04 +0100 | [diff] [blame] | 31 | '--tag', |
| 32 | help='Tag to download r8lib map file for.', |
| 33 | default=None) |
| 34 | parser.add_argument( |
Søren Gjesse | 8ebfddf | 2022-04-29 08:47:30 +0200 | [diff] [blame] | 35 | '--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-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 40 | '--map', |
| 41 | help='Path to r8lib map.', |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 42 | default=None) |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 43 | parser.add_argument( |
Morten Krogh-Jespersen | 3aa4bff8 | 2020-09-30 16:43:26 +0200 | [diff] [blame] | 44 | '--no-r8lib', |
Søren Gjesse | 8ebfddf | 2022-04-29 08:47:30 +0200 | [diff] [blame] | 45 | '--no_r8lib', |
Morten Krogh-Jespersen | 3aa4bff8 | 2020-09-30 16:43:26 +0200 | [diff] [blame] | 46 | default=False, |
| 47 | action='store_true', |
| 48 | help='Use r8.jar and not r8lib.jar.') |
| 49 | parser.add_argument( |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 50 | '--stacktrace', |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 51 | help='Path to stacktrace file (read from stdin if not passed).', |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 52 | default=None) |
Christoffer Quist Adamsen | 7bf6034 | 2020-11-09 14:00:27 +0100 | [diff] [blame] | 53 | parser.add_argument( |
| 54 | '--quiet', |
| 55 | default=None, |
| 56 | action='store_true', |
| 57 | help='Disables diagnostics printing to stdout.') |
Morten Krogh-Jespersen | c9fd21d | 2020-12-14 09:25:11 +0100 | [diff] [blame] | 58 | parser.add_argument( |
Morten Krogh-Jespersen | b6b2eb5 | 2021-08-11 09:33:20 +0200 | [diff] [blame] | 59 | '--debug-agent', |
Søren Gjesse | 8ebfddf | 2022-04-29 08:47:30 +0200 | [diff] [blame] | 60 | '--debug_agent', |
Morten Krogh-Jespersen | b6b2eb5 | 2021-08-11 09:33:20 +0200 | [diff] [blame] | 61 | 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 Adamsen | 5e8f04e | 2021-09-24 08:48:31 +0200 | [diff] [blame] | 69 | parser.add_argument( |
| 70 | '--verbose', |
| 71 | default=None, |
| 72 | action='store_true', |
| 73 | help='Enables verbose retracing.') |
Morten Krogh-Jespersen | b4713b9 | 2022-01-27 13:35:17 +0100 | [diff] [blame] | 74 | parser.add_argument( |
| 75 | '--disable-map-validation', |
| 76 | default=None, |
| 77 | action='store_true', |
| 78 | help='Disable validation of map hash.') |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 79 | return parser.parse_args() |
| 80 | |
| 81 | |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 82 | def 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 Zerny | cf8ef51 | 2022-05-04 14:54:16 +0200 | [diff] [blame] | 115 | # 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 Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 122 | 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 Zerny | 3af5815 | 2023-03-13 10:52:27 +0100 | [diff] [blame] | 127 | if path.exists(utils.R8LIB_MAP) and get_hash_from_map_file(utils.R8LIB_MAP) == maphash: |
Rico Wind | 158ef9f | 2022-05-19 11:08:30 +0200 | [diff] [blame] | 128 | return utils.R8LIB_MAP |
| 129 | |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 130 | try: |
Søren Gjesse | 8ebfddf | 2022-04-29 08:47:30 +0200 | [diff] [blame] | 131 | map_path = utils.find_cloud_storage_file_from_options( |
Ian Zerny | cf8ef51 | 2022-05-04 14:54:16 +0200 | [diff] [blame] | 132 | 'r8lib' + ('-exclude-deps' if is_excldeps else '') + '.jar.map', args) |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 133 | except Exception as e: |
| 134 | print(e) |
| 135 | print('WARNING: Falling back to using local mapping file.') |
| 136 | |
Morten Krogh-Jespersen | b4713b9 | 2022-01-27 13:35:17 +0100 | [diff] [blame] | 137 | if map_path and not args.disable_map_validation: |
Søren Gjesse | 8ebfddf | 2022-04-29 08:47:30 +0200 | [diff] [blame] | 138 | check_maphash(map_path, maphash, args) |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 139 | return map_path |
| 140 | |
| 141 | # If no other map file was found, use the local mapping file. |
Rico Wind | 158ef9f | 2022-05-19 11:08:30 +0200 | [diff] [blame] | 142 | return utils.R8LIB_MAP |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 143 | |
| 144 | |
Søren Gjesse | 8ebfddf | 2022-04-29 08:47:30 +0200 | [diff] [blame] | 145 | def check_maphash(mapping_path, maphash, args): |
Rico Wind | 158ef9f | 2022-05-19 11:08:30 +0200 | [diff] [blame] | 146 | 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 | |
| 156 | def get_hash_from_map_file(mapping_path): |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 157 | map_hash_header = "# pg_map_hash: SHA-256 " |
| 158 | for line in open(mapping_path, 'r'): |
| 159 | if line.startswith(map_hash_header): |
Rico Wind | 158ef9f | 2022-05-19 11:08:30 +0200 | [diff] [blame] | 160 | return line[len(map_hash_header):].strip() |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 161 | |
Morten Krogh-Jespersen | 017a700 | 2019-01-10 14:14:17 +0100 | [diff] [blame] | 162 | def main(): |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 163 | args = parse_arguments() |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 164 | with utils.TempDir() as temp: |
| 165 | map_path = get_map_file(args, temp) |
| 166 | return run( |
Christoffer Quist Adamsen | 4d38d03 | 2021-04-20 12:31:31 +0200 | [diff] [blame] | 167 | map_path, |
Morten Krogh-Jespersen | 3aa4bff8 | 2020-09-30 16:43:26 +0200 | [diff] [blame] | 168 | args.stacktrace, |
Christoffer Quist Adamsen | 7bf6034 | 2020-11-09 14:00:27 +0100 | [diff] [blame] | 169 | args.no_r8lib, |
Morten Krogh-Jespersen | c9fd21d | 2020-12-14 09:25:11 +0100 | [diff] [blame] | 170 | quiet=args.quiet, |
Morten Krogh-Jespersen | b6b2eb5 | 2021-08-11 09:33:20 +0200 | [diff] [blame] | 171 | debug=args.debug_agent, |
Christoffer Quist Adamsen | 5e8f04e | 2021-09-24 08:48:31 +0200 | [diff] [blame] | 172 | regex=args.regex, |
| 173 | verbose=args.verbose) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 174 | |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 175 | |
Christoffer Quist Adamsen | 5e8f04e | 2021-09-24 08:48:31 +0200 | [diff] [blame] | 176 | def run(map_path, stacktrace, no_r8lib, quiet=False, debug=False, regex=None, verbose=False): |
Morten Krogh-Jespersen | c9fd21d | 2020-12-14 09:25:11 +0100 | [diff] [blame] | 177 | retrace_args = [jdk.GetJavaExecutable()] |
| 178 | |
| 179 | if debug: |
Christoffer Quist Adamsen | 4d38d03 | 2021-04-20 12:31:31 +0200 | [diff] [blame] | 180 | retrace_args.append( |
| 181 | '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005') |
Morten Krogh-Jespersen | c9fd21d | 2020-12-14 09:25:11 +0100 | [diff] [blame] | 182 | |
| 183 | retrace_args += [ |
Morten Krogh-Jespersen | 89a5b06 | 2020-01-07 14:50:40 +0100 | [diff] [blame] | 184 | '-cp', |
Morten Krogh-Jespersen | 501efac | 2021-10-25 10:54:05 +0200 | [diff] [blame] | 185 | utils.R8_JAR if no_r8lib else utils.R8RETRACE_JAR, |
Morten Krogh-Jespersen | 89a5b06 | 2020-01-07 14:50:40 +0100 | [diff] [blame] | 186 | 'com.android.tools.r8.retrace.Retrace', |
Morten Krogh-Jespersen | 3aa4bff8 | 2020-09-30 16:43:26 +0200 | [diff] [blame] | 187 | map_path |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 188 | ] |
Morten Krogh-Jespersen | 89a5b06 | 2020-01-07 14:50:40 +0100 | [diff] [blame] | 189 | |
Morten Krogh-Jespersen | b6b2eb5 | 2021-08-11 09:33:20 +0200 | [diff] [blame] | 190 | if regex: |
| 191 | retrace_args.append('--regex') |
| 192 | retrace_args.append(regex) |
| 193 | |
Morten Krogh-Jespersen | 691793a | 2021-01-08 11:36:20 +0100 | [diff] [blame] | 194 | if quiet: |
| 195 | retrace_args.append('--quiet') |
| 196 | |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 197 | if stacktrace: |
| 198 | retrace_args.append(stacktrace) |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 199 | |
Christoffer Quist Adamsen | 5e8f04e | 2021-09-24 08:48:31 +0200 | [diff] [blame] | 200 | if verbose: |
| 201 | retrace_args.append('--verbose') |
| 202 | |
Christoffer Quist Adamsen | 7bf6034 | 2020-11-09 14:00:27 +0100 | [diff] [blame] | 203 | utils.PrintCmd(retrace_args, quiet=quiet) |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 204 | return subprocess.call(retrace_args) |
| 205 | |
Morten Krogh-Jespersen | 017a700 | 2019-01-10 14:14:17 +0100 | [diff] [blame] | 206 | |
| 207 | if __name__ == '__main__': |
| 208 | sys.exit(main()) |