Windows platform compiler use google breakpad

Original: https://blog.csdn.net/bingzhongdehuoyan/article/details/53860433

Brief introduction

  breakpad is a set of client and server components used to implement crash reporting system, but I can only find use in client and online official google, google official did not seem to offer the service side of things, only to crash for uploading files the method (as detailed in the source code src \ tools \ windows \ symupload folder).

  breakpad the sour https://chromium.googlesource.com/breakpad/breakpad . Need access over the wall, using git also need to download the source code over the wall, how over the wall on its own Baidu.

  This paper environment for Windows 10 and Visual Studio 2015, Win7 environment and VS2013 also apply himself is Win7 and VS2013 environment when companies. (Note: hereinafter, the command $ git numbers refer to the command prompt, the bash git without input on the command line.)

1 package breakpad generate project files

1.1 Source get breakpad

breakpad source can https://chromium.googlesource.com/breakpad/breakpad download recommended download with git, under the windows git installed directly in the git official website to download and install to obtain breakpad source with the following command:

$ git clone https://chromium.googlesource.com/breakpad/breakpad

  
  
  • 1

The reason for the wall, clone may fail on its own Baidu it over the wall, it is not available at the following link to download breakpad Source:
http://download.csdn.net/detail/bingzhongdehuoyan/9716434
breakpad source folder is the breakpad .

1.2 gyp acquisition tool

GYP (Generate Your Projects) Chromium was developed by a team of cross-platform automation project build tools, Chromium project is carried out by GYP build management.
gyp tools https://chromium.googlesource.com/external/gyp/ available, the proposed acquisition by git:

$ git clone https://chromium.googlesource.com/external/gyp

  
  
  • 1

Also we need over the wall, not over the wall can be downloaded at the following link:
http://download.csdn.net/detail/bingzhongdehuoyan/9720517
the acquired gyp folder to breakpad \ src \ tools \ folder.

1.3 install python2.7

When packaged with a gyp need support breakpad python, python may be the official website download and install directly and added to the path, specifically on their own Baidu, but can not be installed python3.x, otherwise the following error:

can only be installed python2.7.x, installed the latest 2.7.13.

1.4 acquire googletest

Must also obtain googletest, otherwise it will appear the following warning:

