forked from mozilla/ADBFuzz
-
Notifications
You must be signed in to change notification settings - Fork 1
/
minidump.py
119 lines (97 loc) · 3.46 KB
/
minidump.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# The Original Code is ADBFuzz.
#
# The Initial Developer of the Original Code is Christian Holler (decoder).
#
# Contributors:
# Christian Holler <[email protected]> (Original Developer)
#
# ***** END LICENSE BLOCK *****
import subprocess
import os
import sys
class Minidump:
def __init__(self, dumpFile, libDir=None):
self.dumpFile = dumpFile
self.libDir = libDir
self.cleaned = False
self.crashTrace = []
self.crashType = None
self.crashThread = None
self.crashTraceSymbols = []
def getCrashTrace(self):
# Return cached result if available
if (len(self.crashTrace) > 0):
return self.crashTrace
proc = subprocess.Popen(["minidump_stackwalk", "-m", self.getFilename()], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = proc.communicate()[0].splitlines()
for line in stdout:
# Search for Crash|SIGABRT|0x43e0|9
if line.startswith("Crash|SIG"):
tok = line.split("|")
self.crashType = tok[1]
self.crashThread = tok[3]
break
if self.crashThread == None:
raise Exception("Cannot identify crashing thread from dump")
for line in stdout:
tok = line.split("|")
if ((len(tok) == 7) and (tok[0] == self.crashThread)):
if (int(tok[1]) < 8):
self.crashTrace.append((tok[1], tok[2], tok[6]))
return self.crashTrace
def getCrashType(self):
if (self.crashType == None):
# Will cache the crash type
self.getCrashTrace()
return self.crashType
def getCrashingThread(self):
if (self.crashThread == None):
# Will cache the crashing thread
self.getCrashTrace()
return self.crashThread
def getSymbolizedCrashTrace(self):
# Return cached result if available
if (len(self.crashTraceSymbols) > 0):
return self.crashTraceSymbols
dumpTrace = self.getCrashTrace()
for frame in dumpTrace:
frameNum = frame[0]
frameFile = frame[1]
frameAddr = frame[2]
frameFileResolved = None
for path, dirs, files in os.walk(os.path.abspath(self.libDir)):
for filename in files:
if filename == frameFile:
frameFileResolved = os.path.join(path, filename)
break
if frameFileResolved != None:
addr2line = subprocess.Popen(["addr2line", "-f", "-C", "-e", frameFileResolved, frameAddr], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
a2ldata = addr2line.communicate()[0].splitlines()
if (len(a2ldata) >= 2):
frameFunc = a2ldata[0]
frameSrc = a2ldata[1]
self.crashTraceSymbols.append((frameNum, frameFunc, frameSrc))
continue
elif (len(a2ldata) == 1):
frameFunc = a2ldata[0]
self.crashTraceSymbols.append((frameNum, frameFunc, frameFile))
continue
self.crashTraceSymbols.append((frameNum, frameAddr, frameFile))
return self.crashTraceSymbols
def cleanup(self):
os.remove(self.getFilename())
self.cleaned = True
def getFilename(self):
if self.cleaned:
raise Exception("Attempted to perform file operation on deleted minidump file")
else:
return self.dumpFile
if __name__ == "__main__":
raise Exception("This module cannot run standalone, but is used by ADBFuzz")