Friday, September 4, 2009

[Utility][WinDbg] !htrace for debugging handle leak

Windbg Debugger’s !htrace extension is very good at debugging handle leaks. See the below steps how you can do it.

1. Install Debugging Tool for windows from here.
2. Run WinDbg installed with Debugging Tools in Step 1.
3. Take File->Attach to a process... menu and select your application in the process list
4. Application will break and change in to debug mode
5. Take File->Symbol File path and add the path where your application's pdb file is available
6. In the debug prompt enter !htrace -enable and press enter.
7. Now take Debug->Go menu, then you can operate on your application
8. Now run your scenario where your application is assumed to have handle leak.
9. Now take Debug->Break menu, for changing the application to debug mode.
10. Now enter !htrace -diff in the debug prompt and press enter. Here it list the handle leak logs.

Below is an example of the log generated for 4 opened handle in my test app. From the log you can see where exactly the handle is leaked in the code.
0:001>
!htrace -diff
Handle tracing information snapshot successfully taken.
0x26 new stack traces since the previous snapshot.
Ignoring handles that were already closed...
Outstanding handles opened since the previous snapshot:
--------------------------------------
Handle = 0x0000070c - OPEN
Thread ID = 0x00001758, Process ID = 0x00001248

0x7c8308eb: kernel32!CreateEventA+0x00000068
*** WARNING: Unable to verify checksum for D:\TestFolder\MiscTest\Release\MiscTest.exe
0x0040172b: MiscTest!CMiscTestDlg::OnButtonHandle+0x0000001a
0x73dd24c0: MFC42!_AfxDispatchCmdMsg+0x00000082
0x73dd23bf: MFC42!CCmdTarget::OnCmdMsg+0x0000010a
0x73e3dead: MFC42!CPropertySheet::OnCmdMsg+0x0000001d
0x73dd3244: MFC42!CWnd::OnCommand+0x00000053
0x73dd1bf1: MFC42!CWnd::OnWndMsg+0x0000002f
0x73dd1b9b: MFC42!CWnd::WindowProc+0x00000024
0x73dd1b05: MFC42!AfxCallWndProc+0x00000091
0x73dd1a58: MFC42!AfxWndProc+0x00000036
0x73e6847d: MFC42!AfxWndProcBase+0x00000039
--------------------------------------
Handle = 0x00000710 - OPEN
Thread ID = 0x00001758, Process ID = 0x00001248

0x7c8308eb: kernel32!CreateEventA+0x00000068
0x0040172b: MiscTest!CMiscTestDlg::OnButtonHandle+0x0000001a
0x73dd24c0: MFC42!_AfxDispatchCmdMsg+0x00000082
0x73dd23bf: MFC42!CCmdTarget::OnCmdMsg+0x0000010a
0x73e3dead: MFC42!CPropertySheet::OnCmdMsg+0x0000001d
0x73dd3244: MFC42!CWnd::OnCommand+0x00000053
0x73dd1bf1: MFC42!CWnd::OnWndMsg+0x0000002f
0x73dd1b9b: MFC42!CWnd::WindowProc+0x00000024
0x73dd1b05: MFC42!AfxCallWndProc+0x00000091
0x73dd1a58: MFC42!AfxWndProc+0x00000036
0x73e6847d: MFC42!AfxWndProcBase+0x00000039
--------------------------------------
Handle = 0x00000714 - OPEN
Thread ID = 0x00001758, Process ID = 0x00001248

0x7c8308eb: kernel32!CreateEventA+0x00000068
0x0040172b: MiscTest!CMiscTestDlg::OnButtonHandle+0x0000001a
0x73dd24c0: MFC42!_AfxDispatchCmdMsg+0x00000082
0x73dd23bf: MFC42!CCmdTarget::OnCmdMsg+0x0000010a
0x73e3dead: MFC42!CPropertySheet::OnCmdMsg+0x0000001d
0x73dd3244: MFC42!CWnd::OnCommand+0x00000053
0x73dd1bf1: MFC42!CWnd::OnWndMsg+0x0000002f
0x73dd1b9b: MFC42!CWnd::WindowProc+0x00000024
0x73dd1b05: MFC42!AfxCallWndProc+0x00000091
0x73dd1a58: MFC42!AfxWndProc+0x00000036
0x73e6847d: MFC42!AfxWndProcBase+0x00000039
--------------------------------------
Handle = 0x00000718 - OPEN
Thread ID = 0x00001758, Process ID = 0x00001248

0x7c8308eb: kernel32!CreateEventA+0x00000068
0x0040172b: MiscTest!CMiscTestDlg::OnButtonHandle+0x0000001a
0x73dd24c0: MFC42!_AfxDispatchCmdMsg+0x00000082
0x73dd23bf: MFC42!CCmdTarget::OnCmdMsg+0x0000010a
0x73e3dead: MFC42!CPropertySheet::OnCmdMsg+0x0000001d
0x73dd3244: MFC42!CWnd::OnCommand+0x00000053
0x73dd1bf1: MFC42!CWnd::OnWndMsg+0x0000002f
0x73dd1b9b: MFC42!CWnd::WindowProc+0x00000024
0x73dd1b05: MFC42!AfxCallWndProc+0x00000091
0x73dd1a58: MFC42!AfxWndProc+0x00000036
0x73e6847d: MFC42!AfxWndProcBase+0x00000039
--------------------------------------
Displayed 0x4 stack traces for outstanding handles opened since the previous snapshot.

No comments: