From 55bcf1b00137a23684f71712ebc3c062b99d4ac1 Mon Sep 17 00:00:00 2001 From: lltcggie Date: Mon, 8 Jun 2015 02:43:44 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=80=E3=83=B3=E3=83=97=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=A4=E3=83=AB=E3=82=92=E5=87=BA=E5=8A=9B=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/waifu2x.cpp | 14 +- waifu2x-caffe-gui/MiniDump.cpp | 179 ++++++++++++++++++ waifu2x-caffe-gui/MiniDump.h | 8 + waifu2x-caffe-gui/Source.cpp | 5 + waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj | 2 + .../waifu2x-caffe-gui.vcxproj.filters | 6 + 6 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 waifu2x-caffe-gui/MiniDump.cpp create mode 100644 waifu2x-caffe-gui/MiniDump.h diff --git a/common/waifu2x.cpp b/common/waifu2x.cpp index eeddb5b..7c45bf3 100644 --- a/common/waifu2x.cpp +++ b/common/waifu2x.cpp @@ -12,6 +12,7 @@ #if defined(WIN32) || defined(WIN64) #include +#include "../waifu2x-caffe-gui/MiniDump.h" #undef LoadImage #endif @@ -527,11 +528,22 @@ Waifu2x::eWaifu2xError Waifu2x::init(int argc, char** argv, const std::string &M { assert(argc >= 1); - int tmpargc = 1; char* tmpargvv[] = { argv[0] }; + int tmpargc = sizeof(tmpargvv) / sizeof(tmpargvv[0]); char** tmpargv = tmpargvv; + + FLAGS_minloglevel = 1; + FLAGS_logtostderr = 0; + FLAGS_log_dir = "./logs"; + // glog“™‚̏‰Šϊ‰» caffe::GlobalInit(&tmpargc, &tmpargv); + + ::google::InstallFailureFunction([]() + { + ExceptionHandler(nullptr, 2); + abort(); + }); }); const auto cuDNNCheckStartTime = std::chrono::system_clock::now(); diff --git a/waifu2x-caffe-gui/MiniDump.cpp b/waifu2x-caffe-gui/MiniDump.cpp new file mode 100644 index 0000000..040278c --- /dev/null +++ b/waifu2x-caffe-gui/MiniDump.cpp @@ -0,0 +1,179 @@ +#include +#include +#include +#include +#include +#include +#include "MiniDump.h" + +// #pragma comment (lib, "dbghelp.lib") +#pragma comment (lib, "shlwapi.lib") + +typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD ProcessId, HANDLE hFile, MINIDUMP_TYPE DumpType, PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, PMINIDUMP_CALLBACK_INFORMATION CallbackParam); + +const int DefaultLevel = 2; + + +void CreateDump(EXCEPTION_POINTERS *pep, DWORD ThreadID, int level) +{ + HMODULE mhLib = ::LoadLibrary(_T("dbghelp_local.dll")); + if (!mhLib) + return; + + MINIDUMPWRITEDUMP pDump = (MINIDUMPWRITEDUMP)::GetProcAddress(mhLib, "MiniDumpWriteDump"); + if (!pDump) + { + FreeLibrary(mhLib); + + if (pep) + UnhandledExceptionFilter(pep); + + return; + } + + TCHAR szFilePath[1024]; + + ::GetModuleFileName(NULL, szFilePath, sizeof(szFilePath)); + ::PathRemoveFileSpec(szFilePath); + _tcscat_s(szFilePath, TEXT("\\")); + + TCHAR SettingDir[100] = TEXT(""); + +#if !defined(PRODUCT_MODE) + { + TCHAR iniPath[1024]; + _tcscpy_s(iniPath, szFilePath); + _tcscat_s(iniPath, TEXT("DebugSetting.ini")); + GetPrivateProfileString(TEXT("MemoryDump"), TEXT("RelativePath"), TEXT(""), SettingDir, sizeof(SettingDir) / sizeof(SettingDir[0]), iniPath); + + const size_t len = _tcslen(SettingDir); + if (len > 0 && (SettingDir[len - 1] != TEXT('\\') || SettingDir[len - 1] != TEXT('/'))) + _tcscat_s(SettingDir, TEXT("\\")); + } +#endif + + SYSTEMTIME systime; + GetLocalTime(&systime); + + TCHAR buf[100]; + _stprintf_s(buf, TEXT("%04d_%02d_%02d %02u_%02u_%02u_%03u.dmp"), systime.wYear, systime.wMonth, systime.wDay, systime.wHour, systime.wMinute, systime.wSecond, systime.wMilliseconds); + + + _tcscat_s(szFilePath, SettingDir); + _tcscat_s(szFilePath, buf); + + HANDLE hFile = CreateFile(szFilePath, + GENERIC_READ | GENERIC_WRITE, + 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (hFile == INVALID_HANDLE_VALUE){ + _tprintf(_T("CreateFile failed. Error: %u \n"), + GetLastError()); + FreeLibrary(mhLib); + + if (pep) + UnhandledExceptionFilter(pep); + + return; + } + + MINIDUMP_EXCEPTION_INFORMATION mdei; + + mdei.ThreadId = ThreadID; + mdei.ExceptionPointers = pep; + mdei.ClientPointers = FALSE; + + MINIDUMP_CALLBACK_INFORMATION mci; + + mci.CallbackRoutine = NULL; + mci.CallbackParam = 0; + + MINIDUMP_TYPE mdt; + + switch (level) + { + case 0: + mdt = (MINIDUMP_TYPE)(MiniDumpNormal); + break; + case 1: + mdt = (MINIDUMP_TYPE)( + MiniDumpWithIndirectlyReferencedMemory | + MiniDumpScanMemory); + break; + case 2: + mdt = (MINIDUMP_TYPE)( + MiniDumpWithIndirectlyReferencedMemory | + MiniDumpScanMemory | + MiniDumpWithDataSegs | + MiniDumpWithHandleData | + MiniDumpWithFullMemoryInfo | + MiniDumpWithThreadInfo | + MiniDumpWithUnloadedModules); + break; + case 3: + mdt = (MINIDUMP_TYPE)( + MiniDumpWithPrivateReadWriteMemory | + MiniDumpWithDataSegs | + MiniDumpWithHandleData | + MiniDumpWithFullMemoryInfo | + MiniDumpWithThreadInfo | + MiniDumpWithUnloadedModules); + break; + default: + mdt = (MINIDUMP_TYPE)( + MiniDumpWithFullMemory | + MiniDumpWithFullMemoryInfo | + MiniDumpWithHandleData | + MiniDumpWithThreadInfo | + MiniDumpWithUnloadedModules); + break; + } + + BOOL rv = pDump( + GetCurrentProcess(), GetCurrentProcessId(), + hFile, mdt, (pep != NULL) ? &mdei : NULL, NULL, &mci); + if (rv == FALSE){ + // _tprintf(_T("MiniDumpWriteDump failed. Error: %u \n"), GetLastError()); + } + + CloseHandle(hFile); + + FreeLibrary(mhLib); + + return; +} + + +struct stException +{ + PEXCEPTION_POINTERS ExceptionInfo; + DWORD ThreadID; + int Level; +}; + +static stException g_info; + + +DWORD WINAPI ReportFunc(LPVOID ThreadParam) +{ + CreateDump(g_info.ExceptionInfo, g_info.ThreadID, g_info.Level); + return 0; +} + +LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo, int level) +{ + if (!ExceptionInfo || ExceptionInfo->ExceptionRecord->ExceptionCode != 0xE06D7363) + { + g_info.ExceptionInfo = ExceptionInfo; + g_info.ThreadID = GetCurrentThreadId(); + g_info.Level = level; + HANDLE hThread = CreateThread(NULL, 0, ReportFunc, NULL, 0, NULL); + WaitForSingleObject(hThread, INFINITE); + } + + return EXCEPTION_CONTINUE_SEARCH; +} + +LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) +{ + return ExceptionHandler(ExceptionInfo, DefaultLevel); +} diff --git a/waifu2x-caffe-gui/MiniDump.h b/waifu2x-caffe-gui/MiniDump.h new file mode 100644 index 0000000..5053658 --- /dev/null +++ b/waifu2x-caffe-gui/MiniDump.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +void CreateDump(EXCEPTION_POINTERS *pep, DWORD ThreadID, int level = 2); + +LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo, int level); +LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo); diff --git a/waifu2x-caffe-gui/Source.cpp b/waifu2x-caffe-gui/Source.cpp index 608eb96..e8d6dcd 100644 --- a/waifu2x-caffe-gui/Source.cpp +++ b/waifu2x-caffe-gui/Source.cpp @@ -18,6 +18,7 @@ #include "CDialog.h" #include "CControl.h" +#include "MiniDump.h" #define WM_FAILD_CREATE_DIR (WM_APP + 5) #define WM_ON_WAIFU2X_ERROR (WM_APP + 6) @@ -824,6 +825,8 @@ int WINAPI WinMain(HINSTANCE hInstance, LPSTR lpCmdLine, int nCmdShow) { + //PVOID hVectoredExceptionHandler = AddVectoredExceptionHandler(1, ExceptionHandler); + // CDialogƒNƒ‰ƒX‚Εƒ_ƒCƒAƒƒO‚πμ¬‚·‚ι CDialog cDialog; CDialog cDialog2; @@ -876,5 +879,7 @@ int WINAPI WinMain(HINSTANCE hInstance, // ƒ_ƒCƒAƒƒO‚π•\ަ cDialog.DoModal(hInstance, IDD_DIALOG); + //RemoveVectoredExceptionHandler(hVectoredExceptionHandler); + return 0; } diff --git a/waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj b/waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj index 01abfa8..9da431f 100644 --- a/waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj +++ b/waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj @@ -92,6 +92,7 @@ + @@ -102,6 +103,7 @@ + diff --git a/waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj.filters b/waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj.filters index 4282ea2..11b2f93 100644 --- a/waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj.filters +++ b/waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj.filters @@ -30,6 +30,9 @@ γ‚½γƒΌγ‚Ή フゑむル + + γ‚½γƒΌγ‚Ή フゑむル + @@ -56,6 +59,9 @@ γƒ˜γƒƒγƒ€γƒΌ フゑむル + + γƒ˜γƒƒγƒ€γƒΌ フゑむル +