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 ****************************************************************************/
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
;
109 /* FIXME hardcoded for COM2, 115200 baud */
110 KD_PORT_INFORMATION GdbPortInfo
= { 2, 115200, 0 };
112 /* Number of Registers. */
117 EAX
, ECX
, EDX
, EBX
, ESP
, EBP
, ESI
, EDI
,
118 PC
/* also known as eip */,
119 PS
/* also known as eflags */,
120 CS
, SS
, DS
, ES
, FS
, GS
123 typedef struct _CPU_REGISTER
127 ULONG OffsetInContext
;
128 BOOLEAN SetInContext
;
129 } CPU_REGISTER
, *PCPU_REGISTER
;
131 static CPU_REGISTER GspRegisters
[NUMREGS
] =
135 static PCHAR GspThreadStates
[DeferredReady
+1] =
151 if ((ch
>= '0') && (ch
<= '9'))
155 if ((ch
>= 'a') && (ch
<= 'f'))
157 return (ch
- 'a' + 10);
159 if ((ch
>= 'A') && (ch
<= 'F'))
161 return (ch
- 'A' + 10);
167 static CHAR GspInBuffer
[BUFMAX
];
168 static CHAR GspOutBuffer
[BUFMAX
];
171 GdbPutChar(UCHAR Value
)
173 KdPortPutByteEx(&GdbPortInfo
, Value
);
181 while (!KdPortGetByteEx(&GdbPortInfo
, &Value
))
187 /* scan for the sequence $<data>#<Checksum> */
192 PCHAR Buffer
= &GspInBuffer
[0];
200 /* wait around for the start character, ignore all other characters */
201 while ((ch
= GdbGetChar ()) != '$')
209 /* now, read until a # or end of Buffer is found */
210 while (Count
< BUFMAX
)
221 Checksum
= Checksum
+ ch
;
230 XmitChecksum
= (CHAR
)(HexValue(ch
) << 4);
232 XmitChecksum
+= (CHAR
)(HexValue(ch
));
234 if (Checksum
!= XmitChecksum
)
236 GdbPutChar('-'); /* failed checksum */
240 GdbPutChar('+'); /* successful transfer */
248 /* send the packet in Buffer. */
251 GspPutPacket(PCHAR Buffer
)
257 /* $<packet info>#<Checksum>. */
264 while ((ch
= Buffer
[Count
]))
272 GdbPutChar(HexChars
[(Checksum
>> 4) & 0xf]);
273 GdbPutChar(HexChars
[Checksum
& 0xf]);
275 while (GdbGetChar() != '+');
280 GspPutPacketNoWait(PCHAR Buffer
)
286 /* $<packet info>#<Checksum>. */
291 while ((ch
= Buffer
[Count
]))
299 GdbPutChar(HexChars
[(Checksum
>> 4) & 0xf]);
300 GdbPutChar(HexChars
[Checksum
& 0xf]);
303 /* Indicate to caller of GspMem2Hex or GspHex2Mem that there has been an
305 static volatile BOOLEAN GspMemoryError
= FALSE
;
306 static volatile void *GspAccessLocation
= NULL
;
309 GspReadMemSafe(PCHAR Address
)
315 GspMemoryError
= TRUE
;
319 GspAccessLocation
= Address
;
321 GspAccessLocation
= NULL
;
326 /* Convert the memory pointed to by Address into hex, placing result in Buffer */
327 /* Return a pointer to the last char put in Buffer (null) */
328 /* If MayFault is TRUE, then we should set GspMemoryError in response to
329 a fault; if FALSE treat a fault like any other fault in the stub. */
331 GspMem2Hex(PCHAR Address
,
339 for (i
= 0; i
< (ULONG
) Count
; i
++)
343 ch
= GspReadMemSafe(Address
);
353 *Buffer
++ = HexChars
[(ch
>> 4) & 0xf];
354 *Buffer
++ = HexChars
[ch
& 0xf];
363 GspWriteMem(PCHAR Address
,
366 CHAR (*GetContent
)(PVOID Context
, ULONG Offset
),
377 while (Current
< Address
+ Count
)
379 Page
= (PCHAR
)PAGE_ROUND_DOWN(Current
);
380 if (Address
+ Count
<= Page
+ PAGE_SIZE
)
382 /* Fits in this page */
387 /* Flows into next page, handle only current page in this iteration */
388 CountInPage
= PAGE_SIZE
- (Address
- Page
);
392 OldProt
= MmGetPageProtect(NULL
, Address
);
393 MmSetPageProtect(NULL
, Address
, PAGE_EXECUTE_READWRITE
);
396 for (i
= 0; i
< CountInPage
&& ! GspMemoryError
; i
++)
398 ch
= (*GetContent
)(Context
, Current
- Address
);
402 GspAccessLocation
= Current
;
407 GspAccessLocation
= NULL
;
413 MmSetPageProtect(NULL
, Page
, OldProt
);
416 return Current
- Address
;
421 return Current
- Address
;
425 GspHex2MemGetContent(PVOID Context
, ULONG Offset
)
427 return (CHAR
)((HexValue(*((PCHAR
) Context
+ 2 * Offset
)) << 4) +
428 HexValue(*((PCHAR
) Context
+ 2 * Offset
+ 1)));
431 /* Convert the hex array pointed to by Buffer into binary to be placed at Address */
432 /* Return a pointer to the character AFTER the last byte read from Buffer */
434 GspHex2Mem(PCHAR Buffer
,
439 Count
= GspWriteMem(Address
, Count
, MayFault
, GspHex2MemGetContent
, Buffer
);
441 return Buffer
+ 2 * Count
;
445 GspWriteMemSafeGetContent(PVOID Context
, ULONG Offset
)
449 return *((PCHAR
) Context
);
453 GspWriteMemSafe(PCHAR Address
,
456 GspWriteMem(Address
, 1, TRUE
, GspWriteMemSafeGetContent
, &Ch
);
460 /* This function takes the 386 exception vector and attempts to
461 translate this number into a unix compatible signal value */
463 GspComputeSignal(NTSTATUS ExceptionCode
)
467 switch (ExceptionCode
)
469 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
470 SigVal
= 8; /* divide by zero */
472 case STATUS_SINGLE_STEP
:
473 case STATUS_BREAKPOINT
:
474 SigVal
= 5; /* breakpoint */
476 case STATUS_INTEGER_OVERFLOW
:
477 case STATUS_ARRAY_BOUNDS_EXCEEDED
:
478 SigVal
= 16; /* bound instruction */
480 case STATUS_ILLEGAL_INSTRUCTION
:
481 SigVal
= 4; /* Invalid opcode */
483 case STATUS_STACK_OVERFLOW
:
484 case STATUS_DATATYPE_MISALIGNMENT
:
485 case STATUS_ACCESS_VIOLATION
:
486 SigVal
= 11; /* access violation */
489 SigVal
= 7; /* "software generated" */
495 /**********************************************/
496 /* WHILE WE FIND NICE HEX CHARS, BUILD A LONG */
497 /* RETURN NUMBER OF CHARS PROCESSED */
498 /**********************************************/
500 GspHex2Long(PCHAR
*Address
,
510 Hex
= HexValue(**Address
);
513 *Value
= (*Value
<< 4) | Hex
;
529 GspLong2Hex(PCHAR
*Address
,
534 Save
= (((Value
>> 0) & 0xff) << 24) |
535 (((Value
>> 8) & 0xff) << 16) |
536 (((Value
>> 16) & 0xff) << 8) |
537 (((Value
>> 24) & 0xff) << 0);
538 *Address
= GspMem2Hex((PCHAR
) &Save
, *Address
, 4, FALSE
);
543 * When coming from kernel mode, Esp is not stored in the trap frame.
544 * Instead, it was pointing to the location of the TrapFrame Esp member
545 * when the exception occured. When coming from user mode, Esp is just
546 * stored in the TrapFrame Esp member.
549 GspGetEspFromTrapFrame(PKTRAP_FRAME TrapFrame
)
551 return KeGetPreviousMode() == KernelMode
552 ? (LONG
) &TrapFrame
->Gpr1
: (LONG
)TrapFrame
->Gpr1
;
557 GspGetRegisters(PCHAR Address
,
558 PKTRAP_FRAME TrapFrame
)
564 ULONG_PTR
*KernelStack
;
566 if (NULL
== GspDbgThread
)
568 Thread
= PsGetCurrentThread();
572 TrapFrame
= GspDbgThread
->Tcb
.TrapFrame
;
573 Thread
= GspDbgThread
;
576 if (Waiting
== Thread
->Tcb
.State
)
578 KernelStack
= Thread
->Tcb
.KernelStack
;
579 for (i
= 0; i
< sizeof(GspRegisters
) / sizeof(GspRegisters
[0]); i
++)
584 Address
= GspMem2Hex((PCHAR
) &Value
, Address
, GspRegisters
[i
].Size
,
590 for (i
= 0; i
< sizeof(GspRegisters
) / sizeof(GspRegisters
[0]); i
++)
596 Value
= GspGetEspFromTrapFrame(TrapFrame
);
600 p
= (PULONG
)((ULONG_PTR
) TrapFrame
+
601 GspRegisters
[i
].OffsetInTF
);
608 * This thread has not been sheduled yet so assume it
609 * is still in PsBeginThreadWithContextInternal().
611 Value
= (ULONG
)KiThreadStartup
;
617 Address
= GspMem2Hex((PCHAR
) &Value
, Address
,
618 GspRegisters
[i
].Size
, FALSE
);
625 GspSetRegistersInTrapFrame(PCHAR Address
,
627 PKTRAP_FRAME TrapFrame
)
640 for (i
= 0; i
< NUMREGS
; i
++)
642 if (GspRegisters
[i
].SetInContext
)
644 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[i
].OffsetInContext
);
648 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[i
].OffsetInTF
);
651 Buffer
= GspHex2Mem(Buffer
, (PCHAR
) &Value
, GspRegisters
[i
].Size
, FALSE
);
658 GspSetSingleRegisterInTrapFrame(PCHAR Address
,
661 PKTRAP_FRAME TrapFrame
)
671 if (GspRegisters
[Number
].SetInContext
)
673 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[Number
].OffsetInContext
);
677 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[Number
].OffsetInTF
);
680 GspHex2Mem(Address
, (PCHAR
) &Value
, GspRegisters
[Number
].Size
, FALSE
);
686 GspFindThread(PCHAR Data
,
689 PETHREAD ThreadInfo
= NULL
;
691 if (strcmp (Data
, "-1") == 0)
700 PCHAR ptr
= &Data
[0];
702 GspHex2Long(&ptr
, (PLONG
) &uThreadId
);
703 ThreadId
= (HANDLE
)uThreadId
;
705 if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId
, &ThreadInfo
)))
711 *Thread
= ThreadInfo
;
717 GspSetThread(PCHAR Request
)
720 PCHAR ptr
= &Request
[1];
724 case 'c': /* Run thread */
725 if (GspFindThread(ptr
, &ThreadInfo
))
727 GspOutBuffer
[0] = 'O';
728 GspOutBuffer
[1] = 'K';
730 if (NULL
!= GspRunThread
)
732 ObDereferenceObject(GspRunThread
);
734 GspRunThread
= ThreadInfo
;
735 if (NULL
!= GspRunThread
)
737 ObReferenceObject(GspRunThread
);
742 GspOutBuffer
[0] = 'E';
745 case 'g': /* Debug thread */
746 if (GspFindThread(ptr
, &ThreadInfo
))
748 GspOutBuffer
[0] = 'O';
749 GspOutBuffer
[1] = 'K';
751 if (NULL
!= GspDbgThread
)
753 ObDereferenceObject(GspDbgThread
);
756 if (ThreadInfo
== PsGetCurrentThread())
759 ObDereferenceObject(ThreadInfo
);
763 GspDbgThread
= ThreadInfo
;
768 GspOutBuffer
[0] = 'E';
778 GspQuery(PCHAR Request
)
782 if (strncmp(Request
, "C", 1) == 0)
784 PCHAR ptr
= &GspOutBuffer
[2];
786 /* Get current thread id */
787 GspOutBuffer
[0] = 'Q';
788 GspOutBuffer
[1] = 'C';
789 if (NULL
!= GspDbgThread
)
791 Value
= (ULONG
) GspDbgThread
->Cid
.UniqueThread
;
795 Value
= (ULONG
) PsGetCurrentThread()->Cid
.UniqueThread
;
797 GspLong2Hex(&ptr
, Value
);
799 else if (strncmp(Request
, "fThreadInfo", 11) == 0)
802 PLIST_ENTRY AThread
, AProcess
;
803 PCHAR ptr
= &GspOutBuffer
[1];
805 /* Get first thread id */
806 GspEnumThread
= NULL
;
807 AProcess
= PsActiveProcessHead
.Flink
;
808 while(AProcess
!= &PsActiveProcessHead
)
810 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
811 AThread
= Process
->ThreadListHead
.Flink
;
812 if (AThread
!= &Process
->ThreadListHead
)
814 GspEnumThread
= CONTAINING_RECORD(Process
->ThreadListHead
.Flink
,
815 ETHREAD
, ThreadListEntry
);
818 AProcess
= AProcess
->Flink
;
820 if(GspEnumThread
!= NULL
)
822 GspOutBuffer
[0] = 'm';
823 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
824 GspLong2Hex(&ptr
, Value
);
828 /* FIXME - what to do here? This case should never happen though, there
829 should always be at least one thread on the system... */
830 /* GspOutBuffer[0] = 'l'; */
833 else if (strncmp(Request
, "sThreadInfo", 11) == 0)
836 PLIST_ENTRY AThread
, AProcess
;
837 PCHAR ptr
= &GspOutBuffer
[1];
839 /* Get next thread id */
840 if (GspEnumThread
!= NULL
)
842 /* find the next thread */
843 Process
= GspEnumThread
->ThreadsProcess
;
844 if(GspEnumThread
->ThreadListEntry
.Flink
!= &Process
->ThreadListHead
)
846 GspEnumThread
= CONTAINING_RECORD(GspEnumThread
->ThreadListEntry
.Flink
,
847 ETHREAD
, ThreadListEntry
);
851 PETHREAD Thread
= NULL
;
852 AProcess
= Process
->ActiveProcessLinks
.Flink
;
853 while(AProcess
!= &PsActiveProcessHead
)
855 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
856 AThread
= Process
->ThreadListHead
.Flink
;
857 if (AThread
!= &Process
->ThreadListHead
)
859 Thread
= CONTAINING_RECORD(Process
->ThreadListHead
.Flink
,
860 ETHREAD
, ThreadListEntry
);
863 AProcess
= AProcess
->Flink
;
865 GspEnumThread
= Thread
;
868 if (GspEnumThread
!= NULL
)
871 GspOutBuffer
[0] = 'm';
872 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
873 GspLong2Hex(&ptr
, Value
);
877 GspOutBuffer
[0] = 'l';
882 GspOutBuffer
[0] = 'l';
885 else if (strncmp(Request
, "ThreadExtraInfo", 15) == 0)
889 /* Get thread information */
890 if (GspFindThread(Request
+ 16, &ThreadInfo
))
895 Proc
= (PEPROCESS
) ThreadInfo
->ThreadsProcess
;
900 sprintf(Buffer
, "%s [%d:0x%x], ", Proc
->ImageFileName
,
901 (int) Proc
->UniqueProcessId
,
902 (int) ThreadInfo
->Cid
.UniqueThread
);
904 strcpy(Buffer
+ strlen(Buffer
),
905 GspThreadStates
[ThreadInfo
->Tcb
.State
]);
907 ObDereferenceObject(ThreadInfo
);
909 GspMem2Hex(Buffer
, &GspOutBuffer
[0], strlen(Buffer
), FALSE
);
915 GspQueryThreadStatus(PCHAR Request
)
918 PCHAR ptr
= &Request
[0];
920 if (GspFindThread(ptr
, &ThreadInfo
))
922 ObDereferenceObject(ThreadInfo
);
924 GspOutBuffer
[0] = 'O';
925 GspOutBuffer
[1] = 'K';
926 GspOutBuffer
[2] = '\0';
930 GspOutBuffer
[0] = 'E';
931 GspOutBuffer
[1] = '\0';
935 #define DR7_L0 0x00000001 /* Local breakpoint 0 enable */
936 #define DR7_G0 0x00000002 /* Global breakpoint 0 enable */
937 #define DR7_L1 0x00000004 /* Local breakpoint 1 enable */
938 #define DR7_G1 0x00000008 /* Global breakpoint 1 enable */
939 #define DR7_L2 0x00000010 /* Local breakpoint 2 enable */
940 #define DR7_G2 0x00000020 /* Global breakpoint 2 enable */
941 #define DR7_L3 0x00000040 /* Local breakpoint 3 enable */
942 #define DR7_G3 0x00000080 /* Global breakpoint 3 enable */
943 #define DR7_LE 0x00000100 /* Local exact breakpoint enable (old) */
944 #define DR7_GE 0x00000200 /* Global exact breakpoint enable (old) */
945 #define DR7_GD 0x00002000 /* General detect enable */
946 #define DR7_TYPE0_MASK 0x00030000 /* Breakpoint 0 condition */
947 #define DR7_LEN0_MASK 0x000c0000 /* Breakpoint 0 length */
948 #define DR7_TYPE1_MASK 0x00300000 /* Breakpoint 1 condition */
949 #define DR7_LEN1_MASK 0x00c00000 /* Breakpoint 1 length */
950 #define DR7_TYPE2_MASK 0x03000000 /* Breakpoint 2 condition */
951 #define DR7_LEN2_MASK 0x0c000000 /* Breakpoint 2 length */
952 #define DR7_TYPE3_MASK 0x30000000 /* Breakpoint 3 condition */
953 #define DR7_LEN3_MASK 0xc0000000 /* Breakpoint 3 length */
954 #define DR7_GLOBAL_ENABLE(Bp) (2 << (2 * (Bp)))
955 #define DR7_TYPE(Bp, Type) ((Type) << (16 + 4 * (Bp)))
956 #define DR7_LEN(Bp, Len) ((Len) << (18 + 4 * (Bp)))
958 #define I386_BP_TYPE_EXECUTE 0
959 #define I386_BP_TYPE_DATA_WRITE 1
960 #define I386_BP_TYPE_DATA_READWRITE 3
962 #define I386_OPCODE_INT3 0xcc
964 #define GDB_ZTYPE_MEMORY_BREAKPOINT 0
965 #define GDB_ZTYPE_HARDWARE_BREAKPOINT 1
966 #define GDB_ZTYPE_WRITE_WATCHPOINT 2
967 #define GDB_ZTYPE_READ_WATCHPOINT 3
968 #define GDB_ZTYPE_ACCESS_WATCHPOINT 4
970 typedef struct _GSPHWBREAKPOINT
977 #define MAX_HW_BREAKPOINTS 4
978 static unsigned GspHwBreakpointCount
= 0;
979 static GSPHWBREAKPOINT GspHwBreakpoints
[MAX_HW_BREAKPOINTS
];
981 typedef struct _GSPSWBREAKPOINT
988 #define MAX_SW_BREAKPOINTS 64
989 static unsigned GspSwBreakpointCount
= 0;
990 static GSPSWBREAKPOINT GspSwBreakpoints
[MAX_SW_BREAKPOINTS
];
993 GspSetHwBreakpoint(ULONG Type
, ULONG_PTR Address
, ULONG Length
)
995 DPRINT("GspSetHwBreakpoint(%lu, 0x%p, %lu)\n", Type
, Address
, Length
);
997 if (GDB_ZTYPE_READ_WATCHPOINT
== Type
)
999 DPRINT1("Read watchpoint not supported\n");
1000 strcpy(GspOutBuffer
, "E22");
1002 else if (GDB_ZTYPE_HARDWARE_BREAKPOINT
== Type
&& 1 != Length
)
1004 DPRINT1("Invalid length %lu for hardware breakpoint\n", Length
);
1005 strcpy(GspOutBuffer
, "E22");
1007 else if (1 != Length
&& 2 != Length
&& 4 != Length
)
1009 DPRINT1("Invalid length %lu for GDB Z type %lu\n", Length
, Type
);
1010 strcpy(GspOutBuffer
, "E22");
1012 else if (0 != (Address
& (Length
- 1)))
1014 DPRINT1("Invalid alignment for address 0x%p and length %d\n",
1016 strcpy(GspOutBuffer
, "E22");
1018 else if (MAX_HW_BREAKPOINTS
== GspHwBreakpointCount
)
1020 DPRINT1("Trying to set too many hardware breakpoints\n");
1021 strcpy(GspOutBuffer
, "E22");
1025 DPRINT("Stored at index %u\n", GspHwBreakpointCount
);
1026 GspHwBreakpoints
[GspHwBreakpointCount
].Type
= Type
;
1027 GspHwBreakpoints
[GspHwBreakpointCount
].Address
= Address
;
1028 GspHwBreakpoints
[GspHwBreakpointCount
].Length
= Length
;
1029 GspHwBreakpointCount
++;
1030 strcpy(GspOutBuffer
, "OK");
1035 GspRemoveHwBreakpoint(ULONG Type
, ULONG_PTR Address
, ULONG Length
)
1039 DPRINT("GspRemoveHwBreakpoint(%lu, 0x%p, %lu)\n", Type
, Address
, Length
);
1040 for (Index
= 0; Index
< GspHwBreakpointCount
; Index
++)
1042 if (GspHwBreakpoints
[Index
].Type
== Type
&&
1043 GspHwBreakpoints
[Index
].Address
== Address
&&
1044 GspHwBreakpoints
[Index
].Length
== Length
)
1046 DPRINT("Found match at index %u\n", Index
);
1047 if (Index
+ 1 < GspHwBreakpointCount
)
1049 memmove(GspHwBreakpoints
+ Index
,
1050 GspHwBreakpoints
+ (Index
+ 1),
1051 (GspHwBreakpointCount
- Index
- 1) *
1052 sizeof(GSPHWBREAKPOINT
));
1054 GspHwBreakpointCount
--;
1055 strcpy(GspOutBuffer
, "OK");
1060 DPRINT1("Not found\n");
1061 strcpy(GspOutBuffer
, "E22");
1065 GspSetSwBreakpoint(ULONG_PTR Address
)
1067 DPRINT("GspSetSwBreakpoint(0x%p)\n", Address
);
1069 if (MAX_SW_BREAKPOINTS
== GspSwBreakpointCount
)
1071 DPRINT1("Trying to set too many software breakpoints\n");
1072 strcpy(GspOutBuffer
, "E22");
1076 DPRINT("Stored at index %u\n", GspSwBreakpointCount
);
1077 GspSwBreakpoints
[GspSwBreakpointCount
].Address
= Address
;
1078 GspSwBreakpoints
[GspSwBreakpointCount
].Active
= FALSE
;
1079 GspSwBreakpointCount
++;
1080 strcpy(GspOutBuffer
, "OK");
1085 GspRemoveSwBreakpoint(ULONG_PTR Address
)
1089 DPRINT("GspRemoveSwBreakpoint(0x%p)\n", Address
);
1090 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1092 if (GspSwBreakpoints
[Index
].Address
== Address
)
1094 DPRINT("Found match at index %u\n", Index
);
1095 ASSERT(! GspSwBreakpoints
[Index
].Active
);
1096 if (Index
+ 1 < GspSwBreakpointCount
)
1098 memmove(GspSwBreakpoints
+ Index
,
1099 GspSwBreakpoints
+ (Index
+ 1),
1100 (GspSwBreakpointCount
- Index
- 1) *
1101 sizeof(GSPSWBREAKPOINT
));
1103 GspSwBreakpointCount
--;
1104 strcpy(GspOutBuffer
, "OK");
1109 DPRINT1("Not found\n");
1110 strcpy(GspOutBuffer
, "E22");
1114 GspLoadHwBreakpoint(PKTRAP_FRAME TrapFrame
,
1120 DPRINT("GspLoadHwBreakpoint(0x%p, %d, 0x%p, %d)\n", TrapFrame
, BpIndex
,
1123 /* Set the DR7_Gx bit to globally enable the breakpoint */
1124 TrapFrame
->Dr7
|= DR7_GLOBAL_ENABLE(BpIndex
) |
1125 DR7_LEN(BpIndex
, Length
) |
1126 DR7_TYPE(BpIndex
, Type
);
1131 DPRINT("Setting DR0 to 0x%p\n", Address
);
1132 TrapFrame
->Dr0
= Address
;
1136 DPRINT("Setting DR1 to 0x%p\n", Address
);
1137 TrapFrame
->Dr1
= Address
;
1141 DPRINT("Setting DR2 to 0x%p\n", Address
);
1142 TrapFrame
->Dr2
= Address
;
1146 DPRINT("Setting DR3 to 0x%p\n", Address
);
1147 TrapFrame
->Dr3
= Address
;
1153 GspLoadBreakpoints(PKTRAP_FRAME TrapFrame
)
1158 DPRINT("GspLoadBreakpoints\n");
1159 DPRINT("DR7 on entry: 0x%08x\n", TrapFrame
->Dr7
);
1160 /* Remove all breakpoints */
1161 TrapFrame
->Dr7
&= ~(DR7_L0
| DR7_L1
| DR7_L2
| DR7_L3
|
1162 DR7_G0
| DR7_G1
| DR7_G2
| DR7_G3
|
1163 DR7_TYPE0_MASK
| DR7_LEN0_MASK
|
1164 DR7_TYPE1_MASK
| DR7_LEN1_MASK
|
1165 DR7_TYPE2_MASK
| DR7_LEN2_MASK
|
1166 DR7_TYPE3_MASK
| DR7_LEN3_MASK
);
1168 for (Index
= 0; Index
< GspHwBreakpointCount
; Index
++)
1170 switch(GspHwBreakpoints
[Index
].Type
)
1172 case GDB_ZTYPE_HARDWARE_BREAKPOINT
:
1173 i386Type
= I386_BP_TYPE_EXECUTE
;
1175 case GDB_ZTYPE_WRITE_WATCHPOINT
:
1176 i386Type
= I386_BP_TYPE_DATA_WRITE
;
1178 case GDB_ZTYPE_ACCESS_WATCHPOINT
:
1179 i386Type
= I386_BP_TYPE_DATA_READWRITE
;
1183 i386Type
= I386_BP_TYPE_EXECUTE
;
1187 GspLoadHwBreakpoint(TrapFrame
, Index
, GspHwBreakpoints
[Index
].Address
,
1188 GspHwBreakpoints
[Index
].Length
- 1, i386Type
);
1191 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1193 if (GspHwBreakpointCount
+ Index
< MAX_HW_BREAKPOINTS
)
1195 DPRINT("Implementing software interrupt using hardware register\n");
1196 GspLoadHwBreakpoint(TrapFrame
, GspHwBreakpointCount
+ Index
,
1197 GspSwBreakpoints
[Index
].Address
, 0,
1198 I386_BP_TYPE_EXECUTE
);
1199 GspSwBreakpoints
[Index
].Active
= FALSE
;
1203 DPRINT("Using real software breakpoint\n");
1204 GspMemoryError
= FALSE
;
1205 GspSwBreakpoints
[Index
].PrevContent
= GspReadMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
);
1206 if (! GspMemoryError
)
1208 GspWriteMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
, I386_OPCODE_INT3
);
1210 GspSwBreakpoints
[Index
].Active
= ! GspMemoryError
;
1213 DPRINT1("Failed to set software breakpoint at 0x%p\n",
1214 GspSwBreakpoints
[Index
].Address
);
1218 DPRINT("Successfully set software breakpoint at 0x%p\n",
1219 GspSwBreakpoints
[Index
].Address
);
1220 DPRINT1("Successfully set software breakpoint at 0x%p\n", GspSwBreakpoints
[Index
].Address
);
1225 DPRINT("Final DR7 value 0x%08x\n", TrapFrame
->Dr7
);
1229 GspUnloadBreakpoints(PKTRAP_FRAME TrapFrame
)
1233 DPRINT("GspUnloadHwBreakpoints\n");
1235 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1237 if (GspSwBreakpoints
[Index
].Active
)
1239 GspMemoryError
= FALSE
;
1240 GspWriteMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
,
1241 GspSwBreakpoints
[Index
].PrevContent
);
1242 GspSwBreakpoints
[Index
].Active
= FALSE
;
1245 DPRINT1("Failed to remove software breakpoint from 0x%p\n",
1246 GspSwBreakpoints
[Index
].Address
);
1250 DPRINT("Successfully removed software breakpoint from 0x%p\n",
1251 GspSwBreakpoints
[Index
].Address
);
1257 static BOOLEAN gdb_attached_yet
= FALSE
;
1259 * This function does all command procesing for interfacing to gdb.
1263 KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord
,
1265 PKTRAP_FRAME TrapFrame
)
1274 /* FIXME: Stop on other CPUs too */
1276 if (STATUS_ACCESS_VIOLATION
== (NTSTATUS
) ExceptionRecord
->ExceptionCode
&&
1277 NULL
!= GspAccessLocation
&&
1278 (ULONG_PTR
) GspAccessLocation
==
1279 (ULONG_PTR
) ExceptionRecord
->ExceptionInformation
[1])
1281 GspAccessLocation
= NULL
;
1282 GspMemoryError
= TRUE
;
1283 //Context->Eip += 3;
1287 DPRINT("Thread %p entering stub\n", PsGetCurrentThread());
1288 /* Can only debug 1 thread at a time... */
1289 ExAcquireFastMutex(&GspLock
);
1290 DPRINT("Thread %p acquired mutex\n", PsGetCurrentThread());
1292 /* Disable hardware debugging while we are inside the stub */
1294 GspUnloadBreakpoints(TrapFrame
);
1296 /* Make sure we're debugging the current thread. */
1297 if (NULL
!= GspDbgThread
)
1299 DPRINT1("Internal error: entering stub with non-NULL GspDbgThread\n");
1300 ObDereferenceObject(GspDbgThread
);
1301 GspDbgThread
= NULL
;
1304 /* ugly hack to avoid attempting to send status at the very
1305 * beginning, right when GDB is trying to query the stub */
1306 if (gdb_attached_yet
)
1311 /* reply to host that an exception has occurred */
1312 SigVal
= GspComputeSignal(ExceptionRecord
->ExceptionCode
);
1316 *ptr
++ = 'T'; /* notify gdb with signo, PC, FP and SP */
1317 *ptr
++ = HexChars
[(SigVal
>> 4) & 0xf];
1318 *ptr
++ = HexChars
[SigVal
& 0xf];
1320 *ptr
++ = HexChars
[ESP
];
1323 Esp
= GspGetEspFromTrapFrame(TrapFrame
); /* SP */
1324 ptr
= GspMem2Hex((PCHAR
) &Esp
, ptr
, 4, 0);
1327 *ptr
++ = HexChars
[EBP
];
1329 //ptr = GspMem2Hex((PCHAR) &TrapFrame->Ebp, ptr, 4, 0); /* FP */
1332 *ptr
++ = HexChars
[PC
];
1334 //ptr = GspMem2Hex((PCHAR) &TrapFrame->Eip, ptr, 4, 0); /* PC */
1339 GspPutPacket(&GspOutBuffer
[0]);
1343 gdb_attached_yet
= 1;
1350 /* Zero the buffer now so we don't have to worry about the terminating zero character */
1351 memset(GspOutBuffer
, 0, sizeof(GspInBuffer
));
1352 ptr
= GspGetPacket();
1357 /* a little hack to send more complete status information */
1359 GspOutBuffer
[0] = 'S';
1360 GspOutBuffer
[1] = HexChars
[SigVal
>> 4];
1361 GspOutBuffer
[2] = HexChars
[SigVal
% 16];
1362 GspOutBuffer
[3] = 0;
1365 GspRemoteDebug
= !GspRemoteDebug
; /* toggle debug flag */
1367 case 'g': /* return the value of the CPU Registers */
1368 GspGetRegisters(GspOutBuffer
, TrapFrame
);
1370 case 'G': /* set the value of the CPU Registers - return OK */
1371 if (NULL
!= GspDbgThread
)
1373 GspSetRegistersInTrapFrame(ptr
, Context
, GspDbgThread
->Tcb
.TrapFrame
);
1377 GspSetRegistersInTrapFrame(ptr
, Context
, TrapFrame
);
1379 strcpy(GspOutBuffer
, "OK");
1381 case 'P': /* set the value of a single CPU register - return OK */
1385 if ((GspHex2Long(&ptr
, &Register
)) && (*ptr
++ == '='))
1387 if ((Register
>= 0) && (Register
< NUMREGS
))
1391 GspSetSingleRegisterInTrapFrame(ptr
, Register
,
1393 GspDbgThread
->Tcb
.TrapFrame
);
1397 GspSetSingleRegisterInTrapFrame(ptr
, Register
,
1398 Context
, TrapFrame
);
1400 strcpy(GspOutBuffer
, "OK");
1405 strcpy(GspOutBuffer
, "E01");
1409 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1411 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
1412 if (GspHex2Long(&ptr
, &Address
) &&
1414 GspHex2Long(&ptr
, &Length
))
1416 PEPROCESS DbgProcess
= NULL
;
1419 if (NULL
!= GspDbgThread
&&
1420 PsGetCurrentProcess() != GspDbgThread
->ThreadsProcess
)
1422 DbgProcess
= GspDbgThread
->ThreadsProcess
;
1423 KeAttachProcess(&DbgProcess
->Pcb
);
1425 GspMemoryError
= FALSE
;
1426 GspMem2Hex((PCHAR
) Address
, GspOutBuffer
, Length
, 1);
1427 if (NULL
!= DbgProcess
)
1433 strcpy(GspOutBuffer
, "E03");
1434 DPRINT("Fault during memory read\n");
1440 strcpy(GspOutBuffer
, "E01");
1444 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
1446 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
1447 if (GspHex2Long(&ptr
, &Address
))
1449 if (*(ptr
++) == ',' &&
1450 GspHex2Long(&ptr
, &Length
) &&
1453 PEPROCESS DbgProcess
= NULL
;
1455 if (NULL
!= GspDbgThread
&&
1456 PsGetCurrentProcess() != GspDbgThread
->ThreadsProcess
)
1458 DbgProcess
= GspDbgThread
->ThreadsProcess
;
1459 KeAttachProcess(&DbgProcess
->Pcb
);
1461 GspMemoryError
= FALSE
;
1462 GspHex2Mem(ptr
, (PCHAR
) Address
, Length
, TRUE
);
1463 if (NULL
!= DbgProcess
)
1469 strcpy(GspOutBuffer
, "E03");
1470 DPRINT("Fault during memory write\n");
1474 strcpy(GspOutBuffer
, "OK");
1482 strcpy(GspOutBuffer
, "E02");
1486 /* cAA..AA Continue at address AA..AA(optional) */
1487 /* sAA..AA Step one instruction from AA..AA(optional) */
1492 ULONG BreakpointNumber
;
1495 /* try to read optional parameter, pc unchanged if no parm */
1496 if (GspHex2Long (&ptr
, &Address
))
1498 //Context->Eip = Address;
1501 //NewPC = Context->Eip;
1503 /* clear the trace bit */
1504 //Context->EFlags &= 0xfffffeff;
1506 /* set the trace bit if we're Stepping */
1509 //Context->EFlags |= 0x100;
1512 // XXX arty load dr6
1513 if (!(dr6_
& 0x4000))
1515 for (BreakpointNumber
= 0; BreakpointNumber
< 4; ++BreakpointNumber
)
1517 if (dr6_
& (1 << BreakpointNumber
))
1519 if (GspHwBreakpoints
[BreakpointNumber
].Type
== 0)
1521 /* Set restore flag */
1522 //Context->EFlags |= 0x10000;
1528 GspLoadBreakpoints(TrapFrame
);
1530 if (NULL
!= GspDbgThread
)
1532 ObDereferenceObject(GspDbgThread
);
1533 GspDbgThread
= NULL
;
1536 DPRINT("Thread %p releasing mutex\n", PsGetCurrentThread());
1537 ExReleaseFastMutex(&GspLock
);
1538 DPRINT("Thread %p leaving stub\n", PsGetCurrentThread());
1543 case 'k': /* kill the program */
1544 strcpy(GspOutBuffer
, "OK");
1546 /* kill the program */
1548 case 'H': /* Set thread */
1552 case 'q': /* Query */
1556 case 'T': /* Query thread status */
1557 GspQueryThreadStatus(ptr
);
1566 GspHex2Long(&ptr
, &Type
);
1568 GspHex2Long(&ptr
, &Address
);
1570 GspHex2Long(&ptr
, &Length
);
1573 GspSetSwBreakpoint((ULONG_PTR
) Address
);
1577 GspSetHwBreakpoint(Type
, (ULONG_PTR
) Address
, Length
);
1588 GspHex2Long(&ptr
, &Type
);
1590 GspHex2Long(&ptr
, &Address
);
1592 GspHex2Long(&ptr
, &Length
);
1595 GspRemoveSwBreakpoint((ULONG_PTR
) Address
);
1599 GspRemoveHwBreakpoint(Type
, (ULONG_PTR
) Address
, Length
);
1608 /* reply to the request */
1609 GspPutPacket(GspOutBuffer
);
1616 if (NULL
!= GspDbgThread
)
1618 ObDereferenceObject(GspDbgThread
);
1619 GspDbgThread
= NULL
;
1628 GspBreakIn(PKINTERRUPT Interrupt
,
1629 PVOID ServiceContext
)
1631 PKTRAP_FRAME TrapFrame
;
1637 DPRINT("Break In\n");
1640 while (KdPortGetByteEx(&GdbPortInfo
, &Value
))
1653 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1655 TrapFrame
= PsGetCurrentThread()->Tcb
.TrapFrame
;
1657 KeTrapFrameToContext(TrapFrame
, NULL
, &Context
);
1659 KdpGdbEnterDebuggerException(NULL
, &Context
, TrapFrame
);
1661 KeContextToTrapFrame(&Context
, NULL
, TrapFrame
, Context
.ContextFlags
, KernelMode
);
1663 KeLowerIrql(OldIrql
);
1670 KdpGdbDebugPrint(PCH Message
, ULONG Length
)
1674 /* Initialize the GDB stub */
1677 KdpGdbStubInit(PKD_DISPATCH_TABLE WrapperTable
,
1680 if (!KdDebuggerEnabled
|| !KdpDebugMode
.Gdb
)
1687 ExInitializeFastMutex(&GspLock
);
1689 /* Write out the functions that we support for now */
1690 WrapperTable
->KdpInitRoutine
= KdpGdbStubInit
;
1691 WrapperTable
->KdpPrintRoutine
= KdpGdbDebugPrint
;
1692 WrapperTable
->KdpExceptionRoutine
= KdpGdbEnterDebuggerException
;
1694 /* Initialize the Port */
1695 KdPortInitializeEx(&GdbPortInfo
, 0, 0);
1697 KdpPort
= GdbPortInfo
.ComPort
;
1699 else if (BootPhase
== 1)
1701 GspInitialized
= TRUE
;
1703 GspRunThread
= NULL
;
1704 GspDbgThread
= NULL
;
1705 GspEnumThread
= NULL
;
1707 HalDisplayString("Waiting for GDB to attach\n");
1708 DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C
);
1710 else if (BootPhase
== 2)
1712 HalDisplayString("\n GDB debugging enabled\n\n");