LiXiong's Debugging paper

《Windows用户态程序高效排错》

handle leak ts steps in chinese


1. 安装Windbg到C:\Debuggers目录
2. 安装Application Verifier。对需要调试的程序激活“Handles - Detect invalid handle usage”
3. 确保编译的时候生成了目标程序的symbol文件,并且统一部署到自定义的symbol目录。
4. 启动目标程序
5. 启动性能监视器开始监视目标程序,添加
Process
Processor
Memory
System
6. 启动windbg,设定好symbol路径,开始监视目标程序
7. 在windbg命令窗口输入:
.logopen c:\log.txt
记录windbg输出到文本文件
8. 运行!handle和!htrace命令,等待命令执行完成
9. 输入下列命令避免1st chance exception干扰问题的重现
SXN *
SXN av
SXN clr
SXN eh
SXN cc
10. 输入g命令,让目标程序开始运行
11. 重现问题,问题发生后用windbg挂起程序
12. 再次输入!handle和!htrace,保存handle信息
13. 运行.dump命令抓取问题发生后的dump文件
14. 输入.logclose关闭log文件
15. 收集性能日志文件

通过分析C:\log.txt文件中问题发生前后的差异,找到发生泄漏的handle是在什么callstack中分配的,以及handle的类型。在结合源代码定位。

posted on 2006-11-06 14:16 lixiong 阅读(898) 评论(5)  编辑 收藏

Feedback

#1楼  2006-11-07 19:17 iceboundrock      

其实我一直想问的是,您是从哪儿知道这些调试的方法的?完全靠经验还是有什么需要遵循的规则或者思路,有没有什么思路就像算法里面的贪心、回溯之类的,呵呵,可能我问的比较白痴,希望您指点一下。谢谢。   回复  引用  查看    

#2楼  2006-11-10 16:30 Newer [未注册用户]

最近在弄一些设置系统隐藏参数的工作。也就是tweakui做的东西,我挑一些出来。
微软自己设置系统参数之后(比如隐藏“我的单脑”),不用重起系统就可以看到效果,而一般的软件是要求重新启动系统才能看到效果。不得已,我用windbg+idapro两个一起才跟踪出来tweakui调用了一个SHChangeNotify 完成桌面刷新工作。

在跟踪的过程中,我发现我不会下断点,比如说一个"apply"按钮按下之后,就是我要跟踪的过程,但是我没有办法对这个事件下断点。

我跟踪的过程是这样的:
首先用regmon(一个监视注册表的工具)监视tweakui修改了哪些键值,然后用bp advapi32!RegSetValueExW下断点。
拦截住之后,再看看程序所运行的地址,用idapro重新debug程序,在相应的地方下断点,然后跟踪出结果。

我知道这个方法是比较笨拙的。所以想请教一个比较快捷的方法。

*************
  回复  引用    

#3楼 [楼主] 2006-12-05 17:19 lixiong      

@iceboundrock
天天被客户逼,没办法就摸索出来了

@Newer
我对shell开发不熟悉,但是换成我,我也会这样设断点进行跟踪的   回复  引用  查看    

#4楼  2007-03-15 12:51 BAsil      

我碰到了这样一个问题

未将对象引用设置到对象的实例。

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:

行 769:            if (isEditable) {
            行 770:
            行 771:            	ProfileBase pb = ProfileBase.Create(member.UserName,true);
            行 772:                profile = new Profile(pb);
            行 773:            }

源文件: e:\code\mycs\components\provider\commondataprovider.cs    行: 771

堆栈跟踪:

