WCTwait chain traveral.docx

上传人:b****5 文档编号:3195370 上传时间:2022-11-20 格式:DOCX 页数:34 大小:27.01KB
下载 相关 举报
WCTwait chain traveral.docx_第1页
第1页 / 共34页
WCTwait chain traveral.docx_第2页
第2页 / 共34页
WCTwait chain traveral.docx_第3页
第3页 / 共34页
WCTwait chain traveral.docx_第4页
第4页 / 共34页
WCTwait chain traveral.docx_第5页
第5页 / 共34页
点击查看更多>>
下载资源
资源描述

WCTwait chain traveral.docx

《WCTwait chain traveral.docx》由会员分享,可在线阅读,更多相关《WCTwait chain traveral.docx(34页珍藏版)》请在冰豆网上搜索。

WCTwait chain traveral.docx

WCTwaitchaintraveral

WaitChainTraversal(WCT)enablesdebuggerstodiagnoseapplicationhangsanddeadlocks.Awaitchainisanalternatingsequenceofthreadsandsynchronizationobjects;eachthreadwaitsfortheobjectthatfollowsit,whichisownedbythesubsequentthreadinthechain.

Athreadwaitsforasynchronizationobjectfromthetimeitrequestsituntilithasacquiredit.Alockisownedbyathreadfromthetimethethreadacquiresit,untilitreleasesit.Lockownershipisequivalenttothelockwaitingforthethreadtoreleaseit.Therefore,ifthread1waitsforalockthatisownedbythread2,thisisthesameassayingthatthread1waitsforthread2.WCTcurrentlysupportsthefollowingsynchronizationprimitives:

∙ALPC

∙COM

∙Criticalsections

∙Mutexes

∙SendMessage

∙Waitoperationsonprocessesandthreads

Toretrievethewaitchainforoneormorethreads,createaWCTsessionusingtheOpenThreadWaitChainSessionandGetThreadWaitChainfunctions.WCTsessionsarerepresentedbyahandleoftypeHWCT.Sessionscanbeeithersynchronousorasynchronousinnature.Synchronoussessionswillblockthecallingthreaduntilawaitchainhasbeenretrieved.Synchronoussessionscannotbecanceled.AsynchronoussessionsdonotblockthecallingthreadandcanbecanceledbytheapplicationusingtheCloseThreadWaitChainSessionfunction.ResultsfromasynchronousoperationsaremadeavailablethroughaWaitChainCallbackcallbackfunctionprovidedbytheapplication.

Forasynchronoussessions,thecallercanspecifyapointertoacontextdatastructurethroughGetThreadWaitChain.Thesamepointerispassedtothecallbackfunction.Thiscontextdatastructureisuser-definedandopaquetoWCT.ItcanbeusedbytheapplicationtocommunicatecontextbetweenaWCTqueryandacallbackfunction.Acommonapproachistopassaneventhandlethroughthisstructure;whenthecallbackisexecuted,itsignalsthiseventandinformssomemonitoringthreadthatthequeryhasbeencompleted.

Relatedtopics

UsingWCT

WCTReference

 

ThefollowingsamplecodedemonstratestheuseofthewaitchaintraversalAPI.Itenumeratesallthreadsinthesystemandprintsthewaitchainforeachthread.

Toenumerateallthreadsinthesystem,runthesamplewithnoparameters.Toenumerateonlythethreadsfromaspecifiedprocess,runthesampleandpasstheprocessidentifierasaparameter.Thesampleperformsthefollowingsteps:

1.CallstheRegisterWaitChainCOMCallbackfunctiontoregistertheCOMcallbackfunctions.

2.CallstheOpenThreadWaitChainSessionfunctiontocreatethewaitchainsession.

3.CallstheAdjustTokenPrivilegesfunctiontoenabletheSE_DEBUG_NAMEprivilege.

4.CallstheEnumProcessesandCreateToolhelp32Snapshotfunctionstoenumeratethespecifiedthreads.

5.CallstheGetThreadWaitChaintoretrieveanarrayofWAITCHAIN_NODE_INFOstructuresthatcontainthenodesofthewaitchain.

6.Printsinformationfromthewaitchain.

7.CallstheCloseThreadWaitChainSessionfunctiontocleanupthewaitchain.

Copy

//Copyright(C)Microsoft.Allrightsreserved.

/*

*SamplecodefortheWaitChainTraversal(WCT)API.

*

*Thisprogramenumeratesallthreadsinthesystemandprintsthe

*waitchainforeachofthem.Itshouldberunfromanelevated

*commandprompttogetresultsforservices.

*

*/

#ifndefUNICODE

#defineUNICODE

#endif

#include

#include

#include

#include

#include

#include

#include

#pragmacomment(lib,"Psapi.lib")

#pragmacomment(lib,"Advapi32.lib")

typedefstruct_STR_ARRAY

{

CHARDesc[32];

}STR_ARRAY;

//Human-readablenamesforthedifferentsynchronizationtypes.

STR_ARRAYSTR_OBJECT_TYPE[]=

{

{"CriticalSection"},

{"SendMessage"},

{"Mutex"},

{"Alpc"},

{"Com"},

{"ThreadWait"},

{"ProcWait"},

{"Thread"},

{"ComActivation"},

{"Unknown"},

{"Max"}

};

//GlobalvariabletostoretheWCTsessionhandle

HWCTg_WctHandle=NULL;

//GlobalvariabletostoreOLE32.DLLmodulehandle.

HMODULEg_Ole32Hnd=NULL;

//

//Functionprototypes

//

void

PrintWaitChain(

__inDWORDThreadId

);

 

BOOL

GrantDebugPrivilege()

/*++

RoutineDescription:

Enablesthedebugprivilege(SE_DEBUG_NAME)forthisprocess.

Thisisnecessaryifwewanttoretrievewaitchainsforprocesses

notownedbythecurrentuser.

Arguments:

None.

ReturnValue:

TRUEifthisprivilegecouldbeenabled;FALSEotherwise.

--*/

{

BOOLfSuccess=FALSE;

HANDLETokenHandle=NULL;

TOKEN_PRIVILEGESTokenPrivileges;

if(!

OpenProcessToken(GetCurrentProcess(),

TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,

&TokenHandle))

{

printf("Couldnotgettheprocesstoken");

gotoCleanup;

}

TokenPrivileges.PrivilegeCount=1;

if(!

LookupPrivilegeValue(NULL,

SE_DEBUG_NAME,

&TokenPrivileges.Privileges[0].Luid))

{

p

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 唐诗宋词

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1