博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用WinDbg分析死锁
阅读量:5124 次
发布时间:2019-06-13

本文共 5347 字,大约阅读时间需要 17 分钟。

创建死锁程序

using System;using System.Threading;namespace ConsoleApplication1{    class Program    {        static void Main()        {            new Program().Test1();        }        private void Test1()        {            lock (this)            {                Console.WriteLine("Enter Test1");                new Thread(() =>                    {                        Thread.Sleep(2000);                        Test2();                    }).Start();                Console.ReadKey();                Console.WriteLine("Exit Test1");            }        }        private void Test2()        {            lock (this)            {                Console.WriteLine("Test2");            }        }    }}

运行程序后(不要点击键盘)创建.dump文件

使用WinDbg进行分析

执行~*e!clrstack查看所有持有和等待锁的线程(的下一条要执行的代码)

0:000> ~*e!clrstackOS Thread Id: 0x26d4 (0)        Child SP               IP Call Site000000000045ea38 000007ffe9ab2c5a [InlinedCallFrame: 000000000045ea38] Microsoft.Win32.Win32Native.ReadConsoleInput(IntPtr, InputRecord ByRef, Int32, Int32 ByRef)000000000045ea38 000007ffd1e3f64e [InlinedCallFrame: 000000000045ea38] Microsoft.Win32.Win32Native.ReadConsoleInput(IntPtr, InputRecord ByRef, Int32, Int32 ByRef)000000000045e9f0 000007ffd1e3f64e DomainNeutralILStubClass.IL_STUB_PInvoke(IntPtr, InputRecord ByRef, Int32, Int32 ByRef)000000000045eb30 000007ffd1ea9961 System.Console.ReadKey(Boolean)000000000045ec50 000007ff7b5501b1 ConsoleApplication1.Program.Test1() [f:\Documents\Visual Studio 2012\Projects\2012test\ConsoleApplication1\Program.cs @ 25]000000000045ecc0 000007ff7b5500a8 ConsoleApplication1.Program.Main() [f:\Documents\Visual Studio 2012\Projects\2012test\ConsoleApplication1\Program.cs @ 11]000000000045efd0 000007ffdac338f3 [GCFrame: 000000000045efd0] OS Thread Id: 0x2b74 (1)Unable to walk the managed stack. The current thread is likely not a managed thread. You can run !threads to get a list of managed threads inthe processFailed to start stack walk: 80070057OS Thread Id: 0x2a60 (2)        Child SP               IP Call Site000000001af8f8c8 000007ffe9ab319b [DebuggerU2MCatchHandlerFrame: 000000001af8f8c8] OS Thread Id: 0x1978 (3)Unable to walk the managed stack. The current thread is likely not a managed thread. You can run !threads to get a list of managed threads inthe processFailed to start stack walk: 80070057OS Thread Id: 0x242c (4)        Child SP               IP Call Site000000001ba6e9d8 000007ffe9ab319b [GCFrame: 000000001ba6e9d8] 000000001ba6eb18 000007ffe9ab319b [GCFrame: 000000001ba6eb18] 000000001ba6eb58 000007ffe9ab319b [HelperMethodFrame_1OBJ: 000000001ba6eb58] System.Threading.Monitor.Enter(System.Object)000000001ba6ec50 000007ff7b550300 ConsoleApplication1.Program.Test2() [f:\Documents\Visual Studio 2012\Projects\2012test\ConsoleApplication1\Program.cs @ 33]000000001ba6ecb0 000007ffd16afb65 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)000000001ba6ee10 000007ffd16af8c9 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)000000001ba6ee40 000007ffd16af887 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)000000001ba6ee90 000007ffd16c2fe1 System.Threading.ThreadHelper.ThreadStart()000000001ba6f1a8 000007ffdac338f3 [GCFrame: 000000001ba6f1a8] 000000001ba6f4d8 000007ffdac338f3 [DebuggerU2MCatchHandlerFrame: 000000001ba6f4d8]

执行!runaway查看占有锁的线程运行了多长时间

0:000> !runaway User Mode Time  Thread       Time   4:242c      0 days 0:00:00.000   3:1978      0 days 0:00:00.000   2:2a60      0 days 0:00:00.000   1:2b74      0 days 0:00:00.000   0:26d4      0 days 0:00:00.000

执行.time看进程运行了多长时间

0:000> .timeDebug session time: Mon May 20 17:22:13.000 2013 (UTC + 8:00)System Uptime: 0 days 7:14:13.190Process Uptime: 0 days 0:00:11.000  Kernel time: 0 days 0:00:00.000  User time: 0 days 0:00:00.000

执行!threads看所有的线程统计情况

0:000> !threadsThreadCount:      3UnstartedThread:  0BackgroundThread: 1PendingThread:    0DeadThread:       0Hosted Runtime:   no                                                                                                        Lock         ID OSID ThreadOBJ           State GC Mode     GC Alloc Context                  Domain           Count Apt Exception   0    1 26d4 00000000004fe850    2a020 Preemptive  0000000002826698:0000000002827FD0 00000000004d2ab0 2     MTA    2    2 2a60 00000000005056a0    2b220 Preemptive  0000000000000000:0000000000000000 00000000004d2ab0 0     MTA (Finalizer)    4    3 242c 000000000052aca0  202b020 Preemptive  0000000002828010:0000000002829FD0 00000000004d2ab0 0     MTA

可看到线程0有两个锁

执行!syncblk查看哪些线程拿到了锁

0:000> !syncblkIndex SyncBlock MonitorHeld Recursion Owning Thread Info  SyncBlock Owner    2 000000000052b978            3         1 00000000004fe850 26d4   0   0000000002822e88 ConsoleApplication1.Program-----------------------------Total           2CCW             0RCW             0ComClassFactory 0Free            0

可看到线程“00000000004fe850 ”拿到了锁,再通过!threads命令得到其线程逻辑号为0,即可切换至该线程,并通过!clrstack打印出其运行堆栈。

备注

  •   若表现为界面死掉,就直接找UI线程(STA线程就一个)
  •   堆栈分为本地堆栈与托管堆栈,线程号也有本地线程号与托管线程号
  •   可使用!dso (DumpStackObjects)查看当前堆栈的所有对象
  •   可使用 !analyze -v 分析挂掉线程的详细情形,错误原因

转载于:https://www.cnblogs.com/beta2013/archive/2013/05/20/3377264.html

你可能感兴趣的文章
Java反射机制及其Class类浅析
查看>>
Postman-----如何导入和导出
查看>>
移动设备显示尺寸大全 CSS3媒体查询
查看>>
图片等比例缩放及图片上下剧中
查看>>
【转载】Linux screen 命令详解
查看>>
background-clip,background-origin
查看>>
Android 高级UI设计笔记12:ImageSwitcher图片切换器
查看>>
【Linux】ping命令详解
查看>>
对团队成员公开感谢博客
查看>>
java学习第三天
查看>>
python目录
查看>>
django+uwsgi+nginx+sqlite3部署+screen
查看>>
Andriod小型管理系统(Activity,SQLite库操作,ListView操作)(源代码下载)
查看>>
在Server上得到数据组装成HTML后导出到Excel。两种方法。
查看>>
浅谈项目需求变更管理
查看>>
经典算法系列一-快速排序
查看>>
设置java web工程中默认访问首页的几种方式
查看>>
ASP.NET MVC 拓展ViewResult实现word文档下载
查看>>
8、RDD持久化
查看>>
第二次团队冲刺--2
查看>>