Index: src/untrusted/crash_dump/decode_dump.py |
diff --git a/src/untrusted/crash_dump/decode_dump.py b/src/untrusted/crash_dump/decode_dump.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..c33c66091a44aca5093fa7cadb9a2bd876ac6f5e |
--- /dev/null |
+++ b/src/untrusted/crash_dump/decode_dump.py |
@@ -0,0 +1,181 @@ |
+#!/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. |
+ # NaClMain is the argv[0] setup in sel_main.c |
+ # (null) shows up in chrome. |
+ if options.main_nexe and filename in ['NaClMain', '(null)']: |
Mark Seaborn
2012/02/16 18:42:50
"if options.main_nexe is not None ..."
bradn
2012/02/16 20:13:10
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 nmf_url is None: |
+ return filename |
+ # Look for the module relative to the manifest (if any), then toolchain. |
+ paths = [] |
+ if options.nmf is not None: |
+ paths.append(os.path.dirname(options.nmf)) |
+ if options.toolchain_libs is not None: |
+ paths.append(options.toolchain_libs) |
+ for path in paths: |
+ pfilename = os.path.join(path, nmf_url) |
+ if os.path.exists(pfilename): |
+ return pfilename |
+ # If nothing else, try the path directly. |
+ return filename |
+ |
+ |
+def DecodeAddressSegment(segments, address): |
+ """Convert an address to a segment relative one, plus filename. |
+ |
+ Args: |
+ segments: a list of phdr segments. |
+ address: a process wide code address. |
+ Returns: |
+ A tuple of filename and segment relative address. |
+ """ |
+ for segment in segments: |
+ for phdr in segment['dlpi_phdr']: |
+ start = segment['dlpi_addr'] + phdr['p_vaddr'] |
+ end = start + phdr['p_memsz'] |
+ if address >= start and address < end: |
+ return (segment['dlpi_name'], address - segment['dlpi_addr']) |
+ return ('(null)', address) |
+ |
+ |
+def Addr2Line(options, segments, address): |
+ """Use addr2line to decode a code address. |
+ |
+ Args: |
+ options: option object. |
+ segments: A list of phdr segments. |
+ address: a code address. |
+ Returns: |
+ A list of dicts containing: function, filename, lineno. |
+ """ |
+ filename, address = DecodeAddressSegment(segments, address) |
+ 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' % address, |
+ ] |
+ process = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
+ process_stdout, _ = process.communicate() |
+ assert process.returncode == 0 |
+ lines = process_stdout.splitlines() |
+ assert len(lines) % 2 == 0 |
+ results = [] |
+ for index in range(len(lines) / 2): |
Mark Seaborn
2012/02/16 18:42:50
Nit: xrange() is preferred over range() in general
bradn
2012/02/16 20:13:10
Done.
|
+ func = lines[index * 2] |
+ afilename, lineno = lines[index * 2 + 1].split(':') |
Mark Seaborn
2012/02/16 18:42:50
Nit: split(':', 1) is preferable if you're expecti
bradn
2012/02/16 20:13:10
Done.
|
+ 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.load(open(options.nmf)) |
+ else: |
+ options.nmf_data = {} |
+ core = json.load(open(core_path)) |
+ for frame in core['frames']: |
+ frame['scopes'] = Addr2Line( |
+ options, core['segments'], frame['ip']) |
+ 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']: |
+ for scope in frame['scopes']: |
+ trace.append(scope) |
+ 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>') |
+ parser.add_option('-m', '--main-nexe', dest='main_nexe', |
+ help='nexe to resolve NaClMain references from') |
+ parser.add_option('-n', '--nmf', dest='nmf', |
Mark Seaborn
2012/02/16 18:42:50
dest='nmf_filename' would be a little more descrip
bradn
2012/02/16 20:13:10
Done.
|
+ 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:]) |