[ttssh2-commit] [9405] ミニダンプ出力を追加

Back to archive index
scmno****@osdn***** scmno****@osdn*****
2021年 9月 8日 (水) 22:35:30 JST


Revision: 9405
          https://osdn.net/projects/ttssh2/scm/svn/commits/9405
Author:   zmatsuo
Date:     2021-09-08 22:35:30 +0900 (Wed, 08 Sep 2021)
Log Message:
-----------
ミニダンプ出力を追加

- XP以降の機能
- compat_win にAPI追加
  - SymGetLineFromAddr()
  - MiniDumpWriteDump()
- 例外発生時にミニダンプを出力できるようにした
  - とりあえずデスクトップに
- defineがヘッダファイルの内容を書き換えてしまうので include順を変更
  - inpdlg.cpp, msgdlg.cpp
  - MaxStrLen

Modified Paths:
--------------
    trunk/teraterm/common/compat_win.cpp
    trunk/teraterm/common/compat_win.h
    trunk/teraterm/teraterm/ttdebug.cpp
    trunk/teraterm/teraterm/ttdebug.h
    trunk/teraterm/ttpmacro/inpdlg.cpp
    trunk/teraterm/ttpmacro/msgdlg.cpp

-------------- next part --------------
Modified: trunk/teraterm/common/compat_win.cpp
===================================================================
--- trunk/teraterm/common/compat_win.cpp	2021-09-07 14:59:51 UTC (rev 9404)
+++ trunk/teraterm/common/compat_win.cpp	2021-09-08 13:35:30 UTC (rev 9405)
@@ -83,6 +83,15 @@
 DNS_STATUS (WINAPI *pDnsQuery_A)(PCSTR pszName, WORD wType, DWORD Options, PVOID pExtra, PDNS_RECORD *ppQueryResults, PVOID *pReserved);
 VOID (WINAPI *pDnsFree)(PVOID pData, DNS_FREE_TYPE FreeType);
 
