Code/Add-NgenPdbs.ps1
#requires -version 5.0 ############################################################################### # WintellectPowerShell Module # Copyright (c) 2013-2017 - John Robbins/Wintellect # # Do whatever you want with this module, but please do give credit. ############################################################################### # Always make sure all variables are defined and all best practices are # followed. Set-StrictMode -version Latest ############################################################################### # Public Cmdlets ############################################################################### function Add-NgenPdbs { <# .SYNOPSIS Create PDB files for NGEN'd images on a machine. .DESCRIPTION When running the Visual Studio profiler on Windows 8+ or Windows Server 2012+, you need the PDB files for any binaries that have been NGEN'd and put in the GAC so you can see the calls into those binaries. Fortunately NGEN.EXE can generate the PDB files after the fact and this script automates the process for so you don't have to run NGEN on each binary. Because each machine has it's own unique NGEN'd files, you will have to run this script on the machine you are collecting the performance runs. After creating the PDB files, you'll have to copy them into your symbol server on the machine where you are analyzing the performance runs. Also note that not all files in the GAC can be NGEN'd so you might see some errors about unable to create the PDB. .PARAMETER CacheDirectory The cache directory to place the resulting PDB files. If not specified, looks at the Visual Studio 2013-2017 symbol settings and uses that as the cache directory. On a machine without Visual Studio where you are using the command line profiling tools, alway specify this parameter. .PARAMETER DoAllGACFiles By default, this command only does the NGEN'd binaries that are in the .NET framework directories for both x86 and x64. If you need other NGEN'd binaries from 3rd pary tools, specify this switch. Some NGEN'd binaries, but not from the .NET framework, will report errors when attempting to build their PDB files. .NOTES To read more about using NGEN to produce PDB files after a binary has been precompiled see this article: http://blogs.msdn.com/b/visualstudioalm/archive/2012/12/10/creating-ngen-pdbs-for-profiling-reports.aspx For an alternative implementation to this one, see http://knagis.miga.lv/blog/post/2013/01/22/VS2012-Windows-8-profilesana-ar-NGEN-bibliotekam.aspx .LINK http://www.wintellect.com/devcenter/author/jrobbins https://github.com/Wintellect/WintellectPowerShell #> # I hate suppressing this warning, but I created this cmdlet long before the # script analyzer came out. If someone has this in a script, changing the # cmdlet name will break them. [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Scope="Function")] [CmdletBinding()] param ( [string]$CacheDirectory = "", [switch]$DoAllGACFiles ) # If the CacheDirectory is empty, use the symbol path directory. if ($CacheDirectory.Length -eq 0) { # I have to look for the cache directory. First I'll start with the easy checks # by using Get-SymbolServer to pull out the VS versions. As a last resort, I have # to try and grab it from the _NT_SYMBOL_PATH environment variable. $symServs = Get-SymbolServer if ($symServs.Count -eq 0) { throw "You don't have a symbol server set" } $vsVersion = $null $symPath = $symServs["VS 2017"] if ($null -eq $symPath) { $symPath = $symServs["VS 2015"] if ($null -eq $symPath) { $symPath = $symServs["VS 2013"] if ($null -eq $symPath) { # Pull it out of _NT_SYMBOL_PATH. Yes this is only looking at the # first cache directory specified. $symPath = $symServs["_NT_SYMBOL_PATH"] if ($null -eq $symPath) { throw "No symbol server configured" } else { if ($symPath -match "SRV\*(?<SymCache>[^*]*)\*(?:.*)\;?") { $symPath = $Matches["SymCache"] } else { throw "_NT_SYMBOL_PATH environment variable does not specify a symbol cache" } } } else { $vsVersion = "2013" } } else { $vsVersion = "2015" } } else { $vsVersion = "2017" } if ($null -ne $vsVersion) { [xml]$settings = OpenSettingsFile $vsVersion $xPathLookup = $script:dbgPropertyXPath -f "SymbolUseMSSymbolServers" $useMSFTSymbolServersNode = $settings | Select-Xml -XPath $xPathLookup # If using the public symbol servers I have to put PublicSymbols on the path. if ($useMSFTSymbolServersNode.Node.InnerText -eq "1") { $SymPath = Join-Path -Path $symPath -ChildPath "PublicSymbols" } } if ($symPath.Length -eq 0) { throw "The symbol path is empty" } $CacheDirectory = $symPath } else { if (-not (Test-Path -Path $CacheDirectory)) { New-Item -Path $CacheDirectory -Type Directory | Out-Null } } # Get all the *.ni.dll files out of the 4.* GAC locations $gacSearchPath = $env:windir + "\assembly\NativeImages_v4*" $files = Get-ChildItem -Recurse -Path $gacSearchPath -Filter "*.ni.dll" -Exclude "*.resources.ni.dll" # I'll need this later to strip off the .ni.dll $niLength = ".ni.dll".Length foreach ($f in $files) { # Build up the command line to call NGEN.EXE on this binary. # Get the name of the DLL. $baseName = (Split-Path -Path $f -Leaf) $baseName = $baseName.SubString(0, $baseName.Length - $niLength) # Extract out the bit version of this compiled binary so I know if I'm supposed # to run the 32 or 64 bit version of NGEN.EXE and where to check if the file exists. # The replace looks weird but that's how you get \\ into the path. :) $pattern = $env:windir -replace '\\' , '\\' $pattern += "\\assembly\\NativeImages_(?<version>v4\.\d\.\d\d\d\d\d)_(?<bits>\d\d)" $f.FullName -match $pattern | Out-Null $bits = '64' if ($Matches.bits -eq '32') { $bits = '' } $fwVersion = $Matches.version # if doing the default of only framework files, check to see if this # file is in the appropriate directory. if ($DoAllGACFiles.IsPresent -eq $false) { # Build up the framework path. $fwPath = Join-Path -Path $env:windir -ChildPath "Microsoft.NET" $fwPath = Join-Path -Path $fwPath -ChildPath ("Framework" + $bits) $fwPath = Join-Path -Path $fwPath -ChildPath $fwVersion $fwFile = Get-ChildItem -Recurse -Path $fwPath -Filter ($baseName + ".dll") if ($null -eq $fwFile) { continue } } $msgBitness = '(x86)' if ($bits -eq "64") { $msgBitness = "(x64)" } Write-Verbose -Message "Generating PDB file for $msgBitness $baseName.dll" $ngenCmd = $env:windir + "\Microsoft.NET\Framework" + $bits + "\" + $fwVersion + "\NGEN.EXE" $ngenArgs = ' createPDB ' + '"' + $f.FullName + '"' + ' "' + $CacheDirectory +'"' # Start-Process can't capture output. $pinfo = New-Object System.Diagnostics.ProcessStartInfo $pinfo.FileName = $ngenCmd $pinfo.Arguments = $ngenArgs $pinfo.RedirectStandardError = $true $pinfo.RedirectStandardOutput = $true $pinfo.UseShellExecute = $false $p = New-Object System.Diagnostics.Process $p.StartInfo = $pinfo $p.Start() | Out-Null $p.WaitForExit() $outputData = $p.StandardOutput.ReadToEnd() $outputData += $p.StandardError.ReadToEnd() if ($p.ExitCode -ne 0) { # This may look odd, but it's because Write-Warning is too dumb to write arrays. Write-Warning -Message ([String]::Join("`n", $outputData)) } } } # SIG # Begin signature block # MIIUywYJKoZIhvcNAQcCoIIUvDCCFLgCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # g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