简体   繁体   English

如何在C#程序中使用SOS.dll进行自动调试?

[英]How can I use SOS.dll within a C# program for automated debugging purposes?

In order to track down memory leaks, I have been using Visual Studio and SOS.dll to provide dumps of the object heap, and to see what is GCRooting a particular object in place. 为了追踪内存泄漏,我一直在使用Visual Studio和SOS.dll来提供对象堆的转储,并查看GCR射击特定对象的原因。

I started automating this process using AutoIt to control my mouse and keyboard. 我开始使用AutoIt自动执行此过程来控制我的鼠标和键盘。 Here is an approximation of the first part of my script: 这是我脚本第一部分的近似值:

In Visual Studio, press F5 to start execution 在Visual Studio中,按F5开始执行
Let the program fully load 让程序完全加载
Switch to Visual Studio and break execution 切换到Visual Studio并中断执行

Switch focus to the immediate window 将焦点切换到即时窗口
Load SOS.dll 加载SOS.dll
Call !dumpheap -stat 打电话!dumpheap -stat
Copy the result of the dump to the clipboard 将转储结果复制到剪贴板
Paste in notepad and save as dump1.txt 粘贴在记事本中并保存为dump1.txt

Resume execution 继续执行
Perform the leaky action a number of times 多次执行泄漏操作
Switch to Visual Studio and break execution 切换到Visual Studio并中断执行

Switch focus to the immediate window 将焦点切换到即时窗口
Clear the output 清除输出
Call !dumpheap -stat 打电话!dumpheap -stat
Copy the result of the dump to the clipboard 将转储结果复制到剪贴板
Paste in notepad and save as dump2.txt 粘贴在记事本中并保存为dump2.txt

Run a Python script which compares the two dumps and locates leaked objects 运行Python脚本,比较两个转储并找到泄漏的对象

I have a few concerns with my current method, though. 不过,我对目前的方法有些担忧。 Not only am I dependent on an installation of Visual Studio, but while the script is running I cannot use the mouse or keyboard without throwing the automation out of sequence. 我不仅依赖于Visual Studio的安装,而且在脚本运行时,我不能使用鼠标或键盘而不会使自动化失序。

Is there a way to use SOS.dll within a C# program rather than Visual Studio? 有没有办法在C#程序而不是Visual Studio中使用SOS.dll? Ideally this solution would attach to a specified process and output the result of !dumpheap -stat to a text file without the need for automating Visual Studio. 理想情况下,此解决方案将附加到指定的进程,并将!dumpheap -stat的结果输出到文本文件,而无需自动化Visual Studio。

Below I will present some different approaches you can use to find your leaks. 下面我将介绍一些可用于查找泄漏的不同方法。

(there are commercial "leak" detectors available should you not wish to follow the DIY approach). (如果您不希望遵循DIY方法,可以使用商业“泄漏”探测器)。

Link to my answer detailing lots of debugging tools/resources 链接到我的回答详细说明了许多调试工具/资源

Load SOS.DLL into WinDBG 将SOS.DLL加载到WinDBG中

You actually don't need Visual Studio to use SOS.DLL....it's a debugger extension so can be used in WinDBG too. 实际上你不需要Visual Studio来使用SOS.DLL ....它是一个调试器扩展,所以也可以在WinDBG中使用。

You can get the WinDBG Debugger free as part of the Debugging Tools for Windows. 您可以免费获得WinDBG调试器作为Windows调试工具的一部分。

You could then write a WinDBG script to automate your "dumps". 然后,您可以编写WinDBG脚本来自动执行“转储”。

Also be aware, there are enhanced versions of SOS.DLL available...look for SOSEX, PSSCOR2 (for NET 2), and PSSCOR4 (for NET 4). 另外请注意,SOS.DLL的增强版本可用...寻找SOSEX,PSSCOR2(针对NET 2)和PSSCOR4(针对NET 4)。

CLR Profiling API CLR性能分析API

You can use the CLR Profiling API. 您可以使用CLR Profiling API。

There's an application called CLRProfiler which is an actual .NET profiler that makes use of it....so a good bit of code to see how it's used. 有一个名为CLRProfiler的应用程序,它是一个实际的.NET分析器,它使用它....所以有很多代码可以看看它是如何使用的。

One of the things CLR Profiler can do is allow heap dumps to be made and then compare those dumps made at different time periods, to find the differences...and thus help find leaks. CLR Profiler可以做的事情之一是允许进行堆转储,然后比较在不同时间段进行的转储,以找出差异......从而帮助发现泄漏。

You could provide similar functionality to your application when in a "test" mode (either embedded in the application, or as a companion utility to monitor it). 在“测试”模式下(嵌入在应用程序中,或作为监视它的配套实用程序),您可以为应用程序提供类似的功能。

Process Dumps 流程转储

Another technique you can use is to take periodic "dumps" of your process, which you can then analyze offline via WinDBG with SOS/SOSEX (or any other WinDBG plugin). 您可以使用的另一种技术是定期对您的进程进行“转储”,然后您可以通过WinDBG使用SOS / SOSEX(或任何其他WinDBG插件)离线分析。

You can use DebugDiag (you can set up rules and actions to control when .dmp files are created) to automate the creation of .dmp files....or a tool called ProcDump.exe (from SysInternals). 您可以使用DebugDiag(您可以设置规则和操作来控制何时创建.dmp文件)以自动创建.dmp文件....或者名为ProcDump.exe的工具(来自SysInternals)。

ProcDump is particularly useful because it is able to take the "process snapshot" very quickly because it uses a technique called "Process Reflection"....this enables your process to be interrupted less aggressively eg ProcDump特别有用,因为它能够非常快速地获取“过程快照”,因为它使用了一种称为“过程反射”的技术....这使得您的过程可以不那么积极地中断,例如

(create 3 dumps with 5 seconds gap between each one) (创建3个转储,每个转储间隔5秒)

procdump -s 5 -n 3 notepad.exe notepad.dmp 

Post-Mortem Analysis 死后分析

After you have a set of .dmp files you can then analyze them to find leaks, monitor handle usage, etc. 拥有一组.dmp文件后,您可以分析它们以查找泄漏,监视句柄使用情况等。

You can manually load the .dmp files in WinDBG and then issue debugger commands, or you could write a WinDBG extension or maybe a script which took the set of dump files and processed them in turn (ie ran the !dumpheap stat command on them). 您可以在WinDBG中手动加载.dmp文件然后发出调试器命令,或者您可以编写一个WinDBG扩展或者一个脚本来获取转储文件并依次处理它们(即对它们运行!dumpheap stat命令) 。

Debug Analyzer .NET Debug Analyzer .NET

This is a tool aimed at analyzing .dmp files and doing post-mortem analysis. 这是一个旨在分析.dmp文件和进行事后分析的工具。

It lets you write scripts in .NET so is maybe more accessible than WinDBG. 它允许您在.NET中编写脚本,因此可能比WinDBG更容易访问。

It also has a plugin model if you want to extend it. 如果你想扩展它,它还有一个插件模型。

And it lets your write/use Visualizers to view the data in unique ways. 它允许您编写/使用展示台以独特的方式查看数据。

(currently only supports CLR 2.0 memory dumps...not CLR 4.0) (目前仅支持CLR 2.0内存转储......不支持CLR 4.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM