Debugging the server program is most afraid of encountering the need to run 10 days half a month to encounter a bug, this bug is difficult to restore the scene, but also always pay attention to whether the server is hanging off.
This paper gives a solution that can greatly improve the debugging efficiency.
Use this method to automatically dump an assertion failure, which can be used to restore the bug environment for debugging. In addition, the crash dump is automatically recorded when it crashes.
Assertion function
BOOL Xassert (bool R) {
if (!r) __asm int 3 return r;}
The final exception handling function, encountered here the description program can only hang out, write crash dump
Longwinapi Lastexceptionhandler (pexception_pointers pEi) {Auto H=createfile ("Crash.dmp", Generic_write,0,nullptr, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULLPTR); if (h!=invalid_handle_value) {minidump_exception_information Mei;mei . Clientpointers=false;mei. Threadid=getcurrentthreadid (); Mei. Exceptionpointers=pei; MiniDumpWriteDump (GetCurrentProcess (), GetCurrentProcessId (), H,minidumpwithprivatereadwritememory,&mei, NULLPTR,NULLPTR); CloseHandle (h);} Returnexception_continue_search;}
The first exception handler to handle the int 3 inside the Xassert, write debug dump
LONG WINAPI Firstexceptionhandler (pexception_pointers pEi) {
Switch (Pei->exceptionrecord->exceptioncode) {//All software breakpoints are caseexception_breakpoint://software breakpoints that I submitted Int3 0xcc{ staticintndump=0;staticchartmp[1024];_snprintf_s (tmp,1000, "Debug%02d.dmp", ++ndump); auto H=CreateFile (TMP, GENERIC_WRITE,0,NULLPTR,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULLPTR); if (h!=invalid_handle_value) {MINIDUMP_ Exception_information Mei;mei. Clientpointers=false;mei. Threadid=getcurrentthreadid (); Mei. Exceptionpointers=pei; MiniDumpWriteDump (GetCurrentProcess (), GetCurrentProcessId (), H,minidumpwithprivatereadwritememory,&mei, NULLPTR,NULLPTR); CloseHandle (h);} Pei->contextrecord->eip+=1;returnexception_continue_execution;} Break;caseexception_single_step://debug hardware breakpoint {}break;} Returnexception_continue_search;}
At the end of the program initialization, add
Addvectoredexceptionhandler (0,firstexceptionhandler); SetUnhandledExceptionFilter (Lastexceptionhandler);
Code principle.
Xassert execution of a INT3 instruction when failure occurs, resulting in a debug interrupt
Use the exception handler to capture the debug interrupt and revert to the next debug command after writing the dump with DEBUGAPI.
The final exception handler captures any unhandled exceptions, and the program automatically crashes after writing crash dump.
C + + Server program Bug dump