好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

如何把 .NET 进程中的所有托管异常找出来?

大家应该知道 .NET 异常 本质上就是一个 Object 对象,也就是说只要你执行了 new XXException() 语句,那么它就会分配到 GC Heap 上。

这也就意味着,如果你有一个 进程 的dump文件,那你就可以从dump中导出程序最近都抛了什么异常,换句话说只要这些异常没有被 GC 回收,你都可以给它找出来。

实现起来很简单,只要在 windbg 中输入如下命令即可。

0:015>!dumpheap-typeException ------------------------------ Heap0 AddressMT Size 02ea6b0c79330a8072 02ea75f07930eab476 … 06f57aa47930eab476 06f5829c7930eab476 06f58a947930eab476 06f5928c7930eab476 06f59a847930eab476 06f5a27c7930eab476 06f5aa747930eab476 06f5b26c7930eab476 06f5ba647930eab476 06f5c25c7930eab476 06f5ca547930eab476 06f5d24c7930eab476 total319objects ------------------------------ total656objects Statistics : MT Count TotalSizeClass Name 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:Theemailentered is not avalidemailaddress InnerException: StackTrace(generated): SPIP Function 024AF2C80FE3125EApp_Code_da2s7oyo!BuggyMail.IsValidEmailAddress(System.String)+0x76 024AF2E80FE31192App_Code_da2s7oyo!BuggyMail.SendEmail(System.String,System.String)+0x4a StackTraceString: HResult:80131500 Therearenestedexceptions on thisthread.Run with -nested for details

那现在问题来了,我想看所有异常的详细信息怎么办呢?人肉一个一个的用 !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:Theemailentered is not avalidemailaddress InnerException: StackTrace(generated): SPIP Function 024AF2C80FE3125EApp_Code_da2s7oyo!BuggyMail.IsValidEmailAddress(System.String)+0x76 024AF2E80FE31192App_Code_da2s7oyo!BuggyMail.SendEmail(System.String,System.String)+0x4a StackTraceString: HResult:80131500 Therearenestedexceptions on thisthread.Run with -nested for details ******************************** Exceptionobject:02ea75f0 Exceptiontype:System.IO.DirectoryNotFoundException Message:Could not findapart of thepath 'c:\idontexist\log.txt' . InnerException: StackTrace(generated): SPIP Function 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 Therearenestedexceptions on thisthread.Run with -nested for details ******************************** Exceptionobject:02ea7de8 Exceptiontype:System.IO.DirectoryNotFoundException Message:Could not findapart of thepath 'c:\idontexist\log.txt' . InnerException: StackTrace(generated): SPIP Function 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 Therearenestedexceptions on thisthread.Run with -nested for details

当然你也可以打印出当前异常的内部异常,配上一个 -nest 参数即可。

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

原文链接:https://mp.weixin.qq.com/s/LhxU7Nr8n9bAx1sXKSlT4g

dy("nrwz");

查看更多关于如何把 .NET 进程中的所有托管异常找出来?的详细内容...

  阅读:72次