//死锁检测

.load sosex.dll

:> !dlk

:> !mk -a

The mk command displays a call stack of the currently selected thread (including both managed and unmanaged frames).
The command has now been extended to support the -a switch which outputs both the local variables as well as parameters
(combination of -l and -p switches):

0:003> !finq

  

The finq command (finalization queue) lists all the objects that are on the finalization queue. An example is shown below:

0:003> !frq -stat

  

The frq command (f-reachable queue) on the other hand, lists all objects that are on the f-reachable queue as shown below:

// 200b220 代表线程等待,可能是锁,或者 Sleep(), 这个要进一步检查。

0:046> !threads
ThreadCount: 54
UnstartedThread: 0
BackgroundThread: 22
PendingThread: 0
DeadThread: 9
Hosted Runtime: no
PreEmptive Lock
ID OSID ThreadOBJ State GC GC Alloc Context Domain Count APT Exception
9 1 1644 0000000001412de0 8220 Enabled 0000000000000000:0000000000000000 0000000000311a20 0 Ukn
17 2 528 000000000141f5d0 b220 Enabled 0000000000000000:0000000000000000 0000000000311a20 0 MTA (Finalizer)
19 4 181c 00000000039853e0 100a220 Enabled 0000000000000000:0000000000000000 0000000000311a20 0 MTA (Threadpool Worker)
20 5 221c 0000000003998d00 1220 Enabled 0000000000000000:0000000000000000 0000000000311a20 0 Ukn
21 6 16d8 00000000044484c0 200b220 Enabled 0000000000000000:0000000000000000 00000000039980a0 0 MTA
22 7 1e54 0000000004460680 200b220 Enabled 0000000000000000:0000000000000000 00000000039980a0 0 MTA
23 8 1308 000000000445fd20 200b220 Enabled 0000000000000000:0000000000000000 00000000039980a0 0 MTA
XXXX 9 00000000044b0510 1019820 Enabled 0000000000000000:0000000000000000 0000000000311a20 0 MTA (Threadpool Worker)
25 a 1e50 00000000044e0e80 200b020 Enabled 0000000000000000:0000000000000000 00000000039980a0 0 MTA
26 b 204c 000000000450b110 200b220 Enabled 0000000000000000:0000000000000000 00000000039980a0 0 MTA

  

0:000> !ThreadState 3009220
Legal to Join
Background
CLR Owns
In Multi Threaded Apartment
Thread Pool Worker Thread
Interruptible
0:000> !ThreadState 200b220
Legal to Join
Background
CLR Owns
CoInitialized
In Multi Threaded Apartment
Interruptible
0:000> !ThreadState 8009220
Legal to Join
Background
CLR Owns
In Multi Threaded Apartment
Completion Port Thread

  

// threadstate详细: http://www.parallelfun.com/2012_11_01_archive.html

 0:050> !syncblk
Index SyncBlock MonitorHeld Recursion Owning Thread Info SyncBlock Owner
57 000000000456f9e8 1 1 0000000004567c30 1c98 46 00000001800b6e90 System.Object
Waiting threads:
141 00000000045702a8 1 1 0000000004567c30 1c98 46 00000001800b6f70 ProtoBufV2.Meta.BasicList
Waiting threads:
-----------------------------
Total 152
CCW 3
RCW 2
ComClassFactory 0
Free 64

  

// 46号托管线程拥有一个Monitor, MonitorHeld 的计算方法:(MonitorHeld-1)/2 个线程等待 46号线程。
// 例如: 线程91 的 MonitorHeld 是39,则:(39-1)/2=19 表示有19个线程等待线程91
// 这里的MonitorHeld按msdn的解释,拿锁的线程为1,等锁的线程为2; Monitorheld的值是偶数,也说明已经没有线程拿住锁了。

/*
Sync Block 的信息:
通过!syncblk 索引值 可以显示Sync Block的信息,主要有如下信息
Index: Sync Block Table中的Index值
Sync Block: Sync Block的地址
MonitorHeld: numbers of monitor held
Recursion: 该线程获取该sync block的次数
Owning thread info : 总共有三个值,第一个值是线程的数据结构地址,第二个是线程的系统线程ID,第三个值为线程的托管线程ID
SyncBlock Owner:指向拥有该SyncBlock的对象的内存地址,也就是Object的地址
如果有第二个值指这个synblock第二个Held的类型
统计信息:
Total    152   (sync block table的总的sync Lock数量)
CCW    3     (CCW(COM Callable Wrappers)对象拥有的sync block 数量)
RCW    2       (RCW(Runtime Callable Wrappers )对象拥有的sync block 数量)
ComClassFactory 0
Free 64 Sync block table 剩余的空索引的数量

*/

