Symbols the Microsoft Way
Symbol servers allow developer tools on Windows to automatically find symbols (debug information). All you have to do is adjust the tool’s settings to point at the Microsoft/Chrome/whatever symbol server and the appropriate symbols will be automatically downloaded. The Chromium documentation explains how to use .sympath or set _NT_SYMBOL_PATH for Microsoft and Chrome symbols and is a good starting point.
Automatic symbol download works so well that most developers never have to worry about the internal mechanisms. However when things go wrong it can be helpful to understand how they work, and it turns out that it is all very simple.
Note that elfutils debuginfod may finally be letting Linux catch up to Microsoft and Windows, but allowing automatic retrieval of debug information and source code.
This article should serve as a good comparison to the process of getting symbols, especially for crashes on customer machines, for Linux. I documented that process in a four-part series:
- Symbols on Linux Part One: g++ Library Symbols – learning how to get symbols for my Linux machine
- Symbols on Linux Part Two: Symbols for Other Versions – struggling to get symbols for customer machines
- Symbols on Linux Part Three: Linux versus Windows – the promise of build IDs
- Symbols on Linux update: Fedora Fixes – build ID progress
My discussion of Windows symbol servers make use of the symbol server that I have on my laptop, for my own personal projects. Whenever I release a new version of Fractal eXtreme (64-bit optimized, multi-core, fast and fluid exploration of fractals, demo version here) I put the symbols and binaries on my symbol server so that I can trivially investigate any crash reports that I receive. This may seem like overkill for a home project, but in fact a local symbol server is just a copy of the files, arranged in a specific way for easy retrieval, and it is trivial to set up. For executables that I release publicly, like UIforETW, I publish the PE and PDB files to a public symbol server on Google storage – details are here.
Finding PE files
Symbol servers store not just symbols but also PE files (DLLs and EXEs). If these aren’t already available, such as when looking at a minidump or an xperf profile, then they are usually retrieved first, before the symbols. For crash dumps from 64-bit processes these PE files may be necessary in order to do stack walking because they contain the necessary metadata. There are three pieces of information that are needed in order to retrieve a PE file from a symbol server: the file name, link time stamp, and the image size. In order to manually check whether the latest version of FractalX.exe made it into my symbol server I would extract the link time stamp and the image size from the executable like this:
dumpbin FractalX.exe /headers | find “date stamp” 4FFD0109 time date stamp Tue Jul 10 21:28:57 2012 dumpbin FractalX.exe /headers | find “size of image” 147000 size of image
The format for the path to a PE file in a symbol server share is:
“%s%s%08X%x%s” % (serverName, peName, timeStamp, imageSize, peName)
Note that the timeStamp is always printed as eight digits (with leading zeroes as needed) using upper-case for ‘A’ to ‘F’ (important if your symbol server is case sensitive), whereas the image size is printed using as few digits as needed, in lower-case.
Aside: If you do a pair of builds with minimal differences between them then the size will probably not change, and the peName certainly won’t, so the only thing stopping the two builds from having the same symbol-server address is the timeStamp. This means that reducing the precision of the timeStamp – say, from one second to 24 hours – could cause some pretty serious filename collisions. You could end up overwriting the first entry in the symbol server with the second. You know, hypothetically.
My symbol server is in c:\MySyms (normally it would be on a shared server, but this is my personal laptop) so the full path for the file examined above is:
c:\MySyms\fractalx.exe\4FFD0109147000\FractalX.exe
Simple enough. In my case I use symstore.exe’s /compress option (it saves a lot of space) when I add the files. Compressed files are indicated by replacing the last character with an underscore, so the actual path is this:
c:\MySyms\fractalx.exe\4FFD0109147000\FractalX.ex_
This is a good test to make sure that your PE files have been correctly added to your symbol server but it’s not a very realistic use case since we used the PE file to obtain the values needed to retrieve the PE file. The more common scenario is that you would have a minidump or an xperf ETL file and this file would contain a series of module name, link time stamp, image size triplets and these would be used at analysis time to retrieve the PE files. In the case of minidump files there is an array of MINIDUMP_MODULE structures which contain the relevant data. Note that the layout of symbol server shares can be much more complex. You should use the APIs (discussed later) to retrieve PE files – the technique above is purely for troubleshooting.
If you have a link time-stamp and you want to convert it to a date you can use this Python one-liner batch file:
text
python -c "import time; print time.ctime(int('%1',16))"
One extra quirk is that somewhere in Microsoft’s linker/debugger toolchains used to lower-case the names of PE files. This meant that if you use a case-sensitive file system for your symbol server (as [Chrome does](https://www.chromium.org/developers/how-tos/debugging-on-windows)) then you had to upload your symbols using lower-case file names. The Chrome team discovered this the hard way. The PDB names are extracted from the PE files, with the case intact, so they just have to match. I can no longer reproduce this behavior – UIforETW.exe gets uploaded in mixed-case to the case-sensitive Google Storage and this works.
## Finding PDB files
Finding PE files is handy when analyzing customer crash dumps in order to have the assembly instructions but it’s actually more important than that. Minidump files don’t necessarily record enough information to retrieve PDB files. In those cases the tools retrieve the PE files and then look in the PE files to get the information needed to retrieve the PDB files. Once again we can extract this information from a PE file using dumpbin:
> > dumpbin FractalX.exe /headers | find “Format:”
> 4FFD0109 cv 56 000B9308 B7B08 Format: RSDS, {6143E0D1-9975-4456-AC8E-F24C8777336D}, 1, FractalX.pdb
The long hexadecimal number after *RSDS* is a *[GUID](http://en.wikipedia.org/wiki/GUID)*, and the number after that (a 32-bit decimal number, but in this case just ‘1’) is called the ‘age’. The PDB file name is also listed here. Together these uniquely identify a particular version of a PDB file. The format for the path to a PDB file in a symbol server share is:
> “%s\%s\%s%x\%s” % (serverPath, pdbName, guid, age, pdbName)
Funny thing here – notice that I use %x to print the age, but in the previous paragraph I described the age as being a decimal number. Well, the PDB age (just a measure of how many times the same PDB has been reused) is just a 32-bit number, but dumpbin prints it in decimal, and symbol servers expect it in hexadecimal. Hurray for consistency! This means that if you parse the dumpbin output you need to convert the age to an integer and the print it as hexadecimal. If you get this wrong then the bug won’t show up until you encounter [a PDB with an age of ten or greater](https://twitter.com/BruceDawson0xB/status/618947933086416896). Wonderful.
As with the PE files a final underscore indicates when a file is compressed by symstore.exe. The path on my symbol server for the PDB listed above looks like this:
> c:\MySyms\FractalX.pdb\6143E0D199754456AC8EF24C8777336D1\FractalX.pd\_
Simple enough. The algorithm for generating the GUID and age is that whenever you do a rebuild – whenever a fresh PDB is generated – a new GUID is created and the age is set to one. Whenever you do an partial build the PDB is updated with new debug information and the age is incremented. That’s it – use the PE name, link time stamp, and image size to find the PE (if it isn’t already loaded) and then use the GUID, age, and PDB file name to find the PDB file. Note that the layout of symbol server shares can be much more complex. You should use the APIs (discussed later) to retrieve PDB files – the technique above is purely for troubleshooting.
## Adding to a symbol server
If you ship software on Windows then you should have a symbol server. That symbol server should contain the PE files and PDB files for every product you ship. If you don’t do this then you are doing either yourself or your customers a disservice. You should also have a symbol server for all internal builds that anybody at the company might end up running. If a program might crash, and if you want to be able to investigate the crash then put the symbols on the symbol server. If you’re worried about internal builds using up too much space then put them on a separate symbol server and purge the old files occasionally. You should also make sure that your build machines are running source indexing so that when you’re debugging a crash in an old version of your software you will automatically get the right source files. Luckily I [wrote about that already](/u/123c2e840ab237c6). Adding files to a symbol server is the height of simplicity. Set *sourcedir* to point at a directory containing files to add and set *dest* to your symbol server directory, which should be accessible to all who need the symbols. Then run these commands:
> symstore add /f %sourcedir%\\*.dll /s %dest% /t prodname /compress symstore add /f %sourcedir%\\*.exe /s %dest% /t prodname /compress symstore add /f %sourcedir%\\*.pdb /s %dest% /t prodname /compress
If you’re running a case-sensitive file system then you’ll need to lower-case the PE file names afterwards – sorry.
That’s it. Use /r if you want the files recursively added, and see the help for more information. If you have an existing symbol server that was not compressed, or if you want to do the compression step separately from adding to the symbol server, then the makecab command (ships with Windows) does the trick. This is what Chrome used to use, with this type of command line:
makecab /D CompressionType=LZX /D CompressionMemory=21 chrome.dll.pdb
That will generate a compressed chrome.dll.pd\_ file which will be automatically decompressed when it is downloaded to your local symbol cache.
Another option is to use compress.exe from the *Windows Server 2003 Resource Kit Tools.* But be careful. *compress /help* says that LZX is the default but it isn’t. So be sure to use *compress -ZX* or you will end up with compressed files that SymSrv cannot decompress.
Apparently *makecab*, *compress.exe*, and *symstore* all have the limitation that their input files have to be less than 2 GiB. This is a problem for Chrome’s unified chrome.dll.pdb which is currently (October 2019) about 2.5 GB, and therefore cannot be compressed. [Oops](https://bugs.chromium.org/p/chromium/issues/detail?id=1019811). pigz can handle 4 GiB input files but a version that generates CAB files is not yet open source. We ultimately fixed this by foregoing Microsoft’s compression and instead using HTTP compression – [we added -Z to our gsutil command line](https://chromium-review.googlesource.com/c/chromium/tools/build/+/1934893) so that the PDBs would be gzip compressed on upload.
If you make your symbol server available through HTTP be sure to use https, to ensure the integrity of the downloads.
Or, if you are trying to make a publicly accessible symbol server, just follow [these simple directions](/u/e2cf3771f816d3fe).
## Using a symbol server
The precise details of how to get your development tools to use your symbol server vary, but one almost universal method is to set the [\_NT\_SYMBOL\_PATH](http://support.microsoft.com/kb/311503) environment variable ([advanced usage here](http://msdn.microsoft.com/en-us/library/windows/hardware/ff537994(v=vs.85).aspx) and [here](http://msdn.microsoft.com/en-us/library/ff558829(v=vs.85).aspx)), to something like this:
> \_NT\_SYMBOL\_PATH=SRV\*c:\symbols\*c:\MySyms;SRV\*c:\symbols\*https://msdl.microsoft.com/download/symbols;SRV\*c:\symbols\*https://chromium-browser-symsrv.commondatastorage.googleapis.com
This tells tools to first look in the local cache (c:\symbols) and then look in the symbol server c:\MySyms. If symbols are found in c:\MySyms then they are copied (and decompressed) to c:\symbols. If none of that works then the same process (including the same cache directory) is followed for Microsoft’s web based symbol cache, and then Chrome’s. Note that a local symbol cache is required when dealing with compressed symbols. Note that some symbol servers, such as Chrome’s and Microsofts, can be reached through https as well as http. When https is available as an option you should always use it since otherwise a man-in-the-middle attack could use malformed PDBs or source-indexing commands to execute arbitrary code when you download and use these PDBs.
> Microsoft still lists their symbol server using http in some places, but https works and should be preferred.
The SRV\* part of \_NT\_SYMBOL\_PATH is important, poorly documented, and apparently poorly understood. It is my understanding, confirmed by [discussions on stackoverflow](http://stackoverflow.com/questions/30668691/windbg-takes-extremely-long-time-to-loading-symbols-is-searching-every-director/30673123#30673123), that SRV\* tells symsrv.dll to treat the following paths or URLs as symbol servers instead of just a collection of loose files. So, if \_NT\_SYMBOL\_PATH is c:\symbols then dbghelp or symsrv may recursively search the directory structure for your symbols, but if \_NT\_SYMBOL\_PATH is SRV\*c:\symbols then it will search in a very structured and efficient way. If \_NT\_SYMBOL\_PATH is
SRV\*c:\symbols\*https://msdl.microsoft.com/download/symbols
then symsrv will first look in c:\symbols, using the quick and efficient symbol server algorithm, and will then (if the symbols aren’t found) do the same efficient search in Microsoft’s symbol server. You should prefer using SRV\* and symbol server layout rather than unstructured symbols.
## Programmatically retrieving symbols
Usually the debuggers and profilers that you use will know how to use symbol servers, but occasionally you may need to write code to download symbols – perhaps you are writing a debugger or profiler. In my case I had a web page that listed GUIDs, ages, and PDB names for dozens of Microsoft DLLs from dozens of versions of Windows for which we needed symbols. Writing code to download all of these symbols was trivial – several orders of magnitude easier than getting symbols for [other versions](/u/527584a5f36a24fa) of [Linux](/u/a5618ced725e9357). The short explanation of what I needed to do was “call SymFindFileInPath”. In order to demonstrate how easy it was I decided to give a slightly longer explanation. The sample code, available [on github](https://github.com/google/UIforETW/blob/master/RetrieveSymbols/RetrieveSymbols.cpp) as part of [UIforETW](https://github.com/google/UIforETW), takes a GUID, age, and pdb name, or a datestamp, size, and pename and downloads the PE or PDB file from a symbol server. The biggest chunk of code is for parsing the GUID – the actual downloading is trivial.
The TESTING define uses a known-good GUID, age, name, and symbol server. Comment out that define to use this to download arbitrary symbols from the symbol servers specified in \_NT\_SYMBOL\_PATH. If you encounter any difficulties then run this program in a debugger – dbghelp will print diagnostics to the debugger output window. The one gotcha is that dbghelp.dll and symsrv.dll have to be in the same directory as your tool – having them in your path does not work reliably.
As previously mentioned, the latest version of RetrieveSymbols (the tool whose source code is listed above) ships in UIforETW – source is [here](https://github.com/google/UIforETW/tree/master/RetrieveSymbols) and the latest binary can be found [here](https://github.com/google/UIforETW/tree/master/RetrieveSymbols) or in the latest [UIforETW release](https://github.com/google/UIforETW/releases). The symchk tool (ships in the Windows debugger toolkit) also downloads PDB files when passed a PE file – use the /v option to get information about where the PDB file was downloaded to, and other information. symchk is more convenient if you have a .dmp file or a PE file that you need symbols for, whereas RetrieveSymbols is more convenient if you have a GUID, age, and PDB file name.
## Diagnosing symbol problems with windbg
If you have a minidump and its symbols are not loading then I recommend loading the minidump into [windbg](http://msdn.microsoft.com/en-us/windows/hardware/gg463009.aspx) and using its diagnostics:
- !sym noisy – print verbose information about attempts to get symbols
- lmv m MyModule – print a record from the crash dump’s module list including its name, time stamp, image size, and where the PDB is located if it was found
- !lmi MyModule – print a module’s header information – this only works if the PE file has been loaded, which is a prerequisite for having symbols load
## Dumpbin summary
- “%VS120COMNTOOLS%..\..\VC\vcvarsall.bat” – this adds dumpbin’s directory to the path
- dumpbin FX.exe /headers | find “date stamp” – find the link time stamp of a PE file
- dumpbin FX.exe /headers | find “size of image” – find the image size of a PE file
- dumpbin FractalX.exe /headers | find “Format:” – find the GUID, age, and file name of a PE file’s PDB file
See [this blog post](https://www.timdbg.com/posts/symbol-indexing/) for another perspective on symbol servers, and see [this follow-up blog post](/u/e2cf3771f816d3fe) for instructions on actually creating a public symbol server.
Hacker news discussion [is here](https://news.ycombinator.com/item?id=25972764)