forked from microsoft/vscode-node-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeProcessTree.ts
More file actions
140 lines (115 loc) · 3.56 KB
/
Copy pathnodeProcessTree.ts
File metadata and controls
140 lines (115 loc) · 3.56 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
/*---------------------------------------------------------------------------------------------
* 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 vscode from 'vscode';
import { getProcessTree, ProcessTreeNode } from './processTree';
import { analyseArguments } from './protocolDetection';
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;
}
}
let { usePort, protocol, port } = analyseArguments(args);
if (usePort) {
config.processId = `${protocol}${port}`;
} else {
if (protocol && port > 0) {
config.processId = `${pid}${protocol}${port}`;
} else {
config.processId = pid.toString();
}
}
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, renderer: number) {
if (node.args.indexOf('--type=terminal') >= 0 && (renderer === 0 || node.ppid === renderer)) {
terminal = true;
}
let { protocol } = analyseArguments(node.args);
if (terminal && protocol) {
cb(node.pid, node.command, node.args);
}
for (const child of node.children || []) {
walker(child, terminal, renderer);
}
}
function finder(node: ProcessTreeNode, pid: number): ProcessTreeNode | undefined {
if (node.pid === pid) {
return node;
}
for (const child of node.children || []) {
const p = finder(child, pid);
if (p) {
return p;
}
}
return undefined;
}
return getProcessTree(rootPid).then(tree => {
if (tree) {
// find the pid of the renderer process
const extensionHost = finder(tree, process.pid);
let rendererPid = extensionHost ? extensionHost.ppid : 0;
for (const child of tree.children || []) {
walker(child, !inTerminal, rendererPid);
}
}
});
}