"Reprint" C + + program Crash Troubleshooting method

Source: Internet
Author: User
Tags vc runtime

Windows C + + program release version crash troubleshooting method.

A well-designed 24-hour uninterrupted running, multi-threaded program, suddenly run a few months later, this problem is very difficult to troubleshoot, but also a headache problem.

Now use Google Open Source Tool crashrpt with Microsoft WinDbg tool to solve this problem and share it with everyone.

Use tools crashrpt, Windbg. Because WinDbg this tool is very common, temporarily not introduced. Which highlights the crashrpt.

  I. Introduction of CRASHRPT

Crashrpt is a tool that contains the ability to generate a program error report when a program has various types of unhandled exceptions, and then send the report to the developer in a specified manner, such as HTTP or SMTP, and finally analyze the information.

The CRASHRPT consists of 3 parts:

(1) Error report generation library Crashrpt

We need to use the library in our own programs to capture exceptions that our program does not handle, and CRASHRPT generates minidump files after the library captures these unhandled exceptions.

and package The information you specify with the library (such as log files and screens) together into an error report.

The CRASHRPT library supports handling all kinds of exceptions that I know that are thrown by all Windows C + + programs, but also captures C + + exceptions, signals, and errors in calling functions in various CRT libraries.

(2) Exception message sending tool Crashsender

The tool can send us the generated error report in the way we specify (HTTP, SMTP, or MAPI) in the way we use the CRASHRPT setting.

(3) Automatic Exception information processing tool Crprober

The tool can receive the error report sent to us in the background by Crashsender, and output the program's exception information in text form after parsing the error report.

 Ii. Download and install crashrpt

(1) Download crashrpt

Crashrpt:https://code.google.com/p/crashrpt

For a more detailed introduction to CRASHRPT, you can refer to the Polygon https://code.google.com/p/crashrpt/(the mainland may be difficult to access) and http://crashrpt.sourceforge.net/docs/ Html/getting_started.html (continent may be difficult to access)

Download the extracted directory as shown in: Where the bin directory contains all the CRASHRPT related libraries and programs compiled with VC10, the include and Lib directories contain the header files and Lib files needed for development.

(2) Compiling crashrpt with VC

If you don't mind when the program is released with the VC10 runtime, or your program is developed with VC10, you can use these compiled binaries directly.

If you want crashrpt and your program to rely on the same VC runtime, then you need to recompile crashrpt with your VC.

For friends who use a VC version other than VC10, if you want to compile crashrpt, you need to use the Open source cross-compilation tool CMake, we need to use CMake to generate the same solution as your VC version and the project file .

{

Attached CMake installation usage: First download and install the latest version CMake for your system in http://cmake.org/cmake/resources/software.html (test accessible).

When the installation is complete, run Cmake-gui, enter the top-level directory of CRASHRPT in the where is the source code text box and where to build the binaries text box, That is, the directory containing the file CMakeLists.txt,

Then click on the Configure button and select your VC version in the Pop-up dialog and click Finish, then the output: Select the option to match your VC version in the list box, and finally click the Gnerate button

You can generate solutions and engineering files that match your VC version.

}

Using VC to open the generated solution file, here I use the VC version is VC9, the solution is shown in the project: In the solution, click Build in the context menu, you can generate CRASHRPT.

  Iii. Generating Error reports

Let's look at how to use the CRASHRPT library to generate error reports.

First we need to declare a cr_instalgl_info struct and then set it to our own needs, that is, you can use the Crinstall function to install the exception handler in the CRASHRPT to the program.

After the function is called, if an uncaught exception occurs in the program, Crashrpt catches the exception and generates the MiniDump file .

For a detailed description of the CR_INSTALGL_INFO structure, please refer to http://crashrpt.sourceforge.net/docs/html/struct_c_r___i_n_s_t_a_l_l___i_n_f_o_a.html.

In addition to the MiniDump file, we can also call the CrAddFile2 function to add the specified file to the error report , for example, we can add the program-related log files to the error report, so that we better analyze the internal state of the program, Then through this information faster to find the cause of the program error;

