获取堆栈中异常的堆栈跟踪 [英] Get stack trace of exceptions in heap

查看:77
本文介绍了获取堆栈中异常的堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

!do

在不使用 -stat 的情况下重新运行!dumpheap 命令以获取对象地址,然后可以使用!do< address> 或!dumpobject< address> .

!do

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

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

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.

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

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.

这可能是这样的:

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> 进行查看.否则,它只是内存中未解析地址的列表.

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

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

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

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

!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)

如何在一个转储中包含4个例外?对于读者来说,这可能是一个练习.

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

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

这篇关于获取堆栈中异常的堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