Repository navigation
Expand file tree
/
Copy pathBaseException.cpp
More file actions
358 lines (314 loc) · 10.6 KB
/
Copy pathBaseException.cpp
File metadata and controls
358 lines (314 loc) · 10.6 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "BaseException.h"
#include <iomanip>
#include <string>
#include <sstream>
#include <iostream>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/log/trivial.hpp>
#include <boost/format.hpp>
#include <mutex>
static std::string g_log_folder;
static std::atomic<int> g_crash_log_count = 0;
static std::mutex g_dump_mutex;
CBaseException::CBaseException(HANDLE hProcess, WORD wPID, LPCTSTR lpSymbolPath, PEXCEPTION_POINTERS pEp):
CStackWalker(hProcess, wPID, lpSymbolPath)
{
if (NULL != pEp)
{
m_pEp = new EXCEPTION_POINTERS;
CopyMemory(m_pEp, pEp, sizeof(EXCEPTION_POINTERS));
}
output_file = new boost::nowide::ofstream();
std::time_t t = std::time(0);
std::tm* now_time = std::localtime(&t);
std::stringstream buf;
if (!g_log_folder.empty()) {
buf << std::put_time(now_time, "crash_%a_%b_%d_%H_%M_%S_") <<g_crash_log_count++ <<".log";
auto log_folder = (boost::filesystem::path(g_log_folder) / "log").make_preferred();
if (!boost::filesystem::exists(log_folder)) {
boost::filesystem::create_directory(log_folder);
}
auto crash_log_path = boost::filesystem::path(log_folder / buf.str()).make_preferred();
std::string log_filename = crash_log_path.string();
output_file->open(log_filename, std::ios::out | std::ios::app);
}
}
CBaseException::~CBaseException(void)
{
if (output_file) {
output_file->close();
delete output_file;
}
}
//BBS set crash log folder
void CBaseException::set_log_folder(std::string log_folder)
{
g_log_folder = log_folder;
}
void CBaseException::OutputString(LPCTSTR lpszFormat, ...)
{
TCHAR szBuf[2048] = _T("");
va_list args;
va_start(args, lpszFormat);
_vsntprintf_s(szBuf, 2048, lpszFormat, args);
va_end(args);
//WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), szBuf, _tcslen(szBuf), NULL, NULL);
//output it to the current directory of binary
std::string output_str = textconv_helper::T2A_(szBuf);
*output_file << output_str;
output_file->flush();
}
void CBaseException::ShowLoadModules()
{
LoadSymbol();
LPMODULE_INFO pHead = GetLoadModules();
LPMODULE_INFO pmi = pHead;
TCHAR szBuf[MAX_COMPUTERNAME_LENGTH] = _T("");
DWORD dwSize = MAX_COMPUTERNAME_LENGTH;
GetUserName(szBuf, &dwSize);
OutputString(_T("Current User:%s\r\n"), szBuf);
OutputString(_T("BaseAddress:\tSize:\tName\tPath\tSymbolPath\tVersion\r\n"));
while (NULL != pmi)
{
OutputString(_T("%08x\t%d\t%s\t%s\t%s\t%s\r\n"), (unsigned long)(pmi->ModuleAddress), pmi->dwModSize, pmi->szModuleName, pmi->szModulePath, pmi->szSymbolPath, pmi->szVersion);
pmi = pmi->pNext;
}
FreeModuleInformations(pHead);
}
void CBaseException::ShowCallstack(HANDLE hThread, const CONTEXT* context)
{
OutputString(_T("Show CallStack:\n"));
LPSTACKINFO phead = StackWalker(hThread, context);
// Show RVA of each call stack, so we can locate the symbol using pdb file
// To show the symbol, load the <szFaultingModule> in WinDBG with pdb file, then type the following commands:
// > lm which gives you the start address of each module, as well as module names
// > !dh <module name> list all module headers. Find the <virtual address> of the section given by
// the <section> output in the crash log
// > ln <module start address> + <section virtual address> + <offset> reveals the debug symbol
OutputString(_T("\nLogical Address:\n"));
TCHAR szFaultingModule[MAX_PATH];
DWORD section, offset;
for (LPSTACKINFO ps = phead; ps != nullptr; ps = ps->pNext) {
if (GetLogicalAddress((PVOID) ps->szFncAddr, szFaultingModule, sizeof(szFaultingModule), section, offset)) {
OutputString(_T("0x%X 0x%X:0x%X %s\n"), ps->szFncAddr, section, offset, szFaultingModule);
} else {
OutputString(_T("0x%X Unknown\n"), ps->szFncAddr);
}
}
FreeStackInformations(phead);
}
void CBaseException::ShowExceptionResoult(DWORD dwExceptionCode)
{
OutputString(_T("Exception Code :%08x "), dwExceptionCode);
// BBS: to be checked
#if 1
switch (dwExceptionCode)
{
case EXCEPTION_ACCESS_VIOLATION:
{
OutputString(_T("ACCESS_VIOLATION\r\n"));
}
return ;
case EXCEPTION_DATATYPE_MISALIGNMENT:
{
OutputString(_T("DATATYPE_MISALIGNMENT\r\n"));
}
return ;
case EXCEPTION_BREAKPOINT:
{
OutputString(_T("BREAKPOINT\r\n"));
}
return ;
case EXCEPTION_SINGLE_STEP:
{
OutputString(_T("SINGLE_STEP\r\n"));
}
return ;
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
{
OutputString(_T("ARRAY_BOUNDS_EXCEEDED\r\n"));
}
return ;
case EXCEPTION_FLT_DENORMAL_OPERAND:
{
OutputString(_T("FLT_DENORMAL_OPERAND\r\n"));
}
return ;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
{
OutputString(_T("FLT_DIVIDE_BY_ZERO\r\n"));
}
return ;
case EXCEPTION_FLT_INEXACT_RESULT:
{
OutputString(_T("FLT_INEXACT_RESULT\r\n"));
}
return ;
case EXCEPTION_FLT_INVALID_OPERATION:
{
OutputString(_T("FLT_INVALID_OPERATION\r\n"));
}
return ;
case EXCEPTION_FLT_OVERFLOW:
{
OutputString(_T("FLT_OVERFLOW\r\n"));
}
return ;
case EXCEPTION_FLT_STACK_CHECK:
{
OutputString(_T("STACK_CHECK\r\n"));
}
return ;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
{
OutputString(_T("INT_DIVIDE_BY_ZERO\r\n"));
}
return ;
case EXCEPTION_INVALID_HANDLE:
{
OutputString(_T("INVALID_HANDLE\r\n"));
}
return ;
case EXCEPTION_PRIV_INSTRUCTION:
{
OutputString(_T("PRIV_INSTRUCTION\r\n"));
}
return ;
case EXCEPTION_IN_PAGE_ERROR:
{
OutputString(_T("IN_PAGE_ERROR\r\n"));
}
return ;
case EXCEPTION_ILLEGAL_INSTRUCTION:
{
OutputString(_T("ILLEGAL_INSTRUCTION\r\n"));
}
return ;
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
{
OutputString(_T("NONCONTINUABLE_EXCEPTION\r\n"));
}
return ;
case EXCEPTION_STACK_OVERFLOW:
{
OutputString(_T("STACK_OVERFLOW\r\n"));
}
return ;
case EXCEPTION_INVALID_DISPOSITION:
{
OutputString(_T("INVALID_DISPOSITION\r\n"));
}
return ;
case EXCEPTION_FLT_UNDERFLOW:
{
OutputString(_T("FLT_UNDERFLOW\r\n"));
}
return ;
case EXCEPTION_INT_OVERFLOW:
{
OutputString(_T("INT_OVERFLOW\r\n"));
}
return ;
}
TCHAR szBuffer[512] = { 0 };
FormatMessage( FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE,
GetModuleHandle( _T("NTDLL.DLL") ),
dwExceptionCode, 0, szBuffer, sizeof( szBuffer ), 0 );
OutputString(_T("%s"), szBuffer);
OutputString(_T("\r\n"));
#endif
}
LONG WINAPI CBaseException::UnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo )
{
if (pExceptionInfo->ExceptionRecord->ExceptionCode < 0x80000000
//BBS: Load project on computers with SDC may trigger this exception (in ShowModal()),
// It's not fatal and should be ignored, or there will be lots of meaningless crash logs
|| pExceptionInfo->ExceptionRecord->ExceptionCode==0xe0434352)
//BBS: ignore the exception when copy preset
//|| pExceptionInfo->ExceptionRecord->ExceptionCode==0xe06d7363)
{
//BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << boost::format(": got an ExceptionCode %1%, skip it!") % pExceptionInfo->ExceptionRecord->ExceptionCode;
return EXCEPTION_CONTINUE_SEARCH;
}
g_dump_mutex.lock();
CBaseException base(GetCurrentProcess(), GetCurrentProcessId(), NULL, pExceptionInfo);
base.ShowExceptionInformation();
g_dump_mutex.unlock();
return EXCEPTION_CONTINUE_SEARCH;
}
LONG WINAPI CBaseException::UnhandledExceptionFilter2(PEXCEPTION_POINTERS pExceptionInfo )
{
CBaseException base(GetCurrentProcess(), GetCurrentProcessId(), NULL, pExceptionInfo);
base.ShowExceptionInformation();
return EXCEPTION_CONTINUE_SEARCH;
}
BOOL CBaseException::GetLogicalAddress(
PVOID addr, PTSTR szModule, DWORD len, DWORD& section, DWORD& offset )
{
MEMORY_BASIC_INFORMATION mbi;
if ( !VirtualQuery( addr, &mbi, sizeof(mbi) ) )
return FALSE;
DWORD_PTR hMod = (DWORD_PTR)mbi.AllocationBase;
if ( !GetModuleFileName( (HMODULE)hMod, szModule, len ) )
return FALSE;
if (!hMod)
return FALSE;
PIMAGE_DOS_HEADER pDosHdr = (PIMAGE_DOS_HEADER)hMod;
PIMAGE_NT_HEADERS pNtHdr = (PIMAGE_NT_HEADERS)(hMod + pDosHdr->e_lfanew);
PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION( pNtHdr );
DWORD_PTR rva = (DWORD_PTR)addr - hMod;
for (unsigned i = 0; i < pNtHdr->FileHeader.NumberOfSections; i++, pSection++ )
{
DWORD sectionStart = pSection->VirtualAddress;
DWORD sectionEnd = sectionStart + max(pSection->SizeOfRawData, pSection->Misc.VirtualSize);
if ( (rva >= sectionStart) && (rva <= sectionEnd) )
{
section = i+1;
offset = rva - sectionStart;
return TRUE;
}
}
return FALSE; // Should never get here!
}
void CBaseException::ShowRegistorInformation(PCONTEXT pCtx)
{
#ifdef _M_IX86 // Intel Only!
OutputString( _T("\nRegisters:\r\n") );
OutputString(_T("EAX:%08X\r\nEBX:%08X\r\nECX:%08X\r\nEDX:%08X\r\nESI:%08X\r\nEDI:%08X\r\n"),
pCtx->Eax, pCtx->Ebx, pCtx->Ecx, pCtx->Edx, pCtx->Esi, pCtx->Edi );
OutputString( _T("CS:EIP:%04X:%08X\r\n"), pCtx->SegCs, pCtx->Eip );
OutputString( _T("SS:ESP:%04X:%08X EBP:%08X\r\n"),pCtx->SegSs, pCtx->Esp, pCtx->Ebp );
OutputString( _T("DS:%04X ES:%04X FS:%04X GS:%04X\r\n"), pCtx->SegDs, pCtx->SegEs, pCtx->SegFs, pCtx->SegGs );
OutputString( _T("Flags:%08X\r\n"), pCtx->EFlags );
#endif
OutputString( _T("\r\n") );
}
void CBaseException::STF(unsigned int ui, PEXCEPTION_POINTERS pEp)
{
CBaseException base(GetCurrentProcess(), GetCurrentProcessId(), NULL, pEp);
throw base;
}
void CBaseException::ShowExceptionInformation()
{
OutputString(_T("Exceptions:\r\n"));
ShowExceptionResoult(m_pEp->ExceptionRecord->ExceptionCode);
OutputString(_T("Exception Flag :0x%x "), m_pEp->ExceptionRecord->ExceptionFlags);
OutputString(_T("NumberParameters :%ld \n"), m_pEp->ExceptionRecord->NumberParameters);
for (int i = 0; i < m_pEp->ExceptionRecord->NumberParameters; i++)
{
OutputString(_T("Param %d :0x%x \n"), i, m_pEp->ExceptionRecord->ExceptionInformation[i]);
}
OutputString(_T("Context :%p \n"), m_pEp->ContextRecord);
#if defined(_M_ARM64)
OutputString(_T("ContextFlag : 0x%x \n"), m_pEp->ContextRecord->ContextFlags);
#else
OutputString(_T("ContextFlag : 0x%x, EFlags: 0x%x \n"), m_pEp->ContextRecord->ContextFlags, m_pEp->ContextRecord->EFlags);
#endif
TCHAR szFaultingModule[MAX_PATH];
DWORD section, offset;
GetLogicalAddress(m_pEp->ExceptionRecord->ExceptionAddress, szFaultingModule, sizeof(szFaultingModule), section, offset );
OutputString( _T("Fault address: 0x%X 0x%X:0x%X %s\r\n"), m_pEp->ExceptionRecord->ExceptionAddress, section, offset, szFaultingModule );
ShowRegistorInformation(m_pEp->ContextRecord);
ShowCallstack(GetCurrentThread(), m_pEp->ContextRecord);
}