关于WinDbg/SOS如何快速转储堆上所有的.NET异常

由于.net异常与任何其他异常一样是一个.net对象,因此当您(或您调用的某些代码)调用new XXException()时,它会存储在GC堆中。这意味着,如果您有一个进程的内存转储,您可以转储所有最近发生的异常,或者更确切地说,所有尚未被垃圾收集的异常。

如果您在windbg中加载了sos(.loadby sos mscorwks),则执行此操作非常简单

0:015> !dumpheap -type Exception
------------------------------
Heap 0
Address       MT     Size
02ea6b0c 79330a80       72    
02ea75f0 7930eab4       76    
…
06f57aa4 7930eab4       76    
06f5829c 7930eab4       76    
06f58a94 7930eab4       76    
06f5928c 7930eab4       76    
06f59a84 7930eab4       76    
06f5a27c 7930eab4       76    
06f5aa74 7930eab4       76    
06f5b26c 7930eab4       76    
06f5ba64 7930eab4       76    
06f5c25c 7930eab4       76    
06f5ca54 7930eab4       76    
06f5d24c 7930eab4       76    
total 319 objects
------------------------------
total 656 objects
Statistics:
      MT    Count    TotalSize Class Name
79333dc0        1           12 System.Text.DecoderExceptionFallback
79333d7c        1           12 System.Text.EncoderExceptionFallback
793172f8        2           64 System.UnhandledExceptionEventHandler
79330c30        1           72 System.ExecutionEngineException
79330ba0        1           72 System.StackOverflowException
79330b10        1           72 System.OutOfMemoryException
79330a80        1           72 System.Exception
79330cc0        2          144 System.Threading.ThreadAbortException
7930eab4      646        49096 System.IO.DirectoryNotFoundException
Total 656 objects

要转储有关特定异常的信息,可以使用!pe 02ea6b0c将显示堆栈、异常名称等。问题是,您必须对所有异常执行此操作,如果存在许多异常,那么这些异常可能会变得非常乏味,因此为了加快流程,您可以使用.foreach命令来自动化流程。

.foreach (ex {!dumpheap -type Exception -short}){.echo "********************************";!pe ${ex} }

这将遍历堆上的所有对象,并用!pe(!PrintException的缩写)。请注意,这也将包括并非真正异常的项,如System.Text.DecoderExceptionFallback,但由于这些不是异常,它将尝试打印出来,但败,因此您可以忽略这些。

输出如下所示:

0:015> .foreach (ex {!dumpheap -type Exception -short}){.echo "********************************";!pe ${ex} }
********************************
Exception object: 02ea6b0c
Exception type: System.Exception
Message: The email entered is not a valid email address
InnerException: <none>
StackTrace (generated):
    SP       IP       Function
    024AF2C8 0FE3125E App_Code_da2s7oyo!BuggyMail.IsValidEmailAddress(System.String)+0x76
    024AF2E8 0FE31192 App_Code_da2s7oyo!BuggyMail.SendEmail(System.String, System.String)+0x4a

StackTraceString: <none>
HResult: 80131500
There are nested exceptions on this thread. Run with -nested for details
********************************
Exception object: 02ea75f0
Exception type: System.IO.DirectoryNotFoundException
Message: Could not find a part of the path 'c:idontexistlog.txt'.
InnerException: <none>
StackTrace (generated):
    SP       IP       Function
    024AF044 792741F2 mscorlib_ni!System.IO.__Error.WinIOError(Int32, System.String)+0xc2
    024AF0A0 792EB22B mscorlib_ni!System.IO.FileStream.Init(System.String, System.IO.FileMode, System.IO.FileAccess, Int32, Boolean, System.IO.FileShare, Int32, System.IO.FileOptions, SECURITY_ATTRIBUTES, System.String, Boolean)+0x48b
    024AF198 792EA882 mscorlib_ni!System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32, System.IO.FileOptions)+0x42
    024AF1C0 7927783F mscorlib_ni!System.IO.StreamWriter.CreateFile(System.String, Boolean)+0x3f
    024AF1D4 792777DB mscorlib_ni!System.IO.StreamWriter..ctor(System.String, Boolean, System.Text.Encoding, Int32)+0x3b
    024AF1F4 797EE19F mscorlib_ni!System.IO.StreamWriter..ctor(System.String)+0x1f
    024AF204 0FE31325 App_Code_da2s7oyo!Utility.WriteToLog(System.String, System.String)+0x5d

StackTraceString: <none>
HResult: 80070003
There are nested exceptions on this thread. Run with -nested for details
********************************
Exception object: 02ea7de8
Exception type: System.IO.DirectoryNotFoundException
Message: Could not find a part of the path 'c:idontexistlog.txt'.
InnerException: <none>
StackTrace (generated):
    SP       IP       Function
    024AEF60 792741F2 mscorlib_ni!System.IO.__Error.WinIOError(Int32, System.String)+0xc2
    024AEFBC 792EB22B mscorlib_ni!System.IO.FileStream.Init(System.String, System.IO.FileMode, System.IO.FileAccess, Int32, Boolean, System.IO.FileShare, Int32, System.IO.FileOptions, SECURITY_ATTRIBUTES, System.String, Boolean)+0x48b
    024AF0B4 792EA882 mscorlib_ni!System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32, System.IO.FileOptions)+0x42
    024AF0DC 7927783F mscorlib_ni!System.IO.StreamWriter.CreateFile(System.String, Boolean)+0x3f
    024AF0F0 792777DB mscorlib_ni!System.IO.StreamWriter..ctor(System.String, Boolean, System.Text.Encoding, Int32)+0x3b
    024AF110 797EE19F mscorlib_ni!System.IO.StreamWriter..ctor(System.String)+0x1f
    024AF120 0FE31325 App_Code_da2s7oyo!Utility.WriteToLog(System.String, System.String)+0x5d

StackTraceString: <none>
HResult: 80070003
There are nested exceptions on this thread. Run with -nested for details

如果您还希望它打印出嵌套的异常,只需稍微更改一下命令即可

.foreach (ex {!dumpheap -type Exception -short}){.echo "********************************";!pe –nested ${ex} }

posted on 2021-10-19 08:00  活着的虫子  阅读(195)  评论(0编辑  收藏  举报

导航