forked from freewayz/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteDebugServer.ts
More file actions
203 lines (182 loc) · 8.38 KB
/
Copy pathRemoteDebugServer.ts
File metadata and controls
203 lines (182 loc) · 8.38 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
"use strict";
import {DebugSession, OutputEvent} from "vscode-debugadapter";
import {IPythonProcess, IDebugServer, AttachRequestArguments} from "../Common/Contracts";
import * as net from "net";
import {BaseDebugServer} from "./BaseDebugServer";
import {SocketStream} from "../../common/comms/SocketStream";
const DebuggerProtocolVersion = 6; // must be kept in sync with PTVSDBG_VER in attach_server.py
const DebuggerSignature = "PTVSDBG";
const Accepted = "ACPT";
const Rejected = "RJCT";
const DebuggerSignatureBytes: Buffer = new Buffer(DebuggerSignature, "ascii");
const InfoCommandBytes: Buffer = new Buffer("INFO", "ascii");
const AttachCommandBytes: Buffer = new Buffer("ATCH", "ascii");
const ReplCommandBytes: Buffer = new Buffer("REPL", "ascii");
export class RemoteDebugServer extends BaseDebugServer {
private socket: net.Socket = null;
private args: AttachRequestArguments;
constructor(debugSession: DebugSession, pythonProcess: IPythonProcess, args: AttachRequestArguments) {
super(debugSession, pythonProcess);
this.args = args;
}
public Stop() {
if (this.socket === null) return;
try {
this.socket.end();
}
catch (ex) { }
this.socket = null;
}
private stream: SocketStream = null;
public Start(): Promise<IDebugServer> {
return new Promise<IDebugServer>((resolve, reject) => {
let that = this;
let connected = false;
let secretWrittenToDebugProgram = false;
let secretConfirmedByDebugProgram = false;
let infoBytesWritten = false;
let versionRead = false;
let commandBytesWritten = false;
let languageVersionRead = false;
let portNumber = this.args.port;
let debugCommandsAccepted = false;
let options = { port: portNumber};
if (typeof this.args.host === "string" && this.args.host.length > 0) {
(<any>options).host = this.args.host;
}
this.socket = net.connect(options, () => {
resolve(options);
});
this.socket.on("end", (ex) => {
// let msg = `Debugger client disconneced, ex`;
// that.debugSession.sendEvent(new OutputEvent(msg + "\n", "stderr"));
});
this.socket.on("data", (buffer: Buffer) => {
if (connected) {
that.pythonProcess.HandleIncomingData(buffer);
return;
}
if (that.stream === null) {
that.stream = new SocketStream(that.socket, buffer);
}
else {
if (!connected) {
if (that.stream.Length === 0) {
that.stream = new SocketStream(that.socket, buffer);
}
else {
that.stream.Append(buffer);
}
}
}
if (!secretWrittenToDebugProgram) {
that.stream.BeginTransaction();
let sig = that.stream.ReadAsciiString(DebuggerSignature.length);
if (that.stream.HasInsufficientDataForReading) {
that.stream.RollBackTransaction();
return;
}
if (sig !== DebuggerSignature) {
throw new Error("ConnErrorMessages.RemoteUnsupportedServer");
}
let ver = that.stream.ReadInt64();
if (that.stream.HasInsufficientDataForReading) {
that.stream.RollBackTransaction();
return;
}
// If we are talking the same protocol but different version, reply with signature + version before bailing out
// so that ptvsd has a chance to gracefully close the socket on its side.
that.stream.EndTransaction();
that.stream.Write(DebuggerSignatureBytes);
that.stream.WriteInt64(DebuggerProtocolVersion);
if (ver !== DebuggerProtocolVersion) {
throw new Error("ConnErrorMessages.RemoteUnsupportedServer");
}
that.stream.WriteString(that.args.secret || "");
secretWrittenToDebugProgram = true;
that.stream.EndTransaction();
let secretResp = that.stream.ReadAsciiString(Accepted.length);
if (that.stream.HasInsufficientDataForReading) {
that.stream.RollBackTransaction();
return;
}
if (secretResp !== Accepted) {
throw new Error("ConnErrorMessages.RemoteSecretMismatch");
}
secretConfirmedByDebugProgram = true;
that.stream.EndTransaction();
}
if (!secretConfirmedByDebugProgram) {
let secretResp = that.stream.ReadAsciiString(Accepted.length);
if (that.stream.HasInsufficientDataForReading) {
that.stream.RollBackTransaction();
return;
}
if (secretResp !== Accepted) {
throw new Error("ConnErrorMessages.RemoteSecretMismatch");
}
secretConfirmedByDebugProgram = true;
that.stream.EndTransaction();
}
if (!commandBytesWritten) {
that.stream.Write(AttachCommandBytes);
let debugOptions = "WaitOnAbnormalExit, WaitOnNormalExit, RedirectOutput";
that.stream.WriteString(debugOptions);
commandBytesWritten = true;
}
if (commandBytesWritten && !debugCommandsAccepted) {
let attachResp = that.stream.ReadAsciiString(Accepted.length);
if (that.stream.HasInsufficientDataForReading) {
that.stream.RollBackTransaction();
return;
}
if (attachResp !== Accepted) {
throw new Error("ConnErrorMessages.RemoteAttachRejected");
}
debugCommandsAccepted = true;
that.stream.EndTransaction();
}
if (debugCommandsAccepted && !languageVersionRead) {
that.stream.EndTransaction();
let pid = that.stream.ReadInt32();
let langMajor = that.stream.ReadInt32();
let langMinor = that.stream.ReadInt32();
let langMicro = that.stream.ReadInt32();
let langVer = ((langMajor << 8) | langMinor);
if (that.stream.HasInsufficientDataForReading) {
that.stream.RollBackTransaction();
return;
}
that.stream.EndTransaction();
languageVersionRead = true;
}
if (languageVersionRead) {
if (connected) {
that.pythonProcess.HandleIncomingData(buffer);
}
else {
that.pythonProcess.Connect(that.stream.Buffer, this.socket, true);
connected = true;
}
}
});
this.socket.on("close", d => {
let msg = `Debugger client closed, ${d}`;
that.emit("detach", d);
});
this.socket.on("timeout", d => {
let msg = `Debugger client timedout, ${d}`;
that.debugSession.sendEvent(new OutputEvent(msg + "\n", "stderr"));
});
this.socket.on("error", ex => {
if (connected){
return;
}
let exMessage = JSON.stringify(ex);
let msg = `There was an error in starting the debug server. Error = ${exMessage}`;
that.debugSession.sendEvent(new OutputEvent(msg + "\n", "stderr"));
reject(msg);
});
});
}
}