-
Notifications
You must be signed in to change notification settings - Fork 53
/
exchndl.c
209 lines (168 loc) · 5.39 KB
/
exchndl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright 2002-2013 Jose Fonseca
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "exchndl.h"
#include <assert.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <dbghelp.h>
#include "symbols.h"
#include "log.h"
#include "outdbg.h"
#define REPORT_FILE 1
// Declare the static variables
static LPTOP_LEVEL_EXCEPTION_FILTER g_prevExceptionFilter = NULL;
static TCHAR g_szLogFileName[MAX_PATH] = _T("");
static HANDLE g_hReportFile;
static void
writeReport(const char *szText)
{
if (REPORT_FILE) {
DWORD cbWritten;
WriteFile(g_hReportFile, szText, strlen(szText), &cbWritten, 0);
} else {
OutputDebugStringA(szText);
}
}
static
void GenerateExceptionReport(PEXCEPTION_POINTERS pExceptionInfo)
{
PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord;
// Start out with a banner
lprintf(_T("-------------------\r\n\r\n"));
SYSTEMTIME SystemTime;
GetLocalTime(&SystemTime);
TCHAR szDateStr[128];
GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &SystemTime, _T("dddd',' MMMM d',' yyyy"), szDateStr, _countof(szDateStr));
TCHAR szTimeStr[128];
GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, &SystemTime, _T("HH':'mm':'ss"), szTimeStr, _countof(szTimeStr));
lprintf(_T("Error occured on %s at %s.\r\n\r\n"), szDateStr, szTimeStr);
HANDLE hProcess = GetCurrentProcess();
DWORD dwSymOptions = SymGetOptions();
dwSymOptions |=
SYMOPT_LOAD_LINES |
SYMOPT_DEFERRED_LOADS;
SymSetOptions(dwSymOptions);
if (InitializeSym(hProcess, TRUE)) {
dumpException(hProcess, pExceptionRecord);
PCONTEXT pContext = pExceptionInfo->ContextRecord;
dumpStack(hProcess, GetCurrentThread(), pContext);
if (!SymCleanup(hProcess)) {
assert(0);
}
}
}
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
// Entry point where control comes on an unhandled exception
static
LONG WINAPI TopLevelExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
{
static BOOL bBeenHere = FALSE;
if(!bBeenHere)
{
UINT fuOldErrorMode;
bBeenHere = TRUE;
fuOldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
if (REPORT_FILE) {
g_hReportFile = CreateFile(
g_szLogFileName,
GENERIC_WRITE,
0,
0,
OPEN_ALWAYS,
FILE_FLAG_WRITE_THROUGH,
0
);
if (g_hReportFile) {
SetFilePointer(g_hReportFile, 0, 0, FILE_END);
GenerateExceptionReport(pExceptionInfo);
CloseHandle(g_hReportFile);
g_hReportFile = 0;
}
} else {
GenerateExceptionReport(pExceptionInfo);
}
SetErrorMode(fuOldErrorMode);
}
if (g_prevExceptionFilter)
return g_prevExceptionFilter(pExceptionInfo);
else
return EXCEPTION_CONTINUE_SEARCH;
}
static void OnStartup(void)
{
// Install the unhandled exception filter function
g_prevExceptionFilter = SetUnhandledExceptionFilter(TopLevelExceptionFilter);
setDumpCallback(writeReport);
if (REPORT_FILE) {
// Figure out what the report file will be named, and store it away
if(GetModuleFileName(NULL, g_szLogFileName, MAX_PATH))
{
LPTSTR lpszDot;
// Look for the '.' before the "EXE" extension. Replace the extension
// with "RPT"
if((lpszDot = _tcsrchr(g_szLogFileName, _T('.'))))
{
lpszDot++; // Advance past the '.'
_tcscpy(lpszDot, _T("RPT")); // "RPT" -> "Report"
}
else
_tcscat(g_szLogFileName, _T(".RPT"));
}
else if(GetWindowsDirectory(g_szLogFileName, MAX_PATH))
{
_tcscat(g_szLogFileName, _T("EXCHNDL.RPT"));
}
}
}
static void OnExit(void)
{
SetUnhandledExceptionFilter(g_prevExceptionFilter);
}
BOOL APIENTRY
SetLogFileNameA(const char *szLogFileName)
{
size_t size = _countof(g_szLogFileName);
if (!szLogFileName ||
_tcslen(szLogFileName) > size - 1) {
OutputDebug("EXCHNDL: specified log name is too long or invalid (%s)\n",
szLogFileName);
return FALSE;
}
_tcsncpy(g_szLogFileName, szLogFileName, size - 1);
g_szLogFileName[size - 1] = _T('\0');
return TRUE;
}
BOOL APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
OnStartup();
break;
case DLL_PROCESS_DETACH:
OnExit();
break;
}
return TRUE;
}