1

当第一次发生 OutOfMemoryException 时尝试使用 DebugDiag 进行转储时,我遇到了一个问题。所以我写了一个应用程序,我可以用它来创建内存不足的情况,并按照以下说明进行操作:

http://blogs.msdn.com/b/kaushal/archive/2012/05/09/using-debugdiag-to-capture-a-dump-on-first-chance-exception.aspx

但我没有第一次转储,我只是获得第二次转储。当我查看来自 DebugDiag 的日志文件时,我得到以下信息:

[6/16/2013 9:54:04 PM] First chance exception - 0xe06d7363 caused by thread with  System ID: 4628
[6/16/2013 9:54:04 PM] First chance exception - 0xe0434352 caused by thread with  System ID: 4628
[6/16/2013 9:54:05 PM] Unable to determine CLR exception type

ExceptionObjHexAddr = 0x00000000`00000000

bInnerException = False

DumpObject Output = Invalid parameter 0x00000000`00000000


ChildEBP RetAddr  Args to Child              
002bdee4 6a44c93f e0434352 00000001 00000005 KERNELBASE!RaiseException+0x58
002bdf88 6a573b17 00000000 20b578f4 002be04c clr!RaiseTheExceptionInternalOnly+0x276
002bdfb8 6a5e5589 20b54734 002be090 00000000 clr!UnwindAndContinueRethrowHelperAfterCatch+0x83
002be058 003c0a3a 00000000 00000000 0233d174 clr!JIT_NewArr1+0x1af
...  removed some rows, lots of data ...


OS Thread Id: 0x1214 (0)
Child SP IP       Call Site
002bdfd4 7554c41f [Frame: 002bdfd4] 
002be060 003c0a3a 
...  removed some rows, lots of data ...


Error requesting GC Heap data
Unable to determine bounds of gc heap

然后后来我得到这个:

[6/16/2013 9:54:05 PM] CLR Exception Type - ''
[6/16/2013 9:54:05 PM] First chance exception - 0xe0434352 caused by thread with  System ID: 4628
[6/16/2013 9:54:05 PM] Unable to determine CLR exception type

最后我得到了

[6/16/2013 9:54:05 PM] CLR Exception Type - ''
[6/16/2013 9:54:05 PM] C:\Windows\Microsoft.NET\Framework\v4.0.30319\diasymreader.dll loaded at 0x615c0000
[6/16/2013 9:54:13 PM] Second chance exception - 0xe0434352 caused by thread with  System ID: 4628

貌似可以获取异常对象的地址,为0,所以脚本调用DumpObject时找不到异常信息。

我阅读这些日志条目的方式是我从 malloc 或其他东西中获得原生的第一次机会异常,然后跟进 OutOfMemoryException 的 CLR 异常。我试图弄清楚第二个第一次机会异常是什么,我的代码如下所示:

private void OnGrowMemoryCommand(int growMemorySize)
{
    try
    {
        _heldMemoryChunks.Add(new byte[growMemorySize * 1024 * 1024]);
    }
    catch (Exception)
    {
        throw;
    }
    TotalMemorySize += growMemorySize;
}

此代码是从 WPF 按钮上的命令触发的。因此,源自此代码的任何异常都应导致 TargetInvocationException,我认为这是第一次机会异常中的第二个。最后来自 throw 块的是第二次机会异常,其类型为 TargetInvocationException。

所以我开始查看第二次机会转储文件。我将它加载到 windbg 中,然后发出以下命令:

.symfix C:\symcache
.loadby sos clr
.reload

!pe
Exception object: 023caf9c
Exception type:   System.Reflection.TargetInvocationException
Message:          Exception has been thrown by the target of an invocation.
InnerException:   System.OutOfMemoryException, Use !PrintException 023c9928 to see more.

我可以看到,我的上述假设得到了第二次机会异常是 TargetInvocationException 这一事实的支持,但是为什么 DebugDiag 不能获取 CLR 异常类型呢?对于健全性检查,我尝试进行实时调试会话。所以我启动应用程序并附加,然后发出这些命令。

.symfix C:\symcache
.loadby sos clr
.reload

!threads
Failed to request ThreadStore

!dumpheap
The garbage collector data structures are not in a valid state for traversal.
It is either in the "plan phase," where objects are being moved around, or
we are at the initialization or shutdown of the gc heap. Commands related to 
displaying, finding or traversing objects as well as gc heap segments may not 
work properly. !dumpheap and !verifyheap may incorrectly complain of heap 
consistency errors.
Error requesting GC Heap data
Unable to build snapshot of the garbage collector state

这是完全软管。于是我开始研究这个问题。

此 url 表明它可能是多个 CLR 实例:

http://blogs.msdn.com/b/jjameson/archive/2011/01/11/issues-debugging-managed-code-in-windbg-with-sos-and-psscor2-eg-quot-failed-to-请求-threadstore-quot.aspx

所以我发出这些命令:

.cordll
CLR DLL status: Loaded DLL C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscordacwks.dll

这对我来说很奇怪,我认为从 4.0 开始,mscorwks 就被 clr 抛弃了。mscordacwks 是 4.5 吗?

我发出了这个命令:

lmvm mscordacwks

但是 clr 已加载:

