-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AuxExceptions.inc
2673 lines (2392 loc) · 105 KB
/
AuxExceptions.inc
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
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
AuxExceptions - implementation file
This file (AuxException.inc) contains almost entire implementation of
AuxExceptions library. It is stored separately to allow for classes
rebasing.
Version 1.2.2 (2024-04-20)
Last change 2024-05-15
©2019-2024 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.AuxExceptions
Dependencies:
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
* SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
* StrRect - github.com/TheLazyTomcat/Lib.StrRect
* UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
* WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
Library SimpleCPUID is required only when symbol PurePascal is not defined.
Libraries StrRect, UInt64Utils and WinFileInfo are required only when symbol
AllowExtendedException is defined. Also, StrRect is required only when
compiling for Windows OS and UInt64Utils when compiling for Linux OS.
Library StrRect might also be required as an indirect dependency.
===============================================================================}
{!tun_end!} // ignore this line
{$IFDEF AE_Include_Defs}
{
AuxExceptions_PurePascal
If you want to compile this unit without ASM, don't want to or cannot define
PurePascal for the entire project and at the same time you don't want to or
cannot make changes to this unit, define this symbol for the entire project
and this unit will be compiled in PurePascal mode.
}
{$IFDEF AuxExceptions_PurePascal}
{$DEFINE PurePascal}
{$ENDIF}
{$IF Defined(CPUX86_64) or Defined(CPUX64)}
{$DEFINE x64}
{$ELSEIF Defined(CPU386)}
{$DEFINE x86}
{$ELSE}
{$DEFINE PurePascal}
{$IFEND}
{$IF Defined(WINDOWS) or Defined(MSWINDOWS)}
{$DEFINE Windows}
{$ELSEIF Defined(LINUX) and Defined(FPC)} // there are FPC-specific things used
{$DEFINE Linux}
{$ELSE}
{$MESSAGE FATAL 'Unsupported operating system.'}
{$IFEND}
{$IFDEF FPC}
{$MODE ObjFPC}
{$IFNDEF PurePascal}
{$ASMMODE Intel}
{$ENDIF}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
//------------------------------------------------------------------------------
{$IFDEF AllowExtendedException}
{$DEFINE AE_ExtendedException}
{$ELSE}
{$UNDEF AE_ExtendedException}
{$ENDIF}
{$IFDEF PurePascal}
{$UNDEF ExtendedExceptionRegisters}
{$UNDEF ExtendedExceptionStack}
{$ENDIF}
{$IFOPT W+}
{$DEFINE AE_StackFramesEnabled}
{$ELSE}
{$UNDEF AE_StackFramesEnabled}
{$ENDIF}
{$ENDIF AE_Include_Defs}
{===============================================================================
********************************************************************************
===============================================================================}
{$IFDEF AE_Include_Interface_Uses}
uses
SysUtils{$IFNDEF Windows}, BaseUnix{$ENDIF},
AuxTypes;
{$ENDIF AE_Include_Interface_Uses}
{===============================================================================
********************************************************************************
===============================================================================}
{===============================================================================
Informative symbols
===============================================================================}
{
Following symbols can be used to discern whether the provided classes are
based on extended exception. Since they are true constants, they can be used
as conditionals in compilation.
}
{$IFDEF AE_Include_Interface}
const
ProvidingExtendedException = {$IFDEF AE_ExtendedException}True{$ELSE}False{$ENDIF};
ProvidingExtendedExceptionN = {$IFDEF AE_ExtendedException}1{$ELSE}0{$ENDIF};
{$IFDEF AE_ExtendedException}
ProvidingExtendedExceptionE = True;
{$ENDIF}
{===============================================================================
Common types
===============================================================================}
type
TAEProcessID = {$IFDEF Windows}DWORD{$ELSE}pid_t{$ENDIF};
TAEThreadID = {$IFDEF Windows}DWORD{$ELSE}pid_t{$ENDIF};
{$IFNDEF Windows}
TAELibThreadID = pthread_t;
{$ENDIF}
TAESysErrCode = {$IFDEF Windows}DWORD{$ELSE}cInt{$ENDIF};
{===============================================================================
--------------------------------------------------------------------------------
EAECustomException
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
EAECustomException - class declaration
===============================================================================}
type
EAECustomException = class(EAEBaseException)
protected
fTime: TDateTime;
fProcessID: TAEProcessID;
fThreadID: TAEThreadID;
{$IFNDEF Windows}
fLibThreadID: TAELibThreadID;
{$ENDIF}
fExcAddr: Pointer;
public
constructor CreateFmt(const Msg: String; Args: array of const);
constructor Create(const Msg: String);
property Time: TDateTime read fTime;
property ProcessID: TAEProcessID read fProcessID;
property ThreadID: TAEThreadID read fThreadID;
{$IFNDEF Windows}
property LibThreadID: TAELibThreadID read fLibThreadID;
{$ENDIF}
property ExceptAddress: Pointer read fExcAddr;
end;
{===============================================================================
--------------------------------------------------------------------------------
CPU registers snapshot
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
CPU registers snapshot - public storage types
===============================================================================}
type
TAENativeRegister = PtrUInt;
const
AE_GPR_HIGH = {$IFDEF x64}15{$ELSE}7{$ENDIF}; // highest general purpose register
//------------------------------------------------------------------------------
type
TAEGeneralPurposeRegister = packed record
case Integer of
0: (LoByte: UInt8;
HiByte: UInt8);
1: (Word: UInt16);
2: (Long: UInt32);
{$IFDEF x64}
3: (Quad: UInt64);
{$ENDIF}
4: (Native: TAENativeRegister)
end;
TAEGeneralPurposeRegisters = packed record
case Integer of
0: (A,B,C,D,SI,DI,BP,SP: TAEGeneralPurposeRegister);
1: (R0,R1,R2,R3,R4,R5,R6,R7: TAEGeneralPurposeRegister;
{$IFDEF x64}
R8,R9,R10,R11,R12,R13,R14,R15: TAEGeneralPurposeRegister;
{$ENDIF});
2: (Regs: array[0..AE_GPR_HIGH] of TAEGeneralPurposeRegister);
end;
TAESegmentRegisters = packed record
case Integer of
0: (CS,DS,SS,ES,FS,GS: UInt16);
1: (Regs: packed array[0..5] of UInt16);
end;
TAEBasicRegisters = record
GeneralPurpose: TAEGeneralPurposeRegisters;
Segment: TAESegmentRegisters;
Flags: TAENativeRegister;
InstructionPointer: TAENativeRegister;
end;
//------------------------------------------------------------------------------
type
TAEFloatRegister = packed record
case Integer of
0: (Mantissa: UInt64;
SignExponent: UInt16);
1: (Float80: Float80;
Float64: Float64;
Float32: Float32);
end;
TAEFloatRegisters = record
Stack: array[0..7] of TAEFloatRegister;
ControlWord,StatusWord,TagWord: UInt16;
LastInstructionPointerSelector: UInt16;
LastInstructionPointerOffset: UInt32;
LastDataPointerSelector: UInt16;
LastDataPointerOffset: UInt32;
OpCode: UInt16; // only lower 11 bits are walid
end;
//------------------------------------------------------------------------------
type
TAEIntegerVectorRegister = packed record
case Integer of
0: (VecByte: packed array[0..7] of UInt8);
1: (VecWord: packed array[0..3] of UInt16);
2: (VecLong: packed array[0..1] of UInt32);
3: (VecQuad: UInt64);
end;
TAEIntegerVectorRegisters = packed record
MM: packed array[0..7] of TAEIntegerVectorRegister;
end;
//------------------------------------------------------------------------------
type
TAEFloatVectorRegister = packed record
case Integer of
0: (VecByte: packed array[0..63] of UInt8);
1: (VecWord: packed array[0..31] of UInt16);
2: (VecLong: packed array[0..15] of UInt32);
3: (VecQuad: packed array[0..7] of UInt64);
4: (XMMs: packed array[0..3] of Float32);
5: (XMMd: packed array[0..1] of Float64);
6: (YMMs: packed array[0..7] of Float32);
7: (YMMd: packed array[0..3] of Float64);
8: (ZMMs: packed array[0..15] of Float32);
9: (ZMMd: packed array[0..7] of Float64)
end;
TAEFloatVectorRegisters = record
Length: Integer; // number of valid 32bit floats in each register
Count: Integer; // number of valid registers (Regs array items)
Regs: packed array[0..31] of TAEFloatVectorRegister;
MXCSR: UInt32;
OPMask: packed array[0..7] of UInt64; // K0-K7 registers
end;
//------------------------------------------------------------------------------
type
TAESystemRegisters = record
MachineStatusWord: UInt16;
end;
//------------------------------------------------------------------------------
type
TAEPresentRegister = (
prBasic, // general purpose, flags, segment and instruction pointer registers
prFPU, // x87 FPU registers (ST0-ST7, control word, status word, ...)
prMMX, // MMX registers (MM0-MM7)
prSSE, // SSE-SSE4.x registers (XMM0-XMM7/15)
prAVX, // AVX and AVX2 registers (YMM0-YMM7/15)
prAVX512, // AVX-512 registers (ZMM0-ZMM7/31, K0-K7)
prSystem // MSW and similar
);
TAEPresentRegisters = set of TAEPresentRegister;
TAERegisters = record
PresentRegisters: TAEPresentRegisters;
Basic: TAEBasicRegisters;
Float: TAEFloatRegisters; // x87 FPU
IntegerVector: TAEIntegerVectorRegisters; // MMX
FloatVector: TAEFloatVectorRegisters; // SSE, AVX
System: TAESystemRegisters; // MSW
end;
{===============================================================================
CPU registers snapshot - public functions declaration
===============================================================================}
{
When assembly is allowed (ie. PurePascal symbol is NOT defined):
RegistersSnapshotMake
This function makes a snapshot of CPU registers as they were just before
calling it (except for RIP/EIP, it will contain address of next
instruction after the call) and saves it into a thread-local storage.
If the thread storage does not exists, it is created (allocated), but if
it does exist, then data stored in it are preserved and the current
snapshot is dropped.
In any case, the result will contain return address of the call (address
of next instruction after the call).
If this function is called in conjunction with raising an exception that
is a descendant of extended exception as this:
raise EAEExtendedException.Create at RegistersSnapshotMake;
...then the exception object will take the snapshot instead of creating
its own (the snapshot will not be polluted by constructor call) and will
also automatically free the thread storage, but...
!!! WARNING !!!
...this works only in 32bit Windows (and probably only when compiled in
Delphi), nowhere else. This is because in Win32, the function is called
BEFORE the exception object is created, whereas everywhere else the
object is firss created and only then the function is called (why, I do
not know).
Therefore using this feature is, at this point, strongly discouraged.
RegistersSnapshotGet
This function is to be the used to obtain the snapshot from thread
storage. It returns true when the snapshot is successfully obtained (ie.
was previously created by RegistersSnapshotMake) and false when it could
not be obtained. In that case, the output parameter Snapshot is zeroed
and its field PresentRegisters explicitly contains an empty set
If Cleanup is set to true, then the thread storage is freed (equivalent
to calling RegistersSnapshotCleanup).
RegistersSnapshotCleanup
Frees the thread-local storage (if it exists, otherwise it does nothing).
When PurePascal symbol is defined (assembly disabled):
RegistersSnapshotMake returns its own address, RegistersSnapshotGet returns
false (argument Snapshot is filled with zeroes, field PresentRegisters is
explicitly an empty set) and function RegistersSnapshotCleanup does nothing.
}
Function RegistersSnapshotMake: Pointer;{$IFNDEF PurePascal}register; assembler;{$ENDIF}
Function RegistersSnapshotGet(out Snapshot: TAERegisters; Cleanup: Boolean = False): Boolean;
procedure RegistersSnapshotCleanup;
{$IFDEF AE_ExtendedException}
{===============================================================================
--------------------------------------------------------------------------------
EAEExtendedException
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
EAEExtendedException - public storage types
===============================================================================}
{
TAEProvidedInformation(s) is used to indicate what information the extended
exception object is providing.
}
type
TAEProvidedInformation = (infoRegistersSnapshot,infoStackTrace,infoProcess,
infoModule,infoModules,infoThreads);
TAEProvidedInformations = set of TAEProvidedInformation;
{-------------------------------------------------------------------------------
EAEExtendedException - public storage types - stack
-------------------------------------------------------------------------------}
{
All functions are expected to start with following code or its x64 equivalent
(if not, then all bets are off):
PUSH EBP
MOV EBP, ESP
For the purpose of this library, the frame is started by calling a subroutine
(function). This means the frame start (its highest address) points just
above the linking information (stored return address of the caller).
Let's use some "ASCII art" to ilustrate (x86 variant):
high address --------------------- ---
| | |
| params for B | |-- stack frame of function A
| | |
call to B --------------------- ---
| EIP (points to A) | |
--------------------- |
| EBP(A) | |
EBP(B) points here --------------------- |
| | |
| variables of B | |-- stack frame of function B
| | |
--------------------- |
| | |
| variables of C | |
| | |
call to C --------------------- ---
| EIP (points to B) | |
--------------------- |
| EBP(B) | |
EBP points here --------------------- |-- stack frame of function C (current function)
| | |
| variables of C | |
| | |
low address --------------------- ---
}
type
TAEStackFrame = record
FrameStart: Pointer; // address just above the linking info
FrameBase: Pointer; // points to stored EBP/RBP
FrameAddress: Pointer; // where the stack frame starts in memory (its lowest addresss)
FrameSize: TMemSize; // size of this frame in bytes
OwnerFunction: Pointer; // points inside the function owning/using this frame
InDump:
record
FrameStart: Pointer;
FrameBase: Pointer;
FrameAddress: Pointer;
end;
LinkingInfo:
record
FrameBase: Pointer; // stack frame base pointer of the caller
ReturnAddress: Pointer; // return address of the calling function
end;
end;
TAEStackTrace = record
StackBottom: Pointer; // stack base, points above the first pushed value
StackTop: Pointer; // stack pointer, ESP/RSP address, points to the last pushed value
StackDump: Pointer; // points to a buffer containing the copy of active stack area
StackDumpSize: TMemSize; // size of stack dump buffer
StackFrames: array of TAEStackFrame;
end;
{-------------------------------------------------------------------------------
EAEExtendedException - public storage types - process
-------------------------------------------------------------------------------}
type
TAEProcessInfo = record
ProcessID: TAEProcessID;
ExecFileName: String;
FileInfo: TObject;
end;
{-------------------------------------------------------------------------------
EAEExtendedException - public storage types - current module
-------------------------------------------------------------------------------}
type
TAECurrModuleInfo = record
FileName: String;
FileInfo: TObject;
end;
{-------------------------------------------------------------------------------
EAEExtendedException - public storage types - modules
-------------------------------------------------------------------------------}
{
Windows
BaseAddress is the address at which the module is loaded.
ExecMemory is set to the same value as BaseAddress.
ExecSize is set to the module size obtained when enumerating modules (which
does NOT correspond to actual size of the module file on disk, but rather
in memory).
ExecContiguous is alway true.
Linux
BaseAddress is address of the first memory block into which the module is
mapped.
ExecMemory is address of the first memory block with execute permission
into which the module is mapped.
ExecSize is the size of all memory blocks with execute permission in which
the module is mapped.
ExecContiguous is set to true when all memory blocks with execute permission
are contiguous, false otherwise.
NOTE - in Linux, the modules are obtained by parsing a text file
"proc/[pid]/maps", where pid is ID of the current process.
}
type
TAEModuleInfo = record
ModuleFileName: String; // full path
BaseAddress: Pointer;
ExecMemory: Pointer;
ExecSize: TMemSize;
ExecContiguous: Boolean;
FileInfo: TObject;
end;
TAEModulesInfo = record
Modules: array of TAEModuleInfo;
end;
{-------------------------------------------------------------------------------
EAEExtendedException - public storage types - threads
-------------------------------------------------------------------------------}
type
TAEThreadInfo = record
ThreadID: TAEThreadID;
end;
TAEThreadsInfo = record
Threads: array of TAEThreadInfo;
end;
{===============================================================================
EAEExtendedException - class declaration
===============================================================================}
type
EAEExtendedException = class(EAECustomException)
protected
fProvidedInfo: TAEProvidedInformations;
fRegisters: TAERegisters;
fStackTrace: TAEStackTrace;
fProcessInfo: TAEProcessInfo;
fModuleInfo: TAECurrModuleInfo;
fModulesInfo: TAEModulesInfo;
fThreadsInfo: TAEThreadsInfo;
procedure InitRegisters; virtual;
procedure InitStackTrace; virtual;
procedure InitProcessInfo; virtual;
procedure InitModuleInfo; virtual;
procedure InitModulesInfo; virtual;
procedure InitThreadsInfo; virtual;
procedure Finalize; virtual;
public
constructor CreateFmt(const Msg: String; Args: array of const);
destructor Destroy; override;
property ProvidedInformation: TAEProvidedInformations read fProvidedInfo;
property Registers: TAERegisters read fRegisters;
property StackTrace: TAEStackTrace read fStackTrace;
property ProcessInfo: TAEProcessInfo read fProcessInfo;
property ModuleInfo: TAECurrModuleInfo read fModuleInfo;
property ModulesInfo: TAEModulesInfo read fModulesInfo;
property ThreadsInfo: TAEThreadsInfo read fThreadsInfo;
end;
{$ENDIF AE_ExtendedException}
{===============================================================================
--------------------------------------------------------------------------------
EAEGeneralException
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
EAEGeneralException - class declaration
===============================================================================}
type
{$IFDEF AE_ExtendedException}
EAEGeneralException = class(EAEExtendedException)
{$ELSE AE_ExtendedException}
EAEGeneralException = class(EAECustomException)
{$ENDIF AE_ExtendedException}
protected
fFaultingObject: String;
fFaultingFunction: String;
fFullMessage: String;
public
constructor CreateFmt(const Msg: String; Args: array of const; FaultObject: TObject; const FaultFunction: String); overload;
constructor Create(const Msg: String; FaultObject: TObject; const FaultFunction: String); overload;
property FaultingObject: String read fFaultingObject;
property FaultingFunction: String read fFaultingFunction;
property FullMessage: String read fFullMessage;
end;
{===============================================================================
--------------------------------------------------------------------------------
EAESystemError
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
EAESystemError - class declaration
===============================================================================}
type
EAESystemError = class(EAEGeneralException)
protected
fErrorCode: TAESysErrCode;
public
constructor Create(ErrorCode: TAESysErrCode; FullSysMsg: Boolean; FaultObject: TObject; const FaultFunction: String); overload;
constructor Create(FullSysMsg: Boolean; FaultObject: TObject; const FaultFunction: String); overload;
property ErrorCode: TAESysErrCode read fErrorCode;
end;
{===============================================================================
--------------------------------------------------------------------------------
EAEIndexException
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
EAEIndexException - class declaration
===============================================================================}
type
EAEIndexException = class(EAEGeneralException)
protected
fIndex: Integer;
class Function GetDefaultMessage: String; virtual;
public
constructor Create(const Msg: String; Index: Integer; FaultObject: TObject; const FaultFunction: String); overload;
constructor Create(Index: Integer; FaultObject: TObject; const FaultFunction: String); overload;
property Index: Integer read fIndex;
end;
{===============================================================================
--------------------------------------------------------------------------------
EAEIndexOutOfBounds
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
EAEIndexOutOfBounds - class declaration
===============================================================================}
type
EAEIndexOutOfBounds = class(EAEIndexException)
protected
class Function GetDefaultMessage: String; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
EAEIndexTooLow
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
EAEIndexTooLow - class declaration
===============================================================================}
type
EAEIndexTooLow = class(EAEIndexException)
protected
class Function GetDefaultMessage: String; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
EAEIndexTooHigh
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
EAEIndexTooHigh - class declaration
===============================================================================}
type
EAEIndexTooHigh = class(EAEIndexException)
protected
class Function GetDefaultMessage: String; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
EAEIndexInvalid
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
EAEIndexInvalid - class declaration
===============================================================================}
type
EAEIndexInvalid = class(EAEIndexException)
protected
class Function GetDefaultMessage: String; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
EAEValueException
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
EAEValueException - class declaration
===============================================================================}
type
EAEValueException = class(EAEGeneralException)
protected
fValueName: String;
fValue: Variant;
class Function VariantToStr(Value: Variant): String; virtual;
class Function VariantArrayToStr(Value: Variant): String; virtual;
class Function GetDefaultMessage(ValueString: Boolean): String; virtual;
public
constructor Create(const Msg,ValueName: String; Value: Variant; FaultObject: TObject; const FaultFunction: String); overload;
constructor Create(const Msg,ValueName: String; FaultObject: TObject; const FaultFunction: String); overload;
constructor Create(const ValueName: String; Value: Variant; FaultObject: TObject; const FaultFunction: String); overload;
constructor Create(const ValueName: String; FaultObject: TObject; const FaultFunction: String); overload;
property ValueName: String read FValueName;
property Value: Variant read fValue;
end;
{===============================================================================
--------------------------------------------------------------------------------
EAEValueInvalid
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
EAEValueInvalid - class declaration
===============================================================================}
type
EAEValueInvalid = class(EAEValueException)
protected
class Function GetDefaultMessage(ValueString: Boolean): String; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
EAEValueInvalidNameOnly
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
EAEValueInvalidNameOnly - class declaration
===============================================================================}
type
EAEValueInvalidNameOnly = class(EAEValueException)
protected
class Function GetDefaultMessage(ValueString: Boolean): String; override;
end;
{$ENDIF AE_Include_Interface}
{===============================================================================
********************************************************************************
===============================================================================}
{$IFDEF FPC}{$PUSH}{$WARN 2005 OFF}{$ENDIF}
{$IFDEF AE_Include_Implementation_Uses}
uses
Variants {$IFDEF Windows}, Windows{$ELSE}, syscall, pthreads{$ENDIF}
{$IFDEF AE_ExtendedException}, WinFileInfo
{$IFDEF Windows}
,{$IFDEF FPC}jwaTlHelp32, jwaPsApi{$ELSE}TlHelp32, PsApi{$ENDIF}, StrRect
{$ELSE}
, Classes, StrUtils, dl, UInt64Utils
{$ENDIF}
{$ENDIF}{$IFNDEF PurePascal}, SimpleCPUID{$ENDIF};
//------------------------------------------------------------------------------
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W4055:={$WARN 4055 OFF}} // Conversion between ordinals and pointers is not portable
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$DEFINE W7102:={$WARN 7102 OFF}} // Use of +offset(%ebp) for parameters invalid here
{$PUSH}{$WARN 2005 OFF} // Comment level $1 found
{$IF Defined(FPC) and (FPC_FULLVERSION >= 30000)}
{$DEFINE W7122:={$WARN 7122 OFF}} // Check size of memory operand "$1: memory-operand-size is $2 bits, but expected [$3 bits + $4 byte offset]"
{$ELSE}
{$DEFINE W7122:=}
{$IFEND}
{$POP}
{$ENDIF}
{$ENDIF AE_Include_Implementation_Uses}
{$IFDEF FPC}{$POP}{$ENDIF}
{===============================================================================
********************************************************************************
===============================================================================}
{$IFDEF AE_Include_Implementation}
{===============================================================================
--------------------------------------------------------------------------------
External functions
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
External functions - implementation
===============================================================================}
{$IFNDEF Windows}
Function getpid: pid_t; cdecl; external;
//------------------------------------------------------------------------------
Function gettid: pid_t;
begin
Result := do_syscall(syscall_nr_gettid);
end;
{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
EAECustomException
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
EAECustomException - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
EAECustomException - public methods
-------------------------------------------------------------------------------}
constructor EAECustomException.CreateFmt(const Msg: String; Args: array of const);
begin
inherited CreateFmt(Msg,Args);
fTime := Now;
{$IFDEF Windows}
fProcessID := Windows.GetCurrentProcessID;
fThreadID := Windows.GetCurrentThreadID;
{$ELSE}
fProcessID := getpid;
fThreadID := gettid;
fLibThreadID := pthread_self;
{$ENDIF}
fExcAddr := ExceptAddr;
end;
//------------------------------------------------------------------------------
constructor EAECustomException.Create(const Msg: String);
begin
CreateFmt(Msg,[]);
end;
{===============================================================================
--------------------------------------------------------------------------------
CPU registers snapshot
--------------------------------------------------------------------------------
===============================================================================}
{$IFDEF PurePascal}
{===============================================================================
CPU registers snapshot - public functions implementation
===============================================================================}
Function RegistersSnapshotMake: Pointer;
begin
Result := @RegistersSnapshotMake;
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
Function RegistersSnapshotGet(out Snapshot: TAERegisters; Cleanup: Boolean = False): Boolean;
begin
FillChar(Addr(Snapshot)^,SizeOf(TAERegisters),0);
Snapshot.PresentRegisters := [];
Result := False;
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
procedure RegistersSnapshotCleanup;
begin
// do nothing
end;
{$ELSE}
{===============================================================================
CPU registers snapshot - machine features word
===============================================================================}
const
AE_MACHINEFEATURES_FPU = $00000001;
AE_MACHINEFEATURES_MMX = $00000002;
AE_MACHINEFEATURES_SSE = $00000004;
AE_MACHINEFEATURES_AVX = $00000008;
AE_MACHINEFEATURES_AVX512 = $00000010;
AE_MACHINEFEATURES_FLOATVECS = AE_MACHINEFEATURES_SSE or
AE_MACHINEFEATURES_AVX or
AE_MACHINEFEATURES_AVX512;
var
MachineFeaturesWord: UInt32 = 0;
{===============================================================================
CPU registers snapshot - internal/thread-local storage
===============================================================================}
type
TAERegistersStorage = packed record
IP: TAENativeRegister;
// RAX, RBX, RCX, RDX, RSI, RDI, RBP, RSP, R8, R9, R10, R11, R12, R13, R14, R15
GP: packed array[0..AE_GPR_HIGH] of TAENativeRegister;
FLAGS: TAENativeRegister;
// segment (CS, DS, SS, ES, FS, GS)
SG: packed array[0..5] of UInt16;
{
x87 - following fields, starting with CW, correspond to protected-mode x87
FPU state memory image.
}
CW,fpad0: UInt16; // control word
SW,fpad1: UInt16; // status word
TW,fpad2: UInt16; // tag word
FIP: UInt32; // FPU Instruction Pointer Offset
FCS: UInt16; // FPU Instruction Pointer Selector
OpCode: UInt16; // only lower 10 bits used
FDP: UInt32; // FPU Data Pointer Offset
FDS,fpad3: UInt16; // FPU Data Pointer Selector
Stack: packed array[0..7] of packed array[0..9] of UInt8;
{
MMX - the vectors can be obtained directly from x87 stack, but to be sure
they are loaded separately.
}
MM: packed array[0..7] of UInt64;
{
SSE, AVX(2), AVX-512 - the storage is made large enough to accomodate all
possible data, but depending on machine features, only lower part of the
space might be actually used.
}
ZMM: packed array[0..{$IFDEF x64}2047{$ELSE}511{$ENDIF}] of UInt8;
K: packed array[0..7] of UInt64;
MXCSR: UInt32;
// system registers
MSW: UInt16; // machine status word
end;
PAERegistersStorage = ^TAERegistersStorage;
//------------------------------------------------------------------------------
threadvar
RegistersSnapshot: PAERegistersStorage;
{===============================================================================
CPU registers snapshot - internal functions
===============================================================================}
procedure GetX87Data(Storage: Pointer); register; assembler;
asm
// get x87 state
FSAVE [Storage] // this will initialize FPU...
FRSTOR [Storage] // ...so we restore it again
FWAIT
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W7122{$ENDIF}
procedure GetMMXData(Storage: Pointer); register; assembler;
asm
MOVQ qword ptr [Storage], MM0
MOVQ qword ptr [Storage + 8], MM1
MOVQ qword ptr [Storage + 16], MM2
MOVQ qword ptr [Storage + 24], MM3
MOVQ qword ptr [Storage + 32], MM4
MOVQ qword ptr [Storage + 40], MM5
MOVQ qword ptr [Storage + 48], MM6
MOVQ qword ptr [Storage + 56], MM7
EMMS
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
procedure GetAVX512Data(Storage: Pointer); assembler; register;
asm
{$IFDEF x64}
{$IFDEF Windows}
// Win64
DB $62, $f1, $7c, $48, $11, $01 // VMOVUPS zmmword ptr [RCX + 0000], ZMM0
DB $62, $f1, $7c, $48, $11, $49, $01 // VMOVUPS zmmword ptr [RCX + 0064], ZMM1
DB $62, $f1, $7c, $48, $11, $51, $02 // VMOVUPS zmmword ptr [RCX + 0128], ZMM2
DB $62, $f1, $7c, $48, $11, $59, $03 // VMOVUPS zmmword ptr [RCX + 0192], ZMM3
DB $62, $f1, $7c, $48, $11, $61, $04 // VMOVUPS zmmword ptr [RCX + 0256], ZMM4
DB $62, $f1, $7c, $48, $11, $69, $05 // VMOVUPS zmmword ptr [RCX + 0320], ZMM5
DB $62, $f1, $7c, $48, $11, $71, $06 // VMOVUPS zmmword ptr [RCX + 0384], ZMM6
DB $62, $f1, $7c, $48, $11, $79, $07 // VMOVUPS zmmword ptr [RCX + 0448], ZMM7
DB $62, $71, $7c, $48, $11, $41, $08 // VMOVUPS zmmword ptr [RCX + 0512], ZMM8
DB $62, $71, $7c, $48, $11, $49, $09 // VMOVUPS zmmword ptr [RCX + 0576], ZMM9
DB $62, $71, $7c, $48, $11, $51, $0a // VMOVUPS zmmword ptr [RCX + 0640], ZMM10
DB $62, $71, $7c, $48, $11, $59, $0b // VMOVUPS zmmword ptr [RCX + 0704], ZMM11
DB $62, $71, $7c, $48, $11, $61, $0c // VMOVUPS zmmword ptr [RCX + 0768], ZMM12
DB $62, $71, $7c, $48, $11, $69, $0d // VMOVUPS zmmword ptr [RCX + 0832], ZMM13
DB $62, $71, $7c, $48, $11, $71, $0e // VMOVUPS zmmword ptr [RCX + 0896], ZMM14
DB $62, $71, $7c, $48, $11, $79, $0f // VMOVUPS zmmword ptr [RCX + 0960], ZMM15
DB $62, $E1, $7C, $48, $11, $41, $10 // VMOVUPS zmmword ptr [RCX + 1024], ZMM16