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>
90 /************************************************************************/
91 /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
92 /* at least NUMREGBYTES*2 are needed for register packets */
95 static BOOLEAN GspInitialized
;
97 static BOOLEAN GspRemoteDebug
;
99 static CONST CHAR HexChars
[]="0123456789abcdef";
101 static PETHREAD GspRunThread
; /* NULL means run all threads */
102 static PETHREAD GspDbgThread
;
103 static PETHREAD GspEnumThread
;
105 static FAST_MUTEX GspLock
;
107 extern LIST_ENTRY PsActiveProcessHead
;
108 KD_PORT_INFORMATION GdbPortInfo
= { 2, 115200, 0 }; /* FIXME hardcoded for COM2, 115200 baud */
110 /* Number of Registers. */
115 EAX
, ECX
, EDX
, EBX
, ESP
, EBP
, ESI
, EDI
,
116 PC
/* also known as eip */,
117 PS
/* also known as eflags */,
118 CS
, SS
, DS
, ES
, FS
, GS
121 typedef struct _CPU_REGISTER
125 ULONG OffsetInContext
;
126 BOOLEAN SetInContext
;
127 } CPU_REGISTER
, *PCPU_REGISTER
;
129 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
, HardwareEsp
), FIELD_OFFSET(CONTEXT
, Esp
), TRUE
},
137 { 4, FIELD_OFFSET(KTRAP_FRAME
, DbgEbp
), 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
, DbgEip
), FIELD_OFFSET(CONTEXT
, Eip
), TRUE
},
141 { 4, FIELD_OFFSET(KTRAP_FRAME
, EFlags
), FIELD_OFFSET(CONTEXT
, EFlags
), TRUE
},
142 { 4, FIELD_OFFSET(KTRAP_FRAME
, SegCs
), FIELD_OFFSET(CONTEXT
, SegCs
), TRUE
},
143 { 4, FIELD_OFFSET(KTRAP_FRAME
, HardwareSegSs
), FIELD_OFFSET(CONTEXT
, SegSs
), TRUE
},
144 { 4, FIELD_OFFSET(KTRAP_FRAME
, SegDs
), FIELD_OFFSET(CONTEXT
, SegDs
), TRUE
},
145 { 4, FIELD_OFFSET(KTRAP_FRAME
, SegEs
), FIELD_OFFSET(CONTEXT
, SegEs
), TRUE
},
146 { 4, FIELD_OFFSET(KTRAP_FRAME
, SegFs
), FIELD_OFFSET(CONTEXT
, SegFs
), TRUE
},
147 { 4, FIELD_OFFSET(KTRAP_FRAME
, SegGs
), FIELD_OFFSET(CONTEXT
, SegGs
), TRUE
}
151 static PCHAR GspThreadStates
[DeferredReady
+1] =
167 if ((ch
>= '0') && (ch
<= '9'))
171 if ((ch
>= 'a') && (ch
<= 'f'))
173 return (ch
- 'a' + 10);
175 if ((ch
>= 'A') && (ch
<= 'F'))
177 return (ch
- 'A' + 10);
183 static CHAR GspInBuffer
[BUFMAX
];
184 static CHAR GspOutBuffer
[BUFMAX
];
187 GdbPutChar(UCHAR Value
)
189 KdPortPutByteEx(&GdbPortInfo
, Value
);
197 while (!KdPortGetByteEx(&GdbPortInfo
, &Value
))
203 /* scan for the sequence $<data>#<Checksum> */
208 PCHAR Buffer
= &GspInBuffer
[0];
216 /* wait around for the start character, ignore all other characters */
217 while ((ch
= GdbGetChar ()) != '$')
225 /* now, read until a # or end of Buffer is found */
226 while (Count
< BUFMAX
)
237 Checksum
= Checksum
+ ch
;
246 XmitChecksum
= (CHAR
)(HexValue(ch
) << 4);
248 XmitChecksum
+= (CHAR
)(HexValue(ch
));
250 if (Checksum
!= XmitChecksum
)
252 GdbPutChar('-'); /* failed checksum */
256 GdbPutChar('+'); /* successful transfer */
264 /* send the packet in Buffer. */
267 GspPutPacket(PCHAR Buffer
)
273 /* $<packet info>#<Checksum>. */
280 while ((ch
= Buffer
[Count
]))
288 GdbPutChar(HexChars
[(Checksum
>> 4) & 0xf]);
289 GdbPutChar(HexChars
[Checksum
& 0xf]);
291 while (GdbGetChar() != '+');
296 GspPutPacketNoWait(PCHAR Buffer
)
302 /* $<packet info>#<Checksum>. */
307 while ((ch
= Buffer
[Count
]))
315 GdbPutChar(HexChars
[(Checksum
>> 4) & 0xf]);
316 GdbPutChar(HexChars
[Checksum
& 0xf]);
319 /* Indicate to caller of GspMem2Hex or GspHex2Mem that there has been an
321 static volatile BOOLEAN GspMemoryError
= FALSE
;
322 static volatile void *GspAccessLocation
= NULL
;
325 GspReadMemSafe(PCHAR Address
)
331 GspMemoryError
= TRUE
;
335 GspAccessLocation
= Address
;
337 GspAccessLocation
= NULL
;
342 /* Convert the memory pointed to by Address into hex, placing result in Buffer */
343 /* Return a pointer to the last char put in Buffer (null) */
344 /* If MayFault is TRUE, then we should set GspMemoryError in response to
345 a fault; if FALSE treat a fault like any other fault in the stub. */
347 GspMem2Hex(PCHAR Address
,
355 for (i
= 0; i
< (ULONG
) Count
; i
++)
359 ch
= GspReadMemSafe(Address
);
369 *Buffer
++ = HexChars
[(ch
>> 4) & 0xf];
370 *Buffer
++ = HexChars
[ch
& 0xf];
379 GspWriteMem(PCHAR Address
,
382 CHAR (*GetContent
)(PVOID Context
, ULONG Offset
),
393 while (Current
< Address
+ Count
)
395 Page
= (PCHAR
)PAGE_ROUND_DOWN(Current
);
396 if (Address
+ Count
<= Page
+ PAGE_SIZE
)
398 /* Fits in this page */
403 /* Flows into next page, handle only current page in this iteration */
404 CountInPage
= PAGE_SIZE
- (Address
- Page
);
408 OldProt
= MmGetPageProtect(NULL
, Address
);
409 MmSetPageProtect(NULL
, Address
, PAGE_EXECUTE_READWRITE
);
412 for (i
= 0; i
< CountInPage
&& ! GspMemoryError
; i
++)
414 ch
= (*GetContent
)(Context
, Current
- Address
);
418 GspAccessLocation
= Current
;
423 GspAccessLocation
= NULL
;
429 MmSetPageProtect(NULL
, Page
, OldProt
);
432 return Current
- Address
;
437 return Current
- Address
;
441 GspHex2MemGetContent(PVOID Context
, ULONG Offset
)
443 return (CHAR
)((HexValue(*((PCHAR
) Context
+ 2 * Offset
)) << 4) +
444 HexValue(*((PCHAR
) Context
+ 2 * Offset
+ 1)));
447 /* Convert the hex array pointed to by Buffer into binary to be placed at Address */
448 /* Return a pointer to the character AFTER the last byte read from Buffer */
450 GspHex2Mem(PCHAR Buffer
,
455 Count
= GspWriteMem(Address
, Count
, MayFault
, GspHex2MemGetContent
, Buffer
);
457 return Buffer
+ 2 * Count
;
461 GspWriteMemSafeGetContent(PVOID Context
, ULONG Offset
)
465 return *((PCHAR
) Context
);
469 GspWriteMemSafe(PCHAR Address
,
472 GspWriteMem(Address
, 1, TRUE
, GspWriteMemSafeGetContent
, &Ch
);
476 /* This function takes the 386 exception vector and attempts to
477 translate this number into a unix compatible signal value */
479 GspComputeSignal(NTSTATUS ExceptionCode
)
483 switch (ExceptionCode
)
485 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
486 SigVal
= 8; /* divide by zero */
488 case STATUS_SINGLE_STEP
:
489 case STATUS_BREAKPOINT
:
490 SigVal
= 5; /* breakpoint */
492 case STATUS_INTEGER_OVERFLOW
:
493 case STATUS_ARRAY_BOUNDS_EXCEEDED
:
494 SigVal
= 16; /* bound instruction */
496 case STATUS_ILLEGAL_INSTRUCTION
:
497 SigVal
= 4; /* Invalid opcode */
499 case STATUS_STACK_OVERFLOW
:
500 case STATUS_DATATYPE_MISALIGNMENT
:
501 case STATUS_ACCESS_VIOLATION
:
502 SigVal
= 11; /* access violation */
505 SigVal
= 7; /* "software generated" */
511 /**********************************************/
512 /* WHILE WE FIND NICE HEX CHARS, BUILD A LONG */
513 /* RETURN NUMBER OF CHARS PROCESSED */
514 /**********************************************/
516 GspHex2Long(PCHAR
*Address
,
526 Hex
= HexValue(**Address
);
529 *Value
= (*Value
<< 4) | Hex
;
545 GspLong2Hex(PCHAR
*Address
,
550 Save
= (((Value
>> 0) & 0xff) << 24) |
551 (((Value
>> 8) & 0xff) << 16) |
552 (((Value
>> 16) & 0xff) << 8) |
553 (((Value
>> 24) & 0xff) << 0);
554 *Address
= GspMem2Hex((PCHAR
) &Save
, *Address
, 4, FALSE
);
559 * When coming from kernel mode, Esp is not stored in the trap frame.
560 * Instead, it was pointing to the location of the TrapFrame Esp member
561 * when the exception occured. When coming from user mode, Esp is just
562 * stored in the TrapFrame Esp member.
565 GspGetEspFromTrapFrame(PKTRAP_FRAME TrapFrame
)
568 return KeGetPreviousMode() == KernelMode
569 ? (LONG
) &TrapFrame
->HardwareEsp
: (LONG
)TrapFrame
->HardwareEsp
;
570 #elif defined(_M_PPC)
577 GspGetRegisters(PCHAR Address
,
578 PKTRAP_FRAME TrapFrame
)
584 ULONG_PTR
*KernelStack
;
586 if (NULL
== GspDbgThread
)
588 Thread
= PsGetCurrentThread();
592 TrapFrame
= GspDbgThread
->Tcb
.TrapFrame
;
593 Thread
= GspDbgThread
;
596 if (Waiting
== Thread
->Tcb
.State
)
598 KernelStack
= Thread
->Tcb
.KernelStack
;
599 for (i
= 0; i
< sizeof(GspRegisters
) / sizeof(GspRegisters
[0]); i
++)
604 Value
= KernelStack
[3];
607 Value
= KernelStack
[4];
610 Value
= KernelStack
[5];
613 Value
= KernelStack
[6];
616 Value
= KernelStack
[7];
619 Value
= (ULONG_PTR
) (KernelStack
+ 8);
623 Value
= KGDT_R0_CODE
;
626 Value
= KGDT_R0_DATA
;
633 Address
= GspMem2Hex((PCHAR
) &Value
, Address
, GspRegisters
[i
].Size
,
639 for (i
= 0; i
< sizeof(GspRegisters
) / sizeof(GspRegisters
[0]); i
++)
645 Value
= GspGetEspFromTrapFrame(TrapFrame
);
649 p
= (PULONG
)((ULONG_PTR
) TrapFrame
+
650 GspRegisters
[i
].OffsetInTF
);
657 * This thread has not been sheduled yet so assume it
658 * is still in PsBeginThreadWithContextInternal().
660 Value
= (ULONG
)KiThreadStartup
;
666 Address
= GspMem2Hex((PCHAR
) &Value
, Address
,
667 GspRegisters
[i
].Size
, FALSE
);
674 GspSetRegistersInTrapFrame(PCHAR Address
,
676 PKTRAP_FRAME TrapFrame
)
689 for (i
= 0; i
< NUMREGS
; i
++)
691 if (GspRegisters
[i
].SetInContext
)
693 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[i
].OffsetInContext
);
697 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[i
].OffsetInTF
);
700 Buffer
= GspHex2Mem(Buffer
, (PCHAR
) &Value
, GspRegisters
[i
].Size
, FALSE
);
707 GspSetSingleRegisterInTrapFrame(PCHAR Address
,
710 PKTRAP_FRAME TrapFrame
)
720 if (GspRegisters
[Number
].SetInContext
)
722 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[Number
].OffsetInContext
);
726 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[Number
].OffsetInTF
);
729 GspHex2Mem(Address
, (PCHAR
) &Value
, GspRegisters
[Number
].Size
, FALSE
);
735 GspFindThread(PCHAR Data
,
738 PETHREAD ThreadInfo
= NULL
;
740 if (strcmp (Data
, "-1") == 0)
749 PCHAR ptr
= &Data
[0];
751 GspHex2Long(&ptr
, (PLONG
) &uThreadId
);
752 ThreadId
= (HANDLE
)uThreadId
;
754 if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId
, &ThreadInfo
)))
760 *Thread
= ThreadInfo
;
766 GspSetThread(PCHAR Request
)
769 PCHAR ptr
= &Request
[1];
773 case 'c': /* Run thread */
774 if (GspFindThread(ptr
, &ThreadInfo
))
776 GspOutBuffer
[0] = 'O';
777 GspOutBuffer
[1] = 'K';
779 if (NULL
!= GspRunThread
)
781 ObDereferenceObject(GspRunThread
);
783 GspRunThread
= ThreadInfo
;
784 if (NULL
!= GspRunThread
)
786 ObReferenceObject(GspRunThread
);
791 GspOutBuffer
[0] = 'E';
794 case 'g': /* Debug thread */
795 if (GspFindThread(ptr
, &ThreadInfo
))
797 GspOutBuffer
[0] = 'O';
798 GspOutBuffer
[1] = 'K';
800 if (NULL
!= GspDbgThread
)
802 ObDereferenceObject(GspDbgThread
);
805 if (ThreadInfo
== PsGetCurrentThread())
808 ObDereferenceObject(ThreadInfo
);
812 GspDbgThread
= ThreadInfo
;
817 GspOutBuffer
[0] = 'E';
827 GspQuery(PCHAR Request
)
831 if (strncmp(Request
, "C", 1) == 0)
833 PCHAR ptr
= &GspOutBuffer
[2];
835 /* Get current thread id */
836 GspOutBuffer
[0] = 'Q';
837 GspOutBuffer
[1] = 'C';
838 if (NULL
!= GspDbgThread
)
840 Value
= (ULONG
) GspDbgThread
->Cid
.UniqueThread
;
844 Value
= (ULONG
) PsGetCurrentThread()->Cid
.UniqueThread
;
846 GspLong2Hex(&ptr
, Value
);
848 else if (strncmp(Request
, "fThreadInfo", 11) == 0)
851 PLIST_ENTRY AThread
, AProcess
;
852 PCHAR ptr
= &GspOutBuffer
[1];
854 /* Get first thread id */
855 GspEnumThread
= NULL
;
856 AProcess
= PsActiveProcessHead
.Flink
;
857 while(AProcess
!= &PsActiveProcessHead
)
859 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
860 AThread
= Process
->ThreadListHead
.Flink
;
861 if (AThread
!= &Process
->ThreadListHead
)
863 GspEnumThread
= CONTAINING_RECORD(Process
->ThreadListHead
.Flink
,
864 ETHREAD
, ThreadListEntry
);
867 AProcess
= AProcess
->Flink
;
869 if(GspEnumThread
!= NULL
)
871 GspOutBuffer
[0] = 'm';
872 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
873 GspLong2Hex(&ptr
, Value
);
877 /* FIXME - what to do here? This case should never happen though, there
878 should always be at least one thread on the system... */
879 /* GspOutBuffer[0] = 'l'; */
882 else if (strncmp(Request
, "sThreadInfo", 11) == 0)
885 PLIST_ENTRY AThread
, AProcess
;
886 PCHAR ptr
= &GspOutBuffer
[1];
888 /* Get next thread id */
889 if (GspEnumThread
!= NULL
)
891 /* find the next thread */
892 Process
= GspEnumThread
->ThreadsProcess
;
893 if(GspEnumThread
->ThreadListEntry
.Flink
!= &Process
->ThreadListHead
)
895 GspEnumThread
= CONTAINING_RECORD(GspEnumThread
->ThreadListEntry
.Flink
,
896 ETHREAD
, ThreadListEntry
);
900 PETHREAD Thread
= NULL
;
901 AProcess
= Process
->ActiveProcessLinks
.Flink
;
902 while(AProcess
!= &PsActiveProcessHead
)
904 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
905 AThread
= Process
->ThreadListHead
.Flink
;
906 if (AThread
!= &Process
->ThreadListHead
)
908 Thread
= CONTAINING_RECORD(Process
->ThreadListHead
.Flink
,
909 ETHREAD
, ThreadListEntry
);
912 AProcess
= AProcess
->Flink
;
914 GspEnumThread
= Thread
;
917 if (GspEnumThread
!= NULL
)
920 GspOutBuffer
[0] = 'm';
921 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
922 GspLong2Hex(&ptr
, Value
);
926 GspOutBuffer
[0] = 'l';
931 GspOutBuffer
[0] = 'l';
934 else if (strncmp(Request
, "ThreadExtraInfo", 15) == 0)
938 /* Get thread information */
939 if (GspFindThread(Request
+ 16, &ThreadInfo
))
944 Proc
= (PEPROCESS
) ThreadInfo
->ThreadsProcess
;
949 sprintf(Buffer
, "%s [%d:0x%x], ", Proc
->ImageFileName
,
950 (int) Proc
->UniqueProcessId
,
951 (int) ThreadInfo
->Cid
.UniqueThread
);
953 strcpy(Buffer
+ strlen(Buffer
),
954 GspThreadStates
[ThreadInfo
->Tcb
.State
]);
956 ObDereferenceObject(ThreadInfo
);
958 GspMem2Hex(Buffer
, &GspOutBuffer
[0], strlen(Buffer
), FALSE
);
964 GspQueryThreadStatus(PCHAR Request
)
967 PCHAR ptr
= &Request
[0];
969 if (GspFindThread(ptr
, &ThreadInfo
))
971 ObDereferenceObject(ThreadInfo
);
973 GspOutBuffer
[0] = 'O';
974 GspOutBuffer
[1] = 'K';
975 GspOutBuffer
[2] = '\0';
979 GspOutBuffer
[0] = 'E';
980 GspOutBuffer
[1] = '\0';
984 #define DR7_L0 0x00000001 /* Local breakpoint 0 enable */
985 #define DR7_G0 0x00000002 /* Global breakpoint 0 enable */
986 #define DR7_L1 0x00000004 /* Local breakpoint 1 enable */
987 #define DR7_G1 0x00000008 /* Global breakpoint 1 enable */
988 #define DR7_L2 0x00000010 /* Local breakpoint 2 enable */
989 #define DR7_G2 0x00000020 /* Global breakpoint 2 enable */
990 #define DR7_L3 0x00000040 /* Local breakpoint 3 enable */
991 #define DR7_G3 0x00000080 /* Global breakpoint 3 enable */
992 #define DR7_LE 0x00000100 /* Local exact breakpoint enable (old) */
993 #define DR7_GE 0x00000200 /* Global exact breakpoint enable (old) */
994 #define DR7_GD 0x00002000 /* General detect enable */
995 #define DR7_TYPE0_MASK 0x00030000 /* Breakpoint 0 condition */
996 #define DR7_LEN0_MASK 0x000c0000 /* Breakpoint 0 length */
997 #define DR7_TYPE1_MASK 0x00300000 /* Breakpoint 1 condition */
998 #define DR7_LEN1_MASK 0x00c00000 /* Breakpoint 1 length */
999 #define DR7_TYPE2_MASK 0x03000000 /* Breakpoint 2 condition */
1000 #define DR7_LEN2_MASK 0x0c000000 /* Breakpoint 2 length */
1001 #define DR7_TYPE3_MASK 0x30000000 /* Breakpoint 3 condition */
1002 #define DR7_LEN3_MASK 0xc0000000 /* Breakpoint 3 length */
1003 #define DR7_GLOBAL_ENABLE(Bp) (2 << (2 * (Bp)))
1004 #define DR7_TYPE(Bp, Type) ((Type) << (16 + 4 * (Bp)))
1005 #define DR7_LEN(Bp, Len) ((Len) << (18 + 4 * (Bp)))
1007 #define I386_BP_TYPE_EXECUTE 0
1008 #define I386_BP_TYPE_DATA_WRITE 1
1009 #define I386_BP_TYPE_DATA_READWRITE 3
1011 #define I386_OPCODE_INT3 0xcc
1013 #define GDB_ZTYPE_MEMORY_BREAKPOINT 0
1014 #define GDB_ZTYPE_HARDWARE_BREAKPOINT 1
1015 #define GDB_ZTYPE_WRITE_WATCHPOINT 2
1016 #define GDB_ZTYPE_READ_WATCHPOINT 3
1017 #define GDB_ZTYPE_ACCESS_WATCHPOINT 4
1019 typedef struct _GSPHWBREAKPOINT
1026 #define MAX_HW_BREAKPOINTS 4
1027 static unsigned GspHwBreakpointCount
= 0;
1028 static GSPHWBREAKPOINT GspHwBreakpoints
[MAX_HW_BREAKPOINTS
];
1030 typedef struct _GSPSWBREAKPOINT
1037 #define MAX_SW_BREAKPOINTS 64
1038 static unsigned GspSwBreakpointCount
= 0;
1039 static GSPSWBREAKPOINT GspSwBreakpoints
[MAX_SW_BREAKPOINTS
];
1042 GspSetHwBreakpoint(ULONG Type
, ULONG_PTR Address
, ULONG Length
)
1044 DPRINT("GspSetHwBreakpoint(%lu, 0x%p, %lu)\n", Type
, Address
, Length
);
1046 if (GDB_ZTYPE_READ_WATCHPOINT
== Type
)
1048 DPRINT1("Read watchpoint not supported\n");
1049 strcpy(GspOutBuffer
, "E22");
1051 else if (GDB_ZTYPE_HARDWARE_BREAKPOINT
== Type
&& 1 != Length
)
1053 DPRINT1("Invalid length %lu for hardware breakpoint\n", Length
);
1054 strcpy(GspOutBuffer
, "E22");
1056 else if (1 != Length
&& 2 != Length
&& 4 != Length
)
1058 DPRINT1("Invalid length %lu for GDB Z type %lu\n", Length
, Type
);
1059 strcpy(GspOutBuffer
, "E22");
1061 else if (0 != (Address
& (Length
- 1)))
1063 DPRINT1("Invalid alignment for address 0x%p and length %d\n",
1065 strcpy(GspOutBuffer
, "E22");
1067 else if (MAX_HW_BREAKPOINTS
== GspHwBreakpointCount
)
1069 DPRINT1("Trying to set too many hardware breakpoints\n");
1070 strcpy(GspOutBuffer
, "E22");
1074 DPRINT("Stored at index %u\n", GspHwBreakpointCount
);
1075 GspHwBreakpoints
[GspHwBreakpointCount
].Type
= Type
;
1076 GspHwBreakpoints
[GspHwBreakpointCount
].Address
= Address
;
1077 GspHwBreakpoints
[GspHwBreakpointCount
].Length
= Length
;
1078 GspHwBreakpointCount
++;
1079 strcpy(GspOutBuffer
, "OK");
1084 GspRemoveHwBreakpoint(ULONG Type
, ULONG_PTR Address
, ULONG Length
)
1088 DPRINT("GspRemoveHwBreakpoint(%lu, 0x%p, %lu)\n", Type
, Address
, Length
);
1089 for (Index
= 0; Index
< GspHwBreakpointCount
; Index
++)
1091 if (GspHwBreakpoints
[Index
].Type
== Type
&&
1092 GspHwBreakpoints
[Index
].Address
== Address
&&
1093 GspHwBreakpoints
[Index
].Length
== Length
)
1095 DPRINT("Found match at index %u\n", Index
);
1096 if (Index
+ 1 < GspHwBreakpointCount
)
1098 memmove(GspHwBreakpoints
+ Index
,
1099 GspHwBreakpoints
+ (Index
+ 1),
1100 (GspHwBreakpointCount
- Index
- 1) *
1101 sizeof(GSPHWBREAKPOINT
));
1103 GspHwBreakpointCount
--;
1104 strcpy(GspOutBuffer
, "OK");
1109 DPRINT1("Not found\n");
1110 strcpy(GspOutBuffer
, "E22");
1114 GspSetSwBreakpoint(ULONG_PTR Address
)
1116 DPRINT("GspSetSwBreakpoint(0x%p)\n", Address
);
1118 if (MAX_SW_BREAKPOINTS
== GspSwBreakpointCount
)
1120 DPRINT1("Trying to set too many software breakpoints\n");
1121 strcpy(GspOutBuffer
, "E22");
1125 DPRINT("Stored at index %u\n", GspSwBreakpointCount
);
1126 GspSwBreakpoints
[GspSwBreakpointCount
].Address
= Address
;
1127 GspSwBreakpoints
[GspSwBreakpointCount
].Active
= FALSE
;
1128 GspSwBreakpointCount
++;
1129 strcpy(GspOutBuffer
, "OK");
1134 GspRemoveSwBreakpoint(ULONG_PTR Address
)
1138 DPRINT("GspRemoveSwBreakpoint(0x%p)\n", Address
);
1139 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1141 if (GspSwBreakpoints
[Index
].Address
== Address
)
1143 DPRINT("Found match at index %u\n", Index
);
1144 ASSERT(! GspSwBreakpoints
[Index
].Active
);
1145 if (Index
+ 1 < GspSwBreakpointCount
)
1147 memmove(GspSwBreakpoints
+ Index
,
1148 GspSwBreakpoints
+ (Index
+ 1),
1149 (GspSwBreakpointCount
- Index
- 1) *
1150 sizeof(GSPSWBREAKPOINT
));
1152 GspSwBreakpointCount
--;
1153 strcpy(GspOutBuffer
, "OK");
1158 DPRINT1("Not found\n");
1159 strcpy(GspOutBuffer
, "E22");
1163 GspLoadHwBreakpoint(PKTRAP_FRAME TrapFrame
,
1169 DPRINT("GspLoadHwBreakpoint(0x%p, %d, 0x%p, %d)\n", TrapFrame
, BpIndex
,
1172 /* Set the DR7_Gx bit to globally enable the breakpoint */
1173 TrapFrame
->Dr7
|= DR7_GLOBAL_ENABLE(BpIndex
) |
1174 DR7_LEN(BpIndex
, Length
) |
1175 DR7_TYPE(BpIndex
, Type
);
1180 DPRINT("Setting DR0 to 0x%p\n", Address
);
1181 TrapFrame
->Dr0
= Address
;
1185 DPRINT("Setting DR1 to 0x%p\n", Address
);
1186 TrapFrame
->Dr1
= Address
;
1190 DPRINT("Setting DR2 to 0x%p\n", Address
);
1191 TrapFrame
->Dr2
= Address
;
1195 DPRINT("Setting DR3 to 0x%p\n", Address
);
1196 TrapFrame
->Dr3
= Address
;
1202 GspLoadBreakpoints(PKTRAP_FRAME TrapFrame
)
1207 DPRINT("GspLoadBreakpoints\n");
1208 DPRINT("DR7 on entry: 0x%08x\n", TrapFrame
->Dr7
);
1209 /* Remove all breakpoints */
1210 TrapFrame
->Dr7
&= ~(DR7_L0
| DR7_L1
| DR7_L2
| DR7_L3
|
1211 DR7_G0
| DR7_G1
| DR7_G2
| DR7_G3
|
1212 DR7_TYPE0_MASK
| DR7_LEN0_MASK
|
1213 DR7_TYPE1_MASK
| DR7_LEN1_MASK
|
1214 DR7_TYPE2_MASK
| DR7_LEN2_MASK
|
1215 DR7_TYPE3_MASK
| DR7_LEN3_MASK
);
1217 for (Index
= 0; Index
< GspHwBreakpointCount
; Index
++)
1219 switch(GspHwBreakpoints
[Index
].Type
)
1221 case GDB_ZTYPE_HARDWARE_BREAKPOINT
:
1222 i386Type
= I386_BP_TYPE_EXECUTE
;
1224 case GDB_ZTYPE_WRITE_WATCHPOINT
:
1225 i386Type
= I386_BP_TYPE_DATA_WRITE
;
1227 case GDB_ZTYPE_ACCESS_WATCHPOINT
:
1228 i386Type
= I386_BP_TYPE_DATA_READWRITE
;
1232 i386Type
= I386_BP_TYPE_EXECUTE
;
1236 GspLoadHwBreakpoint(TrapFrame
, Index
, GspHwBreakpoints
[Index
].Address
,
1237 GspHwBreakpoints
[Index
].Length
- 1, i386Type
);
1240 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1242 if (GspHwBreakpointCount
+ Index
< MAX_HW_BREAKPOINTS
)
1244 DPRINT("Implementing software interrupt using hardware register\n");
1245 GspLoadHwBreakpoint(TrapFrame
, GspHwBreakpointCount
+ Index
,
1246 GspSwBreakpoints
[Index
].Address
, 0,
1247 I386_BP_TYPE_EXECUTE
);
1248 GspSwBreakpoints
[Index
].Active
= FALSE
;
1252 DPRINT("Using real software breakpoint\n");
1253 GspMemoryError
= FALSE
;
1254 GspSwBreakpoints
[Index
].PrevContent
= GspReadMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
);
1255 if (! GspMemoryError
)
1257 GspWriteMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
, I386_OPCODE_INT3
);
1259 GspSwBreakpoints
[Index
].Active
= ! GspMemoryError
;
1262 DPRINT1("Failed to set software breakpoint at 0x%p\n",
1263 GspSwBreakpoints
[Index
].Address
);
1267 DPRINT("Successfully set software breakpoint at 0x%p\n",
1268 GspSwBreakpoints
[Index
].Address
);
1269 DPRINT1("Successfully set software breakpoint at 0x%p\n", GspSwBreakpoints
[Index
].Address
);
1274 DPRINT("Final DR7 value 0x%08x\n", TrapFrame
->Dr7
);
1278 GspUnloadBreakpoints(PKTRAP_FRAME TrapFrame
)
1282 DPRINT("GspUnloadHwBreakpoints\n");
1284 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1286 if (GspSwBreakpoints
[Index
].Active
)
1288 GspMemoryError
= FALSE
;
1289 GspWriteMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
,
1290 GspSwBreakpoints
[Index
].PrevContent
);
1291 GspSwBreakpoints
[Index
].Active
= FALSE
;
1294 DPRINT1("Failed to remove software breakpoint from 0x%p\n",
1295 GspSwBreakpoints
[Index
].Address
);
1299 DPRINT("Successfully removed software breakpoint from 0x%p\n",
1300 GspSwBreakpoints
[Index
].Address
);
1306 static BOOLEAN gdb_attached_yet
= FALSE
;
1308 * This function does all command procesing for interfacing to gdb.
1312 KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord
,
1314 PKTRAP_FRAME TrapFrame
)
1323 /* FIXME: Stop on other CPUs too */
1325 if (STATUS_ACCESS_VIOLATION
== (NTSTATUS
) ExceptionRecord
->ExceptionCode
&&
1326 NULL
!= GspAccessLocation
&&
1327 (ULONG_PTR
) GspAccessLocation
==
1328 (ULONG_PTR
) ExceptionRecord
->ExceptionInformation
[1])
1330 GspAccessLocation
= NULL
;
1331 GspMemoryError
= TRUE
;
1336 DPRINT("Thread %p entering stub\n", PsGetCurrentThread());
1337 /* Can only debug 1 thread at a time... */
1338 ExAcquireFastMutex(&GspLock
);
1339 DPRINT("Thread %p acquired mutex\n", PsGetCurrentThread());
1341 /* Disable hardware debugging while we are inside the stub */
1343 #if defined(__GNUC__)
1344 __asm__("movl %0,%%db7" : /* no output */ : "r" (0));
1345 #elif defined(_MSC_VER)
1346 __asm mov eax
, 0 __asm mov dr7
, eax
1348 #error Unknown compiler for inline assembler
1350 #elif defined(_M_PPC)
1351 /* XXX arty fixme */
1354 GspUnloadBreakpoints(TrapFrame
);
1356 /* Make sure we're debugging the current thread. */
1357 if (NULL
!= GspDbgThread
)
1359 DPRINT1("Internal error: entering stub with non-NULL GspDbgThread\n");
1360 ObDereferenceObject(GspDbgThread
);
1361 GspDbgThread
= NULL
;
1364 /* ugly hack to avoid attempting to send status at the very
1365 * beginning, right when GDB is trying to query the stub */
1366 if (gdb_attached_yet
)
1371 /* reply to host that an exception has occurred */
1372 SigVal
= GspComputeSignal(ExceptionRecord
->ExceptionCode
);
1376 *ptr
++ = 'T'; /* notify gdb with signo, PC, FP and SP */
1377 *ptr
++ = HexChars
[(SigVal
>> 4) & 0xf];
1378 *ptr
++ = HexChars
[SigVal
& 0xf];
1380 *ptr
++ = HexChars
[ESP
];
1383 Esp
= GspGetEspFromTrapFrame(TrapFrame
); /* SP */
1384 ptr
= GspMem2Hex((PCHAR
) &Esp
, ptr
, 4, 0);
1388 *ptr
++ = HexChars
[EBP
];
1390 ptr
= GspMem2Hex((PCHAR
) &TrapFrame
->Ebp
, ptr
, 4, 0); /* FP */
1393 *ptr
++ = HexChars
[PC
];
1395 ptr
= GspMem2Hex((PCHAR
) &TrapFrame
->Eip
, ptr
, 4, 0); /* PC */
1401 GspPutPacket(&GspOutBuffer
[0]);
1405 gdb_attached_yet
= 1;
1412 /* Zero the buffer now so we don't have to worry about the terminating zero character */
1413 memset(GspOutBuffer
, 0, sizeof(GspInBuffer
));
1414 ptr
= GspGetPacket();
1419 /* a little hack to send more complete status information */
1421 GspOutBuffer
[0] = 'S';
1422 GspOutBuffer
[1] = HexChars
[SigVal
>> 4];
1423 GspOutBuffer
[2] = HexChars
[SigVal
% 16];
1424 GspOutBuffer
[3] = 0;
1427 GspRemoteDebug
= !GspRemoteDebug
; /* toggle debug flag */
1429 case 'g': /* return the value of the CPU Registers */
1430 GspGetRegisters(GspOutBuffer
, TrapFrame
);
1432 case 'G': /* set the value of the CPU Registers - return OK */
1433 if (NULL
!= GspDbgThread
)
1435 GspSetRegistersInTrapFrame(ptr
, Context
, GspDbgThread
->Tcb
.TrapFrame
);
1439 GspSetRegistersInTrapFrame(ptr
, Context
, TrapFrame
);
1441 strcpy(GspOutBuffer
, "OK");
1443 case 'P': /* set the value of a single CPU register - return OK */
1447 if ((GspHex2Long(&ptr
, &Register
)) && (*ptr
++ == '='))
1449 if ((Register
>= 0) && (Register
< NUMREGS
))
1453 GspSetSingleRegisterInTrapFrame(ptr
, Register
,
1455 GspDbgThread
->Tcb
.TrapFrame
);
1459 GspSetSingleRegisterInTrapFrame(ptr
, Register
,
1460 Context
, TrapFrame
);
1462 strcpy(GspOutBuffer
, "OK");
1467 strcpy(GspOutBuffer
, "E01");
1471 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1473 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
1474 if (GspHex2Long(&ptr
, &Address
) &&
1476 GspHex2Long(&ptr
, &Length
))
1478 PEPROCESS DbgProcess
= NULL
;
1481 if (NULL
!= GspDbgThread
&&
1482 PsGetCurrentProcess() != GspDbgThread
->ThreadsProcess
)
1484 DbgProcess
= GspDbgThread
->ThreadsProcess
;
1485 KeAttachProcess(&DbgProcess
->Pcb
);
1487 GspMemoryError
= FALSE
;
1488 GspMem2Hex((PCHAR
) Address
, GspOutBuffer
, Length
, 1);
1489 if (NULL
!= DbgProcess
)
1495 strcpy(GspOutBuffer
, "E03");
1496 DPRINT("Fault during memory read\n");
1502 strcpy(GspOutBuffer
, "E01");
1506 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
1508 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
1509 if (GspHex2Long(&ptr
, &Address
))
1511 if (*(ptr
++) == ',' &&
1512 GspHex2Long(&ptr
, &Length
) &&
1515 PEPROCESS DbgProcess
= NULL
;
1517 if (NULL
!= GspDbgThread
&&
1518 PsGetCurrentProcess() != GspDbgThread
->ThreadsProcess
)
1520 DbgProcess
= GspDbgThread
->ThreadsProcess
;
1521 KeAttachProcess(&DbgProcess
->Pcb
);
1523 GspMemoryError
= FALSE
;
1524 GspHex2Mem(ptr
, (PCHAR
) Address
, Length
, TRUE
);
1525 if (NULL
!= DbgProcess
)
1531 strcpy(GspOutBuffer
, "E03");
1532 DPRINT("Fault during memory write\n");
1536 strcpy(GspOutBuffer
, "OK");
1544 strcpy(GspOutBuffer
, "E02");
1548 /* cAA..AA Continue at address AA..AA(optional) */
1549 /* sAA..AA Step one instruction from AA..AA(optional) */
1554 ULONG BreakpointNumber
;
1557 /* try to read optional parameter, pc unchanged if no parm */
1558 if (GspHex2Long (&ptr
, &Address
))
1560 Context
->Eip
= Address
;
1563 NewPC
= Context
->Eip
;
1565 /* clear the trace bit */
1566 Context
->EFlags
&= 0xfffffeff;
1568 /* set the trace bit if we're Stepping */
1571 Context
->EFlags
|= 0x100;
1575 #if defined(__GNUC__)
1576 asm volatile ("movl %%db6, %0\n" : "=r" (dr6_
) : );
1577 #elif defined(_MSC_VER)
1578 __asm mov eax
, dr6 __asm mov dr6_
, eax
;
1580 #error Unknown compiler for inline assembler
1582 #elif defined(_M_PPC)
1583 /* XXX arty fixme */
1585 if (!(dr6_
& 0x4000))
1587 for (BreakpointNumber
= 0; BreakpointNumber
< 4; ++BreakpointNumber
)
1589 if (dr6_
& (1 << BreakpointNumber
))
1591 if (GspHwBreakpoints
[BreakpointNumber
].Type
== 0)
1593 /* Set restore flag */
1594 Context
->EFlags
|= 0x10000;
1600 GspLoadBreakpoints(TrapFrame
);
1602 #if defined(__GNUC__)
1603 asm volatile ("movl %0, %%db6\n" : : "r" (0));
1604 #elif defined(_MSC_VER)
1605 __asm mov eax
, 0 __asm mov dr6
, eax
;
1607 #error Unknown compiler for inline assembler
1609 #elif defined(_M_PPC)
1610 /* XXX arty fixme */
1613 if (NULL
!= GspDbgThread
)
1615 ObDereferenceObject(GspDbgThread
);
1616 GspDbgThread
= NULL
;
1619 DPRINT("Thread %p releasing mutex\n", PsGetCurrentThread());
1620 ExReleaseFastMutex(&GspLock
);
1621 DPRINT("Thread %p leaving stub\n", PsGetCurrentThread());
1626 case 'k': /* kill the program */
1627 strcpy(GspOutBuffer
, "OK");
1629 /* kill the program */
1631 case 'H': /* Set thread */
1635 case 'q': /* Query */
1639 case 'T': /* Query thread status */
1640 GspQueryThreadStatus(ptr
);
1649 GspHex2Long(&ptr
, &Type
);
1651 GspHex2Long(&ptr
, &Address
);
1653 GspHex2Long(&ptr
, &Length
);
1656 GspSetSwBreakpoint((ULONG_PTR
) Address
);
1660 GspSetHwBreakpoint(Type
, (ULONG_PTR
) Address
, Length
);
1671 GspHex2Long(&ptr
, &Type
);
1673 GspHex2Long(&ptr
, &Address
);
1675 GspHex2Long(&ptr
, &Length
);
1678 GspRemoveSwBreakpoint((ULONG_PTR
) Address
);
1682 GspRemoveHwBreakpoint(Type
, (ULONG_PTR
) Address
, Length
);
1691 /* reply to the request */
1692 GspPutPacket(GspOutBuffer
);
1699 if (NULL
!= GspDbgThread
)
1701 ObDereferenceObject(GspDbgThread
);
1702 GspDbgThread
= NULL
;
1711 GspBreakIn(PKINTERRUPT Interrupt
,
1712 PVOID ServiceContext
)
1714 PKTRAP_FRAME TrapFrame
;
1720 DPRINT("Break In\n");
1723 while (KdPortGetByteEx(&GdbPortInfo
, &Value
))
1736 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1738 TrapFrame
= PsGetCurrentThread()->Tcb
.TrapFrame
;
1740 KeTrapFrameToContext(TrapFrame
, NULL
, &Context
);
1742 KdpGdbEnterDebuggerException(NULL
, &Context
, TrapFrame
);
1744 KeContextToTrapFrame(&Context
, NULL
, TrapFrame
, Context
.ContextFlags
, KernelMode
);
1746 KeLowerIrql(OldIrql
);
1753 KdpGdbDebugPrint(PCH Message
, ULONG Length
)
1757 /* Initialize the GDB stub */
1760 KdpGdbStubInit(PKD_DISPATCH_TABLE WrapperTable
,
1763 if (!KdDebuggerEnabled
|| !KdpDebugMode
.Gdb
)
1770 ExInitializeFastMutex(&GspLock
);
1772 /* Write out the functions that we support for now */
1773 WrapperTable
->KdpInitRoutine
= KdpGdbStubInit
;
1774 WrapperTable
->KdpPrintRoutine
= KdpGdbDebugPrint
;
1775 WrapperTable
->KdpExceptionRoutine
= KdpGdbEnterDebuggerException
;
1777 /* Initialize the Port */
1778 KdPortInitializeEx(&GdbPortInfo
, 0, 0);
1780 KdpPort
= GdbPortInfo
.ComPort
;
1782 else if (BootPhase
== 1)
1784 GspInitialized
= TRUE
;
1786 GspRunThread
= NULL
;
1787 GspDbgThread
= NULL
;
1788 GspEnumThread
= NULL
;
1790 HalDisplayString("Waiting for GDB to attach\n");
1791 DbgPrint("Module 'hal.dll' loaded at 0x%.08x.\n", LdrHalBase
);
1792 DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C
);
1794 else if (BootPhase
== 2)
1796 HalDisplayString("\n GDB debugging enabled\n\n");