使用WinDbg分析死锁

创建死锁程序

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!clrstack
OS Thread Id: 0x26d4 (0)
        Child SP               IP Call Site
000000000045ea38 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 in
the process
Failed to start stack walk: 80070057
OS Thread Id: 0x2a60 (2)
        Child SP               IP Call Site
000000001af8f8c8 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 in
the process
Failed to start stack walk: 80070057
OS Thread Id: 0x242c (4)
        Child SP               IP Call Site
000000001ba6e9d8 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> .time
Debug session time: Mon May 20 17:22:13.000 2013 (UTC + 8:00)
System Uptime: 0 days 7:14:13.190
Process 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> !threads
ThreadCount:      3
UnstartedThread:  0
BackgroundThread: 1
PendingThread:    0
DeadThread:       0
Hosted 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> !syncblk
Index SyncBlock MonitorHeld Recursion Owning Thread Info  SyncBlock Owner
    2 000000000052b978            3         1 00000000004fe850 26d4   0   0000000002822e88 ConsoleApplication1.Program
-----------------------------
Total           2
CCW             0
RCW             0
ComClassFactory 0
Free            0

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

备注

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

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

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值