[NullReferenceException: 未将对象引用设置到对象的实例。]
            Microsoft.ScalableHosting.Profile.ProfileBase.InitializeStatic() +1212
            Microsoft.ScalableHosting.Profile.ProfileBase.Create(String username, Boolean isAuthenticated) +50
            CommunityServer.Components.CommonDataProvider.PopulateUserFromIDataReader(IDataReader dr, MembershipUser member, Boolean isEditable) in e:\code\mycs\components\provider\commondataprovider.cs:771
            CommunityServer.Components.CommonDataProvider.cs_PopulateUserFromIDataReader(IDataReader dr, Boolean isEditable) in e:\code\mycs\components\provider\commondataprovider.cs:764
            CommunityServer.Data.SqlCommonDataProvider.GetUser(Int32 userID, String username, Boolean isOnline, Boolean isEditable, String lastAction) in e:\code\mycs\data providers\sqldataprovider\sqlcommondataprovider.cs:2720
            CommunityServer.Data.SqlCommonDataProvider.GetAnonymousUser(Int32 settingsID) in e:\code\mycs\data providers\sqldataprovider\sqlcommondataprovider.cs:1930
            CommunityServer.Users.GetAnonymousUser(Boolean fromCache) in e:\code\mycs\components\users.cs:630
            CommunityServer.Users.GetAnonymousUser() in e:\code\mycs\components\users.cs:617
            CommunityServer.Users.GetUser(Int32 userID, String username, Boolean isOnline, Boolean isCacheable, Boolean autoCreateUser) in e:\code\mycs\components\users.cs:76
            CommunityServer.Users.GetUser(Int32 userID, String username, Boolean isOnline, Boolean isCacheable) in e:\code\mycs\components\users.cs:65
            CommunityServer.Users.GetUser(Boolean isOnline) in e:\code\mycs\components\users.cs:52
            CommunityServer.Components.CSContext.get_User() in e:\code\mycs\components\components\cscontext.cs:351
            CommunityServer.CSHttpModule.Application_AuthorizeRequest(Object source, EventArgs e) in e:\code\mycs\components\httpmodule\cshttpmodule.cs:239
            System.Web.SyncEventExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute() +60
            System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +87
我在windbg想分析一下究竟是哪里出问题了,由于Microsoft.ScalableHosting.Profile.ProfileBase是在一个3rd的MemberRole.dll中。
搂住能给个分析步骤吗?我现在在windbg中只能做到捕捉到异常,然后通过!clrstack查看,以下是结果
0:001> !clrstack
 succeeded
Loaded Son of Strike data table version 5 from "C:\WINNT_1\Microsoft.NET\Framework\v1.1.4322\mscorwks.dll"
Thread 1
ESP       EIP    
00ebf698  77e8bc81 [FRAME: HelperMethodFrame]
00ebf6c4  08c54b0b [DEFAULT] Void Microsoft.ScalableHosting.Profile.ProfileBase.InitializeStatic()
00ebf720  08c544eb [DEFAULT] Class Microsoft.ScalableHosting.Profile.ProfileBase Microsoft.ScalableHosting.Profile.ProfileBase.Create(String,Boolean)
00ebf74c  08c4ae9c [DEFAULT] Class CommunityServer.Components.User CommunityServer.Components.CommonDataProvider.PopulateUserFromIDataReader(Class System.Data.IDataReader,Class Microsoft.ScalableHosting.Security.MembershipUser,Boolean)
  at [+0x5c] [+0xb]
00ebf7b4  08c4aab9 [DEFAULT] Class CommunityServer.Components.User CommunityServer.Components.CommonDataProvider.cs_PopulateUserFromIDataReader(Class System.Data.IDataReader,Boolean)
  at [+0x39] [+0x8]
00ebf7d8  08c4a48b [DEFAULT] [hasThis] Class CommunityServer.Components.User CommunityServer.Data.SqlCommonDataProvider.GetUser(I4,String,Boolean,Boolean,String)
  at [+0x1eb] [+0xc3]
00ebf82c  08c4a27c [DEFAULT] [hasThis] Class CommunityServer.Components.User CommunityServer.Data.SqlCommonDataProvider.GetAnonymousUser(I4)
  at [+0x1c4] [+0x95]
00ebf86c  08c4a058 [DEFAULT] Class CommunityServer.Components.User CommunityServer.Users.GetAnonymousUser(Boolean)
  at [+0xc0] [+0x47]
00ebf8a8  08c49f7b [DEFAULT] Class CommunityServer.Components.User CommunityServer.Users.GetAnonymousUser()
  at [+0x13] [+0x0]
00ebf8bc  08c49c61 [DEFAULT] Class CommunityServer.Components.User CommunityServer.Users.GetUser(I4,String,Boolean,Boolean,Boolean)
  at [+0x71] [+0x12]
00ebf964  08c49bcf [DEFAULT] Class CommunityServer.Components.User CommunityServer.Users.GetUser(I4,String,Boolean,Boolean)
  at [+0x27] [+0x0]
