about using Windebug to view thread deadlock issues

Source: Internet
Author: User

The recent project is nearing the end, but the project to the test when the big problem comes, occasionally the interface directly to die so that the subsequent can not be tested, the initial suspicion is where the deadlock, because they are not very familiar with Windebug, only know Windebug have to find memory leaks, thread deadlock and other related functions, So commanded the following people with Windebug to find the problem, straight-line network resources more, through a survey to find the Windebug deadlock find solution, but as a project development manager, encounter deadlock should be able to other at the same time can not be resolved when the corresponding help is taken for granted, So with this mentality to learn windebug find the method of deadlock, summarized as follows:


Deadlock, the condition of establishment is:

(1) There are at least two locks above, if only two locks, and lock A and lock B;

(2) Thread 1 has taken a lock a and then wants to get the lock B; thread 2 has taken a lock B and then wants to get the lock A;

(3) did not get another lock do not forcibly release their acquired locks;


So, the deadlock has come ~


Implement all the instances:

(1) Initialize the two locks A and B, start two threads (can be a main thread and a sub-thread);

(2) The first thread has taken the lock A, the second thread has taken the lock B, the front-line attempt to take the lock B, the second thread tries to get the lock A;


Based on the above thought, I wrote a demon, there is a deadlock situation, and then use Windebug to find the deadlock situation, the method is described as follows:

(1) Enable the application's user stack feature-I use windebug to start without enabling the windebug bind process to fail;

Method use Gflags.exe in Windebug directory: Open command line, go to Windebug directory to run: gflag.exe/i debug exe full path +ust

Then enter, UST is the user stack;

(2) Open Windebug and select File-->attach to process to attach to the processes that need to be debugged EXE;

If you need to view all the thread stacks then enter in the command window: ~*kv

Output all thread stacks as follows:


You can open the stack of all threads at this time, if you need to see the stack of a thread, enter *1kv is the stack of print thread 1; from the stack of all threads we can see the stack information of each thread, and if there is a stack lock at this point, there will usually be an API call at the top of the stack: ntdll! Rtlpwaitoncriticalsection, that is, to find all the API calls we can find the corresponding deadlock thread information;

