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
43 changes: 43 additions & 0 deletions bluetooth/tracing/GetMemoryDump.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
$WerNativeMethods = [PSObject].Assembly.GetType('System.Management.Automation.WindowsErrorReporting').GetNestedType('NativeMethods', 'NonPublic')
$MiniDumpWriteDump = $WerNativeMethods.GetMethod('MiniDumpWriteDump', ([Reflection.BindingFlags]'NonPublic, Static'))

$ServiceNames = @('BthServ', 'Bluetooth%', 'BTAGService', 'BthAvctpSvc')

If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`

[Security.Principal.WindowsBuiltInRole] “Administrator”)) {
Write-Error "Administrator rights are required to collect dumps."
Break
}

$DumpFolder = Join-Path ($Env:Temp) (Get-Random -Minimum 1000)
New-Item -Path $DumpFolder -Type Directory | Out-Null

foreach ( $ServiceName in $ServiceNames ) {
Trap [System.Management.Automation.ParameterBindingException] {
Write-Warning "Could not find $ServiceName, it is likely not running."
Continue }
$Process = $null #ensure null on failure below
$Process = Get-Process -Id (Get-WmiObject -Class Win32_Service -Filter "Name LIKE '$ServiceName'" | Select-Object -ExpandProperty ProcessId)

if ($Process) {
$DumpFilePath = Join-Path $DumpFolder "$($ServiceName)_$($Process.Id).dmp"
$DumpFile = New-Object IO.FileStream($DumpFilePath, [IO.FileMode]::Create)

Write-Host "Dumping service $ServiceName with PID $($Process.Id)..."
$Result = $MiniDumpWriteDump.Invoke($null, @($Process.Handle, $Process.Id, $DumpFile.SafeFileHandle, [UInt32]0x2, [IntPtr]::Zero, [IntPtr]::Zero, [IntPtr]::Zero))

$DumpFile.Close()

if (-not $Result) {
Write-Error "Failed to write dump file for service $ServiceName with PID $($Process.Id)."
Break }
}
}

if ((gci $DumpFolder).Count -gt 1) {
Compress-Archive $DumpFolder "$DumpFolder.zip" -CompressionLevel Optimal -Force
Write-Host "Dumps stored in $DumpFolder.zip."
}

Trap {Continue} Remove-Item $DumpFolder -Force -Recurse
7 changes: 7 additions & 0 deletions bluetooth/tracing/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ wpr.exe /? will also give you more information.
From PowerShell execute:

wget https://github.com/Microsoft/busiotools/raw/master/bluetooth/tracing/GetBluetoothRadioInfo.ps1 -UseBasicParsing | iex

# How to collect memory dump

From elevated PowerShell execute:

wget https://github.com/Microsoft/busiotools/raw/master/bluetooth/tracing/GetMemoryDump.ps1 -UseBasicParsing | iex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProcessDump

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script name is indeed GetMemoryDump are you suggesting it's be renamed to GetProcessDumps.ps1?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I think that would make it clear.