2014-07-17 72 views
0

我想使用IDebugXXX interfaces获取我的local process(没有远程附加)中的某些功能的堆栈跟踪。Stacktrace/Stackwalk为当前进程和当前线程与IDebug接口

使用此代码附加到当前进程的作品,但当前线程的堆栈跟踪送花儿给人包含only one frame,如: ntdll!ZwGetContextThread+0x00000012

{ 
    IDebugClient* debugClient; 
    IDebugControl4 *control4; 

    ... 

    int flags = DEBUG_ATTACH_NONINVASIVE | DEBUG_ATTACH_NONINVASIVE_NO_SUSPEND; 
    debugClient->AttachProcess(0, myProcessId, flags); 

    control4->SetExecutionStatus(DEBUG_STATUS_GO); 


    ... 

    // get the stack trace for the current thread 
    control4->GetStackTrace(0, 0, 0, _stackFrames, ARRAYSIZE((_stackFrames)), &_uFramesFilled) 

    // _uFramesFilled is always '1' for the current thread 
} 

编辑: 该应用程序是用C++/CLI而其他线程的结果至少包含更多的帧。

+0

托管调试器无法获取非托管代码的堆栈跟踪。 –

+0

这是一个C++/CLI应用程序 – boboes

+0

然后,使这个复杂没有意义,只需使用StackTrace类。 –

回答

1

WinDDK阅读assert样品后我发现有上下文丢失堆栈跟踪必须从启动。之后(并添加WaitForEvent(...))跟踪工作正常。

{ 
    ... 

    // capture the context end convert it to debug '.crx' command 
    char CxrCommand[64]; 
    CONTEXT myContext; 
    ZeroMemory(&myContext, sizeof(CONTEXT)); 
    RtlCaptureContext(&myContext); 
    sprintf_s(CxrCommand, 64, ".cxr 0x%p", &myContext); 
    // capture the context end 

    ... 

    control4->WaitForEvent(DEBUG_WAIT_DEFAULT, INFINITE); 

    //execute debugger command: ".cxr (Display Context Record)" 
    control4->Execute(DEBUG_OUTCTL_IGNORE, CxrCommand, DEBUG_EXECUTE_NOT_LOGGED 

    control4->GetStackTrace(....) 
} 
1

这很适合我:(注意:请检查这些API的返回码)

我认为你缺少的是:“AttachProcess”后“WaitForEvent”电话。

IDebugClient4 * debugClient; 
IDebugControl4 * control4; 
DEBUG_STACK_FRAME frames[10]; 
ULONG filled = 0; 
ULONG pid = 7288; 

DebugCreate(__uuidof(IDebugClient4), (void **)&debugClient); 

debugClient->QueryInterface(__uuidof(IDebugControl4), (void**)&control4); 

debugClient->AttachProcess(0, pid, DEBUG_ATTACH_NONINVASIVE | DEBUG_ATTACH_NONINVASIVE_NO_SUSPEND); 

control4->WaitForEvent(DEBUG_WAIT_DEFAULT, INFINITE); 

control4->GetStackTrace(0, 0, 0, &frames[0], 10, &filled); 
+0

谢谢,我可以在星期一进行测试,并让它知道它是如何工作的 – boboes

+0

相同的行为,只有一个帧被填充 – boboes

+1

终于明白了,上下文也丢失了 – boboes