Write a daemon (7) on Windows to capture exceptions and generate dump, daemon dump

Source: Internet
Author: User

Write a daemon (7) on Windows to capture exceptions and generate dump, daemon dump
Write a Windows daemon (7) to capture exceptions and generate dump

No one can ensure that their Code does not contain any bugs. Once a bug occurs, it is best to crash, so that it can be quickly discovered. If it is not a crash, but the service processing is wrong, it will be troublesome and it may be discovered after a long time.

So how can I find an error if it crashes?

If you have written a Windows Driver, you should know that the system will generate a dump file, and then you can use windbg to analyze the dump file, pdb file, and source code. Applications at the application layer can also generate dump files when they crash, but no one will help you complete this step and you have to do it yourself.

 

1. API

The main API involved here is: MiniDumpWriteDump -- generate the dump file. I have said that there is no official authority. To learn more, please go to MSDN. Other auxiliary APIs are the functions used to set abnormal processors, which are not described here.

 

2. Exception type

When I read "Windows core programming", I should know SEH-Windows structured exception handling. After I read it, I thought that all exceptions were in the SEH system, I thought it had been around for many years. Later I found it was not: Because the library that caught exceptions and generated the dump file did not play its role, the crash still crashed and did not generate the dump file!

Google found this article: http://www.codeproject.com/articles/207464/exception-handling-in-visual-cplusplus.

To sum up, not all exceptions in Windows follow the SEH system, but also the CRT error handling system. In addition, not only exceptions can cause program exit, after receiving several signals supported by Windows, the program may exit, for example, SIGABRT. Some "abnormal" processors are not for processes, but only valid for the current thread, the processor must be set for each thread.

Therefore, to capture all "exceptions", you must:

L call SetUnhandledExceptionFilter to set the SEH Exception Handling Function

L call _ set_purecall_handler to set the handler for errors caused by pure virtual function calls

L call the _ set_new_handler to set the error handler caused by new failure

L call _ set_invalid_parameter_handler to set the error handler caused by Invalid Parameters

L call signal to set the processing functions of SIGABRT, SIGINT, and SIGTERM.

L call set_terminate to set the terminate handler. This is only valid for the current thread

L call set_unexpected to set the unexpected handler. This is only valid for the current thread

L call the processing functions of signal to set signal SIGFPE, SIGILL, and SIGSEGV. This is also valid for the current thread

 

3. Exception Information Retrieval

The ExceptionParam of MiniDumpWriteDump needs to be filled with the exception information. How can this information be filled?

The above article has pointed out the method: use the code of function _ invoke_watson in the vc source code file invarg. c.

He uses the source code of vc8.0. I compiled it with vc2008, so he obtained the source code of vc2008, but it seems that he does not need to care about which vc version of the source code.

 

4. debug the dump file

I put the generated dump file in the directory where the application is located, so that we can see at a glance whether the program has crashed. Once the dump file is generated, we only need to put the binary, pdb files, and dump files together, then use vs to open the dump file and run it directly to debug it. (So we need to submit pdb together when submitting binary data .)

 

I modified the source code in the above article and encapsulated it into two functions. I wrote the test result of my test code in the code.

My use: after the main function is up, set all the processing functions; set thread-related processing functions in each task thread.

 

 

Source code: https://git.oschina.net/mkdym/DaemonSvc.git (main) & https://github.com/mkdym/DaemonSvc.git (to improve the Force Grid ).

 

 

Tuesday, November 10, 2015

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.