Save related information after software crash
Introduction:
Now, after the server program crashes, although the related stack information is recorded, the recorded crash location is the absolute address in the executable file and the corresponding software needs to be loaded. map File to analyze and obtain the crash location in the code. if the program automatically saves the position of the collapse point in the code when it crashes, it will be much easier to analyze it. especially when the number of logs is large.
There is a dynamic link library named blackbox on the codeproject. The function of this library is to automatically save all useful information on site, such as stacks and registers when the program crashes; CPU usage at that time; memory usage, other existing processes, and physical information of the computer.
If an application is used on our server side, it is unrealistic to modify a lot of code. The best case is that you do not need to modify the existing code. If necessary, you only need to modify a few lines of code. the usage method provided by blackbox is extremely simple. You only need to load the dynamic link library when the program starts. If the program crashes, blackbox will pop up its own window:
Figure 1
Usage:
As mentioned in the introduction, this library is very convenient to use and everything is transparent to the Host Program. The program only needs to start:
Hinstance hlib = loadlibrary ("blackbox. dll ");
Then there is no need for any operation. blackbox will automatically work when the program is abnormal. Of course, it is best to release it before the program ends:
If (null! = Hlib)
Freelibrary (hlib );
The following is a simple test code that I wrote to illustrate how the database works:
# Include "stdafx. H"
# Include
Hinstance hlib = loadlibrary ("blackbox. dll ");
Class nouse
{
Public:
Virtual void nouseapi (void)
{
Int A = 1, B = 1;
Int c = 100/(a-B );
}
};
Int main (INT argc, char * argv [])
{
Nouse T;
T. nouseapi (); // exception except 0
Printf ("dfsdfsdfsdf ");
If (null! = Hlib ){
Freelibrary (hlib );
}
Return 0;
}
The example is very simple. The yellow part is the loading and release of blackbox. In fact, the loading can be placed anywhere. Of course, the sooner the better, the better, so that you can run it before an exception occurs; the red part implements a simple division of 0 exceptions. you can also experiment with any other exceptions. after the program is started, the interface shown in is displayed.
Disadvantages:
Blackbox provides a large number of functions, including email log information to developers, which is more suitable for terminal programs, such as applications on the client for information feedback;
Because the program ends after an exception and the server needs to run continuously, blackbox is not fully suited to our server applications. Fortunately, jim crafton, author of the library, provides all the source code of the program. Therefore, we can modify the code to meet our needs.
Outlook:
It is necessary to transform the blackbox:
• Streamline the database and keep only the most important stack information.
• Remove the pop-up interface and log directly
• Logs are recorded when a program exception occurs, but the program continues to run.
The first two modifications are relatively simple and can be simply modified on the existing code. As for the last one, the changes are troublesome, but it is not impossible. I will modify the changes so that they can be used with debug_try IN THE basecod on the server, debug_catch is used together to record the crash location without modifying the existing code. After an error is recorded, the program can continue to run.