Use visual studio to generate dump file analysis program crash

pdb file

PDB file is the abbreviation of "Program Database" Program DataBase, and the code information of the program contained in
this file can be used to view the location of the program breakpoint and the function running stack and other information.

dump file

The full name of a dump file is a storage file for additional stack information. The file extension is .dmp.
You can get the stack information at a certain moment when the program is running through the dump file.
It can be used to analyze the dmp file at the moment to troubleshoot the cause of the crash when the program crashes

The release configuration in VS generates a pdb file

  1. Right-click the project properties.
  2. Connector-----Debug-----choose to generate debugging information
    Insert picture description here

Dump to generate dmp file (VS2015 32-bit)

#include <windows.h>
#include <Dbghelp.h>
#include <iostream>  
#include <vector>  
#include <tchar.h>
using namespace std;


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


namespace NSDumpFile
{
    
    
	void CreateDumpFile(LPCWSTR lpstrDumpFilePathName, EXCEPTION_POINTERS *pException)
	{
    
    
		// 创建Dump文件  
		HANDLE hDumpFile = CreateFile(((LPCWSTR)lpstrDumpFilePathName), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);


		// Dump信息  
		MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
		dumpInfo.ExceptionPointers = pException;
		dumpInfo.ThreadId = GetCurrentThreadId();
		dumpInfo.ClientPointers = TRUE;


		// 写入Dump文件内容  
		MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);


		CloseHandle(hDumpFile);
	}


	LPTOP_LEVEL_EXCEPTION_FILTER WINAPI MyDummySetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter)
	{
    
    
		return NULL;
	}


	BOOL PreventSetUnhandledExceptionFilter()
	{
    
    
		//Windows 本身的32位的动态链接库,确保自己有这个库
		HMODULE hKernel32 = LoadLibrary(_T("kernel32.dll"));
		if (hKernel32 == NULL)
			return FALSE;


		void *pOrgEntry = GetProcAddress(hKernel32, "SetUnhandledExceptionFilter");
		if (pOrgEntry == NULL)
			return FALSE;


		unsigned char newJump[100];
		DWORD dwOrgEntryAddr = (DWORD)pOrgEntry;
		dwOrgEntryAddr += 5; // add 5 for 5 op-codes for jmp far


		void *pNewFunc = &MyDummySetUnhandledExceptionFilter;
		DWORD dwNewEntryAddr = (DWORD)pNewFunc;
		DWORD dwRelativeAddr = dwNewEntryAddr - dwOrgEntryAddr;


		newJump[0] = 0xE9;  // JMP absolute
		memcpy(&newJump[1], &dwRelativeAddr, sizeof(pNewFunc));
		SIZE_T bytesWritten;
		BOOL bRet = WriteProcessMemory(GetCurrentProcess(), pOrgEntry, newJump, sizeof(pNewFunc) + 1, &bytesWritten);
		return bRet;
	}


	LONG WINAPI UnhandledExceptionFilterEx(struct _EXCEPTION_POINTERS *pException)
	{
    
    
		TCHAR szMbsFile[MAX_PATH] = {
    
     0 };
		::GetModuleFileName(NULL, szMbsFile, MAX_PATH);
		TCHAR* pFind = _tcsrchr(szMbsFile, '\\');
		if (pFind)
		{
    
    
			*(pFind + 1) = 0;
			//这边是生成的DUMP文件的名称
			_tcscat(szMbsFile, _T("CrashDumpFile.dmp"));
			CreateDumpFile((LPCWSTR)szMbsFile, pException);
		}


		//这边提示的消息可根据自己的需求设计
		// TODO: MiniDumpWriteDump
		MessageBox(NULL, _T("***程序不小心崩溃***"), _T("错误"), MB_OK);
		//FatalAppExit(-1, _T("***又双叒叕崩溃了,惊不惊喜?意不意外?***"));
		return EXCEPTION_CONTINUE_SEARCH;
	}


	void RunCrashHandler()
	{
    
    
		SetUnhandledExceptionFilter(UnhandledExceptionFilterEx);
		PreventSetUnhandledExceptionFilter();
	}
};


#define DeclareDumpFile() NSDumpFile::RunCrashHandler();

The above generated code comes from https://blog.csdn.net/zl_95520/article/details/82985273

class TestDump
{
    
    
public:
	TestDump()
	{
    
    

	}
	~TestDump()
	{
    
    

	}
	void testDumpFunc()
	{
    
    
		std::wcout << "我只是一条输出语句" << std::endl;
	}
	void dump()
	{
    
    
		int *p;
		*p = 5;
		int a = 5;
	}
};
int main()
{
    
    
	// 加入崩溃dump文件功能
	DeclareDumpFile();
	TestDump test;
	test.testDumpFunc();
	test.testDumpFunc();
	test.testDumpFunc();
	test.dump();
	test.testDumpFunc();
	test.testDumpFunc();
	test.testDumpFunc();
	return 0;
}

note

1. Run the exe file to generate a dump file after the crash (using the above code is to generate the dump file in the current exe directory)
2. If there is no exe file in the release directory when the release version is generated, then please check the exe generation directory in the routine
Insert picture description here
3. Please make sure that the release has generated debugging information for specific steps, see above

dmp file analysis

1. Configure the source code directory to
open a project solution (you can create a new one, you can use the existing one). Right-click properties. Select the common attribute debugging source file to add the program source code directory. The
Additional source code directory
blue box in the above figure is the additional source code directory
. 2. Configure pdb information.
Drag the generated dump file directly into vs.
dump file information
After dragging in, you can see the above interface. The red framed part at the top right. Set the symbol path. click to enter. Configure the pdb file.
Configure PDB information
The green boxed part as above is the configured pdb file directory. After the configuration is complete. Just click OK.
In the dump file information interface above , press F5 to locate the crash.
After F5 is running
As shown in FIG. Check the function call stack according to the blue box on the right. Can be located to crash out. Then you can analyze the cause of the crash. (The above picture is because the pointer is not allocated memory. It is used directly, which leads to a crash) So
far, the analysis of the dump file has ended

dump file analysis article

https://www.cnblogs.com/yudongdong/p/9687320.html

Guess you like

Origin blog.csdn.net/weixin_39308337/article/details/107134249