crash: add crash stack print.

This commit is contained in:
2025-06-20 10:33:03 +08:00
parent 32b206c2f5
commit ce3d3a1866
15 changed files with 5048 additions and 8 deletions

View File

@@ -30,6 +30,7 @@ include_directories(${PROJECT_BINARY_DIR})
add_definitions(-DFMT_HEADER_ONLY)
include_directories(3rd)
add_subdirectory(crashelper)
add_subdirectory(Protocol)
add_subdirectory(Server)
add_subdirectory(ClientCore)

View File

@@ -100,7 +100,10 @@ void ClientCore::UseFrame(QSharedPointer<FrameBuffer> frame)
break;
}
case FrameBufferType::FBT_SER_MSG_FORWARD_FAILED: {
qCritical() << QString(tr("************************** forward failed."));
qCritical() << QString(tr("*** forward failed. fid:%1, tid:%2, type:%3"))
.arg(frame->fid)
.arg(frame->tid)
.arg(static_cast<uint32_t>(frame->type));
break;
}
case FrameBufferType::FBT_CLI_REQ_SEND: {

View File

@@ -13,5 +13,5 @@ find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Network)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Network)
add_executable(frelayConsole Console.h Console.cpp main.cpp ../Res/ico.rc)
target_link_libraries(frelayConsole PRIVATE Protocol Util)
target_link_libraries(frelayConsole PRIVATE Protocol Util crashelper)
target_link_libraries(frelayConsole PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Network)

View File

@@ -1,10 +1,18 @@
#include <QCoreApplication>
#include <Util.h>
#include <crashelper.h>
#include "Console.h"
#include <Util.h>
int main(int argc, char* argv[])
{
auto configDir = Util::GetCurConfigPath("frelay");
#ifdef _WIN32
backward::SetDumpFileSavePath(configDir + "/dumpfile");
backward::SetDumpLogSavePath(configDir + "/dumplog");
#else
backward::SetDumpLogSavePath(configDir + QDir::separator() + "dumplog");
#endif
QCoreApplication app(argc, argv);
Util::InitLogger("frelayConsole.log", "frelayConsole");

View File

@@ -56,6 +56,7 @@ target_link_libraries(frelayGUI PRIVATE
ClientCore Protocol
Util
Struct
crashelper
)
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_SYSTEM_NAME MATCHES "Windows")
target_link_libraries(frelayGUI PRIVATE ws2_32 wsock32)

View File

@@ -1,10 +1,24 @@
#include <QApplication>
#include <QDir>
#include <QFile>
#include "frelayGUI.h"
#ifndef ZZXXCC
#include <crashelper.h>
#endif
int main(int argc, char* argv[])
{
auto configDir = Util::GetCurConfigPath("frelay");
#ifdef _WIN32
backward::SetDumpFileSavePath(configDir + "/dumpfile");
backward::SetDumpLogSavePath(configDir + "/dumplog");
#else
backward::SetDumpLogSavePath(configDir + QDir::separator() + "dumplog");
#endif
CRASHELPER_MARK_ENTRY();
QApplication a(argc, argv);
qInstallMessageHandler(frelayGUI::ControlMsgHander);

View File

@@ -13,5 +13,5 @@ find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Network)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Network)
add_executable(frelayServer Server.h Server.cpp main.cpp ../Res/server.rc)
target_link_libraries(frelayServer PRIVATE Protocol Util)
target_link_libraries(frelayServer PRIVATE Protocol Util crashelper)
target_link_libraries(frelayServer PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Network)

View File

@@ -1,4 +1,4 @@
// Server.h
// Server.h
#ifndef SERVER_H
#define SERVER_H
@@ -7,6 +7,7 @@
#include <QTcpServer>
#include <QTcpSocket>
#include <QTimer>
#include <crashelper.h>
#include "Protocol.h"

View File