0 id:11264.10f20Suspend:1 teb:7efdd000 unfrozen
ChildEBP RetAddr Args to Child
003EECD8 77709e2e 00000124 00000000 00000000 ntdll! zwwaitforsingleobject+0x15 (FPO: [3,0,0])
003EED3C 77709d12 00000000 00000000 00000001 ntdll! rtlpwaitoncriticalsection+0x13e (FPO: [Non-fpo])
003eed64 0113e2890161cd80c3c8e76a 00000001 ntdll!rtlentercriticalsection+0x150 (FPO: [Non-fpo])
003eee7c 011548ff 003ef808 003eeeb4 757d62fa lockdemon! clockdemondlg::oninitdialog+0x179 (FPO: [Non-fpo]) (conv:thiscall) [e:\work\c++\test\lockdemon\lockdemon\ Lockdemondlg.cpp @ 122]
003eee88 757d62fa 002d1984 00000110 001d1b26 lockdemon! afxdlgproc+0x3f (Conv:stdcall) [F:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dlgcore.cpp @ 28]
003EEEB4 757ff9df 01119035 002d1984 00000110 user32! internalcallwinproc+0x23
003EEF30 757ff784 00000000 01119035 002d1984 user32! Usercalldlgproccheckwow+0xd7 (FPO: [Non-fpo])
003eef80 757ff889 0409a140 00000000 00000110 user32! Defdlgprocworker+0xb7 (FPO: [Non-fpo])
003eefa0 757d62fa 002d1984 00000110 001d1b26 user32! Defdlgprocw+0x29 (FPO: [Non-fpo])
003EEFCC 757d6d3a 77734308 002d1984 00000110 user32! internalcallwinproc+0x23
003ef044 757e0d27 00000000 77734308 002d1984 user32! usercallwinproccheckwow+0x109 (FPO: [Non-fpo])
003ef07c 757e0d4d 77734308 002d1984 00000110 user32! Callwindowprocaorw+0xab (FPO: [Non-fpo])
003ef09c 0115e4d4 77734308 002d1984 00000110 user32! CALLWINDOWPROCW+0X1B (FPO: [Non-fpo])
003EF0C0 0115c5fd 00000110 001d1b26 00000000 lockdemon! CWnd::D efwindowprocw+0x34 (Conv:thiscall) [F:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp @ 1043]
003EF0DC 01156235 003ef808 003ef0f8 012282d3 lockdemon! CWnd::D efault+0x3d (Conv:thiscall) [F:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp @ 274]
003ef100 011603e5 001d1b26 00000000 c3c8fb46 lockdemon! Cdialog::handleinitdialog+0xd5 (Conv:thiscall) [F:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dlgcore.cpp @ 673]
003ef250 0115fb62 00000110 001d1b26 00000000 lockdemon! cwnd::onwndmsg+0x835 (Conv:thiscall) [F:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp @ 2018]
003ef270 0115c400 00000110 001d1b26 00000000 lockdemon! cwnd::windowproc+0x32 (Conv:thiscall) [F:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp @ 1755]
003ef2ec 0115cb16 003ef808 002d1984 00000110 lockdemon! AFXCALLWNDPROC+0XF0 (Conv:stdcall) [F:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp @ 240]
003ef30c 757d62fa 002d1984 00000110 001d1b26 lockdemon! Afxwndproc+0xa6 (Conv:stdcall) [F:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp @ 403]
003ef338 757d6d3a 01127310 002d1984 00000110 user32! internalcallwinproc+0x23
003ef3b0 757d6de8 00000000 01127310 002d1984 user32! usercallwinproccheckwow+0x109 (FPO: [Non-fpo])
003ef40c 757d6e44 0409a140 00000000 00000110 user32! DISPATCHCLIENTMESSAGE+0XE0 (FPO: [Non-fpo])
003ef448 776e010a 003ef460 00000000 003ef694 user32!__fndword+0x2b (FPO: [Non-fpo])
003ef45c 0409a140 00000000 00000110 001d1b26 ntdll! KIUSERCALLBACKDISPATCHER+0X2E (FPO: [0,0,0])
Warning:frame IP not in any known module. Following frames may wrong.
003ef4c0 7580206f 0409a140 00000000 01127310 0x409a140
003ef594 758010d3 00f00000 00000006 000000a4 user32! internalcreatedialog+0xb9f (FPO: [Non-fpo])
003ef5b8 757ec659 00f00000 0163fdc8 00000000 user32! CREATEDIALOGINDIRECTPARAMAORW+0X33 (FPO: [Non-fpo])
003ef5d8 01155513 00f00000 0163fdc8 00000000 user32! CREATEDIALOGINDIRECTPARAMW+0X1B (FPO: [Non-fpo])
003ef6a0 01155e39 0163fdc8 00000000 00f00000 lockdemon! cwnd::createdlgindirect+0x263 (Conv:thiscall) [F:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dlgcore.cpp @ 312]
003ef714 0113da0d C3c8f1a6 00000000 00000000 lockdemon! CDialog::D omodal+0x199 (Conv:thiscall) [F:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dlgcore.cpp @ 576]
003ef8b0 0153da94 757d9ee1 003ef8c0 00280026 lockdemon! Clockdemonapp::initinstance+0xad (Conv:thiscall) [E:\work\c++\test\lockdemon\lockdemon\lockdemon.cpp @ 64]
003ef8d4 0153d98a 00f00000 00000000 006710b4 lockdemon! afxwinmain+0x84 (Conv:stdcall) [F:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\winmain.cpp @ 37]
003ef8ec 014c1015 00f00000 00000000 006710b4 lockdemon!wwinmain+0x1a (conv:stdcall) [f:\dd\vctools\vc7libs\ship\ Atlmfc\src\mfc\appmodul.cpp @ 34]
003ef990 014c0e9f 003ef9a4 76e0336a 7efde000 lockdemon!__tmaincrtstartup+0x165 (conv:cdecl) [f:\dd\vctools\crt_bld\ SELF_X86\CRT\SRC\CRT0.C @ 263]
003ef998 76e0336a 7efde000 003ef9e4 777092b2 lockdemon!wwinmaincrtstartup+0xf (conv:cdecl) [f:\dd\vctools\crt_bld\ SELF_X86\CRT\SRC\CRT0.C @ 182]
003EF9A4 777092b2 7efde000 61654582 00000000 kernel32! Basethreadinitthunk+0xe (FPO: [Non-fpo])
003ef9e4 77709285 0111a958 7efde000 00000000 ntdll!__rtluserthreadstart+0x70 (FPO: [Non-fpo])
003EF9FC 00000000 0111a958 7efde000 00000000 ntdll!_rtluserthreadstart+0x1b (FPO: [Non-fpo])


