PDB CRACKER - EXPLORING PDB FILES with C#
Modified : June 22 2009

Program Database (PDB) files are generated on demand by Microsoft linker tools and contain a great deal of information about the program they are built against. They will be loaded, for example, if you use a debugger to run your program, so that the debugger can learn all about the internals of your compiled code.

The PDB file format is neither fixed nor documented - to gain access, you use Microsofts Debug Interface Access SDK, or DIA SDK for short.

Having access to this sort of information can be extremely useful, especially if you have to write profiling or analysis tools. PDB access can also be used as a form of metadata reflection for C++.

The DIA SDK is installed as part of Visual Studio. If you make a reference to the msdiaxx.dll (xx being the version code) in a C# project, you can use the DIA SDK to great effect to write .NET applications that work with PDB data. I wrote an example file to demonstrate, PDB Cracker.

PDB Cracker enumerates all the top-level compilands in the PDB, displaying their associated source files and lists of defined functions. Working with the DIA SDK in C# is reasonably straightforward:

using Dia2Lib;

// create a DIA session

m_source = new DiaSourceClass();
m_source.loadDataFromPdb(m_filename);
m_source.openSession(out m_session);

// get a list of all compilands in the global scope
IDiaEnumSymbols results;
m_session.findChildren(m_session.globalScope, SymTagEnum.SymTagCompiland, null, 0, out results);

foreach (IDiaSymbol sym in results)
{
// do something with it!
}

This application also shows how to use delegates to access the UI in a multithreaded fashion, with a seperate thread doing the enumeration of PDB data (potentially a fairly lengthy operation)

Download the source code and compiled executable here (Visual Studio 2005, .NET Framework 2.0)
Download an installer with msdia80.dll included here.