文章目錄
- 概述
- 翻牆通路
- 源碼下載下傳
-
- breakpad
-
- 使用depot_tools下載下傳
- git下載下傳
- github下載下傳
- gyp(Generate Your Projects)
- python 2.7.x
- google test
- 編譯
-
- 源碼目錄準備
- 生成sln檔案
- 編譯
- 示例
概述
breakpad是google開源的一套用于程序crash的處理方案,跨平台。應該是早期版本crash report的更新版本,以前的crashrpt僅支援win平台。
不過好像由于現在有沙盒技術,會導緻breakpad無法生成dump,是以google又開發了一個crashpad項目,用來替代breakpad。
翻牆通路
從http://www.goubanjia.com/找了個代理通路的。使用git下載下傳,也需要設定代理
git config --global http.proxy 203.189.147.33:46631
源碼下載下傳
breakpad
使用depot_tools下載下傳
google好多源碼都是使用depot_tools下載下傳,其可以将第三方依賴檔案也下載下傳下來。不過通路googlesource需要翻牆,如果不能翻牆,本工具就不用下載下傳了。
下載下傳位址:
git:
https://chromium.googlesource.com/chromium/tools/depot_tools.git
git clone https://chromium.googlesource.com/chromium/tools/depot_tools
壓縮包:
https://storage.googleapis.com/chrome-infra/depot_tools.zip
下載下傳解壓後,擷取breakpad源碼:
fetch breakpad
git下載下傳
同樣的需要翻牆,如果不能通路可以使用github上的分支版本。
git下載下傳位址:
https://chromium.googlesource.com/breakpad/breakpad
$ git clone https://chromium.googlesource.com/breakpad/breakpad
github下載下傳
github下載下傳位址:
https://github.com/google/breakpad
gyp(Generate Your Projects)
gyp是chromium開發的跨平台自動化項目建構工具,gyp工具下載下傳位址:
https://chromium.googlesource.com/external/gyp/
$ git clone https://chromium.googlesource.com/external/gyp
不能翻牆就随便找個下載下傳吧,附一個github上的位址:
https://github.com/bnoordhuis/gyp
python 2.7.x
使用gyp時需要python的支援,好像隻能支援python2.7.x,不支援python 3.x。
自行官網下載下傳python。
google test
下載下傳位址:
https://github.com/abseil/googletest
編譯
源碼目錄準備
在
breakpad\src
下建立
testing
目錄,将gtest解壓後的googlemock和googletest拷貝到建立的
testing
下。
生成sln檔案
使用gyp生成sln檔案。
tools\gyp\gyp.bat --no-circular-check client\windows\breakpad_client.gyp
成功後在breakpad\src\client\windows\目錄下有生成的breakpad_client.sln工程檔案。
編譯
vs打開sln檔案,breakpad使用了Unicode編碼,如果更改字元集會編譯失敗。
示例
win下如果使用程序内dump,需要連結下面幾個庫
common.lib
crash_generation_client.lib
exception_handler.lib
code1:
#include "client/windows/handler/exception_handler.h"
int main()
{
google_breakpad::ExceptionHandler *pCrashHandel =
new google_breakpad::ExceptionHandler(
L".",
NULL,
NULL,
NULL,
google_breakpad::ExceptionHandler::HANDLER_ALL,
NULL);
char *pBuf = NULL;
*pBuf = 'a';
return 0;
}
code2:
#include "client/windows/handler/exception_handler.h"
// Minidump with stacks, PEB, TEB, and unloaded module list.
const MINIDUMP_TYPE kSmallDumpType = static_cast<MINIDUMP_TYPE>(
MiniDumpWithProcessThreadData | // Get PEB and TEB.
MiniDumpWithUnloadedModules); // Get unloaded modules when available.
// Minidump with all of the above, plus memory referenced from stack.
const MINIDUMP_TYPE kLargerDumpType = static_cast<MINIDUMP_TYPE>(
MiniDumpWithProcessThreadData | // Get PEB and TEB.
MiniDumpWithUnloadedModules | // Get unloaded modules when available.
MiniDumpWithIndirectlyReferencedMemory); // Get memory referenced by stack.
// Large dump with all process memory.
const MINIDUMP_TYPE kFullDumpType = static_cast<MINIDUMP_TYPE>(
MiniDumpWithFullMemory | // Full memory from process.
MiniDumpWithProcessThreadData | // Get PEB and TEB.
MiniDumpWithHandleData | // Get all handle information.
MiniDumpWithUnloadedModules); // Get unloaded modules when available.
static google_breakpad:Exception *sg_pCrash = NULL;
static std::string sg_strDumpPath = "";
//此方法可以不實作,本示例隻是為了實作:出現異常時才産生dump檔案夾,沒有異常時不建立檔案夾
bool FilterCallback(void *context,
EXCEPTION_POINTERS *exinfo,
MDRawAssertionInfo *assertion)
{
if(CFileUtil::createMultiDir(sg_strDumpPath))
{
return false;
}
return true;
}
bool installBreakpad()
{
enum {BUF_LEN = 1024};
char szBuf[BUF_LEN];
memset(szBuf,0,sizeof(szBuf));
GetModuleFileNameA(NULL,szBuf,BUF_LEN);
boost::filesystem::path objPath(szBuf);
std::string strFileName = objPath.stem().string();
objPath.remove_filename();
objPath /= "../../minidump";
objPath /= strFileName;
sg_strDumpPath = objPath.string();
std::string strCodePage = boost::locale::util::get_system_locale();
std::locale loc = boost::locale::generator().generate(strCodePage);
std::wstring wstrPath = boost::locale::conv::to_utf<wchar_t>(sg_strDumpPath,loc);
sg_pCrash = new google_breakpad::ExceptionHandler(wstrPath,
FilterCallback,
NULL,
NULL,
google_breakpad::ExceptionHandler::HANDLER_ALL,
kFullDumpType,
(HANDLE)NULL,
NULL);
return (sg_pCrash != NULL);
}
bool uninstallBreakpad()
{
if(sg_pCrash != NULL)
{
delete sg_pCrash;
sg_pCrash = NULL;
}
return true;
}
int main()
{
installBreakpad();
char *pBuf = NULL;
*pBuf = 'a';
uninstallBreakpad();
return 0;
}