Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /branches/ttcomtester/teraterm/teraterm/ttdebug.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9124 - (hide annotations) (download) (as text)
Sat Jan 16 05:19:55 2021 UTC (3 years, 2 months ago) by zmatsuo
Original Path: trunk/teraterm/teraterm/ttdebug.cpp
File MIME type: text/x-c++src
File size: 8584 byte(s)
起動時にデバグ用コンソールをオープンできるようにした

- デバグ用 ttdebug.h,cpp を追加
- 例外発生時の処理を ttdebug.cpp に移動
1 zmatsuo 9124 /*
2     * (C) 2020- TeraTerm Project
3     * All rights reserved.
4     *
5     * Redistribution and use in source and binary forms, with or without
6     * modification, are permitted provided that the following conditions
7     * are met:
8     *
9     * 1. Redistributions of source code must retain the above copyright
10     * notice, this list of conditions and the following disclaimer.
11     * 2. Redistributions in binary form must reproduce the above copyright
12     * notice, this list of conditions and the following disclaimer in the
13     * documentation and/or other materials provided with the distribution.
14     * 3. The name of the author may not be used to endorse or promote products
15     * derived from this software without specific prior written permission.
16     *
17     * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20     * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27     */
28    
29     #include <windows.h>
30     #include <stdio.h>
31     #include <imagehlp.h>
32    
33     #include "compat_win.h"
34    
35     #include "ttdebug.h"
36    
37     /**
38     * �R���\�[���E�B���h�E���\�������f�o�O�p
39     * �f�o�O�p������������printf()�n���\����������������������������
40     *
41     * @retval �R���\�[����Window Handle
42     */
43     HWND DebugConsoleOpen(void)
44     {
45     FILE *fp;
46     HWND hWnd = pGetConsoleWindow();
47     if (hWnd != NULL) {
48     return hWnd;
49     }
50     AllocConsole();
51     freopen_s(&fp, "CONOUT$", "w", stdout);
52     freopen_s(&fp, "CONOUT$", "w", stderr);
53    
54     // �������{�^����������
55     hWnd = pGetConsoleWindow();
56     HMENU hmenu = GetSystemMenu(hWnd, FALSE);
57     RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
58    
59     return hWnd;
60     }
61    
62     //
63     // ���O�n���h�����t�b�N�i�X�^�b�N�g���[�X���_���v�j
64     //
65     // cf. http://svn.collab.net/repos/svn/trunk/subversion/libsvn_subr/win32_crashrpt.c
66     // (2007.9.30 yutaka)
67     //
68     // ���O�R�[�h������������������
69     #if !defined(_M_X64)
70     static const char *GetExceptionString(DWORD exception)
71     {
72     #define EXCEPTION(x) case EXCEPTION_##x: return (#x);
73     static char buf[16];
74    
75     switch (exception)
76     {
77     EXCEPTION(ACCESS_VIOLATION)
78     EXCEPTION(DATATYPE_MISALIGNMENT)
79     EXCEPTION(BREAKPOINT)
80     EXCEPTION(SINGLE_STEP)
81     EXCEPTION(ARRAY_BOUNDS_EXCEEDED)
82     EXCEPTION(FLT_DENORMAL_OPERAND)
83     EXCEPTION(FLT_DIVIDE_BY_ZERO)
84     EXCEPTION(FLT_INEXACT_RESULT)
85     EXCEPTION(FLT_INVALID_OPERATION)
86     EXCEPTION(FLT_OVERFLOW)
87     EXCEPTION(FLT_STACK_CHECK)
88     EXCEPTION(FLT_UNDERFLOW)
89     EXCEPTION(INT_DIVIDE_BY_ZERO)
90     EXCEPTION(INT_OVERFLOW)
91     EXCEPTION(PRIV_INSTRUCTION)
92     EXCEPTION(IN_PAGE_ERROR)
93     EXCEPTION(ILLEGAL_INSTRUCTION)
94     EXCEPTION(NONCONTINUABLE_EXCEPTION)
95     EXCEPTION(STACK_OVERFLOW)
96     EXCEPTION(INVALID_DISPOSITION)
97     EXCEPTION(GUARD_PAGE)
98     EXCEPTION(INVALID_HANDLE)
99    
100     default:
101     _snprintf_s(buf, sizeof(buf), _TRUNCATE, "0x%x", exception);
102     return buf;
103     //return "UNKNOWN_ERROR";
104     }
105     #undef EXCEPTION
106     }
107    
108     /* ���O�������������������o���������\�������A���O�t�B���^���� */
109     static LONG CALLBACK ApplicationFaultHandler(EXCEPTION_POINTERS *ExInfo)
110     {
111     HGLOBAL gptr;
112     STACKFRAME sf;
113     BOOL bResult;
114     PIMAGEHLP_SYMBOL pSym;
115     DWORD Disp;
116     HANDLE hProcess = GetCurrentProcess();
117     HANDLE hThread = GetCurrentThread();
118     IMAGEHLP_MODULE ih_module;
119     IMAGEHLP_LINE ih_line;
120     int frame;
121     char msg[3072], buf[256];
122     HMODULE h, h2;
123     char imagehlp_dll[MAX_PATH];
124    
125     // Windows98/Me/NT4�����������������X�L�b�v�����B(2007.10.9 yutaka)
126     GetSystemDirectory(imagehlp_dll, sizeof(imagehlp_dll));
127     strncat_s(imagehlp_dll, sizeof(imagehlp_dll), "\\imagehlp.dll", _TRUNCATE);
128     h2 = LoadLibrary(imagehlp_dll);
129     if (((h = GetModuleHandle(imagehlp_dll)) == NULL) ||
130     (GetProcAddress(h, "SymGetLineFromAddr") == NULL)) {
131     FreeLibrary(h2);
132     goto error;
133     }
134     FreeLibrary(h2);
135    
136     /* �V���{�������i�[�p�o�b�t�@�������� */
137     gptr = GlobalAlloc(GMEM_FIXED, 10000);
138     if (gptr == NULL) {
139     goto error;
140     }
141     pSym = (PIMAGEHLP_SYMBOL)GlobalLock(gptr);
142     ZeroMemory(pSym, sizeof(IMAGEHLP_SYMBOL));
143     pSym->SizeOfStruct = 10000;
144     pSym->MaxNameLength = 10000 - sizeof(IMAGEHLP_SYMBOL);
145    
146     /* �X�^�b�N�t���[���������� */
147     ZeroMemory(&sf, sizeof(sf));
148     sf.AddrPC.Offset = ExInfo->ContextRecord->Eip;
149     sf.AddrStack.Offset = ExInfo->ContextRecord->Esp;
150     sf.AddrFrame.Offset = ExInfo->ContextRecord->Ebp;
151     sf.AddrPC.Mode = AddrModeFlat;
152     sf.AddrStack.Mode = AddrModeFlat;
153     sf.AddrFrame.Mode = AddrModeFlat;
154    
155     /* �V���{���n���h���������� */
156     SymInitialize(hProcess, NULL, TRUE);
157    
158     // ���W�X�^�_���v
159     msg[0] = '\0';
160     _snprintf_s(buf, sizeof(buf), _TRUNCATE, "eax=%08X ebx=%08X ecx=%08X edx=%08X esi=%08X edi=%08X\r\n"
161     "ebp=%08X esp=%08X eip=%08X efl=%08X\r\n"
162     "cs=%04X ss=%04X ds=%04X es=%04X fs=%04X gs=%04X\r\n",
163     ExInfo->ContextRecord->Eax,
164     ExInfo->ContextRecord->Ebx,
165     ExInfo->ContextRecord->Ecx,
166     ExInfo->ContextRecord->Edx,
167     ExInfo->ContextRecord->Esi,
168     ExInfo->ContextRecord->Edi,
169     ExInfo->ContextRecord->Ebp,
170     ExInfo->ContextRecord->Esp,
171     ExInfo->ContextRecord->Eip,
172     ExInfo->ContextRecord->EFlags,
173     ExInfo->ContextRecord->SegCs,
174     ExInfo->ContextRecord->SegSs,
175     ExInfo->ContextRecord->SegDs,
176     ExInfo->ContextRecord->SegEs,
177     ExInfo->ContextRecord->SegFs,
178     ExInfo->ContextRecord->SegGs
179     );
180     strncat_s(msg, sizeof(msg), buf, _TRUNCATE);
181    
182     if (ExInfo->ExceptionRecord != NULL) {
183     _snprintf_s(buf, sizeof(buf), _TRUNCATE, "Exception: %s\r\n", GetExceptionString(ExInfo->ExceptionRecord->ExceptionCode));
184     strncat_s(msg, sizeof(msg), buf, _TRUNCATE);
185     }
186    
187     /* �X�^�b�N�t���[���������\���������� */
188     frame = 0;
189     for (;;) {
190     /* �����X�^�b�N�t���[�������� */
191     bResult = StackWalk(
192     IMAGE_FILE_MACHINE_I386,
193     hProcess,
194     hThread,
195     &sf,
196     NULL,
197     NULL,
198     SymFunctionTableAccess,
199     SymGetModuleBase,
200     NULL);
201    
202     /* ���s�������A���[�v�������� */
203     if (!bResult || sf.AddrFrame.Offset == 0)
204     break;
205    
206     frame++;
207    
208     /* �v���O�����J�E���^�i���z�A�h���X�j�������������I�t�Z�b�g������ */
209     bResult = SymGetSymFromAddr(hProcess, sf.AddrPC.Offset, &Disp, pSym);
210    
211     /* �����������\�� */
212     _snprintf_s(buf, sizeof(buf), _TRUNCATE, "#%d 0x%08x in ", frame, sf.AddrPC.Offset);
213     strncat_s(msg, sizeof(msg), buf, _TRUNCATE);
214     if (bResult) {
215     _snprintf_s(buf, sizeof(buf), _TRUNCATE, "%s() + 0x%x ", pSym->Name, Disp);
216     strncat_s(msg, sizeof(msg), buf, _TRUNCATE);
217     } else {
218     _snprintf_s(buf, sizeof(buf), _TRUNCATE, " --- ");
219     strncat_s(msg, sizeof(msg), buf, _TRUNCATE);
220     }
221    
222     // ���s�t�@�C����������
223     ZeroMemory( &(ih_module), sizeof(ih_module) );
224     ih_module.SizeOfStruct = sizeof(ih_module);
225     bResult = SymGetModuleInfo( hProcess, sf.AddrPC.Offset, &(ih_module) );
226     strncat_s(msg, sizeof(msg), "at ", _TRUNCATE);
227     if (bResult) {
228     _snprintf_s(buf, sizeof(buf), _TRUNCATE, "%s ", ih_module.ImageName );
229     strncat_s(msg, sizeof(msg), buf, _TRUNCATE);
230     } else {
231     _snprintf_s(buf, sizeof(buf), _TRUNCATE, "%s ", "<Unknown Module>" );
232     strncat_s(msg, sizeof(msg), buf, _TRUNCATE);
233     }
234    
235     // �t�@�C�������s����������
236     ZeroMemory( &(ih_line), sizeof(ih_line) );
237     ih_line.SizeOfStruct = sizeof(ih_line);
238     bResult = SymGetLineFromAddr( hProcess, sf.AddrPC.Offset, &Disp, &ih_line );
239     if (bResult)
240     {
241     _snprintf_s(buf, sizeof(buf), _TRUNCATE, "%s:%lu", ih_line.FileName, ih_line.LineNumber );
242     strncat_s(msg, sizeof(msg), buf, _TRUNCATE);
243     }
244    
245     strncat_s(msg, sizeof(msg), "\n", _TRUNCATE);
246     }
247    
248     /* ������ */
249     SymCleanup(hProcess);
250     GlobalUnlock(pSym);
251     GlobalFree(pSym);
252    
253     // ���O�������������AAPI�����������o��
254     ::MessageBoxA(NULL, msg, "Tera Term: Application fault", MB_OK | MB_ICONEXCLAMATION);
255    
256     error:
257     // return (EXCEPTION_EXECUTE_HANDLER); /* ���������v���Z�X���I�������� */
258     return (EXCEPTION_CONTINUE_SEARCH); /* ���������m�A�v���P�[�V�����G���[�n�|�b�v�A�b�v���b�Z�[�W�{�b�N�X�������o�� */
259     }
260     #endif // !defined(_M_X64 )
261    
262    
263     /**
264     * ���O�n���h�����t�b�N
265     */
266     void DebugSetException(void)
267     {
268     #if !defined(_M_X64)
269     SetUnhandledExceptionFilter(ApplicationFaultHandler);
270     #endif
271     }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26