forked from yavfast/dbg-spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebugerTypes.pas
1999 lines (1600 loc) · 50.7 KB
/
DebugerTypes.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 DebugerTypes;
interface
uses System.SysUtils, WinApi.Windows, System.Classes, JclPeImage, System.SyncObjs, ClassUtils, DbgHookTypes, System.Contnrs,
System.Generics.Collections, Collections.Dictionaries, Collections.Queues, CollectList,
uSharedObject;
type
TSysUInt = NativeUInt;
PReal48 = ^Real48;
PReal = ^Real;
const
EXCEPTION_SET_THREAD_NAME = $406D1388;
const
_SERVICE_THREAD_PREFIX = '###';
// Ôëàãè ïðîöåññîðà
const
EFLAGS_CF = $001;
EFLAGS_PF = $004;
EFLAGS_AF = $010;
EFLAGS_ZF = $040;
EFLAGS_SF = $080;
EFLAGS_TF = $100;
EFLAGS_IF = $200;
EFLAGS_DF = $400;
EFLAGS_OF = $800;
const
BPOpcode: Byte = $CC;
type
TThreadId = type Cardinal;
TProcessId = type Cardinal;
EDebugCoreException = class(Exception);
// Äåéñòâèÿ âûïîëíÿåìûå îòëàä÷èêîì ïîñëå ãåíåðàöèè ñîáûòèÿ
TResumeAction = (
raRun, // ïðîäîëæèòü âûïîëíåíèå
raTraceInto, // âûïîëíèòü òðàññèðîâêó îäíîé èíñòðóêöèè ñ çàõîäîì âíóòðü ôóíêöèé
raStepOver, // âûïîëíèòü òðàññèðîâêó îäíîé èíñòðóêöèè áåç çàõîäà âíóòðü ôóíêöèé
raRunUntilReturn, // ïðîäîëæèòü âûïîëíåíèå äî âûõîäà èç òåêóùåé ïðîöåäóðû
raStop // îñòàíîâèòü îòëàäêó
);
// Äåêëàðàöèÿ îñíîâíûõ îáðàáîò÷èêîâ
TCreateThreadEvent = procedure(Sender: TObject; ThreadId: TThreadId; Data: PCreateThreadDebugInfo) of object;
TCreateProcessEvent = procedure(Sender: TObject; ProcessId: TProcessId; Data: PCreateProcessDebugInfo) of object;
TExitThreadEvent = procedure(Sender: TObject; ThreadId: TThreadId; Data: PExitThreadDebugInfo) of object;
TExitProcessEvent = procedure(Sender: TObject; ProcessId: TProcessId; Data: PExitProcessDebugInfo) of object;
TLoadDllEvent = procedure(Sender: TObject; ThreadId: TThreadId; Data: PLoadDLLDebugInfo) of object;
TUnLoadDllEvent = procedure(Sender: TObject; ThreadId: TThreadId; Data: PUnloadDLLDebugInfo) of object;
TDebugStringEvent = procedure(Sender: TObject; ThreadId: TThreadId; Data: POutputDebugStringInfo) of object;
TRipEvent = procedure(Sender: TObject; ThreadId: TThreadId; Data: PRIPInfo) of object;
TDbgLogEvent = procedure(Sender: TObject; ThreadId: TThreadId; const Data: String) of object;
// Äåêëàðàöèÿ îáðàáîò÷èêîâ îòëàäî÷íûõ èñêëþ÷åíèé
TDefaultExceptionEvent = procedure(Sender: TObject; ThreadId: TThreadId; ExceptionRecord: PExceptionRecord) of object;
TBreakPointEvent = procedure(Sender: TObject; ThreadId: TThreadId; ExceptionRecord: PExceptionRecord;
BreakPointIndex: Integer; var ReleaseBreakpoint: LongBool) of object;
// Ñïèñîê òèïîâ îòëàäî÷íûõ èñêëþ÷åíèé (äëÿ âíóòðåííåé ðàáîòû îòëàä÷èêà,
// ïîëüçîâàòåëü ñ íèìè íå ðàáîòàåò)
TExceptionCode = (ecUnknown, ecBreakpoint, ecSingleStep, ecCtrlC, ecNonContinuable, ecGuard, ecSetThreadName);
TExceptionEvents = array[TExceptionCode] of TDefaultExceptionEvent;
// Ñïèñîê ïîääåðæèâàåìûõ òèïîâ òî÷åê îñòàíîâêè (äàëåå ÂÐ)
TBreakpointType = (
btUser,
btTemp,
btCodeTrack,
btMemory
);
// ñòðóêòóðû äëÿ õðàíåíèÿ äàííûõ îá èçâåñòíûõ îòëàä÷èêó ÂÐ
TInt3Breakpoint = record
Address: Pointer;
SaveByte: Byte;
end;
TMemotyBreakPoint = record
Address: Pointer;
Size: Cardinal;
BreakOnWrite: LongBool;
RegionStart: Pointer;
RegionSize: Cardinal;
PreviosRegionProtect: Cardinal;
end;
PBreakpoint = ^TBreakpoint;
TBreakpoint = packed record
bpType: TBreakpointType;
ThreadId: TThreadId;
Description: ShortString;
Active: LongBool;
case Integer of
0: (Int3: TInt3Breakpoint);
1: (Memory: TMemotyBreakPoint);
end;
TBreakpointList = array of TBreakpoint;
TTrackBreakpointType = (tbTrackFunc, tbMemInfo);
TTrackBreakpointTypes = set of TTrackBreakpointType;
PTrackBreakpoint = ^TTrackBreakpoint;
TTrackBreakpoint = record
FuncInfo: TObject;
SaveByte: Byte;
BPType: TTrackBreakpointTypes;
end;
TTrackBreakpointList = TPointerDictionary<Pointer,PTrackBreakpoint>;
PTrackRETBreakpoint = ^TTrackRETBreakpoint;
TTrackRETBreakpoint = record
FuncInfo: TObject;
Count: Cardinal;
SaveByte: Byte;
BPType: TTrackBreakpointTypes;
end;
TTrackRETBreakpointList = TPointerDictionary<Pointer,PTrackRETBreakpoint>;
THWBPIndex = 0..3;
THWBPSize = (hsByte, hdWord, hsDWord);
THWBPMode = (hmExecute, hmWrite, hmIO, hmReadWrite);
PHardwareBreakpoint = ^THardwareBreakpoint;
THardwareBreakpoint = packed record
Address: array [THWBPIndex] of Pointer;
Size: array [THWBPIndex] of THWBPSize;
Mode: array [THWBPIndex] of THWBPMode;
Description: array [THWBPIndex] of ShortString;
Active: array [THWBPIndex] of LongBool;
end;
(*
PStackPoint = ^TStackPoint;
TStackPoint = packed record
EIP: Pointer;
EBP: Pointer;
end;
TStackPointList = Array of TStackPoint;
*)
TDbgInfoStack = array of Pointer;
PDbgInfoStackRec = ^TDbgInfoStackRec;
TDbgInfoStackRec = record
Stack: TDbgInfoStack;
end;
TMemAction = (maGetMem = 0, maFreeMem);
PMemInfo = ^TMemInfo;
TMemInfo = packed record
//PerfIdx: Cardinal;
case MemAction: TMemAction of
maGetMem: (
GetMemPtr: Pointer;
GetMemSize: Cardinal;
//Stack: TStackPointList;
);
maFreeMem: (
FreeMemPtr: Pointer;
//ObjType: Cardinal;
);
end;
//PGetMemInfo = ^TGetMemInfo;
TGetMemInfo = class(TSharedObject)
public
PerfIdx: Integer;
ObjAddr: Pointer;
Size: Integer;
Stack: TDbgInfoStack;
ObjectType: String;
function GetObjectType: String;
procedure CheckObjectType;
procedure LoadStack(const DbgStack: PDbgHookInfoStack);
end;
TGetMemInfoList = TObjectDictionary<Pointer,TGetMemInfo>;
TGetMemInfoItem = TPair<Pointer,TGetMemInfo>;
PDbgSyncObjsInfoEx = ^TDbgSyncObjsInfoEx;
TDbgSyncObjsInfoEx = packed record
public
procedure LoadStack(const DbgStack: PDbgHookInfoStack);
procedure Init(const SyncObjsInfo: PDbgSyncObjsInfo);
public
Id: NativeUInt;
ThreadId: TThreadId;
CurTime: Int64;
Stack: TDbgInfoStack;
SyncObjsStateType: TDbgSyncObjsStateType;
case SyncObjsType: TDbgSyncObjsType of
soUnknown:
();
soSleep:
(
MSec: NativeUInt;
);
soWaitForSingleObject:
(
Handle: THandle;
);
soWaitForMultipleObjects:
(
Handles: PWOHandleArray;
);
soEnterCriticalSection,
soLeaveCriticalSection,
soInCriticalSection:
(
CS: PRTLCriticalSection;
OwningThreadId: TThreadId;
);
soSendMessage:
();
end;
PSyncObjsInfo = ^RSyncObjsInfo;
RSyncObjsInfo = record
PerfIdx: Integer;
Link: PSyncObjsInfo; // Ëèíê íà ïàðó
LinkExt: PSyncObjsInfo; // Âíåøíèé ëèíê íà äðóãóþ ïàðó
SyncObjsInfo: TDbgSyncObjsInfoEx;
public
function WaitTime: Int64;
function IsShortLock: LongBool;
function Enter: PSyncObjsInfo; inline;
function Leave: PSyncObjsInfo; inline;
function EnterExt: PSyncObjsInfo;
function LeaveExt: PSyncObjsInfo;
end;
TSyncObjsInfoList = TBaseCollectList; // TCollectList<RSyncObjsInfo>;
PRPSyncObjsInfo = ^RPSyncObjsInfo;
RPSyncObjsInfo = record
SyncObjsInfo: PSyncObjsInfo;
end;
TSyncObjsInfoListByID = TDictionary<NativeUInt, PSyncObjsInfo>;
TSyncObjsInfoListByCS = TDictionary<Pointer, PSyncObjsInfo>;
// Ñïèñîê SyncObjs, êîòîðûå âûçûâàþòñÿ èç òåêóùåé ôóíêöèè èëè âëîæåííûõ
TFuncSyncObjsInfoList = TCollectList<RPSyncObjsInfo>;
PThreadData = ^TThreadData;
TExceptInfo = class
public
ThreadID: TThreadId;
Address: Pointer;
Frame: Pointer;
ExceptionName: String;
Message: String;
Stack: TList;
constructor Create(DebugEvent: PDebugEvent); overload;
constructor Create(ThreadData: PThreadData); overload;
constructor Create(); overload;
destructor Destroy; override;
end;
TSyncObjsInfo = class
public
SyncObjsType: TDbgSyncObjsType;
SyncObjsStateType: TDbgSyncObjsStateType;
PerfIdx: Cardinal;
Id: NativeUInt;
Data: NativeUInt;
Link: TSyncObjsInfo;
constructor Create(const DebugEvent: PDebugEvent; const ThreadData: PThreadData; const APerfIdx: Cardinal);
destructor Destroy; override;
function FindLink(const ThreadData: PThreadData): TSyncObjsInfo;
end;
TPerfInfo = class
public
DeltaTickCPU: UInt64; // çàãðóçêà CPU
DeltaTime: UInt64;
//StackPoint: TStackPointList;
end;
TDbgPointType = (ptNone = 0, ptWait, ptStart, ptStop, ptException, ptPerfomance, ptThreadInfo, ptMemoryInfo,
ptSyncObjsInfo, ptTraceInfo);
PThreadPoint = ^TThreadPoint;
TThreadPoint = packed record
public
procedure Clear;
public
PerfIdx: Cardinal;
case PointType: TDbgPointType of
ptStart: ();
ptStop: ();
ptException: (
ExceptInfo: TExceptInfo;
);
ptPerfomance: (
PerfInfo: TPerfInfo;
);
ptMemoryInfo: (
);
ptSyncObjsInfo: (
SyncObjsInfo: TSyncObjsInfo;
);
end;
TThreadAdvType = (tatUnknown = 0, tatService, tatNormal);
PThreadAdvInfo = ^TThreadAdvInfo;
TThreadAdvInfo = record
ThreadId: TThreadId;
ThreadParentId: TThreadId;
ThreadData: PThreadData;
ThreadClassName: String;
ThreadName: String;
ThreadAdvType: TThreadAdvType;
function AsString: String;
end;
TCallFuncInfo = class
FuncInfo: TObject;
LineNo: Cardinal;
CallCount: UInt64;
Data: UInt64;
public
property Elapsed: UInt64 read Data write Data;
property Size: UInt64 read Data write Data;
end;
TCallFuncCounter = class(TObjectDictionary<Pointer,TCallFuncInfo>)
private
function AddNewCallFunc(const Addr: Pointer): TCallFuncInfo;
public
function AddCallFunc(const Addr: Pointer): TCallFuncInfo;
function GetCallFunc(const Addr: Pointer): TCallFuncInfo;
end;
TCallFuncCounterPair = TPair<Pointer,TCallFuncInfo>;
TTrackUnitInfo = class;
TTrackFuncInfo = class;
TTrackUnitInfoBaseList = TObjectDictionary<TObject,TTrackUnitInfo>;
TTrackFuncInfoBaseList = TObjectDictionary<TObject,TTrackFuncInfo>;
TTrackUnitInfo = class
private
FUnitInfo: TObject;
FFuncInfoList: TTrackFuncInfoBaseList;
FCallCount: Int64;
public
constructor Create(AUnitInfo: TObject);
destructor Destroy; override;
procedure IncCallCount; inline;
property UnitInfo: TObject read FUnitInfo;
property FuncInfoList: TTrackFuncInfoBaseList read FFuncInfoList;
property CallCount: Int64 read FCallCount;
end;
TCodeTrackUnitInfo = class(TTrackUnitInfo)
private
FElapsed: Int64;
public
procedure GrowElapsed(const Value: Int64); inline;
// TODO: âûñ÷èòàòü îáùåå âðåìÿ âûïîëíåíèÿ ôóíêöèé þíèòà ñ ó÷åòîì âëîæåííîñòè
// property Elapsed: UInt64 read FElapsed;
end;
TMemInfoTrackUnitInfo = class(TTrackUnitInfo)
private
FCurCount: Int64;
FSize: Int64;
public
procedure GrowSize(const Value: Int64); inline;
procedure IncCurCount; inline;
procedure DecCurCount; inline;
property CurCount: Int64 read FCurCount;
end;
TSyncObjsTrackUnitInfo = class(TTrackUnitInfo);
TTrackUnitInfoList = class(TTrackUnitInfoBaseList)
protected
function CreateTrackUnitInfo(const UnitInfo: TObject): TTrackUnitInfo; virtual;
public
function GetTrackUnitInfo(const UnitInfo: TObject): TTrackUnitInfo;
procedure CheckTrackFuncInfo(TrackFuncInfo: TTrackFuncInfo);
end;
TTrackUnitInfoPair = TPair<TObject,TTrackUnitInfo>;
TCodeTrackUnitInfoList = class(TTrackUnitInfoList)
protected
function CreateTrackUnitInfo(const UnitInfo: TObject): TTrackUnitInfo; override;
public
end;
TMemInfoTrackUnitInfoList = class(TTrackUnitInfoList)
protected
function CreateTrackUnitInfo(const UnitInfo: TObject): TTrackUnitInfo; override;
public
procedure LoadStack(const GetMemInfo: TGetMemInfo);
end;
TSyncObjsTrackUnitInfoList = class(TTrackUnitInfoList)
protected
function CreateTrackUnitInfo(const UnitInfo: TObject): TTrackUnitInfo; override;
public
procedure LoadStack(const SyncObjsInfo: PSyncObjsInfo);
end;
TTrackFuncInfo = class
private
FFuncInfo: TObject;
FTrackUnitInfo: TTrackUnitInfo;
FCallCount: UInt64;
FParentFuncs: TCallFuncCounter;
FChildFuncs: TCallFuncCounter;
public
constructor Create(AFuncInfo: TObject);
destructor Destroy; override;
function AddParentCall(const Addr: Pointer): TCallFuncInfo; inline;
function AddChildCall(const Addr: Pointer): TCallFuncInfo; inline;
procedure IncCallCount; inline;
property FuncInfo: TObject read FFuncInfo;
property TrackUnitInfo: TTrackUnitInfo read FTrackUnitInfo write FTrackUnitInfo;
property ParentFuncs: TCallFuncCounter read FParentFuncs;
property ChildFuncs: TCallFuncCounter read FChildFuncs;
property CallCount: UInt64 read FCallCount;
end;
TMemInfoTrackFuncInfo = class(TTrackFuncInfo)
private
FCurCount: Int64;
FSize: Int64;
FGetMemList: TGetMemInfoList;
public
constructor Create(AFuncInfo: TObject);
destructor Destroy; override;
procedure GrowSize(const Value: Int64); inline;
procedure IncCurCount; inline;
procedure DecCurCount; inline;
procedure AddGetMemInfo(const GetMemInfo: TGetMemInfo);
property CurCount: Int64 read FCurCount;
property Size: Int64 read FSize;
property GetMemList: TGetMemInfoList read FGetMemList;
end;
TCodeTrackFuncInfo = class(TTrackFuncInfo)
private
FCPUElapsed: UInt64;
public
procedure GrowElapsed(const Value: UInt64); inline;
property CPUElapsed: UInt64 read FCPUElapsed;
end;
TSyncObjsTrackFuncInfo = class(TTrackFuncInfo)
private
FWaitTime: Int64;
FSyncObjsList: TFuncSyncObjsInfoList;
public
constructor Create(AFuncInfo: TObject);
destructor Destroy; override;
procedure GrowWaitTime(const Value: Int64); inline;
property WaitTime: Int64 read FWaitTime;
property SyncObjsList: TFuncSyncObjsInfoList read FSyncObjsList;
end;
TTrackFuncInfoList = class(TTrackFuncInfoBaseList)
protected
function CreateTrackFuncInfo(const FuncInfo: TObject): TTrackFuncInfo; virtual;
public
function GetTrackFuncInfo(const FuncInfo: TObject): TTrackFuncInfo;
end;
TTrackFuncInfoPair = TPair<TObject,TTrackFuncInfo>;
TMemInfoTrackFuncInfoList = class(TTrackFuncInfoList)
protected
function CreateTrackFuncInfo(const FuncInfo: TObject): TTrackFuncInfo; override;
end;
TCodeTrackFuncInfoList = class(TTrackFuncInfoList)
protected
function CreateTrackFuncInfo(const FuncInfo: TObject): TTrackFuncInfo; override;
end;
TSyncObjsTrackFuncInfoList = class(TTrackFuncInfoList)
protected
function CreateTrackFuncInfo(const FuncInfo: TObject): TTrackFuncInfo; override;
end;
PTrackStackPoint = ^TTrackStackPoint;
TTrackStackPoint = record
private
function GetLeave: UInt64;
procedure SetLeave(const Value: UInt64);
public
TrackFuncInfo: TCodeTrackFuncInfo;
ParentTrackFuncInfo: TCodeTrackFuncInfo;
ProcTrackFuncInfo: TCodeTrackFuncInfo;
ProcParentTrackFuncInfo: TCodeTrackFuncInfo;
TrackRETBreakpoint: PTrackRETBreakpoint;
Enter: UInt64;
Elapsed: UInt64;
property Leave: UInt64 read GetLeave write SetLeave;
end;
TFastStack<T> = class
private
FTop: Integer;
FItems: array of T;
procedure Grow;
function GetCount: Integer; inline;
public
constructor Create(const ACapacity: Integer = 64);
function Push: Pointer;
function Pop: Pointer;
property Count: Integer read GetCount;
end;
TTrackStack = TFastStack<TTrackStackPoint>;
TThreadAdvInfoList = TBaseCollectList; //TCollectList<TThreadAdvInfo>;
TThreadPointList = TBaseCollectList; //TCollectList<TThreadPoint>;
TThreadMemInfoList = TBaseCollectList; //TCollectList<TMemInfo>;
TThreadState = (tsNone, tsActive, tsFinished, tsSuspended, tsLocked);
TThreadData = packed record
ThreadID: TThreadId;
State: TThreadState;
ThreadHandle: THandle;
ThreadAdvInfo: PThreadAdvInfo;
Context: PContext; // Óêàçàòåëü äîëæåí áûòü âûðîâíåí ïî ïàìÿòè 32 áèò
Breakpoint: PHardwareBreakpoint;
Started: Int64; // ìîìåíò çàïóñêà
Elapsed: Int64; // âðåìÿ âûïîëíåíèÿ
CPUElapsed: UInt64; // öèêëû CPU
CPUTime: UInt64; // âðåìÿ èñïîëüçîâàíèÿ CPU
SamplingCPUTime: UInt64;
SamplingCount: Cardinal;
SamplingQueue: TQueue<PDbgInfoStackRec>;
WaitTime: Int64; // Âðåìÿ áëîêèðîâîê
DbgPoints: TThreadPointList;
DbgGetMemInfo: TGetMemInfoList;
DbgGetMemInfoSize: Int64;
DbgSyncObjsInfo: TSyncObjsInfoList;
DbgSyncObjsInfoByID: TSyncObjsInfoListByID;
DbgGetMemUnitList: TMemInfoTrackUnitInfoList;
DbgSyncObjsUnitList: TSyncObjsTrackUnitInfoList;
//DbgGetMemFuncList: TTrackFuncInfoList;
DbgExceptions: TThreadList;
DbgTrackEventCount: Int64;
DbgTrackUnitList: TCodeTrackUnitInfoList;
DbgTrackFuncList: TCodeTrackFuncInfoList;
DbgTrackStack: TTrackStack;
DbgTrackUsedUnitList: TTrackUnitInfoList; // Âðåìåííûé ñïèñîê èñïîëüçóåìûõ þíèòîâ ïðè îáðàáîòêå ñòåêà
function DbgPointsCount: Cardinal;
function DbgPointByIdx(const Idx: Integer): PThreadPoint;
function DbgExceptionsCount: Cardinal;
function DbgExceptionsByIdx(const Idx: Cardinal): TExceptInfo;
function DbgSyncObjsCount: Cardinal;
function DbgSyncObjsByIdx(const Idx: Integer): PSyncObjsInfo;
procedure UpdateGetMemUnitList;
procedure Init;
procedure Clear;
end;
TDbgThreadList = TBaseCollectList; //TCollectList<TThreadData>;
TDbgActiveThreadList = TDictionary<TThreadId, PThreadData>;
TDbgActiveThreads = array of PThreadData;
PProcessPoint = ^TProcessPoint;
TProcessPoint = packed record
FromStart: Int64; // êîë-âî òèêîâ ñî ñòàðòà
CPUTime: UInt64; // òåêóùåå âðåìÿ CPU
case PointType: TDbgPointType of
ptStart: ();
ptStop: ();
ptException: ();
ptPerfomance: (
//DeltaTick: Int64;
//DeltaTickCPU: UInt64;
DeltaTime: UInt64;
);
ptMemoryInfo: ();
end;
TProcessPointList = TBaseCollectList; //TCollectList<TProcessPoint>;
// ñòðóêòóðà õðàíÿùàÿ äàííûå î îòëàæèâàåìîì ïðîöåññå.
// èñïîëüçóåòñÿ îòëàä÷èêîì, äîñòóïíà ïîëüçîâàòåëþ ÷åðåç ñâîéñòâî DebugProcessData
// Âíèìàíèå !!!
// Äàííàÿ èíôîðìàöèÿ ìîæåò èñïîëüçîâàòüñÿ ïîëüçîâàòåëåì òîëüêî íà ÷òåíèå,
// íå çàêðûâàéòå õýíäëû îïèñàííûå â äàííîé ñòðóêòóðå ÷åðåç CloseHandle()
TProcessState = (psNone, psActive, psFinished);
//PProcessData = ^TProcessData;
TProcessData = class
public
ProcessID: TProcessId;
State: TProcessState;
StartAddress: Pointer;
BaseOfImage: Pointer;
MainThreadID: TThreadId;
PEImage: TJclPeImage;
Started: Int64;
Elapsed: Int64;
CPUTime: UInt64;
CPUElapsed: UInt64; // âðåìÿ èñïîëüçîâàíèÿ CPU
SamplingCPUTime: UInt64;
SamplingCount: Int64;
DbgPoints: TProcessPointList;
DbgGetMemInfo: TGetMemInfoList; // Óêàçàòåëè ñ êîëëèçèÿìè
DbgGetMemInfoSize: Cardinal;
ProcessGetMemCount: Int64;
ProcessGetMemSize: Int64;
DbgExceptions: TThreadList;
DbgTrackEventCount: Int64;
DbgTrackUnitList: TCodeTrackUnitInfoList;
DbgTrackFuncList: TCodeTrackFuncInfoList;
DbgTrackUsedUnitList: TTrackUnitInfoList; // Âðåìåííûé ñïèñîê èñïîëüçóåìûõ þíèòîâ ïðè îáðàáîòêå ñòåêà
CreatedProcessHandle: THandle;
CreatedThreadHandle: THandle;
AttachedProcessHandle: THandle;
AttachedThreadHandle: THandle;
AttachedFileHandle: THandle;
constructor Create;
destructor Destroy; override;
//DbgShareMem: THandle; // FileMap äëÿ âçàèìîäåéñòâèÿ ñ ïðîöåññîì
function Elapsed_MSec: Cardinal; // msec
function DbgPointsCount: Cardinal;
function DbgPointByIdx(const Idx: Cardinal): PProcessPoint;
function CurDbgPointIdx: Integer;
function DbgExceptionsCount: Cardinal;
procedure Clear;
procedure SetPEImage(APEImage: TJclPeImage);
end;
THardwareBreakpointEvent = procedure(Sender: TObject; ThreadId: TThreadId; ExceptionRecord: PExceptionRecord;
BreakPointIndex: THWBPIndex; var ReleaseBreakpoint: LongBool) of object;
TDbgState = (dsNone, dsStarted, dsWait, dsPerfomance, dsTrace, dsEvent, dsPause, dsStoping, dsStoped, dsDbgFail);
TDbgTraceState = (dtsContinue, dtsPause, dtsStepIn, dtsStepOver, dtsStepOut);
TDbgLogType = (dltUnknown = 0, dltInfo, dltWarning, dltError, dltDebugOutput, dltProcessEvent, dltThreadEvent, dltExceptionEvent,
dltBreakPointEvent, dltDLLEvent);
TDbgLogItem = Class
public
LogType: TDbgLogType;
DateTime: TDateTime;
LogMessage: String;
destructor Destroy; override;
End;
TDbgLog = class(TList)
private
FLock: TMREWSync;
function GetItem(const Index: Integer): TDbgLogItem;
public
constructor Create;
destructor Destroy; override;
procedure ClearLog;
procedure Add(const LogType: TDbgLogType; const Msg: String); overload;
procedure Add(const LogType: TDbgLogType; const FmtMsg: String; const Args: array of Const); overload;
property Items[const Index: Integer]: TDbgLogItem read GetItem; default;
property Lock: TMREWSync read FLock;
end;
procedure RaiseDebugCoreException(const Msg: String = '');
implementation
uses Debuger, DebugInfo, WinAPIUtils, Collections.Base;
procedure RaiseDebugCoreException(const Msg: String);
begin
raise EDebugCoreException.Create(Msg);
end;
{ TDebugProcessData }
procedure TProcessData.Clear;
begin
SamplingCPUTime := 0;
SamplingCount := 0;
if DbgPoints <> Nil then
FreeAndNil(DbgPoints);
FreeAndNil(DbgGetMemInfo);
DbgExceptions.Clear;
FreeAndNil(DbgTrackFuncList);
FreeAndNil(DbgTrackUnitList);
FreeAndNil(DbgTrackUsedUnitList);
end;
constructor TProcessData.Create;
begin
inherited;
State := psNone;
DbgPoints := Nil;
DbgExceptions := TThreadList.Create;
end;
function TProcessData.CurDbgPointIdx: Integer;
begin
if Assigned(DbgPoints) and (DbgPoints.Count > 0) then
Result := DbgPoints.Count - 1
else
begin
Result := 0;
RaiseDebugCoreException();
end;
end;
function TProcessData.DbgExceptionsCount: Cardinal;
var
L: TList;
begin
L := DbgExceptions.LockList;
try
Result := Cardinal(L.Count);
finally
DbgExceptions.UnlockList;
end;
end;
function TProcessData.DbgPointByIdx(const Idx: Cardinal): PProcessPoint;
begin
Result := DbgPoints[Idx];
end;
function TProcessData.DbgPointsCount: Cardinal;
begin
if Assigned(DbgPoints) then
Result := DbgPoints.Count
else
Result := 0;
end;
destructor TProcessData.Destroy;
begin
Clear;
FreeAndNil(DbgExceptions);
inherited;
end;
function TProcessData.Elapsed_MSec: Cardinal;
var
Cur: Int64;
Freq: Int64;
begin
if State = psActive then
Cur := _QueryPerformanceCounter
else
Cur := Elapsed;
Freq := _QueryPerformanceFrequency;
Result := ((Cur - Started) * 1000) div Freq;
end;
procedure TProcessData.SetPEImage(APEImage: TJclPeImage);
begin
PEImage := APEImage;
end;
{ TThreadData }
procedure TThreadData.Clear;
var
I: Integer;
begin
if Breakpoint <> Nil then
begin
FreeMemory(Breakpoint);
Breakpoint := Nil;
end;
if DbgPoints <> Nil then
begin
for I := 0 to DbgPointsCount - 1 do
DbgPointByIdx(I).Clear;
FreeAndNil(DbgPoints);
end;
FreeAndNil(DbgGetMemInfo);
FreeAndNil(DbgGetMemUnitList);
FreeAndNil(DbgSyncObjsUnitList);
//FreeAndNil(DbgGetMemFuncList);
FreeAndNil(DbgSyncObjsInfo);
FreeAndNil(DbgSyncObjsInfoByID);
FreeAndNil(DbgExceptions);
FreeAndNil(DbgTrackUnitList);
FreeAndNil(DbgTrackFuncList);
FreeAndNil(DbgTrackStack);
FreeAndNil(SamplingQueue);
FreeAndNil(DbgTrackUsedUnitList);
FreeMemory(Context);
ThreadAdvInfo := Nil;
end;
function TThreadData.DbgExceptionsByIdx(const Idx: Cardinal): TExceptInfo;
var
L: TList;
begin
Result := nil;
L := DbgExceptions.LockList;
try
if (L.Count > 0) and (Idx < Cardinal(L.Count)) then
Result := TExceptInfo(L[Idx]);
finally
DbgExceptions.UnlockList;
end;
end;
function TThreadData.DbgExceptionsCount: Cardinal;
var
L: TList;
begin
L := DbgExceptions.LockList;
try
Result := Cardinal(L.Count);
finally
DbgExceptions.UnlockList;
end;
end;
function TThreadData.DbgPointByIdx(const Idx: Integer): PThreadPoint;
begin
if Idx < DbgPoints.Count then
Result := DbgPoints[Idx]
else
Result := Nil;
end;
function TThreadData.DbgPointsCount: Cardinal;
begin
Result := DbgPoints.Count;
end;
function TThreadData.DbgSyncObjsByIdx(const Idx: Integer): PSyncObjsInfo;
begin
if Idx < DbgSyncObjsInfo.Count then
Result := DbgSyncObjsInfo[Idx]
else
Result := Nil;
end;
function TThreadData.DbgSyncObjsCount: Cardinal;
begin
Result := DbgSyncObjsInfo.Count;
end;
procedure TThreadData.Init;
begin
ThreadID := 0;
State := tsNone;
ThreadHandle := 0;
ThreadAdvInfo := Nil;
Context := GetMemory(SizeOf(TContext));
Breakpoint := GetMemory(SizeOf(THardwareBreakpoint));
Started := 0;
Elapsed := 0;
CPUElapsed := 0;
SamplingCPUTime := 0;
SamplingCount := 0;
SamplingQueue := TQueue<PDbgInfoStackRec>.Create(4096, True);
DbgTrackUsedUnitList := TTrackUnitInfoList.Create(64);
DbgTrackUsedUnitList.OwnsKeys := False;
DbgTrackUsedUnitList.OwnsValues := False;
DbgPoints := TCollectList<TThreadPoint>.Create;
DbgGetMemInfo := TGetMemInfoList.Create(1024, True);
DbgGetMemInfo.OwnsValues := True;
DbgGetMemUnitList := TMemInfoTrackUnitInfoList.Create(512, True);
DbgGetMemUnitList.OwnsValues := True;
DbgSyncObjsUnitList := TSyncObjsTrackUnitInfoList.Create(512, True);
DbgSyncObjsUnitList.OwnsValues := True;
DbgExceptions := TThreadList.Create;
DbgSyncObjsInfo := TCollectList<RSyncObjsInfo>.Create;
DbgSyncObjsInfoByID := TSyncObjsInfoListByID.Create(4096, True);
DbgTrackEventCount := 0;
DbgTrackUnitList := TCodeTrackUnitInfoList.Create(512, True);
DbgTrackUnitList.OwnsValues := True;
DbgTrackFuncList := TCodeTrackFuncInfoList.Create(4096, True);
DbgTrackFuncList.OwnsValues := True;
DbgTrackStack := TTrackStack.Create(64);