Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@
"@vscode/debugprotocol": "^1.65.0",
"@vscode/extension-telemetry": "^0.8.4",
"@vscode/python-extension": "^1.0.6",
"@vscode/windows-process-tree": "^0.7.0",
"fs-extra": "^11.2.0",
"iconv-lite": "^0.6.3",
"jsonc-parser": "^3.0.0",
Expand Down
58 changes: 58 additions & 0 deletions src/extension/debugger/attachQuickPick/powerShellProcessParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// due to wmic has been deprecated create this file to replace wmicProcessParser.ts

'use strict';

import { IAttachItem, ProcessListCommand } from './types';

export namespace PowerShellProcessParser {
export const powerShellCommand: ProcessListCommand = {
command: 'powershell',
args: [
'-Command',
'$processes = if (Get-Command Get-CimInstance -ErrorAction SilentlyContinue) { Get-CimInstance Win32_Process } else { Get-WmiObject Win32_Process }; \
$processes | % { @{ name = $_.Name; commandLine = $_.CommandLine; processId = $_.ProcessId } } | ConvertTo-Json',
], // Get-WmiObject For the legacy compatibility
};

//for unit test with Get-WmiObject
export const powerShellWithoutCimCommand: ProcessListCommand = {
command: 'powershell',
args: [
'-Command',
'$processes = if (Get-Command NotExistCommand-That-Will-Never-Exist -ErrorAction SilentlyContinue) { Get-CimInstance Win32_Process } else { Get-WmiObject Win32_Process }; \
$processes | % { @{ name = $_.Name; commandLine = $_.CommandLine; processId = $_.ProcessId } } | ConvertTo-Json',
],
};

export function parseProcesses(processes: string): IAttachItem[] {
const processesArray = JSON.parse(processes);
const processEntries: IAttachItem[] = [];
for (const process of processesArray) {
if (!process.processId) {
continue;
}
const entry: IAttachItem = {
label: process.name || '',
processName: process.name || '',
description: String(process.processId),
id: String(process.processId),
detail: '',
commandLine: '',
};
if (process.commandLine) {
const dosDevicePrefix = '\\??\\'; // DOS device prefix, see https://reverseengineering.stackexchange.com/a/15178
let commandLine = process.commandLine;
if (commandLine.startsWith(dosDevicePrefix)) {
commandLine = commandLine.slice(dosDevicePrefix.length);
}
entry.detail = commandLine;
entry.commandLine = commandLine;
}
processEntries.push(entry);
}
return processEntries;
}
}
88 changes: 70 additions & 18 deletions src/extension/debugger/attachQuickPick/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import { l10n } from 'vscode';
import { getOSType, OSType } from '../../common/platform';
import { PsProcessParser } from './psProcessParser';
import { IAttachItem, IAttachProcessProvider, ProcessListCommand } from './types';
import { WmicProcessParser } from './wmicProcessParser';
import { PowerShellProcessParser } from './powerShellProcessParser';
import { getEnvironmentVariables } from '../../common/python';
import { plainExec } from '../../common/process/rawProcessApis';
import { logProcess } from '../../common/process/logger';
import { WmicProcessParser } from './wmicProcessParser';
import { promisify } from 'util';
import * as wpc from '@vscode/windows-process-tree';
import { ProcessDataFlag } from '@vscode/windows-process-tree';

export class AttachProcessProvider implements IAttachProcessProvider {
constructor() {}

public getAttachItems(): Promise<IAttachItem[]> {
return this._getInternalProcessEntries().then((processEntries) => {
public getAttachItems(specCommand?: ProcessListCommand): Promise<IAttachItem[]> {
return this._getInternalProcessEntries(specCommand).then((processEntries) => {
processEntries.sort(
(
{ processName: aprocessName, commandLine: aCommandLine },
Expand Down Expand Up @@ -57,25 +61,73 @@ export class AttachProcessProvider implements IAttachProcessProvider {
});
}

public async _getInternalProcessEntries(): Promise<IAttachItem[]> {
let processCmd: ProcessListCommand;
const osType = getOSType();
if (osType === OSType.OSX) {
processCmd = PsProcessParser.psDarwinCommand;
} else if (osType === OSType.Linux) {
processCmd = PsProcessParser.psLinuxCommand;
} else if (osType === OSType.Windows) {
processCmd = WmicProcessParser.wmicCommand;
} else {
throw new Error(l10n.t("Operating system '{0}' not supported.", osType));
/**
* Get processes via wmic (fallback)
*/
private async _getProcessesViaWmic(): Promise<IAttachItem[]> {
const customEnvVars = await getEnvironmentVariables();
const output = await plainExec(
WmicProcessParser.wmicCommand.command,
WmicProcessParser.wmicCommand.args,
{ throwOnStdErr: true },
customEnvVars,
);
logProcess(WmicProcessParser.wmicCommand.command, WmicProcessParser.wmicCommand.args, { throwOnStdErr: true });
return WmicProcessParser.parseProcesses(output.stdout);
}

/**
* Get processes via Ps parser (Linux/macOS)
*/
private async _getProcessesViaPsParser(cmd: ProcessListCommand): Promise<IAttachItem[]> {
const customEnvVars = await getEnvironmentVariables();
const output = await plainExec(cmd.command, cmd.args, { throwOnStdErr: true }, customEnvVars);
logProcess(cmd.command, cmd.args, { throwOnStdErr: true });
return PsProcessParser.parseProcesses(output.stdout);
}

public async _getInternalProcessEntries(specCommand?: ProcessListCommand): Promise<IAttachItem[]> {
if (specCommand === undefined) {
const osType = getOSType();
if (osType === OSType.OSX) {
return this._getProcessesViaPsParser(PsProcessParser.psDarwinCommand);
} else if (osType === OSType.Linux) {
return this._getProcessesViaPsParser(PsProcessParser.psLinuxCommand);
} else if (osType === OSType.Windows) {
try {
const getAllProcesses = promisify(wpc.getAllProcesses) as (flags?: ProcessDataFlag) => Promise<wpc.IProcessInfo[]>;
const processList = await getAllProcesses(ProcessDataFlag.CommandLine);

return processList.map((p) => ({
label: p.name,
description: String(p.pid),
detail: p.commandLine || '',
id: String(p.pid),
processName: p.name,
commandLine: p.commandLine || '',
}));
} catch (error) {
console.error('Failed to get processes via windows-process-tree:', error);
// 降级到 wmic
return this._getProcessesViaWmic();
}
} else {
throw new Error(l10n.t("Operating system '{0}' not supported.", osType));
}
}

const processCmd = specCommand;
const customEnvVars = await getEnvironmentVariables();
const output = await plainExec(processCmd.command, processCmd.args, { throwOnStdErr: true }, customEnvVars);
logProcess(processCmd.command, processCmd.args, { throwOnStdErr: true });

return osType === OSType.Windows
? WmicProcessParser.parseProcesses(output.stdout)
: PsProcessParser.parseProcesses(output.stdout);
if (processCmd === WmicProcessParser.wmicCommand) {
return WmicProcessParser.parseProcesses(output.stdout);
} else if (
processCmd === PowerShellProcessParser.powerShellCommand ||
processCmd === PowerShellProcessParser.powerShellWithoutCimCommand
) {
return PowerShellProcessParser.parseProcesses(output.stdout);
}
return PsProcessParser.parseProcesses(output.stdout);
}
}
Loading