【问题标题】:Why does DebugActiveProcessStop crash my debugging app?为什么 DebugActiveProcessStop 会使我的调试应用程序崩溃?
【发布时间】:2012-03-22 23:03:47
【问题描述】:

我有一个我编写的调试程序,用于附加到一个进程并创建一个故障转储文件。这部分工作正常。

我遇到的问题是,当调试器程序终止时,它正在调试的程序也会终止。

我做了一些谷歌搜索并找到了 DebugActiveProcessStop() API 调用。这没有出现在我的旧 MSDN 文档中,因为它只在 Windows XP 中引入,所以我尝试在运行时从 Kernel32.dll 动态加载它。

现在我的问题是,一旦调用 _DebugActiveProcessStop(),我的调试器程序就会崩溃。有人可以告诉我我做错了什么吗?

typedef BOOL (*DEBUGACTIVEPROCESSSTOP)(DWORD);

DEBUGACTIVEPROCESSSTOP _DebugActiveProcessStop;

HMODULE hK32 = LoadLibrary( "kernel32.dll" );

if( hK32 )
  _DebugActiveProcessStop = (DEBUGACTIVEPROCESSSTOP) GetProcAddress( hK32,"DebugActiveProcessStop" );
else
{
  printf( "Can't load Kernel32.dll\n" );
  return;
}

if( ! _DebugActiveProcessStop )
{
  printf( "Can't find DebugActiveProcessStop\n" );
  return;
}

...

void DebugLoop( void )
{
  DEBUG_EVENT de;

  while( 1 )
  {
    WaitForDebugEvent( &de, INFINITE ); 

    switch( de.dwDebugEventCode )
    {
      case CREATE_PROCESS_DEBUG_EVENT:
        hProcess = de.u.CreateProcessInfo.hProcess;
        break;

      case EXCEPTION_DEBUG_EVENT: 

        // PDS: I want a crash dump immediately!
        dwProcessId = de.dwProcessId;
        dwThreadId  = de.dwThreadId;

        WriteCrashDump( &de.u.Exception );
        return;

      case CREATE_THREAD_DEBUG_EVENT:
      case OUTPUT_DEBUG_STRING_EVENT:
      case EXIT_THREAD_DEBUG_EVENT:
      case EXIT_PROCESS_DEBUG_EVENT :
      case LOAD_DLL_DEBUG_EVENT:
      case UNLOAD_DLL_DEBUG_EVENT:
      case RIP_EVENT:
      default:
        break;
    }

    ContinueDebugEvent( de.dwProcessId, de.dwThreadId, DBG_CONTINUE );
  }
}

...
void main( void )
{
...
  BOOL bo = DebugActiveProcess( dwProcessId );

  if( bo == 0 )
    printf( "DebugActiveProcess failed, GetLastError: %u \n",GetLastError() );

  hProcess = OpenProcess( PROCESS_ALL_ACCESS, TRUE, dwProcessId );

  if( hProcess == NULL )
    printf( "OpenProcess failed, GetLastError: %u \n",GetLastError() );

  DebugLoop();

  _DebugActiveProcessStop( dwProcessId );

  CloseHandle( hProcess );
}

【问题讨论】:

    标签: debugging


    【解决方案1】:

    它崩溃的原因是我错过了函数指针定义中的 WINAPI 关键字。

    这行得通:

    typedef BOOL (WINAPI *DEBUGSETPROCESSKILLONEXIT)(BOOL);

    【讨论】:

      猜你喜欢
      • 2012-01-24
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多