大家应该知道 .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:
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:
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:
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:
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:\idontexist\log.txt'.
InnerException:
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:
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:\idontexist\log.txt'.
InnerException:
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:
HResult: 80070003
There are nested exceptions on this thread. Run with -nested for details 

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

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

教你如何找出 .NET 进程中的所有托管异常相关推荐

  1. 找出Java进程中大量消耗CPU

    问题分析: 1,程序属于CPU密集型,和开发沟通过,排除此类情况. 2,程序代码有问题,出现死循环,可能性极大. Java程序很耗CPU是比较好分析的,有这么几步: 1.通过top命令(top之后再按 ...

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

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

  3. isalnum()函数:找出str字符串中为英文字母或数字的字符

    找出str字符串中为英文字母或数字的字符 (1)Linux下编程用到的函数是:int isalnum(int c) 返回值:若参数c为字母或数字,则返回TRUE:否则返回NULL(0) #includ ...

  4. 找出一个数组中出现次数最多的那个元素

    Description 找出一个数组中出现次数最多的那个元素 Input 多组输入,请处理到文件结束 每组第一行输入一个整数n(不大于20) 第二行输入n个整数 Output 找出n个整数中出现次数最 ...

  5. 算法试题 - 找出字符流中第一个不重复的元素

    题目 题目:请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时, 第一个只出现一次的字符是"g".当从该字符流中 ...

  6. 在数据库中, 不用max()/min()找出一个列中最大/最小值的记录

    不用max()/min()找出c1列中最大/最小值的记录 // 找出c1列中,c1是最小值的那条记录,不能用min() select * from t1 where c1 <= all(sele ...

  7. 找出一个数组中唯一一个出现2次的数字

    找出一个数组中唯一一个出现2次的数字,不清楚是不是LeetCode上的题.本人默认是LeetCode上的题. 一个数组中有N个数字,但是只有一个数字出现了2次,其他的数字均不相同.这种问题一般应该采用 ...

  8. 一个N*M的矩阵,找出这个矩阵中所有元素的和不小于K的面积最小的子矩阵

    题目描述: 一个N*M的矩阵,找出这个矩阵中所有元素的和不小于K的面积最小的子矩阵(矩阵中元素个数为矩阵面积) 输入: 每个案例第一行三个正整数N,M<=100,表示矩阵大小,和一个整数K 接下 ...

  9. 输入5个整数,找出5个数中的两位数

    输入: 输入5个整数,找出5个数中的两位数,并输出,并输出两位数的平均数,如果没有两位数就不输出.用while 解决 5 6 20 9 10 输出: 20 10 15

最新文章

  1. 常见运维漏洞-Rsync-Redis
  2. 用C#实现支持gmail邮件发送
  3. C/C++预处理宏的总结
  4. leetcode 52. N皇后 II(回溯)
  5. [模板]LIS(最长上升子序列)
  6. [转]解决IE下CSS背景图片闪烁的Bug
  7. WebDriverException: Cannot find firefox binary in PATH.的解决方法
  8. 基本功:超全面 IO 流教程,小白也能看懂
  9. 白酒板块集体杀跌,我是不是该卖出手中的白酒基金了?
  10. Wine 开发者指导/架构概览
  11. 大学生html作业总结,总结 HTML标签作业
  12. 软考初级程序员主要考什么?如何复习?
  13. 端口扫描工具Namp
  14. 38241415106——胡应兰(实验一)
  15. 四分位距IQR interquartile range
  16. adobe xd 白屏闪退 终极拯救方法
  17. WebSocket + SpringBoot + VUE实现后端实时向前端推送消息
  18. [译]2019版 web 浏览器现状
  19. 土地利用总体规划数据库问题
  20. Wampserver整个下载安装教程

热门文章

  1. 预、自训练之争:谷歌说预训练虽火,但在标注数据上自训练更有效
  2. 我被裁员了!让保安把身患绝症的我被强赶出公司,亲身经历的噩梦!
  3. 如何看待 2020 届校招算法岗「爆炸」的情况?
  4. 数据库设计三大范式和ER模型
  5. 强势推荐一位 Python 原创自动化大佬!
  6. Python加速运行技巧
  7. 【OpenCV 4开发详解】边缘检测原理
  8. CSS中怎么让DIV居中
  9. 理解shared_ptrT
  10. layer-list简单使用以及shape的定义