Chromium Code Reviews| Index: tests/untrusted_crash_dump/decode_dump.py |
| diff --git a/tests/untrusted_crash_dump/decode_dump.py b/tests/untrusted_crash_dump/decode_dump.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..2a61b57eed7e6516fd54f99c4a93e3fd3c15a3dc |
| --- /dev/null |
| +++ b/tests/untrusted_crash_dump/decode_dump.py |
| @@ -0,0 +1,159 @@ |
| +#!/usr/bin/python |
| +# Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Utility to decode a crash dump generated by untrusted_crash_dump.[ch] |
| + |
| +Currently this produces a simple stack trace. |
| +""" |
| + |
| +import json |
| +import optparse |
| +import os |
| +import posixpath |
| +import subprocess |
| +import sys |
| + |
| + |
| +def SelectModulePath(options, filename): |
| + """Select which path to get a module from. |
| + |
| + Args: |
| + options: option object. |
| + filename: filename of a module (as appears in phdrs). |
| + Returns: |
| + Full local path to the file. |
| + Derived by consulting the manifest. |
| + """ |
| + # For some names try the main nexe. |
| + if options.main_nexe and filename in ['NaClMain', '(null)']: |
|
Mark Seaborn
2012/02/13 19:04:08
You could comment that "NaClMain" is the argv[0] v
bradn
2012/02/13 23:42:03
Done.
|
| + return options.main_nexe |
| + filepart = posixpath.basename(filename) |
| + nmf_entry = options.nmf_data.get('files', {}).get(filepart, {}) |
| + # TODO(bradnelson): support x86-64 + arm. |
| + nmf_url = nmf_entry.get('x86-32', {}).get('url') |
| + # Try filename directly if not in manifest. |
| + if not nmf_url: |
|
Mark Seaborn
2012/02/13 19:04:08
"if nmf_url is not None"
bradn
2012/02/13 23:42:03
Done (you mean is None).
|
| + return filename |
| + # Look for the module relative to the manifest, then toolchain. |
| + for path in [os.path.dirname(options.nmf), options.toolchain_libs]: |
| + if path: |
|
Mark Seaborn
2012/02/13 19:04:08
"if path is not None"
bradn
2012/02/13 23:42:03
Done and changed round to make correct!
|
| + pfilename = os.path.join(path, nmf_url) |
| + if os.path.exists(pfilename): |
| + return pfilename |
| + # If nothing else, try the path directly. |
| + return filename |
| + |
| + |
| +def Addr2Line(options, filename, addr): |
| + """Use addr2line to decode a code address. |
| + |
| + Args: |
| + options: option object. |
| + filename: filename of module that the address is relative to. |
| + addr: address. |
| + Returns: |
| + A list of dicts containing: function, filename, lineno. |
| + """ |
| + filename = SelectModulePath(options, filename) |
| + if not os.path.exists(filename): |
| + return [{ |
| + 'function': 'Unknown_function', |
| + 'filename': 'unknown_file', |
| + 'lineno': -1, |
| + }] |
| + cmd = [ |
| + options.addr2line, '-f', '--inlines', '-e', filename, ('0x%08x' % addr), |
|
Mark Seaborn
2012/02/13 19:04:08
Nit: Don't need brackets around '0x%08x' % addr
bradn
2012/02/13 23:42:03
Done.
|
| + ] |
| + p = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
|
Mark Seaborn
2012/02/13 19:04:08
Try to avoid one-char variable names. e.g. 'p' ->
bradn
2012/02/13 23:42:03
Done.
|
| + p_stdout, p_stderr = p.communicate() |
| + assert p.returncode == 0 |
| + lines = p_stdout.splitlines() |
| + assert len(lines) % 2 == 0 |
| + results = [] |
| + for index in range(len(lines) / 2): |
| + func = lines[index * 2] |
| + afilename, lineno = lines[index * 2 + 1].split(':') |
| + results.append({ |
| + 'function': func, |
| + 'filename': afilename, |
| + 'lineno': int(lineno), |
| + }) |
| + return results |
| + |
| + |
| +def LoadAndDecode(options, core_path): |
| + """Given a core.json file, load and embellish with decoded addresses. |
| + |
| + Args: |
| + options: options object. |
| + core_path: source file containing a dump. |
| + Returns: |
| + |
| + """ |
| + if options.nmf: |
| + options.nmf_data = json.loads(open(options.nmf, 'r').read()) |
|
Mark Seaborn
2012/02/13 19:04:08
You can do json.load(open(...)) instead.
bradn
2012/02/13 23:42:03
Done.
|
| + else: |
| + options.nmf_data = {} |
| + core_text = open(core_path, 'r').read() |
| + core = json.loads(core_text) |
|
Mark Seaborn
2012/02/13 19:04:08
Ditto. json.load()
bradn
2012/02/13 23:42:03
Done.
|
| + for frame in core['frames']: |
| + ip_mapped = frame['ip_mapped'] |
| + ip_mapped['scopes'] = Addr2Line( |
| + options, ip_mapped['file'], ip_mapped['addr']) |
| + return core |
| + |
| + |
| +def StackTrace(info): |
| + """Convert a decoded core.json dump to a simple stack trace. |
| + |
| + Args: |
| + info: core.json info with decoded code addresses. |
| + Returns: |
| + A list of dicts with filename, lineno, function (deepest first). |
| + """ |
| + trace = [] |
| + for frame in info['frames']: |
| + ip_mapped = frame['ip_mapped'] |
| + for info in ip_mapped['scopes']: |
|
Mark Seaborn
2012/02/13 19:04:08
Just "frame['ip_mapped']['scopes']"?
bradn
2012/02/13 23:42:03
Done.
|
| + trace.append(info) |
| + return trace |
| + |
| + |
| +def PrintTrace(trace, out): |
| + """Print a trace to a file like object. |
| + |
| + Args: |
| + trace: A list of [filename, lineno, function] (deepest first). |
| + out: file like object to output the trace to. |
| + """ |
| + for scope in trace: |
| + out.write('%s at %s:%d\n' % ( |
| + scope['function'], |
| + scope['filename'], |
| + scope['lineno'])) |
| + |
| + |
| +def Main(args): |
| + parser = optparse.OptionParser( |
| + usage='USAGE: %prog [options] <core.json> <trace>') |
|
Mark Seaborn
2012/02/13 19:04:08
You show two args here but check for one later.
bradn
2012/02/13 23:42:03
Fixed (changed at some point to one)
|
| + parser.add_option('-m', '--main-nexe', dest='main_nexe', |
| + help='nexe to resolve NaClMain references from') |
| + parser.add_option('-n', '--nmf', dest='nmf', |
| + help='nmf to resolve references from') |
| + parser.add_option('-a', '--addr2line', dest='addr2line', |
| + help='path to appropriate addr2line') |
| + parser.add_option('-l', '--toolchain-libs', dest='toolchain_libs', |
| + help='path to the toolchain libraries') |
| + options, args = parser.parse_args(args) |
| + if len(args) != 1: |
| + parser.print_help() |
| + sys.exit(1) |
| + info = LoadAndDecode(options, args[0]) |
| + trace = StackTrace(info) |
| + PrintTrace(trace, sys.stdout) |
| + |
| + |
| +if __name__ == '__main__': |
| + Main(sys.argv[1:]) |