@@ -1,10 +1,18 @@
#include <QCoreApplication>
#include <QCoreApplication>
#include <Util.h>
#include "Server.h"
#include <Util.h>
int main(int argc, char* argv[])
{
auto configDir = Util::GetCurConfigPath("frelay");
#ifdef _WIN32
backward::SetDumpFileSavePath(configDir + "/dumpfile");
backward::SetDumpLogSavePath(configDir + "/dumplog");
#else
backward::SetDumpLogSavePath(configDir + QDir::separator() + "dumplog");
#endif
QCoreApplication app(argc, argv);
Util::InitLogger("frelayServer.log", "frelayServer");

View File

@@ -33,6 +33,33 @@ QString Util::GetUserHome()
return homePath;
}
QString Util::GetCurConfigPath(const QString& sub)
{
// Get user's home directory
QString homePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
if (homePath.isEmpty()) {
qWarning() << "Failed to get user home directory";
return QString();
}
auto configPath = QDir(homePath).absoluteFilePath(".config");
// Append subdirectory if provided
if (!sub.isEmpty()) {
configPath = QDir(configPath).absoluteFilePath(sub);
}
// Create the directory if it doesn't exist
QDir dir(configPath);
if (!dir.exists()) {
if (!dir.mkpath(".")) {
qWarning() << "Failed to create config directory:" << configPath;
return QString();
}
}
return QDir::cleanPath(configPath);
}
QString Util::Join(const QString& path, const QString& name)
{
return QDir::cleanPath(path + QDir::separator() + name);
@@ -111,5 +138,4 @@ QString DirFileHelper::GetErr() const
DirFileHelper::DirFileHelper(QObject* parent) : QObject(parent)
{
}

View File

@@ -15,6 +15,7 @@ public:
static void InitLogger(const QString& logPath, const QString& mark);
static QString Get2FilePath(const QString& file, const QString& directory);
static QString Join(const QString& path, const QString& name);
static QString GetCurConfigPath(const QString& sub);
static void ConsoleMsgHander(QtMsgType type, const QMessageLogContext& context, const QString& msg);
};

35
crashelper/CMakeLists.txt Normal file
View File

