gpt4 book ai didi

.net - 获取堆中异常的堆栈跟踪

转载 作者:行者123 更新时间:2023-12-03 21:41:46 25 4
gpt4 key购买 nike

我使用 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

最佳答案

!做

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

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

还要注意不需要抛出异常。一个 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

关于.net - 获取堆中异常的堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38438522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com