Created
April 11, 2023 17:30
-
-
Save HollisTech/4272212fb6e7d2c747787b24588ee977 to your computer and use it in GitHub Desktop.
Windbg helpers for powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-KitRoot { | |
param( | |
[string]$altRoot="C:\Program Files (x86)\Windows Kits\10\" | |
) | |
try { | |
Get-ItemPropertyValue "HKLM:\SOFTWARE\Microsoft\Windows Kits\Installed Roots\" -Name "KitsRoot10" | |
} | |
catch { | |
if (Test-Path -Path $altRoot -PathType Container) | |
{ | |
$altRoot | |
} | |
else { | |
$null | |
} | |
} | |
} | |
Function Get-Debugger { | |
param( | |
[switch]$interactive=$false | |
) | |
$kitRoot = Get-KitRoot | |
$x64path = Join-Path $kitRoot "Debuggers\x64" | |
$x86path = Join-Path $kitRoot "Debuggers\x86" | |
$d = "" | |
if ($interactive) { | |
$exe = "windbg.exe" | |
} else { | |
$exe = "kd.exe" | |
} | |
if ($env:debug_path -and (Test-Path -Path "$env:debug_path")) { | |
$d = $env:debug_path | |
} | |
elseif (Test-Path -Path $x64path) { | |
$d = $x64path | |
} | |
elseif (Test-Path -Path $x86path) { | |
$d = $x86path | |
} | |
if (!(Test-Path -Path $d -PathType leaf)) { | |
$d = join-path $d $exe | |
} | |
if (!(Test-Path -Path $d)) { | |
throw "debugger not found! Is it installed?" | |
} | |
$d | |
} | |
function pdbStrings { | |
$pdbstr = Join-Path "$(Get-KitRoot)" "Debuggers\x64\srcsrv\pdbstr.exe" | |
& $pdbstr @args | |
} | |
function srcStrings { | |
$srcstr = Join-Path "$(Get-KitRoot)" "Debuggers\x64\srcsrv\srcTool.exe" | |
& $srcstr @args | |
} | |
function pdbFiles { | |
Param ( | |
[Parameter(Mandatory, ValueFromPipeline)] | |
[string]$pdb, | |
[string]$srcRoot=(convert-path "$( git rev-parse --show-toplevel)") | |
) | |
if ($srcRoot -eq $null) { | |
Write-Host "Using current dir as root $PWD" | |
$srcRoot = $PWD | |
} | |
(srcStrings -r $pdb).where({$_.StartsWith($srcRoot)}) | |
} | |
# a json file for setting additional symbol paths | |
# if a symbol path uses srv**\path then it will have the | |
# cachepath inserted between the '**'. | |
# JSON: | |
# { | |
# "sympaths": [ "srv*\\foo\bar\symbols"], | |
# "cachepath": "somepath" | |
# } | |
Function get-symPath | |
{ | |
Param( | |
[string]$sympath=@" | |
srv**http://msdl.microsoft.com/download/symbols | |
"@, | |
[string]$cache="$($env:userprofile)\symbols" | |
) | |
$symJsonPath = "~/.symPath.json" | |
$json = @{} | |
if (Test-Path -Path $symJsonPath) { | |
$custObj = (Get-Content -Path $symJsonPath) | ConvertFrom-Json | |
# psh5 can only return custom objects | |
$custObj.psobject.properties | ForEach-Object { | |
# now its a proper hashtable | |
$json[$_.Name] = $_.Value | |
} | |
if ($json.ContainsKey("sympaths")) { | |
$json.sympaths | ForEach-Object { | |
$sympath += ";$($_)" | |
} | |
} | |
if ($json.ContainsKey("cachepath")) { | |
$cache += $json.cachepath | |
} | |
} | |
$s = $sympath.replace( "srv**",$("srv*$cache*")) | |
$s -replace "`n","" | |
} | |
Function pipeWindbg | |
{ | |
param( | |
[string]$pipe="\\.\pipe\windbg", | |
[string]$server="", | |
[switch]$remote, | |
[switch]$asServer | |
) | |
$syms = get-symPath | |
$debugger = Get-Debugger -interactive | |
$pipeArgs = @("-y", "$syms", "-WX") | |
if ($remote) { | |
$pipeArgs += @("-remote", "npipe:Pipe=$pipe,Server=$server") | |
} else { | |
$pipeArgs += @("-k","com:pipe,port=$pipe,resets=0,reconnect") | |
if ($asServer) { | |
$srvPipe = "\\.\pipe\DBG-$(($pipe -split '\\')[-1])" | |
$pipeArgs += @("-server", "npipe:Pipe=$srvPipe") | |
} | |
} | |
write-host "$debugger $pipeArgs" | |
& $debugger $pipeArgs | |
} | |
# a json file of profiles to re-use | |
# JSON: | |
# { | |
# "name1": { | |
# "port": nnnnn, | |
# "key": "fhfhfhfhfhfhfhfh" | |
# }, | |
# "name2": { | |
# "port": nnnnn, | |
# "key": "fhfhfhfhfhfhfhfh" | |
# } | |
# } | |
function netWindbg | |
{ | |
[CmdletBinding(DefaultParameterSetName = 'PortAndKey')] | |
param( | |
[Parameter(Mandatory=$True, ParameterSetName = 'PortAndKey') | |
] | |
[ValidateRange(49152,65535)] | |
[Int]$port, | |
[Parameter(Mandatory=$True, ParameterSetName = 'PortAndKey')] | |
[string]$key, | |
[Parameter(ParameterSetName = 'PortAndKey')] | |
[string]$name="", | |
[Parameter(ParameterSetName = 'Saved')] | |
[string]$useName | |
) | |
$jsonFile = "~/.netBag.json" | |
$json = @{} | |
if (Test-Path -Path $jsonFile) { | |
$custObj = (Get-Content -Path $jsonFile) | ConvertFrom-Json | |
# psh5 can only return custom objects | |
$custObj.psobject.properties | ForEach-Object { | |
# now its a proper hashtable | |
$json[$_.Name] = $_.Value | |
} | |
} | |
if ($useName) { | |
if ($custObj.$useName) { | |
$json.$useName = $custObj.$useName | |
$port = $json.$useName.port | |
$key = $json.$useName.key | |
} else { | |
Write-Host "$($useName) not found in json file." | |
return | |
} | |
} else { | |
if ($name) { | |
$entry = @{"port" = $port; "key" = $key} | |
$json[$name] = $entry | |
ConvertTo-Json -InputObject $json | Set-Content -Path $jsonFile | |
} | |
} | |
$syms = get-symPath | |
$debugger = Get-Debugger -interactive | |
$netargs = @("-y", "$syms", "-WX", "-k", "net:port=$($port)),key=$($key))") | |
write-host "$debugger $netargs" | |
& $debugger $netargs | |
} | |
function checkSymbols | |
{ | |
param( | |
[string]$file | |
) | |
$syms = get-symPath | |
$symchk = Join-Path "$(Get-KitRoot)" "Debuggers\x64\symchk.exe" | |
$symArgs = @("/v", $file, "/s", $syms, "/op") | |
write-host "$symchk $symArgs" | |
& $symchk $symArgs | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment