Skip to content
Open
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
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;
}
}
58 changes: 41 additions & 17 deletions src/extension/debugger/attachQuickPick/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ 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';

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 +58,48 @@ export class AttachProcessProvider implements IAttachProcessProvider {
});
}

public async _getInternalProcessEntries(): Promise<IAttachItem[]> {
public async _getInternalProcessEntries(specCommand?: ProcessListCommand): 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;
if (specCommand === undefined) {
const osType = getOSType();
if (osType === OSType.OSX) {
processCmd = PsProcessParser.psDarwinCommand;
} else if (osType === OSType.Linux) {
processCmd = PsProcessParser.psLinuxCommand;
} else if (osType === OSType.Windows) {
processCmd = PowerShellProcessParser.powerShellCommand;
} else {
throw new Error(l10n.t("Operating system '{0}' not supported.", osType));
}
} else {
throw new Error(l10n.t("Operating system '{0}' not supported.", osType));
processCmd = specCommand;
}

const customEnvVars = await getEnvironmentVariables();
if (processCmd === PowerShellProcessParser.powerShellCommand) {
try {
const checkPowerShell = await plainExec(
'where',
['powershell'],
{ throwOnStdErr: false },
customEnvVars,
);
if (checkPowerShell.stdout.length === 0) {
processCmd = WmicProcessParser.wmicCommand;
}
} catch (error) {
// If 'where' fails, fall back to wmic (most likely powershell is not available).(Windows Xp or below?
console.log('PowerShell check failed, using WMIC fallback');
processCmd = WmicProcessParser.wmicCommand;
}
}
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