C# 如何调试引用的 dll(有 pdb)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1130417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:55:19  来源:igfitidea点击:

How to debug a referenced dll (having pdb)

c#visual-studiovisual-studio-2008visual-studio-2005

提问by Elad

I have two solutions in my workspace, say A and B.

我的工作区中有两个解决方案,比如 A 和 B。

Solution A is an older project which I finished coding some time ago. In solution B, I need to use some classes from Solution A. To do so, I add a reference to the dll of one of the projects in solution A.

解决方案 A 是一个较旧的项目,我前段时间完成了编码。在解决方案 B 中,我需要使用解决方案 A 中的一些类。为此,我添加了对解决方案 A 中某个项目的 dll 的引用。

The problem is when I try to debug. I want to be able to step into A's code as well. Visual studio is not able to load the code for these classes ("There is no source code available for the current location.") and I can only view the disassembly, which is not useful.

问题是当我尝试调试时。我也希望能够进入 A 的代码。Visual Studio 无法加载这些类的代码(“当前位置没有可用的源代码。”),我只能查看反汇编,这没有用。

The only way I know to debug classes from solution A is by running solution B, detach all processes (in the Debug menu item) and attach the process from solution A.

我知道从解决方案 A 调试类的唯一方法是运行解决方案 B,分离所有进程(在调试菜单项中)并从解决方案 A 附加进程。

However, this is very inconvenient and I can only debug A OR B at once.

但是,这很不方便,我只能一次调试 A 或 B。

Is there a way to allow stepping into the code of referenced dlls (for which I do have the source code)?

有没有办法允许进入引用的 dll 的代码(我有源代码)?



Solution:My mistake was that I thought that a project can only be part of a single solution. In fact, a project can be part of any number of solutions.
When you need to reference the old project, you should simply add the project to the solution. This is done by right clicking the new solution in the Solution Explorer > Add > Existing Project.
Then, you'll be able to add the project reference. As others wrote, you should probably completely avoid using dll references to your own code (or other code you might need to change and debug).

解决方案:我的错误是我认为一个项目只能是一个解决方案的一部分。事实上,一个项目可以是任意数量解决方案的一部分。
当您需要引用旧项目时,您只需将该项目添加到解决方案中即可。这是通过在解决方案资源管理器 > 添加 > 现有项目中右键单击新解决方案来完成的。
然后,您将能够添加项目引用。正如其他人所写,您可能应该完全避免使用对您自己的代码(或您可能需要更改和调试的其他代码)的 dll 引用。

A very good reference to how solutions should be designed can be found in MSDN.

可以在MSDN 中找到有关如何设计解决方案的非常好的参考资料。

采纳答案by Marc Gravell

If you have a projectreference, it should work immediately.

如果您有项目参考,它应该立即生效。

If it is a file(dll) reference, you need the debugging symbols (the "pdb" file) to be in the same folder as the dll. Check that your projects are generating debug symbols (project properties => Build => Advanced => Output / Debug Info = full); and if you have copiedthe dll, put the pdb with it.

如果是文件(dll) 引用,则需要调试符号(“pdb”文件)与 dll 位于同一文件夹中。检查您的项目是否正在生成调试符号(项目属性 => 构建 => 高级 => 输出/调试信息 = 完整);如果您已经复制了 dll,请将 pdb 放在一起。

You can also load symbols directly in the IDE if you don't want to copy any files, but it is more work.

如果您不想复制任何文件,也可以直接在 IDE 中加载符号,但这样做的工作量更大。

The easiest option is to use project references!

最简单的选择是使用项目引用!

回答by Matthieu

It must work. I used to debug a .exe file and a dll at the same time ! What I suggest is 1) Include the path of the dll in your B project, 2) Then compile in debug your A project 3) Control that the path points on the A dll and de pdb file.... 4)After that you start in debug the B project and if all is ok, you will be able to debug in both projects !

它必须工作。我曾经同时调试一个.exe文件和一个dll!我的建议是1)在你的B项目中包含dll的路径,2)然后在调试你的A项目时编译3)控制路径指向A dll和de pdb文件...... 4)之后你开始调试 B 项目,如果一切正常,您将能够在两个项目中进行调试!

回答by KevinHou

Another point to keep in mind, be sure the referenced dlls are not installed in the GAC. After testing, I installed my dlls into the GAC to do system level testing. Later, when I had to debug my code again, I couldn't step into the referenced assemblies until I deleted them from the GAC.

要记住的另一点是,确保引用的 dll 未安装在 GAC 中。测试后,我将我的 dll 安装到 GAC 中进行系统级测试。后来,当我不得不再次调试我的代码时,我无法进入引用的程序集,直到我从 GAC 中删除它们。

回答by Jeff LaFay

I don't want to include an external class library project in some of my solutions, so I step into assemblies that I consume in a different way.

我不想在我的一些解决方案中包含外部类库项目,所以我进入我以不同方式使用的程序集。

My solutions have a "Common Assemblies" directory that contains my own DLLs from other projects. The DLLs that I reference also have their accompanying PDB files for debugging.

我的解决方案有一个“通用程序集”目录,其中包含我自己的来自其他项目的 DLL。我引用的 DLL 也有其随附的 PDB 文件用于调试。

In order to debug and set breakpoints, I set a breakpoint in the consuming application's source where I'm calling a method or constructor from the assembly and then step INTO (F11) the method/constructor call.

为了调试和设置断点,我在消费应用程序的源代码中设置了一个断点,我正在从程序集中调用方法或构造函数,然后执行 INTO (F11) 方法/构造函数调用。

