-
Notifications
You must be signed in to change notification settings - Fork 21
/
exception.cpp
507 lines (452 loc) · 13.4 KB
/
exception.cpp
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
// Exception Handler class
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#include "otpch.h"
#ifdef __EXCEPTION_TRACER__
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
#include <map>
#include "otsystem.h"
#include "exception.h"
#include "game.h"
typedef std::map<uint32_t, char*> FunctionMap;
#ifdef WIN32
#include <excpt.h>
#include <tlhelp32.h>
#endif
extern Game g_game;
uint32_t max_off;
uint32_t min_off;
FunctionMap functionMap;
bool maploaded = false;
OTSYS_THREAD_LOCKVAR maploadlock;
#ifdef WIN32
EXCEPTION_DISPOSITION
__cdecl _SEHHandler(
struct _EXCEPTION_RECORD *ExceptionRecord,
void * EstablisherFrame,
struct _CONTEXT *ContextRecord,
void * DispatcherContext
);
void printPointer(std::ostream* output,uint32_t p);
#endif
#ifndef COMPILER_STRING
#ifdef __GNUC__
#define COMPILER_STRING "gcc " __VERSION__
#else
#define COMPILER_STRING ""
#endif
#endif
#define COMPILATION_DATE __DATE__ " " __TIME__
ExceptionHandler::ExceptionHandler()
{
installed = false;
}
ExceptionHandler::~ExceptionHandler()
{
if(installed == true)
RemoveHandler();
}
bool ExceptionHandler::InstallHandler()
{
#ifdef WIN32
OTSYS_THREAD_LOCK_CLASS lockObj(maploadlock);
if(maploaded == false)
LoadMap();
if(installed == true)
return false;
/*
mov eax,fs:[0]
mov [prevSEH],eax
mov [chain].prev,eax
mov [chain].SEHfunction,_SEHHandler
lea eax,[chain]
mov fs:[0],eax
*/
#ifdef __GNUC__
SEHChain *prevSEH;
__asm__ ("movl %%fs:0,%%eax;movl %%eax,%0;":"=r"(prevSEH)::"%eax" );
chain.prev = prevSEH;
chain.SEHfunction = (void*)&_SEHHandler;
__asm__("movl %0,%%eax;movl %%eax,%%fs:0;": : "g" (&chain):"%eax");
#endif
#endif
installed = true;
return true;
}
bool ExceptionHandler::RemoveHandler()
{
if(installed == false)
return false;
#ifdef WIN32
#ifdef __GNUC__
__asm__ ("movl %0,%%eax;movl %%eax,%%fs:0;"::"r"(chain.prev):"%eax" );
#endif
#endif
installed = false;
return true;
}
char* getFunctionName(unsigned long addr, unsigned long& start)
{
FunctionMap::iterator functions;
if(addr >= min_off && addr <= max_off)
{
for(functions = functionMap.begin(); functions != functionMap.end(); ++functions)
{
if(functions->first > addr && functions != functionMap.begin())
{
functions--;
start = functions->first;
return functions->second;
break;
}
}
}
return NULL;
}
#ifdef WIN32
EXCEPTION_DISPOSITION
__cdecl _SEHHandler(
struct _EXCEPTION_RECORD *ExceptionRecord,
void * EstablisherFrame,
struct _CONTEXT *ContextRecord,
void * DispatcherContext
){
//
uint32_t *esp;
uint32_t *next_ret;
uint32_t stack_val;
uint32_t *stacklimit;
uint32_t *stackstart;
uint32_t nparameters = 0;
uint32_t file,foundRetAddress = 0;
_MEMORY_BASIC_INFORMATION mbi;
g_game.saveGameState();
std::ostream *outdriver;
std::cout << "Error: generating report file..." << std::endl;
std::ofstream output("report.txt", std::ios_base::app);
if(output.fail())
{
outdriver = &std::cout;
file = false;
}
else
{
file = true;
outdriver = &output;
}
time_t rawtime;
time(&rawtime);
*outdriver << "*****************************************************" << std::endl;
*outdriver << "Error report - " << std::ctime(&rawtime) << std::endl;
*outdriver << "Compiler info - " << COMPILER_STRING << std::endl;
*outdriver << "Compilation Date - " << COMPILATION_DATE << std::endl << std::endl;
//system and process info
//- global memory information
MEMORYSTATUSEX mstate;
mstate.dwLength = sizeof(mstate);
if(GlobalMemoryStatusEx(&mstate))
{
*outdriver << "Memory load: " << mstate.dwMemoryLoad << std::endl <<
"Total phys: " << mstate.ullTotalPhys/1024 << " K available phys: " <<
mstate.ullAvailPhys/1024 << " K" << std::endl;
}
else
*outdriver << "Memory load: Error" << std::endl;
//-process info
FILETIME FTcreation,FTexit,FTkernel,FTuser;
SYSTEMTIME systemtime;
GetProcessTimes(GetCurrentProcess(),&FTcreation,&FTexit,&FTkernel,&FTuser);
// creation time
FileTimeToSystemTime(&FTcreation,&systemtime);
*outdriver << "Start time: " << systemtime.wDay << "-" <<
systemtime.wMonth << "-" << systemtime.wYear << " " <<
systemtime.wHour << ":" << systemtime.wMinute << ":" <<
systemtime.wSecond << std::endl;
// kernel time
uint32_t miliseconds;
miliseconds = FTkernel.dwHighDateTime * 429497 + FTkernel.dwLowDateTime / 10000;
*outdriver << "Kernel time: " << miliseconds / 3600000;
miliseconds = miliseconds - (miliseconds / 3600000)* 3600000;
*outdriver << ":" << miliseconds / 60000;
miliseconds = miliseconds - (miliseconds / 60000)* 60000;
*outdriver << ":" << miliseconds / 1000;
miliseconds = miliseconds - (miliseconds / 1000) * 1000;
*outdriver << "." << miliseconds << std::endl;
// user time
miliseconds = FTuser.dwHighDateTime * 429497 + FTuser.dwLowDateTime / 10000;
*outdriver << "User time: " << miliseconds / 3600000;
miliseconds = miliseconds - (miliseconds / 3600000) * 3600000;
*outdriver << ":" << miliseconds / 60000;
miliseconds = miliseconds - (miliseconds / 60000) * 60000;
*outdriver << ":" << miliseconds / 1000;
miliseconds = miliseconds - (miliseconds / 1000) * 1000;
*outdriver << "." << miliseconds << std::endl;
// n threads
PROCESSENTRY32 uProcess;
HANDLE lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
BOOL r;
if(lSnapShot != 0)
{
uProcess.dwSize = sizeof(uProcess);
r = Process32First(lSnapShot, &uProcess);
while(r)
{
if(uProcess.th32ProcessID == GetCurrentProcessId())
{
*outdriver << "Threads: " << uProcess.cntThreads << std::endl;
break;
}
r = Process32Next(lSnapShot, &uProcess);
}
CloseHandle (lSnapShot);
}
*outdriver << std::endl;
//exception header type and eip
outdriver->flags(std::ios::hex | std::ios::showbase);
*outdriver << "Exception: " << (uint32_t)ExceptionRecord->ExceptionCode <<
" at eip = " << (uint32_t)ExceptionRecord->ExceptionAddress;
FunctionMap::iterator functions;
unsigned long functionAddr;
char* functionName = getFunctionName((unsigned long)ExceptionRecord->ExceptionAddress, functionAddr);
if(functionName)
*outdriver << "(" << functionName << " - " << functionAddr << ")";
*outdriver << std::endl ;
//registers
*outdriver << "eax = ";printPointer(outdriver,ContextRecord->Eax);*outdriver << std::endl;
*outdriver << "ebx = ";printPointer(outdriver,ContextRecord->Ebx);*outdriver << std::endl;
*outdriver << "ecx = ";printPointer(outdriver,ContextRecord->Ecx);*outdriver << std::endl;
*outdriver << "edx = ";printPointer(outdriver,ContextRecord->Edx);*outdriver << std::endl;
*outdriver << "esi = ";printPointer(outdriver,ContextRecord->Esi);*outdriver << std::endl;
*outdriver << "edi = ";printPointer(outdriver,ContextRecord->Edi);*outdriver << std::endl;
*outdriver << "ebp = ";printPointer(outdriver,ContextRecord->Ebp);*outdriver << std::endl;
*outdriver << "esp = ";printPointer(outdriver,ContextRecord->Esp);*outdriver << std::endl;
*outdriver << "efl = " << ContextRecord->EFlags << std::endl;
*outdriver << std::endl;
//stack dump
esp = (uint32_t *)(ContextRecord->Esp);
VirtualQuery(esp, &mbi, sizeof(mbi));
stacklimit = (uint32_t*)((uint32_t)(mbi.BaseAddress) + mbi.RegionSize);
*outdriver << "---Stack Trace---" << std::endl;
*outdriver << "From: " << (uint32_t)esp <<
" to: " << (uint32_t)stacklimit << std::endl;
stackstart = esp;
next_ret = (uint32_t*)(ContextRecord->Ebp);
uint32_t frame_param_counter;
frame_param_counter = 0;
while(esp < stacklimit)
{
stack_val = *esp;
if(foundRetAddress)
nparameters++;
if(esp - stackstart < 20 || nparameters < 10 || std::abs(esp - next_ret) < 10 || frame_param_counter < 8)
{
*outdriver << (uint32_t)esp << " | ";
printPointer(outdriver,stack_val);
if(esp == next_ret)
*outdriver << " \\\\\\\\\\\\ stack frame //////";
else if(esp - next_ret == 1)
*outdriver << " <-- ret" ;
else if(esp - next_ret == 2)
{
next_ret = (uint32_t*)*(esp - 2);
frame_param_counter = 0;
}
frame_param_counter++;
*outdriver<< std::endl;
}
if(stack_val >= min_off && stack_val <= max_off)
{
foundRetAddress++;
//
unsigned long functionAddr;
char* functionName = getFunctionName(stack_val, functionAddr);
output << (unsigned long)esp << " " << functionName << "(" <<
functionAddr << ")" << std::endl;
}
esp++;
}
*outdriver << "*****************************************************" << std::endl;
if(file)
((std::ofstream*)outdriver)->close();
MessageBoxA(NULL, "Please send the file report.txt to support service ;). Thanks", "Error", MB_OK | MB_ICONERROR);
std::cout << "Error report generated. Killing server." << std::endl;
exit(1); //force exit
return ExceptionContinueSearch;
}
void printPointer(std::ostream* output,uint32_t p)
{
*output << p;
if(IsBadReadPtr((void*)p,4) == 0)
*output << " -> " << *(uint32_t*)p;
}
#endif
bool ExceptionHandler::LoadMap()
{
#ifdef __GNUC__
if(maploaded == true)
return false;
functionMap.clear();
installed = false;
//load map file if exists
char line[1024];
FILE* input = fopen("forgottenserver.map", "r");
min_off = 0xFFFFFF;
max_off = 0;
int32_t n = 0;
if(!input)
{
std::cout << "Failed loading symbols. forgottenserver.map not found. " << std::endl;
exit(1);
return false;
}
//read until found .text 0x00401000
while(fgets(line, 1024, input))
{
if(memcmp(line,".text",5) == 0)
break;
}
if(feof(input))
return false;
char tofind[] = "0x";
char lib[] = ".a(";
while(fgets(line, 1024, input))
{
char* pos = strstr(line, lib);
if(pos)
break; //not load libs
pos = strstr(line, tofind);
if(pos)
{
//read hex offset
char hexnumber[12];
strncpy(hexnumber, pos, 10);
hexnumber[10] = 0;
char* pEnd;
uint32_t offset = strtol(hexnumber, &pEnd, 0);
if(offset)
{
//read function name
char* pos2 = pos + 12;
while(*pos2 != 0)
{
if(*pos2 != ' ')
break;
pos2++;
}
if(*pos2 == 0 || (*pos2 == '0' && *(pos2+1) == 'x'))
continue;
char* name = new char[strlen(pos2)+1];
strcpy(name, pos2);
name[strlen(pos2) - 1] = 0;
functionMap[offset] = name;
if(offset > max_off)
max_off = offset;
if(offset < min_off)
min_off = offset;
n++;
}
}
}
// close file
fclose(input);
//std::cout << "Loaded " << n << " stack symbols" <<std::endl;
maploaded = true;
#endif
return true;
}
void ExceptionHandler::dumpStack()
{
#ifndef __GNUC__
return;
#endif
uint32_t *esp;
uint32_t *next_ret;
uint32_t stack_val;
uint32_t *stacklimit;
uint32_t *stackstart;
uint32_t nparameters = 0;
uint32_t foundRetAddress = 0;
_MEMORY_BASIC_INFORMATION mbi;
std::cout << "Error: generating report file..." << std::endl;
std::ofstream output("report.txt",std::ios_base::app);
output.flags(std::ios::hex | std::ios::showbase);
time_t rawtime;
time(&rawtime);
output << "*****************************************************" << std::endl;
output << "Stack dump - " << std::ctime(&rawtime) << std::endl;
output << "Compiler info - " << COMPILER_STRING << std::endl;
output << "Compilation Date - " << COMPILATION_DATE << std::endl << std::endl;
#ifdef __GNUC__
__asm__ ("movl %%esp, %0;":"=r"(esp)::);
#else
//
#endif
VirtualQuery(esp, &mbi, sizeof(mbi));
stacklimit = (uint32_t*)((uint32_t)(mbi.BaseAddress) + mbi.RegionSize);
output << "---Stack Trace---" << std::endl;
output << "From: " << (uint32_t)esp <<
" to: " << (uint32_t)stacklimit << std::endl;
stackstart = esp;
#ifdef __GNUC__
__asm__ ("movl %%ebp, %0;":"=r"(next_ret)::);
#else
//
#endif
uint32_t frame_param_counter;
frame_param_counter = 0;
while(esp < stacklimit)
{
stack_val = *esp;
if(foundRetAddress)
nparameters++;
if(esp - stackstart < 20 || nparameters < 10 || std::abs(esp - next_ret) < 10 || frame_param_counter < 8)
{
output << (uint32_t)esp << " | ";
printPointer(&output, stack_val);
if(esp == next_ret)
output << " \\\\\\\\\\\\ stack frame //////";
else if(esp - next_ret == 1)
output << " <-- ret" ;
else if(esp - next_ret == 2)
{
next_ret = (uint32_t*)*(esp - 2);
frame_param_counter = 0;
}
frame_param_counter++;
output << std::endl;
}
if(stack_val >= min_off && stack_val <= max_off)
{
foundRetAddress++;
//
unsigned long functionAddr;
char* functionName = getFunctionName(stack_val, functionAddr);
output << (unsigned long)esp << " " << functionName << "(" <<
functionAddr << ")" << std::endl;
}
esp++;
}
output << "*****************************************************" << std::endl;
output.close();
}
#endif