确定从转储文件中抛出异常的线程

时间:2015-03-06 17:13:09

标签: c# multithreading exception windbg sos

假设我的应用程序有两个线程A和B,每个线程抛出一个异常。我可以使用~{threadid}s然后运行!pe吗?

来确定哪个线程引发了哪个异常

1 个答案:

答案 0 :(得分:3)

通常只有一个线程抛出异常导致应用程序终止。也就是说,我看到它发生在调试会话期间,其中具有异常的线程被冻结并且其他线程被恢复。

.NET !threads命令将列出每个线程上的.NET线程和异常:

0:000> !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)

然后您可以切换到列为ID的线程并将该异常转储到该线程(~xs;!pe~xe!pe,其中x是ID):

0:004> ~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:004> ~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在内存中预先分配了一些异常,以便以后可以抛出它们。这可能会使首次使用WinDbg的开发人员感到困惑。所以,如果您在!dumpheap -stat -type Exception的输出中看到一些例外情况,请不要担心,例如在一个简单的HelloWorld应用程序中:

0:000> !dumpheap -stat -type Exception
total 7 objects
Statistics:
      MT    Count    TotalSize Class Name
78e4421c        1           12 System.Text.DecoderExceptionFallback
78e441d8        1           12 System.Text.EncoderExceptionFallback
78e4108c        1           72 System.ExecutionEngineException
78e40ffc        1           72 System.StackOverflowException
78e40f6c        1           72 System.OutOfMemoryException
78e4111c        2          144 System.Threading.ThreadAbortException
Total 7 objects

在非托管代码中,使用#显示线程时,抛出异常的线程标有~

0:000> ~
.  0  Id: 2b14.251c Suspend: 1 Teb: 000007ff`fffde000 Unfrozen
   1  Id: 2b14.71c Suspend: 1 Teb: 000007ff`fffdb000 Unfrozen
   2  Id: 2b14.2b10 Suspend: 1 Teb: 000007ff`fffd9000 Unfrozen
#  3  Id: 2b14.255c Suspend: 1 Teb: 000007ff`fffd7000 Unfrozen
   4  Id: 2b14.2a48 Suspend: 1 Teb: 000007ff`fffd5000 Unfrozen
   5  Id: 2b14.2e50 Suspend: 1 Teb: 000007ff`fffd3000 Unfrozen
   6  Id: 2b14.d50 Suspend: 1 Teb: 000007ff`fff0e000 Unfrozen

如果您看不到#,那么它通常隐藏在.后面。您可以使用~#s切换到此主题。请注意,WinDbg只显示一个#,尽管其他线程也有例外。我不知道如何在本地解决这个问题。