C# 在 Visual Studio 中调试第三方 DLL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/349918/
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-04 00:07:04  来源:igfitidea点击:

Debugging a third-party DLL in Visual Studio?

c#.netdebuggingdll

提问by Biswanath

I am using a third-party DLL. For some particular cases, a function in the DLL is throwing an exception. Is it possible to debug the DLL in the Visual Studio?

我正在使用第三方 DLL。对于某些特定情况,DLL 中的函数会引发异常。是否可以在 Visual Studio 中调试 DLL?

After the answer from Andrew Rollings, I am able to view the code, but is there any easy way to debug through the code in Visual Studio?

Andrew Rollings 的回答之后,我可以查看代码,但是有没有什么简单的方法可以在 Visual Studio 中调试代码?

采纳答案by Andrew Rollings

If the DLL is in a .NETlanguage, you can decompile it using a tool like .NET Reflectorand then debug against the source code.

如果 DLL 是.NET语言,则可以使用.NET Reflector 之类的工具对其进行反编译,然后针对源代码进行调试。

Or you could ask the vendor if source code is available. That's probably the easiest way.

或者您可以询问供应商是否提供源代码。这可能是最简单的方法。

回答by Brian Lyttle

Building on Andrew's answer, you just treat the decompiled source code as a new library within your project and set breakpoints in the source. Remove all references to the 3rd party DLL so that it is the decompiled code that is executing.

基于 Andrew 的回答,您只需将反编译的源代码视为项目中的新库,并在源代码中设置断点。删除对第 3 方 DLL 的所有引用,以便它是正在执行的反编译代码。

Other things:

其他事情:

  • You may be breaking the law by decompiling the code, or breaching a licensing agreement with the 3rd party vendor. Make sure to review this with someone.
  • You will want to make sure that you remove references to your decompiled version if you are shipping to other developers, or checking into a larger source tree. Easy to forget this!
  • 您可能通过反编译代码或违反与第 3 方供应商的许可协议来违反法律。请务必与某人一起查看此内容。
  • 如果您要发送给其他开发人员或签入更大的源代码树,您将需要确保删除对反编译版本的引用。很容易忘记这个!

回答by dr. evil

I thought .NET Reflector got some debugging plugins. That'd be a so much better idea because decompiling and recompiling code generally fails, and you need to do so many changes in the code to fix it.

我以为 .NET Reflector 有一些调试插件。这是一个更好的主意,因为反编译和重新编译代码通常会失败,您需要对代码进行大量更改才能修复它。

Give .NET Reflector debugger a try. It might help you a lot.

试试 .NET Reflector 调试器。它可能对你有很大帮助。

回答by Robert Gowland

There are two methods I've come across:

我遇到过两种方法:

1) Accessing the DLL project from the using project.This involves building the DLL in a separate instance of Visual Studio and then accessing the DLL through a different project in Visual Studio (this assumes you have the source code). There a number of ways to accomplish this:

1) 从 using 项目访问 DLL 项目。这涉及在单独的 Visual Studio 实例中构建 DLL,然后通过 Visual Studio 中的不同项目访问 DLL(假设您拥有源代码)。有多种方法可以实现这一点:

  • You can add Trace.WriteLinestatements in the DLL that will show up in the 'Output' window in Visual Studio.
  • You can add System.Diagnostics.Debugger.Break()statements to the DLL code. When running the calling project in Visual Studio, program execution will stop there. From here you can add access the call stack (including all function calls in DLL itself) and set break points (although the icon for the breakpoint will appear disabled and the hover text for the break point will read "The breakpoint will not currently be hit. No symbols have been loaded for this document").
  • If the DLL is throwing an exception (which you can see from the 'Output' window if the exception is caught and handled by the DLL) you can tell Visual Studio to always break when that type of exception is thrown. Hit Ctrl+ Alt+ E, find the type of exception being thrown, and click the 'Throw' column for that exception. From here it is exactly as if you had used System.Diagnostics.Debugger.Break()(see above).
  • 您可以Trace.WriteLine在 DLL 中添加语句,这些语句将显示在 Visual Studio 的“输出”窗口中。
  • 您可以System.Diagnostics.Debugger.Break()向 DLL 代码添加语句。在 Visual Studio 中运行调用项目时,程序执行将停止。从这里您可以添加对调用堆栈的访问(包括 DLL 本身中的所有函数调用)并设置断点(尽管断点的图标将显示为禁用状态,并且断点的悬停文本将显示为“当前不会命中断点. 尚未为该文档加载任何符号")。
  • 如果 DLL 抛出异常(如果异常被 DLL 捕获和处理,您可以从“输出”窗口中看到),您可以告诉 Visual Studio 在抛出该类型的异常时始终中断。点击Ctrl+ Alt+ E,找到抛出的异常类型,然后单击该异常的“抛出”列。从这里开始,就好像您已经使用过一样 System.Diagnostics.Debugger.Break()(见上文)。

2) Attaching a using process to the DLL project.This involved hooking the Visual Studio debugger into a running process.

