forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonDebug.ts
More file actions
322 lines (263 loc) · 8.98 KB
/
pythonDebug.ts
File metadata and controls
322 lines (263 loc) · 8.98 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
"use strict";
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import {
Logger, logger,
DebugSession, LoggingDebugSession,
InitializedEvent, TerminatedEvent, StoppedEvent, BreakpointEvent, OutputEvent,
Thread, StackFrame, Scope, Source, Handles, Breakpoint
} from 'vscode-debugadapter';
import { DebugProtocol } from 'vscode-debugprotocol';
import { readFileSync } from 'fs';
import { basename } from 'path';
import { PydevDebugger, Command } from './pydevDebugger';
import { parseString } from 'xml2js';
function logArgsToString(args: any[]): string {
return args.map(arg => {
return typeof arg === 'string' ?
arg :
JSON.stringify(arg);
}).join(' ');
}
export function verbose(...args: any[]) {
logger.verbose(logArgsToString(args));
}
export function log(...args: any[]) {
logger.log(logArgsToString(args));
}
export function logError(...args: any[]) {
logger.error(logArgsToString(args));
}
// This interface should always match the schema found in `package.json`.
export interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArguments {
program: string;
stopOnEntry?: boolean;
args?: string[];
showLog?: boolean;
cwd?: string;
env?: { [key: string]: string; };
mode?: string;
remotePath?: string;
port?: number;
host?: string;
buildFlags?: string;
init?: string;
trace?: boolean | 'verbose';
/** Optional path to .env file. */
envFile?: string;
backend?: string;
}
interface DebuggerState {
exited: boolean;
exitStatus: number;
breakPoint: DebugBreakpoint;
breakPointInfo: {};
breakpointId: number;
currentThread: DebugThread;
}
interface DebugBreakpoint {
addr: number;
continue: boolean;
file: string;
functionName?: string;
id: number;
line: number;
stacktrace: number;
variables?: DebugVariable[];
}
interface DebugThread {
file: string;
id: number;
line: number;
pc: number;
function?: DebugFunction;
};
interface DebugLocation {
pc: number;
file: string;
line: number;
function: DebugFunction;
}
interface DebugFunction {
name: string;
value: number;
type: number;
goType: number;
args: DebugVariable[];
locals: DebugVariable[];
}
interface DebugVariable {
name: string;
addr: number;
type: string;
realType: string;
value: string;
len: number;
cap: number;
children: DebugVariable[];
unreadable: string;
}
class PythonDebugSession extends LoggingDebugSession {
private _variableHandles: Handles<DebugVariable>;
private breakpoints: Map<string, DebugBreakpoint[]>;
private threads: Set<number>;
private debugState: DebuggerState;
private pydevd: PydevDebugger;
private launchArgs: LaunchRequestArguments;
/**
* Creates a new debug adapter that is used for one debug session.
* We configure the default implementation of a debug adapter here.
*/
public constructor() {
super("mock-debug.txt");
// this debugger uses zero-based lines and columns
this.setDebuggerLinesStartAt1(false);
this.setDebuggerColumnsStartAt1(false);
this._variableHandles = new Handles<DebugVariable>();
this.threads = new Set<number>();
this.debugState = null;
this.pydevd = null;
this.breakpoints = new Map<string, DebugBreakpoint[]>();
}
/**
* The 'initialize' request is the first request called by the frontend
* to interrogate the features the debug adapter provides.
*/
protected initializeRequest(response: DebugProtocol.InitializeResponse, args: DebugProtocol.InitializeRequestArguments): void {
verbose('InitializeRequest');
// since this debug adapter can accept configuration requests like 'setBreakpoint' at any time,
// we request them early by sending an 'initializeRequest' to the frontend.
// The frontend will end the configuration sequence by calling 'configurationDone' request.
this.sendEvent(new InitializedEvent());
this.debugState = {
exited: false,
exitStatus: null,
breakPoint: null,
breakPointInfo: {},
breakpointId: 0,
currentThread: null
};
response.body = response.body || {};
response.body.supportsConfigurationDoneRequest = true; // This debug adapter implements the configurationDoneRequest.
response.body.supportsEvaluateForHovers = true; // make VS Code to use 'evaluate' when hovering over source
response.body.supportsStepBack = false; // Pydev does not support 'step back'
this.sendResponse(response);
verbose('InitializeResponse');
}
/**
* MUST create a new pydevd instance.
*/
protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void {
this.launchArgs = args;
let port = args.port || 0; // Autoset the port number by default.
let host = args.host || '127.0.0.1';
this.pydevd = new PydevDebugger(port, host, args.program, args);
this.pydevd.on('call', (command: Command, sequence: number, args) => {
this.handleEvent(command, sequence, args, response);
});
this.pydevd.server.then(() => {
this.pydevd.call(Command.CMD_RUN);
})
// make sure to 'Stop' the buffered logging if 'trace' is not set
// logger.setup(args.trace ? Logger.LogLevel.Verbose : Logger.LogLevel.Stop, false);
logger.setup(Logger.LogLevel.Verbose, false);
}
private handleEvent(command: Command, sequence: number, args: [any], response: DebugProtocol.LaunchResponse) {
// Handle aribitrary commands
let handlers: Map<Command, (args: [any], response: DebugProtocol.LaunchResponse) => void> = new Map([
[Command.CMD_THREAD_SUSPEND, this.handleThreadSuspend],
[Command.CMD_ERROR, this.handleDebuggerError]
]);
if (handlers.has(command)) {
handlers[command](args, response);
}
}
private handleThreadSuspend(args: [string],response: DebugProtocol.LaunchResponse) {
parseString(args[0], (err, result) => {
this.sendEvent(new StoppedEvent('The thread has stopped', 0));
});
}
private handleDebuggerError(args: [string], response: DebugProtocol.LaunchResponse) {
this.sendErrorResponse(response, 3000);
}
protected setBreakPointsRequest(response: DebugProtocol.SetBreakpointsResponse, args: DebugProtocol.SetBreakpointsArguments): void {
verbose('SetBreakPointsRequest');
if (!this.breakpoints.get(args.source.path)) {
this.breakpoints.set(args.source.path, []);
}
// breakpoint_id, 'python-line', self.get_main_filename(), line, func)
let file = args.source.path;
Promise.all(this.breakpoints.get(file).map(existingBP => {
verbose('Clearing: ' + existingBP.id);
this.pydevd.call(Command.CMD_REMOVE_BREAK, ['python-line', args.source.path, existingBP.id]);
})).then(() => {
verbose('All cleared')
return Promise.all(args.lines.map(line => {
verbose('Creating on: ' + file + ':' + line);
this.debugState.breakpointId++;
this.pydevd.call(Command.CMD_SET_BREAK, [this.debugState.breakpointId, 'python-line', file, line, 'None', 'None', 'None']);
}));
}).then(() => {
let breakpoints = args.lines.map(line => {
return { verified: false, line: line };
})
response.body = { breakpoints };
this.sendResponse(response);
verbose('SetBreakPointsResponse');
});
}
protected threadsRequest(response: DebugProtocol.ThreadsResponse): void {
this.pydevd.server.then(() => {
this.pydevd.call(Command.CMD_LIST_THREADS).then(function ([command, sequence, args]: [Command, number, Array<string>]) {
//
//
});
});
this.sendResponse(response);
}
/**
* Returns a fake 'stacktrace' where every 'stackframe' is a word from the current line.
*/
protected stackTraceRequest(response: DebugProtocol.StackTraceResponse, args: DebugProtocol.StackTraceArguments): void {
this.sendResponse(response);
}
protected scopesRequest(response: DebugProtocol.ScopesResponse, args: DebugProtocol.ScopesArguments): void {
this.sendResponse(response);
}
protected variablesRequest(response: DebugProtocol.VariablesResponse, args: DebugProtocol.VariablesArguments): void {
this.sendResponse(response);
}
protected continueRequest(response: DebugProtocol.ContinueResponse, args: DebugProtocol.ContinueArguments): void {
this.pydevd.server.then(() => {
this.pydevd.call(Command.CMD_RUN);
});
this.sendResponse(response);
// no more lines: run to end
this.sendEvent(new TerminatedEvent());
}
protected nextRequest(response: DebugProtocol.NextResponse, args: DebugProtocol.NextArguments): void {
this.pydevd.server.then(() => {
this.pydevd.call(Command.CMD_STEP_OVER);
});
this.sendResponse(response);
// no more lines: run to end
this.sendEvent(new TerminatedEvent());
}
protected stepInRequest(response: DebugProtocol.StepInResponse): void {
this.pydevd.server.then(() => {
this.pydevd.call(Command.CMD_STEP_INTO);
});
this.sendResponse(response);
}
protected stepOutRequest(response: DebugProtocol.StepOutResponse): void {
this.pydevd.server.then(() => {
this.pydevd.call(Command.CMD_STEP_RETURN);
});
this.sendResponse(response);
}
protected evaluateRequest(response: DebugProtocol.EvaluateResponse, args: DebugProtocol.EvaluateArguments): void {
this.sendResponse(response);
}
}
DebugSession.run(PythonDebugSession);