/*

http://blogs.msdn.com/b/oldnewthing/archive/2006/12/12/1266392.aspx

!syncblock 详细解释和 !critsec 使用
http://blogs.msdn.com/b/tess/archive/2006/01/09/a-hang-scenario-locks-and-critical-sections.aspx
*/

0:044> !syncblk
Index SyncBlock MonitorHeld Recursion Owning Thread Info SyncBlock Owner
2193 000000000598fe18 5 1 000000000a4650e0 3038 110 00000001bff69770 System.Object
Waiting threads: 90 121
2616 000000000577db08 133 0 0000000000000000 none 000000011fc6aa30 System.RuntimeType+RuntimeTypeCache+MemberInfoCache`1[[System.Reflection.RuntimeMethodInfo, mscorlib]]
Waiting threads: 39 40 42 48 49 52 53 58 60 63 65 67 69 73 75 76 77 79 81 82 83 85 86 87 88 93 94 95 97 99 100 104 105 107 108 109 110 111 112 114 115 117 120 122 123 124 128 129 132 133 134 135 136
2910 000000000577bae8 3 1 000000000a5821b0 134bc 78 000000019fc88170 System.Object
Waiting threads: 64
-----------------------------
Total 3256
CCW 3
RCW 2
ComClassFactory 1
Free 2992

  

//如果知道临界区的地址,可以用一下命令,如果不知道地址,可以用!locks

0:044> !critsec 000000011fc6aa30

DebugInfo for CritSec at 000000011fc6aa30 does not point back to the critical section
NOT an initialized critical section. CritSec +1fc6aa30 at 000000011fc6aa30
WaiterWoken Yes
LockCount -1
RecursionCount 0
OwningThread 0
EntryCount fef56b35
ContentionCount 2000007
*** Locked

  

// !locks 的输出
// http://msdn.microsoft.com/en-us/library/windows/hardware/ff541979(v=vs.85).aspx

0:105> !locks

CritSec +1192340 at 0000000001192340
WaiterWoken No
LockCount 0
RecursionCount 1
OwningThread bad0
EntryCount 0
ContentionCount 46a
*** Locked

  

//直接切换到进程上去:

0:014> ~~[bad0]s

  

//或者用 ~ 列出所有线程,然后切过去
//参考:http://blogs.msdn.com/b/tess/archive/2006/01/09/a-hang-scenario-locks-and-critical-sections.aspx

0:105> ~
# 0 Id: d9e0.a700 Suspend: 0 Teb: 000007ff`fffde000 Unfrozen
1 Id: d9e0.18bac Suspend: 0 Teb: 000007ff`fffdc000 Unfrozen
2 Id: d9e0.bfa0 Suspend: 0 Teb: 000007ff`fffd7000 Unfrozen
3 Id: d9e0.bad0 Suspend: 0 Teb: 000007ff`fffd3000 Unfrozen
4 Id: d9e0.16364 Suspend: 0 Teb: 000007ff`fff9e000 Unfrozen
0:014> ~3s

  

//

 0:046> .shell -i - -ci "~*e !clrstack" FIND  /i "Monitor.Enter"