+// imagehlp.dll
+BOOL (WINAPI *pSymGetLineFromAddr)(HANDLE hProcess, DWORD dwAddr, PDWORD pdwDisplacement, PIMAGEHLP_LINE Line);
+
+// dbghelp.dll
+BOOL(WINAPI *pMiniDumpWriteDump)(HANDLE hProcess, DWORD ProcessId, HANDLE hFile, MINIDUMP_TYPE DumpType,
+								 PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
+								 PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
+								 PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
+
 class Initializer {
 public:
 	Initializer() {
@@ -200,6 +209,16 @@
 	{},
 };
 
+static const APIInfo Lists_imagehlp[] = {
+	{ "SymGetLineFromAddr", (void **)&pSymGetLineFromAddr },
+	{},
+};
+
+static const APIInfo Lists_dbghelp[] = {
+	{ "MiniDumpWriteDump", (void **)&pMiniDumpWriteDump },
+	{},
+};
+
 static const DllInfo DllInfos[] = {
 	{ L"user32.dll", DLL_LOAD_LIBRARY_SYSTEM, DLL_ACCEPT_NOT_EXIST, Lists_user32 },
 	{ L"msimg32.dll", DLL_LOAD_LIBRARY_SYSTEM, DLL_ACCEPT_NOT_EXIST, Lists_msimg32 },
@@ -208,6 +227,8 @@
 	{ L"kernel32.dll", DLL_LOAD_LIBRARY_SYSTEM, DLL_ACCEPT_NOT_EXIST, Lists_kernel32 },
 	{ L"hhctrl.ocx", DLL_LOAD_LIBRARY_SYSTEM, DLL_ACCEPT_NOT_EXIST, Lists_hhctrl },
 	{ L"dnsapi.dll", DLL_LOAD_LIBRARY_SYSTEM, DLL_ACCEPT_NOT_EXIST, Lists_dnsapi },
+	{ L"imagehlp.dll", DLL_LOAD_LIBRARY_SYSTEM, DLL_ACCEPT_NOT_EXIST, Lists_imagehlp },
+	{ L"dbghelp.dll", DLL_LOAD_LIBRARY_SYSTEM, DLL_ACCEPT_NOT_EXIST, Lists_dbghelp },
 	{},
 };
 

Modified: trunk/teraterm/common/compat_win.h
===================================================================
--- trunk/teraterm/common/compat_win.h	2021-09-07 14:59:51 UTC (rev 9404)
+++ trunk/teraterm/common/compat_win.h	2021-09-08 13:35:30 UTC (rev 9405)
@@ -36,7 +36,7 @@
 #pragma once
 
 #include <windows.h>
-#include <shlobj.h>	// for _SHBrowseForFolderW()
+#include <imagehlp.h>	// for SymGetLineFromAddr()
 
 #ifdef __cplusplus
 extern "C" {
@@ -156,6 +156,15 @@
 // htmlhelp.dll (hhctrl.ocx)
 HWND _HtmlHelpW(HWND hwndCaller, LPCWSTR pszFile, UINT uCommand, DWORD_PTR dwData);
 
+// imagehlp.dll
+extern BOOL (WINAPI *pSymGetLineFromAddr)(HANDLE hProcess, DWORD dwAddr, PDWORD pdwDisplacement, PIMAGEHLP_LINE Line);
+
+// dbghelp.dll
+extern BOOL(WINAPI *pMiniDumpWriteDump)(HANDLE hProcess, DWORD ProcessId, HANDLE hFile, MINIDUMP_TYPE DumpType,
+										PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
+										PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
+										PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
+
 void WinCompatInit();
 
 #ifdef __cplusplus

Modified: trunk/teraterm/teraterm/ttdebug.cpp
===================================================================
--- trunk/teraterm/teraterm/ttdebug.cpp	2021-09-07 14:59:51 UTC (rev 9404)
+++ trunk/teraterm/teraterm/ttdebug.cpp	2021-09-08 13:35:30 UTC (rev 9405)
@@ -29,8 +29,11 @@
 #include <windows.h>
 #include <stdio.h>
 #include <imagehlp.h>
+#include <shlobj.h>	// for SHGetSpecialFolderPathW()
 
 #include "compat_win.h"
+#include "asprintf.h"
+#include "svnversion.h"	// for SVNVERSION
 
 #include "ttdebug.h"
 
@@ -106,7 +109,7 @@
 }
 
 /* \x97\xE1\x8AO\x94\xAD\x90\xB6\x8E\x9E\x82Ɋ֐\x94\x82̌Ăяo\x82\xB5\x97\x9A\x97\xF0\x82\xF0\x95\\x8E\xA6\x82\xB7\x82\xE9\x81A\x97\xE1\x8AO\x83t\x83B\x83\x8B\x83^\x8A֐\x94 */
-static LONG CALLBACK ApplicationFaultHandler(EXCEPTION_POINTERS *ExInfo)
+static void CALLBACK ApplicationFaultHandler(EXCEPTION_POINTERS *ExInfo)
 {
 	HGLOBAL gptr;
 	STACKFRAME sf;
@@ -119,25 +122,10 @@
 	IMAGEHLP_LINE	ih_line;
 	int frame;
 	char msg[3072], buf[256];
-	HMODULE h, h2;
-	char imagehlp_dll[MAX_PATH];
-	BOOL (WINAPI *pSymGetLineFromAddr)(HANDLE hProcess, DWORD dwAddr, PDWORD pdwDisplacement, PIMAGEHLP_LINE Line);
 
-	// Windows98/Me/NT4\x82ł͓\xAE\x82\xA9\x82Ȃ\xA2\x82\xBD\x82߃X\x83L\x83b\x83v\x82\xB7\x82\xE9\x81B(2007.10.9 yutaka)
-	GetSystemDirectory(imagehlp_dll, sizeof(imagehlp_dll));
-	strncat_s(imagehlp_dll, sizeof(imagehlp_dll), "\\imagehlp.dll", _TRUNCATE);
-	h2 = LoadLibrary(imagehlp_dll);
-	h = GetModuleHandle(imagehlp_dll);
-	if (h == NULL) {
-		FreeLibrary(h2);
-		goto error;
-	}
-	*(void **)&pSymGetLineFromAddr = (void *)GetProcAddress(h, "SymGetLineFromAddr");
 	if (pSymGetLineFromAddr == NULL) {
-		FreeLibrary(h2);
 		goto error;
 	}
-	FreeLibrary(h2);
 
 	/* \x83V\x83\x93\x83{\x83\x8B\x8F\xEE\x95\xF1\x8Ai\x94[\x97p\x83o\x83b\x83t\x83@\x82̏\x89\x8A\xFA\x89\xBB */
 	gptr = GlobalAlloc(GMEM_FIXED, 10000);
@@ -260,18 +248,98 @@
 	::MessageBoxA(NULL, msg, "Tera Term: Application fault", MB_OK | MB_ICONEXCLAMATION);
 
 error:
-//	return (EXCEPTION_EXECUTE_HANDLER);  /* \x82\xBB\x82̂܂܃v\x83\x8D\x83Z\x83X\x82\xF0\x8FI\x97\xB9\x82\xB3\x82\xB9\x82\xE9 */
-	return (EXCEPTION_CONTINUE_SEARCH);  /* \x88\xF8\x82\xAB\x91\xB1\x82\xAB\x81m\x83A\x83v\x83\x8A\x83P\x81[\x83V\x83\x87\x83\x93\x83G\x83\x89\x81[\x81n\x83|\x83b\x83v\x83A\x83b\x83v\x83\x81\x83b\x83Z\x81[\x83W\x83{\x83b\x83N\x83X\x82\xF0\x8CĂяo\x82\xB7 */
+	;
 }
 #endif // !defined(_M_X64 )
 
+static wchar_t *CreateDumpFilename()
+{
+	// \x82Ƃ肠\x82\xA6\x82\xB8\x83f\x83X\x83N\x83g\x83b\x83v\x82ɍ쐬
+	wchar_t desktop[MAX_PATH];
+	SHGetSpecialFolderPathW(NULL, desktop, CSIDL_DESKTOPDIRECTORY, FALSE);
 
+	SYSTEMTIME local_time;
+	GetLocalTime(&local_time);
+
+	wchar_t *dump_file;
+	aswprintf(&dump_file, L"%s\\teraterm_r%04d_%04d%02d%02d-%02d%02d%02d.dmp",
+			  desktop,
+			  SVNVERSION,
+			  local_time.wYear, local_time.wMonth, local_time.wDay,
+			  local_time.wHour, local_time.wMinute, local_time.wSecond);
+
+	return dump_file;
+}
+
+static void DumpMiniDump(const wchar_t *filename, struct _EXCEPTION_POINTERS* pExceptionPointers)
+{
+	if (pMiniDumpWriteDump == NULL) {
+		// MiniDumpWriteDump() \x82\xAA\x83T\x83|\x81[\x83g\x82\xB3\x82\xEA\x82Ă\xA2\x82Ȃ\xA2\x81BXP\x82\xE6\x82\xE8\x91O
+		return;
+	}
+
+	HANDLE file = CreateFileW(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+	if (file == INVALID_HANDLE_VALUE) {
+		return;
+	}
+
+	MINIDUMP_EXCEPTION_INFORMATION mdei;
+	mdei.ThreadId           = GetCurrentThreadId();
+	mdei.ExceptionPointers  = pExceptionPointers;
+	mdei.ClientPointers     = TRUE;
+
+	pMiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), file,
+					   (MINIDUMP_TYPE)(MiniDumpNormal|MiniDumpWithHandleData), &mdei, NULL, NULL);
+
+	CloseHandle(file);
+}
+
+static BOOL DumpFile = TRUE;
+
+static LONG WINAPI ExceptionFilter(struct _EXCEPTION_POINTERS* pExceptionPointers)
+{
+	if (DumpFile) {
+		wchar_t *fname = CreateDumpFilename();
+		if (fname != NULL) {
+			DumpMiniDump(fname, pExceptionPointers);
+			MessageBoxW(NULL, fname, L"Tera Term", MB_OK);
+			free(fname);
+		}
+		else {
+			DumpMiniDump(L"teraterm.dmp", pExceptionPointers);
+			MessageBoxW(NULL, L"dump teraterm.dmp", L"Tera Term", MB_OK);
+		}
+	}
+
+#if !defined(_M_X64)
+	ApplicationFaultHandler(pExceptionPointers);
+#endif
+
+//	return EXCEPTION_EXECUTE_HANDLER;  /* \x82\xBB\x82̂܂܃v\x83\x8D\x83Z\x83X\x82\xF0\x8FI\x97\xB9\x82\xB3\x82\xB9\x82\xE9 */
+	return EXCEPTION_CONTINUE_SEARCH;  /* \x88\xF8\x82\xAB\x91\xB1\x82\xAB\x81m\x83A\x83v\x83\x8A\x83P\x81[\x83V\x83\x87\x83\x93\x83G\x83\x89\x81[\x81n\x83|\x83b\x83v\x83A\x83b\x83v\x83\x81\x83b\x83Z\x81[\x83W\x83{\x83b\x83N\x83X\x82\xF0\x8CĂяo\x82\xB7 */
+}
+
+void DebugTestCrash(void)
+{
+	*(int *)0 = 0;
+}
+
+static void InvalidParameterHandler(const wchar_t* /*expression*/,
+									const wchar_t* /*function*/,
+									const wchar_t* /*file*/,
+									unsigned int /*line*/,
+									uintptr_t /*pReserved*/)
+{
+	DebugTestCrash();
+}
+
 /**
  *  \x97\xE1\x8AO\x83n\x83\x93\x83h\x83\x89\x82̃t\x83b\x83N
  */
 void DebugSetException(void)
 {
-#if !defined(_M_X64)
-	SetUnhandledExceptionFilter(ApplicationFaultHandler);
-#endif
+	SetUnhandledExceptionFilter(ExceptionFilter);
+
+	// C\x83\x89\x83\x93\x83^\x83C\x83\x80\x96\xB3\x8C\xF8\x82ȃp\x83\x89\x83\x81\x81[\x83^\x83G\x83\x89\x81[\x83n\x83\x93\x83h\x83\x89
+	_set_invalid_parameter_handler(InvalidParameterHandler);
 }

Modified: trunk/teraterm/teraterm/ttdebug.h
===================================================================
--- trunk/teraterm/teraterm/ttdebug.h	2021-09-07 14:59:51 UTC (rev 9404)
+++ trunk/teraterm/teraterm/ttdebug.h	2021-09-08 13:35:30 UTC (rev 9405)
@@ -40,6 +40,7 @@
 
 void DebugSetException(void);
 HWND DebugConsoleOpen(void);
+void DebugTestCrash(void);
 
 #ifdef __cplusplus
 }

Modified: trunk/teraterm/ttpmacro/inpdlg.cpp
===================================================================
--- trunk/teraterm/ttpmacro/inpdlg.cpp	2021-09-07 14:59:51 UTC (rev 9404)
+++ trunk/teraterm/ttpmacro/inpdlg.cpp	2021-09-08 13:35:30 UTC (rev 9405)
@@ -31,11 +31,12 @@
 
 #include "teraterm.h"
 #include "ttlib.h"
+#include "compat_win.h"
+#include "dlglib.h"
+
 #include "ttmdef.h"
 #include "ttm_res.h"
 #include "ttmlib.h"
-#include "dlglib.h"
-#include "compat_win.h"
 
 #include "inpdlg.h"
 

Modified: trunk/teraterm/ttpmacro/msgdlg.cpp
===================================================================
--- trunk/teraterm/ttpmacro/msgdlg.cpp	2021-09-07 14:59:51 UTC (rev 9404)
+++ trunk/teraterm/ttpmacro/msgdlg.cpp	2021-09-08 13:35:30 UTC (rev 9405)
@@ -32,13 +32,14 @@
 #include <windows.h>
 #include <windowsx.h>
 #include "teraterm.h"
+#include "tmfc.h"
+#include "dlglib.h"
+#include "compat_win.h"
+
 #include "ttlib.h"
 #include "ttm_res.h"
 #include "ttmlib.h"
-#include "tmfc.h"
-#include "dlglib.h"
 #include "ttmdlg.h"
-#include "compat_win.h"
 
 #include "msgdlg.h"
 
@@ -182,7 +183,7 @@
 }
 
 // \x83\x81\x83b\x83Z\x81[\x83W\x83{\x83b\x83N\x83X\x82\xF0\x83L\x83\x83\x83\x93\x83Z\x83\x8B\x82\xB7\x82\xE9(close\x83{\x83^\x83\x93\x82\xF0\x89\x9F\x82\xB7)\x82ƁA\x83}\x83N\x83\x8D\x82̏I\x97\xB9\x82Ƃ\xB7\x82\xE9\x81B
-// (2008.8.5 yutaka)	
+// (2008.8.5 yutaka)
 BOOL CMsgDlg::OnClose()
 {
 	const int ret = MessageBoxHaltScript(m_hWnd);


ttssh2-commit メーリングリストの案内
Back to archive index