代码

// Deadlock_Debug.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "windows.h"
#include <process.h> // All the thread must get all of critial_section
// Classic Deadlock CRITICAL_SECTION gCritSecFirst;
CRITICAL_SECTION gCritSecSecond;
CRITICAL_SECTION gCritSecThird; unsigned __stdcall Thread1Func(void* dummy)
{
printf("Enter Thread 1 \n");
EnterCriticalSection(&gCritSecThird);
{
Sleep();
EnterCriticalSection(&gCritSecFirst);
EnterCriticalSection(&gCritSecSecond);
LeaveCriticalSection(&gCritSecSecond);
LeaveCriticalSection(&gCritSecFirst);
}
LeaveCriticalSection(&gCritSecThird);
printf("Exit thread func 1 \n");
return ;
} unsigned __stdcall Thread2Func(void* dummy)
{
printf("Enter Thread 2 \n");
EnterCriticalSection(&gCritSecFirst);
EnterCriticalSection(&gCritSecSecond);
{
Sleep();
EnterCriticalSection(&gCritSecThird);
Sleep();
LeaveCriticalSection(&gCritSecThird);
}
LeaveCriticalSection(&gCritSecSecond);
LeaveCriticalSection(&gCritSecFirst);
printf("Exit thread func 2 \n");
return ;
} int _tmain(int argc, _TCHAR* argv[])
{
unsigned threadID;
InitializeCriticalSection(&gCritSecFirst);
InitializeCriticalSection(&gCritSecSecond);
InitializeCriticalSection(&gCritSecThird); _beginthreadex( NULL, , &Thread1Func, NULL, , &threadID );
_beginthreadex( NULL, , &Thread2Func, NULL, , &threadID );
while();
return ;
}

这是一个经典死锁,开启两个线程,一个以123的次序拿锁,一个以321次序拿锁,互相等待着对方的锁而均不释放自己手中的锁。(此处锁也指CriticalSection)

