C + + Server program Bug dump

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.