Cameron Aavik
Cameron Aavik

Reputation: 812

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?

Upvotes: 0

Views: 2101

Answers (2)

Zein Makki
Zein Makki

Reputation: 30042

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

  1. The location that is specified inside the DLL or the executable file.
  2. .pdb files that could be present in the same folder as the DLL or executable file.
  3. Any local symbol cache folders.

  4. Any network,internet, or local symbol servers and locations that are specified on, such as the Microsoft symbol server if enabled.

Upvotes: 2

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239764

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.

Upvotes: 2

Related Questions