大家應該知道 .NET異常 本質上就是一個 Object 對象,也就是說只要你執行了 new XXException() 語句,那么它就會分配到 GC Heap 上。
這也就意味著,如果你有一個進程的dump文件,那你就可以從dump中導出程序最近都拋了什么異常,換句話說只要這些異常沒有被 GC 回收,你都可以給它找出來。
實現起來很簡單,只要在 windbg 中輸入如下命令即可。
- 0:015>!dumpheap-typeException
- ------------------------------
- Heap0
- AddressMTSize
- 02ea6b0c79330a8072
- 02ea75f07930eab476
- …
- 06f57aa47930eab476
- 06f5829c7930eab476
- 06f58a947930eab476
- 06f5928c7930eab476
- 06f59a847930eab476
- 06f5a27c7930eab476
- 06f5aa747930eab476
- 06f5b26c7930eab476
- 06f5ba647930eab476
- 06f5c25c7930eab476
- 06f5ca547930eab476
- 06f5d24c7930eab476
- total319objects
- ------------------------------
- total656objects
- Statistics:
- MTCountTotalSizeClassName
- 79333dc0112System.Text.DecoderExceptionFallback
- 79333d7c112System.Text.EncoderExceptionFallback
- 793172f8264System.UnhandledExceptionEventHandler
- 79330c30172System.ExecutionEngineException
- 79330ba0172System.StackOverflowException
- 79330b10172System.OutOfMemoryException
- 79330a80172System.Exception
- 79330cc02144System.Threading.ThreadAbortException
- 7930eab464649096System.IO.DirectoryNotFoundException
- Total656objects
如果你想看某一個具體異常的詳細信息,可以使用命令 !pe 02ea6b0c 。
- !pe02ea6b0c
- Exceptionobject:02ea6b0c
- Exceptiontype:System.Exception
- Message:Theemailenteredisnotavalidemailaddress
-
InnerException:
- StackTrace(generated):
- SPIPFunction
- 024AF2C80FE3125EApp_Code_da2s7oyo!BuggyMail.IsValidEmailAddress(System.String)+0x76
- 024AF2E80FE31192App_Code_da2s7oyo!BuggyMail.SendEmail(System.String,System.String)+0x4a
-
StackTraceString:
- HResult:80131500
- Therearenestedexceptionsonthisthread.Runwith-nestedfordetails
那現在問題來了,我想看所有異常的詳細信息怎么辦呢?人肉一個一個的用 !pe 命令去執行,那將會多惡心。。。所以友好的方式就是寫腳本去提速,這里我使用 .foreach 命令。
- .foreach(ex{!dumpheap-typeException-short}){.echo"********************************";!pe${ex}}
上面我用了一個 -short 參數,目的就是只輸出 address 地址方便腳本遍歷,然后將迭代項送入 !pe ,輸出結果如下:
- 0:015>.foreach(ex{!dumpheap-typeException-short}){.echo"********************************";!pe${ex}}
- ********************************
- Exceptionobject:02ea6b0c
- Exceptiontype:System.Exception
- Message:Theemailenteredisnotavalidemailaddress
-
InnerException:
- StackTrace(generated):
- SPIPFunction
- 024AF2C80FE3125EApp_Code_da2s7oyo!BuggyMail.IsValidEmailAddress(System.String)+0x76
- 024AF2E80FE31192App_Code_da2s7oyo!BuggyMail.SendEmail(System.String,System.String)+0x4a
-
StackTraceString:
- HResult:80131500
- Therearenestedexceptionsonthisthread.Runwith-nestedfordetails
- ********************************
- Exceptionobject:02ea75f0
- Exceptiontype:System.IO.DirectoryNotFoundException
- Message:Couldnotfindapartofthepath'c:\idontexist\log.txt'.
-
InnerException:
- StackTrace(generated):
- SPIPFunction
- 024AF044792741F2mscorlib_ni!System.IO.__Error.WinIOError(Int32,System.String)+0xc2
- 024AF0A0792EB22Bmscorlib_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
- 024AF198792EA882mscorlib_ni!System.IO.FileStream..ctor(System.String,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,Int32,System.IO.FileOptions)+0x42
- 024AF1C07927783Fmscorlib_ni!System.IO.StreamWriter.CreateFile(System.String,Boolean)+0x3f
- 024AF1D4792777DBmscorlib_ni!System.IO.StreamWriter..ctor(System.String,Boolean,System.Text.Encoding,Int32)+0x3b
- 024AF1F4797EE19Fmscorlib_ni!System.IO.StreamWriter..ctor(System.String)+0x1f
- 024AF2040FE31325App_Code_da2s7oyo!Utility.WriteToLog(System.String,System.String)+0x5d
-
StackTraceString:
- HResult:80070003
- Therearenestedexceptionsonthisthread.Runwith-nestedfordetails
- ********************************
- Exceptionobject:02ea7de8
- Exceptiontype:System.IO.DirectoryNotFoundException
- Message:Couldnotfindapartofthepath'c:\idontexist\log.txt'.
-
InnerException:
- StackTrace(generated):
- SPIPFunction
- 024AEF60792741F2mscorlib_ni!System.IO.__Error.WinIOError(Int32,System.String)+0xc2
- 024AEFBC792EB22Bmscorlib_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
- 024AF0B4792EA882mscorlib_ni!System.IO.FileStream..ctor(System.String,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,Int32,System.IO.FileOptions)+0x42
- 024AF0DC7927783Fmscorlib_ni!System.IO.StreamWriter.CreateFile(System.String,Boolean)+0x3f
- 024AF0F0792777DBmscorlib_ni!System.IO.StreamWriter..ctor(System.String,Boolean,System.Text.Encoding,Int32)+0x3b
- 024AF110797EE19Fmscorlib_ni!System.IO.StreamWriter..ctor(System.String)+0x1f
- 024AF1200FE31325App_Code_da2s7oyo!Utility.WriteToLog(System.String,System.String)+0x5d
-
StackTraceString:
- HResult:80070003
- Therearenestedexceptionsonthisthread.Runwith-nestedfordetails
當然你也可以打印出當前異常的內部異常,配上一個 -nest 參數即可。
- .foreach(ex{!dumpheap-typeException-short}){.echo"********************************";!pe–nested${ex}}
原文鏈接:https://mp.weixin.qq.com/s/LhxU7Nr8n9bAx1sXKSlT4g