Skip to content

Commit

Permalink
crashpad;
Browse files Browse the repository at this point in the history
  • Loading branch information
RealChuan committed Apr 22, 2024
1 parent 185196b commit e31b5d3
Show file tree
Hide file tree
Showing 26 changed files with 227 additions and 80 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ jobs:
if: startsWith(matrix.os, 'macos')
shell: bash
run: |
brew install ninja
brew install ninja python-setuptools
ninja --version
cmake --version
clang --version
- name: Install dependencies on ubuntu
if: startsWith(matrix.os, 'ubuntu')
run: |
sudo apt-get update
sudo apt-get install ninja-build
sudo apt-get install ninja-build clang libcurl4-openssl-dev
ninja --version
cmake --version
gcc --version
Expand Down Expand Up @@ -99,7 +99,7 @@ jobs:
-DCMAKE_CXX_COMPILER=cl \
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
-G "${{ matrix.generators }}" \
-DCMAKE_INSTALL_PREFIX:PATH=instdir
|| (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1)
- name: Configure macos or ubuntu
if: startsWith(matrix.os, 'macos') || startsWith(matrix.os, 'ubuntu')
shell: bash
Expand All @@ -109,7 +109,7 @@ jobs:
-B ./build \
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
-G "${{ matrix.generators }}" \
-DCMAKE_INSTALL_PREFIX:PATH=instdir
|| (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1)
- name: Build
shell: bash
run: |
Expand Down
3 changes: 3 additions & 0 deletions AtomicQueue/atomicqueue_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ TEST(AtomicQueue, PushPopManyThreads)
{
AtomicQueue<int> queue;
std::vector<std::thread> threads;
threads.reserve(100);
for (int i = 0; i < 100; ++i) {
threads.emplace_back([&queue, i] { queue.push(i); });
}
Expand All @@ -62,6 +63,7 @@ TEST(AtomicQueue, PushPopManyThreads2)
{
AtomicQueue<int> queue;
std::vector<std::thread> threads;
threads.reserve(100);
for (int i = 0; i < 100; ++i) {
threads.emplace_back([&queue, i] {
queue.push(i);
Expand All @@ -79,6 +81,7 @@ TEST(AtomicQueue, PushPopManyThreads3)
{
AtomicQueue<int> queue;
std::vector<std::thread> threads;
threads.reserve(100);
for (int i = 0; i < 100; ++i) {
threads.emplace_back([&queue, i] {
queue.push(i);
Expand Down
6 changes: 4 additions & 2 deletions Breakpad/breakpad.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#pragma once

#include <utils/object.hpp>

#include <memory>
#include <string>

namespace google_breakpad {
class ExceptionHandler;
}

class Breakpad
class Breakpad : noncopyable
{
public:
Breakpad(const std::string &dump_path);
explicit Breakpad(const std::string &dump_path);
~Breakpad();

private:
Expand Down
10 changes: 8 additions & 2 deletions Breakpad/main.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#include "breakpad.hpp"

auto main(int argc, char *argv[]) -> int
void crash()
{
Breakpad breakpad("./");
int *p = nullptr;
*p = 1;
}

auto main(int argc, char *argv[]) -> int
{
Breakpad breakpad("./");

crash();

return 0;
}
78 changes: 45 additions & 33 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# 设定版本号
cmake_minimum_required(VERSION 3.18)
cmake_minimum_required(VERSION 3.25.1)

if(CMAKE_HOST_WIN32)
set(CMAKE_TOOLCHAIN_FILE
Expand All @@ -9,7 +8,7 @@ elseif(CMAKE_HOST_APPLE)
set(CMAKE_TOOLCHAIN_FILE
"/usr/local/share/vcpkg/scripts/buildsystems/vcpkg.cmake"
CACHE STRING "Vcpkg toolchain file")
elseif(CMAKE_HOST_UNIX)
elseif(CMAKE_HOST_LINUX)
set(CMAKE_TOOLCHAIN_FILE
"/usr/local/share/vcpkg/scripts/buildsystems/vcpkg.cmake"
CACHE STRING "Vcpkg toolchain file")
Expand All @@ -18,19 +17,16 @@ endif()
# 设定工程名
project(
Cpp-Examples
VERSION 0.1
VERSION 0.0.1
DESCRIPTION "This is Cpp-Examples"
HOMEPAGE_URL "https://github.com/RealChuan/Cpp-Examples"
LANGUAGES C CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CURRENT_SOURCE_DIR ON)

# set(CMAKE_CXX_FLAGS_DEBUG "-O0") set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_DEBUG_POSTFIX d)

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
Expand All @@ -44,32 +40,20 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
endif()

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(CURRENT_PLATFORM "-64")
set(BITS "64")
else()
set(CURRENT_PLATFORM "-32")
set(BITS "32")
endif()

message(STATUS "Current Platform is ${CURRENT_PLATFORM}")
message(STATUS "Current Platform is ${BITS} bits.")

# 设定可执行二进制文件的目录
set(EXECUTABLE_OUTPUT_PATH
${PROJECT_SOURCE_DIR}/bin${CURRENT_PLATFORM}/${CMAKE_BUILD_TYPE}) # 源文件目录
# 设定存放编译出来的库文件的目录
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin${CURRENT_PLATFORM}/libs)
# 并且把该目录设为连接目录
${PROJECT_SOURCE_DIR}/bin-${BITS}/${CMAKE_BUILD_TYPE})
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin-${BITS}/libs)
link_directories(${LIBRARY_OUTPUT_PATH})

include_directories(${PROJECT_SOURCE_DIR})

message("CMAKE_MAJOR_VERSION: ${CMAKE_MAJOR_VERSION}")
message("CMAKE_MINOR_VERSION: ${CMAKE_MINOR_VERSION}")
message("CMAKE_PATCH_VERSION: ${CMAKE_PATCH_VERSION}")
message("CMAKE_TWEAK_VERSION: ${CMAKE_TWEAK_VERSION}")
message("CMAKE_VERSION: ${CMAKE_VERSION}")
message("CMAKE_GENERATOR: ${CMAKE_GENERATOR}")
message("CMAKE_C_COMPILER_ID: ${CMAKE_C_COMPILER_ID}")
message("CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")

find_package(unofficial-breakpad CONFIG REQUIRED)
if(unofficial-breakpad_FOUND)
message(STATUS "found unofficial-breakpad")
Expand All @@ -94,7 +78,13 @@ find_package(CURL CONFIG REQUIRED)
if(CURL_FOUND)
message(STATUS "found CURL")
endif()
find_package(crashpad)
# vcpkg install crashpad, it is not good for macos and linux
if(crashpad_FOUND)
message(STATUS "found crashpad")
endif()

include(CTest)
enable_testing()

add_subdirectory(Algorithm)
Expand All @@ -103,6 +93,9 @@ add_subdirectory(BinaryTree)
add_subdirectory(Breakpad)
add_subdirectory(ByteOrder)
add_subdirectory(CountDownLatch)
if(crashpad_FOUND)
add_subdirectory(Crashpad)
endif()
add_subdirectory(Curl)
add_subdirectory(DesignPattern)
add_subdirectory(Glog)
Expand All @@ -113,12 +106,31 @@ add_subdirectory(Mutex)
add_subdirectory(OpenSSL)
add_subdirectory(Thread)

if(CMAKE_HOST_WIN32)

elseif(CMAKE_HOST_APPLE)

elseif(CMAKE_HOST_UNIX)
if(CMAKE_HOST_LINUX)
add_subdirectory(Client)
add_subdirectory(Icmp)
add_subdirectory(Server)
endif()

# 输出 CMake 版本和构建系统类型
message("CMake Version: ${CMAKE_VERSION}")
message("Generator: ${CMAKE_GENERATOR}")

# 输出编译器信息
message("C Compiler ID: ${CMAKE_C_COMPILER_ID}")
message("C++ Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
message("C++ Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}")

# 输出构建类型和编译选项
message("Build Type: ${CMAKE_BUILD_TYPE}")
message("C++ Compiler Flags: ${CMAKE_CXX_FLAGS}")

# 输出链接选项
message("Executable Linker Flags: ${CMAKE_EXE_LINKER_FLAGS}")

# 输出构建和源代码目录
message("Build Directory: ${CMAKE_BINARY_DIR}")
message("Source Directory: ${CMAKE_SOURCE_DIR}")

# 输出目标架构
message("Target Processor: ${CMAKE_SYSTEM_PROCESSOR}")
2 changes: 1 addition & 1 deletion CountDownLatch/countdownlatch.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <object.hpp>
#include <utils/object.hpp>

#include <atomic>
#include <cassert>
Expand Down
13 changes: 13 additions & 0 deletions Crashpad/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
add_executable(Crashpad crashpad.cc crashpad.hpp main.cc)
target_link_libraries(Crashpad PRIVATE crashpad::crashpad)

string(REPLACE "share" "tools" crash_handler_path ${crashpad_DIR})
message(STATUS "crashpad tools directory: ${crash_handler_path}")

add_custom_command(
TARGET Crashpad
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
"$<TARGET_FILE_DIR:Crashpad>/crashpad/"
COMMAND ${CMAKE_COMMAND} -E copy_directory ${crash_handler_path}/
"$<TARGET_FILE_DIR:Crashpad>/crashpad")
59 changes: 59 additions & 0 deletions Crashpad/crashpad.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "crashpad.hpp"

#include <crashpad/client/crash_report_database.h>
#include <crashpad/client/crashpad_client.h>
#include <crashpad/client/settings.h>

#ifdef _WIN32
auto convertStringToWideString(const std::string &str) -> std::wstring
{
if (str.empty()) {
return {};
}

// 首先,获取转换后的字符串长度(不包括空终止符)
int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);

// 如果转换失败,返回空字符串
if (len == 0) {
return {};
}

// 分配足够的空间来存储转换后的字符串
std::wstring wstr(len, 0);

// 执行转换
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], len);

// 去除末尾的空字符
wstr.resize(len - 1);
return wstr;
}
#endif

Crashpad::Crashpad(const std::string &dumpPath,
const std::string &libexecPath,
const std::string &reportUrl,
bool crashReportingEnabled)
{
auto handlerPath = libexecPath + "/crashpad_handler";
#ifdef _WIN32
handlerPath += ".exe";
base::FilePath database(convertStringToWideString(dumpPath));
base::FilePath handler(convertStringToWideString(handlerPath));
#else
base::FilePath database(dumpPath);
base::FilePath handler(handlerPath);
#endif

auto dbPtr = crashpad::CrashReportDatabase::Initialize(database);
if (dbPtr && (dbPtr->GetSettings() != nullptr)) {
dbPtr->GetSettings()->SetUploadsEnabled(crashReportingEnabled);
}

m_crashpadClientPtr = std::make_unique<crashpad::CrashpadClient>();
m_crashpadClientPtr
->StartHandler(handler, database, database, reportUrl, {}, {"--no-rate-limit"}, true, true);
}

Crashpad::~Crashpad() = default;
23 changes: 23 additions & 0 deletions Crashpad/crashpad.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <utils/object.hpp>

#include <memory>
#include <string>

namespace crashpad {
class CrashpadClient;
} // namespace crashpad

class Crashpad : noncopyable
{
public:
explicit Crashpad(const std::string &dumpPath,
const std::string &libexecPath,
const std::string &reportUrl,
bool crashReportingEnabled);
~Crashpad();

private:
std::unique_ptr<crashpad::CrashpadClient> m_crashpadClientPtr;
};
23 changes: 23 additions & 0 deletions Crashpad/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "crashpad.hpp"

#include <filesystem>

void crash()
{
int *p = nullptr;
*p = 1;
}

auto main() -> int
{
auto dumpPath = std::filesystem::current_path() / "crashpad";
if (!std::filesystem::exists(dumpPath)) {
std::filesystem::create_directory(dumpPath);
}

Crashpad crashpad(dumpPath.string(), dumpPath.string(), "http://127.0.0.1:8080", true);

crash();

return 0;
}
2 changes: 1 addition & 1 deletion Curl/httpclient.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <object.hpp>
#include <utils/object.hpp>

#include <curl/curl.h>

Expand Down
4 changes: 2 additions & 2 deletions Curl/httpclient_async.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <object.hpp>
#include <utils/object.hpp>

#include <curl/curl.h>
#include <curl/easy.h>
Expand Down Expand Up @@ -136,7 +136,7 @@ class HttpClientAsync : noncopyable
{
CURLMsg *message = nullptr;
int pending = 0;
while ((message = curl_multi_info_read(m_multi, &pending))) {
while ((message = curl_multi_info_read(m_multi, &pending)) != nullptr) {
switch (message->msg) {
case CURLMSG_DONE: {
CURL *handle = message->easy_handle;
Expand Down
Loading

0 comments on commit e31b5d3

Please sign in to comment.