forked from cheat-engine/cheat-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFoundCodeUnit.pas
1522 lines (1246 loc) · 53 KB
/
FoundCodeUnit.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 FoundCodeUnit;
{$MODE Delphi}
interface
uses
windows, LCLIntf, LResources, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls, disassembler, ExtCtrls, Menus,
NewKernelHandler, clipbrd, ComCtrls, fgl, formChangedAddresses, LastDisassembleData,
vmxfunctions;
type
Tcoderecord = class
public
address: ptrUint;
size: integer;
opcode: string;
description: string;
// eax,ebx,ecx,edx,esi,edi,ebp,esp,eip: dword;
context: TContext;
armcontext: TARMCONTEXT;
stack: record
savedsize: ptruint;
stack: pbyte;
end;
dbvmcontextbasic: PPageEventBasic;
hitcount: integer;
diffcount: integer;
LastDisassembleData: TLastDisassembleData;
formChangedAddresses: TfrmChangedAddresses;
procedure savestack;
constructor create;
destructor destroy; override;
end;
type
{ TFoundCodeDialog }
TFoundCodeDialog=class;
TDBVMWatchPollThread=class(TThread)
private
results: PPageEventListDescriptor;
resultsize: integer;
cr3disassembler: Tcr3Disassembler;
procedure addEntriesToList;
public
id: integer;
fcd: TfoundCodeDialog;
procedure execute; override;
end;
TFoundCodeDialog = class(TForm)
FoundCodeList: TListView;
fcdImageList: TImageList;
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
miFindWhatAccesses: TMenuItem;
miSaveTofile: TMenuItem;
mInfo: TMemo;
Panel1: TPanel;
Description: TLabel;
Panel4: TPanel;
pmOptions: TPopupMenu;
ReplacewithcodethatdoesnothingNOP1: TMenuItem;
SaveDialog1: TSaveDialog;
Showthisaddressinthedisassembler1: TMenuItem;
Addtothecodelist1: TMenuItem;
MoreInfo1: TMenuItem;
Panel2: TPanel;
btnOK: TButton;
Panel3: TPanel;
btnExtraInfo: TButton;
btnAddToCodeList: TButton;
btnOpenDisassembler: TButton;
btnReplacewithnops: TButton;
N1: TMenuItem;
Copyselectiontoclipboard1: TMenuItem;
Splitter1: TSplitter;
procedure FormCreate(Sender: TObject);
procedure FormDeactivate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FoundCodeListChange(Sender: TObject; Item: TListItem;
Change: TItemChange);
procedure FoundcodeListClick(Sender: TObject);
procedure btnOKClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnReplacewithnopsClick(Sender: TObject);
procedure btnOpenDisassemblerClick(Sender: TObject);
procedure btnAddToCodeListClick(Sender: TObject);
procedure FoundcodeListDblClick(Sender: TObject);
procedure btnExtraInfoClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FoundCodeListSelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
procedure MenuItem1Click(Sender: TObject);
procedure miFindWhatAccessesClick(Sender: TObject);
procedure miSaveTofileClick(Sender: TObject);
procedure pmOptionsPopup(Sender: TObject);
procedure Copyselectiontoclipboard1Click(Sender: TObject);
private
{ Private declarations }
setcountwidth: boolean;
fdbvmwatchid: integer;
dbvmwatchpollthread: TDBVMWatchPollThread;
usedmiFindWhatAccesses: boolean; //debug
procedure stopdbvmwatch;
procedure addInfo(Coderecord: TCoderecord);
procedure moreinfo;
function getSelection: string;
procedure setdbvmwatchid(id: integer);
public
{ Public declarations }
addresswatched: ptruint;
useexceptions: boolean;
usesdebugregs: boolean;
multiplerip: boolean;
dbvmwatch_unlock: qword;
procedure AddRecord;
procedure setChangedAddressCount(address :ptruint);
property dbvmwatchid: integer read fdbvmwatchid write setdbvmwatchid;
end;
resourcestring
strClose='Close';
rsFCThesWillSetASiftwareBreakpointInt3OnEverySingleOpcodeEtc = 'This will set a Software Breakpoint (int3) on every single opcode that will be reported here. Are you sure ?';
//var
// FoundCodeDialog: TFoundCodeDialog;
implementation
uses CEFuncProc, CEDebugger,debughelper, debugeventhandler, MemoryBrowserFormUnit,
MainUnit,kerneldebugger, AdvancedOptionsUnit ,formFoundcodeListExtraUnit,
MainUnit2, ProcessHandlerUnit, Globals, Parsers, DBK32functions;
procedure TDBVMWatchPollThread.execute;
var
i: integer;
size: integer;
begin
cr3disassembler:=TCR3Disassembler.create;
getmem(results,4096);
resultsize:=4096;
zeromemory(results,resultsize);
try
while not terminated do
begin
size:=resultsize;
i:=dbvm_watch_retrievelog(id, results, size);
if i=0 then
begin
OutputDebugString('TDBVMWatchPollThread returned 0');
OutputDebugString('results^.numberOfEntries='+inttostr(results^.numberOfEntries));
OutputDebugString('results^.maxSize='+inttostr(results^.maxSize));
//process data
if results^.numberOfEntries>0 then
begin
OutputDebugString('calling addEntriesToList');
synchronize(addEntriesToList);
sleep(10);
end
else
sleep(50);
end
else
if i=2 then
begin
//not enough memory. Allocate twice the needed amount
outputdebugstring(inttostr(resultsize)+' is too small for the buffer. It needs to be at least '+inttostr(size));
freememandnil(results);
resultsize:=size*2;
getmem(results, resultsize);
zeromemory(results,resultsize);
continue; //try again, no sleep
end else
begin
outputdebugstring('dbvm_watch_retrievelog returned '+inttostr(i)+' which is not supported');
exit;
end;
end;
except
on e: exception do
outputdebugstring('TDBVMWatchPollThread crash:'+e.Message);
end;
freememandnil(results);
freeandnil(cr3disassembler);
end;
destructor TCodeRecord.Destroy;
begin
if stack.stack<>nil then
begin
freememandnil(stack.stack);
end;
inherited destroy;
end;
constructor TCodeRecord.create;
begin
formChangedAddresses:=nil;
end;
procedure TDBVMWatchPollThread.addEntriesToList;
var
coderecord: TCodeRecord;
i,j: integer;
opcode,desc: string;
li: TListItem;
ldi: TLastDisassembleData;
basicinfo: TPageEventBasic;
basic: PPageEventBasicArray;
extended: PPageEventExtendedArray absolute basic;
basics: PPageEventBasicWithStackArray absolute basic;
extendeds: PPageEventExtendedWithStackArray absolute basic;
address, address2: ptruint;
skip: boolean;
debug,debug2: pointer;
begin
outputdebugstring('addEntriesToList');
try
basic:=PPageEventBasicArray(ptruint(results)+sizeof(TPageEventListDescriptor));
for i:=0 to results^.numberOfEntries-1 do
begin
case results^.entryType of
0: basicinfo:=basic^[i];
1: basicinfo:=extended^[i].basic;
2: basicinfo:=basics^[i].basic;
3: basicinfo:=extendeds^[i].basic;
end;
address:=basicinfo.RIP;
outputdebugstring('testing entry '+inttostr(i)+'('+inttohex(address,8)+') - basicinfo.Count='+inttostr(basicinfo.Count+1));
//check if this address is inside the list
skip:=false;
for j:=0 to fcd.foundcodelist.Items.Count-1 do
begin
if TCodeRecord(fcd.foundcodelist.Items[j].data).address=address then
begin
//it's already in the list
if fcd.multiplerip=false then
begin
OutputDebugString(inttostr(j)+':Already in the list');
TCodeRecord(fcd.foundcodelist.Items[j].data).hitcount:=TCodeRecord(fcd.foundcodelist.Items[j].data).hitcount+(basicinfo.Count+1);
skip:=true;
fcd.foundcodelist.Items[j].caption:=inttostr(TCodeRecord(fcd.foundcodelist.Items[j].data).hitcount);
break;
end
else
begin
//check the other registers
if (basicinfo.RAX=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.RAX) and
(basicinfo.RBX=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.RBX) and
(basicinfo.RCX=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.RCX) and
(basicinfo.RDX=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.RDX) and
(basicinfo.RSP=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.RSP) and
(basicinfo.RBP=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.RBP) and
(basicinfo.RSI=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.RSI) and
(basicinfo.RDI=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.RDI) and
(basicinfo.R8=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.R8) and
(basicinfo.R9=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.R9) and
(basicinfo.R10=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.R10) and
(basicinfo.R11=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.R11) and
(basicinfo.R12=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.R12) and
(basicinfo.R13=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.R13) and
(basicinfo.R14=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.R14) and
(basicinfo.R15=TCodeRecord(fcd.foundcodelist.Items[j].data).dbvmcontextbasic^.R15) THEN
begin
inc(TCodeRecord(fcd.foundcodelist.Items[j].data).hitcount, basicinfo.Count);
skip:=true;
break;
end;
end;
end;
end;
if skip then
begin
OutputDebugString('Skipping entry. it''s in the list');
continue;
end
else
OutputDebugString('Not in the list yet. Adding it');
coderecord:=TCoderecord.create;
getmem(coderecord.dbvmcontextbasic, sizeof(TPageEventBasicArray));
zeromemory(@coderecord.context, sizeof(coderecord.context));
coderecord.dbvmcontextbasic^:=basicinfo;
outputdebugstring('created coderecord and dbvmcontextbasic');
case results^.entryType of
0: ; //already done
1: //extended
begin
OutputDebugString('Saving fpu data');
//outputdebugstring('FPUDATA is at offset '+inttostr(qword(@extended^[i].fpudata)-QWORD(@extended^[i])));
//outputdebugstring('sizeof(coderecord.context.FltSave)='+inttostr(sizeof(coderecord.context.FltSave)));
copymemory(@coderecord.context.{$ifdef cpu64}FltSave{$else}ext{$endif}, @extended^[i].fpudata, sizeof(coderecord.context.{$ifdef cpu64}FltSave{$else}ext{$endif}));
//getmem(debug, sizeof(coderecord.context.{$ifdef cpu64}FltSave{$else}ext{$endif}));
//copymemory(debug, @coderecord.context.{$ifdef cpu64}FltSave{$else}ext{$endif}, sizeof(coderecord.context.{$ifdef cpu64}FltSave{$else}ext{$endif}));
//getmem(debug2, 512);
//copymemory(debug2, @extended^[i].fpudata, 512);
//outputdebugstring('debug='+inttohex(ptruint(debug),8));
// outputdebugstring('debug2='+inttohex(ptruint(debug2),8));
//outputdebugstring('@coderecord.context.FltSave='+inttohex(qword(@coderecord.context.{$ifdef cpu64}FltSave{$else}ext{$endif}),8));
end;
2: //basic with stack
begin
coderecord.stack.savedsize:=4096;
getmem(coderecord.stack.stack, 4096);
copymemory(coderecord.stack.stack, @basics^[i].stack[0], 4096);
end;
3: //extended with stack
begin
copymemory(@coderecord.context.{$ifdef cpu64}FltSave{$else}ext{$endif}, @extendeds^[i].fpudata, sizeof(coderecord.context.{$ifdef cpu64}FltSave{$else}ext{$endif}));
coderecord.stack.savedsize:=4096;
getmem(coderecord.stack.stack, 4096);
copymemory(coderecord.stack.stack, @basics^[i].stack[0], 4096);
end;
else
begin
//error
outputdebugstring('unexpected type');
freememandnil(coderecord.dbvmcontextbasic);
freeandnil(coderecord);
exit;
end;
end;
address:=coderecord.dbvmcontextbasic^.RIP;
address2:=address;
cr3disassembler.cr3:=coderecord.dbvmcontextbasic^.CR3;
outputdebugstring(format('disassembling %.8x with CR3 %.8x: ',[address, cr3disassembler.cr3]));
opcode:=cr3disassembler.disassemble(address2,desc);
ldi:=cr3disassembler.LastDisassembleData;
outputdebugstring('opcode='+opcode);
coderecord.address:=address;
coderecord.size:=address2-address;
coderecord.opcode:=opcode;
coderecord.description:=desc;
coderecord.LastDisassembleData:=ldi;
coderecord.hitcount:=coderecord.dbvmcontextbasic^.Count;
//make compatible with older code:
OutputDebugString('setting up compatibility context');
coderecord.context.{$ifdef cpu64}Rax{$else}Eax{$endif}:=coderecord.dbvmcontextbasic^.RAX;
coderecord.context.{$ifdef cpu64}Rbx{$else}Ebx{$endif}:=coderecord.dbvmcontextbasic^.RBX;
coderecord.context.{$ifdef cpu64}Rcx{$else}Ecx{$endif}:=coderecord.dbvmcontextbasic^.RCX;
coderecord.context.{$ifdef cpu64}Rdx{$else}Edx{$endif}:=coderecord.dbvmcontextbasic^.RDX;
coderecord.context.{$ifdef cpu64}Rsi{$else}Esi{$endif}:=coderecord.dbvmcontextbasic^.RSI;
coderecord.context.{$ifdef cpu64}Rdi{$else}Edi{$endif}:=coderecord.dbvmcontextbasic^.RDI;
coderecord.context.{$ifdef cpu64}Rbp{$else}Ebp{$endif}:=coderecord.dbvmcontextbasic^.RBP;
coderecord.context.{$ifdef cpu64}Rsp{$else}Esp{$endif}:=coderecord.dbvmcontextbasic^.Rsp;
coderecord.context.{$ifdef cpu64}Rip{$else}Eip{$endif}:=coderecord.dbvmcontextbasic^.Rip;
{$ifdef cpu64}
coderecord.context.R8:=coderecord.dbvmcontextbasic^.R8;
coderecord.context.R9:=coderecord.dbvmcontextbasic^.R9;
coderecord.context.R10:=coderecord.dbvmcontextbasic^.R10;
coderecord.context.R11:=coderecord.dbvmcontextbasic^.R11;
coderecord.context.R12:=coderecord.dbvmcontextbasic^.R12;
coderecord.context.R13:=coderecord.dbvmcontextbasic^.R13;
coderecord.context.R14:=coderecord.dbvmcontextbasic^.R14;
coderecord.context.R15:=coderecord.dbvmcontextbasic^.R15;
{$endif}
coderecord.context.EFlags:=coderecord.dbvmcontextbasic^.FLAGS;
coderecord.context.SegCs:=coderecord.dbvmcontextbasic^.CS;
coderecord.context.SegSs:=coderecord.dbvmcontextbasic^.SS;
coderecord.context.SegDs:=coderecord.dbvmcontextbasic^.DS;
coderecord.context.SegEs:=coderecord.dbvmcontextbasic^.ES;
coderecord.context.SegFs:=coderecord.dbvmcontextbasic^.FS;
coderecord.context.SegGs:=coderecord.dbvmcontextbasic^.GS;
coderecord.hitcount:=coderecord.dbvmcontextbasic^.Count+1;
OutputDebugString('adding to the foundlist');
li:=fcd.FoundCodeList.Items.Add;
li.caption:='1';
li.SubItems.add(opcode);
li.data:=coderecord;
OutputDebugString('done with no errors');
end;
except
on e: exception do
outputdebugstring('TDBVMWatchPollThread.addEntriesToList error: '+e.message);
end;
end;
procedure TCodeRecord.savestack;
var base: qword;
begin
getmem(stack.stack, savedStackSize);
if processhandler.SystemArchitecture=archarm then
base:=armcontext.SP
else
base:=qword(context.{$ifdef cpu64}Rsp{$else}esp{$endif});
if ReadProcessMemory(processhandle, pointer(base), stack.stack, savedStackSize, stack.savedsize)=false then
begin
//for some reason this sometimes returns 0 bytes read even if some of the bytes are readable.
stack.savedsize:=4096-(base mod 4096);
ReadProcessMemory(processhandle, pointer(base), stack.stack, stack.savedsize, stack.savedsize);
end;
end;
procedure TFoundCodedialog.AddRecord;
{
Invoked by the debugger thread
It takes the data from the current thread and stores it in the processlist
}
var currentthread: TDebugThreadHandler;
opcode: string;
ldi: TLastDisassembleData;
address,address2: ptrUint;
desc: string;
coderecord: TCodeRecord;
i: integer;
li: tlistitem;
f: TfrmChangedAddresses;
d: TDisassembler;
begin
//the debuggerthread is idle at this point
currentThread:=debuggerthread.CurrentThread;
if currentthread<>nil then
begin
if processhandler.SystemArchitecture=archARM then
begin
address:=currentthread.armcontext.PC;
end
else
begin
address:=currentThread.context.{$ifdef cpu64}Rip{$else}eip{$endif};
if usesdebugregs or useexceptions then //find out the previous opcode
begin
address2:=address;
d:=TDisassembler.Create;
d.disassemble(address2,desc);
if copy(d.LastDisassembleData.opcode,1,3)<>'REP' then
address:=previousopcode(address);
freeandnil(d);
end;
end;
//disassemble to get the opcode and size
address2:=address;
d:=TDisassembler.Create;
opcode:=d.disassemble(address2,desc);
ldi:=d.LastDisassembleData;
freeandnil(d);
//check if address is inside the list
for i:=0 to foundcodelist.Items.Count-1 do
if TCodeRecord(foundcodelist.Items[i].data).address=address then
begin
//it's already in the list
inc(TCodeRecord(foundcodelist.Items[i].data).hitcount);
if miFindWhatAccesses.checked then
FoundcodeList.items[i].caption:=inttostr(TCodeRecord(foundcodelist.Items[i].data).hitcount)+' ('+inttostr(TCodeRecord(foundcodelist.Items[i].data).diffcount)+')'
else
FoundcodeList.items[i].caption:=inttostr(TCodeRecord(foundcodelist.Items[i].data).hitcount);
exit;
end;
coderecord:=TCoderecord.create;
coderecord.address:=address;
coderecord.size:=address2-address;
coderecord.opcode:=opcode;
coderecord.description:=desc;
coderecord.context:=currentthread.context^;
coderecord.armcontext:=currentthread.armcontext;
coderecord.LastDisassembleData:=ldi;
coderecord.savestack;
coderecord.hitcount:=1;
li:=FoundCodeList.Items.Add;
li.caption:='1';
li.SubItems.add(opcode);
li.data:=coderecord;
if miFindWhatAccesses.Checked then //add it
coderecord.formChangedAddresses:=debuggerthread.FindWhatCodeAccesses(address, self); //ffffffuuuuuuuuuu. Rebuild again
end;
end;
procedure TFoundCodeDialog.stopdbvmwatch;
begin
if dbvmwatchpollthread<>nil then
begin
dbvmwatchpollthread.Terminate;
dbvmwatchpollthread.WaitFor;
freeandnil(dbvmwatchpollthread);
end;
if dbvmwatchid<>-1 then
begin
dbvm_watch_delete(dbvmwatchid);
dbvmwatchid:=-1;
end;
if dbvmwatch_unlock<>0 then
begin
UnlockMemory(dbvmwatch_unlock);
dbvmwatch_unlock:=0;
end;
end;
procedure TFoundCodedialog.setdbvmwatchid(id: integer);
begin
fdbvmwatchid:=id;
if id<>-1 then
begin
if dbvmwatchpollthread<>nil then
freeandnil(dbvmwatchpollthread);
dbvmwatchpollthread:=TDBVMWatchPollThread.Create(true);
dbvmwatchpollthread.id:=id;
dbvmwatchpollthread.fcd:=self;
dbvmwatchpollthread.Start;
useexceptions:=true;
end;
end;
procedure TFoundCodedialog.setChangedAddressCount(address :ptruint);
var i: integer;
begin
for i:=0 to foundcodelist.Items.Count-1 do
if TCodeRecord(foundcodelist.Items[i].data).address=address then
begin
//increase the counter
inc(TCodeRecord(foundcodelist.Items[i].data).diffcount);
FoundcodeList.items[i].caption:=inttostr(TCodeRecord(foundcodelist.Items[i].data).hitcount)+' ('+inttostr(TCodeRecord(foundcodelist.Items[i].data).diffcount)+')';
exit;
end;
end;
procedure TFoundCodedialog.addInfo(Coderecord: TCoderecord);
var
address: ptruint;
disassembled: array[1..5] of string;
firstchar: char;
hexcount: integer;
temp: string;
begin
if processhandler.is64Bit then
hexcount:=16
else
hexcount:=8;
address:=Coderecord.address;
address:=previousopcode(address);
address:=previousopcode(address);
disassembled[1]:=disassemble(address,temp);
disassembled[2]:=disassemble(address,temp);
if address<>coderecord.address then
begin
disassembled[1]:='';
disassembled[2]:='';
disassembled[3]:=coderecord.opcode;
disassembled[4]:='';
disassembled[5]:='';
end
else
begin
disassembled[3]:=disassemble(address,temp);
disassembled[4]:=disassemble(address,temp);
disassembled[5]:=disassemble(address,temp);
end;
minfo.Lines.BeginUpdate;
try
minfo.Lines.Add(disassembled[1]);
minfo.Lines.Add(disassembled[2]);
minfo.Lines.Add(disassembled[3]+' <<');
minfo.Lines.Add(disassembled[4]);
minfo.Lines.Add(disassembled[5]);
minfo.Lines.Add('');
if processhandler.SystemArchitecture=archarm then
begin
minfo.Lines.Add('R10='+inttohex(coderecord.armcontext.R0,8));
minfo.Lines.Add('R11='+inttohex(coderecord.armcontext.R1,8));
minfo.Lines.Add('R12='+inttohex(coderecord.armcontext.R2,8));
minfo.Lines.Add('R13='+inttohex(coderecord.armcontext.R3,8));
minfo.Lines.Add('R14='+inttohex(coderecord.armcontext.R4,8));
minfo.Lines.Add('R15='+inttohex(coderecord.armcontext.R5,8));
minfo.Lines.Add('R16='+inttohex(coderecord.armcontext.R6,8));
minfo.Lines.Add('R17='+inttohex(coderecord.armcontext.R7,8));
minfo.Lines.Add('R18='+inttohex(coderecord.armcontext.R8,8));
minfo.Lines.Add('R19='+inttohex(coderecord.armcontext.R9,8));
minfo.Lines.Add('R10='+inttohex(coderecord.armcontext.R10,8));
minfo.Lines.Add('FP='+inttohex(coderecord.armcontext.FP,8));
minfo.Lines.Add('IP='+inttohex(coderecord.armcontext.IP,8));
minfo.Lines.Add('SP='+inttohex(coderecord.armcontext.SP,8));
minfo.Lines.Add('LR='+inttohex(coderecord.armcontext.LR,8));
minfo.Lines.Add('PC='+inttohex(coderecord.armcontext.PC,8));
minfo.Lines.Add('CPSR='+inttohex(coderecord.armcontext.CPSR,8));
minfo.Lines.Add('ORIG_R0='+inttohex(coderecord.armcontext.ORIG_R0,8));
end
else
begin
if processhandler.is64bit then
firstchar:='R' else firstchar:='E';
minfo.Lines.Add(firstchar+'AX='+IntToHex(coderecord.context.{$ifdef cpu64}Rax{$else}Eax{$endif},hexcount));
minfo.Lines.Add(firstchar+'BX='+IntToHex(coderecord.context.{$ifdef cpu64}Rbx{$else}Ebx{$endif},hexcount));
minfo.Lines.Add(firstchar+'CX='+IntToHex(coderecord.context.{$ifdef cpu64}Rcx{$else}Ecx{$endif},hexcount));
minfo.Lines.Add(firstchar+'DX='+IntToHex(coderecord.context.{$ifdef cpu64}Rdx{$else}Edx{$endif},hexcount));
minfo.Lines.Add(firstchar+'SI='+IntToHex(coderecord.context.{$ifdef cpu64}Rsi{$else}Esi{$endif},hexcount));
minfo.Lines.Add(firstchar+'DI='+IntToHex(coderecord.context.{$ifdef cpu64}Rdi{$else}Edi{$endif},hexcount));
minfo.Lines.Add(firstchar+'SP='+IntToHex(coderecord.context.{$ifdef cpu64}Rsp{$else}Esp{$endif},hexcount));
minfo.Lines.Add(firstchar+'BP='+IntToHex(coderecord.context.{$ifdef cpu64}Rbp{$else}Ebp{$endif},hexcount));
minfo.Lines.Add(firstchar+'IP='+IntToHex(coderecord.context.{$ifdef cpu64}Rip{$else}Eip{$endif},hexcount));
{$ifdef cpu64}
if processhandler.is64bit then
begin
minfo.Lines.Add('R8='+IntToHex(coderecord.context.r8,16));
minfo.Lines.Add('R9='+IntToHex(coderecord.context.r9,16));
minfo.Lines.Add('R10='+IntToHex(coderecord.context.r10,16));
minfo.Lines.Add('R11='+IntToHex(coderecord.context.r11,16));
minfo.Lines.Add('R12='+IntToHex(coderecord.context.r12,16));
minfo.Lines.Add('R13='+IntToHex(coderecord.context.r13,16));
minfo.Lines.Add('R14='+IntToHex(coderecord.context.r14,16));
minfo.Lines.Add('R15='+IntToHex(coderecord.context.r15,16));
end;
{$endif}
end;
minfo.lines.add('');
minfo.lines.add('');
finally
minfo.lines.EndUpdate;
end;
end;
procedure TFoundCodedialog.moreinfo;
var
disassembled: array[1..5] of record
s: string;
a: ptruint;
end;
address: ptrUint;
itemindex: integer;
temp,temp2: string;
maxregistervalue: ptrUint;
p: ptrUint;
i: integer;
coderecord: TCodeRecord;
firstchar: string;
w: integer;
FormFoundCodeListExtra: TFormFoundCodeListExtra;
ew: trect; //extra window rect
cw: trect; //changed address window rect
offset: integer;
d: TDisassembler;
begin
if processhandler.is64bit then
firstchar:='R' else firstchar:='E';
itemindex:=foundcodelist.ItemIndex;
if itemindex<>-1 then
begin
FormFoundCodeListExtra:=TFormFoundCodeListExtra.Create(application);
if useexceptions then
FormFoundCodeListExtra.Label18.Visible:=false
else
FormFoundCodeListExtra.Label18.Visible:=true;
coderecord:=TCodeRecord(foundcodelist.items[itemindex].data);
if coderecord.formChangedAddresses<>nil then
begin
if not coderecord.formChangedAddresses.visible then //override userdefined positioning
begin
LCLIntf.GetWindowRect(coderecord.formChangedAddresses.handle, cw);
LCLIntf.GetWindowRect(FormFoundCodeListExtra.handle, ew);
coderecord.formChangedAddresses.left:=(ew.Left-(cw.Right-cw.left));
coderecord.formChangedAddresses.top:=FormFoundCodeListExtra.top;
end;
coderecord.formChangedAddresses.show;
end;
address:=coderecord.address;
if coderecord.dbvmcontextbasic<>nil then
begin
d:=TCR3Disassembler.Create;
TCR3Disassembler(d).CR3:=coderecord.dbvmcontextbasic^.CR3;
end
else
d:=TDisassembler.Create;
address:=previousopcode(address, d);
address:=previousopcode(address, d);
disassembled[1].a:=address;
disassembled[1].s:=d.disassemble(address,temp);
disassembled[2].a:=address;
disassembled[2].s:=d.disassemble(address,temp);
if address<>coderecord.address then
begin
disassembled[1].s:='';
disassembled[2].s:='';
disassembled[3].s:=coderecord.opcode;
disassembled[4].s:='';
disassembled[5].s:='';
end
else
begin
disassembled[3].a:=address;
disassembled[3].s:=d.disassemble(address,temp);
disassembled[4].a:=address;
disassembled[4].s:=d.disassemble(address,temp);
disassembled[5].a:=address;
disassembled[5].s:=d.disassemble(address,temp);
end;
freeandnil(d);
//convert disassembled strings to address+opcode only (no bytes)
//xxxxxxxx - xx xx xx - opcode
temp:=copy(disassembled[1].s,pos('-',disassembled[1].s)+2,length(disassembled[1].s));
temp:=copy(temp,pos('-',temp)+2,length(temp));
disassembled[1].s:=copy(disassembled[1].s,1,pos('-',disassembled[1].s))+' '+temp;
temp:=copy(disassembled[2].s,pos('-',disassembled[2].s)+2,length(disassembled[2].s));
temp:=copy(temp,pos('-',temp)+2,length(temp));
disassembled[2].s:=copy(disassembled[2].s,1,pos('-',disassembled[2].s))+' '+temp;
temp:=copy(disassembled[3].s,pos('-',disassembled[3].s)+2,length(disassembled[3].s));
temp:=copy(temp,pos('-',temp)+2,length(temp));
disassembled[3].s:=copy(disassembled[3].s,1,pos('-',disassembled[3].s))+' '+temp;
temp:=copy(disassembled[4].s,pos('-',disassembled[4].s)+2,length(disassembled[4].s));
temp:=copy(temp,pos('-',temp)+2,length(temp));
disassembled[4].s:=copy(disassembled[4].s,1,pos('-',disassembled[4].s))+' '+temp;
temp:=copy(disassembled[5].s,pos('-',disassembled[5].s)+2,length(disassembled[5].s));
temp:=copy(temp,pos('-',temp)+2,length(temp));
disassembled[5].s:=copy(disassembled[5].s,1,pos('-',disassembled[5].s))+' '+temp;
with FormFoundCodeListExtra do
begin
Label1.Caption:=disassembled[1].s;
Label1.tag:=disassembled[1].a;
Label2.Caption:=disassembled[2].s;
Label2.tag:=disassembled[2].a;
Label3.Caption:=disassembled[3].s;
Label3.tag:=disassembled[3].a;
Label4.Caption:=disassembled[4].s;
Label4.tag:=disassembled[4].a;
Label5.Caption:=disassembled[5].s;
Label5.tag:=disassembled[5].a;
if processhandler.SystemArchitecture=archarm then
begin
//repurpose the x86 32-bit labels
lblRAX.caption:=' R0='+IntToHex(coderecord.armcontext.R0,8);
lblRDX.caption:=' R1='+IntToHex(coderecord.armcontext.R1,8);
lblRBP.caption:=' R2='+IntToHex(coderecord.armcontext.R2,8);
lblRBX.caption:=' R3='+IntToHex(coderecord.armcontext.R3,8);
lblRSI.caption:=' R4='+IntToHex(coderecord.armcontext.R4,8);
lblRSP.caption:=' R5='+IntToHex(coderecord.armcontext.R5,8);
lblRCX.caption:=' R6='+IntToHex(coderecord.armcontext.R6,8);
lblRDI.caption:=' R7='+IntToHex(coderecord.armcontext.R7,8);
lblRIP.caption:=' R8='+IntToHex(coderecord.armcontext.R8,8);
lblR9:=tlabel.Create(FormFoundCodeListExtra);
lblR9.Top:=lblRCX.top+(lblRCX.top-lblRBX.top);
lblR9.left:=lblRCX.left;
lblR9.caption:=' R9='+IntToHex(coderecord.armcontext.R9,8);
lblR9.parent:=FormFoundCodeListExtra.panel6;
lblR9.OnMouseDown:=registerMouseDown;
lblR9.OnDblClick:=RegisterDblClick;
lblR9.Align:=lblrcx.Align;
lblR10:=tlabel.Create(FormFoundCodeListExtra);
lblR10.Top:=lblRCX.top+(lblRCX.top-lblRBX.top);
lblR10.left:=lblRDI.left;
lblR10.caption:='R10='+IntToHex(coderecord.armcontext.R10,8);
lblR10.parent:=FormFoundCodeListExtra.panel6;
lblR10.OnMouseDown:=registerMouseDown;
lblR10.OnDblClick:=RegisterDblClick;
lblR10.Align:=lblrcx.Align;
lblR11:=tlabel.Create(FormFoundCodeListExtra);
lblR11.Top:=lblRCX.top+(lblRCX.top-lblRBX.top);
lblR11.left:=lblRIP.left;
lblR11.caption:=' FP='+IntToHex(coderecord.armcontext.FP,8);
lblR11.parent:=FormFoundCodeListExtra.panel6;
lblR11.OnMouseDown:=registerMouseDown;
lblR11.OnDblClick:=RegisterDblClick;
lblR11.Align:=lblrcx.Align;
//new row
lblR12:=tlabel.Create(FormFoundCodeListExtra);
lblR12.Top:=lblR9.top+(lblRCX.top-lblRBX.top);
lblR12.left:=lblRCX.left;
lblR12.caption:=' IP='+IntToHex(coderecord.armcontext.IP,8);
lblR12.parent:=FormFoundCodeListExtra.panel6;
lblR12.OnMouseDown:=registerMouseDown;
lblR12.OnDblClick:=RegisterDblClick;
lblR12.Align:=lblrcx.Align;
lblR13:=tlabel.Create(FormFoundCodeListExtra);
lblR13.Top:=lblR9.top+(lblRCX.top-lblRBX.top);
lblR13.left:=lblRDI.left;
lblR13.caption:=' SP='+IntToHex(coderecord.armcontext.SP,8);
lblR13.parent:=FormFoundCodeListExtra.panel6;
lblR13.OnMouseDown:=registerMouseDown;
lblR13.OnDblClick:=RegisterDblClick;
lblR13.Align:=lblrcx.Align;
lblR14:=tlabel.Create(FormFoundCodeListExtra);
lblR14.Top:=lblR9.top+(lblRCX.top-lblRBX.top);
lblR14.left:=lblRIP.left;
lblR14.caption:=' LR='+IntToHex(coderecord.armcontext.LR,8);
lblR14.parent:=FormFoundCodeListExtra.panel6;
lblR14.OnMouseDown:=registerMouseDown;
lblR14.OnDblClick:=RegisterDblClick;
lblR14.Align:=lblrcx.Align;
//new line
lblR15:=tlabel.Create(FormFoundCodeListExtra);
lblR15.Top:=lblR12.top+(lblRCX.top-lblRBX.top);
lblR15.left:=lblRCX.left;
lblR15.caption:=' PC='+IntToHex(coderecord.armcontext.PC,8);
lblR15.parent:=FormFoundCodeListExtra.panel6;
lblR15.OnMouseDown:=registerMouseDown;
lblR15.OnDblClick:=RegisterDblClick;
lblR15.Align:=lblrcx.Align;
lblR16:=tlabel.Create(FormFoundCodeListExtra);
lblR16.Top:=lblR12.top+(lblRCX.top-lblRBX.top);
lblR16.left:=lblRDI.left;
lblR16.caption:=' CPSR='+IntToHex(coderecord.armcontext.CPSR,8);
lblR16.parent:=FormFoundCodeListExtra.panel6;
lblR16.OnMouseDown:=registerMouseDown;
lblR16.OnDblClick:=RegisterDblClick;
lblR16.Align:=lblrcx.Align;
lblR17:=tlabel.Create(FormFoundCodeListExtra);
lblR17.Top:=lblR12.top+(lblRCX.top-lblRBX.top);
lblR17.left:=lblRIP.left;
lblR17.caption:=' ORIG_R0='+IntToHex(coderecord.armcontext.ORIG_R0,8);
lblR17.parent:=FormFoundCodeListExtra.panel6;
lblR17.OnMouseDown:=registerMouseDown;
lblR17.OnDblClick:=RegisterDblClick;
lblR17.Align:=lblrcx.Align;
{Constraints.MinHeight:=panel6.top+(lblR17.top+lblR17.height)+16+panel5.height;
if height<Constraints.MinHeight then
height:=Constraints.MinHeight; }
end
else
begin
lblRAX.caption:=firstchar+'AX='+IntToHex(coderecord.context.{$ifdef cpu64}Rax{$else}Eax{$endif},8);
lblRBX.caption:=firstchar+'BX='+IntToHex(coderecord.context.{$ifdef cpu64}Rbx{$else}Ebx{$endif},8);
lblRCX.caption:=firstchar+'CX='+IntToHex(coderecord.context.{$ifdef cpu64}Rcx{$else}Ecx{$endif},8);
lblRDX.caption:=firstchar+'DX='+IntToHex(coderecord.context.{$ifdef cpu64}Rdx{$else}Edx{$endif},8);
lblRSI.caption:=firstchar+'SI='+IntToHex(coderecord.context.{$ifdef cpu64}Rsi{$else}Esi{$endif},8);
lblRDI.caption:=firstchar+'DI='+IntToHex(coderecord.context.{$ifdef cpu64}Rdi{$else}Edi{$endif},8);
lblRSP.caption:=firstchar+'SP='+IntToHex(coderecord.context.{$ifdef cpu64}Rsp{$else}Esp{$endif},8);
lblRBP.caption:=firstchar+'BP='+IntToHex(coderecord.context.{$ifdef cpu64}Rbp{$else}Ebp{$endif},8);
lblRIP.caption:=firstchar+'IP='+IntToHex(coderecord.context.{$ifdef cpu64}Rip{$else}Eip{$endif},8);
{$ifdef cpu64}
if processhandler.is64bit then
begin
pnlRegisters.ChildSizing.ControlsPerLine:=5;
lblR8:=tlabel.Create(FormFoundCodeListExtra);
lblR8.caption:=' R8='+IntToHex(coderecord.context.r8,8);
lblR8.parent:=FormFoundCodeListExtra.pnlRegisters;
lblR8.OnMouseDown:=registerMouseDown;
lblR8.OnDblClick:=RegisterDblClick;
lblR8.Align:=lblrcx.Align;
lblR9:=tlabel.Create(FormFoundCodeListExtra);
lblR9.caption:=' R9='+IntToHex(coderecord.context.r9,8);
lblR9.parent:=FormFoundCodeListExtra.pnlRegisters;
lblR9.OnMouseDown:=registerMouseDown;
lblR9.OnDblClick:=RegisterDblClick;
lblR9.Align:=lblrcx.Align;
lblR10:=tlabel.Create(FormFoundCodeListExtra);
lblR10.caption:='R10='+IntToHex(coderecord.context.r10,8);
lblR10.parent:=FormFoundCodeListExtra.pnlRegisters;
lblR10.OnMouseDown:=registerMouseDown;
lblR10.OnDblClick:=RegisterDblClick;
lblR10.Align:=lblrcx.Align;
lblR11:=tlabel.Create(FormFoundCodeListExtra);
lblR11.caption:='R11='+IntToHex(coderecord.context.r11,8);
lblR11.parent:=FormFoundCodeListExtra.pnlRegisters;
lblR11.OnMouseDown:=registerMouseDown;
lblR11.OnDblClick:=RegisterDblClick;
lblR11.Align:=lblrcx.Align;