When are pdb files actually loaded - c#

When doing a "Debug" Build in Visual Studio it outputs a pdb file so that you get detailed information when an exception occurs.
Is the information in the pdb files actually loaded when the executable is run or later at the moment when the exception occurs?

Decompilers such as IDA load symbol files when it analyses your executable, or when you tell it to load specific symbols with your executable, as long as it matches the executable currently being debugged.
Debuggers such as the Visual Studio debugger load PDB files and symbols as soon as it starts debugging, not when an exception occurs.
The MSDN page on PDB files further underlines this:
The Visual Studio debugger uses the project.PDB file created by the
linker directly and embeds the absolute path to the PDB in the EXE or
DLL file. If the debugger cannot find the PDB file at that location or
if the path is invalid (for example, if the project was moved to
another computer), the debugger searches the path containing the EXE,
the Symbol Path specified in the solution's Property Pages (Common
Properties folder, Debug Symbol Files page). The debugger will not
load a PDB that does not match the binary being debugged.
Of course, is a program is compiled without /debug, the path to the PDB file won't be included in the resulting exe or DLL.

Yes, they are loaded outside of VS. If your code crashes while loaded with the PDB's you'll see source code location and line numbers.

Related

How to know where the pdb files is looking for source code

I have an external dll and a pdb file from C# code. I have the source code downloaded on my computer. In Visual Studio, under modules, Symbol status is: Symbol loaded for that specific dll. But User code is: N/A.
And I can't step into the functions in that dll. How do I know where to put the source code so the pdb finds it? Is the pdb looking the C# source file in a specific directory and is there a way to find out where?
There is a tool called SrcTool.exe that is installed with the WinSDK. Srctool.exe utility lists all files indexed within the .pdb file.
SrcTool.exe -r filename.pdb will print the source file paths.

Debugging with Pdb file and Source Code File in Visual Studio

I have a web project that posts from client-side code to a method in external dll, which I have source code file and pdb file of this external dll. What I would like to do is to debug external dll using source code file and pdb. Visual studio does not stop to say no symbols are loaded for the module since.
To debug a a symbol file with the same version is always needed. When you are debugging your own applications you usually don't have to care about this.
But there are things happening in the background. Visual Studio always puts the symbol files in the debug folder when you build your application and also loads them as described under Loading the symbols automatic.
(When you distribute your application you usually do not want to distribute those symbols and therefore they won't be copied to the release directory you change your build configuration to release.)
Loading the symbols manually
If you want to load the symbols manually you can load them with the Modules dialog.
"Debug" -> "Windows" -> "Modules".
You can right click a row and there is the option "Load Symbols" which lets you specify a PDB file to load.
Loading the symbols automatically
Visual studio also loads the symbols automatically when they can be found in one of the the places listed in the Specify Symbol (.pdb) and Source Files in the Visual Studio Debugger documentation:
The location that is specified inside the DLL or the executable file.
(By default, if you have built a DLL or an executable file on your computer, the linker places the full path and file name of the associated .pdb file inside the DLL or the executable file. The debugger first checks to see if the symbol file exists in the location that is specified inside the DLL or the executable file. This is helpful, because you always have symbols available for code that you have compiled on your computer.)
.pdb files that could be present in the same folder as the DLL or executable file.
Any local symbol cache folders.
Any network, internet, or local symbol servers and locations that are specified on, such as the Microsoft symbol server if enabled.
If you want to read more about how the symbols are used with visual studio you can read this article about Understanding symbol files and Visual Studio’s symbol settings.

How does the Visual Studio debugger know where the source is when loading an assembly at runtime?

Consider the following code:
private static void Main(string[] args)
{
var exe = new MemoryStream(File.ReadAllBytes(args[0]));
var assembly = AssemblyLoadContext.Default.LoadFromStream(exe);
assembly.EntryPoint.Invoke(null, new object[] { new string[0] });
}
This program was compiled in a .net core 1.1 application, and args contains the path to the dll of a generic hello world in .net core in it's debug output directory.
When I run this program, it loads the assembly from an in-memory copy of the dll and then calls the entry point. Visual Studio seems to successfully recognise that this dll is the same as the hello world project I have open in the solution and I can step through and debug this invoked program.
How does the VS Debugger know where to find the source when I loaded from an in-memory copy of the assembly?
First it locates the .pdb and then from there it can locate the source files.
How does it locate the .pdb? From Specify Symbol (.pdb) and Source Files in the Visual Studio Debugger:
When you debug a project in the Visual Studio IDE, the debugger knows the default location for the .pdb and source files for your code.
...
(By default, if you have built a DLL or an executable file on your computer, the linker places the full path and file name of the associated .pdb file inside the DLL or the executable file. The debugger first checks to see if the symbol file exists in the location that is specified inside the DLL or the executable file. This is helpful, because you always have symbols available for code that you have compiled on your computer.)
There are other locations where it will also search for .pdbs but this one seems like the most likely source when you've tried to dissociate the assembly from its file on disk.
The answer is in this MSD page:
To use the full features of the Visual Studio debugger (like hitting
breakpoints) when attaching to a process, the executable must exactly
match your local source and symbols (that is, the debugger must be
able to load the correct symbol (.pbd) files). By default, this
requires a debug build.
Also,
A program database (.pdb) file, also called a symbol file, maps the
identifiers that you create in source files for classes, methods, and
other code to the identifiers that are used in the compiled
executables of your project. The .pdb file also maps the statements in
the source code to the execution instructions in the executables. The
debugger uses this information to determine two key pieces of
information: the source file and line number that are displayed in the
Visual Studio IDE and the location in the executable to stop at when
you set a breakpoint. A symbol file also contains the original
location of the source files, and optionally, the location of a source
server where the source files can be retrieved from.
Where the debugger searches for .pdb files
The location that is specified inside the DLL or the executable
file.
.pdb files that could be present in the same folder as the DLL
or executable file.
Any local symbol cache folders.
Any network,internet, or local symbol servers and locations that are specified on, such as the Microsoft symbol server if enabled.

Reproducing a crash from a dump file in Windows Store application

I have a dump file of a Metro application created by AdPlus.
I have both the pdb files and the source code.
When I open dmp file in Visual Studio I can see only Disassembled code (asm).
Is there any way to debug source code? How do I add pdbs and debug using source code?
if the file is a dll you just need to put the pdb in the same directory where the dll resides, and in the visual studio debugger you need to point to this file

Debugging MiniDump

I have compiled a Debug-Version of my application in one folder. There are now all dll's, pdb's and the exe with the pdb.
I have now created a MiniDump of my running application started out of this path with Taskmanager. Now, I can open this dmp-File in Visual Studio 2010. But when I try to debug, there will be a message, that the symbols could not be found. I have added the path to my symbols (pdb) to the symbol-paths in visual studio - but with no success. I have also try to load the symbols over the ContextMenu of the modules. It is searching there for MyApp.pdb and I have selected the correct pdb. But I get the message A matching symbol file was not found in this folder.
What can I do to make post mortem debug of my MiniDump with Visual Studio 2010 (so it will find my symbols)?
Do your pdb's and dll's have the same timestamp? Are they both recreated when you rebuild?
Try using chkmatch to ensure your dll and pdb match.
Use chkmatch this way:
chkmatch -c MyApp.exe MyApp.dll
The output should look something like this
Debug information file:
Format: PDB 7.00
Signature: {ef4bc52f-0161-4e0a-8654-cc1368d7a8a6} Age: 1
Result: Matched
You shouldnt need to set the symbol path if your pdb's are in the same location as the exe you are dumping.
Are you on .NET 4?

Categories

Resources