Write a daemon on Windows (7) Catch exceptions and generate dump

Source: Internet
Author: User

Write a daemon on Windows (7) Catch exceptions and generate dump

No one can guarantee that their code is not a bug. Once out of the bug, it is best to crash, so quickly can be found, if not crash, but the business process is wrong, it is troublesome, it may be a long time before being discovered.

So, if it crashes, how do you find out what's wrong?

The classmate who wrote the Windows driver should know that once the crash, the system will generate dump files, and then can be based on the dump file, PDB files, source code with WinDbg analysis. Application tier programs can also generate dump files at the time of crash, but no one will help you with this step and you have to do it yourself.

1. API

Here is the main API is one: minidumpwritedump--generated dump file, I said there is no more official authority, so to see the details, or please go to MSDN. Other auxiliary APIs are functions that set up the exception handler, which is not described here.

2. Exception types

Students who read "Windows core programming" should know that seh--windows structured exception handling, when I read it, I thought all the anomalies were within the SEH system, and then "thought" for many years. Later it wasn't: because I wrote the library that caught the exception and generated the dump file, it didn't work, and the crash went down and didn't generate a dump file!

Google found this article: Http://www.codeproject.com/Articles/207464/Exception-Handling-in-Visual-Cplusplus.

I conclude that not all exceptions in Windows go to the SEH system, as well as the CRT error handling system, and not only the exception will cause the program to exit, and receive a few signals supported by Windows will also cause the program to exit, such as SIGABRT, and some "exception" The processor is not for the process, but only valid for the current thread, and must be set by each thread.

So, to catch all the "exceptions", you have to:

L call SetUnhandledExceptionFilter to set SEH exception handling function

L call _set_purecall_handler to set processing functions that handle errors caused by pure virtual function calls

L Call _set_new_handler to set the handler function for error caused by new failure

L Call _set_invalid_parameter_handler to set the handler for the error caused by invalid arguments

L call signal to set the processing function of signal SIGABRT, SIGINT, Sigterm

L Call Set_terminate to set the Terminate handler function. This is only valid for the current thread

L Call set_unexpected to set the unexpected handler function. This is also valid only for the current thread

L call signal to set the processing function of signal SIGFPE, Sigill, SIGSEGV. This is also valid for the current thread

3. Exception Information Acquisition

MiniDumpWriteDump's exceptionparam needs to be populated with exception information, so how do I populate it?

The above article has pointed out the method: VC source file Invarg.c in the function _invoke_watson code.

He code used is vc8.0 source, I was compiled with vc2008, so take the vc2008 source code, but do not seem to care to take which VC version of the source code.

4. Debug dump File

I put the generated dump file in the directory where the application is located, so you can see that the program has crashed. Once the dump file is generated, we simply put the binaries, PDB files, dump files together and then use vs to open the dump file and run it directly to debug. (so we have to submit the PDB together when we commit the binary.) )

I changed the source code in the above article, encapsulated it into two functions, and I wrote the results of the test code test to write the result.

My use: After the main function, set all the handler functions, and set the thread-related handler function on each task thread.

Source: Https://git.oschina.net/mkdym/DaemonSvc.git (Master) && Https://github.com/mkdym/DaemonSvc.git (for lifting).

November 10, 2015 Tuesday

Write a daemon on Windows (7) Catch exceptions and generate 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.