0000000002f7d428 0000000076f6171a [HelperMethodFrame: 0000000002f7d428] System.Threading.Monitor.Enter(System.Object)
00000000049ad3e8 0000000076f6171a [HelperMethodFrame: 00000000049ad3e8] System.Threading.Monitor.Enter(System.Object)
0000000016a6d898 000007fef76500b9 [HelperMethodFrame: 0000000016a6d898] System.Threading.Monitor.Enter(System.Object)
0000000016b6e8b8 000007fef76500b7 [HelperMethodFrame: 0000000016b6e8b8] System.Threading.Monitor.Enter(System.Object)
.shell: Process exited
0:046> .shell -i - -ci "~*e !clrstack" FIND /i "Monitor.TryEnter"
.shell: Process exited
0:046> .shell -i - -ci "~*e !clrstack" FIND /i "Monitor"
000000000433eae8 0000000076f6186a [HelperMethodFrame_1OBJ: 000000000433eae8] System.Threading.Monitor.ObjWait(Boolean, Int32, System.Object)
000000000504e3b8 0000000076f6186a [HelperMethodFrame_1OBJ: 000000000504e3b8] System.Threading.Monitor.ObjWait(Boolean, Int32, System.Object)
000000000504e4e0 000007fef663d2ae System.Threading.Monitor.Wait(System.Object)
0000000002f7d428 0000000076f6171a [HelperMethodFrame: 0000000002f7d428] System.Threading.Monitor.Enter(System.Object)
00000000049ad3e8 0000000076f6171a [HelperMethodFrame: 00000000049ad3e8] System.Threading.Monitor.Enter(System.Object)
0000000016a6d898 000007fef76500b9 [HelperMethodFrame: 0000000016a6d898] System.Threading.Monitor.Enter(System.Object)
0000000016b6e8b8 000007fef76500b7 [HelperMethodFrame: 0000000016b6e8b8] System.Threading.Monitor.Enter(System.Object)
.shell: Process exited

  

// 有时候会发现无锁定同步快或死锁,还可以用!mlocks 看看

0:164> !dlk
Examining SyncBlocks...
Scanning for ReaderWriterLock instances...
Scanning for holders of ReaderWriterLock locks...
Scanning for ReaderWriterLockSlim instances...
Scanning for holders of ReaderWriterLockSlim locks...
Examining CriticalSections...
Scanning for threads waiting on SyncBlocks...
Scanning for threads waiting on ReaderWriterLock locks...
Scanning for threads waiting on ReaderWriterLocksSlim locks...
Scanning for threads waiting on CriticalSections...
No deadlocks detected.
0:164> !mlocks
Examining SyncBlocks...
Scanning for ReaderWriterLock instances...
Scanning for holders of ReaderWriterLock locks...
Scanning for ReaderWriterLockSlim instances...
Scanning for holders of ReaderWriterLockSlim locks...
Examining CriticalSections... ClrThread DbgThread OsThread LockType Lock LockLevel
------------------------------------------------------------------------------
0x67 116 0x1e8 thinlock 000000014036a2b0 (recursion:0)
0xab 182 0x268 thinlock 00000001c0724188 (recursion:0)
0xa4 177 0x14cc RWLockSlim 000000013ff0a358 Writer 0:164> !rwlock 000000013ff0a358
WriteLockOwnerThread: 0xa4
UpgradableReadLockOwnerThread: None
ReaderCount: 0
ReaderThreadIds: None
WaitingReaderCount: 204
WaitingReaderThreadIds: 0x9,0xa,0x11,0x12,0x13,0x14,0x15,0x16,0x17
WaitingWriterCount: 204
WaitingWriterThreadIds: 0x8,0xf,0x1d,0x21,0x24,0x2b,0x2f,0x30,
WaitingUpgradableReaderCount: 0
WaitingUpgradableReaderThreadIds: None
WaitingWriterUpgradeCount: 0
WaitingWriterUpgradeThreadIds: None ClrThread DbgThread OsThread LockType Lock LockLevel
------------------------------------------------------------------------------
0x15 19 0x15e8 thinlock 00000000010b6130 (recursion:0)
0x16c 226 0x1a1c SyncBlock 0000000000c735a8

  

// shell 命令:

.shell -ci "!mlocks -d" find "Writer"

  

参考:
http://weblogs.thinktecture.com/ingo/2006/08/who-is-blocking-that-mutex---fun-with-windbg-cdb-and-kd.html

http://blogs.msdn.com/b/tess/archive/2006/01/09/a-hang-scenario-locks-and-critical-sections.aspx

http://hi.baidu.com/ju_feng/item/e22f06974dafe530326eeb64

http://stackoverflow.com/questions/22037581/determining-which-method-is-holding-a-readerwriterlockslim-writelock