@@ -0,0 +1,35 @@
cmake_minimum_required(VERSION 3.16)
project(crashelper LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_SYSTEM_NAME MATCHES "Windows")
message(FATAL_ERROR "Unsupported MinGW Currently.")
endif()
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
set(CRASHELPER_SOURCES
src/crashelper.cxx
)
include_directories(include)
add_library(crashelper STATIC ${CRASHELPER_SOURCES})
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(crashelper PUBLIC pthread dl bfd Qt${QT_VERSION_MAJOR}::Core)
target_compile_options(crashelper PUBLIC -g)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
target_link_libraries(crashelper PRIVATE DbgHelp Qt${QT_VERSION_MAJOR}::Core)
target_compile_options(crashelper PUBLIC $<$<OR:$<STREQUAL:$<CXX_COMPILER_ID>,MSVC>,$<STREQUAL:$<C_COMPILER_ID>,MSVC>>:/Zi>)
target_link_options(crashelper PUBLIC $<$<OR:$<STREQUAL:$<CXX_COMPILER_ID>,MSVC>,$<STREQUAL:$<C_COMPILER_ID>,MSVC>>:/DEBUG>)
else()
message(FATAL_ERROR "Unsupported OS: ${CMAKE_SYSTEM_NAME}. This project only supports Linux, macOS, and Windows.")
endif()
#message(STATUS "crashelper:${CMAKE_CURRENT_LIST_DIR}")
target_include_directories(crashelper PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,54 @@
#ifndef CRASHELPER_H
#define CRASHELPER_H
#if defined(_WIN32) || defined(_WIN64)
#define WIN_OS
#include <windows.h>
#if defined(min)
#undef min
#endif
#elif defined(__APPLE__) && defined(__MACH__)
#define MAC_OS
#elif defined(__linux__)
#define LINUX_OS
#define BACKWARD_HAS_BFD 1
#else
#error "Unsupported OS"
#endif
#include "backward.hpp"
#include <QString>
namespace backward {
///
/// @brief 设置dump文件的保存目录,不设置默认当前目录。
/// @param path 若不存在则创建,创建失败返回false
///
bool SetDumpFileSavePath(const QString& path);
///
/// @brief 设置dump日志文件的保存目录,不设置默认当前目录。
/// @param path 若不存在则创建,创建失败返回false
///
bool SetDumpLogSavePath(const QString& path);
std::string GetCurFullLogPath();
#if defined(WIN_OS)
void UseExceptionHandler(EXCEPTION_POINTERS* exception);
#endif
} // namespace backward
#ifdef WIN_OS
#define CRASHELPER_MARK_ENTRY() \
backward::SignalHandling sh; \
sh.register_crash_use_handler([](EXCEPTION_POINTERS* exception) { backward::UseExceptionHandler(exception); }); \
sh.register_crash_path([]() -> std::string { return backward::GetCurFullLogPath(); })
#elif defined(MAC_OS)
#elif defined(LINUX_OS)
#define CRASHELPER_MARK_ENTRY() \
backward::SignalHandling sh; \
sh.register_crash_path([]() -> QString { return backward::GetCurFullLogPath(); })
#endif
#endif // CRASHELPER_H

View File

@@ -0,0 +1,150 @@
#include "crashelper.h"
#include <QCoreApplication>
#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
namespace backward {
class crashHelper
{
public:
static crashHelper& getInstance()
{
static crashHelper instance;
return instance;
}
public:
static bool createDir(const QString& path);
static QString getCurrentTime();
static QString getBinName();
static QString toFull(const QString& path);
public:
QString dumpSavePath_;
QString dumpLogSavePath_;
private:
crashHelper() = default;
~crashHelper() = default;
};
bool crashHelper::createDir(const QString& path)
{
QDir dir(path);
if (dir.exists()) {
if (!dir.isReadable()) {
return false;
}
return true;
}
if (!dir.mkpath(".")) {
return false;
}
return true;
}
QString crashHelper::getCurrentTime()
{
QDateTime now = QDateTime::currentDateTime();
return now.toString("yyyyMMdd-HHmmss-zzz");
}
QString crashHelper::getBinName()
{
QString exePath = QCoreApplication::applicationFilePath();
if (!exePath.isEmpty()) {
QFileInfo fi(exePath);
return fi.completeBaseName(); // Returns the name without extension
}
return "";
}
QString crashHelper::toFull(const QString& path)
{
QFileInfo fi(path);
if (fi.isRelative()) {
return QDir::current().absoluteFilePath(path);
}
return fi.absoluteFilePath();
}
bool SetDumpFileSavePath(const QString& path)
{
auto& h = crashHelper::getInstance();
if (!h.createDir(path)) {
return false;
}
h.dumpSavePath_ = path;
return true;
}
bool SetDumpLogSavePath(const QString& path)
{
auto& h = crashHelper::getInstance();
if (!h.createDir(path)) {
return false;
}
h.dumpLogSavePath_ = path;
return true;
}
std::string GetCurFullLogPath()
{
auto gf = []() {
auto& h = crashHelper::getInstance();
QString dumpName = h.getCurrentTime() + "-" + h.getBinName() + ".log";
QString fullPath = QDir(h.dumpLogSavePath_).absoluteFilePath(dumpName);
return fullPath.toStdString();
};
#if defined(WIN_OS)
#if !defined(NDEBUG) || defined(_DEBUG) || defined(DEBUG)
return gf();
#else
return "";
#endif
#else
return gf();
#endif
}
#if defined(WIN_OS)
void UseExceptionHandler(EXCEPTION_POINTERS* exception)
{
auto& h = crashHelper::getInstance();
// Release mode, output dump result to file
QString dumpBase = h.getCurrentTime() + "-" + h.getBinName();
QString dumpName = h.getCurrentTime() + "-" + h.getBinName() + ".windump";
QString dumpFailedLog = h.getCurrentTime() + "-" + h.getBinName() + ".dumpfailed.log";
QString fullPath = QDir(h.dumpSavePath_).absoluteFilePath(dumpName);
QString fullFailedPath = QDir(h.dumpSavePath_).absoluteFilePath(dumpFailedLog);
HANDLE hFile =
CreateFile(fullPath.toStdWString().c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
QFile file(fullFailedPath);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << "Create dump file failed.\n";
out << "File: " << fullPath << "\n";
out << "Error code: " << GetLastError() << "\n";
}
return;
};
MINIDUMP_EXCEPTION_INFORMATION info;
info.ThreadId = GetCurrentThreadId();
info.ExceptionPointers = exception;
info.ClientPointers = TRUE;
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &info, NULL, NULL);
CloseHandle(hFile);
}
#endif
} // namespace backward