-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCrashParser.java
More file actions
436 lines (411 loc) · 11.9 KB
/
CrashParser.java
File metadata and controls
436 lines (411 loc) · 11.9 KB
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;
public class CrashParser {
/**
* dump文件路径
*/
private String mDumpFilePath;
/**
* SO符号表路径
*/
private String mSymbolFilePath;
/**
* 统计报告中间文件输出路径
*/
private String mMiddleFilePath;
/**
* 统计报告输出路径
*/
private String mReportFilePath;
/**
* 详细的统计报告输出路径
*/
private String mDetailReportFilePath;
/**
* 无效日志路径
*/
private String mInvalidFilePath;
/**
* 统计报告中间文件输出流
*/
private PrintWriter mMidWriter;
/**
* 统计报告输出流
*/
private PrintWriter mReportWriter;
/**
* 详细的统计报告输出流
*/
private PrintWriter mDetailReportWriter;
/**
* 暂时禁止输出报告,和mReportWriter配合使用
*/
private boolean mDisableReportPrint;
/**
* java崩溃日志的总数量
*/
private int mJavaCrashCount;
/**
* C崩溃日志的总数量
*/
private int mCCrashCount;
/**
* 最多从mDumpFilePath文件读取多少条崩溃
*/
private int mCrashMax;
/**
* 一个崩溃日志为多行格式时,累加每行到此字符串
*/
private String mMutilineCrashString;
/**
* 崩溃组列表
*/
private ArrayList<CrashGroup> mCrashGroups = new ArrayList<CrashGroup>(); // 崩溃组列表
/**
* 无效的崩溃日志列表
*/
private ArrayList<CrashItem> mInvalidItems = new ArrayList<CrashItem>(); // 无效的崩溃日志列表
/**
* 统计报告是否输出到System.out
*/
private boolean mEnableStdOut; // 统计报告是否输出到System.out
/**
* mSymbolFilePath为null,却出现了C崩溃日志
*/
private boolean mWarnCCrash; // mSymbolFilePath为null,却出现了C崩溃日志
/**
* 报告输出目录
*/
private String mOutputDir; // 报告输出目录
/**
* 构建一个崩溃解析器。
* @param dumpFilePath dump文件路径。
* @param symbolFilePath SO符号表路径。
* @param crashMax 最多从mDumpFilePath文件读取多少条崩溃。
*/
public CrashParser(String dumpFilePath, String symbolFilePath, int crashMax) {
mSymbolFilePath = symbolFilePath;
mDumpFilePath = dumpFilePath;
mCrashMax = crashMax;
File file = new File(mDumpFilePath);
String dumpDir = file.getParent();
String dumpName = file.getName();
int index = dumpName.lastIndexOf(".");
if (index != -1) {
dumpName = dumpName.substring(0, index);
}
mOutputDir = new File(dumpDir, dumpName + "_crash_report").getAbsolutePath();
FileHelper.delete(mOutputDir);
new File(mOutputDir).mkdirs();
}
/**
* 解析日志,一条日志有可能是单行的(以\\n分隔),也有可能是多行的。
* 解析器每次读取一行日志,判断日志类型.
* @throws java.io.IOException 如果发生IO异常
* @throws java.lang.InterruptedException 如果发生异常
*/
public void parseCrash() throws IOException, InterruptedException {
InputStream fin;
Reader fReader;
BufferedReader bReader;
fin = new FileInputStream(mDumpFilePath);
String encode = decideFileEncode(mDumpFilePath);
if (encode == null) {
encode = "utf-8";
} else if (encode == "unicode") {
fin.skip(2);
} else {
fin.skip(3);
}
fReader = new InputStreamReader(fin, encode);
bReader = new BufferedReader(fReader);
String line;
mMutilineCrashString = "";
while ((line = bReader.readLine()) != null) {
if (line.contains("\\n")) {
finishMutilineCrashString();
CrashItem crashItem = new CrashItem(line, false);
addCrash(crashItem);
} else {
if (line.startsWith("*** *** ***") || line.isEmpty()) {
finishMutilineCrashString();
}
if (!line.isEmpty()) {
mMutilineCrashString += line;
mMutilineCrashString += "\n";
}
}
if (mCrashMax != 0 && mJavaCrashCount + mCCrashCount >= mCrashMax) {
break;
}
}
if (mCrashMax == 0 || mJavaCrashCount + mCCrashCount < mCrashMax) {
finishMutilineCrashString();
}
bReader.close();
sortCrash();
printCrash(CrashType.JAVA_CRASH);
printCrash(CrashType.C_CRASH);
printInvalidCrash();
if (mWarnCCrash) {
System.out.println("warn: dump file contain c crash,but \"-sym\" param not special.");
}
}
/**
* 根据文件头,判断文件编码。
* @param filePath 判断文件编码
* @return 返回文件编码。
*/
private String decideFileEncode(String filePath) throws IOException {
byte buff[] = new byte[3];
FileInputStream in = null;
try {
in = new FileInputStream(filePath);
int readBytes = in.read(buff);
if (readBytes >= 2 && buff[0] == (byte) 0xFF && buff[1] == (byte) 0xFE) {
return "unicode";
} else if (readBytes >= 3 && buff[0] == (byte) 0xEF && buff[1] == (byte) 0xBB && buff[2] == (byte) 0xBF) {
return "utf-8";
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
}
return null;
}
/**
* 完成多行崩溃。
*/
private void finishMutilineCrashString() throws IOException, InterruptedException {
if (mMutilineCrashString.isEmpty()) {
return;
}
CrashItem crashItem = new CrashItem(mMutilineCrashString, true);
addCrash(crashItem);
mMutilineCrashString = "";
}
/**
* 添加一个崩溃。
* @param crashItem 一个CrashItem.
*/
private void addCrash(CrashItem crashItem) throws IOException, InterruptedException {
if (crashItem.getCrashType() == CrashType.JAVA_CRASH) {
mJavaCrashCount++;
} else {
if (mSymbolFilePath == null) {
mWarnCCrash = true;
return;
}
mCCrashCount++;
}
System.out.println(String.format("正在解析第%d个日志...", mJavaCrashCount + mCCrashCount));
decodeCrash(crashItem);
if (!crashItem.isValid()) {
mInvalidItems.add(crashItem);
return;
}
CrashGroup crashGroup = findCrashGroup(crashItem);
if (crashGroup == null) {
mCrashGroups.add(new CrashGroup(crashItem));
} else {
crashGroup.addCrash(crashItem);
}
}
/**
* 找到CrashItem的在的崩溃组
* @param item 一个CrashItem.
* @return 如果找到,返回组,否则,返回null。
*/
private CrashGroup findCrashGroup(CrashItem item) {
for (int i = 0; i < mCrashGroups.size(); i++) {
if (mCrashGroups.get(i).matchCrash(item)) {
return mCrashGroups.get(i);
}
}
return null;
}
/**
* 排序崩溃。
*/
private void sortCrash() {
Collections.sort(mCrashGroups,new Comparator<CrashGroup>() {
@Override
public int compare(CrashGroup o1, CrashGroup o2) {
int o1Count = o1.getCrashCount();
int o2Count = o2.getCrashCount();
if (o1.getKeyword().isEmpty()) {
o1Count = 0;
}
if (o2.getKeyword().isEmpty()) {
o2Count = 0;
}
return o2Count - o1Count;
}
});
for (int i = 0; i < mCrashGroups.size(); i++) {
CrashGroup group = mCrashGroups.get(i);
group.sort();
}
}
/**
* 调用ndk-stack,解析崩溃日志。
*/
private void decodeCrash(CrashItem item) throws IOException, InterruptedException {
if (item.getCrashType() != CrashType.C_CRASH) {
return;
}
String command = String.format("ndk-stack -sym %s", mSymbolFilePath);
Process proc = Runtime.getRuntime().exec(command);
proc.getOutputStream().write(item.getMidCrashString().getBytes("utf-8"));
proc.getOutputStream().close();
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream(), "utf-8"));
String line = null;
ArrayList<String> crashLines = new ArrayList<String>();
while ((line = reader.readLine()) != null) {
crashLines.add(line);
}
item.setCSymbolCrashLines(crashLines);
}
/**
* 输出信息。
*/
private void printMessage(String msg) {
if (mMidWriter != null) {
mMidWriter.println(msg);
}
if (mReportWriter != null) {
if (!mDisableReportPrint) {
mReportWriter.println(msg);
}
}
if (mDetailReportWriter != null) {
mDetailReportWriter.println(msg);
}
if (mEnableStdOut) {
System.out.println(msg);
}
}
/**
* 创建崩溃日志输出流。
*/
private void createPrintStream(CrashType crashType) throws IOException {
String des = crashType == CrashType.JAVA_CRASH ? "java" : "c";
// mMiddleFilePath= new File(mOutputDir, des +
// "_mid.txt").getAbsolutePath();
mReportFilePath = new File(mOutputDir, des + "_report.txt").getAbsolutePath();
mDetailReportFilePath = new File(mOutputDir, des + "_detail_report.txt").getAbsolutePath();
if (mMiddleFilePath != null) {
mMidWriter = new PrintWriter(new FileWriter(mMiddleFilePath));
}
if (mReportFilePath != null) {
mReportWriter = new PrintWriter(new FileWriter(mReportFilePath));
}
if (mDetailReportFilePath != null) {
mDetailReportWriter = new PrintWriter(new FileWriter(mDetailReportFilePath));
}
}
/**
* 输出崩溃报表。
*/
private void printCrash(CrashType crashType) throws IOException {
ArrayList<CrashGroup> crashGroups = new ArrayList<>();
for (int i = 0; i < mCrashGroups.size(); i++) {
CrashGroup crashGroup = mCrashGroups.get(i);
if (crashGroup.getCrashType() == crashType) {
crashGroups.add(crashGroup);
}
}
if (crashGroups.isEmpty()) {
return;
}
createPrintStream(crashType);
String carshTypeDes = crashType == CrashType.JAVA_CRASH ? "java" : "C";
int crashCount = (crashType == CrashType.JAVA_CRASH ? mJavaCrashCount : mCCrashCount);
String msg = String.format("本次共分析%d个%s日志,分析后得到%d种崩溃", crashCount, carshTypeDes, crashGroups.size());
printMessage(msg);
printMessage("");
for (int i = 0; i < crashGroups.size(); i++) {
CrashGroup group = crashGroups.get(i);
msg = String.format(
"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<开始崩溃组<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
printMessage(msg);
msg = String.format("崩溃编号:%d", i + 1);
printMessage(msg);
msg = String.format("崩溃触发函数:%s", group.getKeyword());
printMessage(msg);
msg = String.format("崩溃总次数:%d", group.getCrashCount());
printMessage(msg);
printMessage("崩溃关键函数:");
printMessage("崩溃分析:");
msg = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
printMessage(msg);
for (int j = 0; j < group.size(); j++) {
if (j >= 2) {
mDisableReportPrint = true;
}
CrashItem crashItem = group.get(j);
msg = String.format("\t崩溃次数:%d", crashItem.getCrashCount());
printMessage(msg);
if (mMidWriter != null) {
mMidWriter.println(crashItem.getMidCrashString());
}
if (mReportWriter != null) {
if (j < 2) {
mReportWriter.println(crashItem.getSymbolCrashString());
}
}
if (mDetailReportWriter != null) {
mDetailReportWriter.println(crashItem.getSymbolCrashString());
}
if (mEnableStdOut) {
System.out.println(crashItem.getSymbolCrashString());
}
mDisableReportPrint = false;
}
if (mReportWriter != null) {
if (group.size() > 2) {
mReportWriter.print("\t...");
}
}
printMessage("\n\n");
}
if (mMidWriter != null) {
mMidWriter.close();
}
if (mReportWriter != null) {
mReportWriter.close();
}
if (mDetailReportWriter != null) {
mDetailReportWriter.close();
}
}
/**
* 输出无效的日志。
*/
private void printInvalidCrash() throws IOException {
if (mInvalidItems.size() == 0) {
return;
}
mInvalidFilePath = new File(mOutputDir, "invalid.txt").getAbsolutePath();
PrintWriter invalidWriter = new PrintWriter(new FileWriter(mInvalidFilePath));
invalidWriter.println(String.format("共有%d个无效日志.\n", mInvalidItems.size()));
for (int i = 0; i < mInvalidItems.size(); i++) {
CrashItem crashItem = mInvalidItems.get(i);
invalidWriter.println(crashItem.getOriginCrashString());
}
invalidWriter.close();
}
}