Visual Studio 调试器如何在运行时加载程序集时知道源代码在哪里?
How does the Visual Studio debugger know where the source is when loading an assembly at runtime?
考虑以下代码:
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] });
}
此程序是在 .net core 1.1 应用程序中编译的,args
在其调试输出目录中包含 .net core 中通用 hello world 的 dll 的路径。
当我 运行 这个程序时,它从内存中的 dll 副本加载程序集,然后调用入口点。 Visual Studio 似乎成功识别出这个dll和我在解决方案中打开的hello world项目是一样的,我可以单步调试这个被调用的程序。
当我从程序集的内存副本加载时,VS 调试器如何知道在哪里可以找到源代码?
答案在此 MSD 页中:
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.
调试器搜索 .pdb 文件的位置
- 在 DLL 或可执行文件中指定的位置
文件。
- .pdb 文件可能存在于与 DLL 相同的文件夹中
或可执行文件。
任何本地符号缓存文件夹。
指定的任何网络、互联网或本地符号服务器和位置,例如 Microsoft 符号服务器(如果启用)。
首先它找到 .pdb
然后从那里它可以找到源文件。
它如何找到 .pdb
?来自 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.)
它还会在其他位置搜索 .pdb
,但当您尝试将程序集与其在磁盘上的文件分离时,这似乎是最有可能的来源。
考虑以下代码:
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] });
}
此程序是在 .net core 1.1 应用程序中编译的,args
在其调试输出目录中包含 .net core 中通用 hello world 的 dll 的路径。
当我 运行 这个程序时,它从内存中的 dll 副本加载程序集,然后调用入口点。 Visual Studio 似乎成功识别出这个dll和我在解决方案中打开的hello world项目是一样的,我可以单步调试这个被调用的程序。
当我从程序集的内存副本加载时,VS 调试器如何知道在哪里可以找到源代码?
答案在此 MSD 页中:
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.
调试器搜索 .pdb 文件的位置
- 在 DLL 或可执行文件中指定的位置 文件。
- .pdb 文件可能存在于与 DLL 相同的文件夹中 或可执行文件。
任何本地符号缓存文件夹。
指定的任何网络、互联网或本地符号服务器和位置,例如 Microsoft 符号服务器(如果启用)。
首先它找到 .pdb
然后从那里它可以找到源文件。
它如何找到 .pdb
?来自 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.)
它还会在其他位置搜索 .pdb
,但当您尝试将程序集与其在磁盘上的文件分离时,这似乎是最有可能的来源。