diff --git a/CMakeLists.txt b/CMakeLists.txt index 3cffedbb591f09b33a8c52f09493bd5ced834506..36c5bc5fbe54f5915982ac2ea219f8c61cf842c9 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ find_package(CUDA QUIET) option(WITH_GPU "Compile PaddlePaddle with NVIDIA GPU" ${CUDA_FOUND}) option(WITH_TENSORRT "Compile PaddlePaddle with NVIDIA TensorRT" OFF) option(WITH_XPU "Compile PaddlePaddle with BAIDU KUNLUN" OFF) +option(WITH_WIN_DUMP_DBG "Compile with windows core dump debug mode" OFF) if (WITH_GPU AND WITH_XPU) message(FATAL_ERROR "Error when compile GPU and XPU at the same time") endif() @@ -103,6 +104,18 @@ if(WIN32) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4068 /wd4129 /wd4244 /wd4267 /wd4297 /wd4530 /wd4577 /wd4819 /wd4838") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4068 /wd4129 /wd4244 /wd4267 /wd4297 /wd4530 /wd4577 /wd4819 /wd4838") + + if (WITH_WIN_DUMP_DBG) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Zi") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi") + + foreach(flag_var CMAKE_SHARED_LINKER_FLAGS CMAKE_STATIC_LINKER_FLAGS CMAKE_EXE_LINKER_FLAGS CMAKE_LINKER_FLAGS) + set(${flag_var} "${${flag_var}} /DEBUG /OPT:REF /OPT:ICF") + endforeach(flag_var) + + add_definitions("-DWITH_WIN_DUMP_DBG") + endif() + else(WIN32) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=deprecated-declarations -Wno-deprecated-declarations") endif(WIN32) diff --git a/paddle/fluid/platform/init.cc b/paddle/fluid/platform/init.cc index a3e035a8125274b0f073c472b28bda05511d5a92..4288dc66d679a35fb1d6dd988dab8e86d408d709 100644 --- a/paddle/fluid/platform/init.cc +++ b/paddle/fluid/platform/init.cc @@ -39,6 +39,13 @@ limitations under the License. */ #include "paddle/fluid/platform/xpu_info.h" #endif +#ifdef WITH_WIN_DUMP_DBG +#include +#include +#include +#include "DbgHelp.h" +#endif + DECLARE_int32(paddle_num_threads); DEFINE_int32(multiple_of_cupti_buffer_size, 1, "Multiple of the CUPTI device buffer size. If the timestamps have " @@ -94,8 +101,6 @@ bool InitGflags(std::vector args) { return successed; } - - void InitCupti() { #ifdef PADDLE_WITH_CUPTI if (FLAGS_multiple_of_cupti_buffer_size == 1) return; @@ -292,10 +297,53 @@ void SignalHandle(const char *data, int size) { } #endif +#ifdef WITH_WIN_DUMP_DBG +typedef BOOL(WINAPI *MINIDUMP_WRITE_DUMP)( + IN HANDLE hProcess, IN DWORD ProcessId, IN HANDLE hFile, + IN MINIDUMP_TYPE DumpType, + IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, + OPTIONAL IN PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, + OPTIONAL IN PMINIDUMP_CALLBACK_INFORMATION CallbackParam OPTIONAL); +void CreateDumpFile(LPCSTR lpstrDumpFilePathName, + EXCEPTION_POINTERS *pException) { + HANDLE hDumpFile = CreateFile(lpstrDumpFilePathName, GENERIC_WRITE, 0, NULL, + CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + MINIDUMP_EXCEPTION_INFORMATION dumpInfo; + dumpInfo.ExceptionPointers = pException; + dumpInfo.ThreadId = GetCurrentThreadId(); + dumpInfo.ClientPointers = TRUE; + MINIDUMP_WRITE_DUMP MiniDumpWriteDump_; + HMODULE hDbgHelp = LoadLibrary("DBGHELP.DLL"); + MiniDumpWriteDump_ = + (MINIDUMP_WRITE_DUMP)GetProcAddress(hDbgHelp, "MiniDumpWriteDump"); + MiniDumpWriteDump_(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, + MiniDumpWithPrivateReadWriteMemory, &dumpInfo, NULL, NULL); + CloseHandle(hDumpFile); +} + +LONG ApplicationCrashHandler(EXCEPTION_POINTERS *pException) { + time_t time_seconds = time(0); + struct tm now_time; + localtime_s(&now_time, &time_seconds); + + char buf[1024]; + sprintf_s(buf, "C:\\Paddle%04d%02d%02d-%02d%02d%02d.dmp", + 1900 + now_time.tm_year, 1 + now_time.tm_mon, now_time.tm_mday, + now_time.tm_hour, now_time.tm_min, now_time.tm_sec); + + CreateDumpFile(buf, pException); + return EXCEPTION_EXECUTE_HANDLER; +} +#endif + void InitGLOG(const std::string &prog_name) { std::call_once(glog_init_flag, [&]() { - // glog will not hold the ARGV[0] inside. - // Use strdup to alloc a new string. +// glog will not hold the ARGV[0] inside. +// Use strdup to alloc a new string. +#ifdef WITH_WIN_DUMP_DBG + SetUnhandledExceptionFilter( + (LPTOP_LEVEL_EXCEPTION_FILTER)ApplicationCrashHandler); +#endif google::InitGoogleLogging(strdup(prog_name.c_str())); #ifndef _WIN32 google::InstallFailureSignalHandler();