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中的代码?
8 个解决方案
#1
16
If the DLL is in a .NET language, you can decompile it using a tool like .NET Reflector and 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.
或者您可以询问供应商是否有源代码。这可能是最简单的方法。
#2
5
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的答案的基础上,您只需将反编译的源代码视为项目中的新库,并在源代码中设置断点。删除对第三方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
3
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)从使用项目访问DLL项目。这涉及在Visual Studio的单独实例中构建DLL,然后通过Visual Studio中的不同项目访问DLL(假设您拥有源代码)。有很多方法可以实现这个目标:
- You can add
Trace.WriteLine
statements 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).
您可以在DLL中添加Trace.WriteLine语句,这些语句将显示在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中相同的禁用)。
#4
3
Something that has worked for me with debugging a couple of third-party libraries as well as .NET itself 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/CIL instructions 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)扩展。它也可以调试本机。您需要了解一些关于callstacks和assembly / CIL指令的信息,以便善于使用它。您应该能够确定异常以及导致异常的原因。我们使用WinDbg / SOS来查找例如在HttpWebResponse中,如果您使用Gzip压缩下载页面并且服务器返回错误的Gzip标头,.NET会在线程池中运行解压缩,崩溃将取消您的进程。快乐的调试。
#5
1
As Cesar Reyes mentioned in Stack Overflow question Visual Studio - Attach source code to reference, ReSharper 5 (and later) has this capability.
正如Cesar Reyes在Stack Overflow问题中提到的Visual Studio - 附加源代码以供参考,ReSharper 5(及更高版本)具有此功能。
#6
0
.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 Addin,允许您在没有源代码的程序集上使用Visual Studio的逐步调试。
Have a look at this blog post:
看看这篇博文:
http://www.simple-talk.com/community/blogs/alex/archive/2009/09/22/74919.aspx for 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
#7
0
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调试器。它可能对你有很大的帮助。
#8
0
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-访问程序
#1
16
If the DLL is in a .NET language, you can decompile it using a tool like .NET Reflector and 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.
或者您可以询问供应商是否有源代码。这可能是最简单的方法。
#2
5
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的答案的基础上,您只需将反编译的源代码视为项目中的新库,并在源代码中设置断点。删除对第三方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
3
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)从使用项目访问DLL项目。这涉及在Visual Studio的单独实例中构建DLL,然后通过Visual Studio中的不同项目访问DLL(假设您拥有源代码)。有很多方法可以实现这个目标:
- You can add
Trace.WriteLine
statements 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).
您可以在DLL中添加Trace.WriteLine语句,这些语句将显示在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中相同的禁用)。
#4
3
Something that has worked for me with debugging a couple of third-party libraries as well as .NET itself 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/CIL instructions 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)扩展。它也可以调试本机。您需要了解一些关于callstacks和assembly / CIL指令的信息,以便善于使用它。您应该能够确定异常以及导致异常的原因。我们使用WinDbg / SOS来查找例如在HttpWebResponse中,如果您使用Gzip压缩下载页面并且服务器返回错误的Gzip标头,.NET会在线程池中运行解压缩,崩溃将取消您的进程。快乐的调试。
#5
1
As Cesar Reyes mentioned in Stack Overflow question Visual Studio - Attach source code to reference, ReSharper 5 (and later) has this capability.
正如Cesar Reyes在Stack Overflow问题中提到的Visual Studio - 附加源代码以供参考,ReSharper 5(及更高版本)具有此功能。
#6
0
.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 Addin,允许您在没有源代码的程序集上使用Visual Studio的逐步调试。
Have a look at this blog post:
看看这篇博文:
http://www.simple-talk.com/community/blogs/alex/archive/2009/09/22/74919.aspx for 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
#7
0
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调试器。它可能对你有很大的帮助。
#8
0
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-访问程序