1#!/usr/bin/env python3 2# Copyright (C) 2017 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15"""Like llvm-symbolizer for UI JS/TS sources. 16 17This script is used to "symbolize" UI crashes. It takes a crash, typically 18copied from a bug reports, of the form: 19 20(https://ui.perfetto.dev/v12.1.269/frontend_bundle.js:7639:61) at foo() 21(https://ui.perfetto.dev/v12.1.269/frontend_bundle.js:9235:29) at bar() 22 23it fetches the corresponding source maps and emits in output a translated 24crash report, of the form: 25 26(https://github.com/google/perfetto/blob/de4db33f/ui/src/foo.ts#L61) at foo() 27(https://github.com/google/perfetto/blob/de4db33f/ui/src/baz.ts#L300) at bar() 28""" 29 30import logging 31import re 32import sys 33import tempfile 34import urllib.request 35import ssl 36import os 37 38try: 39 import sourcemap 40except: 41 print('Run `pip3 install sourcemap` and try again') 42 sys.exit(1) 43 44 45def fetch_url_cached(url): 46 normalized = re.sub('[^a-zA-Z0-9-._]', '_', url) 47 local_file = os.path.join(tempfile.gettempdir(), normalized) 48 if os.path.exists(local_file): 49 logging.debug('Using %s', local_file) 50 with open(local_file, 'r') as f: 51 return f.read() 52 context = ssl._create_unverified_context() 53 logging.info('Fetching %s', url) 54 resp = urllib.request.urlopen(url, context=context) 55 contents = resp.read().decode() 56 with open(local_file, 'w') as f: 57 f.write(contents) 58 return contents 59 60 61def Main(): 62 if len(sys.argv) > 1: 63 with open(sys.argv[1], 'r') as f: 64 txt = f.read() 65 else: 66 if sys.stdin.isatty(): 67 print('Paste the crash log and press CTRL-D\n') 68 txt = sys.stdin.read() 69 70 # Look for the GIT commitish appended in crash reports. This is not required 71 # for resolving the sourcemaps but helps generating better links. 72 matches = re.findall(r'([a-f0-9]{40})\sUA', txt) 73 git_rev = matches[0] if matches else 'HEAD' 74 75 matches = re.findall(r'((\bhttp.+?\.js):(\d+):(\d+))', txt) 76 maps_by_url = {} 77 sym_lines = '' 78 for entry in matches: 79 whole_token, script_url, line, col = entry 80 map_url = script_url + '.map' 81 if map_url in maps_by_url: 82 srcmap = maps_by_url[map_url] 83 else: 84 map_file_contents = fetch_url_cached(map_url) 85 srcmap = sourcemap.loads(map_file_contents) 86 maps_by_url[map_url] = srcmap 87 sym = srcmap.lookup(int(line), int(col)) 88 src = sym.src.replace('../../', '') 89 sym_url = '%s#%s' % (src, sym.src_line) 90 if src.startswith('../out/ui/'): 91 src = src.replace('../out/ui/', 'ui/') 92 sym_url = 'https://github.com/google/perfetto/blob/%s/%s#L%d' % ( 93 git_rev, src, sym.src_line) 94 sym_lines += sym_url + '\n' 95 txt = txt.replace(whole_token, sym_url) 96 97 print(txt) 98 print('\nResolved symbols:\n' + sym_lines) 99 100 101if __name__ == '__main__': 102 logging.basicConfig(level=logging.INFO) 103 sys.exit(Main()) 104