-
Notifications
You must be signed in to change notification settings - Fork 0
/
gostackparser.ts
246 lines (233 loc) · 6.39 KB
/
gostackparser.ts
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
import { Readable } from 'stream';
import * as vscode from 'vscode';
export interface Goroutine {
ID: number;
State: string;
Wait: number;
LockedToThread: boolean;
Stack: Frame[];
FramesElided: boolean;
CreatedBy: Frame;
Ancestor?: Goroutine;
}
export interface Frame {
Func: string;
File: string;
Line: number;
}
type ParserState =
| 'stateHeader'
| 'stateStackFunc'
| 'stateStackFile'
| 'stateCreatedBy'
| 'stateCreatedByFunc'
| 'stateCreatedByFile'
| 'stateOriginatingFrom';
function parseGoroutineHeader(line: string): Goroutine | null {
const goroutineHeader = /^(\d+) \[([^,]+)(?:, (\d+) minutes)?(, locked to thread)?\]:$/;
const matches = line.match(goroutineHeader);
if (!matches || matches.length !== 5) {
return null;
}
const id = parseInt(matches[1]);
const state = matches[2];
const waitMinutes = matches[3];
const locked = matches[4] !== undefined;
const g: Goroutine = {
ID: id,
State: state,
Wait: waitMinutes ? parseInt(waitMinutes) * 60 * 1000 : 0,
LockedToThread: locked,
Stack: [],
FramesElided: false,
CreatedBy: { Func: '', File: '', Line: 0 },
};
return g;
}
function parseFunc(line: string, state: ParserState): Frame | null {
if (state === 'stateCreatedByFunc') {
const i = line.indexOf(' ');
if (i > 0) {
return { Func: line.slice(0, i), File: '', Line: 0 };
}
return { Func: line, File: '', Line: 0 };
}
let openIndex = -1;
let closeIndex = -1;
for (let i = 0; i < line.length; i++) {
const r = line[i];
switch (r) {
case '(':
if (openIndex !== -1 && closeIndex === -1) {
return null;
}
openIndex = i;
closeIndex = -1;
break;
case ')':
if (openIndex === -1 || closeIndex !== -1) {
return null;
}
closeIndex = i;
break;
}
}
if (openIndex === -1 || closeIndex === -1 || openIndex === 0) {
return null;
}
return { Func: line.slice(0, openIndex), File: '', Line: 0 };
}
function parseFile(line: string, f: Frame): boolean {
if (line.length < 2 || line[0] !== '\t') {
return false;
}
let ret : boolean = false;
line = line.slice(1);
const stateFilename = 0;
const stateColon = 1;
const stateLine = 2;
let state = stateFilename;
for (let i = 0; i < line.length; i++) {
const c = line[i];
switch (state) {
case stateFilename:
if (c === ':') {
state = stateColon;
}
break;
case stateColon:
if (isDigit(c)) {
f.File = line.slice(0, i - 1);
f.Line = parseInt(c);
state = stateLine;
ret = true;
} else {
state = stateFilename;
}
break;
case stateLine:
if (c === ' ') {
return true;
} else if (!isDigit(c)) {
return false;
}
f.Line = f.Line * 10 + parseInt(c);
break;
}
}
return ret;
}
function isDigit(c: string): boolean {
return c >= '0' && c <= '9';
}
export function parse(value: string): [Goroutine[], Error[]] {
// const sc = r.getReader();
let state: ParserState = 'stateHeader';
let lineNum = 0;
let line = '';
var goroutines: Goroutine[] = [];
const errs: Error[] = [];
let g: Goroutine | null = null;
let f: Frame | null = null;
const abortGoroutine = (msg: string) => {
const err = new Error(`${msg} on line ${lineNum}: ${line}`);
errs.push(err);
goroutines.pop();
state = 'stateHeader';
};
// while (true) {
// const { done, value } = await sc.read();
// if (done) {
// break;
// }
line += value;
if (!line.endsWith("\n")) {
line = line.concat("\n");
}
const lines = line.split('\n');
for (let i = 0; i < lines.length - 1; i++) {
lineNum++;
line = lines[i];
switch (state) {
case 'stateHeader':
case 'stateOriginatingFrom':
if (state === 'stateHeader') {
if (!line.startsWith('goroutine ')) {
continue;
}
g = parseGoroutineHeader(line.slice(10));
if (g === null) {
abortGoroutine('invalid goroutine header');
continue;
}
goroutines.push(g);
}
if (state === 'stateOriginatingFrom') {
const ancestorIDStr = line.slice(28, -2);
const ancestorID = parseInt(ancestorIDStr);
if (isNaN(ancestorID)) {
abortGoroutine('invalid ancestor ID');
continue;
}
const ancestorG: Goroutine = { ID: ancestorID, State: '', Wait: 0, LockedToThread: false, Stack: [], FramesElided: false, CreatedBy: { Func: '', File: '', Line: 0 } };
g!.Ancestor = ancestorG;
g = ancestorG;
}
state = 'stateStackFunc';
break;
case 'stateStackFunc':
case 'stateCreatedByFunc':
if (line.startsWith('created by ')) {
line = line.slice(11);
}
f = parseFunc(line, state);
if (f === null) {
if (line === '...additional frames elided...') {
g!.FramesElided = true;
state = 'stateCreatedBy';
continue;
}
if (line.startsWith('[originating from goroutine ')) {
state = 'stateOriginatingFrom';
i--;
continue;
}
abortGoroutine('invalid function call');
continue;
}
if (state === 'stateStackFunc') {
g!.Stack.push(f);
state = 'stateStackFile';
} else {
g!.CreatedBy = f;
state = 'stateCreatedByFile';
}
break;
case 'stateStackFile':
case 'stateCreatedByFile':
if (!parseFile(line, f!)) {
abortGoroutine('invalid file:line ref');
continue;
}
state = 'stateCreatedBy';
break;
case 'stateCreatedBy':
if (line.startsWith('created by ')) {
line = line.slice(11);
state = 'stateCreatedByFunc';
i--;
continue;
} else if (line.length === 0) {
state = 'stateHeader';
} else {
state = 'stateStackFunc';
i--;
continue;
}
break;
}
}
line = lines[lines.length - 1];
// }
return [goroutines, errs];
}