This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
childProcesses.ts
211 lines (177 loc) · 5.79 KB
/
childProcesses.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as nls from 'vscode-nls';
import { exec } from 'child_process';
import * as vscode from 'vscode';
const localize = nls.loadMessageBundle();
const POLL_INTERVAL = 1000;
const DEBUG_PORT_PATTERN = /\s--(inspect|debug)-port=(\d+)/;
const DEBUG_FLAGS_PATTERN = /\s--(inspect|debug)(-brk)?(=(\d+))?/;
class Cluster {
config: vscode.DebugConfiguration;
session: vscode.DebugSession;
pids: Set<number>;
intervalId: NodeJS.Timer;
constructor(config: vscode.DebugConfiguration) {
this.config = config;
this.pids = new Set<number>();
}
startWatching(session: vscode.DebugSession) {
this.session = session;
setTimeout(_ => {
// get the process ID from the debuggee
this.session.customRequest('evaluate', { expression: 'process.pid' }).then(reply => {
const rootPid = parseInt(reply.result);
this.attachChildProcesses(rootPid);
}, e => {
// 'evaluate' error -> use the fall back strategy
this.attachChildProcesses(NaN);
});
}, this.session.type === 'node2' ? 500 : 100);
}
stopWatching() {
if (this.intervalId) {
clearInterval(this.intervalId);
}
}
private attachChildProcesses(rootPid: number) {
this.pollChildProcesses(rootPid, (pid, cmd) => {
if (!this.pids.has(pid)) {
this.pids.add(pid);
attachChildProcess(pid, cmd, this.config);
}
});
}
private pollChildProcesses(rootPid: number, cb: (pid, cmd) => void) {
findChildProcesses(rootPid, cb);
this.intervalId = setInterval(() => {
findChildProcesses(rootPid, cb);
}, POLL_INTERVAL);
}
}
const clusters = new Map<string,Cluster>();
export function prepareAutoAttachChildProcesses(config: vscode.DebugConfiguration) {
clusters.set(config.name, new Cluster(config));
}
export function startSession(session: vscode.DebugSession) {
const cluster = clusters.get(session.name);
if (cluster) {
cluster.startWatching(session);
}
}
export function stopSession(session: vscode.DebugSession) {
const cluster = clusters.get(session.name);
if (cluster) {
cluster.stopWatching();
clusters.delete(session.name);
}
}
function attachChildProcess(pid: number, cmd: string, baseConfig: vscode.DebugConfiguration) {
const config: vscode.DebugConfiguration = {
type: 'node',
request: 'attach',
name: localize('childProcessWithPid', "Child Process {0}", pid),
stopOnEntry: false
};
// selectively copy attributes
if (baseConfig.timeout) {
config.timeout = baseConfig.timeout;
}
if (baseConfig.sourceMaps) {
config.sourceMaps = baseConfig.sourceMaps;
}
if (baseConfig.outFiles) {
config.outFiles = baseConfig.outFiles;
}
if (baseConfig.sourceMapPathOverrides) {
config.sourceMapPathOverrides = baseConfig.sourceMapPathOverrides;
}
if (baseConfig.smartStep) {
config.smartStep = baseConfig.smartStep;
}
if (baseConfig.skipFiles) {
config.skipFiles = baseConfig.skipFiles;
}
if (baseConfig.showAsyncStacks) {
config.sourceMaps = baseConfig.showAsyncStacks;
}
if (baseConfig.trace) {
config.trace = baseConfig.trace;
}
// match --debug, --debug=1234, --debug-brk, debug-brk=1234, --inspect, --inspect=1234, --inspect-brk, --inspect-brk=1234
let matches = DEBUG_FLAGS_PATTERN.exec(cmd);
if (matches && matches.length >= 2) {
// attach via port
if (matches.length === 5 && matches[4]) {
config.port = parseInt(matches[4]);
}
config.protocol= matches[1] === 'debug' ? 'legacy' : 'inspector';
} else {
// no port -> try to attach via pid (send SIGUSR1)
config.processId = String(pid);
}
// a debug-port=1234 or --inspect-port=1234 overrides the port
matches = DEBUG_PORT_PATTERN.exec(cmd);
if (matches && matches.length === 3) {
// override port
config.port = parseInt(matches[2]);
}
//log(`attach: ${config.protocol} ${config.port}`);
vscode.debug.startDebugging(undefined, config);
}
function findChildProcesses(rootPid: number, cb: (pid: number, cmd: string) => void) {
const set = new Set<number>();
if (!isNaN(rootPid) && rootPid > 0) {
set.add(rootPid);
}
function oneProcess(pid: number, ppid: number, cmd: string) {
if (set.size === 0) {
// try to find the root process
const matches = DEBUG_PORT_PATTERN.exec(cmd);
if (matches && matches.length >= 3) {
// since this is a child we add the parent id as the root id
set.add(ppid);
}
}
if (set.has(ppid)) {
set.add(pid);
const matches = DEBUG_PORT_PATTERN.exec(cmd);
const matches2 = DEBUG_FLAGS_PATTERN.exec(cmd);
if ((matches && matches.length >= 3) || (matches2 && matches2.length >= 5)) {
cb(pid, cmd);
}
}
}
if (process.platform === 'win32') {
const CMD = 'wmic process get CommandLine,ParentProcessId,ProcessId';
const CMD_PAT = /^(.+)\s+([0-9]+)\s+([0-9]+)$/;
exec(CMD, { maxBuffer: 1000 * 1024 }, (err, stdout, stderr) => {
if (!err && !stderr) {
const lines = stdout.split('\r\n');
for (let line of lines) {
let matches = CMD_PAT.exec(line.trim());
if (matches && matches.length === 4) {
oneProcess(parseInt(matches[3]), parseInt(matches[2]), matches[1].trim());
}
}
}
});
} else { // OS X & Linux
const CMD = 'ps -ax -o pid=,ppid=,command=';
const CMD_PAT = /^\s*([0-9]+)\s+([0-9]+)\s+(.+)$/;
exec(CMD, { maxBuffer: 1000 * 1024 }, (err, stdout, stderr) => {
if (!err && !stderr) {
const lines = stdout.toString().split('\n');
for (const line of lines) {
let matches = CMD_PAT.exec(line.trim());
if (matches && matches.length === 4) {
oneProcess(parseInt(matches[1]), parseInt(matches[2]), matches[3]);
}
}
}
});
}
}