1#!/usr/bin/env python
2# Copyright 2013 The Chromium 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"""A tool to extract minidumps from dmp crash dumps."""
7
8import os
9import sys
10from cgi import parse_multipart
11
12
13def ProcessDump(dump_file, minidump_file):
14  """Extracts the part of the dump file that minidump_stackwalk can read.
15
16  The dump files generated by the breakpad integration multi-part form data
17  that include the minidump as file attachment.
18
19  Args:
20    dump_file: the dump file that needs to be processed.
21    minidump_file: the file to write the minidump to.
22  """
23  try:
24    dump = open(dump_file, 'rb')
25    boundary = dump.readline().strip()[2:]
26    data = parse_multipart(dump, {'boundary': boundary})
27  except:
28    print 'Failed to read dmp file %s' % dump_file
29    return
30
31  if not 'upload_file_minidump' in data:
32    print 'Could not find minidump file in dump.'
33    return
34
35  f = open(minidump_file, 'w')
36  f.write("\r\n".join(data['upload_file_minidump']))
37  f.close()
38
39
40def main():
41  if len(sys.argv) != 3:
42    print 'Usage: %s [dmp file] [minidump]' % sys.argv[0]
43    print ''
44    print 'Extracts the minidump stored in the crash dump file'
45    return 1
46
47  ProcessDump(sys.argv[1], sys.argv[2])
48
49
50if '__main__' == __name__:
51  sys.exit(main())
52