VS2015 让Release程序生成pdb文件可以加dump调试的Release版本

只需设置三个部分:

项目->属性->C/C++->General->Debug Information Format->Program Database for Edit & Continue (/ZI)

                 VS2015 让Release程序生成pdb文件可以加dump调试的Release版本_第1张图片


项目->属性->C/C++->Optimization->Optimization->Disabled(/Od)

                VS2015 让Release程序生成pdb文件可以加dump调试的Release版本_第2张图片


项目->属性->Linker->Debugging->Generate Debug Info->Yes(/DEBUG)

                VS2015 让Release程序生成pdb文件可以加dump调试的Release版本_第3张图片

 

当出现设置了上面三个选项后 依然不能挂住断点,那请在试下下面的设置:

有时Release下会出现源文件与PDB文件不匹配的问题:

菜单【Tools】->【Options】->【Debugging】->【General】去掉【Require source files to exactly match original version】选项

            VS2015 让Release程序生成pdb文件可以加dump调试的Release版本_第4张图片

测试demo:

#include "stdafx.h"


#include
#include
#include
#include


#pragma comment(lib, "Dbghelp.lib")


#include


//生产DUMP文件
int GenerateMiniDump(HANDLE hFile, PEXCEPTION_POINTERS pExceptionPointers, PWCHAR pwAppName)
{
    BOOL bOwnDumpFile = FALSE;
    HANDLE hDumpFile = hFile;
    MINIDUMP_EXCEPTION_INFORMATION ExpParam;


    typedef BOOL(WINAPI * MiniDumpWriteDumpT)(
        HANDLE,
        DWORD,
        HANDLE,
        MINIDUMP_TYPE,
        PMINIDUMP_EXCEPTION_INFORMATION,
        PMINIDUMP_USER_STREAM_INFORMATION,
        PMINIDUMP_CALLBACK_INFORMATION
        );


    MiniDumpWriteDumpT pfnMiniDumpWriteDump = NULL;
    HMODULE hDbgHelp = LoadLibrary(L"DbgHelp.dll");
    if (hDbgHelp)
        pfnMiniDumpWriteDump = (MiniDumpWriteDumpT)GetProcAddress(hDbgHelp, "MiniDumpWriteDump");


    if (pfnMiniDumpWriteDump)
    {
        if (hDumpFile == NULL || hDumpFile == INVALID_HANDLE_VALUE)
        {
            TCHAR szFileName[MAX_PATH] = { 0 };
            TCHAR* szVersion = L"v1.0";
            TCHAR dwBufferSize = MAX_PATH;
            SYSTEMTIME stLocalTime;
            GetLocalTime(&stLocalTime);
            CreateDirectory(szFileName, NULL);


            wsprintf(szFileName, L"%s-%04d%02d%02d-%02d%02d%02d-%ld-%ld.dmp",
                szVersion,
                stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
                stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond,
                GetCurrentProcessId(), GetCurrentThreadId());
            // szFileName是全路径文件名,这样可以指定dump文件的保存位置
            hDumpFile = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_WRITE | FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);


            bOwnDumpFile = TRUE;
            OutputDebugString(szFileName);
        }


        if (hDumpFile != INVALID_HANDLE_VALUE)
        {
            ExpParam.ThreadId = GetCurrentThreadId();
            ExpParam.ExceptionPointers = pExceptionPointers;
            ExpParam.ClientPointers = FALSE;


            pfnMiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
                hDumpFile, MiniDumpWithDataSegs, (pExceptionPointers ? &ExpParam : NULL), NULL, NULL);


            if (bOwnDumpFile)
                CloseHandle(hDumpFile);
        }
    }


    if (hDbgHelp != NULL)
        FreeLibrary(hDbgHelp);


    return EXCEPTION_EXECUTE_HANDLER;
}




LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS lpExceptionInfo)
{
    if (IsDebuggerPresent())
    {
        return EXCEPTION_CONTINUE_SEARCH;
    }


    return GenerateMiniDump(NULL, lpExceptionInfo, L"test");
}
int main()
{
    ::SetUnhandledExceptionFilter(ExceptionFilter);


    int a = 0;
    int b = 2;
    int c = b / a;


    return 0;
}

你可能感兴趣的:(c/c++)