windbg .net 程序的死锁检测 常用方法(个人备份笔记)的更多相关文章

  1. linux死锁检测的一种思路【转】

    转自:http://www.cnblogs.com/mumuxinfei/p/4365697.html 前言:  上一篇博文讲述了pstack的使用和原理. 和jstack一样, pstack能获取进 ...

  2. linux死锁检测的一种思路

    前言: 上一篇博文讲述了pstack的使用和原理. 和jstack一样, pstack能获取进程的线程堆栈快照, 方便检验和性能评估. 但jstack功能更加的强大, 它能对潜在的死锁予以提示, 而p ...

  3. UNIX环境高级编程——记录上锁(fcntl函数)以及死锁检测

    一.记录锁 record locking 功能:当一个进程正在读或修改文件的某个部分时,它可以阻止其它进程修改同一文件区. 字节范围锁 byte-range locking 二.历史 flock函数, ...

  4. LiteOS:SpinLock自旋锁及LockDep死锁检测

    摘要:除了多核的自旋锁机制,本文会介绍下LiteOS 5.0引入的LockDep死锁检测特性. 2020年12月发布的LiteOS 5.0推出了全新的内核,支持SMP多核调度功能.想学习SMP多核调度 ...

  5. halcon——缺陷检测常用方法总结(测量拟合)

    引言 机器视觉中缺陷检测分为一下几种: blob分析+特征 模板匹配(定位)+差分:halcon--缺陷检测常用方法总结(模板匹配(定位)+差分) - 唯有自己强大 - 博客园 (cnblogs.co ...

  6. matlab练习程序(SUSAN检测)

    matlab练习程序(SUSAN检测) SUSAN算子既可以检测角点也可以检测边缘,不过角点似乎比不过harris,边缘似乎比不过Canny.不过思想还是有点意思的. 主要思想就是:首先做一个和原图像 ...

  7. 为什么MySQL死锁检测会严重降低TPS

    在大量的客户端,更新数据表的同一行时,会造成数据库的吞吐量大幅降低. 很多数据库的前辈和同行分别通过实验和源码的方法,定位到了罪魁祸首----MySQL死锁检测 实验方式:http://blog.cs ...

  8. 14.3.5.2 Deadlock Detection and Rollback 死锁检测和回滚:

    14.3.5.2 Deadlock Detection and Rollback 死锁检测和回滚: InnoDB 自动检查四艘,回滚一个事务或者事务来打破死锁. InnoDB 试图选择小的事务来回滚, ...

  9. Linux死锁检测-Lockdep

    关键词:LockDep.spinlock.mutex. lockdep是内核提供协助发现死锁问题的功能. 本文首先介绍何为lockdep,然后如何在内核使能lockdep,并简单分析内核lockdep ...

随机推荐

  1. Spring-解决请求中文乱码问题

    解决spring请求中文乱码问题 1.web.xml添加编码拦截器 <filter> <filter-name>CharacterEncoding</filter-nam ...

  2. Writing analyzers

    Writing analyzers There are times when you would like to analyze text in a bespoke fashion, either b ...

  3. python中json库中的load、loads、dump、dumps的区别与用法

    一.json.dumps(i): json中的dumps方法是用来将特定格式的数据进行字符串化的操作,比如列表字典都可以进行字符串化操作然后写入json的file:而且如果是要写入json文件就必须要 ...

  4. 03-树3 Tree Traversals Again (25 分)

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...

  5. 虚拟机下设置CentOS 7使用固定IP地址

    1.设置虚拟机使用桥接网络 2.查看安装虚拟机软件的电脑IP信息 3.启动CentOS 7进行设置

  6. SQL LIKE 通配符随笔

    通配符 说明 _    与任意单字符匹配 %    与包含一个或多个字符的字符串匹配 [ ]  与特定范围(例如,[a-f])或特定集(例如,[abcdef])中的任意单字符匹配. [^]  与特定范 ...

  7. jni使用javap查看java类方法签名

    在Jni开发中,需要回调给java层数据,因此使用java的方法签名是必不可少的. 快速定位java方法签名的方式: java方法签名由(函数参数列表)返回值组成. cmd运行:javap -s 字节 ...

  8. 【Kafka源码】Kafka启动过程

    一般来说,我们是通过命令来启动kafka,但是命令的本质还是调用代码中的main方法,所以,我们重点看下启动类Kafka.源码下下来之后,我们也可以通过直接运行Kafka.scala中的main方法( ...

  9. interface类型

    接口可使用的修饰符如下: InterfaceModifier: one of Annotation public protected private abstract static strictfp  ...

  10. SpringMVC 的工作机制

    在一个工程中如果想要使用 SpringMVC的话,只需要两个步骤 在web.xml中配置一个DispatcherServlet.需要配置一个org.springframework.web.servle ...