-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
175 lines (152 loc) · 5.63 KB
/
extension.js
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
const vscode = require("vscode");
const net = require("net");
let disposables = [];
const RESP_OK = new Uint8Array([0]);
const RESP_INVALID_REQUEST = new Uint8Array([1]);
const RESP_INCOMPLETE_REQUEST = new Uint8Array([2]);
const RESP_ATTACH_FAILURE = new Uint8Array([3]);
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log("debug-trigger is now active!");
let armCommand = vscode.commands.registerCommand("debug-trigger.arm", arm);
context.subscriptions.push(armCommand);
let disarmCommand = vscode.commands.registerCommand("debug-trigger.disarm", disarm);
context.subscriptions.push(disarmCommand);
vscode.debug.onDidStartDebugSession(session => {
console.log(session)
}, this, disposables);
vscode.debug.onDidTerminateDebugSession(session => {
var id = [session.configuration.type];
if (id[0] == "cppdbg")
id = id.append(Number(session.configuration.processId));
else if (id[0] == "python")
id = id.concat([session.configuration.connect.host, session.configuration.connect.port]);
else
return;
debug_sessions.delete(id);
}, this, disposables);
}
function deactivate() {
disposables.forEach(d => {
d.dispose();
});
}
module.exports = {
activate,
deactivate
}
let server = null;
let debug_sessions = new Set;
function arm() {
if (!server) {
const listento = vscode.workspace.getConfiguration("debug-trigger").listento;
server = net.createServer(
{"allowHalfOpen": true},
(c) => {
// "connection" listener.
console.log("client connected");
c.setEncoding("utf8");
c.on("data",
(data) => {
c.input_buffer = (c.input_buffer || "") + data;
let [conf_name, param_str, param_num, r] = c.input_buffer.split("\r\n")
if (r != null) {
if (!isFinite(param_num)) {
vscode.window.showErrorMessage(`Invalid request: ${c.input_buffer}`);
c.write(RESP_INVALID_REQUEST, () => { c.end(); });
c.input_buffer = null;
} else {
console.log(`Request:${c.input_buffer}`);
c.input_buffer = null;
handleRequest(c, conf_name, param_str, Number(param_num));
}
}
}
);
c.on("end", () => {
if (c.input_buffer) {
vscode.window.showErrorMessage(`Incomplete request: ${c.input_buffer}`);
c.write(RESP_INCOMPLETE_REQUEST, () => { c.end(); });
c.input_buffer = null;
}
});
}
);
server.on('error', e => {
vscode.window.showErrorMessage(`Error running server! Error: ${e}`);
server.close();
server = null;
});
let [host, port] = listento.split(" ");
try {
if (port)
server.listen(Number(port), host, () => {
vscode.window.showInformationMessage(`Debug Trigger armed! Server started on ${host}:${port}`);
});
else
server.listen(listento, () => {
vscode.window.showInformationMessage(`Debug Trigger armed! Server started on ${listento}`);
});
}
catch(e){
vscode.window.showErrorMessage(`Cannot start server due to invalid configuration! ${e}`);
server = null;
}
}
else {
vscode.window.showInformationMessage(`Already armed!`);
}
}
function disarm() {
if (server) {
server.close();
server = null;
vscode.window.showInformationMessage(`Disarmed!`);
}
else {
vscode.window.showInformationMessage(`Already disarmed!`);
}
}
function handleRequest(c, conf_name, param1, param2) {
const configurations = vscode.workspace.getConfiguration("debug-trigger").configurations;
var config = configurations[conf_name];
if (typeof(config) != "object")
{
vscode.window.showWarningMessage(`Debug configuration with name "${conf_name}" not found!`);
return;
}
var id;
config = {...config};
if (config.type == "cppdbg") {
if (param1)
config.program = param1;
config.processId = param2;
id = [config.type, param2];
}
else if (config.type == "python") {
config.connect = {...config.connect};
config.connect.host = param1;
config.connect.port = param2;
id = [config.type, param1, param2];
} else {
vscode.window.showWarningMessage(`Unsupported configuration type "${conf_name}"!`);
return;
}
if (debug_sessions.has(id)) {
c.write(new Uint8Array([0]), () => { c.end(); });
return;
}
vscode.window.showInformationMessage(`Attaching ${id}...`);
vscode.debug.startDebugging(undefined, config).then(ok => {
if (ok) {
vscode.window.showInformationMessage(`Debugging of ${id} started!`);
debug_sessions.add(id);
c.write(RESP_OK, () => { c.end(); }); // Success!
} else {
vscode.window.showWarningMessage(`Failed to attach ${id}!`);
c.write(RESP_ATTACH_FAILURE, () => { c.end(); });
}
});
}