Generate dump file method under Windows

Source: Internet
Author: User

"Reprint Please specify source": http://blog.csdn.net/longlong530

I. BACKGROUND

No one can guarantee that their software is running in a variety of unknown environments and will have any problems with wood. So what if the program crashes? Look at the logs? What about the log not all? How detailed can the log help you locate? If there is a way to record the state of the final work of the program, such as the stack call situation, then we can learn "how it hangs out ~"


Two. Research

We investigated five ways to locate program crashes due to program bugs, and finally chose Scenario 5 for the program crash targeting that I used for my project.

Scenario 1: Crash address + Map file
Use the map file generated when the program crashes to locate it. This scheme can only be used for programs developed in previous versions of VC7


Scenario 2: Crash address + Map file + cod file
This solution is primarily intended to address the shortcomings of scenario 1. Since VC8 later versions no longer support the generation of code line information in the map file, the method of adding the Cod file is used to locate the problem.


Scenario 3: Crash address + PDB file + Crashfinder

Description: The first three scenarios, in fact, only need to inform the user crash address, and then find crash address on the local can be, but the process of locating crash is very inconvenient, if the situation of crash than
More, the first three kinds of programs are not suitable. Furthermore, the first three scenarios do not generate stack call information and have limited effect on debug.

Solution 4:setunhandledexceptionfilter + Stackwalker
This method requires the PDB file to correctly generate the function line number and code line number of the stack call, so it is only suitable for debugging on the local release version.

Solution 5:setunhandledexceptionfilter + Minidump
This method is the tool we use to capture the dump file, so here's a little bit more on it.


Three. Code sharing

The core code is as follows:

#include <stdio.h> #include <time.h> #include <windows.h> #include <DbgHelp.h> #pragma comment ( LIB, "DbgHelp.lib") LONG WINAPI toplevelfilter (struct _exception_pointers *pexceptioninfo) {//return Exception_continue_ SEARCH, let the program stop running LONG ret = exception_continue_search;time_t nowtime;time (&nowtime); struct TM *ptime = localtime (&amp ; nowtime); char szfile[128] = {0};//Set core file build directory and file name sprintf (szfile, "c:\\%4d.%0 2d.%0 2d_%02d.%0 2d.%0 2d.dmp ", ptime->tm_year+1900, Ptime->tm_mon+1, Ptime->tm_mday, Ptime->tm_hour, PTime->tm_min, pTime- &GT;TM_SEC); HANDLE hfile =:: CreateFile (Szfile, generic_write, file_share_write, NULL, create_always, file_attribute_normal, NULL); if (hfile! = invalid_handle_value) {minidump_exception_information exinfo; Exinfo.threadid =:: GetCurrentThreadID (); Exinfo.exceptionpointers = Pexceptioninfo; Exinfo.clientpointers = null;//Write the Dumpbool BOK = MiniDumpWriteDump (GetCurrentProcess (), GetCurrentProcessId (), H File, Minidumpnormal, &exinfo, NULL, NULL); ret = Exception_execute_handler;::closehandle (hfile);} return ret;}

Code porting methods:

1 Copy the following header file into your code
#include <time.h>
#include <windows.h>
#include <DbgHelp.h>

2 introducing a static library of Gdbhelp
#pragma comment (lib, "DbgHelp.lib")

3 Copy the Toplevelfilter function into your code

4 in the Mian function, add the following code
:: SetUnhandledExceptionFilter (Toplevelfilter);

5 Modify project Properties, configuration Properties, general--property defaults, modify the character set configuration to use multibyte character sets


6 Compile and run.


Four. Code interpretation


A) SetUnhandledExceptionFilter

The SetUnhandledExceptionFilter function is declared as follows:

Lptop_level_exception_filter WINAPI setunhandledexceptionfilter (      __in          lptop_level_exception_filter Lptoplevelexceptionfilter    );

Setunhandleexceptionfilter allows us to set our own function as the global SEH filter function, which will be called by our function before the program crash. We can use it.
is a variable exceptioninfo of the _exception_pointers struct type that contains the description of the exception and the state of the thread in which the exception occurred, and the filter function can return different values to
Let the system continue to run or exit the application.


b) Minidump

Minidump:minidump (small memory dump) can be understood as a dump file, which records the smallest useful information that can help debug crash. In fact, if you are in System properties
When you select Small memory dump (KB) in the Write debugging information, startup and recovery settings, advanced, start and fail-back, you will be on the C:\Windows\Minidump\ road when the system stops unexpectedly

Path to generate a. dmp suffix file, this file is the minidump file, but this is the kernel state of the minidump. API function to generate MiniDump file: MiniDumpWriteDump, this function requires dbghelp.lib support.

BOOL WINAPI minidumpwritedump (      __in          HANDLE hprocess,      __in          DWORD ProcessId,      __in          HANDLE hfile,      __in          minidump_type dumptype,      __in          pminidump_exception_information Exceptionparam,      __          in Pminidump_user_stream_information userstreamparam,      __in          pminidump_callback_information Callbackparam    );


The debug dump file requires a PDB file first, so we need to set debug infomation format as "program Database" When we build the application. Secondly, we
Also make sure that the dump file used is consistent with the source code, EXE, and PDB file versions, which requires that we maintain good program version information. The most convenient environment for debugging minidump is VS,
All we have to do is put the. dmp,. exe,. pdb files in one path, and ensure that the path to the source code file is consistent with the compile-time path, and the rest is the VS help us. Double-click. dmp
File or in the File open project Select "Dump Files", load dump file, and then press F5 Run will be able to directly restore crash when the scene, you can locate crash code, you can
View the call stack to view thread and module information.


Five. Precautions

For the release version of the program, many of the code is compiler-optimized, so the location of the time may be biased, you can consider setting options to remove the code

Optimization. Detailed methods for using minidump can be found in: http://vicchina.51.net/research/other/seh/minidumps/intro.htm


"Reprint Please specify source": http://blog.csdn.net/longlong530

Generate dump file method under Windows

Related Article

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.