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 96
/
Copy pathnodeProcessTree.ts
136 lines (111 loc) · 3.72 KB
/
nodeProcessTree.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
/*---------------------------------------------------------------------------------------------
* 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 { getProcessTree, ProcessTreeNode } from './processTree';
import * as vscode from 'vscode';
const DEBUG_PORT_PATTERN = /--(inspect|debug)-port=(\d+)/;
const DEBUG_FLAGS_PATTERN = /--(inspect|debug)(-brk)?(=(\d+))?/;
const pids = new Set<number>();
const POLL_INTERVAL = 1000;
/**
* Poll for all subprocesses of given root process.
*/
export function pollProcesses(rootPid: number, inTerminal: boolean, cb: (pid: number, cmd: string, args: string) => void) : vscode.Disposable {
let stopped = false;
function poll() {
//const start = Date.now();
findChildProcesses(rootPid, inTerminal, cb).then(_ => {
//console.log(`duration: ${Date.now() - start}`);
setTimeout(_ => {
if (!stopped) {
poll();
}
}, POLL_INTERVAL);
});
}
poll();
return new vscode.Disposable(() => stopped = true);
}
export function attachToProcess(folder: vscode.WorkspaceFolder | undefined, name: string, pid: number, args: string, baseConfig?: vscode.DebugConfiguration) {
if (pids.has(pid)) {
return;
}
pids.add(pid);
const config: vscode.DebugConfiguration = {
type: 'node',
request: 'attach',
name: name,
stopOnEntry: false
};
if (baseConfig) {
// 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(args);
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(args);
if (matches && matches.length === 3) {
// override port
config.port = parseInt(matches[2]);
}
//log(`attach: ${config.protocol} ${config.port}`);
vscode.debug.startDebugging(folder, config);
}
function findChildProcesses(rootPid: number, inTerminal: boolean, cb: (pid: number, cmd: string, args: string) => void): Promise<void> {
function walker(node: ProcessTreeNode, terminal: boolean) {
const matches = DEBUG_PORT_PATTERN.exec(node.args);
const matches2 = DEBUG_FLAGS_PATTERN.exec(node.args);
if (node.args.indexOf('--type=terminal') >= 0) {
terminal = true;
}
if (terminal && ((matches && matches.length >= 3) || (matches2 && matches2.length >= 5))) {
cb(node.pid, node.command, node.args);
}
for (const child of node.children || []) {
walker(child, terminal);
}
}
return getProcessTree(rootPid).then(tree => {
if (tree) {
for (const child of tree.children || []) {
walker(child, !inTerminal);
}
}
});
}