windows Next C++ Program release Version crash troubleshooting .

One that you designed 24 Hours of uninterrupted operation , Multithreaded programs , All of a sudden, after running for a few months, it collapsed , This problem is very difficult to investigate , It's also a headache .

Current use Google Open source tools crashrpt And Microsoft windbg Tools , Solve this problem , And share it with you .

Using tools Crashrpt、Windbg. because windbg This tool is very common , Don't introduce . It focuses on crashrpt.

One 、crashrpt brief introduction

crashrpt Is a program that can generate program error reports when various types of unhandled exceptions occur , Then the report will be sent in the specified way ( for example HTTP perhaps SMTP) Send to developer , Finally, tools for analyzing this information .

crashrpt from 3 Component composition :

(1) Error report generation library CrashRpt

We need to use this library in our own program to catch exceptions that our program doesn't handle , After the library catches these unhandled exceptions ,CrashRpt Will generate MiniDump file ,

And will use the information specified in the library with you ( For example, log files and screen shots, etc ) Package them together into error reports .

CrashRpt Library support handles all I know Windows C/C++ All kinds of exceptions thrown by the program , And capture C++ abnormal 、 Signals and calls CRT Error in function in Library .

(2) Exception information sending tool CrashSender

The tool can be used according to our requirements CrashRpt How to set it up , The error report will be generated in the way we specify (HTTP、SMTP perhaps MAPI) Send it to us .

(3) Automatic exception information processing tool crprober

The tool can receive data in the background CrashSender Error report sent to us , After analyzing the error report, the abnormal information of the program is output in the form of text .

Two 、 Download and install  crashrpt

(1) download crashrpt

crashrpt Download address :https://code.google.com/p/crashrpt

About crashrpt A more detailed introduction , You can refer to the surface https://code.google.com/p/crashrpt/ ( The mainland may be hard to access ) as well as http://crashrpt.sourceforge.net/docs/html/getting_started.html ( The mainland may be hard to access )

Download and unzip the directory as shown in the figure below : among bin The directory contains the vc10 All compiled crashrpt Related libraries and programs ,include and lib The directory contains the header files and the lib file .

(2) Use vc compile crashrpt

If you don't mind the app being released with it vc10 The runtime , Or your program just uses vc10 Developed , You can use these compiled binaries directly .

If you want to crashrpt And your program depends on the same vc Runtime , Then you need to use your vc recompile crashrpt.

For using Division vc10 Other than vc Version of friends , If you want to compile crashrpt, Open source cross compiling tools are needed cmake, We need to Use cmake Generation and you vc The same version of solution and project file .

{

attach cmake How to install and use : First, in the http://cmake.org/cmake/resources/software.html ( Tests can access ) Download and install the latest version suitable for your system cmake.

After installation , function cmake-gui, stay where is the source code Text box and where to build the binaries Enter in text box crashrpt Top level directory of , That is, include files CMakeLists.txt The catalog of ,

And then click Configure Button , Then select your name in the pop-up dialog box vc Version and click finish, The output as shown in the figure appears :  In the list box, select the and your vc Version matching options , Finally, click Gnerate Button ,

Can be generated with you vc The version matches the solution and project file .

}

Use vc Open the generated solution file , I use it here vc The version is vc9, The solution includes the project shown in the figure below :  Right click in the solution menu build, It can generate crashrpt.

3、 ... and 、 Generate error report

Let's take a look at how to use it crashrpt Library to generate error reports .

First of all, we need to declare a CR_INSTALgL_INFO Structure , Then set it according to your own needs , You can use crInstall Function to install into the program crashrpt Exception handling functions in .

After calling this function , If an uncapped exception occurs in the program , be crashrpt Meeting The exception is caught and generated MiniDump file ,

About CR_INSTALgL_INFO Detailed description of 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.

except MiniDump Outside the document , We can also go through call  crAddFile2 Function to add the specified file to the error report in , For example, we can add program related log files to the error report , So that we can better analyze the internal state of the program , And then find the cause of the program error faster through this information ;

In addition to the ability to add specified files , We can still get through call  crAddScreenshot Function to add a screenshot , So when the program crashes , We were able to include screen shots of that time in the error report ;

Sometimes the hardware running the program can also be the cause of the program crash , We can go through call  crAddProperty function , To add custom information to an error report xml In the description file ;

Last , We can still call crAddRegKey Function to include information about the registry in the error report ;

Please remember before the end of the program call crUninstall Function cleanup crashrpt Related resources used .

About crashrpt A more detailed introduction to using , Please refer to http://crashrpt.sourceforge.net/docs/html/using_crashrpt_api.html

Four 、 Example

Now let's take a look at using crashrpt Examples of Libraries MyApp, Program MyApp Have 2 Threads , The main thread is responsible for interacting with the user , Another worker thread is responsible for some operations that take a lot of time to complete .

