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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Copyright (C) 2019 Unisoc
Created on Jan 18, 2016
2016.03.11 -- add ytag parser
2019.01.05 -- add .ylog parser
2019.11.19 -- add ylog 4.x cap file parser
"""
import errno
import operator
import optparse
import os
import shutil
import sys
import traceback
import zlib
from ctypes import *
LOG_FILE_MAX_SIZE = 1024 * 1024 * 250
KEY_LENGTH = 1
class FileHeader(Structure):
_fields_ = [('m', c_int),
('r', c_char * 64),
('v', c_int),
('re', c_char * 24), ]
class BlockHeader(Structure):
_fields_ = [('m', c_int),
('seq', c_uint),
('l', c_int),
('z', c_int),
('t', c_char)]
class FileTail(Structure):
_fields_ = [('m', c_int),
('seq', c_uint),
('l', c_int),
('v', c_int),
('r', c_char * 64),
('re', c_char * 24), ]
ANDROID_LOG_FILE_DICT = {
'M': 'android_main.log',
'S': 'android_system.log',
'R': 'android_radio.log',
'E': 'android_events.log',
'C': 'android_crash.log',
}
YLOG_LOG_FILE_DICT = {}
YLOG_CTL_CMD_DICT = {}
YLOG_LOG_FILE_FD_DICT = {}
YLOG_LOG_FILE_ID_DICT = {}
IS_DEBUG_MODE = False
# Python version check
ver = sys.version_info
if ver[0] == 3:
my_open = open
else:
def my_open(file, mode='rb'):
return open(file, mode)
def get_next_filename(old_file_name, index):
return "{id}-{name}".format(id=index, name=old_file_name)
def split_andorid_log_file(root_dir, log_file_dir_path, android_log, android_log_file_dict):
android_log_file_fd_dict = {}
android_log_file_id_dict = {}
keys = list(android_log_file_dict.keys())
last_token = ''
log_file_dir_full_path = root_dir + log_file_dir_path
for key in keys:
android_log_file_id_dict[key] = 0
filename = get_next_filename(android_log_file_dict[key], android_log_file_id_dict[key])
android_log_file_fd_dict[key] = my_open(os.path.join(log_file_dir_full_path, filename), 'wb')
count = -1
while True:
count = count + 1
filename = os.path.join(log_file_dir_full_path, get_next_filename(android_log, count))
log_file = None
if os.path.isfile(filename):
log_file = my_open(filename, 'rb')
if log_file is None:
break
for binary_data in log_file.readlines():
log_line = binary_data.decode(errors='ignore')
if log_line[0:KEY_LENGTH] in keys:
token = log_line[0:KEY_LENGTH]
android_log_file_fd_dict[token].write(binary_data)
last_token = token
if android_log_file_fd_dict[token].tell() > LOG_FILE_MAX_SIZE:
android_log_file_id_dict[token] = android_log_file_id_dict[token] + 1
filename = get_next_filename(android_log_file_dict[token], android_log_file_id_dict[token])
android_log_file_fd_dict[token].close()
android_log_file_fd_dict[token] = my_open(os.path.join(log_file_dir_full_path, filename), 'wb')
else:
if len(last_token) > 0:
android_log_file_fd_dict[last_token].write(binary_data)
log_file.close()
for key in keys:
android_log_file_fd_dict[key].close()
def unzip_ylog_file(ylog_file, log_file_dir):
global IS_DEBUG_MODE
ylog_dict_build = False
debug_log_file = None
try:
tmp_ylog_file = ylog_file + ".tmp"
if not os.path.isfile(ylog_file):
print("open ylog err")
return
if IS_DEBUG_MODE:
debug_log_file = open(tmp_ylog_file, "wb")
with open(ylog_file, 'rb') as file:
file_header = FileHeader()
while file.readinto(file_header) == sizeof(file_header):
if file_header.m != 0x2E2E:
if IS_DEBUG_MODE:
print('format error')
pass
break
block_header = BlockHeader()
block_count = 1
block_size = sizeof(BlockHeader)
read_block_size = file.readinto(block_header)
seeking = False
while read_block_size == block_size:
block_count = block_count + 1
if block_header.m != 0x5A5A:
if block_header.m == 0xB5B5:
file.read(sizeof(FileTail) - sizeof(BlockHeader))
else:
if block_header.m == 0x2E2E:
file.read(sizeof(FileHeader) - sizeof(BlockHeader))
else:
file.seek(1 - read_block_size, 1)
seeking = True
else:
if seeking:
if (block_header.seq > 0) and (block_header.l > 0) and (block_header.z == 0 or block_header.z == 1):
if IS_DEBUG_MODE:
print("seek done! position ", file.tell() - block_size)
seeking = False
else:
read_block_size = file.readinto(block_header)
continue
block_data = file.read(block_header.l)
if block_header.z == 1:
try:
raw_data = zlib.decompress(block_data)
except:
pass
else:
raw_data = block_data
if not ylog_dict_build:
ylog_dict_build = True
build_ylog_dict(raw_data, log_file_dir)
if (block_header.t != '\x00') and (ord(block_header.t) != 0):
key_order = ord(block_header.t)
if YLOG_CTL_CMD_DICT[key_order] == 0:
decode_data = raw_data.decode(errors='ignore')
if decode_data.find('YZIPC') != -1 and decode_data.find('CPIZY') != -1:
YLOG_CTL_CMD_DICT[key_order] = 1
YLOG_LOG_FILE_FD_DICT[key_order].write(raw_data)
if (YLOG_LOG_FILE_FD_DICT[key_order].tell() > LOG_FILE_MAX_SIZE) and (
YLOG_LOG_FILE_ID_DICT[key_order] != -1):
YLOG_LOG_FILE_ID_DICT[key_order] = YLOG_LOG_FILE_ID_DICT[key_order] + 1
filename = get_next_filename(YLOG_LOG_FILE_DICT[key_order],
YLOG_LOG_FILE_ID_DICT[key_order])
YLOG_LOG_FILE_FD_DICT[key_order].close()
YLOG_LOG_FILE_FD_DICT[key_order] = my_open(
os.path.join(log_file_dir, filename), 'wb')
YLOG_LOG_FILE_FD_DICT[key_order].write(raw_data[raw_data.rfind("\n".encode())+1:])
if IS_DEBUG_MODE:
debug_log_file.write(raw_data)
read_block_size = file.readinto(block_header)
except:
traceback.print_exc()
pass
if debug_log_file is not None:
debug_log_file.close()
keys = list(YLOG_LOG_FILE_FD_DICT.keys())
for key in keys:
if YLOG_LOG_FILE_FD_DICT[key] is not None:
YLOG_LOG_FILE_FD_DICT[key].close()
def build_ylog_dict(meta_info, log_file_dir):
YLOG_LOG_FILE_DICT.clear()
YLOG_CTL_CMD_DICT.clear()
if IS_DEBUG_MODE:
print(YLOG_LOG_FILE_DICT)
print('meta:', meta_info)
meta = meta_info.decode("utf-8")
start = meta.find("TAGS:")
line = meta[start:]
if line[0:5] == "TAGS:":
if IS_DEBUG_MODE:
print(line)
line.strip()
tags = [x for x in line[5:256].split(';') if x.strip()]
for tag in tags:
key = tag[0:tag.find(":")]
value = tag[tag.find(":") + 1:]
key_order = ord(key)
YLOG_LOG_FILE_DICT.update({key_order: value})
YLOG_CTL_CMD_DICT.update({key_order: 0})
if IS_DEBUG_MODE:
print('all log dict:', YLOG_LOG_FILE_DICT.keys())
keys = list(YLOG_LOG_FILE_DICT.keys())
for key in keys:
YLOG_LOG_FILE_ID_DICT[key] = 0
if YLOG_LOG_FILE_DICT[key].find('.log') == -1:
YLOG_LOG_FILE_ID_DICT[key] = -1
filename = YLOG_LOG_FILE_DICT[key]
if YLOG_LOG_FILE_ID_DICT[key] != -1:
filename = get_next_filename(filename, YLOG_LOG_FILE_ID_DICT[key])
YLOG_LOG_FILE_FD_DICT[key] = my_open(os.path.join(log_file_dir, filename), 'wb')
if IS_DEBUG_MODE:
print("new fd dict:", key, '=', YLOG_LOG_FILE_FD_DICT[key])
def repair_cap_file(filename, filemagic):
if ver[0] == 3:
magic = filemagic
else:
magic = str(filemagic)
raw_cap_file = open(filename, "rb")
while True:
data = raw_cap_file.read(len(magic))
if len(data) < len(magic):
if IS_DEBUG_MODE:
print(filename, "repair failed....")
break
if operator.eq(data, magic):
if raw_cap_file.tell() == len(magic):
if IS_DEBUG_MODE:
print(filename, "raw format ok....")
break
if raw_cap_file.tell() > 0:
tmp_file_name = filename + '.tmp'
tmp_file_fd = open(tmp_file_name, 'wb')
raw_cap_file.seek(-1 * len(magic), 1)
tmp_file_fd.write(raw_cap_file.read())
tmp_file_fd.close()
raw_cap_file.close()
if IS_DEBUG_MODE:
print(filename, "repair ok....")
print(tmp_file_name, filename)
os.remove(filename)
os.rename(tmp_file_name, filename)
return True
raw_cap_file.seek(-1 * (len(magic) - 1), 1)
raw_cap_file.close()
def parser_ylog_files(root_path, ylog_files):
for file in ylog_files:
log_file_dir_path = file.replace('.ylog', '')
log_file_dir = log_file_dir_path
log_file_dir_path = '.' + os.sep + log_file_dir_path
print('extracting ' + file + ' to ' + log_file_dir + os.sep)
if os.path.exists(log_file_dir_path):
shutil.rmtree(log_file_dir_path)
try:
os.mkdir(log_file_dir_path)
except:
if IS_DEBUG_MODE:
traceback.print_exc()
pass
unzip_ylog_file(file, log_file_dir_path)
try:
split_andorid_log_file(root_path, log_file_dir_path, "android.log", ANDROID_LOG_FILE_DICT)
parser_ctl_cmd(log_file_dir_path)
except:
if IS_DEBUG_MODE:
traceback.print_exc()
pass
recheck_file(log_file_dir_path)
continue
def recheck_file(log_file_dir_path):
out_log_files = os.listdir(log_file_dir_path)
for out_log_file in out_log_files:
log_file = os.path.join(log_file_dir_path, out_log_file)
if (not os.path.isdir(log_file)) and (0 == os.path.getsize(log_file)):
os.remove(log_file)
continue
if not os.path.isdir(log_file):
if 'tcpdump' in log_file:
repair_cap_file(log_file, b'\xd4\xc3\xb2\xa1')
if 'hcidump' in log_file:
repair_cap_file(log_file, b'\x62\x74\x73\x6e\x6f\x6f\x70')
def parser_ctl_cmd(log_file_dir):
keys = list(YLOG_LOG_FILE_DICT.keys())
for key in keys:
if YLOG_CTL_CMD_DICT[key] != 1:
continue
filename = YLOG_LOG_FILE_DICT[key]
log_file_name = os.path.join(log_file_dir, '0-' + filename)
yzipctl_cmd_len = 40
if ('lastlog' in log_file_name) and (os.path.getsize(log_file_name) < yzipctl_cmd_len):
os.remove(log_file_name)
continue
log_file_fd = my_open(log_file_name, 'rb')
out_file_fd = None
for binary_data in log_file_fd.readlines():
line_data = binary_data.decode(errors='ignore')
start = line_data.find('YZIPC02')
if start != -1:
end = line_data.find('20CPIZY')
out_file_name = log_file_dir + os.sep + binary_data[start + 7:end].decode(errors='ignore')
if out_file_fd is not None:
out_file_fd.write(binary_data[0:start])
out_file_fd.close()
if not os.path.exists(os.path.dirname(out_file_name)):
try:
os.makedirs(os.path.dirname(out_file_name))
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
out_file_fd = my_open(out_file_name, 'wb')
out_file_fd.write(binary_data[end + 7:])
else:
if out_file_fd is not None:
out_file_fd.write(binary_data)
log_file_fd.close()
out_file_fd.close()
def static_andorid_log(android_log_file):
is_first_line = None
last_line = ""
line_count = 0
liblog_count = 0
lost_log_count = 0
word_dict = {}
is_ylog_log = None
with my_open(android_log_file, 'rb') as f:
for binary_line in f.readlines():
if is_first_line is None:
is_first_line = binary_line
last_line = binary_line
line_count = line_count + 1
line = binary_line.decode(errors='ignore')
if line.find('liblog :') > 0:
liblog_count = liblog_count + 1
liblog_value = line[line.find('liblog :') + 9:]
if IS_DEBUG_MODE:
print(int(liblog_value))
lost_log_count = lost_log_count + int(liblog_value)
else:
keys = line.split()
if len(keys) > 6:
if '---' in keys[0]:
continue
if is_ylog_log is None:
if ('M' in keys[0] or 'S' in keys[0] or 'E' in keys[0] or 'R' in keys[0] or 'C' in keys[0]):
is_ylog_log = True
else:
is_ylog_log = False
if is_ylog_log:
tag_index = 6
else:
tag_index = 5
key = keys[tag_index]
else:
key = "unknown"
if key in word_dict:
word_dict[key] += 1
else:
word_dict[key] = 1
word_tups = sorted(word_dict.items(), key=lambda x: x[1], reverse=True)
if IS_DEBUG_MODE:
print('0:', is_first_line)
print(line_count, ':', last_line)
print('liblog', liblog_count)
print('liblogc', lost_log_count)
print('Android log output statistics ')
ljust_len = 18
print('Lost rate : '.ljust(ljust_len) + '{:.2%}'.format(
lost_log_count * 1.0001 / (line_count + lost_log_count) * 1.0001))
print("Total line num:".ljust(ljust_len) + str(line_count + lost_log_count))
print("Lost line num:".ljust(ljust_len) + str(lost_log_count))
print('\n')
max_tags = 0
print('Chattiest top 12 tags:')
print('TAG'.ljust(32) + 'NUM'.ljust(8) + 'PERCENT')
for word_tup in word_tups:
max_tags += 1
print(
word_tup[0].ljust(32) + str(word_tup[1]).ljust(8) + '{:.2%}'.format(word_tup[1] * 1.0001 / line_count))
if max_tags > 12:
break
print('\n')
def sort_by_apk_path(elem):
return elem[2]
def get_version_info(log_file):
lst = []
version_info_file = log_file + '.version.log'
outfile = open(version_info_file, 'w')
do_dump_package = None
package_name = None
apk_path = None
with my_open(log_file, 'rb') as f:
for binary_line in f.readlines():
line = binary_line.decode(errors='ignore')
if 'dumpsys package' in line:
last_do_dump_package = do_dump_package
do_dump_package = line
if last_do_dump_package is not None:
lst.sort(key=sort_by_apk_path)
outfile.write('\n\n' + last_do_dump_package)
for i in lst:
outfile.write(i[0] + ' ' + i[1] + ' ' + i[2] + '\n')
lst = []
package_name = None
apk_path = None
else:
if package_name is None:
if 'Package [' in line:
package_name = line
else:
if apk_path is None:
if 'codePath=' in line:
apk_path = line
else:
if 'versionName=' in line:
version_info = line
lst.append((package_name.strip(), version_info.strip(), apk_path.strip()))
package_name = None
apk_path = None
if do_dump_package is not None:
lst.sort(key=sort_by_apk_path)
outfile.write('\n\n' + do_dump_package)
for i in lst:
outfile.write(i[0] + ' ' + i[1] + ' ' + i[2] + '\n')
outfile.close()
print('get version info to file ' + version_info_file)
def main():
global IS_DEBUG_MODE
os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))
ylog_parser_relative_path = '.' + os.sep
parser = optparse.OptionParser()
parser.add_option('-d', dest='set_debug_mode', default=False, action='store_true', help='output some debug info')
parser.add_option('-s', dest='do_statis', default=False, action='store_true', help='statistics log file')
parser.add_option('-v', dest='do_versioninfo', default=False, action='store_true', help='get apk version file')
options, args = parser.parse_args()
if options.set_debug_mode:
IS_DEBUG_MODE = True
if options.do_statis:
static_andorid_log(ylog_parser_relative_path + sys.argv[2])
return
if options.do_versioninfo:
get_version_info(ylog_parser_relative_path + sys.argv[2])
return
if not args:
all_files = os.listdir(os.path.join(ylog_parser_relative_path, ''))
if IS_DEBUG_MODE:
print(all_files)
ylog_files = [f for f in all_files if f.find(".ylog", len(f) - 5) > 1]
else:
ylog_files = args
if IS_DEBUG_MODE:
print(ylog_files)
parser_ylog_files(ylog_parser_relative_path, ylog_files)
print("extract done")
if __name__ == '__main__':
main()
|