Once you start Performance Monitor to monitor handles count, note how many handles the process "inetinfo" has.
In our scenario, it shows 427 handles. Verify this count in windbg right after you attach by issuing this command:
0:018> !handle
Handle 4
Type Section
Handle 8
Type Event
Handle c
Type Event
.......
427 Handles
Type Count
Event 114
Section 5
File 181
Port 9
Directory 3
Mutant 8
WindowStation 3
S emaphore 42
Key 27
Token 4
Thread 24
Desktop 2
IoCompletion 5
In windbg, type in "g", and back to Internet Explorer, keep pressing "Handle Leak" button. (or just paste the URL that causes the leak in a new
IE window and keep pressing F5). This is the URL "http://localhost/iisdebuglabs/site/fil_hl1?sf=handleleak1_v1"
Now performance Monitor shows 512 handles open. Back to Windbg, press "Ctrl_break" and run the "!handle" command again.
Here is the output
In Windbg
0:031> !handle
Handle 4
Type Section
Handle 8
Type Event
Handle c
Type Event
.......
512 Handles
Type Count
Event 115
Section 5
File 181
Port 9
Directory 3
Mutant 8
WindowStation 3
Semaphore 42
Key 110
Token 4
Thread 25
Desktop 2
IoCompletion 5
Based on this, we can see that the handle that we are leaking is a handle to a registry key. the key handle count jumped from 27 to 110.
Issue the " !handle 0 4 " to include the handle type information in the display
0:031> !handle 0 4
.........
.........
.........
Handle 650
Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions
Handle 654
Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions
Handle 658
Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions
Handle 65c
Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions
Handle 660
Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions
Handle 664
Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions
Handle 668
Name \REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions
.............
.............
.............
So this shows that the handle we are leaking is to he registry key "\REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\ProductOptions"
Now let try to see who is leaking the handle to the registry key mentioned above. To do this, we are going to set a break point.
In windbg command line, type in this command:
0:003> bp ADVAPI32!RegOpenKeyExA
This will set a break point in RegOpenKeyExA. When the break point is hit, the debugger breaks.
In Windbg command line, type in "g"
In the browser, click the "Leak Handle" button, and notice that the debugger breaks.
- Point the debugger to use the private symbols
- For company wide internal symbols, use \\symbols\symbols
- For the component private symbols, use C:\IISDebugLabs\Symbols\Pri
Issue a "kvn50" in the debugger's command line.
Having the private symbols lined up, here is how the stack would look like:
0:008> kvn50
# ChildEBP RetAddr Args to Child
00 0129f5c0 016b1c96 80000002 016b1428 00000000 ADVAPI32!RegOpenKeyExA (FPO: [Non-Fpo])
01 0129f5dc 016b1752 00000001 00e84f20 00e84e00 badfil!doHandleLeak1+0x21 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\filDBG\badfil.cpp @ 672]
02 0129fbf4 65f0354f 00000001 00004000 08000002 badfil!HttpFilterProc+0x250 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\filDBG\badfil.cpp @ 281]
03 0129fc1c 65f034bc 000003f8 00004000 00000002 w3svc!FILTER_LIST::NotifyFilters+0x9d (FPO: [Non-Fpo])
04 0129fc50 65f02cad 0129fedc 0129fedc 00e84648 w3svc!HTTP_FILTER::NotifyPreProcHeaderFilters+0x3e (FPO: [Non-Fpo])
05 0129fca8 65f02ac1 006a8028 000001fb 0129fcd4 w3svc!HTTP_REQUEST::Parse+0x372 (FPO: [Non-Fpo])
06 0129fcec 65f029b5 006a8028 000001fb 0129fedc w3svc!HTTP_REQ_BASE::OnCompleteRequest+0x28 (FPO: [Non-Fpo])
07 0129fd2c 65f028f8 0129fea4 0129fedc 0129feb8 w3svc!HTTP_REQ_BASE::UnWrapRequest+0x1e6 (FPO: [Non-Fpo])
08 0129fd4c 65f027b3 0129fea4 0129fedc 0129feb8 w3svc!HTTP_REQ_BASE::OnFillClientReq+0x6c (FPO: [Non-Fpo])
09 0129fec4 65f020d7 0129fedc 011a3568 0129ff20 w3svc!HTTP_REQUEST::DoWork+0x8b (FPO: [Non-Fpo])
0a 0129fee4 65f01b47 00000000 00000000 00000000 w3svc!CLIENT_CONN::DoWork+0x1aa (FPO: [Non-Fpo])
0b 0129ff08 65f01aa5 0129ff20 77e873b2 011a3568 w3svc!CreateClient+0x7b (FPO: [Non-Fpo])
0c 0129ff4c 6d701443 011a3568 000001fb 00103bb0 w3svc!W3OnConnectEx+0x118 (FPO: [Non-Fpo])
0d 0129ff80 6d702203 00000000 000001fb 011a356c ISATQ!AtqpProcessContext+0x23a (FPO: [Non-Fpo])
0e 0129ffb4 77e887dd abcdef01 00000000 00012047 ISATQ!AtqPoolThread+0x1a8 (FPO: [EBP 0x0129ffec] [1,4,4])
0f 0129ffec 00000000 6d7091f7 abcdef01 00000000 KERNEL32!BaseThreadStart+0x52 (FPO: [Non-Fpo])
Let see what registry we are trying to open here:
The 2nd parameter of (ADVAPI32!RegOpenKeyExA ) is actually a pointer to a null teminated string that hold the registry key name.
Dumping that address shows the following:
0:006> da 016b1428
0129119c "SYSTEM\CurrentControlSet\Control"
012911bc "\ProductOptions"
So the handle to this key is actually what we are leaking. From the stack backtrace, you could see that (ADVAPI32!RegOpenKeyExA ) is called from (badfil!doHandleLeak1). This is enough to tell teh customer that the
component "badfil.dll" is the one causing the handle leak.
Here is how line 672 looks like in "badfil.cpp"
DWORD doHandleLeak1(int Variation)
{
if (1 == Variation)
{
HKEY hKey;
LONG lRet;
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0, KEY_QUERY_VALUE, &hKey ); //line 672
}
return 1;
}