WinDBG的 !handle 命令可以让你方便调试句柄(handle)。
查看进程内所有句柄, 输入命令行
0:014> !handle
Handle 4
Type Directory
Handle 8
Type Process
Handle c
Type Key
Handle 10
Type Mutant
Handle 14
Type ALPC Port
Handle 18
Type Key
Handle 1c
Type Event
Handle 20
Type Key
Handle 4
Type Directory
Handle 8
Type Process
Handle c
Type Key
Handle 10
Type Mutant
Handle 14
Type ALPC Port
Handle 18
Type Key
Handle 1c
Type Event
Handle 20
Type Key
... (省略)
Handle 7e0
Type Mutant
392 Handles
Type Count
None 17
Event 134
Section 47
File 35
Directory 3
Mutant 34
WindowStation 2
Semaphore 24
Key 47
Token 1
Process 3
Thread 27
Desktop 1
IoCompletion 4
Timer 5
Job 1
KeyedEvent 1
TpWorkerFactory 6
Type Mutant
392 Handles
Type Count
None 17
Event 134
Section 47
File 35
Directory 3
Mutant 34
WindowStation 2
Semaphore 24
Key 47
Token 1
Process 3
Thread 27
Desktop 1
IoCompletion 4
Timer 5
Job 1
KeyedEvent 1
TpWorkerFactory 6
从WinDBG输出结果我们可以看到所有句柄的类型和值,以及统计信息(一共392个handle,其中有35个文件句柄,47个注册表句柄 .... )。
如果想查看某个句柄的详细信息, 可以用命令:
0:014> !handle 0x5c8 f
Handle 5c8
Type Key
Attributes 0
GrantedAccess 0x20019:
ReadControl
QueryValue,EnumSubKey,Notify
HandleCount 2
PointerCount 3
Name \REGISTRY\USER\S-1-5-21-2127521184-1604012920-1887927527-2966534\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts
Object Specific Information
Key last write time: 16:52:14. 5/20/2009
Key name FileExts
Handle 5c8
Type Key
Attributes 0
GrantedAccess 0x20019:
ReadControl
QueryValue,EnumSubKey,Notify
HandleCount 2
PointerCount 3
Name \REGISTRY\USER\S-1-5-21-2127521184-1604012920-1887927527-2966534\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts
Object Specific Information
Key last write time: 16:52:14. 5/20/2009
Key name FileExts
0x5c8 是句柄的值, 参数f 表示显示全部信息。 我们可以看到句柄0x5c8 是注册表句柄,路径为\REGISTRY\USER\S-1-5-21-2127521184-1604012920-1887927527-2966534\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts。
值得一提的是,WinDBG 还提供了!htrace 命令,可以很方便来用检查句柄泄露(Handle Leak), 下次再写篇blog 专门介绍 !htrace 。
本文转自 陈本峰 51CTO博客,原文链接:http://blog.51cto.com/wingeek/274066,如需转载请自行联系原作者