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 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
|
/*
This file is part of Warzone 2100.
Copyright (C) 1997-XXXX José Fonseca <j_r_fonseca@yahoo.co.uk>
* Originally based on Matt Pietrek's MSJEXHND.CPP in Microsoft Systems Journal, April 1997.
Copyright (C) 2008 Giel van Schijndel
Copyright (C) 2008-2020 Warzone 2100 Project
Warzone 2100 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.
Warzone 2100 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 Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if (_WIN32_WINNT < 0x0500) // must force win 2k or higher
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif
#include "lib/framework/frame.h"
#include "dumpinfo.h"
#include "exchndl.h"
#include <assert.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <shlobj.h>
#include <shlwapi.h>
#if !defined(WZ_CC_MSVC)
#define HAVE_BFD 1
#endif
#define WSTRING(x) CONCAT(L, x)
// Declare the static variables
static wchar_t szLogFileName[MAX_PATH] = L"";
static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL;
static HANDLE hReportFile;
static char *formattedVersionString = NULL;
static
int __cdecl rprintf(const TCHAR *format, ...)
{
TCHAR szBuff[4096];
int retValue;
DWORD cbWritten;
va_list argptr;
va_start(argptr, format);
retValue = wvsprintf(szBuff, format, argptr);
va_end(argptr);
WriteFile(hReportFile, szBuff, retValue * sizeof(TCHAR), &cbWritten, 0);
return retValue;
}
// The GetModuleBase function retrieves the base address of the module that contains the specified address.
static
DWORD GetModuleBase(DWORD dwAddress)
{
MEMORY_BASIC_INFORMATION Buffer;
return VirtualQuery((LPCVOID) dwAddress, &Buffer, sizeof(Buffer)) ? (DWORD) Buffer.AllocationBase : 0;
}
#ifdef HAVE_BFD
#include <bfd.h>
extern "C"
{
#include "include/demangle.h"
// cross compiler does not like these...
#if !defined(WZ_CC_MINGW)
#include "include/coff/internal.h"
#include "include/libcoff.h"
#endif
}
// Read in the symbol table.
static bfd_boolean
slurp_symtab(bfd *abfd, asymbol ***syms, long *symcount)
{
long storage;
if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
{
return FALSE;
}
storage = bfd_get_symtab_upper_bound(abfd);
if (storage < 0)
{
return FALSE;
}
*syms = (asymbol **) GlobalAlloc(GMEM_FIXED, storage);
if (*syms == NULL)
{
return FALSE;
}
if ((*symcount = bfd_canonicalize_symtab(abfd, *syms)) < 0)
{
return FALSE;
}
return TRUE;
}
// This structure is used to pass information between translate_addresses and find_address_in_section.
struct find_handle
{
asymbol **syms;
bfd_vma pc;
const char *filename;
const char *functionname;
unsigned int line;
bfd_boolean found;
};
// Look for an address in a section. This is called via bfd_map_over_sections.
static void find_address_in_section(bfd *abfd, asection *section, PTR data)
{
struct find_handle *info = (struct find_handle *) data;
bfd_vma vma;
bfd_size_type size;
if (info->found)
{
return;
}
if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
{
return;
}
vma = bfd_get_section_vma(abfd, section);
size = bfd_get_section_size(section);
if (info->pc < (vma = bfd_get_section_vma(abfd, section)))
{
return;
}
if (info->pc >= vma + (size = bfd_get_section_size(section)))
{
return;
}
info->found = bfd_find_nearest_line(abfd, section, info->syms, info->pc - vma, &info->filename, &info->functionname, &info->line);
}
static
bool BfdDemangleSymName(LPCTSTR lpName, LPTSTR lpDemangledName, DWORD nSize)
{
char *res;
assert(lpName != NULL);
if ((res = cplus_demangle(lpName, DMGL_ANSI /*| DMGL_PARAMS*/)) == NULL)
{
lstrcpyn(lpDemangledName, lpName, nSize);
return FALSE;
}
else
{
lstrcpyn(lpDemangledName, res, nSize);
free(res);
return TRUE;
}
}
static
bool BfdGetSymFromAddr(bfd *abfd, asymbol **syms, long symcount, DWORD dwAddress, LPTSTR lpSymName, DWORD nSize)
{
HMODULE hModule;
struct find_handle info;
if (!(hModule = (HMODULE) GetModuleBase(dwAddress)))
{
return FALSE;
}
info.pc = dwAddress;
if (!(bfd_get_file_flags(abfd) & HAS_SYMS) || !symcount)
{
return FALSE;
}
info.syms = syms;
info.found = FALSE;
bfd_map_over_sections(abfd, find_address_in_section, (PTR) &info);
if (info.found == FALSE || info.line == 0)
{
return FALSE;
}
assert(lpSymName);
if (info.functionname == NULL && *info.functionname == '\0')
{
return FALSE;
}
lstrcpyn(lpSymName, info.functionname, nSize);
return TRUE;
}
static
bool BfdGetLineFromAddr(bfd *abfd, asymbol **syms, long symcount, DWORD dwAddress, LPTSTR lpFileName, DWORD nSize, LPDWORD lpLineNumber)
{
HMODULE hModule;
struct find_handle info;
if (!(hModule = (HMODULE) GetModuleBase(dwAddress)))
{
return FALSE;
}
info.pc = dwAddress;
if (!(bfd_get_file_flags(abfd) & HAS_SYMS) || !symcount)
{
return FALSE;
}
info.syms = syms;
info.found = FALSE;
bfd_map_over_sections(abfd, find_address_in_section, (PTR) &info);
if (info.found == FALSE || info.line == 0)
{
return FALSE;
}
assert(lpFileName && lpLineNumber);
lstrcpyn(lpFileName, info.filename, nSize);
*lpLineNumber = info.line;
return TRUE;
}
#endif /* HAVE_BFD */
#include <imagehlp.h>
static bool bSymInitialized = FALSE;
static HMODULE hModule_Imagehlp = NULL;
typedef bool (WINAPI *PFNSYMINITIALIZE)(HANDLE, LPSTR, bool);
static PFNSYMINITIALIZE pfnSymInitialize = NULL;
static
bool WINAPI j_SymInitialize(HANDLE hProcess, PSTR UserSearchPath, bool fInvadeProcess)
{
if (
(hModule_Imagehlp || (hModule_Imagehlp = LoadLibrary(_T("IMAGEHLP.DLL")))) &&
(pfnSymInitialize || (pfnSymInitialize = (PFNSYMINITIALIZE) GetProcAddress(hModule_Imagehlp, "SymInitialize")))
)
{
return pfnSymInitialize(hProcess, UserSearchPath, fInvadeProcess);
}
else
{
return FALSE;
}
}
typedef bool (WINAPI *PFNSYMCLEANUP)(HANDLE);
static PFNSYMCLEANUP pfnSymCleanup = NULL;
static
bool WINAPI j_SymCleanup(HANDLE hProcess)
{
if (
(hModule_Imagehlp || (hModule_Imagehlp = LoadLibrary(_T("IMAGEHLP.DLL")))) &&
(pfnSymCleanup || (pfnSymCleanup = (PFNSYMCLEANUP) GetProcAddress(hModule_Imagehlp, "SymCleanup")))
)
{
return pfnSymCleanup(hProcess);
}
else
{
return FALSE;
}
}
typedef DWORD (WINAPI *PFNSYMSETOPTIONS)(DWORD);
static PFNSYMSETOPTIONS pfnSymSetOptions = NULL;
static
DWORD WINAPI j_SymSetOptions(DWORD SymOptions)
{
if (
(hModule_Imagehlp || (hModule_Imagehlp = LoadLibrary(_T("IMAGEHLP.DLL")))) &&
(pfnSymSetOptions || (pfnSymSetOptions = (PFNSYMSETOPTIONS) GetProcAddress(hModule_Imagehlp, "SymSetOptions")))
)
{
return pfnSymSetOptions(SymOptions);
}
else
{
return FALSE;
}
}
typedef bool (WINAPI *PFNSYMUNDNAME)(PIMAGEHLP_SYMBOL, PSTR, DWORD);
static PFNSYMUNDNAME pfnSymUnDName = NULL;
static
bool WINAPI j_SymUnDName(PIMAGEHLP_SYMBOL Symbol, PSTR UnDecName, DWORD UnDecNameLength)
{
if (
(hModule_Imagehlp || (hModule_Imagehlp = LoadLibrary(_T("IMAGEHLP.DLL")))) &&
(pfnSymUnDName || (pfnSymUnDName = (PFNSYMUNDNAME) GetProcAddress(hModule_Imagehlp, "SymUnDName")))
)
{
return pfnSymUnDName(Symbol, UnDecName, UnDecNameLength);
}
else
{
return FALSE;
}
}
typedef PFUNCTION_TABLE_ACCESS_ROUTINE PFNSYMFUNCTIONTABLEACCESS;
static PFNSYMFUNCTIONTABLEACCESS pfnSymFunctionTableAccess = NULL;
static
PVOID WINAPI j_SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
{
if (
(hModule_Imagehlp || (hModule_Imagehlp = LoadLibrary(_T("IMAGEHLP.DLL")))) &&
(pfnSymFunctionTableAccess || (pfnSymFunctionTableAccess = (PFNSYMFUNCTIONTABLEACCESS) GetProcAddress(hModule_Imagehlp, "SymFunctionTableAccess")))
)
{
return pfnSymFunctionTableAccess(hProcess, AddrBase);
}
else
{
return NULL;
}
}
typedef PGET_MODULE_BASE_ROUTINE PFNSYMGETMODULEBASE;
static PFNSYMGETMODULEBASE pfnSymGetModuleBase = NULL;
static
DWORD WINAPI j_SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
{
if (
(hModule_Imagehlp || (hModule_Imagehlp = LoadLibrary(_T("IMAGEHLP.DLL")))) &&
(pfnSymGetModuleBase || (pfnSymGetModuleBase = (PFNSYMGETMODULEBASE) GetProcAddress(hModule_Imagehlp, "SymGetModuleBase")))
)
{
return pfnSymGetModuleBase(hProcess, dwAddr);
}
else
{
return 0;
}
}
typedef bool (WINAPI *PFNSTACKWALK)(DWORD, HANDLE, HANDLE, LPSTACKFRAME, LPVOID, PREAD_PROCESS_MEMORY_ROUTINE, PFUNCTION_TABLE_ACCESS_ROUTINE, PGET_MODULE_BASE_ROUTINE, PTRANSLATE_ADDRESS_ROUTINE);
static PFNSTACKWALK pfnStackWalk = NULL;
static
bool WINAPI j_StackWalk(
DWORD MachineType,
HANDLE hProcess,
HANDLE hThread,
LPSTACKFRAME StackFrame,
PVOID ContextRecord,
PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
)
{
if (
(hModule_Imagehlp || (hModule_Imagehlp = LoadLibrary(_T("IMAGEHLP.DLL")))) &&
(pfnStackWalk || (pfnStackWalk = (PFNSTACKWALK) GetProcAddress(hModule_Imagehlp, "StackWalk")))
)
return pfnStackWalk(
MachineType,
hProcess,
hThread,
StackFrame,
ContextRecord,
ReadMemoryRoutine,
FunctionTableAccessRoutine,
GetModuleBaseRoutine,
TranslateAddress
);
else
{
return FALSE;
}
}
typedef bool (WINAPI *PFNSYMGETSYMFROMADDR)(HANDLE, DWORD, LPDWORD, PIMAGEHLP_SYMBOL);
static PFNSYMGETSYMFROMADDR pfnSymGetSymFromAddr = NULL;
static
bool WINAPI j_SymGetSymFromAddr(HANDLE hProcess, DWORD Address, PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
{
if (
(hModule_Imagehlp || (hModule_Imagehlp = LoadLibrary(_T("IMAGEHLP.DLL")))) &&
(pfnSymGetSymFromAddr || (pfnSymGetSymFromAddr = (PFNSYMGETSYMFROMADDR) GetProcAddress(hModule_Imagehlp, "SymGetSymFromAddr")))
)
{
return pfnSymGetSymFromAddr(hProcess, Address, Displacement, Symbol);
}
else
{
return FALSE;
}
}
typedef bool (WINAPI *PFNSYMGETLINEFROMADDR)(HANDLE, DWORD, LPDWORD, PIMAGEHLP_LINE);
static PFNSYMGETLINEFROMADDR pfnSymGetLineFromAddr = NULL;
static
bool WINAPI j_SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr, PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
{
if (
(hModule_Imagehlp || (hModule_Imagehlp = LoadLibrary(_T("IMAGEHLP.DLL")))) &&
(pfnSymGetLineFromAddr || (pfnSymGetLineFromAddr = (PFNSYMGETLINEFROMADDR) GetProcAddress(hModule_Imagehlp, "SymGetLineFromAddr")))
)
{
return pfnSymGetLineFromAddr(hProcess, dwAddr, pdwDisplacement, Line);
}
else
{
return FALSE;
}
}
static
bool ImagehlpDemangleSymName(LPCTSTR lpName, LPTSTR lpDemangledName, DWORD nSize)
{
BYTE symbolBuffer[sizeof(IMAGEHLP_SYMBOL) + 512];
PIMAGEHLP_SYMBOL pSymbol = (PIMAGEHLP_SYMBOL) symbolBuffer;
memset(symbolBuffer, 0, sizeof(symbolBuffer));
pSymbol->SizeOfStruct = sizeof(symbolBuffer);
pSymbol->MaxNameLength = 512;
lstrcpyn((LPTSTR)pSymbol->Name, lpName, pSymbol->MaxNameLength);
if (!j_SymUnDName(pSymbol, (PSTR)lpDemangledName, nSize))
{
return FALSE;
}
return TRUE;
}
static
bool ImagehlpGetSymFromAddr(HANDLE hProcess, DWORD dwAddress, LPTSTR lpSymName, DWORD nSize)
{
// IMAGEHLP is wacky, and requires you to pass in a pointer to a
// IMAGEHLP_SYMBOL structure. The problem is that this structure is
// variable length. That is, you determine how big the structure is
// at runtime. This means that you can't use sizeof(struct).
// So...make a buffer that's big enough, and make a pointer
// to the buffer. We also need to initialize not one, but TWO
// members of the structure before it can be used.
BYTE symbolBuffer[sizeof(IMAGEHLP_SYMBOL) + 512];
PIMAGEHLP_SYMBOL pSymbol = (PIMAGEHLP_SYMBOL) symbolBuffer;
DWORD dwDisplacement = 0; // Displacement of the input address, relative to the start of the symbol
pSymbol->SizeOfStruct = sizeof(symbolBuffer);
pSymbol->MaxNameLength = 512;
assert(bSymInitialized);
if (!j_SymGetSymFromAddr(hProcess, dwAddress, &dwDisplacement, pSymbol))
{
return FALSE;
}
lstrcpyn(lpSymName, (LPCTSTR)pSymbol->Name, nSize);
return TRUE;
}
static
bool ImagehlpGetLineFromAddr(HANDLE hProcess, DWORD dwAddress, LPTSTR lpFileName, DWORD nSize, LPDWORD lpLineNumber)
{
IMAGEHLP_LINE Line;
DWORD dwDisplacement = 0; // Displacement of the input address, relative to the start of the symbol
// Do the source and line lookup.
memset(&Line, 0, sizeof(IMAGEHLP_LINE));
Line.SizeOfStruct = sizeof(IMAGEHLP_LINE);
assert(bSymInitialized);
#if 1
{
// The problem is that the symbol engine only finds those source
// line addresses (after the first lookup) that fall exactly on
// a zero displacement. I will walk backwards 100 bytes to
// find the line and return the proper displacement.
DWORD dwTempDisp = 0 ;
while (dwTempDisp < 100 && !j_SymGetLineFromAddr(hProcess, dwAddress - dwTempDisp, &dwDisplacement, &Line))
{
++dwTempDisp;
}
if (dwTempDisp >= 100)
{
return FALSE;
}
// It was found and the source line information is correct so
// change the displacement if it was looked up multiple times.
if (dwTempDisp < 100 && dwTempDisp != 0)
{
dwDisplacement = dwTempDisp;
}
}
#else
if (!j_SymGetLineFromAddr(hProcess, dwAddress, &dwDisplacement, &Line))
{
return FALSE;
}
#endif
assert(lpFileName && lpLineNumber);
lstrcpyn(lpFileName, (LPCTSTR)Line.FileName, nSize);
*lpLineNumber = Line.LineNumber;
return TRUE;
}
static
bool PEGetSymFromAddr(HANDLE hProcess, DWORD dwAddress, LPTSTR lpSymName, DWORD nSize)
{
HMODULE hModule;
PIMAGE_NT_HEADERS pNtHdr;
IMAGE_NT_HEADERS NtHdr;
PIMAGE_SECTION_HEADER pSection;
DWORD dwNearestAddress = 0, dwNearestName;
int i;
if (!(hModule = (HMODULE) GetModuleBase(dwAddress)))
{
return FALSE;
}
{
PIMAGE_DOS_HEADER pDosHdr;
LONG e_lfanew;
// Point to the DOS header in memory
pDosHdr = (PIMAGE_DOS_HEADER)hModule;
// From the DOS header, find the NT (PE) header
if (!ReadProcessMemory(hProcess, &pDosHdr->e_lfanew, &e_lfanew, sizeof(e_lfanew), NULL))
{
return FALSE;
}
pNtHdr = (PIMAGE_NT_HEADERS)((DWORD)hModule + (DWORD)e_lfanew);
if (!ReadProcessMemory(hProcess, pNtHdr, &NtHdr, sizeof(IMAGE_NT_HEADERS), NULL))
{
return FALSE;
}
}
pSection = (PIMAGE_SECTION_HEADER)((DWORD)pNtHdr + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + NtHdr.FileHeader.SizeOfOptionalHeader);
// Look for export section
for (i = 0; i < NtHdr.FileHeader.NumberOfSections; i++, pSection++)
{
IMAGE_SECTION_HEADER Section;
PIMAGE_EXPORT_DIRECTORY pExportDir = NULL;
BYTE ExportSectionName[IMAGE_SIZEOF_SHORT_NAME] = {'.', 'e', 'd', 'a', 't', 'a', '\0', '\0'};
if (!ReadProcessMemory(hProcess, pSection, &Section, sizeof(IMAGE_SECTION_HEADER), NULL))
{
return FALSE;
}
if (memcmp(Section.Name, ExportSectionName, IMAGE_SIZEOF_SHORT_NAME) == 0)
{
pExportDir = (PIMAGE_EXPORT_DIRECTORY) Section.VirtualAddress;
}
else if ((NtHdr.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress >= Section.VirtualAddress) && (NtHdr.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress < (Section.VirtualAddress + Section.SizeOfRawData)))
{
pExportDir = (PIMAGE_EXPORT_DIRECTORY) NtHdr.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
}
if (pExportDir)
{
IMAGE_EXPORT_DIRECTORY ExportDir;
if (!ReadProcessMemory(hProcess, (PVOID)((DWORD)hModule + (DWORD)pExportDir), &ExportDir, sizeof(IMAGE_EXPORT_DIRECTORY), NULL))
{
return FALSE;
}
{
PDWORD *AddressOfFunctions = (PDWORD *)alloca(ExportDir.NumberOfFunctions * sizeof(PDWORD));
int j;
if (!ReadProcessMemory(hProcess, (PVOID)((DWORD)hModule + (DWORD)ExportDir.AddressOfFunctions), AddressOfFunctions, ExportDir.NumberOfFunctions * sizeof(PDWORD), NULL))
{
return FALSE;
}
for (j = 0; j < ExportDir.NumberOfNames; ++j)
{
DWORD pFunction = (DWORD)hModule + (DWORD)AddressOfFunctions[j];
//ReadProcessMemory(hProcess, (DWORD) hModule + (DWORD) (&ExportDir.AddressOfFunctions[j]), &pFunction, sizeof(pFunction), NULL);
if (pFunction <= dwAddress && pFunction > dwNearestAddress)
{
dwNearestAddress = pFunction;
if (!ReadProcessMemory(hProcess, (PVOID)((DWORD)hModule + (DWORD)(&ExportDir.AddressOfNames)[j]), &dwNearestName, sizeof(dwNearestName), NULL))
{
return FALSE;
}
dwNearestName = (DWORD) hModule + dwNearestName;
}
}
}
}
}
if (!dwNearestAddress)
{
return FALSE;
}
if (!ReadProcessMemory(hProcess, (PVOID)dwNearestName, lpSymName, nSize, NULL))
{
return FALSE;
}
lpSymName[nSize - 1] = 0;
return TRUE;
}
// Cross platform compatibility.
// If you change this, make sure it doesn't break the cross compile or the native compile.
// Don't ask why this is needed, have no idea.
struct ItDoesntMatterIfItsADWORDOrAVoidPointer_JustCompileTheDamnThing
{
ItDoesntMatterIfItsADWORDOrAVoidPointer_JustCompileTheDamnThing(void *whatever) : whatever(whatever) {}
operator void *() const
{
return whatever; // Oooh, look compiler1, I'm a void *, just what you wanted!
}
operator DWORD() const
{
return (DWORD)whatever; // Oooh, look compiler2, I'm a DWORD, just what you wanted!
}
void *whatever;
};
static
bool WINAPI IntelStackWalk(
DWORD MachineType,
HANDLE hProcess,
WZ_DECL_UNUSED HANDLE hThread,
LPSTACKFRAME StackFrame,
PCONTEXT ContextRecord,
PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
WZ_DECL_UNUSED PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
WZ_DECL_UNUSED PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
WZ_DECL_UNUSED PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
)
{
assert(MachineType == IMAGE_FILE_MACHINE_I386);
if (ReadMemoryRoutine == NULL)
{
ReadMemoryRoutine = (PREAD_PROCESS_MEMORY_ROUTINE)ReadProcessMemory;
}
if (!StackFrame->Reserved[0])
{
StackFrame->Reserved[0] = 1;
StackFrame->AddrPC.Mode = AddrModeFlat;
StackFrame->AddrPC.Offset = ContextRecord->Eip;
StackFrame->AddrStack.Mode = AddrModeFlat;
StackFrame->AddrStack.Offset = ContextRecord->Esp;
StackFrame->AddrFrame.Mode = AddrModeFlat;
StackFrame->AddrFrame.Offset = ContextRecord->Ebp;
StackFrame->AddrReturn.Mode = AddrModeFlat;
// Error 26 error C2664: 'bool (HANDLE,DWORD,PVOID,DWORD,PDWORD)' :
// cannot convert parameter 2 from 'void *' to 'DWORD'
// c:\warzone\lib\exceptionhandler\exchndl.cpp 599
// ../../../../lib/exceptionhandler/exchndl.cpp: In function ‘bool IntelStackWalk(DWORD, void*, void*, _tagSTACKFRAME*, CONTEXT*, bool (*)(void*, const void*, void*, DWORD, DWORD*), void* (*)(void*, DWORD), DWORD (*)(void*, DWORD), DWORD (*)(void*, void*, _tagADDRESS*))’:
// ../../../../lib/exceptionhandler/exchndl.cpp:599: error: invalid conversion from ‘long unsigned int’ to ‘const void*’
if (!ReadMemoryRoutine((HANDLE)hProcess, ItDoesntMatterIfItsADWORDOrAVoidPointer_JustCompileTheDamnThing((void *)(StackFrame->AddrFrame.Offset + sizeof(DWORD))), (void *)&StackFrame->AddrReturn.Offset, sizeof(DWORD), NULL))
{
return FALSE;
}
}
else
{
StackFrame->AddrPC.Offset = StackFrame->AddrReturn.Offset;
//AddrStack = AddrFrame + 2*sizeof(DWORD);
if (!ReadMemoryRoutine((HANDLE)hProcess, ItDoesntMatterIfItsADWORDOrAVoidPointer_JustCompileTheDamnThing((void *) StackFrame->AddrFrame.Offset), (void *)&StackFrame->AddrFrame.Offset, sizeof(DWORD), NULL))
{
return FALSE;
}
if (!ReadMemoryRoutine((HANDLE)hProcess, ItDoesntMatterIfItsADWORDOrAVoidPointer_JustCompileTheDamnThing((void *)(StackFrame->AddrFrame.Offset + sizeof(DWORD))), (void *)&StackFrame->AddrReturn.Offset, sizeof(DWORD), NULL))
{
return FALSE;
}
}
ReadMemoryRoutine((HANDLE)hProcess, ItDoesntMatterIfItsADWORDOrAVoidPointer_JustCompileTheDamnThing((void *)(StackFrame->AddrFrame.Offset + 2 * sizeof(DWORD))), (void *)StackFrame->Params, sizeof(StackFrame->Params), NULL);
return TRUE;
}
static
bool StackBackTrace(HANDLE hProcess, HANDLE hThread, PCONTEXT pContext)
{
STACKFRAME StackFrame;
HMODULE hModule = NULL;
TCHAR szModule[MAX_PATH];
#ifdef HAVE_BFD
bfd *abfd = NULL;
asymbol **syms = NULL; // The symbol table.
long symcount = 0; // Number of symbols in `syms'.
#endif /* HAVE_BFD */
assert(!bSymInitialized);
j_SymSetOptions(/* SYMOPT_UNDNAME | */ SYMOPT_LOAD_LINES);
if (j_SymInitialize(hProcess, NULL, TRUE))
{
bSymInitialized = TRUE;
}
memset(&StackFrame, 0, sizeof(StackFrame));
// Initialize the STACKFRAME structure for the first call. This is only
// necessary for Intel CPUs, and isn't mentioned in the documentation.
StackFrame.AddrPC.Offset = pContext->Eip;
StackFrame.AddrPC.Mode = AddrModeFlat;
StackFrame.AddrStack.Offset = pContext->Esp;
StackFrame.AddrStack.Mode = AddrModeFlat;
StackFrame.AddrFrame.Offset = pContext->Ebp;
StackFrame.AddrFrame.Mode = AddrModeFlat;
rprintf(_T("Call stack:\r\n"));
if (0)
{
rprintf(_T("AddrPC AddrReturn AddrFrame AddrStack\r\n"));
}
while (1)
{
bool bSuccess = FALSE;
#ifdef HAVE_BFD
const HMODULE hPrevModule = hModule;
#endif /* HAVE_BFD */
TCHAR szSymName[512] = _T("");
TCHAR szFileName[MAX_PATH] = _T("");
DWORD LineNumber = 0;
if (bSymInitialized)
{
if (!j_StackWalk(
IMAGE_FILE_MACHINE_I386,
hProcess,
hThread,
&StackFrame,
pContext,
NULL,
j_SymFunctionTableAccess,
j_SymGetModuleBase,
NULL
)
)
{
break;
}
}
else
{
if (!IntelStackWalk(
IMAGE_FILE_MACHINE_I386,
hProcess,
hThread,
&StackFrame,
pContext,
NULL,
NULL,
NULL,
NULL
)
)
{
break;
}
}
// Basic sanity check to make sure the frame is OK. Bail if not.
if (0 == StackFrame.AddrFrame.Offset)
{
break;
}
if (0)
{
rprintf(
_T("%08lX %08lX %08lX %08lX\r\n"),
StackFrame.AddrPC.Offset,
StackFrame.AddrReturn.Offset,
StackFrame.AddrFrame.Offset,
StackFrame.AddrStack.Offset
);
rprintf(
_T("%08lX %08lX %08lX %08lX\r\n"),
StackFrame.Params[0],
StackFrame.Params[1],
StackFrame.Params[2],
StackFrame.Params[3]
);
}
rprintf(_T("%08lX"), StackFrame.AddrPC.Offset);
if ((hModule = (HMODULE) GetModuleBase(StackFrame.AddrPC.Offset)) && GetModuleFileName(hModule, szModule, sizeof(szModule)))
{
#ifndef HAVE_BFD
rprintf(_T(" %s:ModulBase %08lX"), szModule, hModule);
#else /* HAVE_BFD */
rprintf(_T(" %s:%08lX"), szModule, StackFrame.AddrPC.Offset);
if (hModule != hPrevModule)
{
if (syms)
{
GlobalFree(syms);
syms = NULL;
symcount = 0;
}
if (abfd)
{
bfd_close(abfd);
}
if ((abfd = bfd_openr(szModule, NULL)))
if (bfd_check_format(abfd, bfd_object))
if (bfd_get_file_flags(abfd) & HAS_SYMS)
/* Read in the symbol table. */
{
slurp_symtab(abfd, &syms, &symcount);
}
}
if (!bSuccess && abfd && syms && symcount)
if ((bSuccess = BfdGetSymFromAddr(abfd, syms, symcount, StackFrame.AddrPC.Offset, szSymName, 512)))
{
/*
framepointer = StackFrame.AddrFrame.Offset;
hprocess = hProcess;
*/
BfdDemangleSymName(szSymName, szSymName, 512);
rprintf(_T(" %s"), szSymName);
if (BfdGetLineFromAddr(abfd, syms, symcount, StackFrame.AddrPC.Offset, szFileName, MAX_PATH, &LineNumber))
{
rprintf(_T(" %s:%ld"), szFileName, LineNumber);
}
}
#endif /* HAVE_BFD */
if (!bSuccess && bSymInitialized)
if ((bSuccess = ImagehlpGetSymFromAddr(hProcess, StackFrame.AddrPC.Offset, szSymName, 512)))
{
rprintf(_T(" %s"), szSymName);
ImagehlpDemangleSymName(szSymName, szSymName, 512);
if (ImagehlpGetLineFromAddr(hProcess, StackFrame.AddrPC.Offset, szFileName, MAX_PATH, &LineNumber))
{
rprintf(_T(" %s:%ld"), szFileName, LineNumber);
}
}
if (!bSuccess)
if ((bSuccess = PEGetSymFromAddr(hProcess, StackFrame.AddrPC.Offset, szSymName, 512)))
{
rprintf(_T(" %s"), szSymName);
}
}
rprintf(_T("\r\n"));
}
#ifdef HAVE_BFD
if (syms)
{
GlobalFree(syms);
syms = NULL;
symcount = 0;
}
if (abfd)
{
bfd_close(abfd);
}
#endif /* HAVE_BFD */
if (bSymInitialized)
{
if (!j_SymCleanup(hProcess))
{
assert(0);
}
bSymInitialized = FALSE;
}
return TRUE;
}
static
void GenerateExceptionReport(PEXCEPTION_POINTERS pExceptionInfo)
{
PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord;
TCHAR szModule[MAX_PATH];
HMODULE hModule;
PCONTEXT pContext;
// Start out with a banner
rprintf(_T("-------------------\r\n\r\n"));
{
const TCHAR *lpDayOfWeek[] =
{
_T("Sunday"),
_T("Monday"),
_T("Tuesday"),
_T("Wednesday"),
_T("Thursday"),
_T("Friday"),
_T("Saturday")
};
const TCHAR *lpMonth[] =
{
NULL,
_T("January"),
_T("February"),
_T("March"),
_T("April"),
_T("May"),
_T("June"),
_T("July"),
_T("August"),
_T("September"),
_T("October"),
_T("November"),
_T("December")
};
SYSTEMTIME SystemTime;
GetLocalTime(&SystemTime);
rprintf(_T("Error occurred on %s, %s %i, %i at %02i:%02i:%02i.\r\n\r\n"),
lpDayOfWeek[SystemTime.wDayOfWeek],
lpMonth[SystemTime.wMonth],
SystemTime.wDay,
SystemTime.wYear,
SystemTime.wHour,
SystemTime.wMinute,
SystemTime.wSecond
);
}
// Dump a generic info header
dbgDumpHeader(hReportFile);
// First print information about the type of fault
rprintf(_T("\r\n%s caused "), GetModuleFileName(NULL, szModule, MAX_PATH) ? szModule : _T("Application"));
switch (pExceptionRecord->ExceptionCode)
{
case EXCEPTION_ACCESS_VIOLATION:
rprintf(_T("an Access Violation"));
break;
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
rprintf(_T("an Array Bound Exceeded"));
break;
case EXCEPTION_BREAKPOINT:
rprintf(_T("a Breakpoint"));
break;
case EXCEPTION_DATATYPE_MISALIGNMENT:
rprintf(_T("a Datatype Misalignment"));
break;
case EXCEPTION_FLT_DENORMAL_OPERAND:
rprintf(_T("a Float Denormal Operand"));
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
rprintf(_T("a Float Divide By Zero"));
break;
case EXCEPTION_FLT_INEXACT_RESULT:
rprintf(_T("a Float Inexact Result"));
break;
case EXCEPTION_FLT_INVALID_OPERATION:
rprintf(_T("a Float Invalid Operation"));
break;
case EXCEPTION_FLT_OVERFLOW:
rprintf(_T("a Float Overflow"));
break;
case EXCEPTION_FLT_STACK_CHECK:
rprintf(_T("a Float Stack Check"));
break;
case EXCEPTION_FLT_UNDERFLOW:
rprintf(_T("a Float Underflow"));
break;
case EXCEPTION_GUARD_PAGE:
rprintf(_T("a Guard Page"));
break;
case EXCEPTION_ILLEGAL_INSTRUCTION:
rprintf(_T("an Illegal Instruction"));
break;
case EXCEPTION_IN_PAGE_ERROR:
rprintf(_T("an In Page Error"));
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
rprintf(_T("an Integer Divide By Zero"));
break;
case EXCEPTION_INT_OVERFLOW:
rprintf(_T("an Integer Overflow"));
break;
case EXCEPTION_INVALID_DISPOSITION:
rprintf(_T("an Invalid Disposition"));
break;
case EXCEPTION_INVALID_HANDLE:
rprintf(_T("an Invalid Handle"));
break;
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
rprintf(_T("a Noncontinuable Exception"));
break;
case EXCEPTION_PRIV_INSTRUCTION:
rprintf(_T("a Privileged Instruction"));
break;
case EXCEPTION_SINGLE_STEP:
rprintf(_T("a Single Step"));
break;
case EXCEPTION_STACK_OVERFLOW:
rprintf(_T("a Stack Overflow"));
break;
case DBG_CONTROL_C:
rprintf(_T("a Control+C"));
break;
case DBG_CONTROL_BREAK:
rprintf(_T("a Control+Break"));
break;
case DBG_TERMINATE_THREAD:
rprintf(_T("a Terminate Thread"));
break;
case DBG_TERMINATE_PROCESS:
rprintf(_T("a Terminate Process"));
break;
case RPC_S_UNKNOWN_IF:
rprintf(_T("an Unknown Interface"));
break;
case RPC_S_SERVER_UNAVAILABLE:
rprintf(_T("a Server Unavailable"));
break;
default:
/*
static TCHAR szBuffer[512] = { 0 };
// If not one of the "known" exceptions, try to get the string
// from NTDLL.DLL's message table.
FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE,
GetModuleHandle(_T("NTDLL.DLL")),
dwCode, 0, szBuffer, sizeof(szBuffer), 0);
*/
rprintf(_T("an Unknown [0x%lX] Exception"), pExceptionRecord->ExceptionCode);
break;
}
// Now print information about where the fault occurred
rprintf(_T(" at location %08x"), (DWORD) pExceptionRecord->ExceptionAddress);
if ((hModule = (HMODULE) GetModuleBase((DWORD) pExceptionRecord->ExceptionAddress)) && GetModuleFileName(hModule, szModule, sizeof(szModule)))
{
rprintf(_T(" in module %s"), szModule);
}
// If the exception was an access violation, print out some additional information, to the error log and the debugger.
if (pExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && pExceptionRecord->NumberParameters >= 2)
{
rprintf(_T(" %s location %08x"), pExceptionRecord->ExceptionInformation[0] ? "Writing to" : "Reading from", pExceptionRecord->ExceptionInformation[1]);
}
rprintf(_T(".\r\n\r\n"));
dbgDumpLog(hReportFile);
pContext = pExceptionInfo->ContextRecord;
#ifdef _M_IX86 // Intel Only!
// Show the registers
rprintf(_T("Registers:\r\n"));
if (pContext->ContextFlags & CONTEXT_INTEGER)
rprintf(
_T("eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\r\n"),
pContext->Eax,
pContext->Ebx,
pContext->Ecx,
pContext->Edx,
pContext->Esi,
pContext->Edi
);
if (pContext->ContextFlags & CONTEXT_CONTROL)
rprintf(
_T("eip=%08lx esp=%08lx ebp=%08lx iopl=%1lx %s %s %s %s %s %s %s %s %s %s\r\n"),
pContext->Eip,
pContext->Esp,
pContext->Ebp,
(pContext->EFlags >> 12) & 3, // IOPL level value
pContext->EFlags & 0x00100000 ? "vip" : " ", // VIP (virtual interrupt pending)
pContext->EFlags & 0x00080000 ? "vif" : " ", // VIF (virtual interrupt flag)
pContext->EFlags & 0x00000800 ? "ov" : "nv", // VIF (virtual interrupt flag)
pContext->EFlags & 0x00000400 ? "dn" : "up", // OF (overflow flag)
pContext->EFlags & 0x00000200 ? "ei" : "di", // IF (interrupt enable flag)
pContext->EFlags & 0x00000080 ? "ng" : "pl", // SF (sign flag)
pContext->EFlags & 0x00000040 ? "zr" : "nz", // ZF (zero flag)
pContext->EFlags & 0x00000010 ? "ac" : "na", // AF (aux carry flag)
pContext->EFlags & 0x00000004 ? "po" : "pe", // PF (parity flag)
pContext->EFlags & 0x00000001 ? "cy" : "nc" // CF (carry flag)
);
if (pContext->ContextFlags & CONTEXT_SEGMENTS)
{
rprintf(
_T("cs=%04lx ss=%04lx ds=%04lx es=%04lx fs=%04lx gs=%04lx"),
pContext->SegCs,
pContext->SegSs,
pContext->SegDs,
pContext->SegEs,
pContext->SegFs,
pContext->SegGs,
pContext->EFlags
);
if (pContext->ContextFlags & CONTEXT_CONTROL)
rprintf(
_T(" efl=%08lx"),
pContext->EFlags
);
}
else if (pContext->ContextFlags & CONTEXT_CONTROL)
rprintf(
_T(" efl=%08lx"),
pContext->EFlags
);
rprintf(_T("\r\n\r\n"));
#endif
// FIXME: We *never* return from the below call!
StackBackTrace(GetCurrentProcess(), GetCurrentThread(), pContext);
rprintf(_T("\r\n\r\n"));
}
#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);
hReportFile = CreateFileW(
szLogFileName,
GENERIC_WRITE,
0,
0,
OPEN_ALWAYS,
FILE_FLAG_WRITE_THROUGH,
0
);
if (hReportFile == INVALID_HANDLE_VALUE)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
DWORD dw = GetLastError();
TCHAR szBuffer[4196];
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL);
wsprintf(szBuffer, _T("Exception handler failed with error %d: %s\n"), dw, lpMsgBuf);
MessageBox((HWND)MB_ICONEXCLAMATION, szBuffer, _T("Error"), MB_OK);
LocalFree(lpMsgBuf);
debug(LOG_ERROR, "Exception handler failed to create file!");
}
#ifdef HAVE_BFD
bfd_set_error_handler((bfd_error_handler_type) rprintf);
#endif /* HAVE_BFD */
if (hReportFile)
{
wchar_t szBuffer[4196];
int err;
SetFilePointer(hReportFile, 0, 0, FILE_END);
// FIXME: We don't return from the below function call
GenerateExceptionReport(pExceptionInfo);
CloseHandle(hReportFile);
wsprintfW(szBuffer, L"Warzone has crashed.\r\nSee %s for more details\r\n", szLogFileName);
err = MessageBoxW((HWND)MB_ICONERROR, szBuffer, L"Warzone Crashed!", MB_OK | MB_ICONERROR);
if (err == 0)
{
LPVOID lpMsgBuf;
DWORD dw = GetLastError();
wchar_t szBuffer[4196];
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR) &lpMsgBuf,
0, NULL);
wsprintfW(szBuffer, L"Exception handler failed with error %d: %s\n", dw, lpMsgBuf);
MessageBoxW((HWND)MB_ICONEXCLAMATION, szBuffer, L"Error", MB_OK);
LocalFree(lpMsgBuf);
debug(LOG_ERROR, "Exception handler failed to create file!");
}
hReportFile = 0;
}
SetErrorMode(fuOldErrorMode);
}
if (prevExceptionFilter)
{
return prevExceptionFilter(pExceptionInfo);
}
else
{
return EXCEPTION_CONTINUE_SEARCH;
}
}
void ExchndlSetup(const char *packageVersion, const std::string &writeDir, bool portable_mode)
{
# if defined(WZ_CC_MINGW)
wchar_t miniDumpPath[PATH_MAX] = {'\0'};
DWORD dwRetVal = 0;
#ifdef HAVE_BFD
bfd_init();
#endif /* HAVE_BFD */
// Install the unhandled exception filter function
prevExceptionFilter = SetUnhandledExceptionFilter(TopLevelExceptionFilter);
// Retrieve the current version
formattedVersionString = strdup(packageVersion);
// Convert the writeDir to WSTRING (UTF-16)
int wcharsRequired = MultiByteToWideChar(CP_UTF8, 0, writeDir.c_str(), -1, NULL, 0);
if (wcharsRequired == 0)
{
MessageBox((HWND)MB_ICONEXCLAMATION, "Failed to convert writeDir string to wide chars!\nProgram will now exit." , _T("Error"), MB_OK);
exit(1);
}
wchar_t wchar_writeDir[wcharsRequired];
if(MultiByteToWideChar(CP_UTF8, 0, writeDir.c_str(), -1, wchar_writeDir, wcharsRequired) == 0)
{
MessageBox((HWND)MB_ICONEXCLAMATION, "Failed to convert writeDir string to wide chars! (2)\nProgram will now exit." , _T("Error"), MB_OK);
exit(1);
}
// Because of UAC on vista / win7 we use this to write our dumps to (unless we override it via OverrideRPTDirectory())
// NOTE: CSIDL_PERSONAL = C:\Users\user name\Documents
if (!portable_mode && SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, miniDumpPath)))
{
PathAppendW(miniDumpPath, wchar_writeDir);
PathAppendW(miniDumpPath, L"\\logs");
if (!PathFileExistsW(miniDumpPath))
{
if (ERROR_SUCCESS != SHCreateDirectoryExW(NULL, miniDumpPath, NULL))
{
//last attempt to get a path
dwRetVal = GetTempPathW(PATH_MAX, miniDumpPath);
if (dwRetVal > MAX_PATH || (dwRetVal == 0))
{
MessageBox((HWND)MB_ICONEXCLAMATION, "Could not created a temporary directory!\nProgram will now exit." , _T("Error"), MB_OK);
exit(1);
}
}
}
}
// in portable mode, we use where they installed it on, since this is a removeable drive (most likely), we will use where the program is located in.
else if (portable_mode && ((dwRetVal = GetCurrentDirectoryW(MAX_PATH, miniDumpPath))))
{
if (dwRetVal > MAX_PATH)
{
MessageBox((HWND)MB_ICONEXCLAMATION, "Buffer exceeds maximum path to create directory. Exiting.", _T("Error"), MB_OK);
exit(1);
}
PathAppendW(miniDumpPath, wchar_writeDir);
PathAppendW(miniDumpPath, L"\\logs");
}
else
{
// should never fail, but if it does, we fall back to this
dwRetVal = GetTempPathW(PATH_MAX, miniDumpPath);
if (dwRetVal > MAX_PATH || (dwRetVal == 0))
{
MessageBox((HWND)MB_ICONEXCLAMATION, "Could not created a temporary directory!\nProgram will now exit." , _T("Error!"), MB_OK);
exit(1);
}
}
wcscat(szLogFileName, L"Warzone2100.RPT");
wcscat(miniDumpPath, L"\\");
wcscat(miniDumpPath, szLogFileName);
wcscpy(szLogFileName, miniDumpPath);
atexit(ExchndlShutdown);
#endif
}
void ResetRPTDirectory(wchar_t *newPath)
{
wchar_t miniDumpPath[PATH_MAX] = { '\0' };
size_t newPathLen = wcslen(newPath);
ASSERT(newPathLen < PATH_MAX, "ResetRPTDirectory: length of newPath exceeds maximum allowed");
wcscpy(miniDumpPath, newPath);
ASSERT((newPathLen + wcslen(L"\\logs\\Warzone2100.RPT")) < PATH_MAX, "ResetRPTDirectory: insufficient buffer length");
wcscat(miniDumpPath, L"\\logs\\Warzone2100.RPT");
wcscpy(szLogFileName, miniDumpPath);
}
void ExchndlShutdown(void)
{
if (prevExceptionFilter)
{
SetUnhandledExceptionFilter(prevExceptionFilter);
}
prevExceptionFilter = NULL;
free(formattedVersionString);
formattedVersionString = NULL;
}
|