Skip to content

Commit

Permalink
crashpad;
Browse files Browse the repository at this point in the history
  • Loading branch information
RealChuan committed Apr 15, 2024
1 parent 185196b commit 64e9f96
Show file tree
Hide file tree
Showing 22 changed files with 258 additions and 42 deletions.
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;
}
13 changes: 10 additions & 3 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 Down Expand Up @@ -94,6 +93,11 @@ 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()

enable_testing()

Expand All @@ -103,6 +107,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 Down
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
2 changes: 1 addition & 1 deletion Curl/tcpclient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ auto TcpClient::connect() -> bool
}

m_curl = curl_easy_init();
if (!m_curl) {
if (m_curl == nullptr) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion Curl/tcpclient.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
2 changes: 1 addition & 1 deletion DesignPattern/Singleton/singleton.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef SINGLETON_HPP
#define SINGLETON_HPP

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

#include <iostream>
#include <memory>
Expand Down
2 changes: 1 addition & 1 deletion Icmp/icmp.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 <netinet/in.h>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion MonitorDir/monitordir.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

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

#include <filesystem>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion Mutex/mutex.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
2 changes: 1 addition & 1 deletion OpenSSL/openssl_rsa.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "openssl_utils.hpp"

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

#include <gtest/gtest.h>
#include <openssl/pem.h>
Expand Down
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@
5. [ByteOrder](/ByteOrder/byteorder.hpp)——判断系统的字节序;
6. [Client](/Client/client.cpp)——一个简单的Linux select socket客户端;
7. [CountDownLatch](/CountDownLatch/countdownlatch.hpp)——使用std::mutex和std::condition_variable实现的简单倒计时门闩(std::latch c++20);
8. [Curl](/Curl/)——curl的简单使用;
1. [TcpClient](/Curl/tcpclient.hpp)——使用curl实现的简单tcp客户端;
2. [HttpClient](/Curl/httpclient.hpp)——使用curl实现的简单http同步客户端;
3. [HttpClientAsync](/Curl/httpclient_async.hpp)——使用curl实现的简单http异步客户端;
9. [DesignPattern](/DesignPattern)——设计模式的一些例子;
1. [Factory](/DesignPattern/Factory/factory.hpp)——工厂模式;
2. [MVC](/DesignPattern/MVC/model.hpp)——mvc模式;
3. [Observer](/DesignPattern/Observer/observer.hpp)——观察者模式;
4. [Singleton](/DesignPattern/Singleton/singleton.hpp)——单例模式;
10. [Glog](/Glog/main.cc)——google glog的例子;
11. [Icmp](/Icmp/icmp.hpp)——linux icmp协议的简单封装;
12. [LinkedList](/LinkedList/linkedlist.hpp)——链表的相关操作,插入、移除、反转、打印;
13. [Memcpy](/Memcpy/memcpy.hpp)——`memcpy`函数实现;
14. [MonitorDir](/MonitorDir/monitordir.hpp)——windows(`ReadDirectoryChangesW`),macos(`FSEvents`)和linux(`inotify`)目录监控的简单例子;
15. [Mutex](/Mutex/mutex.hpp)——使用std::atomic_flag实现的简单互斥锁和自旋锁;
16. [OpenSSL](/OpenSSL)——openssl的一些例子;
8. [Crashpad](/Crashpad/crashpad.hpp)——crashpad的简单封装;
1. vcpkg对google crashpad在macos和linux上的支持并不好;
9. [Curl](/Curl/)——curl的简单使用;
1. [TcpClient](/Curl/tcpclient.hpp)——使用curl实现的简单tcp客户端;
2. [HttpClient](/Curl/httpclient.hpp)——使用curl实现的简单http同步客户端;
3. [HttpClientAsync](/Curl/httpclient_async.hpp)——使用curl实现的简单http异步客户端;
10. [DesignPattern](/DesignPattern)——设计模式的一些例子;
11. [Factory](/DesignPattern/Factory/factory.hpp)——工厂模式;
12. [MVC](/DesignPattern/MVC/model.hpp)——mvc模式;
13. [Observer](/DesignPattern/Observer/observer.hpp)——观察者模式;
14. [Singleton](/DesignPattern/Singleton/singleton.hpp)——单例模式;
15. [Glog](/Glog/main.cc)——google glog的例子;
16. [Icmp](/Icmp/icmp.hpp)——linux icmp协议的简单封装;
17. [LinkedList](/LinkedList/linkedlist.hpp)——链表的相关操作,插入、移除、反转、打印;
18. [Memcpy](/Memcpy/memcpy.hpp)——`memcpy`函数实现;
19. [MonitorDir](/MonitorDir/monitordir.hpp)——windows(`ReadDirectoryChangesW`),macos(`FSEvents`)和linux(`inotify`)目录监控的简单例子;
20. [Mutex](/Mutex/mutex.hpp)——使用std::atomic_flag实现的简单互斥锁和自旋锁;
21. [OpenSSL](/OpenSSL)——openssl的一些例子;
1. [aes](/OpenSSL/openssl_aes.cc)——aes加解密的例子;
2. [base64](/OpenSSL/openssl_base64.cc)——base64编解码的例子;
3. [hash](/OpenSSL/openssl_hash.cc)——sha256的例子;
Expand All @@ -41,10 +43,10 @@
7. [sm4](/OpenSSL/openssl_sm4.cc)——sm4加解密的例子;
8. [x509](/OpenSSL/openssl_x509.cc)——x509证书的例子;
9. [bash](/OpenSSL/openssl_bash.sh)——openssl命令行的例子;
17. [Server](/Server)——linux server的一些例子;
22. [Server](/Server)——linux server的一些例子;
1. [server_epoll](/Server/server_epoll.cc)——epoll的例子;
2. [server_poll](/Server/server_poll.cc)——poll的例子;
3. [server_select](/Server/server_select.cc)——select的例子;
18. [Thread](/Thread/)——基于std::thread实现的线程类,包括线程池;
23. [Thread](/Thread/)——基于std::thread实现的线程类,包括线程池;
1. [Thread](/Thread/thread.hpp)——线程类;
2. [ThreadPool](/Thread/threadpool.hpp)——线程池;
10 changes: 5 additions & 5 deletions Thread/thread.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 All @@ -16,7 +16,7 @@ class Thread : noncopyable
using Task = std::function<void()>;

Thread() = default;
Thread(Task task) { setTask(task); }
explicit Thread(Task task) { setTask(task); }
~Thread() { stop(); }

void setTask(Task task) { m_task = std::move(task); }
Expand Down Expand Up @@ -46,9 +46,9 @@ class Thread : noncopyable
m_condition.wait(lock, [this]() { return m_running.load(); });
}

bool isRunning() const { return m_running; }
auto isRunning() const -> bool { return m_running; }

std::thread::id getThreadId() const { return m_thread.get_id(); }
auto getThreadId() const -> std::thread::id { return m_thread.get_id(); }

static void yield() { std::this_thread::yield(); }

Expand All @@ -62,7 +62,7 @@ class Thread : noncopyable
std::this_thread::sleep_until(timePoint);
}

static unsigned int hardwareConcurrency()
static auto hardwareConcurrency() -> unsigned int
{
unsigned int n = std::thread::hardware_concurrency(); // 如果不支持,返回0
assert(n > 0);
Expand Down
Loading

0 comments on commit 64e9f96

Please sign in to comment.