-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.cpp
95 lines (83 loc) · 2.49 KB
/
Parser.cpp
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
// Copyright 2014-present. All rights reserved.
//
// Author: Victor Loh
#include "folly/String.h"
#include <iostream>
#include <fstream>
using namespace std;
struct JstackThreadInfo {
string threadName;
string threadState;
vector<string> stacktrace;
// A JstackThreadInfo is considered empty if its threadName is not set
bool empty() {
return threadName.empty();
}
void clear() {
threadName.clear();
threadState.clear();
stacktrace.clear();
}
};
struct JstackInfo {
vector<JstackThreadInfo> threadsInfo;
};
int main(int argc, char* argv[]) {
if (argc < 2) {
cout << "usage is ./a.out <jstack_file>" << endl;
exit(0);
}
ifstream inFile(argv[1]);
stringstream strStream;
strStream << inFile.rdbuf();
string str = strStream.str();
vector<folly::StringPiece> v;
folly::split("\n", str, v);
// Skip the first 2 rows (timestamp + jstack junk)
JstackInfo jstackInfo;
JstackThreadInfo threadInfo;
for (int i = 2; i < v.size(); ++i) {
auto& sp = v[i];
if (sp.front() == '"') {
sp.advance(1);
auto p = sp.split_step('"');
threadInfo.threadName = std::move(p.toString());
} else if (sp.empty()) {
// Reached a newline, the right course of action would be to add
// JstackThreadInfo to JstackInfo and then clear out the current
// JstackThreadInfo
if (!threadInfo.empty()) {
jstackInfo.threadsInfo.emplace_back(std::move(threadInfo));
threadInfo.clear();
}
} else {
folly::StringPiece threadStateNeedle("java.lang.Thread.State: ");
auto pos = sp.find(threadStateNeedle);
if (pos != string::npos) {
sp.advance(pos + threadStateNeedle.size());
threadInfo.threadState = std::move(sp.toString());
continue;
}
folly::StringPiece stackTraceStartNeedle("at ");
pos = sp.find(stackTraceStartNeedle);
if (pos != string::npos) {
sp.advance(pos + stackTraceStartNeedle.size());
auto p = sp.split_step('(');
string m = std::move(p.toString());
threadInfo.stacktrace.emplace_back(std::move(p.toString()));
}
}
}
cout << "Jstack info" << endl;
for (auto& info : jstackInfo.threadsInfo) {
cout << "Thread name: " << info.threadName << endl;
cout << "Thread state: " << info.threadState << endl;
cout << "Stacktrace: " << endl;
for (int i = 0; i < info.stacktrace.size(); ++i) {
if (i != 0) cout << ", ";
cout << info.stacktrace[i];
}
cout << endl;
}
return 0;
}