In addition to being able to add the specified file, we are able to add a screen by calling the Craddscreenshot function , so that when the program crashes, we can include the current screen in the error report;

Sometimes the hardware that runs the program may be the cause of the program crash, we can add the custom information to the XML description file in the error report by calling the Craddproperty function ;

Finally, we can also call the Craddregkey function to include information about the registry in the error report ;

Remember to call the Cruninstall function before the end of the program to clean up the related resources used by Crashrpt .

For a more detailed introduction to CRASHRPT use, please refer to http://crashrpt.sourceforge.net/docs/html/using_crashrpt_api.html

  Iv. examples

Now let's take a look at a sample MyApp using the CRASHRPT Library, the program MyApp has 2 threads, the main thread is responsible for interacting with the user, and the other worker is responsible for handling some of the operations that take a lot of time to complete.

The program will also create a log file that we can use CRASHRPT Library to package the file with the MiniDump file when the program crashes, in order to better analyze the cause of the program crash.

The code for the program is as follows:

#include <windows.h>

#include <stdio.h>

#include <tchar.h>

#include "CrashRpt.h"

<span style= "font-family:monospace, fixed; ">//contains header files required for Crashrpt library use </span>

file* g_hlog = NULL; Log file handle//callback function called by CRASHRPT when the program crashes

