怎样把 .NET 进程中的所有托管异常找出来?
发布时间:2021-11-03 18:42:24  所属栏目:语言  来源:互联网 
            导读:大家应该知道 .NET异常 本质上就是一个 Object 对象,也就是说只要你执行了 new XXException() 语句,那么它就会分配到 GC Heap 上。这也就意味着,如果你有一个进程的dump文件,那你就可以从dump中导出程序最近都抛了什么异常,换句话说只要这些异常没有被
                
                
                
            | 大家应该知道 .NET异常 本质上就是一个 Object 对象,也就是说只要你执行了 new XXException() 语句,那么它就会分配到 GC Heap 上。
	 
	这也就意味着,如果你有一个进程的dump文件,那你就可以从dump中导出程序最近都抛了什么异常,换句话说只要这些异常没有被 GC 回收,你都可以给它找出来。
	 
	实现起来很简单,只要在 windbg 中输入如下命令即可。
	 
	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 。
	 
	!pe 02ea6b0c 
	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 
	那现在问题来了,我想看所有异常的详细信息怎么办呢?人肉一个一个的用 !pe 命令去执行,那将会多恶心。。。所以友好的方式就是写脚本去提速,这里我使用 .foreach 命令。
	 
	.foreach (ex {!dumpheap -type Exception -short}){.echo "********************************";!pe ${ex} } 
	上面我用了一个 -short 参数,目的就是只输出 address 地址方便脚本遍历,然后将迭代项送入 !pe ,输出结果如下:
	 
	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): (编辑:宣城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 

