-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathigprof-flamegraph.cc
155 lines (138 loc) · 4.86 KB
/
igprof-flamegraph.cc
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
#include <iostream>
#include <string>
#include <regex>
#include <map>
#include <cassert>
typedef std::string::const_iterator Iterator;
struct KW {
std::string kw;
int64_t id;
bool def;
std::vector<KW> value;
std::array<int64_t, 3> ctrs;
std::string data;
double fvalue;
};
struct Frame {
int64_t level;
std::vector<KW> value;
};
std::pair<bool, std::array<int64_t, 3>> parse_ctrs(Iterator begin, Iterator end, int base) {
std::array<int64_t, 3> out;
static std::regex ctr(":\\(([0-9a-f]+),([0-9a-f]+),([0-9a-f]+)\\)");
std::smatch sm;
auto ok = std::regex_match(begin, end, sm, ctr);
if (!ok) return std::make_pair(false, out);
for (unsigned i = 0; i < out.size(); i++) {
out[i] = std::stoul(sm[i+1].str(), nullptr, base);
}
return std::make_pair(true, out);
}
std::pair<bool, KW> parse_kw(Iterator& begin, Iterator end, int base) {
KW out;
out.fvalue = 0.0;
// 1:KW 2:id 3:data 4 5:sub 6:num 7
static std::regex kw("([A-Z]+)([0-9a-f]*)(=\\(([^ )=]*)\\))?(=\\()?(=([0-9a-f.-]+))?.*");
static std::regex tail("((:\\([^)]+\\))?([^ ()=:]*)\\s*).*");
std::smatch sm;
auto ok = std::regex_match(begin, end, sm, kw);
if (!ok) return std::make_pair(false, out);
out.kw = sm[1].str();
out.id = sm[2].length() > 0 ? std::stoul(sm[2].str(), nullptr, base) : 0;
out.def = true;
if (sm[5].length() > 0) {
begin = sm[5].second;
for (;;) {
auto kw = parse_kw(begin, end, base);
if (kw.first) out.value.push_back(kw.second);
if (!kw.first) break;
}
if (begin == end || *begin != ')') return std::make_pair(false, out);
++begin;
} else if (sm[6].length() > 0) {
try {
out.fvalue = std::stof(sm[7].str());
} catch (...) {
out.fvalue = std::stoul(sm[7].str(), nullptr, 16);
}
begin = sm[6].second;
} else if (sm[3].length() > 0) {
out.data = sm[4].str();
begin = sm[3].second;
} else {
out.def = false;
begin = sm[2].second;
}
ok = std::regex_match(begin, end, sm, tail);
if (!ok) return std::make_pair(false, out);
if (sm[2].length() > 0) {
auto ctr = parse_ctrs(sm[2].first, sm[2].second, base);
if (!ctr.first) return std::make_pair(false, out);
out.ctrs = ctr.second;
}
if (sm[3].length() > 0) out.data = sm[3].str();
begin = sm[1].second;
return std::make_pair(true, out);
}
std::pair<bool, Frame> parse_frame(Iterator& begin, Iterator end, int base) {
Frame out;
static std::regex stmt("(C([0-9a-f]+)\\s+).*");
std::smatch sm;
auto ok = std::regex_match(begin, end, sm, stmt);
if (!ok) return std::make_pair(false, out);
out.level = std::stoul(sm[2].str(), nullptr, base);
begin = sm[1].second;
for (;;) {
auto kw = parse_kw(begin, end, base);
if (kw.first) out.value.push_back(kw.second);
if (begin == end) return std::make_pair(true, out);
if (!kw.first) return std::make_pair(false, out);
}
}
int main() {
std::string head;
std::getline(std::cin, head);
Iterator h_it = head.begin();
auto info = parse_kw(h_it, head.end(), 10);
int base = 10;
if (!info.first || !info.second.def) {
std::cerr << "Parse error: " << std::string(head.cbegin(), h_it) << "_" << std::string(h_it, head.cend()) << "\n";
exit(1);
}
if (info.second.value.at(0).kw == "HEX") base = 16;
std::vector<std::string*> stack;
stack.resize(1024);
std::map<int, KW> fns;
while (std::cin) {
std::string line;
std::getline(std::cin, line);
Iterator it = line.begin();
auto res = parse_frame(it, line.end(), base);
if (!res.first) {
std::cerr << "Parse error: " << std::string(line.cbegin(), it) << "_" << std::string(it, line.cend()) << "\n";
continue;
}
Frame& c = res.second;
assert(c.value.size() >= 1);
assert(c.value[0].kw == "FN");
KW& fn = c.value[0];
if (fn.def) {
assert(fn.value.size() == 2);
assert(fn.value[1].kw == "N");
fns[fn.id] = fn;
}
KW& fref = fns[fn.id];
fref.data = fn.data;
assert(c.level < stack.size());
stack[c.level] = &fref.value[1].data;
if (c.value.size() >= 2) {
KW& ctr = c.value[1];
assert(ctr.kw == "V");
for (unsigned i = 1; i <= c.level; i++) {
std::cout << *stack[i] << ";";
}
std::cout << " " << ctr.ctrs[0] << "\n";
}
}
return 0;
}