BOOL WINAPI crashcallback (lpvoid/*lpvstate*/) {

You need to close the log file handle here, otherwise crashrpt cannot operate on a file that is in a occupied state

if (g_hlog!=null)

{

Fclose (G_hlog);

G_hlog = NULL;

}

Returns true, generated by CRASHRPT error report

return TRUE;} Log function void Log_write (LPCTSTR szformat, ...) {

if (G_hlog = = NULL)

Return

Va_list args;

Va_start (args);

_vftprintf_s (G_hlog, Szformat, args);

Fflush (G_hlog);} Thread handler function DWORD WINAPI ThreadProc (LPVOID lpparam) {

Installing the CRASHRPT library on this thread handles unhandled exceptions

CrInstallToCurrentThread2 (0);

Log_write (_t ("Entering the Thread proc\n"));

for (;;)

{

Simulate a memory out of bounds here

int* p = NULL;

*p = 13;

}

Log_write (_t ("Leaving the Thread proc\n"));

Clean up Crashrpt Resources

Cruninstallfromcurrentthread ();

return 0;} int _tmain (int argc, _tchar* argv[]) {

Set the parameters of the Crashrpt

Cr_install_info INFO;

memset (&info, 0, sizeof (cr_install_info));

INFO.CB = sizeof (Cr_install_info);

Info.pszappname = _t ("MyApp");

Info.pszappversion = _t ("1.0.0");

Info.pszemailsubject = _t ("MyApp 1.0.0 Error report");

Info.pszemailto = _t ("[email protected]");

Info.pszurl = _t ("http://myapp.com/tools/crashrpt.php");

Info.pfncrashcallback = Crashcallback;

Info.upriorities[cr_http] = 3;

Send error reports first using HTTP

INFO.UPRIORITIES[CR_SMTP] = 2;

Then send the error report using SMTP

INFO.UPRIORITIES[CR_SMAPI] = 1; Finally try to send the error report using the Smapi method

Captures all the exceptions that can be caught, transmitted using HTTP binary encoding

Info.dwflags |= cr_inst_all_possible_handlers;

Info.dwflags |= cr_inst_http_binary_encoding;

Info.dwflags |= Cr_inst_app_restart;

Info.dwflags |= Cr_inst_send_queued_reports;

Info.pszrestartcmdline = _t ("/restart");

Privacy Policy URL

Info.pszprivacypolicyurl = _t ("http://myapp.com/privacypolicy.html");

int nresult = Crinstall (&info);

if (nresult!=0)

{

TCHAR szerrormsg[512] = _t ("");

Crgetlasterrormsg (SZERRORMSG, 512);

_tprintf_s (_t ("%s\n"), szerrormsg);

return 1;

}

Add a log file to the error report

CrAddFile2 (_t ("Log.txt"), NULL, _t ("Log File"), cr_af_make_file_copy);

Add screenshot to error report when program crashes

Craddscreenshot (Cr_as_virtual_screen);

Add arbitrary information to the error report, with video card information as an example

Craddproperty (_t ("Videocard"), _t ("NVidia GeForce 8600 GTS");

errno_t err = _tfopen_s (&g_hlog, _t ("Log.txt"), _t ("WT"));

if (err!=0 | | g_hlog==null)

{

_tprintf_s (_t ("Error opening log.txt\n"));

return 1; Couldn ' t open log file

}

Log_write (_t ("Started successfully\n"));

HANDLE Hworkingthread = CreateThread (NULL, 0,

ThreadProc, (LPVOID) NULL, 0, NULL);

Log_write (_t ("Created working thread\n"));

tchar* szformatstring = NULL;

_tprintf_s (szformatstring);

WaitForSingleObject (Hworkingthread, INFINITE);

Log_write (_t ("Working thread has exited\n"));

if (g_hlog!=null)

{

Fclose (G_hlog);

G_hlog = NULL;

}

Cruninstall ();

return 0;}

There are a few points to note in the sample program:

1. If you want to include log files in the error report, keep in mind that you must use the Crashcallback function similar to the example to set the Pfncrashcallback field in Cr_install_info and close the handle to the log file in the function.

2. Based on my experience, there is no need to use crinstalltocurrentthread2/cruninstallfromcurrentthread in the thread to install the exception handling process, as long as the crinstall is called in the main thread. will be able to catch exceptions that are unhandled in all threads in the program.

3. The reason for calling Crinstall error is generally that CrashRptXXXX.dll, CrashSenderXXXX.exe, and Crashrpt_lang.ini are not placed in the correct path, which, by default, is the same path as the application. where xxxx refers to the version number of the CRASHRPT, the version number in this article is 1300.

For an introduction to this example, please refer to the http://crashrpt.sourceforge.net/docs/html/simple_example.html

Finally, a block of code I use CRASHRPT, I use the purpose is to pass the program to the tester to test, if the program crashes, CRASHRPT Save the program error report to local, the tester found that the program crashed, the report sent to me for debugging.

The code looks like this:

int main (int argc, char **argv) {#if defined (WIN32) && defined (USE_CRASHRPT)

Cr_install_info INFO = {0};

INFO.CB = sizeof (Cr_install_info);

Info.pszappname = TEXT ("xxx");

Info.pszappversion = TEXT ("0.1.0");

Info.dwflags |= cr_inst_all_possible_handlers;

Info.dwflags |= Cr_inst_dont_send_report;

Info.pszerrorreportsavedir = TEXT ("./xxx");

if (exit_success! = Crinstall (&info))

{

TCHAR errormsg[512];

Crgetlasterrormsg (ERRORMSG, 512);

Std::cerr << errormsg;

return exit_failure;

} #endif

int ret = Mainimpl (argc, argv); #if defined (WIN32) && defined (USE_CRASHRPT)

Cruninstall (); #endif

return ret;}

Crashrpt is a powerful tool for generating, sending, and analyzing error reports. Because my use is relatively simple, so I am here to introduce only a small part of the CRASHRPT function, as described in the CRASHRPT documentation, CRASHRPT can be used to send error reports using HTTP, and we use the bug management system linkage, I think this can greatly improve the efficiency of the bug modification,

If you are interested in Crashrpt friends, you can refer to http://crashrpt.sourceforge.net/docs/html/index.html for more in-depth study.

Tested CRASHRPT Resources: http://download.csdn.net/detail/dotnetpig/8543863

Original content excerpt from: http://blog.csdn.net/lingchen214/article/details/11918977 Thank the author of the sharing, I just processing, the resources attached to everyone, so as not to go to Google can not find the right resources, and responsible to tell you the resources I compiled through.

"Reprint" C + + program Crash Troubleshooting method

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.