mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-02-19 06:40:54 +08:00
Add win32 core dump (#4291)
单元测试代码
int main() {
System::systemSetup();
int *p = nullptr;
*p = 1; // 强制崩溃
return 0;
}
崩溃后直接用 vs2019(2022) 打开 dmp 文件。
菜单选择 File → Open → File...,选择你的 .dmp 文件。
提示你“选择符号和源码”,若有源码和 pdb/symbol 文件能看到更详细的堆栈。
This commit is contained in:
@@ -15,6 +15,12 @@
|
||||
#if !defined(ANDROID)
|
||||
#include <execinfo.h>
|
||||
#endif//!defined(ANDROID)
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
#include <Windows.h>
|
||||
#include <DbgHelp.h>
|
||||
#pragma comment(lib, "DbgHelp.lib")
|
||||
#endif//!defined(_WIN32)
|
||||
|
||||
#include <cstdlib>
|
||||
@@ -213,6 +219,48 @@ void System::systemSetup(){
|
||||
// Ignore the hang up signal
|
||||
signal(SIGHUP, SIG_IGN);
|
||||
#endif// ANDROID
|
||||
#else
|
||||
// 避免系统弹窗导致程序阻塞,适合无界面或后台服务场景。
|
||||
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
|
||||
|
||||
#if !defined(__MINGW32__)
|
||||
// 将assert和error时错误输出
|
||||
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
|
||||
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
|
||||
#endif
|
||||
|
||||
_setmode(0, _O_BINARY);
|
||||
_setmode(1, _O_BINARY);
|
||||
_setmode(2, _O_BINARY);
|
||||
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
setvbuf(stderr, NULL, _IONBF, 0);
|
||||
std::ios_base::sync_with_stdio(false);
|
||||
|
||||
// 注册crash自动生成dump(等价core dump)
|
||||
SetUnhandledExceptionFilter([](EXCEPTION_POINTERS *pException) -> LONG {
|
||||
// 生成 dump 文件名,带时间戳
|
||||
char dumpPath[MAX_PATH];
|
||||
std::time_t t = std::time(nullptr);
|
||||
std::tm tm;
|
||||
#ifdef _MSC_VER
|
||||
localtime_s(&tm, &t);
|
||||
#else
|
||||
tm = *std::localtime(&t);
|
||||
#endif
|
||||
std::strftime(dumpPath, sizeof(dumpPath), "crash_%Y%m%d_%H%M%S.dmp", &tm);
|
||||
|
||||
HANDLE hFile = CreateFileA(dumpPath, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (hFile != INVALID_HANDLE_VALUE) {
|
||||
MINIDUMP_EXCEPTION_INFORMATION mdei;
|
||||
mdei.ThreadId = GetCurrentThreadId();
|
||||
mdei.ExceptionPointers = pException;
|
||||
mdei.ClientPointers = FALSE;
|
||||
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &mdei, nullptr, nullptr);
|
||||
CloseHandle(hFile);
|
||||
}
|
||||
return EXCEPTION_EXECUTE_HANDLER;
|
||||
});
|
||||
#endif//!defined(_WIN32)
|
||||
}
|
||||
|
||||
|
||||
@@ -41,9 +41,7 @@
|
||||
#include "ZLMVersion.h"
|
||||
#endif
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#include "System.h"
|
||||
#endif//!defined(_WIN32)
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
@@ -259,10 +257,11 @@ int start_main(int argc,char *argv[]) {
|
||||
// Start daemon process
|
||||
System::startDaemon(kill_parent_if_failed);
|
||||
}
|
||||
#endif //! defined(_WIN32)
|
||||
|
||||
// 开启崩溃捕获等 [AUTO-TRANSLATED:9c7c759c]
|
||||
// Enable crash capture, etc.
|
||||
System::systemSetup();
|
||||
#endif//!defined(_WIN32)
|
||||
|
||||
// 启动异步日志线程 [AUTO-TRANSLATED:c93cc6f4]
|
||||
// Start asynchronous log thread
|
||||
|
||||
Reference in New Issue
Block a user