1id:11264.11284Suspend:1 teb:7efda000 unfrozen
ChildEBP RetAddr Args to Child
0631fe7c 759714ab 00000104 00000000 00000000 ntdll! zwwaitforsingleobject+0x15 (FPO: [3,0,0])
0631fee8 76e01194 00000104 Ffffffff 00000000 kernelbase! Waitforsingleobjectex+0x98 (FPO: [Non-fpo])
0631ff00 76e01148 00000104 Ffffffff 00000000 kernel32! WAITFORSINGLEOBJECTEXIMPLEMENTATION+0X75 (FPO: [Non-fpo])
0631ff14 7235107b 00000104 ffffffff 76e010ff kernel32! waitforsingleobject+0x12 (FPO: [Non-fpo])
Warning:stack unwind information not available. Following frames may wrong.
0631ff58 7235290a 72377db8 76e0336a 72377db8 cksee+0x107b
0631FF6C 777092b2 72377db8 676a43ca 00000000 cksee! Kinkoo_getinterface+0x13aa
0631FFAC 77709285 72352900 72377db8 00000000 ntdll!__rtluserthreadstart+0x70 (FPO: [Non-fpo])
0631ffc4 00000000 72352900 72377db8 00000000 ntdll!_rtluserthreadstart+0x1b (FPO: [Non-fpo])


2id:11264.9238Suspend:1 teb:7efd7000 unfrozen
ChildEBP RetAddr Args to Child
064efbf4 77709e2e 00000120 00000000 00000000 ntdll! zwwaitforsingleobject+0x15 (FPO: [3,0,0])
064EFC58 77709d12 00000000 00000000 005819a0 ntdll! rtlpwaitoncriticalsection+0x13e (FPO: [Non-fpo])
064efc80 01140f780161cd6800000000 00000000 ntdll!rtlentercriticalsection+0x150 (FPO: [Non-fpo])
064efd58 014c22e3 003ef808 c5b8f482 00000000 lockdemon! myfunc+0x48 (FPO: [Non-fpo]) (conv:stdcall) [E:\work\c++\test\lockdemon\lockdemon\lockdemondlg.cpp @ 79]
064efd94 014c2254 00000000 064efdac 76e0336a lockdemon!_callthreadstartex+0x53 (conv:cdecl) [f:\dd\vctools\crt_bld\ SELF_X86\CRT\SRC\THREADEX.C @ 348]
064efda0 76e0336a 005819a0 064efdec 777092b2 lockdemon!_threadstartex+0xa4 (conv:stdcall) [f:\dd\vctools\crt_bld\self _X86\CRT\SRC\THREADEX.C @ 331]
064efdac 777092b2 005819a0 6715418a 00000000 kernel32! Basethreadinitthunk+0xe (FPO: [Non-fpo])
064efdec 77709285 014c21b0 005819a0 00000000 ntdll!__rtluserthreadstart+0x70 (FPO: [Non-fpo])
064efe04 00000000 014c21b0 005819a0 00000000 ntdll!_rtluserthreadstart+0x1b (FPO: [Non-fpo])

#3 id:11264.1175cSuspend:1 teb:7ef9f000 unfrozen
ChildEBP RetAddr Args to Child
0580fcf8 7776fb96 64db414e 00000000 00000000 ntdll! DbgBreakPoint (FPO: [0,0,0])
0580FD28 76e0336a 00000000 0580fd74 777092b2 ntdll! DBGUIREMOTEBREAKIN+0X3C (FPO: [Non-fpo])
0580fd34 777092b2 00000000 64db4112 00000000 kernel32! Basethreadinitthunk+0xe (FPO: [Non-fpo])
0580fd74 77709285 7776fb5a 00000000 00000000 ntdll!__rtluserthreadstart+0x70 (FPO: [Non-fpo])
0580fd8c 00000000 7776fb5a 00000000 00000000 ntdll!_rtluserthreadstart+0x1b (FPO: [Non-fpo])


