Add a -Dump flag to collect-wsl-logs (#9077)

* Add a -Dump flag to collect-wsl-logs * Format

Blue committed Oct 31, 2022 at 19:47 UTC 631dd11c65356972c095327b3a3de1e84cdc90d8
1 file changed +36 -2
diagnostics/collect-wsl-logs.ps1
+36 -2
@@ -2,8 +2,9 @@
2
3 [CmdletBinding()]
4 Param (
5 - $LogProfile = $null
6 - )
5 + $LogProfile = $null,
6 + [switch]$Dump = $false
7 + )
8
9 Set-StrictMode -Version Latest
10
@@ -42,6 +43,39 @@ $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
43 Write-Host "`nSaving logs..."
44
45 wpr.exe -stop $folder/logs.etl
46 +
47 +if ($Dump)
48 +{
49 + $Assembly = [PSObject].Assembly.GetType('System.Management.Automation.WindowsErrorReporting')
50 + $DumpMethod = $Assembly.GetNestedType('NativeMethods', 'NonPublic').GetMethod('MiniDumpWriteDump', [Reflection.BindingFlags] 'NonPublic, Static')
51 +
52 + $dumpFolder = Join-Path (Resolve-Path "$folder") dumps
53 + New-Item -ItemType "directory" -Path "$dumpFolder"
54 +
55 + $executables = "wsl", "wslservice", "wslhost", "msrdc"
56 + foreach($process in Get-Process | Where-Object { $executables -contains $_.ProcessName})
57 + {
58 + $dumpFile = "$dumpFolder/$($process.ProcessName).$($process.Id).dmp"
59 + Write-Host "Writing $($dumpFile)"
60 +
61 + $OutputFile = New-Object IO.FileStream($dumpFile, [IO.FileMode]::Create)
62 +
63 + $Result = $DumpMethod.Invoke($null, @($process.Handle,
64 + $process.id,
65 + $OutputFile.SafeFileHandle,
66 + [UInt32] 2
67 + [IntPtr]::Zero,
68 + [IntPtr]::Zero,
69 + [IntPtr]::Zero))
70 +
71 + $OutputFile.Close()
72 + if (-not $Result)
73 + {
74 + Write-Host "Failed to write dump for: $($dumpFile)"
75 + }
76 + }
77 +}
78 +
79 $logArchive = "$(Resolve-Path $folder).zip"
80 Compress-Archive -Path $folder -DestinationPath $logArchive
81 Remove-Item $folder -Recurse