Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2012 The Native Client Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Utility to decode a crash dump generated by untrusted_crash_dump.[ch] | |
| 7 | |
| 8 Current this produces a simple stack trace. | |
|
Mark Seaborn
2012/02/06 19:44:06
'Currently'
bradn
2012/02/06 22:27:48
Done.
| |
| 9 """ | |
| 10 | |
| 11 import json | |
| 12 import optparse | |
| 13 import os | |
| 14 import posixpath | |
| 15 import subprocess | |
| 16 import sys | |
| 17 | |
| 18 | |
| 19 def Addr2Line(options, filename, addr): | |
| 20 """Use addr2line to decode a code address. | |
| 21 | |
| 22 Args: | |
| 23 options: option object. | |
| 24 filename: filename of module that the address is relative to. | |
| 25 addr: address. | |
| 26 Returns: | |
| 27 Dict containing: function, filename, lineno. | |
| 28 """ | |
| 29 filepart = posixpath.split(filename)[1] | |
|
Mark Seaborn
2012/02/06 19:44:06
Use posixpath.dirname()?
bradn
2012/02/06 22:27:48
I assume you mean basename.
Done.
| |
| 30 nmf_entry = options.nmf_data.get('files', {}).get(filepart, {}) | |
| 31 nmf_url = nmf_entry.get('x86-32', {}).get('url') | |
|
Mark Seaborn
2012/02/06 19:44:06
Add a TODO for the 'x86-32' part.
bradn
2012/02/06 22:27:48
Done.
| |
| 32 if nmf_url: | |
| 33 for p in [os.path.os.path.dirname(options.nmf), options.toolchain_libs]: | |
| 34 if not p: continue | |
|
Mark Seaborn
2012/02/06 19:44:06
Split into two lines.
bradn
2012/02/06 22:27:48
Done.
| |
| 35 pfilename = os.path.join(p, nmf_url) | |
| 36 if os.path.exists(pfilename): | |
| 37 filename = pfilename | |
| 38 break | |
|
Mark Seaborn
2012/02/06 19:44:06
Split this loop into a separate lookup function, a
bradn
2012/02/06 22:27:48
Done.
| |
| 39 | |
| 40 if filename in ['NaClMain', '(null)']: | |
| 41 if options.main_nexe: | |
| 42 filename = options.main_nexe | |
| 43 else: | |
| 44 return { | |
| 45 'function': 'Unknown_function', | |
| 46 'filename': 'unknown_file', | |
| 47 'lineno': '-1', | |
| 48 } | |
| 49 cmd = [ | |
| 50 options.addr2line, '-f', '-e', filename, ('0x%08x' % addr), | |
|
Mark Seaborn
2012/02/06 19:44:06
Ideally you should also use '--inlines', and check
bradn
2012/02/06 22:27:48
Done.
| |
| 51 ] | |
| 52 p = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
| 53 (p_stdout, p_stderr) = p.communicate() | |
|
Mark Seaborn
2012/02/06 19:44:06
BTW, you can use
var1, var2 = foo()
without the br
bradn
2012/02/06 22:27:48
Done.
| |
| 54 assert p.returncode == 0 | |
| 55 (func, fline) = p_stdout.rstrip().split('\n') | |
| 56 (afilename, lineno) = fline.split(':') | |
| 57 return { | |
| 58 'function': func, | |
| 59 'filename': afilename, | |
| 60 'lineno': lineno, | |
| 61 } | |
| 62 | |
| 63 | |
| 64 def Decode(options, core_path, dst_trace): | |
|
Mark Seaborn
2012/02/06 19:44:06
'dst' -> 'dest'
bradn
2012/02/06 22:27:48
Done.
| |
| 65 """Given a core.json file, decode and emit a stack trace. | |
| 66 | |
| 67 Args: | |
| 68 options: options object. | |
| 69 core_path: source file containing a dump. | |
| 70 dst_trace: output file to write the trace to. | |
| 71 """ | |
| 72 core_text = open(core_path, 'r').read() | |
| 73 core = json.loads(core_text) | |
| 74 out = open(dst_trace, 'w') | |
| 75 for f in core['frames']: | |
|
Mark Seaborn
2012/02/06 19:44:06
'f' -> 'frame'
bradn
2012/02/06 22:27:48
Done.
| |
| 76 ip_mapped = f['ip_mapped'] | |
| 77 info = Addr2Line(options, ip_mapped['file'], ip_mapped['addr']) | |
| 78 out.write('%s at %s:%s\n' % ( | |
| 79 info['function'], | |
| 80 posixpath.split(info['filename'])[1], | |
|
Mark Seaborn
2012/02/06 19:44:06
posixpath.basename()? But why do you want to drop
bradn
2012/02/06 22:27:48
To allow golden stace traces.
I've added a verbose
| |
| 81 info['lineno'])) | |
| 82 out.close() | |
| 83 | |
| 84 | |
| 85 def Main(args): | |
| 86 parser = optparse.OptionParser( | |
| 87 usage='USAGE: %prog [options] <core.json> <trace>') | |
| 88 parser.add_option('-m', '--main-nexe', dest='main_nexe', | |
| 89 help='nexe to resolve NaClMain references from') | |
| 90 parser.add_option('-n', '--nmf', dest='nmf', | |
| 91 help='nmf to resolve references from') | |
| 92 parser.add_option('-a', '--addr2line', dest='addr2line', | |
| 93 help='path to appropriate addr2line') | |
| 94 parser.add_option('-l', '--toolchain-libs', dest='toolchain_libs', | |
| 95 help='path to the toolchain libraries') | |
| 96 options, args = parser.parse_args(args) | |
| 97 if len(args) != 2: | |
| 98 parser.print_help() | |
| 99 sys.exit(1) | |
| 100 if options.nmf: | |
| 101 options.nmf_data = json.loads(open(options.nmf, 'r').read()) | |
| 102 else: | |
| 103 options.nmf_data = {} | |
| 104 Decode(options, args[0], args[1]) | |
| 105 | |
| 106 | |
| 107 if __name__ == '__main__': | |
| 108 Main(sys.argv[1:]) | |
| OLD | NEW |