开启WINDBG 附加死锁进程,查看
0:003> ~* kv 0 Id: 2b8.2f0 Suspend: 1 Teb: 7ffdf000 Unfrozen
ChildEBP RetAddr Args to Child
WARNING: Stack unwind information not available. Following frames may be wrong.
0012ff6c 0042e346 00000001 003c30b8 003c3168 Deadlock_Debug+0x2d85c
0012ffb8 0042e21f 0012fff0 7c816d4f 0007da50 Deadlock_Debug+0x2e346
0012ffc0 7c816d4f 0007da50 7c92e1fe 7ffd9000 Deadlock_Debug+0x2e21f
0012fff0 00000000 0042b523 00000000 78746341 kernel32!BaseProcessStart+0x23 (FPO: [Non-Fpo]) 1 Id: 2b8.1e0 Suspend: 1 Teb: 7ffde000 Unfrozen
ChildEBP RetAddr Args to Child
0059fe00 7c92e9c0 7c93901b 00000030 00000000 ntdll!KiFastSystemCallRet (FPO: [0,0,0])
0059fe04 7c93901b 00000030 00000000 00000000 ntdll!ZwWaitForSingleObject+0xc (FPO: [3,0,0])
0059fe8c 7c92104b 004944f0 0042d5e0 004944f0 ntdll!RtlpWaitForCriticalSection+0x132 (FPO: [1,26,4])
0059fe94 0042d5e0 004944f0 57565554 5b5a5958 ntdll!RtlEnterCriticalSection+0x46 (FPO: [1,0,0])
WARNING: Stack unwind information not available. Following frames may be wrong.
0059ff6c 0042e123 00000000 2d7b25f9 57565554 Deadlock_Debug+0x2d5e0
0059ffa8 0042e094 00000000 0059ffec 7c80b50b Deadlock_Debug+0x2e123
0059ffb4 7c80b50b 003c2c20 57565554 5b5a5958 Deadlock_Debug+0x2e094
0059ffec 00000000 0042dff0 003c2c20 00000000 kernel32!BaseThreadStart+0x37 (FPO: [Non-Fpo]) 2 Id: 2b8.1e4 Suspend: 1 Teb: 7ffdd000 Unfrozen
ChildEBP RetAddr Args to Child
0069fe00 7c92e9c0 7c93901b 00000034 00000000 ntdll!KiFastSystemCallRet (FPO: [0,0,0])
0069fe04 7c93901b 00000034 00000000 00000000 ntdll!ZwWaitForSingleObject+0xc (FPO: [3,0,0])
0069fe8c 7c92104b 004944c0 0042d714 004944c0 ntdll!RtlpWaitForCriticalSection+0x132 (FPO: [1,26,4])
0069fe94 0042d714 004944c0 57565554 5b5a5958 ntdll!RtlEnterCriticalSection+0x46 (FPO: [1,0,0])
WARNING: Stack unwind information not available. Following frames may be wrong.
0069ff6c 0042e123 00000000 2d4b25f9 57565554 Deadlock_Debug+0x2d714
0069ffa8 0042e094 00000000 0069ffec 7c80b50b Deadlock_Debug+0x2e123
0069ffb4 7c80b50b 003c2e60 57565554 5b5a5958 Deadlock_Debug+0x2e094
0069ffec 00000000 0042dff0 003c2e60 00000000 kernel32!BaseThreadStart+0x37 (FPO: [Non-Fpo]) # 3 Id: 2b8.948 Suspend: 1 Teb: 7ffdc000 Unfrozen
ChildEBP RetAddr Args to Child
0038ffc8 7c9707a8 00000005 00000004 00000001 ntdll!DbgBreakPoint (FPO: [0,0,0])
0038fff4 00000000 00000000 00000000 00000000 ntdll!DbgUiRemoteBreakin+0x2d (FPO: [Non-Fpo]) ////////////////////////////////////////////////////
在切换到线程2 1进行观察 可以看到均是在等待CriticalSection
0:003> ~2 kv
ChildEBP RetAddr Args to Child
0069fe00 7c92e9c0 7c93901b 00000034 00000000 ntdll!KiFastSystemCallRet (FPO: [0,0,0])
0069fe04 7c93901b 00000034 00000000 00000000 ntdll!ZwWaitForSingleObject+0xc (FPO: [3,0,0])
0069fe8c 7c92104b 004944c0 0042d714 004944c0 ntdll!RtlpWaitForCriticalSection+0x132 (FPO: [1,26,4])
0069fe94 0042d714 004944c0 57565554 5b5a5958 ntdll!RtlEnterCriticalSection+0x46 (FPO: [1,0,0] 0:003> ~1 kv
ChildEBP RetAddr Args to Child
0059fe00 7c92e9c0 7c93901b 00000030 00000000 ntdll!KiFastSystemCallRet (FPO: [0,0,0])
0059fe04 7c93901b 00000030 00000000 00000000 ntdll!ZwWaitForSingleObject+0xc (FPO: [3,0,0])
0059fe8c 7c92104b 004944f0 0042d5e0 004944f0 ntdll!RtlpWaitForCriticalSection+0x132 (FPO: [1,26,4])
0059fe94 0042d5e0 004944f0 57565554 5b5a5958 ntdll!RtlEnterCriticalSection+0x46 (FPO: [1,0,0])
//////////////////////////////////////////////
使用!LOCKS指令查看当前的锁
0:003> !locks CritSec Deadlock_Debug+944f0 at 004944f0
LockCount 1
RecursionCount 1
OwningThread 1e4
EntryCount 1
ContentionCount 1
*** Locked CritSec Deadlock_Debug+944d8 at 004944d8
LockCount 0
RecursionCount 1
OwningThread 1e4
EntryCount 0
ContentionCount 0
*** Locked CritSec Deadlock_Debug+944c0 at 004944c0
LockCount 1
RecursionCount 1
OwningThread 1e0
EntryCount 1
ContentionCount 1
*** Locked

比对线程等待的锁 及个锁的拥有者和锁定情况变可以判断
线程编号1e4 也就是线程2 等待004944c0 的锁(根据堆栈的RtlEnterCriticalSection函数部分查看)
0069fe94 0042d714 004944c0 57565554 5b5a5958 ntdll!RtlEnterCriticalSection+0x46 (FPO: [1,0,0])

而004844c0的锁由线程编号1e0 就是线程1持有
而线程编号1e0 就是线程1 等待004944f0 的锁 该所有线程编号1e4持有
0059fe94 0042d5e0 004944f0 57565554 5b5a5958 ntdll!RtlEnterCriticalSection+0x46 (FPO: [1,0,0])

死锁!!!
找到原因就好修正了。

windbg 经典死锁调试的更多相关文章

  1. Windbg在软件调试中的应用

    Windbg在软件调试中的应用 Windbg是微软提供的一款免费的,专门针对Windows应用程序的调试工具.借助于Windbg, 我们常见的软件问题:软件异常,死锁,内存泄漏等,就可以进行高效的排查 ...

  2. VS 2013驱动开发 + Windbg + VM双机调试(亲测+详解)

    ------------VS 2013驱动开发 + Windbg + VM双机调试(亲测+详解)------------- WIN10已上线,随之而来的是VS2015:微软在 "WDK760 ...

  3. Windbg驱动双机调试环境配置

    [由于进入了Windows驱动编程领域第一步就是搭建环境,整个环境来说说难也不难,只是比较麻烦.文章有些地方比较繁琐的,而且别人写的比较好,作为引用参考直接贴连接了.如果你按照我写的一步步完成,很快就 ...

  4. 【旧文章搬运】Windbg+Vmware驱动调试入门(四)---VirtualKD内核调试加速工具

    原文发表于百度空间,2009-01-09========================================================================== 今天又想起 ...

  5. 【旧文章搬运】Windbg+Vmware驱动调试入门(一)---Windbg的设置

    原文发表于百度空间,2009-01-08========================================================================== Windb ...

  6. 【旧文章搬运】Windbg+Vmware驱动调试入门(二)---Vmware及GuestOS的设置

    原文发表于百度空间,2009-01-08========================================================================== 这一篇是主 ...

  7. Windbg源码调试

    Windbg提供比VS2008丰富很多的调试命令,尤其是调试多线程程序. 今天试着怎么使用源代码方式调试.为了说明调试命令,<C++标准库>一书里的例子做示范. // testcast.c ...

  8. JAVA并发,经典死锁案例-哲学家就餐

    转自:http://blog.csdn.net/tayanxunhua/article/details/38691005 死锁经典案例:哲学家就餐. 这个案例会导致死锁. 通过修改<Java编程 ...

  9. windbg蓝屏调试

    一般在写Windows内核程序的时候,经常会出现蓝屏的问题,这个时候一般是采用记录下dump文件然后用windbg查看得方式,具体的过程就不说了,网上一大堆的内容.现在我主要记录自己当初按照网上的方案 ...

随机推荐

  1. Browser Render Engine & Javascript Engine

    Browser Render Engine Programming Language Open Source Javascript Engine Comparation for CSS Compati ...

  2. nginx的启动,停止和重启

    启动 启动代码格式:nginx安装目录地址 -c nginx配置文件地址 例如: [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /us ...

  3. python:带参数的装饰器,函数的有用信息

    一.带参数的装饰器,函数的有用信息 def func1(): '''此函数的功能是完成的登陆的功能 return: 返回值是登陆成功与否(true,false) ''' print(333) func ...

  4. MySQL Antelope和Barracuda的区别分析

    Antelope是innodb-base的文件格式,Barracude是innodb-plugin后引入的文件格式,同时Barracude也支持Antelope文件格式.两者区别在于: 文件格式 支持 ...

  5. LSTM/RNN中的Attention机制

    一.解决的问题 采用传统编码器-解码器结构的LSTM/RNN模型存在一个问题,不论输入长短都将其编码成一个固定长度的向量表示,这使模型对于长输入序列的学习效果很差(解码效果很差). 注意下图中,ax ...

  6. Java 里面各种类型之间的相互转换

    1.整形与字符型之间的数据类型转换: 一.int转换成char有两种方法: ①  是利用char的unicode编码 例:int num1 = 8; char ch1 = (char) (num1 + ...

  7. Python模拟登陆TAPD

    因为在wiki中未找到需要的数据,查询也很迷,打算用python登录tapd抓取所需项目下的wiki数据,方便查找. 2018-9-30 19:12:44 几步走 模拟登录tapd 抓取wiki页左侧 ...

  8. 实战ELK(4)Metricbeat 轻量型指标采集器

    一.介绍 用于从系统和服务收集指标.从 CPU 到内存,从 Redis 到 Nginx,Metricbeat 能够以一种轻量型的方式,输送各种系统和服务统计数据. 1.系统级监控,更简洁(轻量型指标采 ...

  9. leetcode494

    public class Solution { public int FindTargetSumWays(int[] nums, int S) { Queue<int> Q = new Q ...

  10. Java语法 [常识1]

    1. Java 语言采用的是双字节Unicode 编码 . 2. 标识符就是变量.常量.方法[函数].枚举.类.接口等由写代码的猴子们制定的名字.构成标识符的字母均有一定的规范,Java语言中的命名规 ...