The program will also create a log file , We can use crashrpt When the program crashes, the library stores the file with the MiniDump The files are packaged and sent to us , So that we can better analyze the cause of the program crash .

The code of the program is as follows :

#include <windows.h>

#include <stdio.h>

#include <tchar.h>

#include "CrashRpt.h"

<span style="font-family: monospace, fixed; ">// contain crashrpt The header file required by the library </span>

FILE* g_hLog = NULL; // Log file handle // When the program crashes, the crashrpt Called callback function

BOOL WINAPI CrashCallback(LPVOID /*lpvState*/){

// You need to close the log file handle here , otherwise crashrpt Cannot operate on file in occupied state

if(g_hLog!=NULL)

{

fclose(g_hLog);

g_hLog = NULL;

}

// return TRUE, from crashrpt Generate 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 DWORD WINAPI ThreadProc(LPVOID lpParam){

// Install in this thread crashrpt Library's handling of unhandled exception

crInstallToCurrentThread2(0);

log_write(_T("Entering the thread proc\n"));

for(;;)

{

// Simulate a memory overrun here

int* p = NULL;

*p = 13;

}

log_write(_T("Leaving the thread proc\n"));

// clear crashrpt resources

crUninstallFromCurrentThread();

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

// Set up crashrpt Parameters of

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("myapp_support@hotmail.com");

info.pszUrl = _T("http://myapp.com/tools/crashrpt.php");

info.pfnCrashCallback = CrashCallback;

info.uPriorities[CR_HTTP] = 3;

// use first HTTP Send error reports in the same way

info.uPriorities[CR_SMTP] = 2;

// And then use SMTP Send error reports in the same way

info.uPriorities[CR_SMAPI] = 1; // Finally try to use SMAPI Send error reports in the same way

// Catch all exceptions that can be caught , Use HTTP Transmission by binary code

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 log file to error report

crAddFile2(_T("log.txt"), NULL, _T("Log File"), CR_AF_MAKE_FILE_COPY);

// Add a screen capture of a program crash to the error report

crAddScreenshot(CR_AS_VIRTUAL_SCREEN);

// Add any information to the error report , Here, the graphics card information is taken 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 several points to note in the sample program :

1. If you want to include the log file in the error report , Remember to use something similar to the one in the example CrashCallBack Function to set CR_INSTALL_INFO Medium pfnCrashCallback Domain , And close the handle of the log file in the function .

2. According to my experience , It doesn't need to be used in threads crInstallToCurrentThread2/crUninstallFromCurrentThread Use this pair of functions to install exception handling , As long as it is called in the main thread crInstall. You can catch the unhandled exception in all threads in the program .

3. call crInstall The reason for the error is that there is no CrashRptXXXX.dll、CrashSenderXXXX.exe as well as crashrpt_lang.ini Put it in the right path , By default , This is the same path as the application . Among them XXXX refer to crashrpt Version number of , The version number in this article is 1300.

An introduction to the example , Please refer to http://crashrpt.sourceforge.net/docs/html/simple_example.html

I use the last paragraph crashrpt Code block for , The purpose I use is to give the program to the tester for testing , If the program crashes ,crashrpt Save the error report of the program locally , After the tester found that the program crashed , Send the report to me for debugging .

The code is as follows :

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 very powerful error report generation 、 Sending and analysis tools . Because my use is relatively simple , So what I'm introducing here is crashrpt A small part of the function , according to crashrpt Description in document ,crashrpt It can be used at all times http When sending error report , It's the same as what we use BUG Management system for linkage , I think it can be greatly improved BUG Efficiency of modification ,

If the crashrpt Interested friends , You can refer to http://crashrpt.sourceforge.net/docs/html/index.html Learn more .

Passed the test crashrpt Resource download address :http://download.csdn.net/detail/dotnetpig/8543863

The original text is excerpted from :http://blog.csdn.net/lingchen214/article/details/11918977 Thank you for sharing , I'm just processing it , Attach resources to everyone , So as not to go google No suitable resources were found , And responsible to tell you that the resources I compiled passed .

【Crash】C++ More related articles on program crash detection methods

  1. [CareerCup] 12.2 Find the Reason of Crash Find out why the program crashed

    12.2 You are given the source to an application which crashes when it is run. After running it ten t ...

  2. Water and fire are hard to contain : Synchronous method call async method-initiated ASP.NET Application crash

    It was only known that the asynchronous method was invoked in the synchronization method. (async) When the method is used , If you use .Result Waiting for call result , Can cause thread deadlock (deadlock). I've suffered from it myself , I'll wait until the flowers are gone await. It was an accident yesterday , cause ...

  3. android Program crash crash Capture of log

    android Program crash crash Capture of log Before in the project development process , It's going to crash all the time , But the brothers of the test group didn't export the log in time .... Later, I listened to others when I was in the group , Tencent has such a name bugly Things that are take ...

  4. Windbg Grab program crash dmp Method of file

    Windbg Grab program crash dmp Method of file One .        brief introduction windbg Is in windows Under the platform , Powerful user and kernel debugging tools . Compared with Visual Studio, It is a lightweight debugging tool , ...

  5. 《 Yes “XXX::Invoke” Callback for garbage collection delegate of type . This can cause the application to crash 、 Corruption and data loss . When passing a delegate to unmanaged code , Managed applications must keep these delegates active , Until you are sure they will not be called again 》 The solution of the problem

    < Yes “XXX::Invoke” Callback for garbage collection delegate of type . This can cause the application to crash . Corruption and data loss . When passing a delegate to unmanaged code , Managed applications must keep these delegates active , Until you are sure they will not be called again & ...

  6. [ios- Must see ] IOS Debugging skills : What to do when the program crashes iphone IOS

    from:http://article.ityran.com/archives/1143 There is a situation : When we are happily committed to our work app when , And everything is very smooth , But suddenly , Pit father ah , It broke down .( sad ...

  7. iOS - Capture application crash logs

    As a iOS Mobile app developers , To make sure your app is right , Before submitting the app to the store , You're bound to do a lot of testing : And in the course of your testing, the application runs well , But after the app store goes online , There are still users who complain ...

  8. iOS Development - Capture the program crash log

    iOS It's normal to encounter a program crash during development , How to catch the exception information and inform the developer when the program crashes , It's the way most software chooses . Here's how to iOS To realize : 1. Add an exception capture listener when the program starts , Used to handle program crashes ...

  9. Android How to deal with a program crash

    After the app is released , Because of the diversity of Android models , All kinds of problems may arise , If we can collect this information at this time , And it's nice to make changes . Now let's talk about how to deal with a program crash , Error messages on mobile phones . Java It has been mentioned in ...

Random recommendation

  1. canvas Summary

    I didn't have much to do in the company some time ago , I studied it canvas, It hasn't been formally applied in actual development , But I've felt its charm . Let's write about it. I think canvas The more important point in the book , If there is a mistake in understanding , Welcome to correct . First canvas yes h5 ...

  2. install pdo.so and pdo_mysql.so also pcntl.so Extended to php in

    1. Download the source code , decompression tar -xzvf php-5.4.20.tar.gz cd  /usr/local/src/php-5.4.20/ext/pdo /usr/local/php/bin/phpize ...

  3. Modules you should know in Python Libray

    Two days ago, I was asked about python lib and module What are they? ? The most commonly used ones , The others couldn't answer all of a sudden . Wanting is also , In general , There's a problem , Search the Internet , Follow the clues to the examples available , And then I didn't go deep into it . result ...

  4. c++ Review the basic points 02 Virtual function and template And static inline Coexistence

    1.       Can virtual functions be defined as template functions When a class has a virtual function , It must have a virtual table , Used to record the actual address of each virtual function . That is to say, the size of the virtual table is determined at compile time . How many virtual functions are there , How many are recorded in the virtual table .       ...

  5. 《JavaScript+DOM Programming art 》 A summary of the ( Two )---DOM A few of them

    //DOM: // Object is an independent data set , Variables associated with a particular object are called properties of the object , Functions that can be called through a particular object are called methods of that object // //4 A very practical DOM Method : // Retrieve specific elements ...

  6. L3-007. Ladder map

    L3-007. Ladder map Topic link :https://www.patest.cn/contests/gplt/L3-007 Dijstra This is the question Dijstra Deformation of , The trouble is the choice of the two shortest paths at the same distance ...

  7. Centos 6.5 install python3.6.2、pip9.0.1、ipython6.1

    explain : because Cenots 6.5 The default is installed python 2.6.6   To use multiple servers for the same host python edition , The original version cannot be affected , Because a lot of systems depend on python, such as yum    python ...

  8. git Introduction to Xiaobai

    git What is it? ( On the front , It's just a metaphor for Xiao Bai who doesn't understand , Please jump by yourself ) git It is used too frequently in normal development , I don't know how to describe it . Embarrassed . Suppose we develop work similar to book finishing , The code is written by a lot of people ...

  9. iOS Play gif Discussion on the method of dynamic graph

    Link to the original text :http://my.oschina.net/u/2340880/blog/608560 Abstract iOS There is no ready-made interface to show in gif Dynamic graph , But it can be dealt with in other ways gif The picture shows .iOS In the play g ...

  10. centos Install stand-alone redis4.0.10

    redis Source code address : http://download.redis.io/releases/ download redis-4.0.10.tar.gz   and  redis-stable.tar.gz First step : install g ...