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
;
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
] =
133 static PCHAR GspThreadStates
[DeferredReady
+1] =
149 if ((ch
>= '0') && (ch
<= '9'))
153 if ((ch
>= 'a') && (ch
<= 'f'))
155 return (ch
- 'a' + 10);
157 if ((ch
>= 'A') && (ch
<= 'F'))
159 return (ch
- 'A' + 10);
165 static CHAR GspInBuffer
[BUFMAX
];
166 static CHAR GspOutBuffer
[BUFMAX
];
169 GdbPutChar(UCHAR Value
)
171 KdPortPutByteEx(&GdbPortInfo
, Value
);
179 while (!KdPortGetByteEx(&GdbPortInfo
, &Value
))
185 /* scan for the sequence $<data>#<Checksum> */
190 PCHAR Buffer
= &GspInBuffer
[0];
198 /* wait around for the start character, ignore all other characters */
199 while ((ch
= GdbGetChar ()) != '$')
207 /* now, read until a # or end of Buffer is found */
208 while (Count
< BUFMAX
)
219 Checksum
= Checksum
+ ch
;
228 XmitChecksum
= (CHAR
)(HexValue(ch
) << 4);
230 XmitChecksum
+= (CHAR
)(HexValue(ch
));
232 if (Checksum
!= XmitChecksum
)
234 GdbPutChar('-'); /* failed checksum */
238 GdbPutChar('+'); /* successful transfer */
246 /* send the packet in Buffer. */
249 GspPutPacket(PCHAR Buffer
)
255 /* $<packet info>#<Checksum>. */
262 while ((ch
= Buffer
[Count
]))
270 GdbPutChar(HexChars
[(Checksum
>> 4) & 0xf]);
271 GdbPutChar(HexChars
[Checksum
& 0xf]);
273 while (GdbGetChar() != '+');
278 GspPutPacketNoWait(PCHAR Buffer
)
284 /* $<packet info>#<Checksum>. */
289 while ((ch
= Buffer
[Count
]))
297 GdbPutChar(HexChars
[(Checksum
>> 4) & 0xf]);
298 GdbPutChar(HexChars
[Checksum
& 0xf]);
301 /* Indicate to caller of GspMem2Hex or GspHex2Mem that there has been an
303 static volatile BOOLEAN GspMemoryError
= FALSE
;
304 static volatile void *GspAccessLocation
= NULL
;
307 GspReadMemSafe(PCHAR Address
)
313 GspMemoryError
= TRUE
;
317 GspAccessLocation
= Address
;
319 GspAccessLocation
= NULL
;
324 /* Convert the memory pointed to by Address into hex, placing result in Buffer */
325 /* Return a pointer to the last char put in Buffer (null) */
326 /* If MayFault is TRUE, then we should set GspMemoryError in response to
327 a fault; if FALSE treat a fault like any other fault in the stub. */
329 GspMem2Hex(PCHAR Address
,
337 for (i
= 0; i
< (ULONG
) Count
; i
++)
341 ch
= GspReadMemSafe(Address
);
351 *Buffer
++ = HexChars
[(ch
>> 4) & 0xf];
352 *Buffer
++ = HexChars
[ch
& 0xf];
361 GspWriteMem(PCHAR Address
,
364 CHAR (*GetContent
)(PVOID Context
, ULONG Offset
),
375 while (Current
< Address
+ Count
)
377 Page
= (PCHAR
)PAGE_ROUND_DOWN(Current
);
378 if (Address
+ Count
<= Page
+ PAGE_SIZE
)
380 /* Fits in this page */
385 /* Flows into next page, handle only current page in this iteration */
386 CountInPage
= PAGE_SIZE
- (Address
- Page
);
390 OldProt
= MmGetPageProtect(NULL
, Address
);
391 MmSetPageProtect(NULL
, Address
, PAGE_EXECUTE_READWRITE
);
394 for (i
= 0; i
< CountInPage
&& ! GspMemoryError
; i
++)
396 ch
= (*GetContent
)(Context
, Current
- Address
);
400 GspAccessLocation
= Current
;
405 GspAccessLocation
= NULL
;
411 MmSetPageProtect(NULL
, Page
, OldProt
);
414 return Current
- Address
;
419 return Current
- Address
;
423 GspHex2MemGetContent(PVOID Context
, ULONG Offset
)
425 return (CHAR
)((HexValue(*((PCHAR
) Context
+ 2 * Offset
)) << 4) +
426 HexValue(*((PCHAR
) Context
+ 2 * Offset
+ 1)));
429 /* Convert the hex array pointed to by Buffer into binary to be placed at Address */
430 /* Return a pointer to the character AFTER the last byte read from Buffer */
432 GspHex2Mem(PCHAR Buffer
,
437 Count
= GspWriteMem(Address
, Count
, MayFault
, GspHex2MemGetContent
, Buffer
);
439 return Buffer
+ 2 * Count
;
443 GspWriteMemSafeGetContent(PVOID Context
, ULONG Offset
)
447 return *((PCHAR
) Context
);
451 GspWriteMemSafe(PCHAR Address
,
454 GspWriteMem(Address
, 1, TRUE
, GspWriteMemSafeGetContent
, &Ch
);
458 /* This function takes the 386 exception vector and attempts to
459 translate this number into a unix compatible signal value */
461 GspComputeSignal(NTSTATUS ExceptionCode
)
465 switch (ExceptionCode
)
467 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
468 SigVal
= 8; /* divide by zero */
470 case STATUS_SINGLE_STEP
:
471 case STATUS_BREAKPOINT
:
472 SigVal
= 5; /* breakpoint */
474 case STATUS_INTEGER_OVERFLOW
:
475 case STATUS_ARRAY_BOUNDS_EXCEEDED
:
476 SigVal
= 16; /* bound instruction */
478 case STATUS_ILLEGAL_INSTRUCTION
:
479 SigVal
= 4; /* Invalid opcode */
481 case STATUS_STACK_OVERFLOW
:
482 case STATUS_DATATYPE_MISALIGNMENT
:
483 case STATUS_ACCESS_VIOLATION
:
484 SigVal
= 11; /* access violation */
487 SigVal
= 7; /* "software generated" */
493 /**********************************************/
494 /* WHILE WE FIND NICE HEX CHARS, BUILD A LONG */
495 /* RETURN NUMBER OF CHARS PROCESSED */
496 /**********************************************/
498 GspHex2Long(PCHAR
*Address
,
508 Hex
= HexValue(**Address
);
511 *Value
= (*Value
<< 4) | Hex
;
527 GspLong2Hex(PCHAR
*Address
,
532 Save
= (((Value
>> 0) & 0xff) << 24) |
533 (((Value
>> 8) & 0xff) << 16) |
534 (((Value
>> 16) & 0xff) << 8) |
535 (((Value
>> 24) & 0xff) << 0);
536 *Address
= GspMem2Hex((PCHAR
) &Save
, *Address
, 4, FALSE
);
541 * When coming from kernel mode, Esp is not stored in the trap frame.
542 * Instead, it was pointing to the location of the TrapFrame Esp member
543 * when the exception occured. When coming from user mode, Esp is just
544 * stored in the TrapFrame Esp member.
547 GspGetEspFromTrapFrame(PKTRAP_FRAME TrapFrame
)
549 return KeGetPreviousMode() == KernelMode
550 ? (LONG
) &TrapFrame
->Gpr1
: (LONG
)TrapFrame
->Gpr1
;
555 GspGetRegisters(PCHAR Address
,
556 PKTRAP_FRAME TrapFrame
)
562 ULONG_PTR
*KernelStack
;
564 if (NULL
== GspDbgThread
)
566 Thread
= PsGetCurrentThread();
570 TrapFrame
= GspDbgThread
->Tcb
.TrapFrame
;
571 Thread
= GspDbgThread
;
574 if (Waiting
== Thread
->Tcb
.State
)
576 KernelStack
= Thread
->Tcb
.KernelStack
;
577 for (i
= 0; i
< sizeof(GspRegisters
) / sizeof(GspRegisters
[0]); i
++)
582 Address
= GspMem2Hex((PCHAR
) &Value
, Address
, GspRegisters
[i
].Size
,
588 for (i
= 0; i
< sizeof(GspRegisters
) / sizeof(GspRegisters
[0]); i
++)
594 Value
= GspGetEspFromTrapFrame(TrapFrame
);
598 p
= (PULONG
)((ULONG_PTR
) TrapFrame
+
599 GspRegisters
[i
].OffsetInTF
);
606 * This thread has not been sheduled yet so assume it
607 * is still in PsBeginThreadWithContextInternal().
609 Value
= (ULONG
)KiThreadStartup
;
615 Address
= GspMem2Hex((PCHAR
) &Value
, Address
,
616 GspRegisters
[i
].Size
, FALSE
);
623 GspSetRegistersInTrapFrame(PCHAR Address
,
625 PKTRAP_FRAME TrapFrame
)
638 for (i
= 0; i
< NUMREGS
; i
++)
640 if (GspRegisters
[i
].SetInContext
)
642 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[i
].OffsetInContext
);
646 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[i
].OffsetInTF
);
649 Buffer
= GspHex2Mem(Buffer
, (PCHAR
) &Value
, GspRegisters
[i
].Size
, FALSE
);
656 GspSetSingleRegisterInTrapFrame(PCHAR Address
,
659 PKTRAP_FRAME TrapFrame
)
669 if (GspRegisters
[Number
].SetInContext
)
671 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[Number
].OffsetInContext
);
675 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[Number
].OffsetInTF
);
678 GspHex2Mem(Address
, (PCHAR
) &Value
, GspRegisters
[Number
].Size
, FALSE
);
684 GspFindThread(PCHAR Data
,
687 PETHREAD ThreadInfo
= NULL
;
689 if (strcmp (Data
, "-1") == 0)
698 PCHAR ptr
= &Data
[0];
700 GspHex2Long(&ptr
, (PLONG
) &uThreadId
);
701 ThreadId
= (HANDLE
)uThreadId
;
703 if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId
, &ThreadInfo
)))
709 *Thread
= ThreadInfo
;
715 GspSetThread(PCHAR Request
)
718 PCHAR ptr
= &Request
[1];
722 case 'c': /* Run thread */
723 if (GspFindThread(ptr
, &ThreadInfo
))
725 GspOutBuffer
[0] = 'O';
726 GspOutBuffer
[1] = 'K';
728 if (NULL
!= GspRunThread
)
730 ObDereferenceObject(GspRunThread
);
732 GspRunThread
= ThreadInfo
;
733 if (NULL
!= GspRunThread
)
735 ObReferenceObject(GspRunThread
);
740 GspOutBuffer
[0] = 'E';
743 case 'g': /* Debug thread */
744 if (GspFindThread(ptr
, &ThreadInfo
))
746 GspOutBuffer
[0] = 'O';
747 GspOutBuffer
[1] = 'K';
749 if (NULL
!= GspDbgThread
)
751 ObDereferenceObject(GspDbgThread
);
754 if (ThreadInfo
== PsGetCurrentThread())
757 ObDereferenceObject(ThreadInfo
);
761 GspDbgThread
= ThreadInfo
;
766 GspOutBuffer
[0] = 'E';
776 GspQuery(PCHAR Request
)
780 if (strncmp(Request
, "C", 1) == 0)
782 PCHAR ptr
= &GspOutBuffer
[2];
784 /* Get current thread id */
785 GspOutBuffer
[0] = 'Q';
786 GspOutBuffer
[1] = 'C';
787 if (NULL
!= GspDbgThread
)
789 Value
= (ULONG
) GspDbgThread
->Cid
.UniqueThread
;
793 Value
= (ULONG
) PsGetCurrentThread()->Cid
.UniqueThread
;
795 GspLong2Hex(&ptr
, Value
);
797 else if (strncmp(Request
, "fThreadInfo", 11) == 0)
800 PLIST_ENTRY AThread
, AProcess
;
801 PCHAR ptr
= &GspOutBuffer
[1];
803 /* Get first thread id */
804 GspEnumThread
= NULL
;
805 AProcess
= PsActiveProcessHead
.Flink
;
806 while(AProcess
!= &PsActiveProcessHead
)
808 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
809 AThread
= Process
->ThreadListHead
.Flink
;
810 if (AThread
!= &Process
->ThreadListHead
)
812 GspEnumThread
= CONTAINING_RECORD(Process
->ThreadListHead
.Flink
,
813 ETHREAD
, ThreadListEntry
);
816 AProcess
= AProcess
->Flink
;
818 if(GspEnumThread
!= NULL
)
820 GspOutBuffer
[0] = 'm';
821 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
822 GspLong2Hex(&ptr
, Value
);
826 /* FIXME - what to do here? This case should never happen though, there
827 should always be at least one thread on the system... */
828 /* GspOutBuffer[0] = 'l'; */
831 else if (strncmp(Request
, "sThreadInfo", 11) == 0)
834 PLIST_ENTRY AThread
, AProcess
;
835 PCHAR ptr
= &GspOutBuffer
[1];
837 /* Get next thread id */
838 if (GspEnumThread
!= NULL
)
840 /* find the next thread */
841 Process
= GspEnumThread
->ThreadsProcess
;
842 if(GspEnumThread
->ThreadListEntry
.Flink
!= &Process
->ThreadListHead
)
844 GspEnumThread
= CONTAINING_RECORD(GspEnumThread
->ThreadListEntry
.Flink
,
845 ETHREAD
, ThreadListEntry
);
849 PETHREAD Thread
= NULL
;
850 AProcess
= Process
->ActiveProcessLinks
.Flink
;
851 while(AProcess
!= &PsActiveProcessHead
)
853 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
854 AThread
= Process
->ThreadListHead
.Flink
;
855 if (AThread
!= &Process
->ThreadListHead
)
857 Thread
= CONTAINING_RECORD(Process
->ThreadListHead
.Flink
,
858 ETHREAD
, ThreadListEntry
);
861 AProcess
= AProcess
->Flink
;
863 GspEnumThread
= Thread
;
866 if (GspEnumThread
!= NULL
)
869 GspOutBuffer
[0] = 'm';
870 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
871 GspLong2Hex(&ptr
, Value
);
875 GspOutBuffer
[0] = 'l';
880 GspOutBuffer
[0] = 'l';
883 else if (strncmp(Request
, "ThreadExtraInfo", 15) == 0)
887 /* Get thread information */
888 if (GspFindThread(Request
+ 16, &ThreadInfo
))
893 Proc
= (PEPROCESS
) ThreadInfo
->ThreadsProcess
;
898 sprintf(Buffer
, "%s [%d:0x%x], ", Proc
->ImageFileName
,
899 (int) Proc
->UniqueProcessId
,
900 (int) ThreadInfo
->Cid
.UniqueThread
);
902 strcpy(Buffer
+ strlen(Buffer
),
903 GspThreadStates
[ThreadInfo
->Tcb
.State
]);
905 ObDereferenceObject(ThreadInfo
);
907 GspMem2Hex(Buffer
, &GspOutBuffer
[0], strlen(Buffer
), FALSE
);
913 GspQueryThreadStatus(PCHAR Request
)
916 PCHAR ptr
= &Request
[0];
918 if (GspFindThread(ptr
, &ThreadInfo
))
920 ObDereferenceObject(ThreadInfo
);
922 GspOutBuffer
[0] = 'O';
923 GspOutBuffer
[1] = 'K';
924 GspOutBuffer
[2] = '\0';
928 GspOutBuffer
[0] = 'E';
929 GspOutBuffer
[1] = '\0';
933 #define DR7_L0 0x00000001 /* Local breakpoint 0 enable */
934 #define DR7_G0 0x00000002 /* Global breakpoint 0 enable */
935 #define DR7_L1 0x00000004 /* Local breakpoint 1 enable */
936 #define DR7_G1 0x00000008 /* Global breakpoint 1 enable */
937 #define DR7_L2 0x00000010 /* Local breakpoint 2 enable */
938 #define DR7_G2 0x00000020 /* Global breakpoint 2 enable */
939 #define DR7_L3 0x00000040 /* Local breakpoint 3 enable */
940 #define DR7_G3 0x00000080 /* Global breakpoint 3 enable */
941 #define DR7_LE 0x00000100 /* Local exact breakpoint enable (old) */
942 #define DR7_GE 0x00000200 /* Global exact breakpoint enable (old) */
943 #define DR7_GD 0x00002000 /* General detect enable */
944 #define DR7_TYPE0_MASK 0x00030000 /* Breakpoint 0 condition */
945 #define DR7_LEN0_MASK 0x000c0000 /* Breakpoint 0 length */
946 #define DR7_TYPE1_MASK 0x00300000 /* Breakpoint 1 condition */
947 #define DR7_LEN1_MASK 0x00c00000 /* Breakpoint 1 length */
948 #define DR7_TYPE2_MASK 0x03000000 /* Breakpoint 2 condition */
949 #define DR7_LEN2_MASK 0x0c000000 /* Breakpoint 2 length */
950 #define DR7_TYPE3_MASK 0x30000000 /* Breakpoint 3 condition */
951 #define DR7_LEN3_MASK 0xc0000000 /* Breakpoint 3 length */
952 #define DR7_GLOBAL_ENABLE(Bp) (2 << (2 * (Bp)))
953 #define DR7_TYPE(Bp, Type) ((Type) << (16 + 4 * (Bp)))
954 #define DR7_LEN(Bp, Len) ((Len) << (18 + 4 * (Bp)))
956 #define I386_BP_TYPE_EXECUTE 0
957 #define I386_BP_TYPE_DATA_WRITE 1
958 #define I386_BP_TYPE_DATA_READWRITE 3
960 #define I386_OPCODE_INT3 0xcc
962 #define GDB_ZTYPE_MEMORY_BREAKPOINT 0
963 #define GDB_ZTYPE_HARDWARE_BREAKPOINT 1
964 #define GDB_ZTYPE_WRITE_WATCHPOINT 2
965 #define GDB_ZTYPE_READ_WATCHPOINT 3
966 #define GDB_ZTYPE_ACCESS_WATCHPOINT 4
968 typedef struct _GSPHWBREAKPOINT
975 #define MAX_HW_BREAKPOINTS 4
976 static unsigned GspHwBreakpointCount
= 0;
977 static GSPHWBREAKPOINT GspHwBreakpoints
[MAX_HW_BREAKPOINTS
];
979 typedef struct _GSPSWBREAKPOINT
986 #define MAX_SW_BREAKPOINTS 64
987 static unsigned GspSwBreakpointCount
= 0;
988 static GSPSWBREAKPOINT GspSwBreakpoints
[MAX_SW_BREAKPOINTS
];
991 GspSetHwBreakpoint(ULONG Type
, ULONG_PTR Address
, ULONG Length
)
993 DPRINT("GspSetHwBreakpoint(%lu, 0x%p, %lu)\n", Type
, Address
, Length
);
995 if (GDB_ZTYPE_READ_WATCHPOINT
== Type
)
997 DPRINT1("Read watchpoint not supported\n");
998 strcpy(GspOutBuffer
, "E22");
1000 else if (GDB_ZTYPE_HARDWARE_BREAKPOINT
== Type
&& 1 != Length
)
1002 DPRINT1("Invalid length %lu for hardware breakpoint\n", Length
);
1003 strcpy(GspOutBuffer
, "E22");
1005 else if (1 != Length
&& 2 != Length
&& 4 != Length
)
1007 DPRINT1("Invalid length %lu for GDB Z type %lu\n", Length
, Type
);
1008 strcpy(GspOutBuffer
, "E22");
1010 else if (0 != (Address
& (Length
- 1)))
1012 DPRINT1("Invalid alignment for address 0x%p and length %d\n",
1014 strcpy(GspOutBuffer
, "E22");
1016 else if (MAX_HW_BREAKPOINTS
== GspHwBreakpointCount
)
1018 DPRINT1("Trying to set too many hardware breakpoints\n");
1019 strcpy(GspOutBuffer
, "E22");
1023 DPRINT("Stored at index %u\n", GspHwBreakpointCount
);
1024 GspHwBreakpoints
[GspHwBreakpointCount
].Type
= Type
;
1025 GspHwBreakpoints
[GspHwBreakpointCount
].Address
= Address
;
1026 GspHwBreakpoints
[GspHwBreakpointCount
].Length
= Length
;
1027 GspHwBreakpointCount
++;
1028 strcpy(GspOutBuffer
, "OK");
1033 GspRemoveHwBreakpoint(ULONG Type
, ULONG_PTR Address
, ULONG Length
)
1037 DPRINT("GspRemoveHwBreakpoint(%lu, 0x%p, %lu)\n", Type
, Address
, Length
);
1038 for (Index
= 0; Index
< GspHwBreakpointCount
; Index
++)
1040 if (GspHwBreakpoints
[Index
].Type
== Type
&&
1041 GspHwBreakpoints
[Index
].Address
== Address
&&
1042 GspHwBreakpoints
[Index
].Length
== Length
)
1044 DPRINT("Found match at index %u\n", Index
);
1045 if (Index
+ 1 < GspHwBreakpointCount
)
1047 memmove(GspHwBreakpoints
+ Index
,
1048 GspHwBreakpoints
+ (Index
+ 1),
1049 (GspHwBreakpointCount
- Index
- 1) *
1050 sizeof(GSPHWBREAKPOINT
));
1052 GspHwBreakpointCount
--;
1053 strcpy(GspOutBuffer
, "OK");
1058 DPRINT1("Not found\n");
1059 strcpy(GspOutBuffer
, "E22");
1063 GspSetSwBreakpoint(ULONG_PTR Address
)
1065 DPRINT("GspSetSwBreakpoint(0x%p)\n", Address
);
1067 if (MAX_SW_BREAKPOINTS
== GspSwBreakpointCount
)
1069 DPRINT1("Trying to set too many software breakpoints\n");
1070 strcpy(GspOutBuffer
, "E22");
1074 DPRINT("Stored at index %u\n", GspSwBreakpointCount
);
1075 GspSwBreakpoints
[GspSwBreakpointCount
].Address
= Address
;
1076 GspSwBreakpoints
[GspSwBreakpointCount
].Active
= FALSE
;
1077 GspSwBreakpointCount
++;
1078 strcpy(GspOutBuffer
, "OK");
1083 GspRemoveSwBreakpoint(ULONG_PTR Address
)
1087 DPRINT("GspRemoveSwBreakpoint(0x%p)\n", Address
);
1088 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1090 if (GspSwBreakpoints
[Index
].Address
== Address
)
1092 DPRINT("Found match at index %u\n", Index
);
1093 ASSERT(! GspSwBreakpoints
[Index
].Active
);
1094 if (Index
+ 1 < GspSwBreakpointCount
)
1096 memmove(GspSwBreakpoints
+ Index
,
1097 GspSwBreakpoints
+ (Index
+ 1),
1098 (GspSwBreakpointCount
- Index
- 1) *
1099 sizeof(GSPSWBREAKPOINT
));
1101 GspSwBreakpointCount
--;
1102 strcpy(GspOutBuffer
, "OK");
1107 DPRINT1("Not found\n");
1108 strcpy(GspOutBuffer
, "E22");
1112 GspLoadHwBreakpoint(PKTRAP_FRAME TrapFrame
,
1118 DPRINT("GspLoadHwBreakpoint(0x%p, %d, 0x%p, %d)\n", TrapFrame
, BpIndex
,
1121 /* Set the DR7_Gx bit to globally enable the breakpoint */
1122 TrapFrame
->Dr7
|= DR7_GLOBAL_ENABLE(BpIndex
) |
1123 DR7_LEN(BpIndex
, Length
) |
1124 DR7_TYPE(BpIndex
, Type
);
1129 DPRINT("Setting DR0 to 0x%p\n", Address
);
1130 TrapFrame
->Dr0
= Address
;
1134 DPRINT("Setting DR1 to 0x%p\n", Address
);
1135 TrapFrame
->Dr1
= Address
;
1139 DPRINT("Setting DR2 to 0x%p\n", Address
);
1140 TrapFrame
->Dr2
= Address
;
1144 DPRINT("Setting DR3 to 0x%p\n", Address
);
1145 TrapFrame
->Dr3
= Address
;
1151 GspLoadBreakpoints(PKTRAP_FRAME TrapFrame
)
1156 DPRINT("GspLoadBreakpoints\n");
1157 DPRINT("DR7 on entry: 0x%08x\n", TrapFrame
->Dr7
);
1158 /* Remove all breakpoints */
1159 TrapFrame
->Dr7
&= ~(DR7_L0
| DR7_L1
| DR7_L2
| DR7_L3
|
1160 DR7_G0
| DR7_G1
| DR7_G2
| DR7_G3
|
1161 DR7_TYPE0_MASK
| DR7_LEN0_MASK
|
1162 DR7_TYPE1_MASK
| DR7_LEN1_MASK
|
1163 DR7_TYPE2_MASK
| DR7_LEN2_MASK
|
1164 DR7_TYPE3_MASK
| DR7_LEN3_MASK
);
1166 for (Index
= 0; Index
< GspHwBreakpointCount
; Index
++)
1168 switch(GspHwBreakpoints
[Index
].Type
)
1170 case GDB_ZTYPE_HARDWARE_BREAKPOINT
:
1171 i386Type
= I386_BP_TYPE_EXECUTE
;
1173 case GDB_ZTYPE_WRITE_WATCHPOINT
:
1174 i386Type
= I386_BP_TYPE_DATA_WRITE
;
1176 case GDB_ZTYPE_ACCESS_WATCHPOINT
:
1177 i386Type
= I386_BP_TYPE_DATA_READWRITE
;
1181 i386Type
= I386_BP_TYPE_EXECUTE
;
1185 GspLoadHwBreakpoint(TrapFrame
, Index
, GspHwBreakpoints
[Index
].Address
,
1186 GspHwBreakpoints
[Index
].Length
- 1, i386Type
);
1189 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1191 if (GspHwBreakpointCount
+ Index
< MAX_HW_BREAKPOINTS
)
1193 DPRINT("Implementing software interrupt using hardware register\n");
1194 GspLoadHwBreakpoint(TrapFrame
, GspHwBreakpointCount
+ Index
,
1195 GspSwBreakpoints
[Index
].Address
, 0,
1196 I386_BP_TYPE_EXECUTE
);
1197 GspSwBreakpoints
[Index
].Active
= FALSE
;
1201 DPRINT("Using real software breakpoint\n");
1202 GspMemoryError
= FALSE
;
1203 GspSwBreakpoints
[Index
].PrevContent
= GspReadMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
);
1204 if (! GspMemoryError
)
1206 GspWriteMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
, I386_OPCODE_INT3
);
1208 GspSwBreakpoints
[Index
].Active
= ! GspMemoryError
;
1211 DPRINT1("Failed to set software breakpoint at 0x%p\n",
1212 GspSwBreakpoints
[Index
].Address
);
1216 DPRINT("Successfully set software breakpoint at 0x%p\n",
1217 GspSwBreakpoints
[Index
].Address
);
1218 DPRINT1("Successfully set software breakpoint at 0x%p\n", GspSwBreakpoints
[Index
].Address
);
1223 DPRINT("Final DR7 value 0x%08x\n", TrapFrame
->Dr7
);
1227 GspUnloadBreakpoints(PKTRAP_FRAME TrapFrame
)
1231 DPRINT("GspUnloadHwBreakpoints\n");
1233 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1235 if (GspSwBreakpoints
[Index
].Active
)
1237 GspMemoryError
= FALSE
;
1238 GspWriteMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
,
1239 GspSwBreakpoints
[Index
].PrevContent
);
1240 GspSwBreakpoints
[Index
].Active
= FALSE
;
1243 DPRINT1("Failed to remove software breakpoint from 0x%p\n",
1244 GspSwBreakpoints
[Index
].Address
);
1248 DPRINT("Successfully removed software breakpoint from 0x%p\n",
1249 GspSwBreakpoints
[Index
].Address
);
1255 static BOOLEAN gdb_attached_yet
= FALSE
;
1257 * This function does all command procesing for interfacing to gdb.
1261 KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord
,
1263 PKTRAP_FRAME TrapFrame
)
1272 /* FIXME: Stop on other CPUs too */
1274 if (STATUS_ACCESS_VIOLATION
== (NTSTATUS
) ExceptionRecord
->ExceptionCode
&&
1275 NULL
!= GspAccessLocation
&&
1276 (ULONG_PTR
) GspAccessLocation
==
1277 (ULONG_PTR
) ExceptionRecord
->ExceptionInformation
[1])
1279 GspAccessLocation
= NULL
;
1280 GspMemoryError
= TRUE
;
1281 //Context->Eip += 3;
1285 DPRINT("Thread %p entering stub\n", PsGetCurrentThread());
1286 /* Can only debug 1 thread at a time... */
1287 ExAcquireFastMutex(&GspLock
);
1288 DPRINT("Thread %p acquired mutex\n", PsGetCurrentThread());
1290 /* Disable hardware debugging while we are inside the stub */
1292 GspUnloadBreakpoints(TrapFrame
);
1294 /* Make sure we're debugging the current thread. */
1295 if (NULL
!= GspDbgThread
)
1297 DPRINT1("Internal error: entering stub with non-NULL GspDbgThread\n");
1298 ObDereferenceObject(GspDbgThread
);
1299 GspDbgThread
= NULL
;
1302 /* ugly hack to avoid attempting to send status at the very
1303 * beginning, right when GDB is trying to query the stub */
1304 if (gdb_attached_yet
)
1309 /* reply to host that an exception has occurred */
1310 SigVal
= GspComputeSignal(ExceptionRecord
->ExceptionCode
);
1314 *ptr
++ = 'T'; /* notify gdb with signo, PC, FP and SP */
1315 *ptr
++ = HexChars
[(SigVal
>> 4) & 0xf];
1316 *ptr
++ = HexChars
[SigVal
& 0xf];
1318 *ptr
++ = HexChars
[ESP
];
1321 Esp
= GspGetEspFromTrapFrame(TrapFrame
); /* SP */
1322 ptr
= GspMem2Hex((PCHAR
) &Esp
, ptr
, 4, 0);
1325 *ptr
++ = HexChars
[EBP
];
1327 //ptr = GspMem2Hex((PCHAR) &TrapFrame->Ebp, ptr, 4, 0); /* FP */
1330 *ptr
++ = HexChars
[PC
];
1332 //ptr = GspMem2Hex((PCHAR) &TrapFrame->Eip, ptr, 4, 0); /* PC */
1337 GspPutPacket(&GspOutBuffer
[0]);
1341 gdb_attached_yet
= 1;
1348 /* Zero the buffer now so we don't have to worry about the terminating zero character */
1349 memset(GspOutBuffer
, 0, sizeof(GspInBuffer
));
1350 ptr
= GspGetPacket();
1355 /* a little hack to send more complete status information */
1357 GspOutBuffer
[0] = 'S';
1358 GspOutBuffer
[1] = HexChars
[SigVal
>> 4];
1359 GspOutBuffer
[2] = HexChars
[SigVal
% 16];
1360 GspOutBuffer
[3] = 0;
1363 GspRemoteDebug
= !GspRemoteDebug
; /* toggle debug flag */
1365 case 'g': /* return the value of the CPU Registers */
1366 GspGetRegisters(GspOutBuffer
, TrapFrame
);
1368 case 'G': /* set the value of the CPU Registers - return OK */
1369 if (NULL
!= GspDbgThread
)
1371 GspSetRegistersInTrapFrame(ptr
, Context
, GspDbgThread
->Tcb
.TrapFrame
);
1375 GspSetRegistersInTrapFrame(ptr
, Context
, TrapFrame
);
1377 strcpy(GspOutBuffer
, "OK");
1379 case 'P': /* set the value of a single CPU register - return OK */
1383 if ((GspHex2Long(&ptr
, &Register
)) && (*ptr
++ == '='))
1385 if ((Register
>= 0) && (Register
< NUMREGS
))
1389 GspSetSingleRegisterInTrapFrame(ptr
, Register
,
1391 GspDbgThread
->Tcb
.TrapFrame
);
1395 GspSetSingleRegisterInTrapFrame(ptr
, Register
,
1396 Context
, TrapFrame
);
1398 strcpy(GspOutBuffer
, "OK");
1403 strcpy(GspOutBuffer
, "E01");
1407 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1409 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
1410 if (GspHex2Long(&ptr
, &Address
) &&
1412 GspHex2Long(&ptr
, &Length
))
1414 PEPROCESS DbgProcess
= NULL
;
1417 if (NULL
!= GspDbgThread
&&
1418 PsGetCurrentProcess() != GspDbgThread
->ThreadsProcess
)
1420 DbgProcess
= GspDbgThread
->ThreadsProcess
;
1421 KeAttachProcess(&DbgProcess
->Pcb
);
1423 GspMemoryError
= FALSE
;
1424 GspMem2Hex((PCHAR
) Address
, GspOutBuffer
, Length
, 1);
1425 if (NULL
!= DbgProcess
)
1431 strcpy(GspOutBuffer
, "E03");
1432 DPRINT("Fault during memory read\n");
1438 strcpy(GspOutBuffer
, "E01");
1442 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
1444 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
1445 if (GspHex2Long(&ptr
, &Address
))
1447 if (*(ptr
++) == ',' &&
1448 GspHex2Long(&ptr
, &Length
) &&
1451 PEPROCESS DbgProcess
= NULL
;
1453 if (NULL
!= GspDbgThread
&&
1454 PsGetCurrentProcess() != GspDbgThread
->ThreadsProcess
)
1456 DbgProcess
= GspDbgThread
->ThreadsProcess
;
1457 KeAttachProcess(&DbgProcess
->Pcb
);
1459 GspMemoryError
= FALSE
;
1460 GspHex2Mem(ptr
, (PCHAR
) Address
, Length
, TRUE
);
1461 if (NULL
!= DbgProcess
)
1467 strcpy(GspOutBuffer
, "E03");
1468 DPRINT("Fault during memory write\n");
1472 strcpy(GspOutBuffer
, "OK");
1480 strcpy(GspOutBuffer
, "E02");
1484 /* cAA..AA Continue at address AA..AA(optional) */
1485 /* sAA..AA Step one instruction from AA..AA(optional) */
1490 ULONG BreakpointNumber
;
1493 /* try to read optional parameter, pc unchanged if no parm */
1494 if (GspHex2Long (&ptr
, &Address
))
1496 //Context->Eip = Address;
1499 //NewPC = Context->Eip;
1501 /* clear the trace bit */
1502 //Context->EFlags &= 0xfffffeff;
1504 /* set the trace bit if we're Stepping */
1507 //Context->EFlags |= 0x100;
1510 // XXX arty load dr6
1511 if (!(dr6_
& 0x4000))
1513 for (BreakpointNumber
= 0; BreakpointNumber
< 4; ++BreakpointNumber
)
1515 if (dr6_
& (1 << BreakpointNumber
))
1517 if (GspHwBreakpoints
[BreakpointNumber
].Type
== 0)
1519 /* Set restore flag */
1520 //Context->EFlags |= 0x10000;
1526 GspLoadBreakpoints(TrapFrame
);
1528 if (NULL
!= GspDbgThread
)
1530 ObDereferenceObject(GspDbgThread
);
1531 GspDbgThread
= NULL
;
1534 DPRINT("Thread %p releasing mutex\n", PsGetCurrentThread());
1535 ExReleaseFastMutex(&GspLock
);
1536 DPRINT("Thread %p leaving stub\n", PsGetCurrentThread());
1541 case 'k': /* kill the program */
1542 strcpy(GspOutBuffer
, "OK");
1544 /* kill the program */
1546 case 'H': /* Set thread */
1550 case 'q': /* Query */
1554 case 'T': /* Query thread status */
1555 GspQueryThreadStatus(ptr
);
1564 GspHex2Long(&ptr
, &Type
);
1566 GspHex2Long(&ptr
, &Address
);
1568 GspHex2Long(&ptr
, &Length
);
1571 GspSetSwBreakpoint((ULONG_PTR
) Address
);
1575 GspSetHwBreakpoint(Type
, (ULONG_PTR
) Address
, Length
);
1586 GspHex2Long(&ptr
, &Type
);
1588 GspHex2Long(&ptr
, &Address
);
1590 GspHex2Long(&ptr
, &Length
);
1593 GspRemoveSwBreakpoint((ULONG_PTR
) Address
);
1597 GspRemoveHwBreakpoint(Type
, (ULONG_PTR
) Address
, Length
);
1606 /* reply to the request */
1607 GspPutPacket(GspOutBuffer
);
1614 if (NULL
!= GspDbgThread
)
1616 ObDereferenceObject(GspDbgThread
);
1617 GspDbgThread
= NULL
;
1626 GspBreakIn(PKINTERRUPT Interrupt
,
1627 PVOID ServiceContext
)
1629 PKTRAP_FRAME TrapFrame
;
1635 DPRINT("Break In\n");
1638 while (KdPortGetByteEx(&GdbPortInfo
, &Value
))
1651 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1653 TrapFrame
= PsGetCurrentThread()->Tcb
.TrapFrame
;
1655 KeTrapFrameToContext(TrapFrame
, NULL
, &Context
);
1657 KdpGdbEnterDebuggerException(NULL
, &Context
, TrapFrame
);
1659 KeContextToTrapFrame(&Context
, NULL
, TrapFrame
, Context
.ContextFlags
, KernelMode
);
1661 KeLowerIrql(OldIrql
);
1668 KdpGdbDebugPrint(PCH Message
, ULONG Length
)
1672 /* Initialize the GDB stub */
1675 KdpGdbStubInit(PKD_DISPATCH_TABLE WrapperTable
,
1678 if (!KdDebuggerEnabled
|| !KdpDebugMode
.Gdb
)
1685 ExInitializeFastMutex(&GspLock
);
1687 /* Write out the functions that we support for now */
1688 WrapperTable
->KdpInitRoutine
= KdpGdbStubInit
;
1689 WrapperTable
->KdpPrintRoutine
= KdpGdbDebugPrint
;
1690 WrapperTable
->KdpExceptionRoutine
= KdpGdbEnterDebuggerException
;
1692 /* Initialize the Port */
1693 KdPortInitializeEx(&GdbPortInfo
, 0, 0);
1695 KdpPort
= GdbPortInfo
.ComPort
;
1697 else if (BootPhase
== 1)
1699 GspInitialized
= TRUE
;
1701 GspRunThread
= NULL
;
1702 GspDbgThread
= NULL
;
1703 GspEnumThread
= NULL
;
1705 HalDisplayString("Waiting for GDB to attach\n");
1706 DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C
);
1708 else if (BootPhase
== 2)
1710 HalDisplayString("\n GDB debugging enabled\n\n");