WCT GetThreadWaitChain call allways return false

2019-01-20 17:52发布

I am trying to get WCT information about some thread, but every time I am calling GetThreadWaitChain function I am getting false as a response and the ref parameters remain zero.

What am I doing wrong?

I am calling this function on a thread which calls WaitForMultipleObjects, and I am making sure that this thread waits while I am debugging.

That's my code:

    internal void CollectWaitInformation(ClrThread thread)
    {
        var g_WctHandle = OpenThreadWaitChainSession(0, 0);

        uint threadID = thread.OSThreadId;

        WAITCHAIN_NODE_INFO[] NodeInfoArray = new WAITCHAIN_NODE_INFO[WCT_MAX_NODE_COUNT];


        int isCycle = 0;
        int count = 0;

        // Make a synchronous WCT call to retrieve the wait chain.
        bool result = GetThreadWaitChain(g_WctHandle,
                                IntPtr.Zero,
                                WCTP_GETINFO_ALL_FLAGS,
                                threadID, ref count, NodeInfoArray, out isCycle);

        if (!result)
        {
            //error
        }

        //Finaly ...
        CloseSession(g_WctHandle);
    }

    [DllImport("Advapi32.dll")]
    public static extern IntPtr OpenThreadWaitChainSession(OpenThreadChainFlags Flags, DWORD callback);

    [DllImport("Advapi32.dll")]
    public static extern bool GetThreadWaitChain(
        IntPtr WctHandle,
        IntPtr Context,
        UInt32 Flags,
        uint ThreadId,
        ref int NodeCount,
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 4)]
        [In, Out]
        WAITCHAIN_NODE_INFO[] NodeInfoArray,
        out int IsCycle
    );

    [StructLayout(LayoutKind.Sequential)]
    public struct WAITCHAIN_NODE_INFO
    {
        public WCT_OBJECT_TYPE ObjectType;
        public WCT_OBJECT_STATUS ObjectStatus;

        public struct LockObject
        {
            string ObjectName;
            LARGE_INTEGER Timeout;
            BOOL Alertable;
        }
        public struct ThreadObject
        {
            DWORD ProcessId;
            DWORD ThreadId;
            DWORD WaitTime;
            DWORD ContextSwitches;
        }
    }
}

}

With a help from my previous question:

Calling C++ method from C# with pointer parameter (WCT)

1条回答
叼着烟拽天下
2楼-- · 2019-01-20 17:56

From the documentation:

NodeCount [in, out]

On input, a number from 1 to WCT_MAX_NODE_COUNT that specifies the number of nodes in the wait chain. On return, the number of nodes retrieved.

....

You fail to meet this requirement because you pass in 0.

查看更多
登录 后发表回答