forked from opentibia-xx/otserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexception.cpp
476 lines (434 loc) · 13.9 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
//////////////////////////////////////////////////////////////////////
// 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 <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
#include <map>
#include "otsystem.h"
#include "exception.h"
#include <map>
#include <string>
typedef std::map<unsigned long, char*> FunctionMap;
#if defined WIN32 || defined __WINDOWS__
#include "excpt.h"
#include "tlhelp32.h"
#endif
unsigned long max_off;
unsigned long min_off;
FunctionMap functionMap;
bool maploaded = false;
OTSYS_THREAD_LOCKVAR maploadlock;
#if defined WIN32 || defined __WINDOWS__
EXCEPTION_DISPOSITION
__cdecl _SEHHandler(
struct _EXCEPTION_RECORD *ExceptionRecord,
void * EstablisherFrame,
struct _CONTEXT *ContextRecord,
void * DispatcherContext
);
void printPointer(std::ostream* output,unsigned long p);
#endif
#ifdef __GNUC__
#define COMPILER_STRING "gcc " __VERSION__
#else
#define COMPILER_STRING ""
#endif
#define COMPILATION_DATE __DATE__ " " __TIME__
ExceptionHandler::ExceptionHandler(){
installed = false;
}
ExceptionHandler::~ExceptionHandler(){
if(installed ==true)
RemoveHandler();
}
bool ExceptionHandler::InstallHandler(){
#if defined WIN32 || defined __WINDOWS__
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//__GNUC__
#endif//WIN32 || defined __WINDOWS__
installed = true;
return true;
}
bool ExceptionHandler::RemoveHandler(){
if(installed == false)
return false;
#if defined WIN32 || defined __WINDOWS__
/*
mov eax,[chain.prev]
mov fs:[0],eax
*/
#ifdef __GNUC__
__asm__ ("movl %0,%%eax;movl %%eax,%%fs:0;"::"r"(chain.prev):"%eax" );
#endif //__GNUC__
#endif //WIN32 || defined __WINDOWS__
installed = false;
return true;
}
#if defined WIN32 || defined __WINDOWS__
EXCEPTION_DISPOSITION
__cdecl _SEHHandler(
struct _EXCEPTION_RECORD *ExceptionRecord,
void * EstablisherFrame,
struct _CONTEXT *ContextRecord,
void * DispatcherContext
){
//
unsigned long *esp;
unsigned long *next_ret;
unsigned long stack_val;
unsigned long *stacklimit;
unsigned long *stackstart;
unsigned long nparameters = 0;
unsigned long file,foundRetAddress = 0;
_MEMORY_BASIC_INFORMATION mbi;
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
MEMORYSTATUS mstate;
GlobalMemoryStatus(&mstate);
*outdriver << "Memory load: " << mstate.dwMemoryLoad << std::endl <<
"Total phys: " << mstate.dwTotalPhys/1024 << " K available phys: " <<
mstate.dwAvailPhys/1024 << " K" << 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
unsigned long 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: " << (unsigned long)ExceptionRecord->ExceptionCode <<
" at eip = " << (unsigned long)ExceptionRecord->ExceptionAddress;
FunctionMap::iterator functions;
if((unsigned long)(ExceptionRecord->ExceptionAddress) >= min_off &&
(unsigned long)(ExceptionRecord->ExceptionAddress) <= max_off){
for(functions = functionMap.begin(); functions != functionMap.end(); ++functions) {
if(functions->first > (unsigned long)(ExceptionRecord->ExceptionAddress) &&
functions != functionMap.begin()) {
functions--;
*outdriver << "(" <<functions->second << ")" ;
break;
}
}
}
*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 = (unsigned long *)(ContextRecord->Esp);
VirtualQuery(esp, &mbi, sizeof(mbi));
stacklimit = (unsigned long*)((unsigned long)(mbi.BaseAddress) + mbi.RegionSize);
*outdriver << "---Stack Trace---" << std::endl;
*outdriver << "From: " << (unsigned long)esp <<
" to: " << (unsigned long)stacklimit << std::endl;
stackstart = esp;
next_ret = (unsigned long*)(ContextRecord->Ebp);
unsigned long 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 << (unsigned long)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 = (unsigned long*)*(esp - 2);
frame_param_counter = 0;
}
frame_param_counter++;
*outdriver<< std::endl;
}
if(stack_val >= min_off && stack_val <= max_off){
foundRetAddress++;
//
FunctionMap::iterator functions;
for(functions = functionMap.begin(); functions != functionMap.end(); ++functions) {
if(functions->first > stack_val && functions != functionMap.begin()) {
functions--;
*outdriver << (unsigned long)esp << " " <<
functions->second <<"(" << functions->first <<")" << std::endl;
break;
}
}
}
esp++;
}
*outdriver << "*****************************************************" << std::endl;
if(file)
((std::ofstream*)outdriver)->close();
MessageBox(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,unsigned long p){
*output << p;
if(IsBadReadPtr((void*)p,4) == 0){
*output << " -> " << *(unsigned long*)p;
}
}
#endif //WIN32 || defined __WINDOWS__
#ifdef __GNUC__
bool ExceptionHandler::LoadMap(){
if(maploaded == true){
return false;
}
installed = false;
//load map file if exists
char line[1024];
FILE* input = fopen("otserv.map", "r");
min_off = 0xFFFFFF;
max_off = 0;
long n = 0;
if(!input){
std::cout << "Failed loading symbols. otserv.map not found. " << std::endl;
std::cout << "Go to http://otfans.net/showthread.php?t=4718 for more info." << std::endl;
system("pause");
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;
unsigned long 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;
return true;
}
#else
bool ExceptionHandler::LoadMap(){
return true;
};
#endif //__GNUC__
void ExceptionHandler::dumpStack()
{
#ifndef __GNUC__
return;
#endif
unsigned long *esp;
unsigned long *next_ret;
unsigned long stack_val;
unsigned long *stacklimit;
unsigned long *stackstart;
unsigned long nparameters = 0;
unsigned long 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 = (unsigned long*)((unsigned long)(mbi.BaseAddress) + mbi.RegionSize);
output << "---Stack Trace---" << std::endl;
output << "From: " << (unsigned long)esp <<
" to: " << (unsigned long)stacklimit << std::endl;
stackstart = esp;
#ifdef __GNUC__
__asm__ ("movl %%ebp, %0;":"=r"(next_ret)::);
#else
//
#endif
unsigned long 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 << (unsigned long)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 = (unsigned long*)*(esp - 2);
frame_param_counter = 0;
}
frame_param_counter++;
output << std::endl;
}
if(stack_val >= min_off && stack_val <= max_off){
foundRetAddress++;
//
FunctionMap::iterator functions;
for(functions = functionMap.begin(); functions != functionMap.end(); ++functions) {
if(functions->first > stack_val && functions != functionMap.begin()) {
functions--;
output << (unsigned long)esp << " " <<
functions->second <<"(" << functions->first <<")" << std::endl;
break;
}
}
}
esp++;
}
output << "*****************************************************" << std::endl;
output.close();
}