2012-03-23 278 views
2

我已經創建了我自己的調試器應用程序。它附加到一個進程並創建一個崩潰轉儲文件。這在大多數情況下都是有效的。我遇到的問題是,當正在調試的應用程序正在等待互斥對象時(這是我想要調試的問題),它將無法正常工作。爲什麼MiniDumpWriteDump失敗?

此外,我創建了一個簡單的test.exe應用程序,它只是循環並調用Sleep(100),但我的調試器在每次調用此應用程序的MiniDumpWriteDump時失敗。

我在做什麼錯?

錯誤代碼我從下面的代碼返回爲2147942699(0x8007012b)

void WriteCrashDump(EXCEPTION_DEBUG_INFO *pExceptionInfo) 
{ 
    CONTEXT c; 

    memset(&c, 0, sizeof(c)); 

    GetThreadContext(hThread, &c); 

    EXCEPTION_POINTERS ep; 

    memset(&ep, 0, sizeof(ep)); 

    ep.ContextRecord = &c; 
    ep.ExceptionRecord = &pExceptionInfo->ExceptionRecord; 

    MINIDUMP_EXCEPTION_INFORMATION minidump_exception; 

    memset(&minidump_exception, 0, sizeof(minidump_exception)); 

    minidump_exception .ThreadId   = dwThreadId; 
    minidump_exception.ExceptionPointers = &ep; 
    minidump_exception.ClientPointers = true; 

    char txDumpPath[ MAX_PATH + 1 ]; 

    sprintf(txDumpPath, "%s.dmp", txProcess); 

    HANDLE hFile = CreateFile(txDumpPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 

    if(hFile) 
    { 
    BOOL fSuccess; 


    SetLastError(0L); 

    int nDumpOptions = 

    MiniDumpNormal 
| MiniDumpWithDataSegs     
| MiniDumpWithFullMemory     
| MiniDumpWithHandleData     
| MiniDumpFilterMemory     
| MiniDumpScanMemory      
| MiniDumpWithUnloadedModules   
| MiniDumpWithIndirectlyReferencedMemory 
| MiniDumpFilterModulePaths    
| MiniDumpWithProcessThreadData   
| MiniDumpWithPrivateReadWriteMemory  
| MiniDumpWithoutOptionalData   
    ; 

    fSuccess = MiniDumpWriteDump(hProcess, 
            dwProcessId, 
            hFile, 
            (MINIDUMP_TYPE) nDumpOptions, 
            &minidump_exception, 
            NULL, 
            NULL); 

    DWORD dwErr = GetLastError(); 

    if(! fSuccess) 
      printf("MiniDumpWriteDump -FAILED (LastError:%u)\n", dwErr); 

     CloseHandle(hFile); 
    } 
} 

我也曾嘗試用下面的代碼片段是我從別人借了誰似乎也有類似的增加特權問題:

BOOL SetDumpPrivileges() 
{ 
    BOOL  fSuccess = FALSE; 
    HANDLE  TokenHandle = NULL; 
    TOKEN_PRIVILEGES TokenPrivileges; 

    if (!OpenProcessToken(GetCurrentProcess(), 
     TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, 
     &TokenHandle)) 
    { 
     printf("Could not get the process token"); 
     goto Cleanup; 
    } 

    TokenPrivileges.PrivilegeCount = 1; 

    if (!LookupPrivilegeValue(NULL, 
     SE_DEBUG_NAME, 
     &TokenPrivileges.Privileges[0].Luid)) 
    { 
     printf("Couldn't lookup SeDebugPrivilege name"); 
     goto Cleanup; 
    } 

    TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 

    //Add privileges here. 
    if (!AdjustTokenPrivileges(TokenHandle, 
     FALSE, 
     &TokenPrivileges, 
     sizeof(TokenPrivileges), 
     NULL, 
     NULL)) 
    { 
     printf("Could not revoke the debug privilege"); 
     goto Cleanup; 
    } 

    fSuccess = TRUE; 

Cleanup: 

    if (TokenHandle) 
    { 
     CloseHandle(TokenHandle); 
    } 

    return fSuccess; 
} 

回答

2

我貼關於MSDN的問題和有人向我提供了我的問題的答案。以下是討論的link以及我在下面複製的工作代碼片段。

void WriteCrashDump(EXCEPTION_DEBUG_INFO *pExceptionInfo) 
{ 
    CONTEXT c; 

    memset(&c, 0, sizeof(c)); 

    HANDLE hThread; 
    c.ContextFlags = CONTEXT_FULL; 
    hThread = _OpenThread(THREAD_ALL_ACCESS, FALSE, dwThreadId); 

    GetThreadContext(hThread, &c); 

    EXCEPTION_POINTERS ep; 

    memset(&ep, 0, sizeof(ep)); 

    ep.ContextRecord = &c; 
    ep.ExceptionRecord = &pExceptionInfo->ExceptionRecord; 

    MINIDUMP_EXCEPTION_INFORMATION minidump_exception; 

    memset(&minidump_exception, 0, sizeof(minidump_exception)); 

    minidump_exception.ThreadId   = dwThreadId; 
    minidump_exception.ExceptionPointers = &ep; 
    minidump_exception.ExceptionPointers->ContextRecord = &c; 
    minidump_exception.ClientPointers = false; 

    char txDumpPath[ MAX_PATH + 1 ]; 

    time_t tNow = time(NULL); 
    struct tm *pTm = localtime(&tNow); 

    sprintf(txDumpPath, "%s.%02d%02d%04d_%02d%02d%02d.dmp", 
      txProcess, 
      pTm->tm_mday, 
      pTm->tm_mon, 
      pTm->tm_year, 
      pTm->tm_hour, 
      pTm->tm_min, 
      pTm->tm_sec); 

    HANDLE hFile = CreateFile(txDumpPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 

    if(hFile != INVALID_HANDLE_VALUE) 
    { 
    BOOL fSuccess; 

    printf("hProcess : %d (0x%x)\n", hProcess, hProcess); 
    printf("dwProcessId: %u (0x%lx)\n", dwProcessId, dwProcessId); 
    printf("dwThreadId : %u (0x%lx)\n", dwThreadId, dwThreadId); 

    SetLastError(0L); 

    fSuccess = MiniDumpWriteDump(hProcess, 
            dwProcessId, 
            hFile, 
            MiniDumpNormal, 
            &minidump_exception, 
            NULL, 
            NULL); 

    DWORD dwErr = GetLastError(); 

    if(! fSuccess) 
    { 
     printf("MiniDumpWriteDump -FAILED (LastError:%u)\n", dwErr); 

     LPVOID lpMsgBuf; 

     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 
        NULL, 
        dwErr, 
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 
        (LPTSTR) &lpMsgBuf, 
        0, 
        NULL); 

     // Display the string. 
     printf("%s\n", (LPCTSTR)lpMsgBuf); 

     // Free the buffer. 
     LocalFree(lpMsgBuf); 
    } 
    } 

    if(hThread) 
    CloseHandle(hThread); 
} 
+0

嗨,上面的東西不適合我。事情是我有我的MiniDump寫從一個單獨的過程。你是一個進程崩潰處理程序嗎?或正在進行中?你能澄清一下嗎? – 2013-06-11 16:06:52

0

這個問題和其他問題一樣嗎question我回答了這段文字?

這豈不是包括即使有標誌MiniDumpWithHandleData互斥的信息,它也可能是失敗的,因爲一些標誌可能與DebugHlp.dll您呼叫的對看的版本不兼容:here

+0

你好。不,它是一個不同的問題。我之前的問題涉及處理信息的寫作。我在這裏描述的問題是MiniDumpWriteDump無法爲大多數處理寫入任何內容。我有一個小小的測試應用程序,它拒絕創建任何類型的轉儲(創建大小爲0字節的文件)。如果我將調試器應用程序附加到我們的其中一個更大的應用程序,它會寫入轉儲。 – SparkyNZ 2012-03-25 04:23:57