Exception Handling and minidump (4) minidump)

Source: Internet
Author: User
Tags microsoft c
I. Summary

I finally talked about minidump.

I cannot count the usefulness of dump. Basically, it is a powerful tool for locating errors and fixing bugs. (Logs can be regarded as the "Dragon Sword") These are all for those bugs that are not essential, and the bugs that can be found through simple debugging when running on the outside, generally, it is insufficient to fear.

 

Ii. Basic Applications

Minidump is called minidump because it has its mini... (Nonsense), haha, Ms provides an API function, minidumpwritedump, (IN dbghelp. h declares that dbghelp needs to be imported. so I call it minidump. In fact, dump can also express the same meaning ....

The simplest application of minidump is that when the program crashes, the information at the moment of the crash is written into a file to facilitate future error search. The usage is simple and difficult.

1. How can I perceive program crashes?

Window provides a more convenient way to perceive several program crashes.

In the breakpad process description for completing dump in the process, I described how breakpad obtained the program crash. In fact, this is also a typical method for detecting program crashes in windows, this article is a work note written when I finished my own exceptionhandle Library at the beginning of my work. It still seems to have some reference value.

In Windows, there are three core functions to detect program crashes (which is actually a serious error at runtime:

Setunhandledexceptionfilter (handleexception) determines that handleexception is called when an uncontrolled exception occurs.

_ Set_invalid_parameter_handler (handleinvalidparameter) determines that handleinvalidparameter is called when an invalid parameter is called.

_ Set_purecall_handler (handlepurevirtualcall) determines that the function called when a pure virtual function call occurs is handlepurevirtualcall.

The three functions are used in the same way. When an exception occurs (as described above), the callback function is called by the parameter. In Windows, the crash information is passed into the callback function through the parameter, this is the perfect time for dump. For detailed information, refer to msdn. I will not copy the document here, so there is a possibility of copying the document. Here we use setunhandledexceptionfilter as an example to demonstrate the actual use of IT with minidumpwritedump. Similar to these complex APIs, msdn does not even have an example. To be honest, it took a little time to master it.

Predictionexample:

# Include <windows. h>

# Include <dbghelp. h>

Using namespace STD;

 

# Pragma auto_inline (off)

# Pragma comment (Lib, "dbghelp ")

 

// For the sake of simplicity and focus on the items of interest, ignore the error check according to the practice of the sample program. Pay attention to the actual usage.

Long winapi myunhandledexceptionfilter (

Struct _ prediction_pointers * predictioninfo

)

{

Handle lhdumpfile = createfile (_ T ("dumpfile. dmp"), generic_write, 0, null, create_always, file_attribute_normal, null );

 

Minidump_exception_information loexceptioninfo;

Loexceptioninfo. exceptionpointers = exceptioninfo;

Loexceptioninfo. threadid = getcurrentthreadid ();

Loexceptioninfo. clientpointers = true;

Minidumpwritedump (getcurrentprocess (), getcurrentprocessid (), lhdumpfile, minidumpnormal, & loexceptioninfo, null, null );

 

Closehandle (lhdumpfile );

 

Return exception_execute_handler;

}

 

 

Void fun2 ()

{

Int * P = NULL;

* P = 0;

}

 

Void fun ()

{

Fun2 ();

}

 

Int main ()

{

Setunhandledexceptionfilter (myunhandledexceptionfilter );

 

Fun ();

 

Return 1;

}

 

The API call is only released. Check the msdn method,

# Pragma auto_inline (off)

# Pragma comment (Lib, "dbghelp ")

The first sentence is to cancel the automatic inline effect to achieve better demonstration. Otherwise, simple functions such as fun and fun2 will be automatically inline, so there is no stack, so it is difficult to see the role of dump in reality. The effect is the same as that of vs2005 compilation options, C/C ++-> Optimization-> inline function expansion-> only _ inline.

The second sentence indicates importing the dbghelp library to use the minidumpwriteminidump API. It works the same way as adding dbghelp. lib to the connector> input> additional dependency of vs2005 compilation options.

 

Actually running the program. (You cannot call the test run in vs. Otherwise, the control of exceptions is always controlled by vs. Therefore, there is always no way to control myunhandledexceptionfilter. For detailed descriptions, refer to 2 ), you can obtain a file named dumpfile. DMP file. This file is what we call dump for a long time. The use of the other two functions is similar to that of minidumpwriteminidump.

 

2. Dump File Usage

The usage of dump files in Windows is very simple, but it is because it is too simple, so the description on the Internet is very simple. When I think of it, I was very excited when I threw the dump file, the solution finds that there is no way to take the dump file. There is always no clue about how to use vs to open and debug the file on the Internet .... Haha

The correct method is to place the DMP, PDB, and exe files of the crashed program in the same directory, and then double-click to run DMP (or open it with ), then a project named dumpfile is displayed and contains a dumpfile. Right-click the project, select debug-> Start a new instance (or start and start a new instance in one step). At this time, the program is automatically adjusted to the line that crashes in the source code, in addition, the call stack contains complete stack information, and the value of temporary variables can also be displayed through vs, as well as module information and thread information,

In the preceding example, the stack information is:

> Exception.exe! Fun2 () Row 36 C ++

Exception.exe! Main () Row 50 C ++

Exception.exe! _ Tmaincrtstartup () Row 597 + 0x17 bytes C

Kernel32.dll! 7c817077 ()

[The following framework may be incorrect and/or missing, and the symbols are not loaded for kernel32.dll]

Ntdll. dll! 7c93005d ()

 

Then, the register value is:

Eax = 00000000 EBX = 00000000 ECx = 492b623

EdX = 7c92e514 ESI = 00000001 EDI = 00403384

EIP = 00401072 ESP = 0013ff7c EBP = 0013ffc0

EFL = 1, 00010246

 

In normal mode, the dump file is fast, but there is no memory information. You can even dump the entire memory at run time by adjusting the minidumpwriteminidump parameter, check the msdn minidumpwriteminidump information.

With this information, the program's error locating (usually with a lot of NULL pointer access in C ++) is very clear, and with the log, the general error is not difficult to find. Here, it is explained that when a running program is renamed or merged into another place for running, in this way, there is no complete information in the stack information at the beginning, in this case, you can right-click the load symbol in the stack information and select the appropriate file PDB. Then the information will come out ..... (This problem has plagued us for a day)

 

3. Advanced Applications

The program crash problem is solved. The problem is that many programs are not allowed to crash at will. In this way, it is a little late to discover the problem after the program crash, is there a way to discover problems without program crash? The previously described seh is a way to keep the program from crashing, but in that way, according to the previously described method, the crash will not crash, but in fact, it masks many problems, it is not suitable for Problem Discovery. As described above, minidump is a good way to quickly discover problems, but it cannot avoid program crashes. What is the ultimate solution? Since our goal is not to crash the program + quickly discover problems, the ultimate solution is seh + minidump. :) seh and minidump are both Windows features, and Ms does provide a combination of methods. See the example below. Don't be too excited .... This is also because our company's servers had no notifications from the internal test several times a day, warning the crash (the Director even got up three o'clock in the middle of the night to solve the server crash problem due to my problem) now, the server has basically never crashed. Even if there is a problem, there is plenty of time to solve it. Then, I will send a notice to the server, telling the file server that temporary maintenance is required .... They all rely on this ultimate solution .....

The usage and features of seh are not repeated here. See the previous article. 《Exception Handling and minidump (3) Seh (structured exception handling)"

To use minidumpwriteminidump, You need to obtain information about the minidump_exception_information structure. The most important information in this structure comes from the pexception_pointers information. In the above example, when the program crashes, the exception handler function we set is passed in as a parameter in windows. Now the main problem is where to get the exception information. Through msdn, we find the getexceptioninformation function, the returned information is shown in msdn:

Lpexception_pointers getexceptioninformation (void );

However, Ms provides a notice:

The Microsoft C/C ++ Optimizing Compiler interprets this function as a keyword, and its use outside the appropriate exception-handling syntax generates a compiler error.

In fact, when I started using it, I tried it all at the beginning, and it was a compilation error. Because this function is used in such a strange way, and there is no example ..... Finally, in despair... I saw the description of the word function in platform builder for Microsoft Windows CE 5.0. There is a description in it, and then I vomit blood ....

Try

{

// Try Block

}

Except T (filterfunction (getexceptioninformation ())

{

// Exception handler Block

}

 

It turns out to be used in this way .......... Dizzy

Handlewithoutcrash example:

# Include <windows. h>

# Include <dbghelp. h>

Using namespace STD;

 

# Pragma auto_inline (off)

# Pragma comment (Lib, "dbghelp ")

 

// For the sake of simplicity and focus on the items of interest, ignore the error check according to the practice of the sample program. Pay attention to the actual usage.

Long winapi myunhandledexceptionfilter (

Struct _ prediction_pointers * predictioninfo

)

{

Handle lhdumpfile = createfile (_ T ("dumpfile. dmp"), generic_write, 0, null, create_always, file_attribute_normal, null );

 

Minidump_exception_information loexceptioninfo;

Loexceptioninfo. exceptionpointers = exceptioninfo;

Loexceptioninfo. threadid = getcurrentthreadid ();

Loexceptioninfo. clientpointers = true;

Minidumpwritedump (getcurrentprocess (), getcurrentprocessid (), lhdumpfile, minidumpnormal, & loexceptioninfo, null, null );

 

Closehandle (lhdumpfile );

 

Return exception_execute_handler;

}

 

 

Void fun2 ()

{

_ Try

{

Static bool B = false;

If (! B)

{

B = true;

Int * P = NULL;

* P = 0;

}

Else

{

MessageBox (null, _ T ("here"), _ T (""), mb_ OK );

}

 

}

_ Random t (myunhandledexceptionfilter (getexceptioninformation ()))

{

}

}

 

Void fun ()

{

Fun2 ();

}

 

Int main ()

{

Fun ();

Fun ();

 

Return 1;

}

 

In this example, you can debug the program, because the program will not crash, so Vs will not compete with you for exception Control. At the same time, when you see the dump file, you can also see that the program actually continues to run, because MessageBox still pops up. This... Is what we want .....

I suddenly thought of a song .... "I want to nobody but you... I want nobody but you?

Here are several key points. getexceptioninformation () can only be called in the filter called by Ms of _ blocks T. compilation errors will be reported elsewhere. Second, the returned value has the same meaning as the general _ handler T. To run the program, you must return prediction_execute_handler, indicating that the exception is under control. For the meanings of the other values, see seh in the previous article.

 

Iv. References
  1. Msdn-Visual Studio 2005, Microsoft
  2. Efficient troubleshooting of Windows user-state programs, Xiong Li, Electronics Industry Press

Previous articles:

Exception Handling and minidump (3) Seh (structured exception handling)

Exception Handling and minidump (2) intelligent pointer and C ++ exception

Exception Handling and minidump (1) c ++ exceptions

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.