获取堆中的异常堆栈跟踪

时间:2016-07-18 13:52:10

标签: .net windbg crash-dumps

我使用WinDbg,我想得到异常细节。在!dumpheap -type Exception命令的帮助下获取转储中的异常列表但是如何访问这些异常详细信息?

000007fef84e1298        1          160 System.StackOverflowException
000007fef84e1220        1          160 System.OutOfMemoryException

000007fef84e1388        2          320 System.Threading.ThreadAbortException
000007fef84e1038        2          320 System.Exception
000007fef84ec220        6          384 System.UnhandledExceptionEventHandler
000007fef746ea90       10         1760 System.Net.WebException
000007fef84ed780      131        22008 System.ObjectDisposedException

2 个答案:

答案 0 :(得分:7)

!做

在没有!dumpheap的情况下重新运行-stat命令以获取对象地址,然后您可以使用!do <address>!dumpobject <address>访问详细信息。

请注意,即使在简单的hello world应用程序中,每个程序中都存在一些异常(StackOverflowException,OutOfMemoryException和ThreadAbortException)。它们是预先分配的,因为在抛出它时可能无法使用新内存。

另请注意,不需要抛出异常。 var ex = new Exception()将创建一个对象但不会抛出它。因此,他们可能没有调用堆栈。

这就是它的样子:

0:003> !dumpheap -type NotImplementedException
         Address               MT     Size
000000000278a070 000007feebe03870      136     
total 1 objects
Statistics:
              MT    Count    TotalSize Class Name
000007feebe03870        1          136 System.NotImplementedException
Total 1 objects

0:003> !do 000000000278a070 
Name: System.NotImplementedException
MethodTable: 000007feebe03870
EEClass: 000007feeb311568
Size: 136(0x88) bytes
 (C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
[...]
000007feeb627680  40000bc       40        System.Object  0 instance 000000000278a1b8 _stackTrace
000007feeb627d90  40000bd       48        System.String  0 instance 0000000000000000 _stackTraceString
000007feeb627d90  40000be       50        System.String  0 instance     
[...]

如果存在堆栈跟踪,请使用!pe <address>查看它。否则它只是内存中未解析地址的列表。

0:003> !pe 000000000278a070 
Exception object: 000000000278a070
Exception type: System.NotImplementedException
Message: This method does nothing but thorwing this exception.
InnerException: <none>
StackTrace (generated):
    SP               IP               Function
    000000000135F2C0 000007FF0017067F MultiException!MultiException.Program.ThrowException2()+0x5f
    000000000135F300 000007FEEB4E2BBC mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)+0x9c
    000000000135F350 000007FEEB57AADE mscorlib_ni!System.Threading.ThreadHelper.ThreadStart()+0x4e

StackTraceString: <none>
HResult: 80004001

由于存在大量异常,您可能希望使用foreach循环自动执行它:

.foreach (ex {!dumpheap -type Exception -short}){ !do ${ex} }

!PE

对于当前抛出的异常,使用!threads确定引发异常的线程。在此示例中,有4个线程具有异常(向右滚动以查看它):

0:003> !threads
ThreadCount: 6
UnstartedThread: 0
BackgroundThread: 2
PendingThread: 0
DeadThread: 0
Hosted Runtime: no
                                              PreEmptive                                                Lock
       ID OSID        ThreadOBJ     State   GC     GC Alloc Context                  Domain           Count APT Exception
   0    1 251c 00000000003deff0   201a220 Enabled  00000000027846f8:0000000002785fd0 0000000000382ca0     0 MTA
   2    2 2b10 0000000000a88280      b220 Enabled  0000000000000000:0000000000000000 0000000000382ca0     0 MTA (Finalizer)
   3    3 255c 0000000000aacac0      b020 Enabled  00000000027862b8:0000000002787fd0 0000000000382ca0     0 MTA System.ArgumentException (0000000002786090)
   4    4 2a48 0000000000aad5b0      b020 Enabled  000000000278a290:000000000278bfd0 0000000000382ca0     0 MTA System.NotImplementedException (000000000278a070)
   5    5 2e50 0000000000aa20d0      b020 Enabled  0000000002788268:0000000002789fd0 0000000000382ca0     0 MTA System.OutOfMemoryException (0000000002788048)
   6    6  d50 0000000000aa2e00      b020 Enabled  000000000278c280:000000000278dfd0 0000000000382ca0     0 MTA System.Threading.ThreadInterruptedException (000000000278c060)

如何在一个转储中有4个例外?这可能是读者的练习。

在这种情况下,您可以使用~ns切换到该线程,其中n是第二列中的ID。或者使用~ne !pe直接在该线程上运行命令,例如像这样:

0:003> ~3e !pe
Exception object: 0000000002786090
Exception type: System.ArgumentException
Message: This method does not have arguments.
InnerException: <none>
StackTrace (generated):
    SP               IP               Function
    000000000112F100 000007FF0017055F MultiException!MultiException.Program.ThrowException1()+0x5f
    000000000112F140 000007FEEB4E2BBC mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)+0x9c
    000000000112F190 000007FEEB57AADE mscorlib_ni!System.Threading.ThreadHelper.ThreadStart()+0x4e

StackTraceString: <none>
HResult: 80070057

0:003> ~4e !pe
Exception object: 000000000278a070
Exception type: System.NotImplementedException
Message: This method does nothing but thorwing this exception.
InnerException: <none>
StackTrace (generated):
    SP               IP               Function
    000000000135F2C0 000007FF0017067F MultiException!MultiException.Program.ThrowException2()+0x5f
    000000000135F300 000007FEEB4E2BBC mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)+0x9c
    000000000135F350 000007FEEB57AADE mscorlib_ni!System.Threading.ThreadHelper.ThreadStart()+0x4e

StackTraceString: <none>
HResult: 80004001

答案 1 :(得分:1)

我下载并安装了windbg的netext扩展。并且netext !wdae 命令(转储堆中的所有异常)是查看异常的另一个选项。