-
Notifications
You must be signed in to change notification settings - Fork 27
/
anr.py
executable file
·201 lines (155 loc) · 6.74 KB
/
anr.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
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
196
197
198
199
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Simple ANR analyzer, use graphviz to output for every analyzed process
# Copyright 2017 Wanghong Lin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
import re
import sys
has_graphviz = False
try:
import graphviz
has_graphviz = True
except ImportError:
has_graphviz = False
print 'No graphviz installation found'
print 'Install the package'
print ''
print '\tpip install graphviz\n'
print 'If you want to output ANR dependency graphic as pdf'
print ''
RE_PROCESS_START = re.compile('.*pid (\d+) at.*')
RE_PROCESS_NAME = re.compile('Cmd line: (\S+)')
RE_PROCESS_END = re.compile('.*end (\d+).*')
RE_THREAD_START = re.compile('(.*) prio=(\d+) tid=(\d+) (\S+)')
RE_ANR_PATTERN = re.compile(r'.*waiting to lock.*held by (?:thread |tid=)(\d+)', re.MULTILINE | re.DOTALL)
def make_html_colored_text(color_notation, text):
return '<<font color="{0}">{1}</font>>'.format(color_notation, text)
class ThreadBlock(object):
def __init__(self):
super(ThreadBlock, self).__init__()
self.title = ''
self.priority = 0
self.tid = 0
self.thread_name = ''
self.state = ''
self.thread_stack = ''
class Anr(object):
def __init__(self):
super(Anr, self).__init__()
self.anr_thread = ThreadBlock()
self.held_by_thread = ThreadBlock()
self.depends_on_anr = None
class ProcessBlock(object):
def __init__(self):
super(ProcessBlock, self).__init__()
self.pid = 0
self.name = ''
self.process_header = ''
self.thread_blocks = []
def get_anr_for_process(process_b):
anr = None
anrs = []
for thread_b in process_b.thread_blocks:
anrm = RE_ANR_PATTERN.match(thread_b.thread_stack)
if anrm:
anr = Anr()
anr.anr_thread = thread_b
held_by_tid = anrm.group(1)
for held_by_thread in process_b.thread_blocks:
if held_by_thread.tid == held_by_tid:
anr.held_by_thread = held_by_thread
# print '\nFound ANR in ', thread_b.tid, ', held by tid ', held_by_tid
anrs.append(anr)
return anrs
def build_anr_hierarchy(in_anrs):
for in_anr in in_anrs:
for in_anr2 in in_anrs:
if in_anr.held_by_thread.tid == in_anr2.anr_thread.tid:
in_anr.depends_on_anr = in_anr2
return in_anrs
def analyze_anr(anr_file, out_format):
with anr_file as anr:
handle_process_header = False
handle_thread_stack = False
pbs = []
for line in anr.readlines():
psm = RE_PROCESS_START.match(line)
pem = RE_PROCESS_END.match(line)
tsm = RE_THREAD_START.match(line)
if psm:
pb = ProcessBlock()
pb.pid = psm.group(1)
pbs.append(pb)
handle_process_header = True
handle_thread_stack = False
continue
if handle_process_header:
pbs[-1].process_header = pbs[-1].process_header + line
pnm = RE_PROCESS_NAME.match(line)
if pnm:
pbs[-1].name = pnm.group(1)
if tsm:
handle_process_header = False
handle_thread_stack = True
tb = ThreadBlock()
tb.title = tsm.group(0)
tb.thread_name = tsm.group(1)
tb.priority = tsm.group(2)
tb.tid = tsm.group(3)
tb.state = tsm.group(4)
pbs[-1].thread_blocks.append(tb)
continue
if handle_thread_stack:
pbs[-1].thread_blocks[-1].thread_stack = pbs[-1].thread_blocks[-1].thread_stack + line
for out_pb in pbs:
if len(out_pb.name) > 0:
anr_list = get_anr_for_process(out_pb)
if len(anr_list) is 0:
continue
anr_new_list = build_anr_hierarchy(anr_list)
print out_pb.name, 'tid =', out_pb.pid, 'total threads =', len(out_pb.thread_blocks), 'anrs =', len \
(anr_list), len(anr_new_list)
if has_graphviz:
dot = graphviz.Digraph(comment='ANR output for {0}'.format(out_pb.name), graph_attr={'rankdir': 'LR'}, format=out_format)
for oanr in anr_new_list:
if has_graphviz:
dot.node(oanr.anr_thread.title, oanr.anr_thread.thread_stack, _attributes={'xlabel': oanr.anr_thread.title})
# DO NOT allow empty name
if len(oanr.held_by_thread.title) == 0:
oanr.held_by_thread.title = 'Unknow'
dot.node(oanr.held_by_thread.title, oanr.held_by_thread.thread_stack, _attributes={'xlabel': make_html_colored_text('#ff0000', oanr.held_by_thread.title)})
dot.edge(oanr.anr_thread.title, oanr.held_by_thread.title)
out = 'ANR in thread {0} held by tid {1}'.format(oanr.anr_thread.tid, oanr.held_by_thread.tid)
next_anr = oanr.depends_on_anr
while next_anr is not None:
appended_out = ', tid {0} held by tid {1}'.format(next_anr.anr_thread.tid, next_anr.held_by_thread.tid)
out = out + appended_out
next_anr = next_anr.depends_on_anr
print out
if has_graphviz:
out_gv_file = '{0}_{1}.gv'.format(out_pb.name, out_pb.pid)
# remove the special characters at some ANR logs
out_gv_file = out_gv_file.replace('/', '_')
print 'render to file', out_gv_file
dot.render(out_gv_file)
pass
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Analyze ANR from traces.txt, output as pdf by graphviz')
parser.add_argument('--format', choices=['ps', 'pdf', 'svg', 'png', 'gif', 'jpg'], help='Specify the output format')
parser.add_argument('file', type=argparse.FileType('r'), default=sys.stdin, help='Absolute path to traces.txt')
args = parser.parse_args()
analyze_anr(args.file, args.format)