获取堆中的异常堆栈跟踪

Bil*_*han 3 .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
Run Code Online (Sandbox Code Playgroud)

Tho*_*ler 8

!做

重新运行!dumpheap命令而不-stat获取对象地址,然后您可以使用!do <address>或访问详细信息!dumpobject <address>.

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

另请注意,不需要抛出异常.A 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     
[...]
Run Code Online (Sandbox Code Playgroud)

如果存在堆栈跟踪,请使用!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
Run Code Online (Sandbox Code Playgroud)

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

.foreach (ex {!dumpheap -type Exception -short}){ !do ${ex} }
Run Code Online (Sandbox Code Playgroud)

!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)
Run Code Online (Sandbox Code Playgroud)

如何在一个转储中有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
Run Code Online (Sandbox Code Playgroud)