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 | |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 6 | import argparse |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 7 | import os |
Morten Krogh-Jespersen | 017a700 | 2019-01-10 14:14:17 +0100 | [diff] [blame] | 8 | import subprocess |
| 9 | import sys |
Ian Zerny | 9ff31b7 | 2021-04-22 11:36:26 +0200 | [diff] [blame] | 10 | |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 11 | import jdk |
Morten Krogh-Jespersen | 017a700 | 2019-01-10 14:14:17 +0100 | [diff] [blame] | 12 | import utils |
| 13 | |
Ian Zerny | 9ff31b7 | 2021-04-22 11:36:26 +0200 | [diff] [blame] | 14 | |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 15 | def 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-Jespersen | 08701bf | 2019-12-02 21:31:04 +0100 | [diff] [blame] | 28 | '--tag', |
| 29 | help='Tag to download r8lib map file for.', |
| 30 | default=None) |
| 31 | parser.add_argument( |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 32 | '--map', |
| 33 | help='Path to r8lib map.', |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 34 | default=None) |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 35 | parser.add_argument( |
Morten Krogh-Jespersen | 3aa4bff8 | 2020-09-30 16:43:26 +0200 | [diff] [blame] | 36 | '--no-r8lib', |
| 37 | default=False, |
| 38 | action='store_true', |
| 39 | help='Use r8.jar and not r8lib.jar.') |
| 40 | parser.add_argument( |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 41 | '--stacktrace', |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 42 | help='Path to stacktrace file (read from stdin if not passed).', |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 43 | default=None) |
Christoffer Quist Adamsen | 7bf6034 | 2020-11-09 14:00:27 +0100 | [diff] [blame] | 44 | parser.add_argument( |
| 45 | '--quiet', |
| 46 | default=None, |
| 47 | action='store_true', |
| 48 | help='Disables diagnostics printing to stdout.') |
Morten Krogh-Jespersen | c9fd21d | 2020-12-14 09:25:11 +0100 | [diff] [blame] | 49 | parser.add_argument( |
Morten Krogh-Jespersen | b6b2eb5 | 2021-08-11 09:33:20 +0200 | [diff] [blame] | 50 | '--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 Adamsen | 5e8f04e | 2021-09-24 08:48:31 +0200 | [diff] [blame] | 59 | parser.add_argument( |
| 60 | '--verbose', |
| 61 | default=None, |
| 62 | action='store_true', |
| 63 | help='Enables verbose retracing.') |
Morten Krogh-Jespersen | b4713b9 | 2022-01-27 13:35:17 +0100 | [diff] [blame] | 64 | parser.add_argument( |
| 65 | '--disable-map-validation', |
| 66 | default=None, |
| 67 | action='store_true', |
| 68 | help='Disable validation of map hash.') |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 69 | return parser.parse_args() |
| 70 | |
| 71 | |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 72 | def 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-Jespersen | b4713b9 | 2022-01-27 13:35:17 +0100 | [diff] [blame] | 116 | if map_path and not args.disable_map_validation: |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 117 | 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 | |
| 124 | def 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-Jespersen | 017a700 | 2019-01-10 14:14:17 +0100 | [diff] [blame] | 136 | def main(): |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 137 | args = parse_arguments() |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 138 | with utils.TempDir() as temp: |
| 139 | map_path = get_map_file(args, temp) |
| 140 | return run( |
Christoffer Quist Adamsen | 4d38d03 | 2021-04-20 12:31:31 +0200 | [diff] [blame] | 141 | map_path, |
Morten Krogh-Jespersen | 3aa4bff8 | 2020-09-30 16:43:26 +0200 | [diff] [blame] | 142 | args.stacktrace, |
Christoffer Quist Adamsen | 7bf6034 | 2020-11-09 14:00:27 +0100 | [diff] [blame] | 143 | args.no_r8lib, |
Morten Krogh-Jespersen | c9fd21d | 2020-12-14 09:25:11 +0100 | [diff] [blame] | 144 | quiet=args.quiet, |
Morten Krogh-Jespersen | b6b2eb5 | 2021-08-11 09:33:20 +0200 | [diff] [blame] | 145 | debug=args.debug_agent, |
Christoffer Quist Adamsen | 5e8f04e | 2021-09-24 08:48:31 +0200 | [diff] [blame] | 146 | regex=args.regex, |
| 147 | verbose=args.verbose) |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 148 | |
Ian Zerny | cbcd503 | 2021-10-15 13:34:28 +0200 | [diff] [blame] | 149 | |
Christoffer Quist Adamsen | 5e8f04e | 2021-09-24 08:48:31 +0200 | [diff] [blame] | 150 | 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] | 151 | retrace_args = [jdk.GetJavaExecutable()] |
| 152 | |
| 153 | if debug: |
Christoffer Quist Adamsen | 4d38d03 | 2021-04-20 12:31:31 +0200 | [diff] [blame] | 154 | retrace_args.append( |
| 155 | '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005') |
Morten Krogh-Jespersen | c9fd21d | 2020-12-14 09:25:11 +0100 | [diff] [blame] | 156 | |
| 157 | retrace_args += [ |
Morten Krogh-Jespersen | 89a5b06 | 2020-01-07 14:50:40 +0100 | [diff] [blame] | 158 | '-cp', |
Morten Krogh-Jespersen | 501efac | 2021-10-25 10:54:05 +0200 | [diff] [blame] | 159 | utils.R8_JAR if no_r8lib else utils.R8RETRACE_JAR, |
Morten Krogh-Jespersen | 89a5b06 | 2020-01-07 14:50:40 +0100 | [diff] [blame] | 160 | 'com.android.tools.r8.retrace.Retrace', |
Morten Krogh-Jespersen | 3aa4bff8 | 2020-09-30 16:43:26 +0200 | [diff] [blame] | 161 | map_path |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 162 | ] |
Morten Krogh-Jespersen | 89a5b06 | 2020-01-07 14:50:40 +0100 | [diff] [blame] | 163 | |
Morten Krogh-Jespersen | b6b2eb5 | 2021-08-11 09:33:20 +0200 | [diff] [blame] | 164 | if regex: |
| 165 | retrace_args.append('--regex') |
| 166 | retrace_args.append(regex) |
| 167 | |
Morten Krogh-Jespersen | 691793a | 2021-01-08 11:36:20 +0100 | [diff] [blame] | 168 | if quiet: |
| 169 | retrace_args.append('--quiet') |
| 170 | |
Ian Zerny | 5ffa58f | 2020-02-26 08:37:14 +0100 | [diff] [blame] | 171 | if stacktrace: |
| 172 | retrace_args.append(stacktrace) |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 173 | |
Christoffer Quist Adamsen | 5e8f04e | 2021-09-24 08:48:31 +0200 | [diff] [blame] | 174 | if verbose: |
| 175 | retrace_args.append('--verbose') |
| 176 | |
Christoffer Quist Adamsen | 7bf6034 | 2020-11-09 14:00:27 +0100 | [diff] [blame] | 177 | utils.PrintCmd(retrace_args, quiet=quiet) |
Morten Krogh-Jespersen | a6f0f2f | 2019-01-17 13:57:39 +0100 | [diff] [blame] | 178 | return subprocess.call(retrace_args) |
| 179 | |
Morten Krogh-Jespersen | 017a700 | 2019-01-10 14:14:17 +0100 | [diff] [blame] | 180 | |
| 181 | if __name__ == '__main__': |
| 182 | sys.exit(main()) |