lmvm clr
start    end        module name
6a350000 6a9e2000   clr        (pdb symbols)          C:\symcache\clr.pdb\97FD69E1786F42F9A541C81D81AC96852\clr.pdb
    Loaded symbol image file: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Image path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Image name: clr.dll
    Timestamp:        Fri Mar 29 00:13:44 2013 (51553118)
    CheckSum:         0069496E
    ImageSize:        00692000
    File version:     4.0.30319.18047
    Product version:  4.0.30319.18047
    File flags:       8 (Mask 3F) Private
    File OS:          4 Unknown Win32
    File type:        2.0 Dll
    File date:        00000000.00000000
    Translations:     0409.04b0
    CompanyName:      Microsoft Corporation
    ProductName:      Microsoft® .NET Framework
    InternalName:     clr.dll
    OriginalFilename: clr.dll
    ProductVersion:   4.0.30319.18047
    FileVersion:      4.0.30319.18047 built by: FX45RTMGDR
    PrivateBuild:     DDBLD316
    FileDescription:  Microsoft .NET Runtime Common Language Runtime - WorkStation
    LegalCopyright:   © Microsoft Corporation.  All rights reserved.
    Comments:         Flavor=Retail

所以我认为我没有加载多个 CLR。

因此,我假设导致我的实时调试问题的同一件事是导致我未能在第一次机会问题上触发。有任何想法吗?

4

1 回答 1

2

我在两台机器上遇到了同样的错误。两者都配备了 .NET 4.5,所以我认为这就是它不起作用的原因。

原来是 DebugDiag Scripts 中的一个错误。它无法在其中一个脚本中检索 CLR 异常的名称。但是,这个问题是可以修复的,因为每个 DebugDiag 规则都会创建一个可以修改的 vbs 脚本。它使用完全相同的命令,您也可以在 WinDbg 中使用。以下是解决方法:

  • 使用 GUI 创建您的 DebugDiag 规则,就像您已经做的那样。
  • 对于每条规则,都会生成一个 vbs 脚本文件。在文本编辑器中打开它C:\Program Files\DebugDiag\Scripts\CrashRule_<rulename>.vbs
  • 查找功能GetCLRExceptionType
  • 修改如下:

    Function GetCLRExceptionType(ByVal ExceptionObjHexAddr, ByVal bInnerException)
        Dim Output, Lines, i
    
        If Debugger.IsClrExtensionMissing Then
            WriteToLog "Unable to determine CLR exception type - extension dll could not be loaded."
        Else
            ' Output = Debugger.Execute("!DumpObj " & ExceptionObjHexAddr) ' Does not work in .NET 4.5
            Output = Debugger.Execute("!pe") ' FIX .NET45
    
            Lines = Split(Output, Chr(10))
            For i = 0 To UBound(Lines)      
                If bInnerException Then
                    If InStr(Lines(i), "_innerException") <> 0 Then
                        Tokens = Split(Lines(i), " ")
                        For j = 0 To UBound(Tokens)
                            If Len(Tokens(j)) = 8 Then                              
                                GetCLRExceptionType = GetCLRExceptionType(Tokens(j), False)
                                Exit For
                            End If
                        Next
                    End If
                ElseIf Len(Lines(i)) >= 7 Then
                    If InStr(Lines(i), "Exception type:") = 1 Then  ' FIX .NET45
                        GetCLRExceptionType = Trim(Mid(Lines(i), 16))  ' FIX .NET45
                        Exit For
                    End If
                End If
            Next
    
            If GetCLRExceptionType = "" Then
                If g_ClrExceptionTypeFailureLogCount < MAX_CLR_EXCEPTION_TYPE_FAILURE_LOG_ENTRIES Then
                    g_ClrExceptionTypeFailureLogCount = g_ClrExceptionTypeFailureLogCount + 1
    
                    WriteToLog "Unable to determine CLR exception type" & vbcrlf & _
                        "ExceptionObjHexAddr = " & ExceptionObjHexAddr & vbcrlf & _
                        "bInnerException = " & bInnerException & vbcrlf & _
                        "DumpObject Output = " & Output & vbcrlf & _
                        Debugger.Execute("kb100") & vbcrlf & _
                        Debugger.Execute("!clrstack") & vbcrlf & _
                        Debugger.Execute("!dso")
                End If
            End If
        End If
    End Function
    

默认情况下,!DumpObj用于转储异常对象的内容。但是,在 .NET 4.5 中发生了一些变化,显然,无法再提取异常的类型。相反,将!pe命令放在此处并从其结果中解析异常类型。

为了!pe工作,你需要做.loadby sos clr. 我将它添加到 Sub Debugger_OnLoadModule

Sub Debugger_OnLoadModule(ByVal NewModule)
    WriteToLog NewModule.ImageName & " loaded at " & Debugger.GetAs32BitHexString(NewModule.Base)
    Select Case UCase(NewModule.ModuleName)
        Case "MSCORWKS", "MSCORSVR", "CLR", "CORECLR"
            UpdateDeferredManagedBreakpoints
    End Select

    Debugger.Execute(".loadby sos clr")
End Sub

进行更改后,您需要重新启动进程或停用/重新激活规则以应用它。

另外:请注意,如果您在 DebugDiag GUI 中修改规则,您的更改将被完全覆盖。

于 2013-09-23T20:33:13.753 回答