-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdebugger.ts
388 lines (353 loc) · 15.9 KB
/
debugger.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
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
import * as vscode from 'vscode';
import * as util from './util'
import { DebugProtocol } from 'vscode-debugprotocol';
export enum Language { Cpp, CSharp, Java, JavaScript, Python, Ruby };
export class SessionInfo {
constructor(
public session: vscode.DebugSession,
public threadId: number,
public frameId: number)
{
this.language = this._getLanguage();
this.context = this._getContext();
this.debugger = this._getDebugger();
}
private _getLanguage(): Language | undefined {
const sessionType = this.session.type;
if (sessionType === undefined)
return undefined;
if (['cppvsdbg', 'cppdbg', 'lldb', 'cortex-debug'].includes(sessionType))
return Language.Cpp;
else if (['coreclr'].includes(sessionType))
return Language.CSharp;
else if (['java'].includes(sessionType))
return Language.Java;
else if (['node', 'chrome', 'msedge', 'pwa-node', 'pwa-chrome', 'pwa-msedge'].includes(sessionType))
return Language.JavaScript;
else if (['python', 'debugpy', 'Python Kernel Debug Adapter'].includes(sessionType))
return Language.Python;
else if (['rdbg'].includes(sessionType))
return Language.Ruby;
else
return undefined;
}
private _getContext(): string | undefined {
const sessionType = this.session.type;
if (sessionType === 'rdbg')
return 'watch';
else
return undefined;
}
private _getDebugger(): string | undefined {
const sessionType = this.session.type;
if (sessionType !== undefined && this.language === Language.Cpp) {
if (sessionType === 'cppvsdbg')
return 'vsdbg';
else if (sessionType === 'cppdbg') {
if (this.session.configuration.MIMode === 'gdb')
return 'gdb';
else if (this.session.configuration.MIMode === 'lldb')
return 'lldb';
}
else if (sessionType === 'lldb')
return 'lldb';
else if (sessionType === 'cortex-debug')
return 'gdb';
}
return undefined;
}
public language: Language | undefined;
public debugger: string | undefined;
public context: string | undefined;
}
export enum Endianness { Little, Big };
export class MachineInfo {
constructor(
public pointerSize: number,
public endianness: Endianness)
{}
}
export class Debugger {
private _onStopped: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
readonly onStopped: vscode.Event<void> = this._onStopped.event;
private _onUnavailable: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
readonly onUnavailable: vscode.Event<void> = this._onUnavailable.event;
constructor(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.debug.registerDebugAdapterTrackerFactory("*", {
createDebugAdapterTracker: session => {
return {
onDidSendMessage: async (message: DebugProtocol.ProtocolMessage) => {
if (message.type === 'event') {
const event = message as DebugProtocol.Event;
if (event.event === 'stopped') {
const threadId = (message as DebugProtocol.StoppedEvent).body.threadId;
if (threadId !== undefined) {
const frameId = await this._frameId(session, threadId);
if (frameId != undefined) {
this.sessionInfo = new SessionInfo(session, threadId, frameId);
this._onStopped.fire();
} else {
this._setUnavailable();
}
} else {
this._setUnavailable();
}
} else if (event.event === 'continued'
|| event.event === 'exited'
|| event.event === 'terminated') {
this._setUnavailable();
}
} else if (message.type === 'response') {
const response = message as DebugProtocol.Response
if (response.command === 'continue'
|| response.command === 'next'
|| response.command === 'stepIn'
|| response.command === 'stepOut'
|| response.command === 'stepBack'
|| response.command === 'reverseContinue'
|| response.command === 'goto'
|| response.command === 'disconnect'
|| response.command === 'initialize'
|| response.command === 'launch') {
this._setUnavailable();
}
}
}
};
}
}));
}
private _setUnavailable() {
if (this.sessionInfo) {
this.sessionInfo = undefined;
this._onUnavailable.fire();
}
}
private async _customRequest(session: vscode.DebugSession, command: string, args: any) {
try {
return await session.customRequest(command, args);
} catch (error) {
return undefined;
}
}
private async _frameId(session: vscode.DebugSession, threadId: number) {
const stackArgs: DebugProtocol.StackTraceArguments = { threadId: threadId, startFrame: 0, levels: 1 };
const stackTrace = await this._customRequest(session, 'stackTrace', stackArgs);
if (stackTrace && stackTrace.stackFrames && stackTrace.stackFrames.length > 0) {
const frame = stackTrace.stackFrames[0] as DebugProtocol.StackFrame;
return frame.id;
} else {
return undefined;
}
}
private async _evaluate(session: vscode.DebugSession, expression: string, frameId: number, context: string | undefined) {
let exprArgs : DebugProtocol.EvaluateArguments = { expression: expression, frameId: frameId };
if (context !== undefined)
exprArgs.context = context;
return await this._customRequest(session, 'evaluate', exprArgs);
}
private async _variables(session: vscode.DebugSession, variablesReference:number, count: number | undefined) {
const exprArgs : DebugProtocol.VariablesArguments = { variablesReference: variablesReference, count: count };
return await this._customRequest(session, 'variables', exprArgs);
}
isStopped() : boolean {
return this.sessionInfo !== undefined;
}
language(): Language | undefined {
if (this.sessionInfo === undefined)
return undefined;
return this.sessionInfo.language;
}
workspaceFolder(): string | undefined {
if (this.sessionInfo === undefined)
return undefined;
return this.sessionInfo.session.workspaceFolder?.uri.fsPath;
}
async machineInfo() {
if (this.sessionInfo === undefined)
return undefined;
if (this.sessionInfo.language === Language.Cpp) {
const session = this.sessionInfo.session;
const frameId = this.sessionInfo.frameId;
const context = this.sessionInfo.context;
//const expr1 = await this._evaluate(session, '(unsigned int)((unsigned char)-1)', frameId, context);
const expr2 = await this._evaluate(session, 'sizeof(void*)', frameId, context);
if (expr2 === undefined || expr2.type === undefined)
return undefined;
let pointerSize: number = 0;
if (expr2.result === '4')
pointerSize = 4;
else if (expr2.result === '8')
pointerSize = 8;
else
return undefined;
const expr3 = await this._evaluate(session, 'sizeof(unsigned long)', frameId, context);
if (expr3 === undefined || expr3.type === undefined)
return undefined;
let endianness: Endianness | undefined = undefined;
let expression = '';
let expectedLittle = '';
let expectedBig = '';
if (expr3.result === '4') {
expression = '*(unsigned long*)"abc"';
expectedLittle = '6513249';
expectedBig = '1633837824';
} else if (expr3.result === '8') {
expression = '*(unsigned long*)"abcdefg"';
expectedLittle = '29104508263162465';
expectedBig = '7017280452245743360';
} else
return undefined;
const expr4 = await this._evaluate(session, expression, frameId, context);
if (expr4 === undefined || expr4.type === undefined)
return undefined;
if (expr4.result === expectedLittle)
endianness = Endianness.Little;
else if (expr4.result === expectedBig)
endianness = Endianness.Big;
else
return undefined;
return new MachineInfo(pointerSize, endianness);
}
return undefined;
}
private _isPythonError(type: string): boolean {
return (type === 'NameError' || type === 'AttributeError' || type === 'TypeError') && this.sessionInfo?.language === Language.Python;
}
private _isRubyError(type: string): boolean {
return (type === 'NameError' || type === 'NoMethodError') && this.sessionInfo?.language === Language.Ruby;
}
private _isJSObject(type: string): boolean {
return type === 'object' && this.sessionInfo?.language === Language.JavaScript;
}
rawType(type: string): string {
if (this.sessionInfo?.language === Language.Cpp) {
return util.cppRemoveTypeModifiers(type);
}
return type;
}
// type has to be raw type, without modifiers, refs and ptrs
async unrollTypeAlias(type: string): Promise<string> {
const debuggerName = this.sessionInfo?.debugger;
if (debuggerName === 'gdb') {
const evalResult = (await this.evaluate('-exec ptype /rmt ' + type))?.result;
let typeInfo: string = typeof evalResult === 'string' ? evalResult.trim() : '';
if (typeInfo.startsWith('type = ')) {
// console.log(typeInfo);
typeInfo = typeInfo.substring(7);
if (typeInfo.startsWith('class ')) {
typeInfo = typeInfo.substring(6);
}
else if (typeInfo.startsWith('struct ')) {
typeInfo = typeInfo.substring(7);
}
else if (typeInfo.startsWith('union ')) {
typeInfo = typeInfo.substring(6);
}
else if (typeInfo.startsWith('enum class ')) {
typeInfo = typeInfo.substring(11);
}
else if (typeInfo.startsWith('enum ')) {
typeInfo = typeInfo.substring(5);
}
return util.cppType(typeInfo);
}
}
else if (debuggerName === 'lldb') {
const typeInfo = (await this.evaluate('image lookup --type "' + type + '"', '_command'))?.result;
if (typeof typeInfo === 'string') {
const begin = typeInfo.indexOf('qualified = "');
if (begin >= 0) {
const end = typeInfo.indexOf('"', begin + 13);
if (end >= 0) {
return typeInfo.substring(begin + 13, end);
}
}
}
}
return type;
}
async getType(expression: string): Promise<string | undefined> {
let type = (await this.evaluate(expression))?.type;
if (this._isPythonError(type))
return undefined;
if (this._isRubyError(type))
return undefined;
if (this._isJSObject(type)) {
const expr = await this.evaluate('(' + expression + ').constructor.name');
if (expr?.type !== undefined && expr?.result !== undefined) { // type === 'string'?
type = expr.result.substr(1, expr.result.length - 2);
}
}
return type;
}
// NOTE: In LLDB members of cv types are also cv while in GDB they are not
// This function can be used to consistently get types without modifiers
async getRawType(expression: string): Promise<string | undefined> {
const type = await this.getType(expression);
return type !== undefined ? this.rawType(type) : undefined;
}
async getValue(expression: string): Promise<string | undefined> {
const result = await this.evaluate(expression);
if (this._isPythonError(result?.type))
return undefined;
if (this._isRubyError(result?.type))
return undefined;
return result?.type ? result.result : undefined;
}
async getValueAndType(expression: string): Promise<[string, string] | undefined> {
const expr = await this.evaluate(expression);
const value = expr?.result;
let type = expr?.type;
if (this._isPythonError(type))
return undefined;
if (this._isRubyError(type))
return undefined;
if (this._isJSObject(type)) {
const expr = await this.evaluate('(' + expression + ').constructor.name');
if (expr?.type !== undefined && expr?.result !== undefined) { // type === 'string'?
type = expr.result.substr(1, expr.result.length - 2);
}
}
return type !== undefined && value !== undefined ? [value, type] : undefined;
}
// NOTE: In LLDB members of cv types are also cv while in GDB they are not
// This function can be used to consistently get types without modifiers
async getValueAndRawType(expression: string): Promise<[string, string] | undefined> {
const result = await this.getValueAndType(expression);
if (result !== undefined) {
result[1] = this.rawType(result[1]);
}
return result;
}
async evaluate(expression: string, context: string | undefined = undefined) {
if (this.sessionInfo === undefined)
return undefined;
const session = this.sessionInfo.session;
const frameId = this.sessionInfo.frameId;
if (context === undefined)
context = this.sessionInfo.context;
return await this._evaluate(session, expression, frameId, context);
}
async variables(variablesReference:number, count: number | undefined = undefined) {
if (this.sessionInfo === undefined)
return undefined;
const session = this.sessionInfo.session;
return await this._variables(session, variablesReference, count);
}
async readMemory(memoryReference: string, offset: number, count: number) {
if (this.sessionInfo === undefined)
return undefined;
const session = this.sessionInfo.session;
let readMemoryArgs : DebugProtocol.ReadMemoryArguments = { memoryReference: memoryReference, offset: offset, count: count };
return await this._customRequest(session, 'readMemory', readMemoryArgs);
}
async readMemoryBuffer(memoryReference: string, offset: number, count: number) {
let mem = await this.readMemory(memoryReference, offset, count);
if (mem && mem.data)
return Buffer.from(mem.data, 'base64');
else
return undefined;
}
private sessionInfo: SessionInfo | undefined = undefined;
}