Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
weinand committed Jul 2, 2018
1 parent 3b946f1 commit 3bb7e13
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 76 deletions.
50 changes: 8 additions & 42 deletions src/node/extension/nodeProcessTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

import * as vscode from 'vscode';
import { getProcessTree, ProcessTreeNode } from './processTree';
import { INSPECTOR_PORT_DEFAULT, LEGACY_PORT_DEFAULT } from './protocolDetection';
import { analyseArguments } from './protocolDetection';

const DEBUG_FLAGS_PATTERN = /--(inspect|debug)(-brk)?(=(\d+))?[^-]/;
const DEBUG_PORT_PATTERN = /--(inspect|debug)-port=(\d+)/;

const pids = new Set<number>();

Expand Down Expand Up @@ -82,62 +80,30 @@ export function attachToProcess(folder: vscode.WorkspaceFolder | undefined, name
}
}

let port = -1;
let protocol = '';
let usePid = true;

// 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]) {
port = parseInt(matches[4]);
}
protocol= matches[1] === 'debug' ? 'legacy' : 'inspector';
usePid = false;
}

// a debug-port=1234 or --inspect-port=1234 overrides the port
matches = DEBUG_PORT_PATTERN.exec(args);
if (matches && matches.length === 3) {
// override port
port = parseInt(matches[2]);
protocol = matches[1] === 'debug' ? 'legacy' : 'inspector';
}

if (usePid) {
let { usePort, protocol, port } = analyseArguments(args);
if (usePort) {
config.processId = `${protocol}${port}`;
} else {
if (protocol && port > 0) {
config.processId = `${pid}${protocol}${port}`;
} else {
// no port given
//if (NODE.test(executable_name)) {
config.processId = pid.toString();
//}
}
} else {
if (port < 0) {
port = protocol === 'inspector' ? INSPECTOR_PORT_DEFAULT : LEGACY_PORT_DEFAULT;
config.processId = pid.toString();
}
config.processId = `${protocol}${port}`;
}

//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, renderer: number) {

const matches = DEBUG_PORT_PATTERN.exec(node.args);
const matches2 = DEBUG_FLAGS_PATTERN.exec(node.args);

if (node.args.indexOf('--type=terminal') >= 0 && (renderer === 0 || node.ppid === renderer)) {
terminal = true;
}

if (terminal && ((matches && matches.length >= 3) || (matches2 && matches2.length >= 5))) {
let { protocol } = analyseArguments(node.args);
if (terminal && protocol) {
cb(node.pid, node.command, node.args);
}

Expand Down
49 changes: 15 additions & 34 deletions src/node/extension/processPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { basename } from 'path';
import { getProcesses } from './processTree';
import { execSync } from 'child_process';
import { detectProtocolForPid, INSPECTOR_PORT_DEFAULT, LEGACY_PORT_DEFAULT } from './protocolDetection';
import { analyseArguments } from './protocolDetection';

const localize = nls.loadMessageBundle();

Expand Down Expand Up @@ -132,9 +133,6 @@ function listProcesses(ports: boolean): Promise<ProcessItem[]> {

const items: ProcessItem[] = [];

const DEBUG_FLAGS_PATTERN = /--(inspect|debug)(-brk)?(=(\d+))?[^-]/;
const DEBUG_PORT_PATTERN = /--(inspect|debug)-port=(\d+)/;

const NODE = new RegExp('^(?:node|iojs)$', 'i');

let seq = 0; // default sort key
Expand All @@ -149,34 +147,27 @@ function listProcesses(ports: boolean): Promise<ProcessItem[]> {
const executable_name = basename(command, '.exe');

let port = -1;
let protocol = '';
let usePid = true;
let protocol: string | undefined = '';
let usePort = true;

if (ports) {
// 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]) {
port = parseInt(matches[4]);
}
protocol = matches[1] === 'debug' ? 'legacy' : 'inspector';
usePid = false;
}

// a debug-port=1234 or --inspect-port=1234 overrides the port
matches = DEBUG_PORT_PATTERN.exec(args);
if (matches && matches.length === 3) {
// override port
port = parseInt(matches[2]);
protocol = matches[1] === 'debug' ? 'legacy' : 'inspector';
}
const x = analyseArguments(args);
usePort = x.usePort;
protocol = x.protocol;
port = x.port;
}

let description = '';
let pidOrPort = '';

if (usePid) {
if (usePort) {
if (protocol === 'inspector') {
description = localize('process.id.port', "process id: {0}, debug port: {1}", pid, port);
} else {
description = localize('process.id.port.legacy', "process id: {0}, debug port: {1} (legacy protocol)", pid, port);
}
pidOrPort = `${protocol}${port}`;
} else {
if (protocol && port > 0) {
description = localize('process.id.port.signal', "process id: {0}, debug port: {1} ({2})", pid, port, 'SIGUSR1');
pidOrPort = `${pid}${protocol}${port}`;
Expand All @@ -187,16 +178,6 @@ function listProcesses(ports: boolean): Promise<ProcessItem[]> {
pidOrPort = pid.toString();
}
}
} else {
if (port < 0) {
port = protocol === 'inspector' ? INSPECTOR_PORT_DEFAULT : LEGACY_PORT_DEFAULT;
}
if (protocol === 'inspector') {
description = localize('process.id.port', "process id: {0}, debug port: {1}", pid, port);
} else {
description = localize('process.id.port.legacy', "process id: {0}, debug port: {1} (legacy protocol)", pid, port);
}
pidOrPort = `${protocol}${port}`;
}

if (description && pidOrPort) {
Expand Down
49 changes: 49 additions & 0 deletions src/node/extension/protocolDetection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,52 @@ function getPidListeningOnPortUnix(port: number): Promise<number> {
});
});
}

export class DebugArguments {
usePort: boolean; // if true debug by using the debug port
protocol?: 'legacy' | 'inspector'; //
address?: string;
port: number;
}

/*
* analyse the given command line arguments and extract debug port and protocol from it.
*/
export function analyseArguments(args: string): DebugArguments {

const DEBUG_FLAGS_PATTERN = /--(inspect|debug)(-brk)?(=((\[[0-9a-fA-F:]*\]|[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[a-zA-Z0-9\.]*):)?(\d+))?/;
const DEBUG_PORT_PATTERN = /--(inspect|debug)-port=(\d+)/;

const result: DebugArguments = {
usePort: false,
port: -1
};

// 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
result.usePort = true;
if (matches.length >= 6 && matches[5]) {
result.address = matches[5];
}
if (matches.length >= 7 && matches[6]) {
result.port = parseInt(matches[6]);
}
result.protocol = matches[1] === 'debug' ? 'legacy' : 'inspector';
}

// a debug-port=1234 or --inspect-port=1234 overrides the port
matches = DEBUG_PORT_PATTERN.exec(args);
if (matches && matches.length === 3) {
// override port
result.port = parseInt(matches[2]);
result.protocol = matches[1] === 'debug' ? 'legacy' : 'inspector';
}

if (result.port < 0) {
result.port = result.protocol === 'inspector' ? INSPECTOR_PORT_DEFAULT : LEGACY_PORT_DEFAULT;
}

return result;
}

0 comments on commit 3bb7e13

Please sign in to comment.