如何使用Visual Studio调试和分析任何EXE
In Visual Studio you can open any EXE as a ‘project’. Just go to File->Open->Project/Solution and browse to the .exe file. Like you would if it was a .sln file. Visual Studio will then open that EXE as a project. This feature has been around for a long time. It works on all currently supported Visual Studio versions and the docs for it are at ‘Debug an app that isn’t part of a Visual Studio solution‘.
在Visual Studio中,您可以将任何EXE作为“项目”打开。 只需转到文件->打开->项目/解决方案,然后浏览到.exe文件。 就像您是.sln文件一样。 然后,Visual Studio将打开该EXE作为项目。 此功能已经存在很长时间了。 它适用于当前所有受支持的Visual Studio版本,其文档位于“ 调试不属于Visual Studio解决方案的应用程序 ”中。
This article in our blog.
本文在我们的博客中 。
调试 (Debugging)
Just as with a normal project you can start debugging with F5, which will launch the EXE and attach the debugger. If you want to debug startup you can launch with F11, which will launch the EXE and stop on the first line of user code. Both of these options are available on the context menu for the EXE project in Solution Explorer window as illustrated below:
与普通项目一样,您可以使用F5开始调试,这将启动EXE并附加调试器。 如果要调试启动,可以使用F11启动,它将启动EXE并在用户代码的第一行停止。 这两个选项都在“ 解决方案资源管理器”窗口中的EXE项目的上下文菜单中可用,如下所示:
For debugging will need to have symbols, PDB files, for the EXE and any DLLs you need to debug. Visual Studio will follow the same process to try to obtain symbols as it does when debugging a normal project. Since it’s not likely that the PDB files were distributed alongside the EXE you might want to locate them from a build drop or, better yet, from a symbol server. More information and best practices for symbols can be found in this blog.
调试时,将需要符号,PDB文件,EXE和您需要调试的所有DLL。 Visual Studio将按照与调试普通项目时相同的过程来尝试获取符号。 由于PDB文件不太可能与EXE一起分发,因此您可能希望从构建位置或更好的是从符号服务器中找到它们。 有关符号的更多信息和最佳做法,请参见此博客 。
To effectively debug you’ll also need the source code that was used to build the EXE, even for just a few files that you care about. You’ll need to locate those files and open them in Visual Studio. If the source code isn’t the exact same as the source code that was built the EXE Visual Studio will warn you when you try to insert a breakpoint and the breakpoint won’t bind. That behavior can be overridden from the Breakpoint Settings peek window. In the settings peek window click on the Must match source link text and then check the box to allow mismatched source, as illustrated below. Of course, with mismatched source you never really know what’s going to happen, so use at your peril.
为了有效地调试,您甚至还需要用于构建EXE的源代码,即使您只关心几个文件也是如此。 您需要找到这些文件并在Visual Studio中打开它们。 如果源代码与生成的源代码不完全相同,则当您尝试插入断点时,EXE Visual Studio会警告您,并且断点不会绑定。 可以从“ 断点设置”监视窗口覆盖该行为。 在设置查看窗口中,单击“ 必须匹配源”链接文本,然后选中复选框以允许源不匹配,如下所示。 当然,对于不匹配的源,您永远不会真正知道会发生什么,因此要冒风险。
If the EXE was built with SourceLink enabled then information about the source will be included in the PDBs and Visual Studio will try to download the source automatically. This is a really good reason to use SourceLink with your projects. Even if you have a local enlistment you might not have the same version that was used to build the binary. SourceLink is your sure-fire way to make sure that the right source is linked with the right binary.
如果EXE是在启用SourceLink的情况下构建的,则有关源的信息将包含在PDB中,Visual Studio将尝试自动下载源。 这是在项目中使用SourceLink的确实很好的理由。 即使您有本地征募,也可能没有用于构建二进制文件的相同版本。 SourceLink是确保正确的源与正确的二进制文件链接的可靠方法。
If you can’t obtain any source code you still have a couple of options:
如果您无法获得任何源代码,您仍然可以选择以下两种方法:
-
Use a tool to decompile the assemblies back into C#, which you can recompile into new assemble to patch the old one.
使用一种工具将程序集反编译回C#,您可以将其重新编译为新的程序集以修补旧的程序集。
-
ILSpy is a great choice for this, but there’s plenty of other good paid and free tools out there.
ILSpy是一个不错的选择,但是还有许多其他付费和免费工具。
Use a tool to decompile the assemblies back into C#, which you can recompile into new assemble to patch the old one.
使用一种工具将程序集反编译回C#,您可以将其重新编译为新的程序集以修补旧的程序集。
-
-
Use the Disassembly tool window in Visual Studio.
使用Visual Studio中的反汇编工具窗口。
-
The Source Not Found document has a link to view disassembly. Be warned, if you’re use to debugging C# code the disassembly view is a tool of last resort.
“ 找不到源文件”文档具有查看反汇编的链接。 请注意,如果您习惯于调试C#代码,则反汇编视图是不得已的工具。
Use the Disassembly tool window in Visual Studio.
使用Visual Studio中的反汇编工具窗口。
-
Lastly, if you need to pass in any arguments to the EXE that’s being debugged you can configure them along with other options in the Project Properties page (Right Click->Properties on the project node in solution explorer).
最后,如果需要将任何参数传递给正在调试的EXE,则可以在“ 项目属性”页面(在解决方案资源管理器中 ,在项目节点上右键单击->属性)中配置它们以及其他选项。
剖析 (Profiling)
You can also use the profiling tools with the EXE by launching them from the Debug -> Performance Profiling. From the launch page of the profiling tools you can select what tools to use against the EXE. More information on profiling can be found in this docs (https://docs.microsoft.com/en-us/visualstudio/profiling/profiling-feature-tour?view=vs-2019).
您还可以通过从Debug-> Performance Profiling启动它们来与EXE一起使用分析工具。 在性能分析工具的启动页面中,您可以选择针对EXE使用哪些工具。 可以在此文档( https://docs.microsoft.com/zh-cn/visualstudio/profiling/profiling-feature-tour?view=vs-2019 )中找到有关分析的更多信息。
结论 (Conclusion)
That’s it. A brief overview of how you can use Visual Studio to debug and profile applications that you aren’t building and might not even have source for. So, next time you need to debug or profile an EXE don’t forget you can open it as a Solution in Visual Studio!
而已。 简要概述如何使用Visual Studio调试和分析未构建的应用程序,甚至可能没有源代码。 因此,下次您需要调试或分析EXE时,不要忘了可以在Visual Studio中将其作为解决方案打开!