1 /****************************************************************************
3 THIS SOFTWARE IS NOT COPYRIGHTED
5 HP offers the following for use in the public domain. HP makes no
6 warranty with regard to the software or it's performance and the
7 user accepts the software "AS IS" with all faults.
9 HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
10 TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
11 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 ****************************************************************************/
15 /****************************************************************************
16 * Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
18 * Module name: remcom.c $
20 * Date: 91/03/09 12:29:49 $
21 * Contributor: Lake Stevens Instrument Division$
23 * Description: low level support for gdb debugger. $
25 * Considerations: only works on target hardware $
27 * Written by: Glenn Engel $
28 * ModuleState: Experimental $
32 * Modified for 386 by Jim Kingdon, Cygnus Support.
33 * Modified for ReactOS by Casper S. Hornstrup <chorns@users.sourceforge.net>
35 * To enable debugger support, two things need to happen. One, setting
36 * up a routine so that it is in the exception path, is necessary in order
37 * to allow any breakpoints or error conditions to be properly intercepted
38 * and reported to gdb.
39 * Two, a breakpoint needs to be generated to begin communication.
41 * Because gdb will sometimes write to the stack area to execute function
42 * calls, this program cannot rely on using the supervisor stack so it
43 * uses it's own stack area.
47 * The following gdb commands are supported:
49 * command function Return value
51 * g return the value of the CPU Registers hex data or ENN
52 * G set the value of the CPU Registers OK or ENN
54 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
55 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
57 * c Resume at current address SNN ( signal NN)
58 * cAA..AA Continue at address AA..AA SNN
60 * s Step one instruction SNN
61 * sAA..AA Step one instruction from AA..AA SNN
65 * ? What was the last sigval ? SNN (signal NN)
67 * All commands and responses are sent with a packet which includes a
68 * Checksum. A packet consists of
70 * $<packet info>#<Checksum>.
73 * <packet info> :: <characters representing the command or response>
74 * <Checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
76 * When a packet is received, it is first acknowledged with either '+' or '-'.
77 * '+' indicates a successful transfer. '-' indicates a failed transfer.
82 * $m0,10#2a +$00010203040506070809101112131415#42
84 ****************************************************************************/
88 #include <internal/debug.h>
89 #include <internal/ps.h>
91 /************************************************************************/
92 /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
93 /* at least NUMREGBYTES*2 are needed for register packets */
96 static BOOLEAN GspInitialized
;
98 static BOOLEAN GspRemoteDebug
;
100 static CONST CHAR HexChars
[]="0123456789abcdef";
102 static PETHREAD GspRunThread
; /* NULL means run all threads */
103 static PETHREAD GspDbgThread
;
104 static PETHREAD GspEnumThread
;
106 static FAST_MUTEX GspLock
;
108 extern LIST_ENTRY PsActiveProcessHead
;
109 KD_PORT_INFORMATION GdbPortInfo
= { 2, 115200, 0 }; /* FIXME hardcoded for COM2, 115200 baud */
111 /* Number of Registers. */
116 EAX
, ECX
, EDX
, EBX
, ESP
, EBP
, ESI
, EDI
,
117 PC
/* also known as eip */,
118 PS
/* also known as eflags */,
119 CS
, SS
, DS
, ES
, FS
, GS
122 typedef struct _CPU_REGISTER
126 DWORD OffsetInContext
;
127 BOOLEAN SetInContext
;
128 } CPU_REGISTER
, *PCPU_REGISTER
;
130 static CPU_REGISTER GspRegisters
[NUMREGS
] =
132 { 4, FIELD_OFFSET(KTRAP_FRAME
, Eax
), FIELD_OFFSET(CONTEXT
, Eax
), TRUE
},
133 { 4, FIELD_OFFSET(KTRAP_FRAME
, Ecx
), FIELD_OFFSET(CONTEXT
, Ecx
), TRUE
},
134 { 4, FIELD_OFFSET(KTRAP_FRAME
, Edx
), FIELD_OFFSET(CONTEXT
, Edx
), FALSE
},
135 { 4, FIELD_OFFSET(KTRAP_FRAME
, Ebx
), FIELD_OFFSET(CONTEXT
, Ebx
), TRUE
},
136 { 4, FIELD_OFFSET(KTRAP_FRAME
, Esp
), FIELD_OFFSET(CONTEXT
, Esp
), TRUE
},
137 { 4, FIELD_OFFSET(KTRAP_FRAME
, DebugEbp
), FIELD_OFFSET(CONTEXT
, Ebp
), TRUE
},
138 { 4, FIELD_OFFSET(KTRAP_FRAME
, Esi
), FIELD_OFFSET(CONTEXT
, Esi
), TRUE
},
139 { 4, FIELD_OFFSET(KTRAP_FRAME
, Edi
), FIELD_OFFSET(CONTEXT
, Edi
), TRUE
},
140 { 4, FIELD_OFFSET(KTRAP_FRAME
, DebugEip
), FIELD_OFFSET(CONTEXT
, Eip
), TRUE
},
141 { 4, FIELD_OFFSET(KTRAP_FRAME
, Eflags
), FIELD_OFFSET(CONTEXT
, EFlags
), TRUE
},
142 { 4, FIELD_OFFSET(KTRAP_FRAME
, Cs
), FIELD_OFFSET(CONTEXT
, SegCs
), TRUE
},
143 { 4, FIELD_OFFSET(KTRAP_FRAME
, Ss
), FIELD_OFFSET(CONTEXT
, SegSs
), TRUE
},
144 { 4, FIELD_OFFSET(KTRAP_FRAME
, Ds
), FIELD_OFFSET(CONTEXT
, SegDs
), TRUE
},
145 { 4, FIELD_OFFSET(KTRAP_FRAME
, Es
), FIELD_OFFSET(CONTEXT
, SegEs
), TRUE
},
146 { 4, FIELD_OFFSET(KTRAP_FRAME
, Fs
), FIELD_OFFSET(CONTEXT
, SegFs
), TRUE
},
147 { 4, FIELD_OFFSET(KTRAP_FRAME
, Gs
), FIELD_OFFSET(CONTEXT
, SegGs
), TRUE
}
150 static PCHAR GspThreadStates
[DeferredReady
+1] =
166 if ((ch
>= '0') && (ch
<= '9'))
170 if ((ch
>= 'a') && (ch
<= 'f'))
172 return (ch
- 'a' + 10);
174 if ((ch
>= 'A') && (ch
<= 'F'))
176 return (ch
- 'A' + 10);
182 static CHAR GspInBuffer
[BUFMAX
];
183 static CHAR GspOutBuffer
[BUFMAX
];
186 GdbPutChar(UCHAR Value
)
188 KdPortPutByteEx(&GdbPortInfo
, Value
);
196 while (!KdPortGetByteEx(&GdbPortInfo
, &Value
))
202 /* scan for the sequence $<data>#<Checksum> */
207 PCHAR Buffer
= &GspInBuffer
[0];
215 /* wait around for the start character, ignore all other characters */
216 while ((ch
= GdbGetChar ()) != '$')
224 /* now, read until a # or end of Buffer is found */
225 while (Count
< BUFMAX
)
236 Checksum
= Checksum
+ ch
;
245 XmitChecksum
= (CHAR
)(HexValue(ch
) << 4);
247 XmitChecksum
+= (CHAR
)(HexValue(ch
));
249 if (Checksum
!= XmitChecksum
)
251 GdbPutChar('-'); /* failed checksum */
255 GdbPutChar('+'); /* successful transfer */
263 /* send the packet in Buffer. */
266 GspPutPacket(PCHAR Buffer
)
272 /* $<packet info>#<Checksum>. */
279 while ((ch
= Buffer
[Count
]))
287 GdbPutChar(HexChars
[(Checksum
>> 4) & 0xf]);
288 GdbPutChar(HexChars
[Checksum
& 0xf]);
290 while (GdbGetChar() != '+');
295 GspPutPacketNoWait(PCHAR Buffer
)
301 /* $<packet info>#<Checksum>. */
306 while ((ch
= Buffer
[Count
]))
314 GdbPutChar(HexChars
[(Checksum
>> 4) & 0xf]);
315 GdbPutChar(HexChars
[Checksum
& 0xf]);
318 /* Indicate to caller of GspMem2Hex or GspHex2Mem that there has been an
320 static volatile BOOLEAN GspMemoryError
= FALSE
;
321 static volatile void *GspAccessLocation
= NULL
;
324 GspReadMemSafe(PCHAR Address
)
330 GspMemoryError
= TRUE
;
334 GspAccessLocation
= Address
;
336 GspAccessLocation
= NULL
;
341 /* Convert the memory pointed to by Address into hex, placing result in Buffer */
342 /* Return a pointer to the last char put in Buffer (null) */
343 /* If MayFault is TRUE, then we should set GspMemoryError in response to
344 a fault; if FALSE treat a fault like any other fault in the stub. */
346 GspMem2Hex(PCHAR Address
,
354 for (i
= 0; i
< (ULONG
) Count
; i
++)
358 ch
= GspReadMemSafe(Address
);
368 *Buffer
++ = HexChars
[(ch
>> 4) & 0xf];
369 *Buffer
++ = HexChars
[ch
& 0xf];
378 GspWriteMem(PCHAR Address
,
381 CHAR (*GetContent
)(PVOID Context
, ULONG Offset
),
392 while (Current
< Address
+ Count
)
394 Page
= (PCHAR
)PAGE_ROUND_DOWN(Current
);
395 if (Address
+ Count
<= Page
+ PAGE_SIZE
)
397 /* Fits in this page */
402 /* Flows into next page, handle only current page in this iteration */
403 CountInPage
= PAGE_SIZE
- (Address
- Page
);
407 OldProt
= MmGetPageProtect(NULL
, Address
);
408 MmSetPageProtect(NULL
, Address
, PAGE_EXECUTE_READWRITE
);
411 for (i
= 0; i
< CountInPage
&& ! GspMemoryError
; i
++)
413 ch
= (*GetContent
)(Context
, Current
- Address
);
417 GspAccessLocation
= Current
;
422 GspAccessLocation
= NULL
;
428 MmSetPageProtect(NULL
, Page
, OldProt
);
431 return Current
- Address
;
436 return Current
- Address
;
440 GspHex2MemGetContent(PVOID Context
, ULONG Offset
)
442 return (CHAR
)((HexValue(*((PCHAR
) Context
+ 2 * Offset
)) << 4) +
443 HexValue(*((PCHAR
) Context
+ 2 * Offset
+ 1)));
446 /* Convert the hex array pointed to by Buffer into binary to be placed at Address */
447 /* Return a pointer to the character AFTER the last byte read from Buffer */
449 GspHex2Mem(PCHAR Buffer
,
454 Count
= GspWriteMem(Address
, Count
, MayFault
, GspHex2MemGetContent
, Buffer
);
456 return Buffer
+ 2 * Count
;
460 GspWriteMemSafeGetContent(PVOID Context
, ULONG Offset
)
464 return *((PCHAR
) Context
);
468 GspWriteMemSafe(PCHAR Address
,
471 GspWriteMem(Address
, 1, TRUE
, GspWriteMemSafeGetContent
, &Ch
);
475 /* This function takes the 386 exception vector and attempts to
476 translate this number into a unix compatible signal value */
478 GspComputeSignal(NTSTATUS ExceptionCode
)
482 switch (ExceptionCode
)
484 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
485 SigVal
= 8; /* divide by zero */
487 case STATUS_SINGLE_STEP
:
488 case STATUS_BREAKPOINT
:
489 SigVal
= 5; /* breakpoint */
491 case STATUS_INTEGER_OVERFLOW
:
492 case STATUS_ARRAY_BOUNDS_EXCEEDED
:
493 SigVal
= 16; /* bound instruction */
495 case STATUS_ILLEGAL_INSTRUCTION
:
496 SigVal
= 4; /* Invalid opcode */
498 case STATUS_STACK_OVERFLOW
:
499 case STATUS_DATATYPE_MISALIGNMENT
:
500 case STATUS_ACCESS_VIOLATION
:
501 SigVal
= 11; /* access violation */
504 SigVal
= 7; /* "software generated" */
510 /**********************************************/
511 /* WHILE WE FIND NICE HEX CHARS, BUILD A LONG */
512 /* RETURN NUMBER OF CHARS PROCESSED */
513 /**********************************************/
515 GspHex2Long(PCHAR
*Address
,
525 Hex
= HexValue(**Address
);
528 *Value
= (*Value
<< 4) | Hex
;
544 GspLong2Hex(PCHAR
*Address
,
549 Save
= (((Value
>> 0) & 0xff) << 24) |
550 (((Value
>> 8) & 0xff) << 16) |
551 (((Value
>> 16) & 0xff) << 8) |
552 (((Value
>> 24) & 0xff) << 0);
553 *Address
= GspMem2Hex((PCHAR
) &Save
, *Address
, 4, FALSE
);
558 * When coming from kernel mode, Esp is not stored in the trap frame.
559 * Instead, it was pointing to the location of the TrapFrame Esp member
560 * when the exception occured. When coming from user mode, Esp is just
561 * stored in the TrapFrame Esp member.
564 GspGetEspFromTrapFrame(PKTRAP_FRAME TrapFrame
)
566 return KeGetPreviousMode() == KernelMode
567 ? (LONG
) &TrapFrame
->Esp
: (LONG
)TrapFrame
->Esp
;
572 GspGetRegisters(PCHAR Address
,
573 PKTRAP_FRAME TrapFrame
)
579 ULONG_PTR
*KernelStack
;
581 if (NULL
== GspDbgThread
)
583 Thread
= PsGetCurrentThread();
587 TrapFrame
= GspDbgThread
->Tcb
.TrapFrame
;
588 Thread
= GspDbgThread
;
591 if (Waiting
== Thread
->Tcb
.State
)
593 KernelStack
= Thread
->Tcb
.KernelStack
;
594 for (i
= 0; i
< sizeof(GspRegisters
) / sizeof(GspRegisters
[0]); i
++)
599 Value
= KernelStack
[3];
602 Value
= KernelStack
[4];
605 Value
= KernelStack
[5];
608 Value
= KernelStack
[6];
611 Value
= KernelStack
[7];
614 Value
= (ULONG_PTR
) (KernelStack
+ 8);
617 Value
= KGDT_R0_CODE
;
620 Value
= KGDT_R0_DATA
;
626 Address
= GspMem2Hex((PCHAR
) &Value
, Address
, GspRegisters
[i
].Size
,
632 for (i
= 0; i
< sizeof(GspRegisters
) / sizeof(GspRegisters
[0]); i
++)
638 Value
= GspGetEspFromTrapFrame(TrapFrame
);
642 p
= (PULONG
)((ULONG_PTR
) TrapFrame
+
643 GspRegisters
[i
].OffsetInTF
);
650 * This thread has not been sheduled yet so assume it
651 * is still in PsBeginThreadWithContextInternal().
653 Value
= (ULONG
)KiThreadStartup
;
659 Address
= GspMem2Hex((PCHAR
) &Value
, Address
,
660 GspRegisters
[i
].Size
, FALSE
);
667 GspSetRegistersInTrapFrame(PCHAR Address
,
669 PKTRAP_FRAME TrapFrame
)
682 for (i
= 0; i
< NUMREGS
; i
++)
684 if (GspRegisters
[i
].SetInContext
)
686 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[i
].OffsetInContext
);
690 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[i
].OffsetInTF
);
693 Buffer
= GspHex2Mem(Buffer
, (PCHAR
) &Value
, GspRegisters
[i
].Size
, FALSE
);
700 GspSetSingleRegisterInTrapFrame(PCHAR Address
,
703 PKTRAP_FRAME TrapFrame
)
713 if (GspRegisters
[Number
].SetInContext
)
715 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[Number
].OffsetInContext
);
719 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[Number
].OffsetInTF
);
722 GspHex2Mem(Address
, (PCHAR
) &Value
, GspRegisters
[Number
].Size
, FALSE
);
728 GspFindThread(PCHAR Data
,
731 PETHREAD ThreadInfo
= NULL
;
733 if (strcmp (Data
, "-1") == 0)
742 PCHAR ptr
= &Data
[0];
744 GspHex2Long(&ptr
, (PLONG
) &uThreadId
);
745 ThreadId
= (HANDLE
)uThreadId
;
747 if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId
, &ThreadInfo
)))
753 *Thread
= ThreadInfo
;
759 GspSetThread(PCHAR Request
)
762 PCHAR ptr
= &Request
[1];
766 case 'c': /* Run thread */
767 if (GspFindThread(ptr
, &ThreadInfo
))
769 GspOutBuffer
[0] = 'O';
770 GspOutBuffer
[1] = 'K';
772 if (NULL
!= GspRunThread
)
774 ObDereferenceObject(GspRunThread
);
776 GspRunThread
= ThreadInfo
;
777 if (NULL
!= GspRunThread
)
779 ObReferenceObject(GspRunThread
);
784 GspOutBuffer
[0] = 'E';
787 case 'g': /* Debug thread */
788 if (GspFindThread(ptr
, &ThreadInfo
))
790 GspOutBuffer
[0] = 'O';
791 GspOutBuffer
[1] = 'K';
793 if (NULL
!= GspDbgThread
)
795 ObDereferenceObject(GspDbgThread
);
798 if (ThreadInfo
== PsGetCurrentThread())
801 ObDereferenceObject(ThreadInfo
);
805 GspDbgThread
= ThreadInfo
;
810 GspOutBuffer
[0] = 'E';
820 GspQuery(PCHAR Request
)
824 if (strncmp(Request
, "C", 1) == 0)
826 PCHAR ptr
= &GspOutBuffer
[2];
828 /* Get current thread id */
829 GspOutBuffer
[0] = 'Q';
830 GspOutBuffer
[1] = 'C';
831 if (NULL
!= GspDbgThread
)
833 Value
= (ULONG
) GspDbgThread
->Cid
.UniqueThread
;
837 Value
= (ULONG
) PsGetCurrentThread()->Cid
.UniqueThread
;
839 GspLong2Hex(&ptr
, Value
);
841 else if (strncmp(Request
, "fThreadInfo", 11) == 0)
844 PLIST_ENTRY AThread
, AProcess
;
845 PCHAR ptr
= &GspOutBuffer
[1];
847 /* Get first thread id */
848 GspEnumThread
= NULL
;
849 AProcess
= PsActiveProcessHead
.Flink
;
850 while(AProcess
!= &PsActiveProcessHead
)
852 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
853 AThread
= Process
->ThreadListHead
.Flink
;
854 if (AThread
!= &Process
->ThreadListHead
)
856 GspEnumThread
= CONTAINING_RECORD(Process
->ThreadListHead
.Flink
,
857 ETHREAD
, ThreadListEntry
);
860 AProcess
= AProcess
->Flink
;
862 if(GspEnumThread
!= NULL
)
864 GspOutBuffer
[0] = 'm';
865 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
866 GspLong2Hex(&ptr
, Value
);
870 /* FIXME - what to do here? This case should never happen though, there
871 should always be at least one thread on the system... */
872 /* GspOutBuffer[0] = 'l'; */
875 else if (strncmp(Request
, "sThreadInfo", 11) == 0)
878 PLIST_ENTRY AThread
, AProcess
;
879 PCHAR ptr
= &GspOutBuffer
[1];
881 /* Get next thread id */
882 if (GspEnumThread
!= NULL
)
884 /* find the next thread */
885 Process
= GspEnumThread
->ThreadsProcess
;
886 if(GspEnumThread
->ThreadListEntry
.Flink
!= &Process
->ThreadListHead
)
888 GspEnumThread
= CONTAINING_RECORD(GspEnumThread
->ThreadListEntry
.Flink
,
889 ETHREAD
, ThreadListEntry
);
893 PETHREAD Thread
= NULL
;
894 AProcess
= Process
->ActiveProcessLinks
.Flink
;
895 while(AProcess
!= &PsActiveProcessHead
)
897 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
898 AThread
= Process
->ThreadListHead
.Flink
;
899 if (AThread
!= &Process
->ThreadListHead
)
901 Thread
= CONTAINING_RECORD(Process
->ThreadListHead
.Flink
,
902 ETHREAD
, ThreadListEntry
);
905 AProcess
= AProcess
->Flink
;
907 GspEnumThread
= Thread
;
910 if (GspEnumThread
!= NULL
)
913 GspOutBuffer
[0] = 'm';
914 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
915 GspLong2Hex(&ptr
, Value
);
919 GspOutBuffer
[0] = 'l';
924 GspOutBuffer
[0] = 'l';
927 else if (strncmp(Request
, "ThreadExtraInfo", 15) == 0)
931 /* Get thread information */
932 if (GspFindThread(Request
+ 16, &ThreadInfo
))
937 Proc
= (PEPROCESS
) ThreadInfo
->ThreadsProcess
;
942 sprintf(Buffer
, "%s [%d:0x%x], ", Proc
->ImageFileName
,
943 (int) Proc
->UniqueProcessId
,
944 (int) ThreadInfo
->Cid
.UniqueThread
);
946 strcpy(Buffer
+ strlen(Buffer
),
947 GspThreadStates
[ThreadInfo
->Tcb
.State
]);
949 ObDereferenceObject(ThreadInfo
);
951 GspMem2Hex(Buffer
, &GspOutBuffer
[0], strlen(Buffer
), FALSE
);
957 GspQueryThreadStatus(PCHAR Request
)
960 PCHAR ptr
= &Request
[0];
962 if (GspFindThread(ptr
, &ThreadInfo
))
964 ObDereferenceObject(ThreadInfo
);
966 GspOutBuffer
[0] = 'O';
967 GspOutBuffer
[1] = 'K';
968 GspOutBuffer
[2] = '\0';
972 GspOutBuffer
[0] = 'E';
973 GspOutBuffer
[1] = '\0';
977 #define DR7_L0 0x00000001 /* Local breakpoint 0 enable */
978 #define DR7_G0 0x00000002 /* Global breakpoint 0 enable */
979 #define DR7_L1 0x00000004 /* Local breakpoint 1 enable */
980 #define DR7_G1 0x00000008 /* Global breakpoint 1 enable */
981 #define DR7_L2 0x00000010 /* Local breakpoint 2 enable */
982 #define DR7_G2 0x00000020 /* Global breakpoint 2 enable */
983 #define DR7_L3 0x00000040 /* Local breakpoint 3 enable */
984 #define DR7_G3 0x00000080 /* Global breakpoint 3 enable */
985 #define DR7_LE 0x00000100 /* Local exact breakpoint enable (old) */
986 #define DR7_GE 0x00000200 /* Global exact breakpoint enable (old) */
987 #define DR7_GD 0x00002000 /* General detect enable */
988 #define DR7_TYPE0_MASK 0x00030000 /* Breakpoint 0 condition */
989 #define DR7_LEN0_MASK 0x000c0000 /* Breakpoint 0 length */
990 #define DR7_TYPE1_MASK 0x00300000 /* Breakpoint 1 condition */
991 #define DR7_LEN1_MASK 0x00c00000 /* Breakpoint 1 length */
992 #define DR7_TYPE2_MASK 0x03000000 /* Breakpoint 2 condition */
993 #define DR7_LEN2_MASK 0x0c000000 /* Breakpoint 2 length */
994 #define DR7_TYPE3_MASK 0x30000000 /* Breakpoint 3 condition */
995 #define DR7_LEN3_MASK 0xc0000000 /* Breakpoint 3 length */
996 #define DR7_GLOBAL_ENABLE(Bp) (2 << (2 * (Bp)))
997 #define DR7_TYPE(Bp, Type) ((Type) << (16 + 4 * (Bp)))
998 #define DR7_LEN(Bp, Len) ((Len) << (18 + 4 * (Bp)))
1000 #define I386_BP_TYPE_EXECUTE 0
1001 #define I386_BP_TYPE_DATA_WRITE 1
1002 #define I386_BP_TYPE_DATA_READWRITE 3
1004 #define I386_OPCODE_INT3 0xcc
1006 #define GDB_ZTYPE_MEMORY_BREAKPOINT 0
1007 #define GDB_ZTYPE_HARDWARE_BREAKPOINT 1
1008 #define GDB_ZTYPE_WRITE_WATCHPOINT 2
1009 #define GDB_ZTYPE_READ_WATCHPOINT 3
1010 #define GDB_ZTYPE_ACCESS_WATCHPOINT 4
1012 typedef struct _GSPHWBREAKPOINT
1019 #define MAX_HW_BREAKPOINTS 4
1020 static unsigned GspHwBreakpointCount
= 0;
1021 static GSPHWBREAKPOINT GspHwBreakpoints
[MAX_HW_BREAKPOINTS
];
1023 typedef struct _GSPSWBREAKPOINT
1030 #define MAX_SW_BREAKPOINTS 64
1031 static unsigned GspSwBreakpointCount
= 0;
1032 static GSPSWBREAKPOINT GspSwBreakpoints
[MAX_SW_BREAKPOINTS
];
1035 GspSetHwBreakpoint(ULONG Type
, ULONG_PTR Address
, ULONG Length
)
1037 DPRINT("GspSetHwBreakpoint(%lu, 0x%p, %lu)\n", Type
, Address
, Length
);
1039 if (GDB_ZTYPE_READ_WATCHPOINT
== Type
)
1041 DPRINT1("Read watchpoint not supported\n");
1042 strcpy(GspOutBuffer
, "E22");
1044 else if (GDB_ZTYPE_HARDWARE_BREAKPOINT
== Type
&& 1 != Length
)
1046 DPRINT1("Invalid length %lu for hardware breakpoint\n", Length
);
1047 strcpy(GspOutBuffer
, "E22");
1049 else if (1 != Length
&& 2 != Length
&& 4 != Length
)
1051 DPRINT1("Invalid length %lu for GDB Z type %lu\n", Length
, Type
);
1052 strcpy(GspOutBuffer
, "E22");
1054 else if (0 != (Address
& (Length
- 1)))
1056 DPRINT1("Invalid alignment for address 0x%p and length %d\n",
1058 strcpy(GspOutBuffer
, "E22");
1060 else if (MAX_HW_BREAKPOINTS
== GspHwBreakpointCount
)
1062 DPRINT1("Trying to set too many hardware breakpoints\n");
1063 strcpy(GspOutBuffer
, "E22");
1067 DPRINT("Stored at index %u\n", GspHwBreakpointCount
);
1068 GspHwBreakpoints
[GspHwBreakpointCount
].Type
= Type
;
1069 GspHwBreakpoints
[GspHwBreakpointCount
].Address
= Address
;
1070 GspHwBreakpoints
[GspHwBreakpointCount
].Length
= Length
;
1071 GspHwBreakpointCount
++;
1072 strcpy(GspOutBuffer
, "OK");
1077 GspRemoveHwBreakpoint(ULONG Type
, ULONG_PTR Address
, ULONG Length
)
1081 DPRINT("GspRemoveHwBreakpoint(%lu, 0x%p, %lu)\n", Type
, Address
, Length
);
1082 for (Index
= 0; Index
< GspHwBreakpointCount
; Index
++)
1084 if (GspHwBreakpoints
[Index
].Type
== Type
&&
1085 GspHwBreakpoints
[Index
].Address
== Address
&&
1086 GspHwBreakpoints
[Index
].Length
== Length
)
1088 DPRINT("Found match at index %u\n", Index
);
1089 if (Index
+ 1 < GspHwBreakpointCount
)
1091 memmove(GspHwBreakpoints
+ Index
,
1092 GspHwBreakpoints
+ (Index
+ 1),
1093 (GspHwBreakpointCount
- Index
- 1) *
1094 sizeof(GSPHWBREAKPOINT
));
1096 GspHwBreakpointCount
--;
1097 strcpy(GspOutBuffer
, "OK");
1102 DPRINT1("Not found\n");
1103 strcpy(GspOutBuffer
, "E22");
1107 GspSetSwBreakpoint(ULONG_PTR Address
)
1109 DPRINT("GspSetSwBreakpoint(0x%p)\n", Address
);
1111 if (MAX_SW_BREAKPOINTS
== GspSwBreakpointCount
)
1113 DPRINT1("Trying to set too many software breakpoints\n");
1114 strcpy(GspOutBuffer
, "E22");
1118 DPRINT("Stored at index %u\n", GspSwBreakpointCount
);
1119 GspSwBreakpoints
[GspSwBreakpointCount
].Address
= Address
;
1120 GspSwBreakpoints
[GspSwBreakpointCount
].Active
= FALSE
;
1121 GspSwBreakpointCount
++;
1122 strcpy(GspOutBuffer
, "OK");
1127 GspRemoveSwBreakpoint(ULONG_PTR Address
)
1131 DPRINT("GspRemoveSwBreakpoint(0x%p)\n", Address
);
1132 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1134 if (GspSwBreakpoints
[Index
].Address
== Address
)
1136 DPRINT("Found match at index %u\n", Index
);
1137 ASSERT(! GspSwBreakpoints
[Index
].Active
);
1138 if (Index
+ 1 < GspSwBreakpointCount
)
1140 memmove(GspSwBreakpoints
+ Index
,
1141 GspSwBreakpoints
+ (Index
+ 1),
1142 (GspSwBreakpointCount
- Index
- 1) *
1143 sizeof(GSPSWBREAKPOINT
));
1145 GspSwBreakpointCount
--;
1146 strcpy(GspOutBuffer
, "OK");
1151 DPRINT1("Not found\n");
1152 strcpy(GspOutBuffer
, "E22");
1156 GspLoadHwBreakpoint(PKTRAP_FRAME TrapFrame
,
1162 DPRINT("GspLoadHwBreakpoint(0x%p, %d, 0x%p, %d)\n", TrapFrame
, BpIndex
,
1165 /* Set the DR7_Gx bit to globally enable the breakpoint */
1166 TrapFrame
->Dr7
|= DR7_GLOBAL_ENABLE(BpIndex
) |
1167 DR7_LEN(BpIndex
, Length
) |
1168 DR7_TYPE(BpIndex
, Type
);
1173 DPRINT("Setting DR0 to 0x%p\n", Address
);
1174 TrapFrame
->Dr0
= Address
;
1178 DPRINT("Setting DR1 to 0x%p\n", Address
);
1179 TrapFrame
->Dr1
= Address
;
1183 DPRINT("Setting DR2 to 0x%p\n", Address
);
1184 TrapFrame
->Dr2
= Address
;
1188 DPRINT("Setting DR3 to 0x%p\n", Address
);
1189 TrapFrame
->Dr3
= Address
;
1195 GspLoadBreakpoints(PKTRAP_FRAME TrapFrame
)
1200 DPRINT("GspLoadBreakpoints\n");
1201 DPRINT("DR7 on entry: 0x%08x\n", TrapFrame
->Dr7
);
1202 /* Remove all breakpoints */
1203 TrapFrame
->Dr7
&= ~(DR7_L0
| DR7_L1
| DR7_L2
| DR7_L3
|
1204 DR7_G0
| DR7_G1
| DR7_G2
| DR7_G3
|
1205 DR7_TYPE0_MASK
| DR7_LEN0_MASK
|
1206 DR7_TYPE1_MASK
| DR7_LEN1_MASK
|
1207 DR7_TYPE2_MASK
| DR7_LEN2_MASK
|
1208 DR7_TYPE3_MASK
| DR7_LEN3_MASK
);
1210 for (Index
= 0; Index
< GspHwBreakpointCount
; Index
++)
1212 switch(GspHwBreakpoints
[Index
].Type
)
1214 case GDB_ZTYPE_HARDWARE_BREAKPOINT
:
1215 i386Type
= I386_BP_TYPE_EXECUTE
;
1217 case GDB_ZTYPE_WRITE_WATCHPOINT
:
1218 i386Type
= I386_BP_TYPE_DATA_WRITE
;
1220 case GDB_ZTYPE_ACCESS_WATCHPOINT
:
1221 i386Type
= I386_BP_TYPE_DATA_READWRITE
;
1225 i386Type
= I386_BP_TYPE_EXECUTE
;
1229 GspLoadHwBreakpoint(TrapFrame
, Index
, GspHwBreakpoints
[Index
].Address
,
1230 GspHwBreakpoints
[Index
].Length
- 1, i386Type
);
1233 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1235 if (GspHwBreakpointCount
+ Index
< MAX_HW_BREAKPOINTS
)
1237 DPRINT("Implementing software interrupt using hardware register\n");
1238 GspLoadHwBreakpoint(TrapFrame
, GspHwBreakpointCount
+ Index
,
1239 GspSwBreakpoints
[Index
].Address
, 0,
1240 I386_BP_TYPE_EXECUTE
);
1241 GspSwBreakpoints
[Index
].Active
= FALSE
;
1245 DPRINT("Using real software breakpoint\n");
1246 GspMemoryError
= FALSE
;
1247 GspSwBreakpoints
[Index
].PrevContent
= GspReadMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
);
1248 if (! GspMemoryError
)
1250 GspWriteMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
, I386_OPCODE_INT3
);
1252 GspSwBreakpoints
[Index
].Active
= ! GspMemoryError
;
1255 DPRINT1("Failed to set software breakpoint at 0x%p\n",
1256 GspSwBreakpoints
[Index
].Address
);
1260 DPRINT("Successfully set software breakpoint at 0x%p\n",
1261 GspSwBreakpoints
[Index
].Address
);
1262 DPRINT1("Successfully set software breakpoint at 0x%p\n", GspSwBreakpoints
[Index
].Address
);
1267 DPRINT("Final DR7 value 0x%08x\n", TrapFrame
->Dr7
);
1271 GspUnloadBreakpoints(PKTRAP_FRAME TrapFrame
)
1275 DPRINT("GspUnloadHwBreakpoints\n");
1277 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1279 if (GspSwBreakpoints
[Index
].Active
)
1281 GspMemoryError
= FALSE
;
1282 GspWriteMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
,
1283 GspSwBreakpoints
[Index
].PrevContent
);
1284 GspSwBreakpoints
[Index
].Active
= FALSE
;
1287 DPRINT1("Failed to remove software breakpoint from 0x%p\n",
1288 GspSwBreakpoints
[Index
].Address
);
1292 DPRINT("Successfully removed software breakpoint from 0x%p\n",
1293 GspSwBreakpoints
[Index
].Address
);
1299 static BOOLEAN gdb_attached_yet
= FALSE
;
1301 * This function does all command procesing for interfacing to gdb.
1305 KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord
,
1307 PKTRAP_FRAME TrapFrame
)
1316 /* FIXME: Stop on other CPUs too */
1318 if (STATUS_ACCESS_VIOLATION
== (NTSTATUS
) ExceptionRecord
->ExceptionCode
&&
1319 NULL
!= GspAccessLocation
&&
1320 (ULONG_PTR
) GspAccessLocation
==
1321 (ULONG_PTR
) ExceptionRecord
->ExceptionInformation
[1])
1323 GspAccessLocation
= NULL
;
1324 GspMemoryError
= TRUE
;
1329 DPRINT("Thread %p entering stub\n", PsGetCurrentThread());
1330 /* Can only debug 1 thread at a time... */
1331 ExAcquireFastMutex(&GspLock
);
1332 DPRINT("Thread %p acquired mutex\n", PsGetCurrentThread());
1334 /* Disable hardware debugging while we are inside the stub */
1335 #if defined(__GNUC__)
1336 __asm__("movl %0,%%db7" : /* no output */ : "r" (0));
1337 #elif defined(_MSC_VER)
1338 __asm mov eax
, 0 __asm mov dr7
, eax
1340 #error Unknown compiler for inline assembler
1343 GspUnloadBreakpoints(TrapFrame
);
1345 /* Make sure we're debugging the current thread. */
1346 if (NULL
!= GspDbgThread
)
1348 DPRINT1("Internal error: entering stub with non-NULL GspDbgThread\n");
1349 ObDereferenceObject(GspDbgThread
);
1350 GspDbgThread
= NULL
;
1353 /* ugly hack to avoid attempting to send status at the very
1354 * beginning, right when GDB is trying to query the stub */
1355 if (gdb_attached_yet
)
1360 /* reply to host that an exception has occurred */
1361 SigVal
= GspComputeSignal(ExceptionRecord
->ExceptionCode
);
1365 *ptr
++ = 'T'; /* notify gdb with signo, PC, FP and SP */
1366 *ptr
++ = HexChars
[(SigVal
>> 4) & 0xf];
1367 *ptr
++ = HexChars
[SigVal
& 0xf];
1369 *ptr
++ = HexChars
[ESP
];
1372 Esp
= GspGetEspFromTrapFrame(TrapFrame
); /* SP */
1373 ptr
= GspMem2Hex((PCHAR
) &Esp
, ptr
, 4, 0);
1376 *ptr
++ = HexChars
[EBP
];
1378 ptr
= GspMem2Hex((PCHAR
) &TrapFrame
->Ebp
, ptr
, 4, 0); /* FP */
1381 *ptr
++ = HexChars
[PC
];
1383 ptr
= GspMem2Hex((PCHAR
) &TrapFrame
->Eip
, ptr
, 4, 0); /* PC */
1388 GspPutPacket(&GspOutBuffer
[0]);
1392 gdb_attached_yet
= 1;
1399 /* Zero the buffer now so we don't have to worry about the terminating zero character */
1400 memset(GspOutBuffer
, 0, sizeof(GspInBuffer
));
1401 ptr
= GspGetPacket();
1406 /* a little hack to send more complete status information */
1408 GspOutBuffer
[0] = 'S';
1409 GspOutBuffer
[1] = HexChars
[SigVal
>> 4];
1410 GspOutBuffer
[2] = HexChars
[SigVal
% 16];
1411 GspOutBuffer
[3] = 0;
1414 GspRemoteDebug
= !GspRemoteDebug
; /* toggle debug flag */
1416 case 'g': /* return the value of the CPU Registers */
1417 GspGetRegisters(GspOutBuffer
, TrapFrame
);
1419 case 'G': /* set the value of the CPU Registers - return OK */
1420 if (NULL
!= GspDbgThread
)
1422 GspSetRegistersInTrapFrame(ptr
, Context
, GspDbgThread
->Tcb
.TrapFrame
);
1426 GspSetRegistersInTrapFrame(ptr
, Context
, TrapFrame
);
1428 strcpy(GspOutBuffer
, "OK");
1430 case 'P': /* set the value of a single CPU register - return OK */
1434 if ((GspHex2Long(&ptr
, &Register
)) && (*ptr
++ == '='))
1436 if ((Register
>= 0) && (Register
< NUMREGS
))
1440 GspSetSingleRegisterInTrapFrame(ptr
, Register
,
1442 GspDbgThread
->Tcb
.TrapFrame
);
1446 GspSetSingleRegisterInTrapFrame(ptr
, Register
,
1447 Context
, TrapFrame
);
1449 strcpy(GspOutBuffer
, "OK");
1454 strcpy(GspOutBuffer
, "E01");
1458 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1460 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
1461 if (GspHex2Long(&ptr
, &Address
) &&
1463 GspHex2Long(&ptr
, &Length
))
1465 PEPROCESS DbgProcess
= NULL
;
1468 if (NULL
!= GspDbgThread
&&
1469 PsGetCurrentProcess() != GspDbgThread
->ThreadsProcess
)
1471 DbgProcess
= GspDbgThread
->ThreadsProcess
;
1472 KeAttachProcess(&DbgProcess
->Pcb
);
1474 GspMemoryError
= FALSE
;
1475 GspMem2Hex((PCHAR
) Address
, GspOutBuffer
, Length
, 1);
1476 if (NULL
!= DbgProcess
)
1482 strcpy(GspOutBuffer
, "E03");
1483 DPRINT("Fault during memory read\n");
1489 strcpy(GspOutBuffer
, "E01");
1493 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
1495 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
1496 if (GspHex2Long(&ptr
, &Address
))
1498 if (*(ptr
++) == ',' &&
1499 GspHex2Long(&ptr
, &Length
) &&
1502 PEPROCESS DbgProcess
= NULL
;
1504 if (NULL
!= GspDbgThread
&&
1505 PsGetCurrentProcess() != GspDbgThread
->ThreadsProcess
)
1507 DbgProcess
= GspDbgThread
->ThreadsProcess
;
1508 KeAttachProcess(&DbgProcess
->Pcb
);
1510 GspMemoryError
= FALSE
;
1511 GspHex2Mem(ptr
, (PCHAR
) Address
, Length
, TRUE
);
1512 if (NULL
!= DbgProcess
)
1518 strcpy(GspOutBuffer
, "E03");
1519 DPRINT("Fault during memory write\n");
1523 strcpy(GspOutBuffer
, "OK");
1531 strcpy(GspOutBuffer
, "E02");
1535 /* cAA..AA Continue at address AA..AA(optional) */
1536 /* sAA..AA Step one instruction from AA..AA(optional) */
1541 ULONG BreakpointNumber
;
1544 /* try to read optional parameter, pc unchanged if no parm */
1545 if (GspHex2Long (&ptr
, &Address
))
1547 Context
->Eip
= Address
;
1550 NewPC
= Context
->Eip
;
1552 /* clear the trace bit */
1553 Context
->EFlags
&= 0xfffffeff;
1555 /* set the trace bit if we're Stepping */
1558 Context
->EFlags
|= 0x100;
1561 #if defined(__GNUC__)
1562 asm volatile ("movl %%db6, %0\n" : "=r" (dr6_
) : );
1563 #elif defined(_MSC_VER)
1564 __asm mov eax
, dr6 __asm mov dr6_
, eax
;
1566 #error Unknown compiler for inline assembler
1568 if (!(dr6_
& 0x4000))
1570 for (BreakpointNumber
= 0; BreakpointNumber
< 4; ++BreakpointNumber
)
1572 if (dr6_
& (1 << BreakpointNumber
))
1574 if (GspHwBreakpoints
[BreakpointNumber
].Type
== 0)
1576 /* Set restore flag */
1577 Context
->EFlags
|= 0x10000;
1583 GspLoadBreakpoints(TrapFrame
);
1584 #if defined(__GNUC__)
1585 asm volatile ("movl %0, %%db6\n" : : "r" (0));
1586 #elif defined(_MSC_VER)
1587 __asm mov eax
, 0 __asm mov dr6
, eax
;
1589 #error Unknown compiler for inline assembler
1592 if (NULL
!= GspDbgThread
)
1594 ObDereferenceObject(GspDbgThread
);
1595 GspDbgThread
= NULL
;
1598 DPRINT("Thread %p releasing mutex\n", PsGetCurrentThread());
1599 ExReleaseFastMutex(&GspLock
);
1600 DPRINT("Thread %p leaving stub\n", PsGetCurrentThread());
1605 case 'k': /* kill the program */
1606 strcpy(GspOutBuffer
, "OK");
1608 /* kill the program */
1610 case 'H': /* Set thread */
1614 case 'q': /* Query */
1618 case 'T': /* Query thread status */
1619 GspQueryThreadStatus(ptr
);
1628 GspHex2Long(&ptr
, &Type
);
1630 GspHex2Long(&ptr
, &Address
);
1632 GspHex2Long(&ptr
, &Length
);
1635 GspSetSwBreakpoint((ULONG_PTR
) Address
);
1639 GspSetHwBreakpoint(Type
, (ULONG_PTR
) Address
, Length
);
1650 GspHex2Long(&ptr
, &Type
);
1652 GspHex2Long(&ptr
, &Address
);
1654 GspHex2Long(&ptr
, &Length
);
1657 GspRemoveSwBreakpoint((ULONG_PTR
) Address
);
1661 GspRemoveHwBreakpoint(Type
, (ULONG_PTR
) Address
, Length
);
1670 /* reply to the request */
1671 GspPutPacket(GspOutBuffer
);
1678 if (NULL
!= GspDbgThread
)
1680 ObDereferenceObject(GspDbgThread
);
1681 GspDbgThread
= NULL
;
1690 GspBreakIn(PKINTERRUPT Interrupt
,
1691 PVOID ServiceContext
)
1693 PKTRAP_FRAME TrapFrame
;
1699 DPRINT("Break In\n");
1702 while (KdPortGetByteEx(&GdbPortInfo
, &Value
))
1715 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1717 TrapFrame
= PsGetCurrentThread()->Tcb
.TrapFrame
;
1719 KeTrapFrameToContext(TrapFrame
, NULL
, &Context
);
1721 KdpGdbEnterDebuggerException(NULL
, &Context
, TrapFrame
);
1723 KeContextToTrapFrame(&Context
, NULL
, TrapFrame
, KernelMode
);
1725 KeLowerIrql(OldIrql
);
1732 KdpGdbDebugPrint(PCH Message
, ULONG Length
)
1736 /* Initialize the GDB stub */
1739 KdpGdbStubInit(PKD_DISPATCH_TABLE WrapperTable
,
1742 if (!KdDebuggerEnabled
|| !KdpDebugMode
.Gdb
)
1749 ExInitializeFastMutex(&GspLock
);
1751 /* Write out the functions that we support for now */
1752 WrapperTable
->KdpInitRoutine
= KdpGdbStubInit
;
1753 WrapperTable
->KdpPrintRoutine
= KdpGdbDebugPrint
;
1754 WrapperTable
->KdpExceptionRoutine
= KdpGdbEnterDebuggerException
;
1756 /* Initialize the Port */
1757 KdPortInitializeEx(&GdbPortInfo
, 0, 0);
1759 else if (BootPhase
== 1)
1761 GspInitialized
= TRUE
;
1763 GspRunThread
= NULL
;
1764 GspDbgThread
= NULL
;
1765 GspEnumThread
= NULL
;
1767 HalDisplayString("Waiting for GDB to attach\n");
1768 DbgPrint("Module 'hal.dll' loaded at 0x%.08x.\n", LdrHalBase
);
1769 DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C
);
1771 else if (BootPhase
== 2)
1773 HalDisplayString("\n GDB debugging enabled\n\n");