我有一个简单的测试程序导致无限期等待锁定。
public class SyncBlock
{
}
class Program
{
public static SyncBlock sync = new SyncBlock();
private static void ThreadProc()
{
try
{
Monitor.Enter(sync);
}
catch (Exception)
{
//Monitor.Exit(sync);
Console.WriteLine("3rd party code threw an exception");
}
}
static void Main(string[] args)
{
Thread newThread = new Thread(ThreadProc);
newThread.Start();
Console.WriteLine("Acquiring lock");
Monitor.Enter(sync);
Console.WriteLine("Releasing lock");
Monitor.Exit(sync);
}
}
因此主线程在尝试执行Monitor.Enter(sync)时基本上被锁定。如果我看看主线程上的clrStack,它的输出基本上显示它有意义但是当我试图看到堆栈的原生端时,我期待看到一些等待单/多对象类型的调用,但我看不到它。任何人都可以解释它。感谢
0:000> !CLRStack
未加载mscorwks.dll的PDB符号
操作系统线程ID:0x1e8(0)
ESP EIP
0012f0a8 77455e74 [GCFrame:0012f0a8]
0012f178 77455e74 [HelperMethodFrame_1OBJ:0012f178] System.Threading.Monitor.Enter(System.Object)
0012f1d0 00a40177 ConsoleApplication1.Program.Main(System.String [])
0012f400 70fc1b4c [GCFrame:0012f400]
0:000> KB
ChildEBP RetAddr Args to Child
警告:堆栈展开信息不可用。以下框架可能是错误的
0012eeb4 710afb92 0012ee68 002d6280 00000000 ntdll!KiFastSystemCallRet
0012ef1c 710af7c3 00000001 002d6280 00000000 mscorwks!StrongNameFreeBuffer + 0x1b1f2
0012ef3c 710af8cc 00000001 002d6280 00000000 mscorwks!StrongNameFreeBuffer + 0x1ae23
0012efc0 710af961 00000001 002d6280 00000000 mscorwks!StrongNameFreeBuffer + 0x1af2c
0012f010 710afae1 00000001 002d6280 00000000 mscorwks!StrongNameFreeBuffer + 0x1afc1
0012f06c 70fdc5ae ffffffff 00000001 00000000 mscorwks!StrongNameFreeBuffer + 0x1b141
0012f080 710df68a ffffffff 00000001 00000000 mscorwks!LogHelp_NoGuiOnAssert + 0x10562
0012f10c 710b1154 002aad90 ffffffff 002aad90 mscorwks!StrongNameFreeBuffer + 0x4acea
0012f128 710b10d8 42b8b47d 00000000 002aad90 mscorwks!StrongNameFreeBuffer + 0x1c7b4
0012f1e0 70fc1b4c 0012f1f0 0012f230 0012f270 mscorwks!StrongNameFreeBuffer + 0x1c738
0012f1f0 70fd2219 0012f2c0 00000000 0012f290 mscorwks + 0x1b4c
0012f270 70fe6591 0012f2c0 00000000 0012f290 mscorwks!LogHelp_NoGuiOnAssert + 0x61cd
0012f3ac 70fe65c4 0023c038 0012f478 0012f444 mscorwks!CoUninitializeEE + 0x2ead
0012f3c8 70fe65e2 0023c038 0012f478 0012f444 mscorwks!CoUninitializeEE + 0x2ee0
0012f3e0 7103389d 0012f444 42b8b0f1 00000000 mscorwks!CoUninitializeEE + 0x2efe
0012f544 710337bd 002332e0 00000001 0012f580 mscorwks!GetPrivateContextsPerfCounters + 0xf546
0012f7ac 71033d0d 00000000 42b8b9c9 00000001 mscorwks!GetPrivateContextsPerfCounters + 0xf466
0012fc7c 71033ef7 00ce0000 00000000 42b8979 mscorwks!GetPrivateContextsPerfCounters + 0xf9b6
0012fccc 71033e27 00ce0000 42b8b8a1 00000000 mscorwks!CorExeMain + 0x168
*错误:找不到符号文件。默认导出C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ mscoreei.dll的符号 -
0012fd14 71cf55ab 71033d8f 0012fd30 71f37f16 mscorwks!CorExeMain + 0x98
* 错误:找不到符号文件。默认导出C:\ Windows \ system32 \ mscoree.dll的符号 -
0012fd20 71f37f16 00000000 71cf0000 0012fd44 mscoreei!CorExeMain + 0x38
0012fd30 71f34de3 00000000 7723d0e9 7ffd8000 mscoree!CreateConfigStream + 0x13f
0012fd44 774319bb 7ffd8000 084952f9 00000000 mscoree!CorExeMain + 0x8
0012fd84 7743198e 71f34ddb 7ffd8000 00000000 ntdll!RtlInitializeExceptionChain + 0x63
0012fd9c 00000000 71f34ddb 7ffd8000 00000000 ntdll!RtlInitializeExceptionChain + 0x36
答案 0 :(得分:4)
您必须将windbg指向microsoft windows符号服务器才能获得良好的堆栈跟踪。
在windbg命令窗口中输入以下内容:
.sympath srv * c:\ websymbols * http://msdl.microsoft.com/download/symbols
另见:
Using microsoft symbol server to get symbols
另外,要回答有关如何调试此问题的原始问题,请参阅本手册:
0:000> !clrstack OS Thread Id: 0x1358 (0) ESP EIP 0012f328 7c90e514 [GCFrame: 0012f328] 0012f3f8 7c90e514 [HelperMethodFrame_1OBJ: 0012f3f8] System.Threading.Monitor.Enter(System.Object) 0012f450 00d10177 Program.Main(System.String[]) 0012f688 79e71b4c [GCFrame: 0012f688]
在原始程序中,首先启动后台线程。所以,它获得了锁定。然而,它在没有释放锁的情况下退出。之后,你的主线程试图获取锁,并且因为锁已经拥有而被卡住了。
你如何找出谁拥有它?首先做一个!线程后跟!syncblk。
0:000> !threads ThreadCount: 3 UnstartedThread: 0 BackgroundThread: 1 PendingThread: 0 DeadThread: 1 Hosted Runtime: no PreEmptive GC Alloc Lock ID OSID ThreadOBJ State GC Context Domain Count APT Exception 0 1 1358 0014bb00 200a020 Enabled 00000000:00000000 001540d0 0 MTA 2 2 1360 0015e320 b220 Enabled 00000000:00000000 001540d0 0 MTA (Finalizer) XXXX 3 0 00175a98 9820 Enabled 00000000:00000000 001540d0 1 Ukn 0:000> !syncblk Index SyncBlock MonitorHeld Recursion Owning Thread Info SyncBlock Owner 2 0017903c 3 1 00175a98 0 XXX 013503cc SyncBlock ----------------------------- Total 2 CCW 0 RCW 0 ComClassFactory 0 Free 0
如您所见,!syncblk表示所拥有的线程对象是 00175a98 。从!threads输出,你可以看到线程对象00175a98是拥有锁时退出的死线程。
希望这有帮助。