forked from JakeWharton/pidcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pidcat.py
executable file
·267 lines (221 loc) · 8.41 KB
/
pidcat.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/python -u
'''
Copyright 2009, The Android Open Source Project
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.
'''
# Script to highlight adb logcat output for console
# Originally written by Jeff Sharkey, http://jsharkey.org/
# Piping detection and popen() added by other Android team members
# Package filtering and output improvements by Jake Wharton, http://jakewharton.com
import argparse
import os
import sys
import re
import subprocess
from subprocess import PIPE
LOG_LEVELS = 'VDIWEF'
LOG_LEVELS_MAP = dict([(LOG_LEVELS[i], i) for i in range(len(LOG_LEVELS))])
parser = argparse.ArgumentParser(description='Filter logcat by package name')
parser.add_argument('package', nargs='*', help='Application package name(s)')
parser.add_argument('-w', '--tag-width', metavar='N', dest='tag_width', type=int, default=22, help='Width of log tag')
parser.add_argument('-l', '--min-level', dest='min_level', type=str, choices=LOG_LEVELS, default='V', help='Minimum level to be displayed')
parser.add_argument('--color-gc', dest='color_gc', action='store_true', help='Color garbage collection')
parser.add_argument('--always-display-tags', dest='always_tags', action='store_true',help='Always display the tag name')
parser.add_argument('-s', '--serial', dest='device_serial', help='Device serial number (adb -s option)')
parser.add_argument('-d', '--device', dest='use_device', action='store_true', help='Use first device for log input (adb -d option).')
parser.add_argument('-e', '--emulator', dest='use_emulator', action='store_true', help='Use first emulator for log input (adb -e option).')
args = parser.parse_args()
min_level = LOG_LEVELS_MAP[args.min_level]
header_size = args.tag_width + 1 + 3 + 1 # space, level, space
width = -1
try:
# Get the current terminal width
import fcntl, termios, struct
h, width = struct.unpack('hh', fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack('hh', 0, 0)))
except:
pass
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
RESET = '\033[0m'
def termcolor(fg=None, bg=None):
codes = []
if fg is not None: codes.append('3%d' % fg)
if bg is not None: codes.append('10%d' % bg)
return '\033[%sm' % ';'.join(codes) if codes else ''
def colorize(message, fg=None, bg=None):
return termcolor(fg, bg) + message + RESET
def indent_wrap(message):
if width == -1:
return message
message = message.replace('\t', ' ')
wrap_area = width - header_size
messagebuf = ''
current = 0
while current < len(message):
next = min(current + wrap_area, len(message))
messagebuf += message[current:next]
if next < len(message):
messagebuf += '\n'
messagebuf += ' ' * header_size
current = next
return messagebuf
LAST_USED = [RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN]
KNOWN_TAGS = {
'dalvikvm': WHITE,
'Process': WHITE,
'ActivityManager': WHITE,
'ActivityThread': WHITE,
'AndroidRuntime': CYAN,
'jdwp': WHITE,
'StrictMode': WHITE,
'DEBUG': YELLOW,
}
def allocate_color(tag):
# this will allocate a unique format for the given tag
# since we dont have very many colors, we always keep track of the LRU
if tag not in KNOWN_TAGS:
KNOWN_TAGS[tag] = LAST_USED[0]
color = KNOWN_TAGS[tag]
if color in LAST_USED:
LAST_USED.remove(color)
LAST_USED.append(color)
return color
RULES = {
# StrictMode policy violation; ~duration=319 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=31 violation=1
re.compile(r'^(StrictMode policy violation)(; ~duration=)(\d+ ms)')
: r'%s\1%s\2%s\3%s' % (termcolor(RED), RESET, termcolor(YELLOW), RESET),
}
# Only enable GC coloring if the user opted-in
if args.color_gc:
# GC_CONCURRENT freed 3617K, 29% free 20525K/28648K, paused 4ms+5ms, total 85ms
key = re.compile(r'^(GC_(?:CONCURRENT|FOR_M?ALLOC|EXTERNAL_ALLOC|EXPLICIT) )(freed <?\d+.)(, \d+\% free \d+./\d+., )(paused \d+ms(?:\+\d+ms)?)')
val = r'\1%s\2%s\3%s\4%s' % (termcolor(GREEN), RESET, termcolor(YELLOW), RESET)
RULES[key] = val
TAGTYPES = {
'V': colorize(' V ', fg=WHITE, bg=BLACK),
'D': colorize(' D ', fg=BLACK, bg=BLUE),
'I': colorize(' I ', fg=BLACK, bg=GREEN),
'W': colorize(' W ', fg=BLACK, bg=YELLOW),
'E': colorize(' E ', fg=BLACK, bg=RED),
'F': colorize(' F ', fg=BLACK, bg=RED),
}
PID_START = re.compile(r'^Start proc ([a-zA-Z0-9._:]+) for ([a-z]+ [^:]+): pid=(\d+) uid=(\d+) gids=(.*)$')
PID_KILL = re.compile(r'^Killing (\d+):([a-zA-Z0-9._]+)/[^:]+: (.*)$')
PID_LEAVE = re.compile(r'^No longer want ([a-zA-Z0-9._]+) \(pid (\d+)\): .*$')
PID_DEATH = re.compile(r'^Process ([a-zA-Z0-9._]+) \(pid (\d+)\) has died.?$')
LOG_LINE = re.compile(r'^([A-Z])/(.+?)\( *(\d+)\): (.*?)$')
BUG_LINE = re.compile(r'.*nativeGetEnabledTags.*')
BACKTRACE_LINE = re.compile(r'^#(.*?)pc\s(.*?)$')
adb_command = ['adb']
if args.device_serial:
adb_command.extend(['-s', args.device_serial])
if args.use_device:
adb_command.append('-d')
if args.use_emulator:
adb_command.append('-e')
adb_command.append('logcat')
adb = subprocess.Popen(adb_command, stdin=PIPE, stdout=PIPE, stderr=PIPE)
pids = set()
last_tag = None
app_pid = None
def match_packages(token):
if len(args.package) == 0:
return True
index = token.find(':')
return (token in args.package) if index == -1 else (token[:index] in args.package)
def parse_death(tag, message):
if tag != 'ActivityManager':
return None
kill = PID_KILL.match(message)
if kill:
pid = kill.group(1)
if match_packages(kill.group(2)) and pid in pids:
return pid
leave = PID_LEAVE.match(message)
if leave:
pid = leave.group(2)
if match_packages(leave.group(1)) and pid in pids:
return pid
death = PID_DEATH.match(message)
if death:
pid = death.group(2)
if match_packages(death.group(1)) and pid in pids:
return pid
return None
while adb.poll() is None:
try:
line = adb.stdout.readline().decode('utf-8', 'replace').strip()
except KeyboardInterrupt:
break
if len(line) == 0:
break
bug_line = BUG_LINE.match(line)
if bug_line is not None:
continue
log_line = LOG_LINE.match(line)
if log_line is None:
continue
level, tag, owner, message = log_line.groups()
start = PID_START.match(message)
if start is not None:
line_package, target, line_pid, line_uid, line_gids = start.groups()
if match_packages(line_package):
pids.add(line_pid)
app_pid = line_pid
linebuf = '\n'
linebuf += colorize(' ' * (header_size - 1), bg=WHITE)
linebuf += indent_wrap(' Process created for %s\n' % target)
linebuf += colorize(' ' * (header_size - 1), bg=WHITE)
linebuf += ' PID: %s UID: %s GIDs: %s' % (line_pid, line_uid, line_gids)
linebuf += '\n'
print(linebuf)
last_tag = None # Ensure next log gets a tag printed
dead_pid = parse_death(tag, message)
if dead_pid:
pids.remove(dead_pid)
linebuf = '\n'
linebuf += colorize(' ' * (header_size - 1), bg=RED)
linebuf += ' Process %s ended' % dead_pid
linebuf += '\n'
print(linebuf)
last_tag = None # Ensure next log gets a tag printed
# Make sure the backtrace is printed after a native crash
if tag.strip() == 'DEBUG':
bt_line = BACKTRACE_LINE.match(message.lstrip())
if bt_line is not None:
message = message.lstrip()
owner = app_pid
if owner not in pids:
continue
if level in LOG_LEVELS_MAP and LOG_LEVELS_MAP[level] < min_level:
continue
linebuf = ''
# right-align tag title and allocate color if needed
tag = tag.strip()
if tag != last_tag or args.always_tags:
last_tag = tag
color = allocate_color(tag)
tag = tag[-args.tag_width:].rjust(args.tag_width)
linebuf += colorize(tag, fg=color)
else:
linebuf += ' ' * args.tag_width
linebuf += ' '
# write out level colored edge
if level in TAGTYPES:
linebuf += TAGTYPES[level]
else:
linebuf += ' ' + level + ' '
linebuf += ' '
# format tag message using rules
for matcher in RULES:
replace = RULES[matcher]
message = matcher.sub(replace, message)
linebuf += indent_wrap(message)
print(linebuf.encode('utf-8'))