windbg 클래식 잠금 디버깅

9030 단어 DB
코드
// 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 
"); EnterCriticalSection(&gCritSecThird); { Sleep(5000); EnterCriticalSection(&gCritSecFirst); EnterCriticalSection(&gCritSecSecond); LeaveCriticalSection(&gCritSecSecond); LeaveCriticalSection(&gCritSecFirst); } LeaveCriticalSection(&gCritSecThird); printf("Exit thread func 1
"); return 1; } unsigned __stdcall Thread2Func(void* dummy) { printf("Enter Thread 2
"); EnterCriticalSection(&gCritSecFirst); EnterCriticalSection(&gCritSecSecond); { Sleep(2000); EnterCriticalSection(&gCritSecThird); Sleep(5000); LeaveCriticalSection(&gCritSecThird); } LeaveCriticalSection(&gCritSecSecond); LeaveCriticalSection(&gCritSecFirst); printf("Exit thread func 2
"); return 1; } int _tmain(int argc, _TCHAR* argv[]) { unsigned threadID; InitializeCriticalSection(&gCritSecFirst); InitializeCriticalSection(&gCritSecSecond); InitializeCriticalSection(&gCritSecThird); _beginthreadex( NULL, 0, &Thread1Func, NULL, 0, &threadID ); _beginthreadex( NULL, 0, &Thread2Func, NULL, 0, &threadID ); while(1); return 0; }

이것은 고전적인 사라진 자물쇠로 두 개의 라인을 열고 하나는 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을 기다리는 자물쇠(스택의 Rtl EnterCritical Section 함수 부분에 따라 보기) 0069fe94 0042d714 004944c0 57565554 5b5a5958ntdll!Rtl EnterCritical Section+0x46(FPO:[1,0,0]) 그리고 004844c0의 자물쇠는 라인 번호 1e0이 라인 1이고 라인 번호 1e0이 라인 1이 004944f0을 기다리는 자물쇠입니다. 이 모든 라인 번호 1e4는 0059fe94 0042d5e0 004944f0 57565554 5b5a5958ntdll을 가지고 있습니다!RtlEnterCriticalSection+0x46(FPO:[1,0,0]) 데드 로크!!!원인을 찾으면 수정하기 쉽다.
 

좋은 웹페이지 즐겨찾기