It can be seen that there is a total of 0-3 4 threads, green indicates the thread thread index number, The blue indicates the address of the thread (the 11,264-bit thread in 11264.1175c is id,1175c the thread address), and of course all threads print out a lock is not easy to observe the deadlock, in order to be able to directly print a deadlock between the threads of the process, directly using the following command

First, look at all the locks in the process, enter the command:!locks, as follows:
0:003> !locks

This prints out all the lock information in the process:

Critsec lockdemon!g_locka+0 at 0161cd80
Waiterwoken No
Lockcount 1
Recursioncount 1
OwningThread 9238
EntryCount 0
Contentioncount 1
Locked


Critsec lockdemon!g_lockb+0 at 0161cd68
Waiterwoken No
Lockcount 1
Recursioncount 1
OwningThread 10f20
EntryCount 0
Contentioncount 1
Locked


The first lock address is 0161cd80,lockcount, which represents the number of locks that are held, Recursioncount indicates the number of times the owner thread has entered the lock (which can be entered several times after the lock is taken), and the OwningThread owner thread is 9238, that is, the 0161CD80 lock is the address of 9238 thread occupancy, that is, the thread index number 2, thread ID 11264 thread address 9238 thread has taken the lock 0161cd80, but waiting for the lock 0161cd68

The second lock address is 0161cd68,lockcount, which represents the number of locks that are held, Recursioncount indicates the number of times the owner thread has entered the lock (which can be entered several times after the lock is reached), and the OwningThread owner thread is 10f20 , which means that the 0161cd68 lock is 10f20 thread occupancy, that is, thread index number is 0, thread ID is 11264 thread address is 10f20 thread has taken the lock 0161cd68, but waiting for the lock 0161cd80


This way the deadlock is so determined that thread 0 and thread 2 interlock!


The Stack column information is interpreted as follows:

2id:11264.9238Suspend:1 teb:7efd7000 unfrozen
ChildEBP RetAddr Args to Child
064efbf4 77709e2e 00000120 00000000 00000000 ntdll! zwwaitforsingleobject+0x15 (FPO: [3,0,0])
064EFC58 77709d12 00000000 00000000 005819a0 ntdll! rtlpwaitoncriticalsection+0x13e (FPO: [Non-fpo])
064efc80 01140f780161cd6800000000 00000000 ntdll! rtlentercriticalsection+0x150 (FPO: [Non-fpo])
064efd58 014c22e3 003ef808 c5b8f482 00000000 lockdemon! myfunc+0x48 (FPO: [Non-fpo]) (conv:stdcall) [E:\work\c++\test\lockdemon\lockdemon\lockdemondlg.cpp @ 79]
064efd94 014c2254 00000000 064efdac 76e0336a lockdemon!_callthreadstartex+0x53 (conv:cdecl) [f:\dd\vctools\crt_bld\ SELF_X86\CRT\SRC\THREADEX.C @ 348]
064efda0 76e0336a 005819a0 064efdec 777092b2 lockdemon!_threadstartex+0xa4 (conv:stdcall) [f:\dd\vctools\crt_bld\self _X86\CRT\SRC\THREADEX.C @ 331]
064efdac 777092b2 005819a0 6715418a 00000000 kernel32! Basethreadinitthunk+0xe (FPO: [Non-fpo])
064efdec 77709285 014c21b0 005819a0 00000000 ntdll!__rtluserthreadstart+0x70 (FPO: [Non-fpo])
064efe04 00000000 014c21b0 005819a0 00000000 ntdll!_rtluserthreadstart+0x1b (FPO: [Non-fpo])

The third column is the first parameter of the next API, for the API is the first parameter of rtlentercriticalsection, namely the third column is the address of the lock, that is, thread ID 2 (thread address is 0x9238) line thread is waiting lock 0x 0161cd68


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

about using Windebug to view thread deadlock issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.