00ebf98c  08c49a21 [DEFAULT] Class CommunityServer.Components.User CommunityServer.Users.GetUser(Boolean)
  at [+0x29] [+0x6]
00ebf9ac  08c499c9 [DEFAULT] [hasThis] Class CommunityServer.Components.User CommunityServer.Components.CSContext.get_User()
  at [+0x21] [+0x8]
00ebf9cc  08c498d1 [DEFAULT] [hasThis] Void CommunityServer.CSHttpModule.Application_AuthorizeRequest(Object,Class System.EventArgs)
  at [+0x89] [+0x2c]
00ebfa38  086e633d [DEFAULT] [hasThis] Void System.Web.HttpApplication/SyncEventExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute()
00ebfa48  086e6148 [DEFAULT] [hasThis] Class System.Exception System.Web.HttpApplication.ExecuteStep(Class IExecutionStep,ByRef Boolean)
00ebfa90  086e5c3b [DEFAULT] [hasThis] Void System.Web.HttpApplication.ResumeSteps(Class System.Exception)
00ebfad8  086e5b13 [DEFAULT] [hasThis] Class System.IAsyncResult System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(Class System.Web.HttpContext,Class System.AsyncCallback,Object)
00ebfaf4  07ab6bcf [DEFAULT] [hasThis] Void System.Web.HttpRuntime.ProcessRequestInternal(Class System.Web.HttpWorkerRequest)
00ebfb30  07ab6660 [DEFAULT] Void System.Web.HttpRuntime.ProcessRequest(Class System.Web.HttpWorkerRequest)
00ebfb3c  07ab2f75 [DEFAULT] [hasThis] I4 System.Web.Hosting.ISAPIRuntime.ProcessRequest(I,I4)
00ebfbf4  79206069 [FRAME: ContextTransitionFrame]
00ebfcd0  79206069 [FRAME: ComMethodFrame]
0:001> !dumpstack
Current frame: KERNEL32!RaiseException+0x56
ChildEBP RetAddr  Caller,Callee
00ebf5b0 77e8bc81 KERNEL32!RaiseException+0x56, calling ntdll!RtlRaiseException
00ebf5b8 791b9d02 mscorwks!HndCreateHandle+0x50, calling mscorwks!HndAssignHandle
00ebf5cc 791be271 mscorwks!CreateHandle+0x12, calling mscorwks!HndCreateHandle
00ebf5e4 791be286 mscorwks!BaseDomain::CreateHandle+0x11, calling mscorwks!CreateHandle
00ebf5f8 7921020d mscorwks!RaiseTheException+0xa0, calling KERNEL32!RaiseException
00ebf638 791d50c1 mscorwks!HelperMethodFrame::HelperMethodFrame+0x1d, calling mscorwks!HelperMethodFrame::LazyInit
00ebf650 792ed555 mscorwks!JIT_Throw+0x4d, calling mscorwks!RaiseTheException
00ebf690 792ed51e mscorwks!JIT_Throw+0x16, calling mscorwks!LazyMachState::captureState
00ebf6bc 08c54b0b (MethodDesc 0x89ce0d0 +0x4b3 Microsoft.ScalableHosting.Profile.ProfileBase.InitializeStatic), calling mscorwks!JIT_Throw
00ebf718 08c544eb (MethodDesc 0x89ce060 +0x33 Microsoft.ScalableHosting.Profile.ProfileBase.Create), calling (MethodDesc 0x89ce0d0 Microsoft.ScalableHosting.Profile.ProfileBase.InitializeStatic)
00ebf744 08c4ae9c (MethodDesc 0x86f6a38 +0x5c CommunityServer.Components.CommonDataProvider.PopulateUserFromIDataReader), calling (MethodDesc 0x89ce060 Microsoft.ScalableHosting.Profile.ProfileBase.Create)
00ebf7a8 08c4aab9 (MethodDesc 0x86f6a28 +0x39 CommunityServer.Components.CommonDataProvider.cs_PopulateUserFromIDataReader), calling (MethodDesc 0x86f6a38 CommunityServer.Components.CommonDataProvider.PopulateUserFromIDataReader)
00ebf7d0 08c4a48b (MethodDesc 0x86f72b0 +0x1eb CommunityServer.Data.SqlCommonDataProvider.GetUser), calling (MethodDesc 0x86f6a28 CommunityServer.Components.CommonDataProvider.cs_PopulateUserFromIDataReader)
00ebf800 791fc8c4 mscorwks!GCHeap::SetFinalizationRun+0x14, calling mscorwks!OrMaskUP
00ebf814 08c4a27c (MethodDesc 0x86f7180 +0x1c4 CommunityServer.Data.SqlCommonDataProvider.GetAnonymousUser)
00ebf864 08c4a058 (MethodDesc 0x86f8148 +0xc0 CommunityServer.Users.GetAnonymousUser)
00ebf890 791ff15c mscorwks!EEHashTable<int,EEIntHashTableHelper,0>::FindItem+0xe, calling mscorwks!EEHashTable<int,EEIntHashTableHelper,0>::FindItem
00ebf8a0 08c49f7b (MethodDesc 0x86f8138 +0x13 CommunityServer.Users.GetAnonymousUser), calling (MethodDesc 0x86f8148 CommunityServer.Users.GetAnonymousUser)
00ebf8b4 08c49c61 (MethodDesc 0x86f8038 +0x71 CommunityServer.Users.GetUser), calling (MethodDesc 0x86f8138 CommunityServer.Users.GetAnonymousUser)
00ebf8cc 07c1357f 07c1357f
00ebf940 791b2f04 mscorwks!EE_LeaveCriticalSection+0xc, calling ntdll!RtlLeaveCriticalSection
00ebf950 08c49bcf (MethodDesc 0x86f8028 +0x27 CommunityServer.Users.GetUser), calling (MethodDesc 0x86f8038 CommunityServer.Users.GetUser)
00ebf97c 08c49a21 (MethodDesc 0x86f7ff8 +0x29 CommunityServer.Users.GetUser), calling (MethodDesc 0x86f8028 CommunityServer.Users.GetUser)
00ebf998 07498c77 (MethodDesc 0x761d280 +0x37 System.Threading.Thread.GetData), calling (MethodDesc 0x7664ee8 System.LocalDataStore.GetData)
00ebf9a4 08c499c9 (MethodDesc 0x7f8d728 +0x21 CommunityServer.Components.CSContext.get_User), calling (MethodDesc 0x86f7ff8 CommunityServer.Users.GetUser)
00ebf9c4 08c498d1 (MethodDesc 0x7f8b050 +0x89 CommunityServer.CSHttpModule.Application_AuthorizeRequest), calling (MethodDesc 0x7f8d728 CommunityServer.Components.CSContext.get_User)
00ebf9e0 07ab3fed (MethodDesc 0x7d22538 +0x75 System.Web.PerfCounters.IncrementCounter), calling aspnet_isapi!PerfIncrementCounter
00ebf9ec 0112a282 0112a282
00ebfa2c 086e633d (MethodDesc 0x7f97870 +0x3d System.Web.HttpApplication/SyncEventExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute), calling 0762792b (MethodDesc 0x7627930 System.EventHandler.Invoke)
00ebfa40 086e6148 (MethodDesc 0x7d90788 +0x58 System.Web.HttpApplication.ExecuteStep)
00ebfa64 791f12bc mscorwks!ThreadNative::FastGetCurrentThread+0x6, calling 00136158
00ebfa68 08305e81 (MethodDesc 0x7d90728 +0xb1 System.Web.HttpApplication.SetCulture), calling mscorwks!JIT_UP_WriteBarrierReg_Buf
00ebfa7c 086e5e44 (MethodDesc 0x7d90768 +0xa4 System.Web.HttpApplication.OnThreadEnter), calling (MethodDesc 0x7d90728 System.Web.HttpApplication.SetCulture)
00ebfa84 086e5c3b (MethodDesc 0x7d907b8 +0xf3 System.Web.HttpApplication.ResumeSteps), calling (MethodDesc 0x7d90788 System.Web.HttpApplication.ExecuteStep)
00ebfab0 0741afa2 (MethodDesc 0x7627b88 +0x12 System.Runtime.InteropServices.GCHandle..ctor), calling mscorwks!GCHandleInternalAlloc
00ebfabc 07423528 (MethodDesc 0x7627ba8 +0x10 System.Runtime.InteropServices.GCHandle.Alloc), calling (MethodDesc 0x7627b88 System.Runtime.InteropServices.GCHandle..ctor)
00ebfad0 086e5b13 (MethodDesc 0x7d90838 +0xe3 System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest), calling (MethodDesc 0x7d907b8 System.Web.HttpApplication.ResumeSteps)
00ebfae4 07ab6bcf (MethodDesc 0x1149800 +0x1e7 System.Web.HttpRuntime.ProcessRequestInternal)
00ebfb28 07ab6660 (MethodDesc 0x1149900 +0xb0 System.Web.HttpRuntime.ProcessRequest), calling (MethodDesc 0x1149800 System.Web.HttpRuntime.ProcessRequestInternal)
00ebfb34 07ab2f75 (MethodDesc 0x11481f8 +0x65 System.Web.Hosting.ISAPIRuntime.ProcessRequest), calling (MethodDesc 0x1149900 System.Web.HttpRuntime.ProcessRequest)
00ebfb3c 00196f0c 00196f0c, calling 0112a06c
00ebfb44 79206069 mscorwks!ComCallMLStubCache::CompileMLStub+0x3da
00ebfb60 006114ac fusion!CApplicationContext::Get+0x117, calling ntdll!RtlLeaveCriticalSection
00ebfb78 00611525 fusion!CApplicationContext::SxsActivateContext+0x41
00ebfba8 792fd884 mscorwks!ComCallMLStubCache::CompileMLStub+0x1af
00ebfbe8 792999a8 mscorwks!Thread::DoADCallBack+0x5c
00ebfc28 791bc474 mscorwks!SystemDomain::GetAppDomainAtId+0x12, calling mscorwks!ArrayList::GetPtr
00ebfc30 7926d380 mscorwks!ComCallMLStubCache::CompileMLStub+0x2c2, calling mscorwks!Thread::DoADCallBack
00ebfc88 79233661 mscorwks!SimpleComCallWrapper::GetDomainSynchronized+0x1b, calling mscorwks!SystemDomain::GetAppDomainAtId
00ebfc94 0112a0bd 0112a0bd, calling mscorwks!ComCallMLStubCache::CompileMLStub+0x255
00ebfcd4 00196f0c 00196f0c, calling 0112a06c
00ebfcd8 00441f8c aspnet_wp!CAsyncPipeManager::ProcessMessage+0x225
00ebfd2c 77fcb4be ntdll!RtlAllocateHeap+0x7d4, calling ntdll!RtlLeaveCriticalSection
00ebfd30 77fcb618 ntdll!RtlAllocateHeap+0x92e, calling ntdll!RtlAllocateHeap+0x930
00ebfd58 77fb2c0c ntdll!RtlpAllocateFromHeapLookaside+0x40, calling ntdll!RtlpInterlockedPopEntrySList
00ebfdb4 77f8c167 ntdll!LdrpSnapThunk+0xaf, calling ntdll!LdrpNameToOrdinal
00ebfdf0 77fabdb6 ntdll!RtlImageDirectoryEntryToData+0x38, calling ntdll!RtlpImageDirectoryEntryToData32
00ebff14 00441a79 aspnet_wp!CAsyncPipeManager::StartRead+0xb7, calling aspnet_wp!TimeClass::SnapCurrentTime
00ebff28 00441c86 aspnet_wp!CAsyncPipeManager::ProcessCompletion+0x1e7, calling aspnet_wp!CAsyncPipeManager::ProcessMessage
00ebff44 79e8cb3d aspnet_isapi!CorThreadPoolCompletionCallback+0x3c
00ebff68 77e8bc15 KERNEL32!ResumeThread+0x10, calling ntdll!ZwResumeThread
00ebff88 792e9c69 mscorwks!ThreadpoolMgr::CompletionPortThreadStart+0x183
00ebffb4 77e6b396 KERNEL32!BaseThreadStart+0x52
请楼主帮帮看看,谢谢。
  回复  引用  查看    

#5楼  2007-03-15 12:57 BAsil      

真是不好意思啊,把你的页面给撑大了。
我刚接触windbg,写了一篇文章,想请楼主过目,指点一下。
思归的“动态控件的状态问题”的分析中用WinDbg跟踪TrackViewState方法的步骤
  回复  引用  查看    


标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      


相关链接: