(pid: number)
| 63 | } |
| 64 | |
| 65 | async function describeWindowsProcess(pid: number): Promise<ProcessDescription> { |
| 66 | // Win32_Process has the command line; tasklist, the fallback when PowerShell |
| 67 | // can't run, has only the image name. |
| 68 | const script = |
| 69 | "$ErrorActionPreference = 'Stop'; [Console]::OutputEncoding = [Text.Encoding]::UTF8; " + |
| 70 | `$p = Get-CimInstance Win32_Process -Filter 'ProcessId = ${pid}'; ` + |
| 71 | 'if ($p) { [Console]::Out.Write((ConvertTo-Json -Compress @{ name = $p.Name; commandLine = $p.CommandLine })) }'; |
| 72 | const json = await query(windowsTool('WindowsPowerShell', 'v1.0', 'powershell.exe'), |
| 73 | ['-NoProfile', '-NonInteractive', '-NoLogo', '-Command', script]); |
| 74 | if (json?.trim()) { |
| 75 | try { |
| 76 | const found = JSON.parse(json) as { name?: unknown; commandLine?: unknown }; |
| 77 | return { |
| 78 | name: typeof found.name === 'string' && found.name ? found.name : null, |
| 79 | commandLine: typeof found.commandLine === 'string' && found.commandLine.trim() ? found.commandLine : null, |
| 80 | }; |
| 81 | } catch { /* fall back to tasklist */ } |
| 82 | } |
| 83 | const csv = await query(windowsTool('tasklist.exe'), ['/FI', `PID eq ${pid}`, '/FO', 'CSV', '/NH']); |
| 84 | for (const line of csv?.split(/\r?\n/) ?? []) { |
| 85 | const fields = /^"([^"]*)","(\d+)"/.exec(line); |
| 86 | if (fields && Number(fields[2]) === pid) return { name: fields[1] || null, commandLine: null }; |
| 87 | } |
| 88 | return { name: null, commandLine: null }; |
| 89 | } |
| 90 | |
| 91 | function describeLinuxProcess(pid: number): ProcessDescription { |
| 92 | let commandLine: string | null = null; |
no test coverage detected