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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
#!/usr/bin/env python
#
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Find the most recent tombstone file(s) on all connected devices
# and prints their stacks.
#
# Assumes tombstone file was created with current symbols.
import datetime
import logging
import multiprocessing
import os
import subprocess
import sys
import optparse
from pylib import android_commands
def _ListTombstones(adb):
"""List the tombstone files on the device.
Args:
adb: An instance of AndroidCommands.
Yields:
Tuples of (tombstone filename, date time of file on device).
"""
lines = adb.RunShellCommand('TZ=UTC su -c ls -a -l /data/tombstones')
for line in lines:
if 'tombstone' in line and not 'No such file or directory' in line:
details = line.split()
t = datetime.datetime.strptime(details[-3] + ' ' + details[-2],
'%Y-%m-%d %H:%M')
yield details[-1], t
def _GetDeviceDateTime(adb):
"""Determine the date time on the device.
Args:
adb: An instance of AndroidCommands.
Returns:
A datetime instance.
"""
device_now_string = adb.RunShellCommand('TZ=UTC date')
return datetime.datetime.strptime(
device_now_string[0], '%a %b %d %H:%M:%S %Z %Y')
def _GetTombstoneData(adb, tombstone_file):
"""Retrieve the tombstone data from the device
Args:
tombstone_file: the tombstone to retrieve
Returns:
A list of lines
"""
return adb.GetProtectedFileContents('/data/tombstones/' + tombstone_file)
def _EraseTombstone(adb, tombstone_file):
"""Deletes a tombstone from the device.
Args:
tombstone_file: the tombstone to delete.
"""
return adb.RunShellCommand('su -c rm /data/tombstones/' + tombstone_file)
def _ResolveSymbols(tombstone_data, include_stack):
"""Run the stack tool for given tombstone input.
Args:
tombstone_data: a list of strings of tombstone data.
include_stack: boolean whether to include stack data in output.
Yields:
A string for each line of resolved stack output.
"""
stack_tool = os.path.join(os.path.dirname(__file__), '..', '..',
'third_party', 'android_platform', 'development',
'scripts', 'stack')
proc = subprocess.Popen(stack_tool, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
output = proc.communicate(input='\n'.join(tombstone_data))[0]
for line in output.split('\n'):
if not include_stack and 'Stack Data:' in line:
break
yield line
def _ResolveTombstone(tombstone):
lines = []
lines += [tombstone['file'] + ' created on ' + str(tombstone['time']) +
', about this long ago: ' +
(str(tombstone['device_now'] - tombstone['time']) +
' Device: ' + tombstone['serial'])]
print '\n'.join(lines)
print 'Resolving...'
lines += _ResolveSymbols(tombstone['data'], tombstone['stack'])
return lines
def _ResolveTombstones(jobs, tombstones):
"""Resolve a list of tombstones.
Args:
jobs: the number of jobs to use with multiprocess.
tombstones: a list of tombstones.
"""
if not tombstones:
print 'No device attached? Or no tombstones?'
return
if len(tombstones) == 1:
data = _ResolveTombstone(tombstones[0])
else:
pool = multiprocessing.Pool(processes=jobs)
data = pool.map(_ResolveTombstone, tombstones)
data = ['\n'.join(d) for d in data]
print '\n'.join(data)
def _GetTombstonesForDevice(adb, options):
"""Returns a list of tombstones on a given adb connection.
Args:
adb: An instance of Androidcommands.
options: command line arguments from OptParse
"""
ret = []
all_tombstones = list(_ListTombstones(adb))
if not all_tombstones:
print 'No device attached? Or no tombstones?'
return ret
# Sort the tombstones in date order, descending
all_tombstones.sort(cmp=lambda a, b: cmp(b[1], a[1]))
# Only resolve the most recent unless --all-tombstones given.
tombstones = all_tombstones if options.all_tombstones else [all_tombstones[0]]
device_now = _GetDeviceDateTime(adb)
for tombstone_file, tombstone_time in tombstones:
ret += [{'serial': adb.Adb().GetSerialNumber(),
'device_now': device_now,
'time': tombstone_time,
'file': tombstone_file,
'stack': options.stack,
'data': _GetTombstoneData(adb, tombstone_file)}]
# Erase all the tombstones if desired.
if options.wipe_tombstones:
for tombstone_file, _ in all_tombstones:
_EraseTombstone(adb, tombstone_file)
return ret
def main():
parser = optparse.OptionParser()
parser.add_option('--device',
help='The serial number of the device. If not specified '
'will use all devices.')
parser.add_option('-a', '--all-tombstones', action='store_true',
help="""Resolve symbols for all tombstones, rather than just
the most recent""")
parser.add_option('-s', '--stack', action='store_true',
help='Also include symbols for stack data')
parser.add_option('-w', '--wipe-tombstones', action='store_true',
help='Erase all tombstones from device after processing')
parser.add_option('-j', '--jobs', type='int',
default=4,
help='Number of jobs to use when processing multiple '
'crash stacks.')
options, args = parser.parse_args()
if options.device:
devices = [options.device]
else:
devices = android_commands.GetAttachedDevices()
tombstones = []
for device in devices:
adb = android_commands.AndroidCommands(device)
tombstones += _GetTombstonesForDevice(adb, options)
_ResolveTombstones(options.jobs, tombstones)
if __name__ == '__main__':
sys.exit(main())
|