GoogleTest available on GitHub ( https://github.com/google/googletest ), as follows:

$ git clone https://github.com/google/googletest.git

  
  
  • 1

Will get down to the googletest folder will have two folders, googlemock and googletest, as shown below:

will be one of googlemock folder to the \ src \ directory breakpad, and rename testing; then acquired googletest file googletest folder under the folder to breakpad \ src \ testing \ folder, and rename gtest.

1.5 Construction of the project file with gyp

In the open breakpad \ src folder command window, there are two methods: one is open run win + R, cmd open a command line input, and then enter breakpad \ src catalogs cd command; the second is at breakpad \ src folder shift + right mouse button, click the open command window here. Then the command window enter the following command:

tools\gyp\gyp.bat --no-circular-check client\windows\breakpad_client.gyp

  
  
  • 1


After the success of the project has generated breakpad_client.sln file in breakpad \ src \ client \ windows \ directory.

2 comes sample program using breakpad

breakpad has built-in sample program can be used to feel the crash report how breakpad crawl, the following briefly use this example program.
Open with Visual Studio breakpad \ src \ breakpad_client.sln works under client \ windows \ directory, open the Solution Explorer Figure:

Right build_all project, click Generate, VS2015 will appear the following errors:

what are the warning be considered as an error, the solution is: right project name (such as crash_generation_client), open the properties, expand the C / C ++, click General, to the right of the "warnings as errors" option is selected No, as:

turn operations other build_all project outside each property has a C project / C ++ tab, and then right-build_all project, click Rebuild, or fail, Win10 and VS2015 things really, I use Win7 and when VS2013 did so many things, the following error message:

Double-click the error after locating the error line:

rough row error deleted it:

once again right build_all project, click Rebuild, and finally succeeded, not easy:

after the success will breakpad \ src \ client \ windows \ Debug \ folder there crash_generation_app.exe program, double click to open, click Client-> Deref Zero, the program will collapse, the collapse of the latter to the root directory of C look Whether there Dumps folder that has a name similar to the crash dump file a9414977-693d-4013-89c1-9c7c4ef81689.dmp, and open the file with VS:

you can see the error message, click Debugging only use this machine, you can For more details, see the error and error which occurred in the specific line of code:

Careful study of the program code, you can probably understand how to use breakpad grab a C ++ program crash reports.

3 write a small program to try water

下面就写个简单的小程序试试用breakpad抓取崩溃报告。
用VS 2015新建一个Visual C++控制台程序,本文程序起名为wincrash。
然后在VS的wincrash解决方案中添加common、crash_generation_client、crash_generation_server、exception_handler等四个项目,右键解决方案->添加->现有项目,选中breakpad\src\client\windows\目录中和crash_generation、handler子目录中扩展名为.vcxproj的文件即可:


然后在wincrash项目中添加包含目录breakpad\src目录的全路径,本人的路径是D:\breakpad\src,添加方法是:右键wincrash->属性->VC++目录->包含目录->编辑:


之后分别右键common、crash_generation_client、crash_generation_server、exception_handler等四个项目点击生成,以编译出lib库文件:

在wincrash项目中包含生成的四个.lib库文件:右键wincrash->属性->链接器->输入->附加依赖项->编辑:

在附加依赖项编辑框中输入:

..\Debug\lib\common.lib
..\Debug\lib\crash_generation_client.lib
..\Debug\lib\crash_generation_server.lib
..\Debug\lib\exception_handler.lib

  
  
  • 1
  • 2
  • 3
  • 4


wincrash主程序代码如下:

#include <cstdio>
#include "client/windows/handler/exception_handler.h"

bool callback(const wchar_t *dump_path, const wchar_t *id,
    void *context, EXCEPTION_POINTERS *exinfo,
    MDRawAssertionInfo *assertion,
    bool succeeded)
{
    if (succeeded) {
        printf("dump guid is %ws\n", id);
    }
    else {
        printf("dump failed\n");
    }
    system("pause");
    return succeeded;
}

int mydiv(int x, int y)
{
    int z;
    z = x / y;
    return z;
}

int main()
{
    google_breakpad::ExceptionHandler eh(
        L".", NULL, callback, NULL,
        google_breakpad::ExceptionHandler::HANDLER_ALL);
    printf("9/3=%d\n", mydiv(9, 3));
    printf("9/0=%d\n", mydiv(9, 0));  //程序将在此崩溃
    printf("8/2=%d\n", mydiv(8, 2));
    system("pause");
    return 0;
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

编译程序,可能会有如下错误:

原因是common、crash_generation_client、crash_generation_server、exception_handler等四个项目的代码生成运行库为“多线程调试 (/MTd)”,跟wincrash的不一样,将wincrash的改为“多线程调试 (/MTd)”即可:右键wincrash->属性->C/C++->代码生成->运行库->多线程调试 (/MTd),如图:

再次编译程序,这次应该能在Debug目录下成功生成wincrash.exe程序,双击打开运行:

成功抓取崩溃文件!!
在wincrash.exe程序所在目录下可看到生成的.dmp扩展名的崩溃转储文件(名称类似1a4ecf76-1df3-46de-be69-88df37bb1b11.dmp),用VS打开该文件:

可以看到错误信息,点击使用 仅限本机 进行调试,可以看到错误详情及错误具体发生在哪行代码:

准确定位26行和36行!!
下面简单解析一下这个小程序,使用breakpad抓取C++程序的崩溃报告,需要包含exception_handler.h头文件,并在程序所有错误之前创建一个google_breakpad::ExceptionHandler对象,建议在main()函数开头创建该对象,当程序出错时,该对象会捕捉到错误,输出dump文件,还可通过回调函数作一些必要的处理操作。
本程序所用google_breakpad::ExceptionHandler的其中一个构造函数定义如下(见exception_handler.h文件):

  // Creates a new ExceptionHandler instance to handle writing minidumps.
  // Before writing a minidump, the optional filter callback will be called.
  // Its return value determines whether or not Breakpad should write a
  // minidump.  Minidump files will be written to dump_path, and the optional
  // callback is called after writing the dump file, as described above.
  // handler_types specifies the types of handlers that should be installed.
  ExceptionHandler(const wstring& dump_path,
                   FilterCallback filter,
                   MinidumpCallback callback,
                   void* callback_context,
                   int handler_types);
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • const wstring& dump_path:宽字符串类型的dump_path用于定义.dmp扩展名的崩溃转储文件生成路径。
  • FilterCallback filter:程序崩溃时用于过滤的回调函数,通过返回ture/false来继续/停止 异常处理,其定义如下:
  // A callback function to run before Breakpad performs any substantial
  // processing of an exception.  A FilterCallback is called before writing
  // a minidump.  context is the parameter supplied by the user as
  // callback_context when the handler was created.  exinfo points to the
  // exception record, if any; assertion points to assertion information,
  // if any.
  //
  // If a FilterCallback returns true, Breakpad will continue processing,
  // attempting to write a minidump.  If a FilterCallback returns false,
  // Breakpad will immediately report the exception as unhandled without
  // writing a minidump, allowing another handler the opportunity to handle it.
  typedef bool (*FilterCallback)(void* context, EXCEPTION_POINTERS* exinfo,
                                 MDRawAssertionInfo* assertion);
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • MinidumpCallback callback:输出dump文件后的回调函数,可做一些必要的处理操作,不过不建议太复杂,毕竟程序崩溃后堆栈已被破坏,其定义如下:
  // A callback function to run after the minidump has been written.
  // minidump_id is a unique id for the dump, so the minidump
  // file is <dump_path>\<minidump_id>.dmp.  context is the parameter supplied
  // by the user as callback_context when the handler was created.  exinfo
  // points to the exception record, or NULL if no exception occurred.
  // succeeded indicates whether a minidump file was successfully written.
  // assertion points to information about an assertion if the handler was
  // invoked by an assertion.
  //
  // If an exception occurred and the callback returns true, Breakpad will treat
  // the exception as fully-handled, suppressing any other handlers from being
  // notified of the exception.  If the callback returns false, Breakpad will
  // treat the exception as unhandled, and allow another handler to handle it.
  // If there are no other handlers, Breakpad will report the exception to the
  // system as unhandled, allowing a debugger or native crash dialog the
  // opportunity to handle the exception.  Most callback implementations
  // should normally return the value of |succeeded|, or when they wish to
  // not report an exception of handled, false.  Callbacks will rarely want to
  // return true directly (unless |succeeded| is true).
  //
  // For out-of-process dump generation, dump path and minidump ID will always
  // be NULL. In case of out-of-process dump generation, the dump path and
  // minidump id are controlled by the server process and are not communicated
  // back to the crashing process.
  typedef bool (*MinidumpCallback)(const wchar_t* dump_path,
                                   const wchar_t* minidump_id,
                                   void* context,
                                   EXCEPTION_POINTERS* exinfo,
                                   MDRawAssertionInfo* assertion,
                                   bool succeeded);
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • void* callback_context:设备上下文,回调使用的。
  • int handler_types:HandlerType异常类型,可在exception_handler.h查看。

google_breakpad::ExceptionHandler的详细使用方法就自己参考官方文档或源码中的注释。

Guess you like

Origin blog.csdn.net/a844651990/article/details/85316627