-
Notifications
You must be signed in to change notification settings - Fork 50
/
DebugInfo.pas
2298 lines (1878 loc) · 56 KB
/
DebugInfo.pas
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
Unit DebugInfo;
Interface
Uses
SysUtils, Windows, Classes, Debuger, DebugerTypes, Generics.Collections, Generics.Defaults;
Type
TFindResult = (slNotFound = 0, slFoundExact, slFoundNotExact, slFoundWithoutLine);
Type
TSegmentCodeInfo = Class;
TUnitInfo = Class;
TFuncInfo = Class;
TTypeInfo = Class;
TUnitSourceModuleInfo = class;
TLineInfo = Class
public
LineNo: Integer;
Address: Pointer;
SrcSegment: TUnitSourceModuleInfo;
End;
TLineInfoList = TObjectList<TLineInfo>;
TTypeKind = (tkBoolean, tkWordBool, tkLongBool, tkShortInt, tkSmallInt, tkInteger, tkInt64, tkByte, tkWord, tkCardinal, tkUInt64, tkSingle, tkReal48, tkReal, tkExtended, tkCurrency, tkComplex,
tkPString, tkLString, tkWString, tkChar, tkPointer, tkSubRange, tkArray, tkEnum, tkStructure, tkClass, tkSet, tkVariant, tkProperty, tkFieldList, tkClosure, tkClassRef, tkWideChar, tkProcedure,
tkArgList, tkMFunction, tkVoid, tkObject, tkDynamicArray);
TNameId = type Integer;
TNameInfo = Class(TObject)
public
NameId: Integer;
SymbolInfo: TObject; // Óêàçàòåëü íà TJclTD32SymbolInfo
function Name: AnsiString; virtual; abstract;
function ShortName: String; virtual; abstract;
End;
TNameIdList = TDictionary<TNameId, TNameInfo>;
TNameList = Class(TList)
private
FNameIdList: TNameIdList;
FFreeItems: LongBool;
function GetNameInfoItem(const Index: Integer): TNameInfo;
protected
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
procedure CheckNameIdList;
public
constructor Create;
destructor Destroy; override;
procedure Clear; override;
function FindByName(Const Name: AnsiString; const SubStr: LongBool = False): TNameInfo;
function FindByNameId(Const NameId: TNameId): TNameInfo;
property NameInfoItems[const Index: Integer]: TNameInfo read GetNameInfoItem; default;
property FreeItems: LongBool read FFreeItems write FFreeItems;
End;
TTypeInfo = Class(TNameInfo)
public
Kind: TTypeKind;
BaseType: TTypeInfo;
DataSize: Integer;
MinValue: Integer;
MaxValue: Integer;
IndexType: TTypeInfo;
Members: TNameList;
Elements: TNameList;
UnitInfo: TUnitInfo;
TypeInfoIdx: Integer; // Index in UnitInfo.Types
Constructor Create;
Destructor Destroy; Override;
function Name: AnsiString; override;
function ShortName: String; override;
function KindAsString: String;
function TypeOf: String;
function ElementsToString: String;
end;
TEnumInfo = Class(TNameInfo)
public
TypeInfo: TTypeInfo;
OrderValue: Integer;
function Name: AnsiString; override;
function ShortName: String; override;
End;
TConstInfo = Class(TNameInfo)
private
FValue: Variant;
procedure SetValue(const Value: Variant);
public
TypeInfo: TTypeInfo;
Owner: TSegmentCodeInfo;
function Name: AnsiString; override;
function ShortName: String; override;
function UnitInfo: TUnitInfo;
function ValueAsString: String;
property Value: Variant read FValue write SetValue;
End;
TRegInfo = Class
public
StartOffset: Cardinal;
EndOffset: Cardinal;
RegisterIndex: Integer;
End;
TVarKind = (vkGlobal, vkStack, vkRegister, vkLink, vkTLS);
TVarInfo = Class(TNameInfo)
public
DataType: TTypeInfo;
Owner: TSegmentCodeInfo;
VarKind: TVarKind;
// IsPointer : LongBool;
// ByRef : LongBool;
Offset: Integer;
RegisterRanges: TList;
Constructor Create;
Destructor Destroy; Override;
function UnitInfo: TUnitInfo;
function Name: AnsiString; override;
function ShortName: String; override;
function DataTypeName: String;
function AsString: String;
function Value: Variant;
End;
TMemberScope = (msPrivate, msProtected, msPublic);
TStructMember = Class(TNameInfo)
public
DataType: TTypeInfo;
Offset: Integer;
DataSize: Integer;
Scope: TMemberScope;
AliasNameId: TNameId;
MethodNameId: TNameId;
Method: TFuncInfo; // read function for properties
IsDefault: LongBool; // true for default property
function Alias: AnsiString; // read field for properties
function MethodName: AnsiString; // read function name for properties
function Name: AnsiString; override;
function ShortName: String; override;
end;
// .text,.itext,.data,.bss,.tls,.pdata,.idata,.didata,.rdata,.reloc,.rsrc
TSegmentType = (ustUnknown = 0, ustCode, ustICode, ustData, ustBSS, ustTLS, ustPData, ustIData, ustDIData, ustRData, ustReloc, ustSrc);
TSegmentClassInfo = class
public
Address: Pointer;
Size: Cardinal;
SegType: TSegmentType;
ID: Word;
function SegTypeName: String;
class function StrToSegmentType(const Str: String): TSegmentType; static;
end;
TUnitSegmentInfo = Class
public
UnitInfo: TUnitInfo;
Address: Pointer;
Size: Cardinal;
SegmentClassInfo: TSegmentClassInfo;
End;
TUnitSourceModuleInfo = Class(TNameInfo)
public
UnitInfo: TUnitInfo;
Lines: TLineInfoList;
Address: Pointer;
Constructor Create;
Destructor Destroy; Override;
Procedure Clear; Virtual;
function Name: AnsiString; Override;
function ShortName: String; override;
function FullUnitName: String;
End;
TDebugInfo = Class;
TSegmentCodeInfo = Class(TNameInfo)
public
Address: Pointer;
Size: Cardinal;
Consts: TNameList;
Types: TNameList;
Vars: TNameList;
Funcs: TNameList;
Lines: TLineInfoList;
Constructor Create;
Destructor Destroy; Override;
function Name: AnsiString; override;
function ShortName: String; override;
Procedure Clear; Virtual;
function FindTypeByName(const TypeName: AnsiString; const SubStr: LongBool = False): TTypeInfo;
function FindFuncByName(const FuncName: AnsiString; const SubStr: LongBool = False): TFuncInfo;
function FindFuncByNameId(const FuncNameId: Integer): TFuncInfo;
function FindConstByName(const ConstName: AnsiString; const SubStr: LongBool = False): TConstInfo;
function FindVarByName(const VarName: AnsiString; const SubStr: LongBool = False): TVarInfo;
function CheckAddress(const Addr: Pointer): Integer;
End;
ISegmentCodeInfoComparer = IComparer<TSegmentCodeInfo>;
TSegmentCodeInfoComparer = class(TInterfacedObject, ISegmentCodeInfoComparer)
public
function Compare(const Left, Right: TSegmentCodeInfo): Integer;
end;
TSegmentCodeInfoList = Class(TList<TSegmentCodeInfo>)
private
FSorted: LongBool;
public
constructor Create;
destructor Destroy; override;
procedure CheckSorted;
procedure UpdateSort;
function FindByAddress(const Address: Pointer): TSegmentCodeInfo;
End;
TFuncInfo = Class(TSegmentCodeInfo)
UnitInfo: TUnitInfo;
UnitSegment: TUnitSegmentInfo;
Params: TNameList; // TODO: Îòäåëèòü ïàðàìåòðû îò Vars
ResultType: TTypeInfo;
Parent: TFuncInfo;
ID: TObject;
ParentID: TObject;
Constructor Create;
Destructor Destroy; Override;
function ShortName: String; override;
function ParamsAsString: String;
End;
TUnitType = (utProject, utSystem, utComponentLib, utExternal, utUnknown);
TUnitInfo = Class(TSegmentCodeInfo)
protected
function GetUnitType: TUnitType;
public
Segments: TList;
SourceSegments: TList;
UsedUnits: TStringList;
FuncsByAddr: TSegmentCodeInfoList;
Constructor Create;
Destructor Destroy; Override;
Procedure Clear; Override;
function ShortName: String; Override;
function FullUnitName: String;
function FindSegmentByAddr(const Addr: Pointer; const SegmentID: Word = 0): TUnitSegmentInfo;
function FindSourceSegmentByNameId(const NameId: TNameId): TUnitSourceModuleInfo;
function FindSourceSegmentByAddr(const Addr: Pointer): TUnitSourceModuleInfo;
property UnitType: TUnitType read GetUnitType;
end;
TDLLInfo = Class(TSegmentCodeInfo)
End;
PAddressInfo = ^RAddressInfo;
RAddressInfo = record
Addr: Pointer;
UnitInfo: TUnitInfo;
FuncInfo: TFuncInfo;
LineInfo: TLineInfo;
FindResult: TFindResult;
end;
TAddressInfoList = class(TDictionary<Pointer, PAddressInfo>)
private
FLock: TMREWSync;
protected
procedure ValueNotify(const Value: PAddressInfo; Action: TCollectionNotification); override;
public
constructor Create(ACapacity: Integer = 0);
destructor Destroy; override;
property Lock: TMREWSync read FLock;
end;
TStackEntry = Class
Public
UnitInfo: TUnitInfo;
FuncInfo: TFuncInfo;
LineInfo: TLineInfo;
EIP: Pointer;
RET: Pointer;
EBP: Pointer;
Constructor Create;
Function GetInfo: String;
Function UpdateInfo(Const Addr: Pointer = nil): TFindResult;
End;
TDebugInfoProgressCallback = procedure(const Action: String; const Progress: Integer) of object;
TDbgSourceDirListItem = record
ShortFileName: String;
FileName: String;
HashCode : Cardinal;
end;
TDbgSourceDirList = class
private
FBuckets: array of TDbgSourceDirListItem;
FCount: Integer;
FGrowth: Integer;
FCapacity: Integer;
protected
procedure Grow;
function LinearFind(const HashCode: Cardinal; const ShortFileName: String; var Index : Integer): Boolean;
function SameItem(const HashCode1, HashCode2: Cardinal; const ShortFileName1, ShortFileName2: String): Boolean;
function GetItemHashCode(const ShortFileName: String): Integer;
public
function Add(const FileName: String): Boolean;
function Contains(const ShortFileName: String): Boolean;
function TryGetValue(const ShortFileName: String; out FileName: String): Boolean;
procedure Clear;
property Count : Integer read FCount;
end;
TDbgSourceList = Array [Low(TUnitType) .. High(TUnitType)] of TDbgSourceDirList;
TMemoryManagerInfo = class
public
VarInfo: TVarInfo;
GetMem: TFuncInfo;
FreeMem: TFuncInfo;
ReallocMem: TFuncInfo;
AllocMem: TFuncInfo;
constructor Create;
procedure Clear;
end;
TRTLInfo = class
public
vmtClassNameInfo: TConstInfo;
constructor Create;
procedure Clear;
function vmtClassName: Integer;
end;
TDebugInfoClass = Class Of TDebugInfo;
TDebugInfo = Class
Private
FDirs: TDbgSourceList;
FSegments: TStringList;
FUnits: TStringList; // Sorted by name
FUnitsByAddr: TSegmentCodeInfoList; // Sorted by Address
FDbgLog: TDbgLog;
FMemoryManagerInfo: TMemoryManagerInfo;
FRTLInfo: TRTLInfo;
FExeFileName: String;
FDebugInfoLoaded: LongBool;
FDebugInfoProgressCallback: TDebugInfoProgressCallback;
FLastProgressAction: String;
FLastProgress: Integer;
function GetDirs(const SourceType: TUnitType): TDbgSourceDirList;
procedure ClearDirs;
Protected
FDebugInfoType: String;
FUseShortNames: LongBool;
procedure DoProgress(const Action: String; const Progress: Integer); virtual;
Function DoReadDebugInfo(Const FileName: String; ALoadDebugInfo: LongBool): LongBool; Virtual; abstract;
function GetSegmentByID(const ID: Word): TSegmentClassInfo;
function GetSegmentByType(const SegType: TSegmentType): TSegmentClassInfo;
function ParseUnitName(UnitInfo: TUnitInfo; const WithExt: LongBool = True): String; virtual;
function ParseFuncName(FuncInfo: TFuncInfo): String; virtual;
function ParseTypeName(TypeInfo: TTypeInfo): String; virtual;
function ParseConstName(ConstInfo: TConstInfo): String; virtual;
function ParseVarName(VarInfo: TVarInfo): String; virtual;
function ParseStructMemberName(StructMember: TStructMember): String; virtual;
Public
Constructor Create;
Destructor Destroy; Override;
Procedure ClearDebugInfo; Virtual;
Function HasDebugInfo(Const FileName: String): LongBool; Virtual; abstract;
Function ReadDebugInfo(Const FileName: String): LongBool; Virtual;
Function GetFileCount: Integer; Virtual;
Function GetFile(Index: Integer): String; Virtual;
Function GetTypeInfo(Const TypeName: String): TTypeInfo; Virtual;
Function GetAddrInfo(Var Addr: Pointer; Const FileName: String; Line: Cardinal): TFindResult; Virtual; abstract;
Procedure GetCallStackItems(const ThreadID: TThreadId; Const ExceptAddr, ExceptFrame: Pointer; StackItems: TList); Virtual;
Function GetLineInfo(const Addr: Pointer; Var UnitInfo: TUnitInfo; Var FuncInfo: TFuncInfo; Var LineInfo: TLineInfo; GetPrevLine: LongBool): TFindResult; Virtual; abstract;
Function GetLineInformation(const Addr: Pointer; Var UnitName: String; Var FuncName: String; Var Line: LongInt; GetPrevLine: LongBool): TFindResult; Virtual;
procedure UpdateSourceDirs(const SourceType: TUnitType; const SourceDirs: String); virtual;
procedure AddSourceDir(const SourceDir: TDbgSourceDirList; const Dir: String; const Recursive: LongBool = True); virtual;
function FullUnitName(const UnitName: String): String;
function GetUnitType(const UnitName: String): TUnitType;
Function MakeFuncDbgFullName(Const ClassName, MethodName: AnsiString): AnsiString; Virtual; abstract;
Function MakeFuncShortName(Const MethodName: AnsiString): AnsiString; Virtual; abstract;
Function MakeFuncNativeName(Const MethodName: AnsiString): AnsiString; Virtual; abstract;
Function FuncByName(const FuncName: AnsiString): TFuncInfo;
Function Evaluate(BriefMode: LongBool; Const Expression: String; Const TimeOut: Cardinal = INFINITE): String; Virtual; abstract;
Function EvaluateVariable(VarInfo: TVarInfo): Variant; virtual; abstract;
Function VarValueAsString(const Value: Variant): String; virtual; abstract;
procedure SetMemoryManagerBreakpoints; Virtual; abstract;
procedure ResetMemoryManagerBreakpoints; Virtual; abstract;
Procedure InitDebugHook; Virtual; abstract;
Function GetNameById(const Idx: TNameId): AnsiString; virtual; abstract;
Function CheckAddr(Const Addr: Pointer): LongBool; Virtual;
Function DumpLineInformation(Const Addr: Pointer): String;
Function GetParamsStr(FuncInfo: TFuncInfo; Const EBP: Pointer; IsTopStack: LongBool): String;
Function GetClassName(Const ObjectPtr: Pointer): String; Virtual; abstract;
Function GetExceptionName(ExceptionRecord: PExceptionRecord): String; Virtual;
Function GetExceptionMessage(ExceptionRecord: PExceptionRecord; const ThreadID: TThreadId): String; Virtual;
Function GetExceptionAddress(ExceptionRecord: PExceptionRecord): Pointer; Virtual;
Function GetExceptionFrame(ExceptionRecord: PExceptionRecord): Pointer; Virtual;
Function CheckDebugException(ExceptionRecord: PExceptionRecord; Var IsTraceException: LongBool): LongBool; Virtual;
Function CheckSystemFile(Const FileName: String): LongBool; Virtual; abstract;
Function IsSystemException(Const ExceptionCode: DWORD): LongBool;
Function CheckDebugOutputMessage(DebugEvent: PDebugEvent): LongBool;
Function ProcessDebugOutputMessage(Const Msg: WideString; DebugEvent: PDebugEvent): LongBool; Virtual;
Function IsValidAddr(Const Addr: Pointer): LongBool;
Function IsValidCodeAddr(Const Addr: Pointer): LongBool;
Function IsValidStackAddr(Const Addr: Pointer; const ThreadID: TThreadId): LongBool;
Function IsValidDataAddr(Const Addr: Pointer; const ThreadID: TThreadId): LongBool;
// Property SourceDirs: String read FSourceDirs;
Property Dirs[const SourceType: TUnitType]: TDbgSourceDirList Read GetDirs;
Property Segments: TStringList Read FSegments;
Property Units: TStringList Read FUnits;
Property UnitsByAddr: TSegmentCodeInfoList read FUnitsByAddr;
Property DbgLog: TDbgLog read FDbgLog;
property DebugInfoLoaded: LongBool read FDebugInfoLoaded;
property DebugInfoType: String read FDebugInfoType;
property DebugInfoProgressCallback: TDebugInfoProgressCallback read FDebugInfoProgressCallback write FDebugInfoProgressCallback;
property UseShortNames: LongBool read FUseShortNames write FUseShortNames;
property MemoryManagerInfo: TMemoryManagerInfo read FMemoryManagerInfo;
property RTLInfo: TRTLInfo read FRTLInfo;
End;
const
// TODO: for MACOS '_text'
SegmentTypeNames: array [TSegmentType] of String =
('', 'text', 'itext', 'data', 'bss', 'tls', 'pdata', 'idata', 'didata', 'rdata', 'reloc', 'rsrc');
var
gvDebugInfo: TDebugInfo = nil;
Implementation
Uses
ClassUtils, Variants, IOUtils, Types, System.AnsiStrings;
function IncPointer(Ptr: Pointer; Offset: Integer): Pointer; inline;
begin
Result := Pointer(Integer(Ptr) + Offset);
end;
// SimpleStringHash and SimpleLowerCaseStringHash are taken from DWScript
// The code is probably under copyright of Eric Grange
function SimpleStringHash(const s : UnicodeString) : Cardinal; inline;
var
i : Integer;
begin
// modified FNV-1a using length as seed
Result:=Length(s);
for i:=1 to Result do
Result:=(Result xor Ord(s[i]))*16777619;
end;
function SimpleLowerCaseStringHash(const s : UnicodeString) : Cardinal;
function Fallback(const s : UnicodeString) : Cardinal;
begin
Result:=SimpleStringHash(LowerCase(s));
end;
var
i : Integer;
c : Word;
begin
// modified FNV-1a using length as seed
Result:=Length(s);
for i:=1 to Result do begin
c:=Ord(s[i]);
if c>127 then
Exit(Fallback(s))
else if c in [Ord('A')..Ord('Z')] then
c:=c+(Ord('a')-Ord('A'));
Result:=(Result xor c)*16777619;
end;
end;
{ TDebugInfo }
Constructor TDebugInfo.Create;
var
ST: TUnitType;
Begin
Inherited Create;
for ST := Low(TUnitType) to High(TUnitType) do
FDirs[ST] := TDbgSourceDirList.Create;
FSegments := TStringList.Create;
FSegments.OwnsObjects := True;
FUnits := TStringList.Create;
FUnitsByAddr := TSegmentCodeInfoList.Create;
FDbgLog := TDbgLog.Create;
FExeFileName := '';
FDebugInfoLoaded := False;
FDebugInfoType := '';
FDebugInfoProgressCallback := Nil;
FLastProgressAction := '';
FLastProgress := 0;
FUseShortNames := True;
FMemoryManagerInfo := TMemoryManagerInfo.Create;
FRTLInfo := TRTLInfo.Create;
End;
Destructor TDebugInfo.Destroy;
var
ST: TUnitType;
Begin
ClearDebugInfo;
FreeAndNil(FUnitsByAddr);
FreeAndNil(FUnits);
FreeAndNil(FSegments);
for ST := Low(TUnitType) to High(TUnitType) do
FreeAndNil(FDirs[ST]);
FreeAndNil(FDbgLog);
FreeAndNil(FMemoryManagerInfo);
FreeAndNil(FRTLInfo);
Inherited Destroy;
End;
procedure TDebugInfo.DoProgress(const Action: String; const Progress: Integer);
begin
if (FLastProgressAction <> Action) or (FLastProgress <> Progress) then
begin
FLastProgressAction := Action;
FLastProgress := Progress;
if Assigned(FDebugInfoProgressCallback) then
FDebugInfoProgressCallback(Action, Progress);
end;
end;
procedure TDebugInfo.AddSourceDir(const SourceDir: TDbgSourceDirList; const Dir: String; const Recursive: LongBool);
const
_PAS_EXTS: array [0 .. 2] of String = ('*.pas', '*.inc', '*.dpr');
var
Files: TStringDynArray;
J, I: Integer;
FileName: String;
begin
for J := 0 to High(_PAS_EXTS) do
begin
Files := TDirectory.GetFiles(Dir, _PAS_EXTS[J], TSearchOption.soAllDirectories);
if Length(Files) > 0 then
for I := 0 to High(Files) do
begin
FileName := Files[I];
SourceDir.Add(FileName);
end;
end;
end;
Function TDebugInfo.CheckAddr(Const Addr: Pointer): LongBool;
Var
UnitInfo: TUnitInfo;
FuncInfo: TFuncInfo;
LineInfo: TLineInfo;
Begin
Result := GetLineInfo(Addr, UnitInfo, FuncInfo, LineInfo, False) <> slNotFound;
End;
Function TDebugInfo.CheckDebugException(ExceptionRecord: PExceptionRecord; Var IsTraceException: LongBool): LongBool;
Begin
IsTraceException := False;
Case ExceptionRecord^.ExceptionCode Of
EXCEPTION_SET_THREAD_NAME, STATUS_NONCONTINUABLE_EXCEPTION:
Result := True;
Else
Result := False;
End;
End;
Procedure TDebugInfo.ClearDebugInfo;
Begin
If FDebugInfoLoaded Then
Begin
FDebugInfoLoaded := False;
ClearDirs;
FUnitsByAddr.Clear;
ClearStringList(FUnits);
FDbgLog.ClearLog;
FMemoryManagerInfo.Clear;
FRTLInfo.Clear;
FExeFileName := '';
End;
End;
procedure TDebugInfo.ClearDirs;
var
ST: TUnitType;
begin
for ST := Low(TUnitType) to High(TUnitType) do
FDirs[ST].Clear;
end;
Function TDebugInfo.ReadDebugInfo(Const FileName: String): LongBool;
Begin
Result := FDebugInfoLoaded And SameText(FExeFileName, FileName);
If Not Result Then
Begin
Result := DoReadDebugInfo(FileName, True);
If Result Then
Begin
FExeFileName := FileName;
FDebugInfoLoaded := True;
End;
End;
End;
procedure TDebugInfo.UpdateSourceDirs(const SourceType: TUnitType; const SourceDirs: String);
var
SL: TStringList;
I: Integer;
S: String;
begin
FDirs[SourceType].Clear;
SL := TStringList.Create;
try
SL.Delimiter := ';';
SL.StrictDelimiter := True;
SL.Duplicates := dupIgnore;
SL.DelimitedText := SourceDirs;
for I := 0 to SL.Count - 1 do
begin
S := Trim(SL[I]);
if (S <> '') then
begin
S := ExcludeTrailingPathDelimiter(S);
if DirectoryExists(S) then
AddSourceDir(FDirs[SourceType], S, True);
end;
end;
finally
FreeAndNil(SL);
end;
end;
Function TDebugInfo.DumpLineInformation(Const Addr: Pointer): String;
Var
UnitName: String;
FuncName: String;
Line: Integer;
Begin
If GetLineInformation(Addr, UnitName, FuncName, Line, False) <> slNotFound Then
Result := Format('%p: %s@%s(%d)', [Pointer(Addr), UnitName, FuncName, Line])
Else
Result := Format('%p: no source info', [Pointer(Addr)]);
End;
function TDebugInfo.FullUnitName(const UnitName: String): String;
const
_PAS = '.pas';
_INC = '.inc';
_DPR = '.dpr';
var
Res: String;
ResExt: String;
ST: TUnitType;
begin
for ST := Low(TUnitType) to High(TUnitType) do
begin
Res := AnsiLowerCase(UnitName);
ResExt := ExtractFileExt(Res);
if (Length(ResExt) <> 4) or ((ResExt <> _PAS) and (ResExt <> _INC) and (ResExt <> _DPR)) then
Res := Res + _PAS;
if not FDirs[ST].TryGetValue(Res, Result) then
begin
if not SameStr(ResExt, _PAS) then
begin
Res := ChangeFileExt(Res, _PAS);
if FDirs[ST].TryGetValue(Res, Result) then
Exit;
end;
if not SameStr(ResExt, _INC) then
begin
Res := ChangeFileExt(Res, _INC);
if FDirs[ST].TryGetValue(Res, Result) then
Exit;
end;
if not SameStr(ResExt, _DPR) then
begin
Res := ChangeFileExt(Res, _DPR);
if FDirs[ST].TryGetValue(Res, Result) then
Exit;
end;
end
else
Exit;
end;
Result := UnitName;
end;
function TDebugInfo.FuncByName(const FuncName: AnsiString): TFuncInfo;
var
I: Integer;
begin
for I := 0 to Units.Count - 1 do
begin
Result := TUnitInfo(Units.Objects[I]).FindFuncByName(FuncName);
if Assigned(Result) then
Exit;
end;
Result := Nil;
end;
Function TDebugInfo.GetFileCount: Integer;
Begin
Result := FUnits.Count;
End;
function TDebugInfo.GetLineInformation(const Addr: Pointer; var UnitName, FuncName: String; var Line: Integer; GetPrevLine: LongBool): TFindResult;
Var
UnitInfo: TUnitInfo;
FuncInfo: TFuncInfo;
LineInfo: TLineInfo;
Begin
UnitName := '';
FuncName := '';
Line := -1;
Result := GetLineInfo(Addr, UnitInfo, FuncInfo, LineInfo, GetPrevLine);
If Result <> slNotFound Then
Begin
UnitName := UnitInfo.FullUnitName;
FuncName := String(FuncInfo.Name);
If LineInfo <> Nil Then
Line := LineInfo.LineNo;
End;
end;
Function TDebugInfo.GetFile(Index: Integer): String;
Begin
Result := TUnitInfo(FUnits[Index]).FullUnitName;
End;
Function TDebugInfo.GetParamsStr(FuncInfo: TFuncInfo; Const EBP: Pointer; IsTopStack: LongBool): String;
// Var
// I : Integer;
// ToStringData : TToStringData;
// ParamName : String;
// ParamValue : TVarInfo;
// ParamEval : Variant;
// ParamRes : String;
Begin
// Result := '';
//
// If (FuncInfo <> Nil) And (FuncInfo.Params.Count > 0) Then
// Begin
// ToStringData.DebuggeeControl := DebuggeeControl;
// ToStringData.DebugInfo := Self;
// ToStringData.Mode := tsmBrief;
// ToStringData.RecursionLevel := 0;
//
// For I := 0 To FuncInfo.Params.Count - 1 Do
// Begin
// ParamName := FuncInfo.Params[I];
// ParamValue := TVarInfo(FuncInfo.Params.Objects[I]);
//
// ParamRes := '???';
//
// If (EBP <> 0) And ((ParamValue.VarKind In [vkGlobal, vkStack]) Or (IsTopStack And (ParamValue.VarKind = vkRegister))) Then
// Begin
// Try
// ParamEval := EvaluateVariable(DebuggeeControl, ParamValue, EBP, False);
// ParamRes := VariantToString(ParamEval, ToStringData);
// Except
// ParamRes := '[error]';
// End;
// End;
//
// If Result <> '' Then Result := Result + ', ';
// Result := Result + Format('%s=%s', [ParamName, ParamRes]);
// End;
//
// If Result <> '' Then
// Result := '(' + Result + ')';
// End;
End;
function TDebugInfo.GetSegmentByID(const ID: Word): TSegmentClassInfo;
var
Idx: Integer;
begin
for Idx := 0 to Segments.Count - 1 do
begin
Result := TSegmentClassInfo(Segments.Objects[Idx]);
if Result.ID = ID then
Exit;
end;
Result := Nil;
end;
function TDebugInfo.GetSegmentByType(const SegType: TSegmentType): TSegmentClassInfo;
var
Idx: Integer;
begin
for Idx := 0 to Segments.Count - 1 do
begin
Result := TSegmentClassInfo(Segments.Objects[Idx]);
if Result.SegType = SegType then
Exit;
end;
Result := Nil;
end;
Function TDebugInfo.GetTypeInfo(Const TypeName: String): TTypeInfo;
Var
UnitInfo: TUnitInfo;
I: Integer;
Begin
Result := Nil;
For I := 0 To Units.Count - 1 Do
Begin
UnitInfo := TUnitInfo(Units.Objects[I]);
Result := UnitInfo.FindTypeByName(AnsiString(TypeName));
if Result <> Nil then
Exit;
End;
End;
function TDebugInfo.GetUnitType(const UnitName: String): TUnitType;
begin
for Result := Low(TUnitType) to High(TUnitType) do
begin
if FDirs[Result].Contains(AnsiLowerCase(UnitName)) then
Exit;
end;
if CheckSystemFile(UnitName) then
begin
Result := utSystem;
Exit;
end;
Result := utUnknown;
end;
Function TDebugInfo.GetExceptionName(ExceptionRecord: PExceptionRecord): String;
Begin
case ExceptionRecord^.ExceptionCode of
STATUS_ACCESS_VIOLATION:
Result := 'EACCESS_VIOLATION';
STATUS_ARRAY_BOUNDS_EXCEEDED:
Result := 'EARRAY_BOUNDS_EXCEEDED';
STATUS_FLOAT_DENORMAL_OPERAND:
Result := 'EFLOAT_DENORMAL_OPERAND';
STATUS_FLOAT_DIVIDE_BY_ZERO:
Result := 'EFLOAT_DIVIDE_BY_ZERO';
STATUS_FLOAT_INEXACT_RESULT:
Result := 'EFLOAT_INEXACT_RESULT';
STATUS_FLOAT_INVALID_OPERATION:
Result := 'EFLOAT_INVALID_OPERATION';
STATUS_FLOAT_OVERFLOW:
Result := 'EFLOAT_OVERFLOW';
STATUS_FLOAT_STACK_CHECK:
Result := 'EFLOAT_STACK_CHECK';
STATUS_FLOAT_UNDERFLOW:
Result := 'EFLOAT_UNDERFLOW';
STATUS_INTEGER_DIVIDE_BY_ZERO:
Result := 'EINTEGER_DIVIDE_BY_ZERO';
STATUS_INTEGER_OVERFLOW:
Result := 'EINTEGER_OVERFLOW';
STATUS_PRIVILEGED_INSTRUCTION:
Result := 'EPRIVILEGED_INSTRUCTION';
STATUS_STACK_OVERFLOW:
Result := 'ESTACK_OVERFLOW';
STATUS_CONTROL_C_EXIT:
Result := 'ECONTROL_C_EXIT';
else
Result := Format('$%x', [ExceptionRecord^.ExceptionCode]);
end;
End;
Function TDebugInfo.GetExceptionAddress(ExceptionRecord: PExceptionRecord): Pointer;
Begin
Result := ExceptionRecord^.ExceptionAddress;