Get stack trace of exceptions in heap

▼魔方 西西 提交于 2021-01-27 13:38:58

问题


I use WinDbg and I want to get exception details. With help of !dumpheap -type Exception command get a list of exceptions in dump but how can I access these exception details?

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

回答1:


!do

Rerun the !dumpheap command without -stat to get the object addresses, then you can access the details with !do <address> or !dumpobject <address>.

Note that some of the Exceptions (StackOverflowException, OutOfMemoryException and ThreadAbortException) exist in every program, even in simple hello world applications. They are pre-allocated, because new memory might not be available at the time of throwing it.

Also note that the exceptions needn't be thrown. A var ex = new Exception() will create an object but not throw it. Thus they may not have a call stack.

This is how it may look like:

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     
[...]

If the stack trace is present, use !pe <address> to see it. Otherwise it it just a list of unresolved addresses in memory.

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

Due to the high number of exceptions, you might want to automate it with a foreach loop:

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

!pe

For exceptions that are currently thrown, use !threads to determine the threads that threw an exception. In this example, there are 4 threads with exceptions (scroll right to see it):

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)

How can you have 4 exceptions in one dump? This may be an exercise for the reader.

In that case, you can switch to the thread using ~ns where n is the ID in the second column. Or use ~ne !pe to run a command directly on that thread, e.g. like this:

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



回答2:


I download and install netext extension of windbg.And netext !wdae command(Dump all exceptions in the heap) is another option to see exceptions.



来源:https://stackoverflow.com/questions/38438522/get-stack-trace-of-exceptions-in-heap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!