-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcept.c
More file actions
1258 lines (1044 loc) · 42.6 KB
/
except.c
File metadata and controls
1258 lines (1044 loc) · 42.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
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
//***************************************************************************
// NARS2000 -- Exception Handling
//***************************************************************************
/***************************************************************************
NARS2000 -- An Experimental APL Interpreter
Copyright (C) 2006-2016 Sudley Place Software
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 3 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, see <http://www.gnu.org/licenses/>.
***************************************************************************/
#define STRICT
#include <windows.h>
#pragma pack(push,4)
#include <dbghelp.h>
#pragma pack(pop)
#define REAL_MPIFNS
#include "headers.h"
#undef REAL_MPIFNS
#include "startaddr.h"
// Save area for exception address if EXCEPTION_BREAKPOINT
APLU3264 gExceptAddr; // Exception address
LPWCHAR glpExceptionText; // Ptr to Exception text
LPUCHAR glpInvalidAddr; // Ptr to invalid address
EXCEPTION_CODES gExceptionCode;
// Save area for crash information
CONTEXT gContextRecord;
#ifndef PROTO
#ifdef _WIN64
#define ADDR_MASK 0xFFFFFFFFFFFFF000
EXCEPTION_RECORD64 gExceptionRecord;
#elif defined (_WIN32)
#define ADDR_MASK 0x00000000FFFFF000
EXCEPTION_RECORD32 gExceptionRecord;
#else
#error Need code for this architecture.
#endif
#endif
#define STACKWALK_MAX_NAMELEN 1024
typedef struct tagCallstackEntry
{
DWORD64 offset; // If 0, we have no valid entry
CHAR name[STACKWALK_MAX_NAMELEN];
CHAR undName[STACKWALK_MAX_NAMELEN];
CHAR undFullName[STACKWALK_MAX_NAMELEN];
DWORD64 offsetFromSmybol;
DWORD offsetFromLine;
DWORD lineNumber;
CHAR lineFileName[STACKWALK_MAX_NAMELEN];
DWORD symType;
LPCSTR symTypeString;
CHAR moduleName[STACKWALK_MAX_NAMELEN];
DWORD64 baseOfImage;
CHAR loadedImageName[STACKWALK_MAX_NAMELEN];
} CallstackEntry;
enum CallstackEntryType
{
firstEntry,
nextEntry,
lastEntry
};
typedef struct tagIMAGEHLP_MODULE64_V2
{
DWORD SizeOfStruct; // Set to sizeof(IMAGEHLP_MODULE64)
DWORD64 BaseOfImage; // Base load address of module
DWORD ImageSize; // Virtual size of the loaded module
DWORD TimeDateStamp; // Date/time stamp from pe header
DWORD CheckSum; // Checksum from the pe header
DWORD NumSyms; // Number of symbols in the symbol table
SYM_TYPE SymType; // Type of symbols loaded
CHAR ModuleName[32]; // Module name
CHAR ImageName[256]; // Image name
CHAR LoadedImageName[256]; // Symbol file name
} IMAGEHLP_MODULE64_V2;
//***************************************************************************
// $MyGetExceptionCode
//
// Return the current ExceptionCode
//***************************************************************************
EXCEPTION_CODES MyGetExceptionCode
(void)
{
// Return the ExceptionCode
return gExceptionCode;
} // End MyGetExceptionCode
//***************************************************************************
// $MyGetExceptionStr
//
// Return the current ExceptionCode as a string
//***************************************************************************
LPWSTR MyGetExceptionStr
(EXCEPTION_CODES exceptCode)
{
static WCHAR wszTemp[256];
// Split cases based upon the exception code
switch (exceptCode)
{
case EXCEPTION_ACCESS_VIOLATION:
return L"EXCEPTION_ACCESS_VIOLATION";
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
return L"EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
case EXCEPTION_BREAKPOINT:
return L"EXCEPTION_BREAKPOINT";
case EXCEPTION_DATATYPE_MISALIGNMENT:
return L"EXCEPTION_DATATYPE_MISALIGNMENT";
case EXCEPTION_FLT_DENORMAL_OPERAND:
return L"EXCEPTION_FLT_DENORMAL_OPERAND";
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
return L"EXCEPTION_FLT_DIVIDE_BY_ZERO";
case EXCEPTION_FLT_INEXACT_RESULT:
return L"EXCEPTION_FLT_INEXACT_RESULT";
case EXCEPTION_FLT_INVALID_OPERATION:
return L"EXCEPTION_FLT_INVALID_OPERATION";
case EXCEPTION_FLT_OVERFLOW:
return L"EXCEPTION_FLT_OVERFLOW";
case EXCEPTION_FLT_STACK_CHECK:
return L"EXCEPTION_FLT_STACK_CHECK";
case EXCEPTION_FLT_UNDERFLOW:
return L"EXCEPTION_FLT_UNDERFLOW";
case EXCEPTION_GUARD_PAGE:
return L"EXCEPTION_GUARD_PAGE";
case EXCEPTION_ILLEGAL_INSTRUCTION:
return L"EXCEPTION_ILLEGAL_INSTRUCTION";
case EXCEPTION_IN_PAGE_ERROR:
return L"EXCEPTION_IN_PAGE_ERROR";
case EXCEPTION_INT_DIVIDE_BY_ZERO:
return L"EXCEPTION_INT_DIVIDE_BY_ZERO";
case EXCEPTION_INT_OVERFLOW:
return L"EXCEPTION_INT_OVERFLOW";
case EXCEPTION_INVALID_DISPOSITION:
return L"EXCEPTION_INVALID_DISPOSITION";
case EXCEPTION_INVALID_HANDLE:
return L"EXCEPTION_INVALID_HANDLE";
case EXCEPTION_PRIV_INSTRUCTION:
return L"EXCEPTION_PRIV_INSTRUCTION";
case EXCEPTION_SUCCESS:
return L"EXCEPTION_SUCCESS";
case EXCEPTION_RESULT_FLOAT:
return L"EXCEPTION_RESULT_FLOAT";
case EXCEPTION_RESULT_RAT:
return L"EXCEPTION_RESULT_RAT";
case EXCEPTION_RESULT_VFP:
return L"EXCEPTION_RESULT_VFP";
case EXCEPTION_DOMAIN_ERROR:
return L"EXCEPTION_DOMAIN_ERROR";
case EXCEPTION_LIMIT_ERROR:
return L"EXCEPTION_LIMIT_ERROR";
case EXCEPTION_NONCE_ERROR:
return L"EXCEPTION_NONCE_ERROR";
case EXCEPTION_WS_FULL:
return L"EXCEPTION_WS_FULL";
case EXCEPTION_CTRL_BREAK:
return L"EXCEPTION_CTRL_BREAK";
case EXCEPTION_SINGLE_STEP:
return L"EXCEPTION_SINGLE_STEP";
case EXCEPTION_STACK_OVERFLOW:
return L"EXCEPTION_STACK_OVERFLOW";
case STATUS_UNWIND_CONSOLIDATE:
return L"STATUS_UNWIND_CONSOLIDATE";
default:
MySprintfW (wszTemp,
sizeof (wszTemp),
L"*** Unknown Exception Code: %u",
gExceptionCode);
return wszTemp;
} // End SWITCH
} // End MyGetExceptionStr
//***************************************************************************
// $MySetExceptionCode
//
// Set the current ExceptionCode
//***************************************************************************
void MySetExceptionCode
(EXCEPTION_CODES ExceptionCode) // Exception code
{
// Set the ExceptionCode
gExceptionCode = ExceptionCode;
} // End MySetExceptionCode
//***************************************************************************
// $CheckVirtAlloc
//
// Check on virtual allocs
//***************************************************************************
long CheckVirtAlloc
(LPEXCEPTION_POINTERS lpExcept, // Ptr to exception & context records
LPWCHAR lpText) // Ptr to text of exception handler
{
LPUCHAR lpInvalidAddr; // Ptr to invalid address
int iRet; // Return code
// Handle access violations only
if (lpExcept->ExceptionRecord->ExceptionCode EQ EXCEPTION_ACCESS_VIOLATION)
{
// Get the invalid address
#ifdef _WIN64
lpInvalidAddr = (LPUCHAR) ((PEXCEPTION_RECORD64) lpExcept->ExceptionRecord)->ExceptionInformation[1];
#elif defined (_WIN32)
lpInvalidAddr = (LPUCHAR) ((PEXCEPTION_RECORD32) lpExcept->ExceptionRecord)->ExceptionInformation[1];
#else
#error Need code for this architecture.
#endif
// Check on virtual allocs from <memVirtStr>
iRet = CheckMemVirtStr (lpInvalidAddr);
if (iRet)
return iRet;
// Check on virtual allocs in the <lpMemPTD->lpLstMVS> chain
iRet = CheckPTDVirtStr (lpInvalidAddr);
if (iRet)
return iRet;
} // End IF
return EXCEPTION_CONTINUE_SEARCH;
} // End CheckVirtAlloc
//***************************************************************************
// $CheckPTDVirtStr
//
// Check on virtual allocs in the <lpMemPTD->lpLstMVS> chain
//***************************************************************************
int CheckPTDVirtStr
(LPUCHAR lpInvalidAddr) // Ptr to invalid address
{
LPPERTABDATA lpMemPTD; // Ptr to PerTabData global memory
LPMEMVIRTSTR lpLstMVS; // Ptr to last MEMVIRTSTR (NULL = none)
LPUCHAR lpIniAddr; // Ptr to invalid address
// Get ptr to PerTabData global memory
lpMemPTD = TlsGetValue (dwTlsPerTabData); // Assert (IsValidPtr (lpMemPTD, sizeof (lpMemPTD)));
// If lpMemPTD isn't set, just exit
if (lpMemPTD EQ NULL)
return 0;
// Get the ptr to the last MVS
lpLstMVS = lpMemPTD->lpLstMVS;
// Check for global VirtualAlloc memory that needs to be expanded
while (lpLstMVS)
{
// Get the initial address
lpIniAddr = lpLstMVS->IniAddr;
// If it's within range for this VirtualAlloc address, ...
if (lpIniAddr <= lpInvalidAddr
&& lpInvalidAddr < (lpIniAddr + lpLstMVS->MaxSize))
{
// Allocate more memory
if (VirtualAlloc ((LPVOID) (ADDR_MASK & (HANDLE_PTR) lpInvalidAddr),
lpLstMVS->IncrSize,
MEM_COMMIT,
PAGE_READWRITE) NE NULL)
return EXCEPTION_CONTINUE_EXECUTION;
else
// Can't allocate more memory??
{
MessageBoxW (hWndMF,
L"Not enough memory for <VirtualAlloc> in <CheckPTDVirtStr>",
lpwszAppName,
MB_OK | MB_ICONERROR);
MySetExceptionCode (EXCEPTION_LIMIT_ERROR);
return EXCEPTION_EXECUTE_HANDLER;
} // End IF/ELSE
} else
{
// Skip to the guard page address
lpIniAddr += lpLstMVS->MaxSize;
// Check for the guard page
if (lpIniAddr <= lpInvalidAddr
&& lpInvalidAddr < (lpIniAddr + PAGESIZE))
{
dprintfWL0 (L"Exceeded LIMIT of %08X @ %S", lpLstMVS->MaxSize, lpLstMVS->lpText);
MySetExceptionCode (EXCEPTION_LIMIT_ERROR);
return EXCEPTION_EXECUTE_HANDLER;
} // End IF
} // End IF/ELSE
// Get the previous ptr in the chain
lpLstMVS = lpLstMVS->lpPrvMVS;
} // End FOR
// Mark as no match
return 0;
} // End CheckPTDVirtStr
//***************************************************************************
// $CheckMemVirtStr
//
// Check on virtual allocs from <memVirtStr>
//***************************************************************************
int CheckMemVirtStr
(LPUCHAR lpInvalidAddr) // Ptr to invalid address
{
UINT uMem; // Loop counter
LPUCHAR lpIniAddr; // Ptr to initial address
// Check for global VirtualAlloc memory that needs to be expanded
for (uMem = 0; uMem < uMemVirtCnt; uMem++)
{
// Get the initial address
lpIniAddr = memVirtStr[uMem].IniAddr;
// If it's within range for this VirtualAlloc address, ...
if (lpIniAddr <= lpInvalidAddr
&& lpInvalidAddr < (lpIniAddr + memVirtStr[uMem].MaxSize))
{
// Allocate more memory
if (VirtualAlloc (lpInvalidAddr,
memVirtStr[uMem].IncrSize,
MEM_COMMIT,
PAGE_READWRITE) NE NULL)
return EXCEPTION_CONTINUE_EXECUTION;
else
// Can't allocate more memory??
{
MessageBoxW (hWndMF,
L"Not enough memory for <VirtualAlloc> in <CheckMemVirtStr>",
lpwszAppName,
MB_OK | MB_ICONERROR);
MySetExceptionCode (EXCEPTION_LIMIT_ERROR);
return EXCEPTION_EXECUTE_HANDLER;
} // End IF/ELSE
} else
{
// Skip to the guard page address
lpIniAddr += memVirtStr[uMem].MaxSize;
// Check for the guard page
if (lpIniAddr <= lpInvalidAddr
&& lpInvalidAddr < (lpIniAddr + PAGESIZE))
{
MySetExceptionCode (EXCEPTION_LIMIT_ERROR);
return EXCEPTION_EXECUTE_HANDLER;
} // End IF
} // End IF/ELSE
} // End FOR
// Mark as no match
return 0;
} // End CheckMemVirtStr
//***************************************************************************
// $CheckException
//
// Check on a structured exception
//***************************************************************************
long CheckException
(LPEXCEPTION_POINTERS lpExcept, // Ptr to exception information
LPWCHAR lpText) // Ptr to text of exception handler
{
int iRet; // Return code
// Save in globals
gContextRecord = *lpExcept->ContextRecord;
#ifdef _WIN64
gExceptionRecord = *(PEXCEPTION_RECORD64) lpExcept->ExceptionRecord;
#elif defined (_WIN32)
gExceptionRecord = *(PEXCEPTION_RECORD32) lpExcept->ExceptionRecord;
#else
#error Need code for this architecture.
#endif
// Get the invalid address
glpInvalidAddr = (LPUCHAR) gExceptionRecord.ExceptionInformation[1]; // Save as global
// Save the exception code, address, and text for later use
MySetExceptionCode (lpExcept->ExceptionRecord->ExceptionCode); // ***DELETEME***
glpExceptionText = lpText;
// Split cases based upon the exception code
switch (lpExcept->ExceptionRecord->ExceptionCode)
{
case EXCEPTION_ACCESS_VIOLATION:
// Check on virtual allocs from <memVirtStr>
iRet = CheckMemVirtStr (glpInvalidAddr);
if (iRet)
return iRet;
// Check on virtual allocs in the <lpMemPTD->lpLstMVS> chain
iRet = CheckPTDVirtStr (glpInvalidAddr);
if (iRet)
return iRet;
// Fall through to common handler execution
////////case EXCEPTION_RESULT_BOOL:
////////case EXCEPTION_RESULT_INT:
case EXCEPTION_RESULT_FLOAT:
case EXCEPTION_RESULT_VFP:
case EXCEPTION_RESULT_RAT:
case EXCEPTION_DOMAIN_ERROR:
case EXCEPTION_NONCE_ERROR:
case EXCEPTION_LIMIT_ERROR:
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
case EXCEPTION_INT_DIVIDE_BY_ZERO:
case EXCEPTION_SINGLE_STEP:
case EXCEPTION_GUARD_PAGE:
case EXCEPTION_STACK_OVERFLOW:
return EXCEPTION_EXECUTE_HANDLER;
case EXCEPTION_BREAKPOINT:
// In this case, we need to know who called us,
// so we can report it to the end user
// Save our return address for later use
#ifdef _WIN64
gExceptAddr = lpExcept->ContextRecord->Rsp;
#elif defined (_WIN32)
gExceptAddr = lpExcept->ContextRecord->Esp;
#else
#error Need code for this architecture.
#endif
return EXCEPTION_EXECUTE_HANDLER;
case EXCEPTION_CTRL_BREAK:
return EXCEPTION_CONTINUE_EXECUTION;
default:
return EXCEPTION_CONTINUE_SEARCH;
} // End SWITCH
} // End CheckException
//***************************************************************************
// $CompareStartAddresses
//
// Compare starting addresses so as to sort them
//***************************************************************************
UINT __cdecl CompareStartAddresses
(const void *elem1,
const void *elem2)
{
#define lpSALft ((LPSTART_ADDRESSES) elem1)
#define lpSARht ((LPSTART_ADDRESSES) elem2)
return (UINT) (lpSALft->StartAddressAddr
- lpSARht->StartAddressAddr);
#undef lpSARht
#undef lpSALft
} // End CompareStartAddresses
//***************************************************************************
// $IsGoodReadPtr
//
// Return TRUE iff the given ptr is valid for reading a given # bytes
//***************************************************************************
UBOOL IsGoodReadPtr
(LPBYTE lpReadPtr,
DWORD dwBytes)
{
DWORD dwCnt;
BYTE dwRead;
__try
{
for (dwCnt = 0; dwCnt < dwBytes; dwCnt++)
dwRead += *lpReadPtr++;
return TRUE;
} __except (EXCEPTION_EXECUTE_HANDLER)
{
return FALSE;
} // End __try/__except
} // End IsGoodReadPtr
//***************************************************************************
// $DisplayException
//
// Display an exception code, address, etc.
//***************************************************************************
void DisplayException
(void)
{
#ifdef DEBUG
WCHAR wszTemp[1024]; // Temp output save area
EXCEPTION_CODES exceptCode; // Exception code
UINT uMem, // Loop counter
uCnt, // ...
SILevel; // The current SI level
APLU3264 nearAddress, // Offset from closest address
nearIndex, // Index into StartAddresses
nearAddress0, // Offset from closest address
nearIndex0, // Index into StartAddresses
nearAddress1, // Offset from closest address
nearIndex1; // Index into StartAddresses
LPPERTABDATA lpMemPTD; // Ptr to PerTabData global memory
LPWCHAR exceptText; // Ptr to exception text
LPUCHAR exceptAddr; // Exception address
APLU3264 regEBP, // Stack trace ptr
regEIP; // Instruction ptr
LPSIS_HEADER lpSISCur; // Ptr to current SIS header
LPMEMVIRTSTR lpLstMVS; // Ptr to last MEMVIRTSTR (NULL = none)
// Sort the StartAddresses in ascending order by address
qsort (StartAddresses,
START_ADDRESSES_LENGTH,
sizeof (StartAddresses[0]),
&CompareStartAddresses);
// Get ptr to PerTabData global memory
lpMemPTD = TlsGetValue (dwTlsPerTabData); // Assert (IsValidPtr (lpMemPTD, sizeof (lpMemPTD)));
// If lpMemPTD isn't valid, just exit
if (!IsValidPtr (lpMemPTD, sizeof (lpMemPTD)))
return;
// Get the saved exception code & address, & text
exceptCode = gExceptionCode;
exceptAddr = (LPUCHAR) gExceptionRecord.ExceptionAddress;
exceptText = glpExceptionText;
#ifdef _WIN64
regEBP = gContextRecord.Rbp;
#elif defined (_WIN32)
regEBP = gContextRecord.Ebp;
#else
#error Need code for this architecture.
#endif
lpSISCur = lpMemPTD->lpSISCur;
lpLstMVS = lpMemPTD->lpLstMVS;
// If the exception is EXCEPTION_BREAKPOINT (from DbgStop),
// we need to display the return address as that's from
// where we were called. Displaying DbgStop address is
// of no help
if (exceptCode EQ EXCEPTION_BREAKPOINT)
exceptAddr = *(LPUCHAR *) &gExceptAddr;
// Find the address closest to and at or below the given address
// If the address is not found, it could be that we're
// running under a debugger and the debugger has changed the
// starting address of the routine to a near JMP instruction,
// so try again with that assumption
FindRoutineAddress (exceptAddr, &nearAddress0, &nearIndex0, FALSE);
FindRoutineAddress (exceptAddr, &nearAddress1, &nearIndex1, TRUE);
if (nearAddress0 < nearAddress1)
{
nearAddress = nearAddress0;
nearIndex = nearIndex0;
} else
{
nearAddress = nearAddress1;
nearIndex = nearIndex1;
} // End IF/ELSE
ShowWindow (hWndCC, SW_SHOWNORMAL);
UpdateWindow (hWndCC);
#define NewMsg(a) SendMessageW (hWndCC_LB, LB_ADDSTRING, 0, (LPARAM) (a)); UpdateWindow (hWndCC_LB)
NewMsg (L"COPY THIS TEXT TO AN EMAIL MESSAGE" );
NewMsg (L"----------------------------------------------------" );
NewMsg (L"Use Right-click: Select All, and" );
NewMsg (L" Right-click: Copy" );
NewMsg (L" to copy the entire text to the clipboard." );
NewMsg (L"----------------------------------------------------" );
NewMsg (L"Post the text on the Forum <http://forum.nars2000.org>");
NewMsg (L" in the Bug Reports section along with a detailed" );
NewMsg (L" statement of what you were doing just prior to the" );
NewMsg (L" crash." );
NewMsg (L"Also, if at all possible, it would be great if you" );
NewMsg (L" could send along a copy of the last saved workspace");
NewMsg (L" (the one with an extension of .save.bak.ws.nars)." );
NewMsg (L"----------------- Copy Below Here ------------------" );
// Display the version # of the executable
MySprintfW (wszTemp,
sizeof (wszTemp),
WS_APPNAME L" -- Version %s (%s)" WS_APPEND_DEBUG,
wszFileVer,
#ifdef _WIN64
L"Win64"
#elif defined (_WIN32)
L"Win32"
#else
#error Need code for this architecture.
#endif
);
NewMsg (wszTemp);
// Display the exception code and string
MySprintfW (wszTemp,
sizeof (wszTemp),
L"Exception code = %08X (%s)",
exceptCode,
MyGetExceptionStr (exceptCode));
NewMsg (L"");
NewMsg (wszTemp);
MySprintfW (wszTemp,
sizeof (wszTemp),
L" at %p (%S + %p)",
exceptAddr,
StartAddresses[nearIndex].StartAddressName,
nearAddress);
NewMsg (wszTemp);
MySprintfW (wszTemp,
sizeof (wszTemp),
L" from %s",
exceptText);
NewMsg (wszTemp);
// Display the registers
NewMsg (L"");
NewMsg (L"== REGISTERS ==");
MySprintfW (wszTemp,
sizeof (wszTemp),
#ifdef _WIN64
L"RAX = %p RBX = %p RCX = %p RDX = %p RIP = %p",
gContextRecord.Rax,
gContextRecord.Rbx,
gContextRecord.Rcx,
gContextRecord.Rdx,
gContextRecord.Rip
#elif defined (_WIN32)
L"EAX = %p EBX = %p ECX = %p EDX = %p EIP = %p",
gContextRecord.Eax,
gContextRecord.Ebx,
gContextRecord.Ecx,
gContextRecord.Edx,
gContextRecord.Eip
#else
#error Need code for this architecture.
#endif
);
NewMsg (wszTemp);
MySprintfW (wszTemp,
sizeof (wszTemp),
#ifdef _WIN64
L"RSI = %p RDI = %p RBP = %p RSP = %p EFL = %08X",
gContextRecord.Rsi,
gContextRecord.Rdi,
gContextRecord.Rbp,
gContextRecord.Rsp,
#elif defined (_WIN32)
L"ESI = %p EDI = %p EBP = %p ESP = %p EFL = %08X",
gContextRecord.Esi,
gContextRecord.Edi,
gContextRecord.Ebp,
gContextRecord.Esp,
#else
#error Need code for this architecture.
#endif
gContextRecord.EFlags);
NewMsg (wszTemp);
MySprintfW (wszTemp,
sizeof (wszTemp),
#ifdef _WIN64
L"CS = %04X DS = %04X ES = %04X FS = %04X GS = %04X SS = %04X CR2 = %p",
#elif defined (_WIN32)
L"CS = %04X DS = %04X ES = %04X FS = %04X GS = %04X SS = %04X CR2 = %p",
#else
#error Need code for this architecture.
#endif
gContextRecord.SegCs,
gContextRecord.SegDs,
gContextRecord.SegEs,
gContextRecord.SegFs,
gContextRecord.SegGs,
gContextRecord.SegSs,
gExceptionRecord.ExceptionInformation[1]);
NewMsg (wszTemp);
// Display the instruction stream
NewMsg (L"");
NewMsg (L"== INSTRUCTIONS ==");
// Get the instruction pointer
#ifdef _WIN64
regEIP = gContextRecord.Rip;
#elif defined (_WIN32)
regEIP = gContextRecord.Eip;
#else
#error Need code for this architecture.
#endif
// Start instruction display three rows before the actual fault instruction
regEIP -= 3 * 16;
if (IsGoodReadPtr (*(LPUCHAR *) ®EIP, 48))
{
for (uCnt = 0; uCnt < 7; uCnt++, regEIP += 16)
{
MySprintfW (wszTemp,
sizeof (wszTemp),
L"%p: ",
regEIP);
for (uMem = 0; uMem < 16; uMem++)
MySprintfW (&wszTemp[lstrlenW (wszTemp)],
sizeof (wszTemp) - (lstrlenW (wszTemp) * sizeof (wszTemp[0])),
L" %02X",
*(LPBYTE) (regEIP + uMem));
NewMsg (wszTemp);
} // End FOR
} // End IF
// Display the backtrace
NewMsg (L"");
NewMsg (L"== BACKTRACE ==");
// Do a stack walk
DoStackWalk (&gContextRecord);
// Display the virtual memory ranges
NewMsg (L"");
NewMsg (L"== MEMVIRTSTR ==");
#ifdef _WIN64
NewMsg (L" IniAddr IncrSize MaxSize GuardPage");
#elif defined (_WIN32)
NewMsg (L" IniAddr IncrSize MaxSize GuardPage");
#else
#error Need code for this architecture.
#endif
// Check for global VirtualAlloc memory that needs to be expanded
for (uMem = 0; uMem < uMemVirtCnt; uMem++)
{
MySprintfW (wszTemp,
sizeof (wszTemp),
L"%p %08X %08X %p %S",
memVirtStr[uMem].IniAddr,
memVirtStr[uMem].IncrSize,
memVirtStr[uMem].MaxSize,
memVirtStr[uMem].IniAddr + memVirtStr[uMem].MaxSize,
memVirtStr[uMem].lpText
);
NewMsg (wszTemp);
} // End FOR
// Display the local virtual memory ranges
NewMsg (L"");
NewMsg (L"== LCLMEMVIRTSTR ==");
#ifdef _WIN64
NewMsg (L" IniAddr IncrSize MaxSize GuardPage");
#elif defined (_WIN32)
NewMsg (L" IniAddr IncrSize MaxSize GuardPage");
#else
#error Need code for this architecture.
#endif
while (lpLstMVS)
{
MySprintfW (wszTemp,
sizeof (wszTemp),
L"%p %08X %08X %p %S",
lpLstMVS->IniAddr,
lpLstMVS->IncrSize,
lpLstMVS->MaxSize,
lpLstMVS->IniAddr + lpLstMVS->MaxSize,
lpLstMVS->lpText
);
NewMsg (wszTemp);
// Get the previous ptr in the chain
lpLstMVS = lpLstMVS->lpPrvMVS;
} // End WHILE
// Display the SI stack
NewMsg (L"");
NewMsg (L"== SI STACK ==");
// Loop backwards through the SI levels
for (SILevel = 0;
lpSISCur;
lpSISCur = lpSISCur->lpSISPrv, SILevel++)
{
LPAPLCHAR lpMemName; // Ptr to function name global memory
// Split cases based upon the caller's function type
switch (lpSISCur->DfnType)
{
case DFNTYPE_IMM:
#ifdef DEBUG
NewMsg (WS_UTF16_IOTA);
#endif
break;
case DFNTYPE_OP1:
case DFNTYPE_OP2:
case DFNTYPE_FCN:
// Lock the memory to get a ptr to it
lpMemName = MyGlobalLockWsz (lpSISCur->hGlbFcnName);
// Format the Name, Line #, and Suspension marker
MySprintfW (wszTemp,
sizeof (wszTemp),
L"%s[%d] %c",
lpMemName,
lpSISCur->CurLineNum,
L" *"[lpSISCur->bSuspended]);
// We no longer need this ptr
MyGlobalUnlock (lpSISCur->hGlbFcnName); lpMemName = NULL;
// Display the function name & line #
NewMsg (wszTemp);
break;
case DFNTYPE_EXEC:
NewMsg (WS_UTF16_UPTACKJOT);
break;
case DFNTYPE_QUAD:
NewMsg (WS_UTF16_QUAD);
break;
case DFNTYPE_UNK:
default:
NewMsg (L"***UNKNOWN***");
break;
} // End SWITCH
} // End FOR
// Tell the Crash Control window to display a MessageBox
SendMessageW (hWndCC, MYWM_DISPMB, 0, 0);
#undef NewMsg
exit (exceptCode);
#endif
} // End DisplayException
//***************************************************************************
// $DoStackWalk
//
// Display the stack backtrace
//***************************************************************************
void DoStackWalk
(LPCONTEXT lpContextRecord)
{
STACKFRAME64 stackFrame = {0};
CONTEXT context;
HANDLE hProcess,
hThread;
////CallstackEntry csEntry;
////IMAGEHLP_SYMBOL64 *pSym = NULL;
////IMAGEHLP_MODULE64_V2 Module;
////IMAGEHLP_LINE64 Line;
LPBYTE caller; // Ptr to caller in stack trace
APLU3264 nearAddress, // Offset from closest address
nearIndex, // Index into StartAddresses
nearAddress0, // Offset from closest address
nearIndex0, // Index into StartAddresses
nearAddress1, // Offset from closest address
nearIndex1; // Index into StartAddresses
WCHAR wszTemp[1024]; // Temp output save area
////char szAppDPFE[_MAX_PATH],
//// szDir [_MAX_DIR],
//// szDrive[_MAX_DRIVE],
//// szSymPath[_MAX_PATH];
////PSYMBOL_INFO lpSymInfo; // Ptr to ...
// Initialize the handles
hProcess = GetCurrentProcess ();
hThread = GetCurrentThread ();
////// Allocate space for the symbol name struc
////pSym = (IMAGEHLP_SYMBOL64 *) malloc (sizeof (IMAGEHLP_SYMBOL64) + STACKWALK_MAX_NAMELEN);
////if (!pSym)
//// goto CLEANUP; // Not enough memory...
////memset (pSym, 0, sizeof (IMAGEHLP_SYMBOL64) + STACKWALK_MAX_NAMELEN);
////pSym->SizeOfStruct = sizeof (IMAGEHLP_SYMBOL64);
////pSym->MaxNameLength = STACKWALK_MAX_NAMELEN;
////// Allocate space for the symbol info struc
////lpSymInfo = (PSYMBOL_INFO) malloc (sizeof (SYMBOL_INFO) + STACKWALK_MAX_NAMELEN);
////if (lpSymInfo EQ NULL)
//// goto CLEANUP; // Not enough memory...
////memset (lpSymInfo, 0, sizeof (SYMBOL_INFO) + STACKWALK_MAX_NAMELEN);
////lpSymInfo->SizeOfStruct = sizeof (SYMBOL_INFO);
////lpSymInfo->MaxNameLen = STACKWALK_MAX_NAMELEN;
////memset (&Line, 0, sizeof (Line));
////Line.SizeOfStruct = sizeof (Line);
////memset (&Module, 0, sizeof (Module));
////Module.SizeOfStruct = sizeof (Module);
////if (GetModuleFileNameA (_hInstance, szAppDPFE, sizeof (szSymPath)))
////{
//// // Split out the drive and path from the module filename
//// _splitpath (szAppDPFE, szDrive, szDir, NULL, NULL);
////
//// // Create the .HLP file name
//// _makepath (szSymPath, szDrive, szDir, NULL, NULL);
////} else
//// szSymPath[0] = '\0';
////
////// Set the symbol options
////SymSetOptions (SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
////
////// Initialize the symbols
////SymInitialize (hProcess, szSymPath, TRUE);
// Copy the outer ContextRecord
context = *lpContextRecord;
// Initialize these fields before the first call to StackWalk64
#ifdef _WIN64
stackFrame.AddrPC.Offset = lpContextRecord->Rip; // Starting instruction address
stackFrame.AddrFrame.Offset = lpContextRecord->Rbp; // Starting frame address
stackFrame.AddrStack.Offset = lpContextRecord->Rsp; // Starting stack address
#elif defined (_WIN32)
stackFrame.AddrPC.Offset = lpContextRecord->Eip; // Starting instruction address