2) 将使用过程附加到 DLL 项目中。这涉及将 Visual Studio 调试器挂接到正在运行的进程中。

  • Open the DLL project in Visual Studio.
  • Run an application that uses the DLL (this application can't be run from another instance of Visual Studio since the process will already have a debugger attached to it).
  • From here you can add break points and step through the DLL code loaded in Visual Studio (although the break point will appear disabled the same as in method 1).
  • 在 Visual Studio 中打开 DLL 项目。
  • 运行一个使用 DLL 的应用程序(该应用程序不能从 Visual Studio 的另一个实例运行,因为该进程已经附加了一个调试器)。
  • 从这里您可以添加断点并逐步执行在 Visual Studio 中加载的 DLL 代码(尽管断点将显示为与方法 1 中相同的禁用状态)。

回答by Steve Severance

Something that has worked for me with debugging a couple of third-party libraries as well as .NETitself is WinDbg. It is an awesome debugger from Microsoft that I have used to troubleshoot some sticky problems that were occuring deep inside the framework.

在调试几个第三方库以及.NET本身时对我有用的东西是WinDbg。这是一个来自微软的很棒的调试器,我用来解决框架内部发生的一些棘手的问题。

You need to use the Son of Strike(SOS) extensions if it is a managed DLL. It can debug native also. You will need to know a bit about callstacks and assembly/CILinstructions to be good at using it. You should be able to determine the exception and what is causing it. We have used WinDbg/SOS to find for instance that in HttpWebResponse, if you are using Gzip compression to download a page and the server returns a bad Gzip header, .NET runs the decompression in the threadpool and a crash will take out your process. Happy debugging.

如果它是托管 DLL,则需要使用Son of Strike(SOS) 扩展。它也可以调试本机。您需要了解一些有关调用堆栈和汇编/ CIL指令的知识才能熟练使用它。您应该能够确定异常以及导致异常的原因。我们已经使用 WinDbg/SOS 来发现例如在 HttpWebResponse 中,如果您使用 Gzip 压缩下载页面并且服务器返回错误的 Gzip 标头,.NET 在线程池中运行解压缩,并且崩溃将取消您的进程。调试愉快。

回答by Jeeva Subburaj

.NET Reflector 6 comes with a Visual Studio Addin that lets you use Visual Studio's step-through-debugging on assemblies that you don't have the source code for.

.NET Reflector 6 附带一个 Visual Studio 插件,它允许您在没有源代码的程序集上使用 Visual Studio 的逐步调试。

Have a look at this blog post:

看看这篇博文:

http://www.simple-talk.com/community/blogs/alex/archive/2009/09/22/74919.aspxfor more details.

http://www.simple-talk.com/community/blogs/alex/archive/2009/09/22/74919.aspx了解更多详情。

This is still a very early build. So no guarantee that it'll work, and it might break your visual studio configuration or project configuration. Make sure you have backups (or source control) for any projects you use this on.

这仍然是一个非常早期的构建。所以不能保证它会工作,它可能会破坏你的 Visual Studio 配置或项目配置。确保您对使用它的任何项目都有备份(或源代码控制)。

Download here: http://www.red-gate.com/MessageBoard/viewforum.php?f=109

在这里下载:http: //www.red-gate.com/MessageBoard/viewforum.php?f=109

回答by alan

As Cesar Reyes mentioned in Stack Overflow question Visual Studio - Attach source code to reference, ReSharper5 (and later) has this capability.

正如 Cesar Reyes 在 Stack Overflow 问题Visual Studio - Attach source code to reference 中提到的那样ReSharper5(及更高版本)具有此功能。

回答by Shkredov S.

One more option we should mention here is dotPeek 1.2 (a free decompiler from creators of ReSharper). Here is a nice post describing how to configure VS symbol server and dotPeek 1.2 to debug decompiled code from VisualStudio: http://blog.jetbrains.com/dotnet/2014/04/09/introducing-dotpeek-1-2-early-access-program

我们应该在这里提到的另一个选项是 dotPeek 1.2(来自 ReSharper 创建者的免费反编译器)。这是一篇很好的文章,描述了如何配置 VS 符号服务器和 dotPeek 1.2 以调试来自 VisualStudio 的反编译代码:http://blog.jetbrains.com/dotnet/2014/04/09/introducing-dotpeek-1-2-early-访问程序