The debugger will load the assembly's source file in VS and new breakpoints inside of the assembly can be set at that point.

调试器将在 VS 中加载程序集的源文件,并且可以在该点设置程序集内的新断点。

It's not straight forward but works if you don't want to include a new project reference and simply want to reference a shared assembly instead.

它不是直截了当的,但如果您不想包含新的项目引用而只想引用共享程序集,则可以使用。

回答by scott_f

I had the same issue. He is what I found:

我遇到过同样的问题。他是我发现的:

1) make sure all projects are using the same Framework (this is crucial!)

1)确保所有项目都使用相同的框架(这很关键!)

2) in Tools/Options>Debugging>General make sure "Enable Just My Code (Managed Only) is NOT ticked

2)在工具/选项>调试>常规中确保“仅启用我的代码(仅限托管)未勾选

3) in Tools/Options>Debugging>Symbols clear any cached symbols, untick and delete all folder locations under the "Symbols file (.pdb) locations" listbox except the default "Microsoft Symbol Servers" but still untick it too. Also delete any static paths in the "Cache symbols in this directory" textbox. Click the "Empty Symbols Cache" button. Finally make sure the "Only specified modules" radio button is ticked.

3)在工具/选项>调试>符号中清除所有缓存的符号,取消勾选并删除“符号文件(.pdb)位置”列表框下的所有文件夹位置,除了默认的“Microsoft符号服务器”,但仍然取消勾选它。还要删除“在此目录中缓存符号”文本框中的所有静态路径。单击“空符号缓存”按钮。最后确保勾选“仅指定模块”单选按钮。

4) in the Build/Configuration Manager menu for all projects make sure the configuration is in Debug mode.

4) 在所有项目的 Build/Configuration Manager 菜单中确保配置处于 Debug 模式。

回答by Carl in 't Veld

When you want to set a breakpoint in source code of a referenced dll, first make sure that you have a pdb file available for it. Then you can just open the related source code file and set a breakpoint over there. The source file does not need to be part of your solution. As explained in How can I set a breakpoint in referenced code in Visual Studio?

当您想在引用的 dll 的源代码中设置断点时,首先确保您有一个可用的 pdb 文件。然后你就可以打开相关的源代码文件并在那里设置一个断点。源文件不需要成为解决方案的一部分。如如何在 Visual Studio 中的引用代码中设置断点中所述?

You can review your breakpoints through the breakpoints window, available via Debug -> Windows -> Breakpoints.

您可以通过断点窗口查看您的断点,可通过调试 -> 窗口 -> 断点访问。

This approach has the benefit that you are not required to add an existing project to your solution just for debugging purposes as leaving it out has saved me a lot of build time. Evidently, building a solution with only one project in it is much faster than building a solution with lots of them.

这种方法的好处是您不需要为了调试目的而将现有项目添加到您的解决方案中,因为省略它为我节省了大量构建时间。显然,构建一个只有一个项目的解决方案比构建一个包含大量项目的解决方案要快得多。

回答by Arindam Dhar

Step 1:Go to Tools-->Option-->Debugging

步骤1:转到工具-->选项-->调试

Step 2:Uncheck Enable Just My Code

第 2 步:取消选中仅启用我的代码

Step 3:Uncheck Require source file exactly match with original Version

步骤 3:取消选中要求源文件与原始版本完全匹配

Step 4:Uncheck Step over Properties and Operators

第 4 步:取消选中 Step over Properties 和 Operators

回答by Roald

I had the *.pdbfiles in the same folder and used the options from Arindam, but it still didn't work. Turns out I needed to enable Enable native code debuggingwhich can be found under Project properties > Debug.

我将这些*.pdb文件放在同一个文件夹中,并使用了Arindam 中的选项,但它仍然不起作用。原来我需要启用Enable native code debugging,它可以在Project properties > Debug下找到。

回答by Guillermo Prandi

Make sure your DLL is not registered in the GAC. Visual Studio will use the version in the GAC and it will probably have no debugging information.

确保您的 DLL 未在 GAC 中注册。Visual Studio 将使用 GAC 中的版本,它可能没有调试信息。

回答by magicode118

The most straigh forward way I found using VisualStudio 2019 to debug an external library to which you are referencing in NuGet, is by taking the following steps:

我发现使用 VisualStudio 2019 调试您在 NuGet 中引用的外部库的最直接方法是执行以下步骤:

  1. Tools > Options > Debugging > General > Untick 'Enable Just My Code'

  2. Go to Assembly Explorer > Open from NuGet Packages Cache List item

  3. Type the NuGet package name you want to debug in the search field & click 'OK' enter image description here

  4. From the Assembly Explorer, right-click on the assembly imported and select 'Generate Pdb' enter image description here

  5. Select a custom path where you want to save the .PDB file and the framework you want this to be generated for

    enter image description here

  6. Copy the .PDB file from the folder generated to your Debug folder and you can now set breakpoints on this assembly's library code

  1. 工具 > 选项 > 调试 > 常规 > 取消勾选“仅启用我的代码”

  2. 转到程序集资源管理器 > 从 NuGet 包缓存打开 项目清单

  3. 在搜索字段中输入要调试的 NuGet 包名称并单击“确定” 在此处输入图片说明

  4. 在程序集资源管理器中,右键单击导入的程序集并选择“生成 Pdb” 在此处输入图片说明

  5. 选择要保存 .PDB 文件的自定义路径以及要为其生成的框架

    在此处输入图片说明

  6. 将 .PDB 文件从生成的文件夹复制到您的 Debug 文件夹,您现在可以在此程序集的库代码上设置断点