forked from cheat-engine/cheat-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugger.c
1603 lines (1187 loc) · 43.8 KB
/
debugger.c
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
/*
debugger.c:
This unit will handle all debugging related code, from hooking, to handling interrupts
todo: this whole thing can be moved to a few simple lines in dbvm...
*/
#pragma warning( disable: 4100 4103 4189)
#include <ntifs.h>
#include <windef.h>
#include "DBKFunc.h"
#include "interruptHook.h"
#include "debugger.h"
#ifdef AMD64
extern void interrupt1_asmentry( void ); //declared in debuggera.asm
#else
void interrupt1_asmentry( void );
#endif
volatile struct
{
BOOL isDebugging; //TRUE if a process is currently being debugged
BOOL stoppingTheDebugger;
DWORD debuggedProcessID; //The processID that is currently debugger
struct {
BOOL active;
UINT_PTR address; //Up to 4 addresses to break on
BreakType breakType; //What type of breakpoint for each seperate address
BreakLength breakLength; //How many bytes does this breakpoint look at
} breakpoint[4];
//...
BOOL globalDebug; //If set all threads of every process will raise an interrupt on taskswitch
//while debugging:
UINT_PTR *LastStackPointer;
UINT_PTR *LastRealDebugRegisters;
HANDLE LastThreadID;
BOOL handledlastevent;
BOOL storeLBR;
int storeLBR_max;
UINT_PTR *LastLBRStack;
volatile struct {
UINT_PTR DR0;
UINT_PTR DR1;
UINT_PTR DR2;
UINT_PTR DR3;
UINT_PTR DR6;
UINT_PTR DR7;
UINT_PTR reserved;
volatile int inEpilogue; //if set the global debug bit does no faking
} FakedDebugRegisterState[256];
char b[1];
volatile BYTE DECLSPEC_ALIGN(16) fxstate[512];
} DebuggerState;
KEVENT debugger_event_WaitForContinue; //event for kernelmode. Waits till it's set by usermode (usermode function: DBK_Continue_Debug_Event sets it)
KEVENT debugger_event_CanBreak; //event for kernelmode. Waits till a break has been handled so a new one can enter
KEVENT debugger_event_WaitForDebugEvent; //event for usermode. Waits till it's set by a debugged event
DebugReg7 debugger_dr7_getValue(void);
void debugger_dr7_setValue(DebugReg7 value);
DebugReg6 debugger_dr6_getValue(void);
JUMPBACK Int1JumpBackLocation;
void debugger_dr7_setGD(int state)
{
DebugReg7 _dr7=debugger_dr7_getValue();
_dr7.GD=state; //usually 1
debugger_dr7_setValue(_dr7);
}
void debugger_dr0_setValue(UINT_PTR value)
{
__writedr(0,value);
}
UINT_PTR debugger_dr0_getValue(void)
{
return __readdr(0);
}
void debugger_dr1_setValue(UINT_PTR value)
{
__writedr(1,value);
}
UINT_PTR debugger_dr1_getValue(void)
{
return __readdr(1);
}
void debugger_dr2_setValue(UINT_PTR value)
{
__writedr(2,value);
}
UINT_PTR debugger_dr2_getValue(void)
{
return __readdr(2);
}
void debugger_dr3_setValue(UINT_PTR value)
{
__writedr(3,value);
}
UINT_PTR debugger_dr3_getValue(void)
{
return __readdr(3);
}
void debugger_dr6_setValue(UINT_PTR value)
{
__writedr(6,value);
}
void debugger_dr7_setValue(DebugReg7 value)
{
UINT_PTR temp=*(UINT_PTR *)&value;
__writedr(7,temp);
}
void debugger_dr7_setValueDword(UINT_PTR value)
{
__writedr(7,value);
}
UINT_PTR debugger_dr7_getValueDword(void) //I wonder why I couldn't just typecast the DebugReg7 to a dword...
{
return __readdr(7);
}
DebugReg7 debugger_dr7_getValue(void)
{
UINT_PTR temp=debugger_dr7_getValueDword();
return *(DebugReg7 *)&temp;
}
UINT_PTR debugger_dr6_getValueDword(void)
{
return __readdr(6);
}
DebugReg6 debugger_dr6_getValue(void)
{
UINT_PTR temp=debugger_dr6_getValueDword();
return *(DebugReg6 *)&temp;
}
void debugger_touchDebugRegister(UINT_PTR param)
{
DbgPrint("Touching debug register. inepilogue=\n", DebuggerState.FakedDebugRegisterState[cpunr()].inEpilogue);
debugger_dr0_setValue(debugger_dr0_getValue());
}
void debugger_initialize(void)
{
DbgPrint("Initializing debugger events\n");
KeInitializeEvent(&debugger_event_WaitForContinue, SynchronizationEvent, FALSE);
KeInitializeEvent(&debugger_event_CanBreak, SynchronizationEvent, TRUE); //true so the first can enter
KeInitializeEvent(&debugger_event_WaitForDebugEvent, SynchronizationEvent, FALSE);
DbgPrint("DebuggerState.fxstate=%p\n",DebuggerState.fxstate);
}
void debugger_setInitialFakeState(void)
{
DbgPrint("setInitialFakeState for cpu %d\n",cpunr());
DebuggerState.FakedDebugRegisterState[cpunr()].DR0=debugger_dr0_getValue();
DebuggerState.FakedDebugRegisterState[cpunr()].DR1=debugger_dr1_getValue();
DebuggerState.FakedDebugRegisterState[cpunr()].DR2=debugger_dr2_getValue();
DebuggerState.FakedDebugRegisterState[cpunr()].DR3=debugger_dr3_getValue();
DebuggerState.FakedDebugRegisterState[cpunr()].DR6=debugger_dr6_getValueDword();
DebuggerState.FakedDebugRegisterState[cpunr()].DR7=debugger_dr7_getValueDword();
}
VOID debugger_initHookForCurrentCPU_DPC(IN struct _KDPC *Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
{
debugger_initHookForCurrentCPU();
}
int debugger_removeHookForCurrentCPU(UINT_PTR params)
{
//DbgPrint("Unhooking int1 for this cpu\n");
return inthook_UnhookInterrupt(1);
}
int debugger_initHookForCurrentCPU(void)
/*
Must be called for each cpu
*/
{
int result=TRUE;
DbgPrint("Hooking int1 for cpu %d\n", cpunr());
result=inthook_HookInterrupt(1,getCS() & 0xfff8, (ULONG_PTR)interrupt1_asmentry, &Int1JumpBackLocation);
#ifdef AMD64
if (result)
{
DbgPrint("hooked int1. Int1JumpBackLocation=%x:%llx\n", Int1JumpBackLocation.cs, Int1JumpBackLocation.eip);
}
#endif
if (DebuggerState.globalDebug)
{
//set the fake state
//debugger_setInitialFakeState();
DbgPrint("Setting GD bit for cpu %d\n",cpunr());
debugger_dr7_setGD(1); //enable the GD flag
}
if (DebuggerState.storeLBR)
{
DbgPrint("Enabling LBR logging. IA32_DEBUGCTL was %x\n", __readmsr(0x1d9));
__writemsr(0x1d9, __readmsr(0x1d9) | 1);
DbgPrint("Enabling LBR logging. IA32_DEBUGCTL is %x\n", __readmsr(0x1d9));
}
return result;
}
void debugger_setStoreLBR(BOOL state)
{
if (state)
DbgPrint("Setting storeLBR to true\n");
else
DbgPrint("Setting storeLBR to false\n");
DebuggerState.storeLBR=state; //it's not THAT crucial to disable/enable it
DebuggerState.storeLBR_max=0;
switch (cpu_model)
{
case 0x2a:
case 0x1a:
case 0x1e:
case 0x1f:
case 0x2e:
case 0x25:
case 0x2c:
DebuggerState.storeLBR_max=16;
break;
case 0x17:
case 0x1d:
case 0x0f:
DebuggerState.storeLBR_max=4;
break;
case 0x1c:
DebuggerState.storeLBR_max=8;
break;
}
DbgPrint("Because your cpu_model=%d I think that your storeLBR_max=%d\n", cpu_model, DebuggerState.storeLBR_max);
}
int debugger_setGlobalDebugState(BOOL state)
//call this BEFORE debugging, if already debugging, the user must call this for each cpu
{
DbgPrint("debugger_setGlobalDebugState(%d)\n",state);
if (state)
DebuggerState.globalDebug=state; //on enable set this first
if (inthook_isHooked(1))
{
int oldEpilogueState=DebuggerState.FakedDebugRegisterState[cpunr()].inEpilogue;
DbgPrint("Int 1 is hooked,%ssetting GD\n",(state ? "":"un"));
DbgPrint("oldEpilogueState=%d\n",oldEpilogueState);
//debugger_setInitialFakeState();
DebuggerState.FakedDebugRegisterState[cpunr()].inEpilogue=TRUE;
DebuggerState.globalDebug=state;
debugger_dr7_setGD(state);
DebuggerState.FakedDebugRegisterState[cpunr()].inEpilogue=oldEpilogueState;
DebuggerState.FakedDebugRegisterState[cpunr()].DR7=0x400;
debugger_dr7_setValueDword(0x400);
}
return TRUE;
}
int debugger_startDebugging(DWORD debuggedProcessID)
/*
Call this AFTER the interrupts are hooked
*/
{
DbgPrint("debugger_startDebugging. Processid=%x\n",debuggedProcessID);
Int1JumpBackLocation.eip=inthook_getOriginalEIP(1);
Int1JumpBackLocation.cs=inthook_getOriginalCS(1);
#ifdef AMD64
DbgPrint("Int1 jump back = %x:%llx\n", Int1JumpBackLocation.cs, Int1JumpBackLocation.eip);
#endif
DebuggerState.isDebugging=TRUE;
DebuggerState.debuggedProcessID=debuggedProcessID;
return TRUE;
}
int debugger_stopDebugging(void)
{
int i;
DbgPrint("Stopping the debugger if it is running\n");
DebuggerState.stoppingTheDebugger=TRUE;
if (DebuggerState.globalDebug)
{
//touch the global debug for each debug processor
DbgPrint("Touching the debug registers\n");
forEachCpuPassive(debugger_touchDebugRegister, 0);
}
DebuggerState.globalDebug=FALSE; //stop when possible, saves speed
DebuggerState.isDebugging=FALSE;
for (i=0; i<4; i++)
DebuggerState.breakpoint[i].active=FALSE;
//unhook all processors
forEachCpuPassive(debugger_removeHookForCurrentCPU, 0);
return TRUE;
}
int debugger_unsetGDBreakpoint(int breakpointnr)
{
int result=DebuggerState.breakpoint[breakpointnr].active;
DebuggerState.breakpoint[breakpointnr].active=FALSE;
return result; //returns true if it was active
}
int debugger_setGDBreakpoint(int breakpointnr, ULONG_PTR Address, BreakType bt, BreakLength bl)
/*
Will register a specific breakpoint. If global debug is used it'll set this debug register accordingly
*/
{
DbgPrint("debugger_setGDBreakpoint(%d, %x, %d, %d)\n", breakpointnr, Address, bt, bl);
DebuggerState.breakpoint[breakpointnr].active=TRUE;
DebuggerState.breakpoint[breakpointnr].address=Address;
DebuggerState.breakpoint[breakpointnr].breakType=bt;
DebuggerState.breakpoint[breakpointnr].breakLength=bl;
return TRUE;
}
NTSTATUS debugger_waitForDebugEvent(ULONG timeout)
{
NTSTATUS r;
LARGE_INTEGER wait;
//DbgPrint("debugger_waitForDebugEvent with timeout of %d\n",timeout);
//-10000000LL=1 second
//-10000LL should be 1 millisecond
//-10000LL
wait.QuadPart=-10000LL * timeout;
if (timeout==0xffffffff) //infinite wait
r=KeWaitForSingleObject(&debugger_event_WaitForDebugEvent, UserRequest, KernelMode, TRUE, NULL);
else
r=KeWaitForSingleObject(&debugger_event_WaitForDebugEvent, UserRequest, KernelMode, TRUE, &wait);
if (r==STATUS_SUCCESS)
return r;
else
return STATUS_UNSUCCESSFUL;
}
NTSTATUS debugger_continueDebugEvent(BOOL handled)
/*
Only call this by one thread only, and only when there's actually a debug eevnt in progress
*/
{
DbgPrint("debugger_continueDebugEvent\n");
DebuggerState.handledlastevent=handled;
KeSetEvent(&debugger_event_WaitForContinue, 0,FALSE);
return STATUS_SUCCESS;
}
UINT_PTR *debugger_getLastStackPointer(void)
{
return DebuggerState.LastStackPointer;
}
NTSTATUS debugger_getDebuggerState(PDebugStackState state)
{
DbgPrint("debugger_getDebuggerState\n");
state->threadid=(UINT64)DebuggerState.LastThreadID;
if (DebuggerState.LastStackPointer)
{
state->rflags=(UINT_PTR)DebuggerState.LastStackPointer[si_eflags];
state->rax=DebuggerState.LastStackPointer[si_eax];
state->rbx=DebuggerState.LastStackPointer[si_ebx];
state->rcx=DebuggerState.LastStackPointer[si_ecx];
state->rdx=DebuggerState.LastStackPointer[si_edx];
state->rsi=DebuggerState.LastStackPointer[si_esi];
state->rdi=DebuggerState.LastStackPointer[si_edi];
state->rbp=DebuggerState.LastStackPointer[si_ebp];
#ifdef AMD64
//fill in the extra registers
state->r8=DebuggerState.LastStackPointer[si_r8];
state->r9=DebuggerState.LastStackPointer[si_r9];
state->r10=DebuggerState.LastStackPointer[si_r10];
state->r11=DebuggerState.LastStackPointer[si_r11];
state->r12=DebuggerState.LastStackPointer[si_r12];
state->r13=DebuggerState.LastStackPointer[si_r13];
state->r14=DebuggerState.LastStackPointer[si_r14];
state->r15=DebuggerState.LastStackPointer[si_r15];
#endif
memcpy(state->fxstate, (void *)DebuggerState.fxstate,512);
//generally speaking, NOTHING should touch the esp register, but i'll provide it anyhow
if ((DebuggerState.LastStackPointer[si_cs] & 3) == 3) //if usermode code segment
{
//priv level change, so the stack info was pushed as well
state->rsp=DebuggerState.LastStackPointer[si_esp];
state->ss=DebuggerState.LastStackPointer[si_ss];
}
else
{
//kernelmode stack, yeah, it's really useless here since changing it here only means certain doom, but hey...
state->rsp=(UINT_PTR)(DebuggerState.LastStackPointer)-4;
state->ss=getSS();; //unchangeble by the user
}
state->rip=DebuggerState.LastStackPointer[si_eip];
state->cs=DebuggerState.LastStackPointer[si_cs];
state->ds=DebuggerState.LastStackPointer[si_ds];
state->es=DebuggerState.LastStackPointer[si_es];
#ifdef AMD64
state->fs=0;
state->gs=0;
#else
state->fs=DebuggerState.LastStackPointer[si_fs];
state->gs=DebuggerState.LastStackPointer[si_gs];
#endif
state->dr0=DebuggerState.LastRealDebugRegisters[0];
state->dr1=DebuggerState.LastRealDebugRegisters[1];
state->dr2=DebuggerState.LastRealDebugRegisters[2];
state->dr3=DebuggerState.LastRealDebugRegisters[3];
state->dr6=DebuggerState.LastRealDebugRegisters[4];
state->dr7=DebuggerState.LastRealDebugRegisters[5];
if (DebuggerState.storeLBR)
{
DbgPrint("Copying the LBR stack to usermode\n");
DbgPrint("storeLBR_max=%d\n", DebuggerState.storeLBR_max);
for (state->LBR_Count=0; state->LBR_Count<DebuggerState.storeLBR_max; state->LBR_Count++ )
{
DbgPrint("DebuggerState.LastLBRStack[%d]=%x\n", state->LBR_Count, DebuggerState.LastLBRStack[state->LBR_Count]);
state->LBR[state->LBR_Count]=DebuggerState.LastLBRStack[state->LBR_Count];
if (state->LBR[state->LBR_Count]==0) //no need to copy once a 0 has been reached
break;
}
}
else
state->LBR_Count=0;
return STATUS_SUCCESS;
}
else
{
DbgPrint("debugger_getDebuggerState was called while DebuggerState.LastStackPointer was still NULL");
return STATUS_UNSUCCESSFUL;
}
}
NTSTATUS debugger_setDebuggerState(PDebugStackState state)
{
if (DebuggerState.LastStackPointer)
{
DebuggerState.LastStackPointer[si_eflags]=(UINT_PTR)state->rflags;
DbgPrint("have set eflags to %x\n",DebuggerState.LastStackPointer[si_eflags]);
DebuggerState.LastStackPointer[si_eax]=(UINT_PTR)state->rax;
DebuggerState.LastStackPointer[si_ebx]=(UINT_PTR)state->rbx;
DebuggerState.LastStackPointer[si_ecx]=(UINT_PTR)state->rcx;
DebuggerState.LastStackPointer[si_edx]=(UINT_PTR)state->rdx;
DebuggerState.LastStackPointer[si_esi]=(UINT_PTR)state->rsi;
DebuggerState.LastStackPointer[si_edi]=(UINT_PTR)state->rdi;
DebuggerState.LastStackPointer[si_ebp]=(UINT_PTR)state->rbp;
//generally speaking, NOTHING should touch the esp register, but i'll provide it anyhow
if ((DebuggerState.LastStackPointer[si_cs] & 3) == 3) //if usermode code segment
{
//priv level change, so the stack info was pushed as well
DebuggerState.LastStackPointer[si_esp]=(UINT_PTR)state->rsp;
//don't mess with ss
}
else
{
//no change in kernelmode allowed
}
DebuggerState.LastStackPointer[si_eip]=(UINT_PTR)state->rip;
DebuggerState.LastStackPointer[si_cs]=(UINT_PTR)state->cs;
DebuggerState.LastStackPointer[si_ds]=(UINT_PTR)state->ds;
DebuggerState.LastStackPointer[si_es]=(UINT_PTR)state->es;
#ifndef AMD64
DebuggerState.LastStackPointer[si_fs]=(UINT_PTR)state->fs;
DebuggerState.LastStackPointer[si_gs]=(UINT_PTR)state->gs;
#else //don't touch fs or gs in 64-bit
DebuggerState.LastStackPointer[si_r8]=(UINT_PTR)state->r8;
DebuggerState.LastStackPointer[si_r9]=(UINT_PTR)state->r9;
DebuggerState.LastStackPointer[si_r10]=(UINT_PTR)state->r10;
DebuggerState.LastStackPointer[si_r11]=(UINT_PTR)state->r11;
DebuggerState.LastStackPointer[si_r12]=(UINT_PTR)state->r12;
DebuggerState.LastStackPointer[si_r13]=(UINT_PTR)state->r13;
DebuggerState.LastStackPointer[si_r14]=(UINT_PTR)state->r14;
DebuggerState.LastStackPointer[si_r15]=(UINT_PTR)state->r15;
#endif
if (!DebuggerState.globalDebug)
{
//no idea why someone would want to use this method, but it's in (for NON globaldebug only)
//updating this array too just so the user can see it got executed. (it eases their state of mind...)
DebuggerState.LastRealDebugRegisters[0]=(UINT_PTR)state->dr0;
DebuggerState.LastRealDebugRegisters[1]=(UINT_PTR)state->dr1;
DebuggerState.LastRealDebugRegisters[2]=(UINT_PTR)state->dr2;
DebuggerState.LastRealDebugRegisters[3]=(UINT_PTR)state->dr3;
DebuggerState.LastRealDebugRegisters[4]=(UINT_PTR)state->dr6;
DebuggerState.LastRealDebugRegisters[5]=(UINT_PTR)state->dr7;
//no setting of the DebugRegs here
}
}
else
{
DbgPrint("debugger_setDebuggerState was called while DebuggerState.LastStackPointer was still NULL");
return STATUS_UNSUCCESSFUL;
}
return STATUS_SUCCESS;
}
int breakpointHandler_kernel(UINT_PTR *stackpointer, UINT_PTR *currentdebugregs, UINT_PTR *LBR_Stack)
//Notice: This routine is called when interrupts are enabled and the GD bit has been set if globaL DEBUGGING HAS BEEN USED
//Interrupts are enabled and should be at passive level, so taskswitching is possible
{
NTSTATUS r=STATUS_UNSUCCESSFUL;
int handled=0; //0 means let the OS handle it
LARGE_INTEGER timeout;
timeout.QuadPart=-100000;
//DbgPrint("breakpointHandler for kernel breakpoints\n");
#ifdef AMD64
DbgPrint("cs=%x ss=%x ds=%x es=%x fs=%x gs=%x\n",getCS(), getSS(), getDS(), getES(), getFS(), getGS());
DbgPrint("fsbase=%llx gsbase=%llx gskernel=%llx\n", readMSR(0xc0000100), readMSR(0xc0000101), readMSR(0xc0000102));
DbgPrint("rbp=%llx\n", getRBP());
DbgPrint("gs:188=%llx\n", __readgsqword(0x188));
#endif
if (KeGetCurrentIrql()==0)
{
//crititical section here
if ((stackpointer[si_cs] & 3)==0)
{
DbgPrint("Going to wait in a kernelmode routine\n");
}
//block other threads from breaking until this one has been handled
while (r != STATUS_SUCCESS)
{
r=KeWaitForSingleObject(&debugger_event_CanBreak,Executive, KernelMode, FALSE, NULL);
//check r and handle specific events
DbgPrint("Woke up. r=%x\n",r);
}
if ((stackpointer[si_cs] & 3)==0)
{
DbgPrint("Woke up in a kernelmode routine\n");
}
//We're here, let's notify the usermode debugger of our situation
//first store the stackpointer so it can be manipulated externally
DebuggerState.LastStackPointer=stackpointer;
DebuggerState.LastRealDebugRegisters=currentdebugregs;
DebuggerState.LastLBRStack=LBR_Stack;
DebuggerState.LastThreadID=PsGetCurrentThreadId();
#ifdef AMD64
_fxsave(DebuggerState.fxstate);
#else
__asm
{
fxsave [DebuggerState.fxstate]
}
#endif
//notify usermode app that this thread has halted due to a debug event
KeSetEvent(&debugger_event_WaitForDebugEvent,0,FALSE);
//wait for event from usermode that debgu event has been handled
//KeWaitForSingleObject();
//continue with state
while (1)
{
//LARGE_INTEGER wt;
NTSTATUS s=STATUS_UNSUCCESSFUL;
//wt.QuadPart=-10000000LL;
//s=KeDelayExecutionThread(KernelMode, FALSE, &wt);
DbgPrint("Waiting...\n");
while (s != STATUS_SUCCESS)
{
s=KeWaitForSingleObject(&debugger_event_WaitForContinue, Executive, KernelMode, FALSE, NULL);
DbgPrint("KeWaitForSingleObject=%x\n",s);
}
if (s==STATUS_SUCCESS)
{
if (DebuggerState.handledlastevent)
{
//DbgPrint("handledlastevent=TRUE");
handled=1;
}
else
handled=0;
break;
}
}
DebuggerState.LastStackPointer=NULL; //NULL the stackpointer so routines know it should not be called
//i'm done, let other threads catch it
KeSetEvent(&debugger_event_CanBreak, 0, FALSE);
DbgPrint("Returning after a wait. handled=%d and eflags=%x\n",handled, stackpointer[si_eflags]);
if ((stackpointer[si_cs] & 3)==0) //check rpl of cs
{
DbgPrint("and in kernelmode\n");
}
return handled;
}
else
{
DbgPrint("Breakpoint wasn't at passive level. Screw this, i'm not going to break here\n");
return 1;
}
}
int interrupt1_handler(UINT_PTR *stackpointer, UINT_PTR *currentdebugregs)
{
HANDLE CurrentProcessID=PsGetCurrentProcessId();
UINT_PTR originaldr6=currentdebugregs[4];
DebugReg6 _dr6=*(DebugReg6 *)¤tdebugregs[4];
UINT_PTR LBR_Stack[16]; //max 16
// DebugReg7 _dr7=*(DebugReg7 *)¤tdebugregs[5];
if (cpu_familyID==0x6)
{
if (DebuggerState.storeLBR)
{
//fetch the lbr stack
int MSR_LASTBRANCH_TOS=0x1c9;
int MSR_LASTBRANCH_0=0x40;
int i;
int count;
i=(int)__readmsr(MSR_LASTBRANCH_TOS);
count=0;
while (count<DebuggerState.storeLBR_max)
{
UINT64 x;
x=__readmsr(MSR_LASTBRANCH_0+i);
LBR_Stack[count]=(UINT_PTR)x;
__writemsr(MSR_LASTBRANCH_0+i,0); //it has been read out, so can be erased now
count++;
i++;
i=i % DebuggerState.storeLBR_max;
}
}
}
//DbgPrint("interrupt1_handler\n");
//check if this break should be handled or not
if (DebuggerState.globalDebug)
{
//DbgPrint("DebuggerState.globalDebug=TRUE\n");
//global debugging is being used
if (_dr6.BD)
{
//The debug registers are being accessed, emulate it with DebuggerState.FakedDebugRegisterState[cpunr()].DRx
int instructionPointer;
#ifdef AMD64
int prefixpointer;
#endif
int currentcpunr=cpunr();
int debugregister;
int generalpurposeregister;
unsigned char *instruction=(unsigned char *)stackpointer[si_eip];
//unset this flag in DR6
_dr6.BD=0;
debugger_dr6_setValue(*(UINT_PTR *)&_dr6);
if (DebuggerState.FakedDebugRegisterState[cpunr()].inEpilogue)
{
((EFLAGS *)&stackpointer[si_eflags])->RF=1; //repeat this instruction and don't break
return 2;
}
//DbgPrint("handler: Setting fake dr6 to %x\n",*(UINT_PTR *)&_dr6);
//DebuggerState.FakedDebugRegisterState[cpunr()].DR6=*(UINT_PTR *)&_dr6;
for (instructionPointer=0; instruction[instructionPointer] != 0x0f; instructionPointer++) ; //find the start of the instruction, skipping prefixes etc...
//we now have the start of the instruction.
//Find out which instruction it is, and which register is used
debugregister=(instruction[instructionPointer+2] >> 3) & 7;
generalpurposeregister=instruction[instructionPointer+2] & 7;
#ifdef AMD64
for (prefixpointer=0; prefixpointer<instructionPointer; prefixpointer++)
{
//check for a REX.B prefix (0x40 + 0x1 : 0x41)
if ((instruction[prefixpointer] & 0x41) == 0x41)
{
//rex.b prefix is used, r8 to r15 are being accessed
generalpurposeregister+=8;
}
}
#endif
//DbgPrint("debugregister=%d, generalpurposeregister=%d\n",debugregister,generalpurposeregister);
if (instruction[instructionPointer+1]==0x21)
{
UINT_PTR drvalue=0;
//DbgPrint("read opperation\n");
//21=read
switch (debugregister)
{
case 0:
drvalue=DebuggerState.FakedDebugRegisterState[cpunr()].DR0;
//DbgPrint("Reading DR0 (returning %x real %x)\n", drvalue, currentdebugregs[0]);
break;
case 1:
drvalue=DebuggerState.FakedDebugRegisterState[cpunr()].DR1;
break;
case 2:
drvalue=DebuggerState.FakedDebugRegisterState[cpunr()].DR2;
break;
case 3:
drvalue=DebuggerState.FakedDebugRegisterState[cpunr()].DR3;
break;
case 4:
case 6:
drvalue=DebuggerState.FakedDebugRegisterState[cpunr()].DR6;
//DbgPrint("reading dr6 value:%x\n",drvalue);
break;
case 5:
case 7:
drvalue=DebuggerState.FakedDebugRegisterState[cpunr()].DR7;
break;
default:
DbgPrint("Invalid debugregister\n");
drvalue = 0;
break;
}
switch (generalpurposeregister)
{
case 0:
stackpointer[si_eax]=drvalue;
break;
case 1:
stackpointer[si_ecx]=drvalue;
break;
case 2:
stackpointer[si_edx]=drvalue;
break;
case 3:
stackpointer[si_ebx]=drvalue;
break;
case 4:
if ((stackpointer[si_cs] & 3) == 3) //usermode dr access ?
stackpointer[si_esp]=drvalue;
else
stackpointer[si_stack_esp]=drvalue;
break;
case 5:
stackpointer[si_ebp]=drvalue;
break;
case 6:
stackpointer[si_esi]=drvalue;
break;
case 7:
stackpointer[si_edi]=drvalue;
break;
#ifdef AMD64
case 8:
stackpointer[si_r8]=drvalue;
break;
case 9:
stackpointer[si_r9]=drvalue;
break;
case 10:
stackpointer[si_r10]=drvalue;
break;
case 11:
stackpointer[si_r11]=drvalue;
break;
case 12:
stackpointer[si_r12]=drvalue;
break;
case 13:
stackpointer[si_r13]=drvalue;
break;
case 14:
stackpointer[si_r14]=drvalue;
break;
case 15:
stackpointer[si_r15]=drvalue;
break;
#endif
}
}
else
if (instruction[instructionPointer+1]==0x23)
{
//23=write
UINT_PTR gpvalue=0;
//DbgPrint("Write operation\n");
switch (generalpurposeregister)
{
case 0:
gpvalue=stackpointer[si_eax];
break;
case 1:
gpvalue=stackpointer[si_ecx];
break;
case 2:
gpvalue=stackpointer[si_edx];
break;
case 3:
gpvalue=stackpointer[si_ebx];
break;
case 4:
if ((stackpointer[si_cs] & 3) == 3)
gpvalue=stackpointer[si_esp];
break;
case 5:
gpvalue=stackpointer[si_ebp];
break;
case 6:
gpvalue=stackpointer[si_esi];
break;
case 7:
gpvalue=stackpointer[si_edi];
break;
#ifdef AMD64
case 8:
gpvalue=stackpointer[si_r8];
break;