Skip to content

Commit 8be610e

Browse files
committed
breakpad;
1 parent 9d7a691 commit 8be610e

File tree

6 files changed

+108
-3
lines changed

6 files changed

+108
-3
lines changed

.github/workflows/build.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
choco install ninja
4949
ninja --version
5050
cmake --version
51-
vcpkg install glog --triplet x64-windows
51+
vcpkg install glog breakpad --triplet x64-windows
5252
- name: Install dependencies on macos
5353
if: startsWith(matrix.os, 'macos')
5454
shell: bash
@@ -57,7 +57,7 @@ jobs:
5757
ninja --version
5858
cmake --version
5959
clang --version
60-
vcpkg install glog --triplet x64-osx
60+
vcpkg install glog breakpad --triplet x64-osx
6161
- name: Install dependencies on ubuntu
6262
if: startsWith(matrix.os, 'ubuntu')
6363
run: |
@@ -66,7 +66,7 @@ jobs:
6666
ninja --version
6767
cmake --version
6868
gcc --version
69-
vcpkg install glog --triplet x64-linux
69+
vcpkg install glog breakpad --triplet x64-linux
7070
7171
- uses: actions/checkout@v3
7272
with:

Breakpad/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_executable(Breakpad main.cc breakpad.cc breakpad.hpp)
2+
target_link_libraries(Breakpad PRIVATE unofficial::breakpad::libbreakpad
3+
unofficial::breakpad::libbreakpad_client)

Breakpad/breakpad.cc

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "breakpad.hpp"
2+
3+
#ifdef _WIN32
4+
#include <client/windows/handler/exception_handler.h>
5+
#elif __APPLE__
6+
#include <client/mac/handler/exception_handler.h>
7+
#elif __linux__
8+
#include <client/linux/handler/exception_handler.h>
9+
#endif
10+
11+
#include <codecvt>
12+
#include <iostream>
13+
#include <locale>
14+
15+
#ifdef _WIN32
16+
bool callback(const wchar_t *dump_path,
17+
const wchar_t *id,
18+
void *context,
19+
EXCEPTION_POINTERS *exinfo,
20+
MDRawAssertionInfo *assertion,
21+
bool succeeded)
22+
{
23+
auto succeeded_str = succeeded ? "succeeded" : "fialed";
24+
auto convert_str = [](const wchar_t *wstr) {
25+
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
26+
return converter.to_bytes(wstr);
27+
};
28+
auto dump_path_str = convert_str(dump_path) + convert_str(id);
29+
std::cout << "Create dump file " << succeeded_str << " Dump path: " << dump_path_str
30+
<< std::endl;
31+
return succeeded;
32+
}
33+
#elif __APPLE__
34+
bool callback(const char *dump_path, const char *id, void *context, bool succeeded)
35+
{
36+
auto succeeded_str = succeeded ? "succeeded" : "fialed";
37+
std::cout << "Create dump file " << succeeded_str << " Dump path: " << dump_path << std::endl;
38+
return succeeded;
39+
}
40+
#elif __linux__
41+
bool callback(const google_breakpad::MinidumpDescriptor &descriptor, void *context, bool succeeded)
42+
{
43+
auto succeeded_str = succeeded ? "succeeded" : "fialed";
44+
std::cout << "Create dump file " << succeeded_str << " Dump path: " << descriptor.path()
45+
<< std::endl;
46+
return succeeded;
47+
}
48+
#endif
49+
50+
Breakpad::Breakpad(const std::string &dump_path)
51+
{
52+
#ifdef _WIN32
53+
auto dump_path_w = std::wstring(dump_path.begin(), dump_path.end());
54+
handler_ptr = std::make_unique<google_breakpad::ExceptionHandler>(
55+
dump_path_w, nullptr, callback, nullptr, google_breakpad::ExceptionHandler::HANDLER_ALL);
56+
#elif __APPLE__
57+
handler_ptr = std::make_unique<google_breakpad::ExceptionHandler>(dump_path,
58+
nullptr,
59+
callback,
60+
nullptr,
61+
true,
62+
nullptr);
63+
#elif __linux__
64+
handler_ptr = std::make_unique<google_breakpad::ExceptionHandler>(
65+
google_breakpad::MinidumpDescriptor(dump_path), nullptr, callback, nullptr, true, -1);
66+
#endif
67+
}
68+
69+
Breakpad::~Breakpad() {}

Breakpad/breakpad.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <string>
5+
6+
namespace google_breakpad {
7+
class ExceptionHandler;
8+
}
9+
10+
class Breakpad
11+
{
12+
public:
13+
Breakpad(const std::string &dump_path);
14+
~Breakpad();
15+
16+
private:
17+
std::unique_ptr<google_breakpad::ExceptionHandler> handler_ptr;
18+
};

Breakpad/main.cc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "breakpad.hpp"
2+
3+
int main(int argc, char *argv[])
4+
{
5+
Breakpad breakpad("./");
6+
int *p = nullptr;
7+
*p = 1;
8+
9+
return 0;
10+
}

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,16 @@ message("CMAKE_GENERATOR: ${CMAKE_GENERATOR}")
7777
message("CMAKE_C_COMPILER_ID: ${CMAKE_C_COMPILER_ID}")
7878
message("CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")
7979

80+
find_package(unofficial-breakpad CONFIG REQUIRED)
81+
if(unofficial-breakpad_FOUND)
82+
message(STATUS "found unofficial-breakpad")
83+
endif()
8084
find_package(glog CONFIG REQUIRED)
8185
if(glog_FOUND)
8286
message(STATUS "found glog")
8387
endif()
8488

89+
add_subdirectory(Breakpad)
8590
add_subdirectory(DesignPattern)
8691
add_subdirectory(Glog)
8792
add_subdirectory(Mutex)

0 commit comments

Comments
 (0)