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 /************************************************************************/
92 static BOOLEAN GspInitialized
;
93 static BOOLEAN GspRemoteDebug
;
95 static CONST CHAR HexChars
[]="0123456789abcdef";
97 static PETHREAD GspRunThread
; /* NULL means run all threads */
98 static PETHREAD GspDbgThread
;
99 static PETHREAD GspEnumThread
;
101 static FAST_MUTEX GspLock
;
103 extern LIST_ENTRY PsActiveProcessHead
;
105 /* FIXME hardcoded for COM2, 115200 baud */
106 ULONG GdbPortNumber
= DEFAULT_DEBUG_PORT
;
107 CPPORT GdbPortInfo
= {0, DEFAULT_DEBUG_BAUD_RATE
, 0};
109 /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
110 /* at least NUMREGBYTES*2 are needed for register packets */
112 static CHAR GspInBuffer
[BUFMAX
];
113 static CHAR GspOutBuffer
[BUFMAX
];
115 /* Number of Registers. */
120 EAX
, ECX
, EDX
, EBX
, ESP
, EBP
, ESI
, EDI
,
121 PC
/* also known as eip */,
122 PS
/* also known as eflags */,
123 CS
, SS
, DS
, ES
, FS
, GS
126 typedef struct _CPU_REGISTER
130 ULONG OffsetInContext
;
131 BOOLEAN SetInContext
;
132 } CPU_REGISTER
, *PCPU_REGISTER
;
134 static CPU_REGISTER GspRegisters
[NUMREGS
] =
138 static PCHAR GspThreadStates
[DeferredReady
+1] =
154 if ((ch
>= '0') && (ch
<= '9'))
157 if ((ch
>= 'a') && (ch
<= 'f'))
158 return (ch
- 'a' + 10);
160 if ((ch
>= 'A') && (ch
<= 'F'))
161 return (ch
- 'A' + 10);
167 GdbPutChar(UCHAR Value
)
169 KdPortPutByteEx(&GdbPortInfo
, Value
);
177 while (!KdPortGetByteEx(&GdbPortInfo
, &Value
)) ;
181 /* scan for the sequence $<data>#<Checksum> */
186 PCHAR Buffer
= &GspInBuffer
[0];
194 /* wait around for the start character, ignore all other characters */
195 while ((ch
= GdbGetChar ()) != '$') ;
202 /* now, read until a # or end of Buffer is found */
203 while (Count
< BUFMAX
)
212 Checksum
= Checksum
+ ch
;
221 XmitChecksum
= (CHAR
)(HexValue(ch
) << 4);
223 XmitChecksum
+= (CHAR
)(HexValue(ch
));
225 if (Checksum
!= XmitChecksum
)
227 GdbPutChar('-'); /* failed checksum */
231 GdbPutChar('+'); /* successful transfer */
238 /* send the packet in Buffer. */
241 GspPutPacket(PCHAR Buffer
)
247 /* $<packet info>#<Checksum>. */
254 while ((ch
= Buffer
[Count
]))
262 GdbPutChar(HexChars
[(Checksum
>> 4) & 0xf]);
263 GdbPutChar(HexChars
[Checksum
& 0xf]);
265 while (GdbGetChar() != '+');
270 GspPutPacketNoWait(PCHAR Buffer
)
276 /* $<packet info>#<Checksum>. */
281 while ((ch
= Buffer
[Count
]))
289 GdbPutChar(HexChars
[(Checksum
>> 4) & 0xf]);
290 GdbPutChar(HexChars
[Checksum
& 0xf]);
293 /* Indicate to caller of GspMem2Hex or GspHex2Mem that there has been an
295 static volatile BOOLEAN GspMemoryError
= FALSE
;
296 static volatile void *GspAccessLocation
= NULL
;
299 GspReadMemSafe(PCHAR Address
)
305 GspMemoryError
= TRUE
;
309 GspAccessLocation
= Address
;
311 GspAccessLocation
= NULL
;
317 GspWriteMemSafeGetContent(PVOID Context
, ULONG Offset
)
320 return *((PCHAR
) Context
);
324 GspWriteMemSafe(PCHAR Address
,
327 GspWriteMem(Address
, 1, TRUE
, GspWriteMemSafeGetContent
, &Ch
);
330 /* Convert the memory pointed to by Address into hex, placing result in Buffer */
331 /* Return a pointer to the last char put in Buffer (null) */
332 /* If MayFault is TRUE, then we should set GspMemoryError in response to
333 a fault; if FALSE treat a fault like any other fault in the stub. */
335 GspMem2Hex(PCHAR Address
,
343 for (i
= 0; i
< (ULONG
) Count
; i
++)
347 ch
= GspReadMemSafe(Address
);
355 *Buffer
++ = HexChars
[(ch
>> 4) & 0xf];
356 *Buffer
++ = HexChars
[ch
& 0xf];
365 GspWriteMem(PCHAR Address
,
368 CHAR (*GetContent
)(PVOID Context
, ULONG Offset
),
379 while (Current
< Address
+ Count
)
381 Page
= (PCHAR
)PAGE_ROUND_DOWN(Current
);
382 if (Address
+ Count
<= Page
+ PAGE_SIZE
)
384 /* Fits in this page */
389 /* Flows into next page, handle only current page in this iteration */
390 CountInPage
= PAGE_SIZE
- (Address
- Page
);
394 OldProt
= MmGetPageProtect(NULL
, Address
);
395 MmSetPageProtect(NULL
, Address
, PAGE_EXECUTE_READWRITE
);
398 for (i
= 0; i
< CountInPage
&& ! GspMemoryError
; i
++)
400 ch
= (*GetContent
)(Context
, Current
- Address
);
403 GspAccessLocation
= Current
;
408 GspAccessLocation
= NULL
;
414 MmSetPageProtect(NULL
, Page
, OldProt
);
416 return Current
- Address
;
420 return Current
- Address
;
424 GspHex2MemGetContent(PVOID Context
, ULONG Offset
)
426 return (CHAR
)((HexValue(*((PCHAR
) Context
+ 2 * Offset
)) << 4) +
427 HexValue(*((PCHAR
) Context
+ 2 * Offset
+ 1)));
430 /* Convert the hex array pointed to by Buffer into binary to be placed at Address */
431 /* Return a pointer to the character AFTER the last byte read from Buffer */
433 GspHex2Mem(PCHAR Buffer
,
438 Count
= GspWriteMem(Address
, Count
, MayFault
, GspHex2MemGetContent
, Buffer
);
439 return Buffer
+ 2 * Count
;
443 /* This function takes the 386 exception vector and attempts to
444 translate this number into a unix compatible signal value */
446 GspComputeSignal(NTSTATUS ExceptionCode
)
450 switch (ExceptionCode
)
452 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
453 SigVal
= 8; /* divide by zero */
456 case STATUS_SINGLE_STEP
:
457 case STATUS_BREAKPOINT
:
458 SigVal
= 5; /* breakpoint */
461 case STATUS_INTEGER_OVERFLOW
:
462 case STATUS_ARRAY_BOUNDS_EXCEEDED
:
463 SigVal
= 16; /* bound instruction */
466 case STATUS_ILLEGAL_INSTRUCTION
:
467 SigVal
= 4; /* Invalid opcode */
470 case STATUS_STACK_OVERFLOW
:
471 case STATUS_DATATYPE_MISALIGNMENT
:
472 case STATUS_ACCESS_VIOLATION
:
473 SigVal
= 11; /* access violation */
477 SigVal
= 7; /* "software generated" */
483 /**********************************************/
484 /* WHILE WE FIND NICE HEX CHARS, BUILD A LONG */
485 /* RETURN NUMBER OF CHARS PROCESSED */
486 /**********************************************/
488 GspHex2Long(PCHAR
*Address
,
498 Hex
= HexValue(**Address
);
501 *Value
= (*Value
<< 4) | Hex
;
517 GspLong2Hex(PCHAR
*Address
,
522 Save
= (((Value
>> 0) & 0xff) << 24) |
523 (((Value
>> 8) & 0xff) << 16) |
524 (((Value
>> 16) & 0xff) << 8) |
525 (((Value
>> 24) & 0xff) << 0);
526 *Address
= GspMem2Hex((PCHAR
) &Save
, *Address
, 4, FALSE
);
531 * When coming from kernel mode, Esp is not stored in the trap frame.
532 * Instead, it was pointing to the location of the TrapFrame Esp member
533 * when the exception occured. When coming from user mode, Esp is just
534 * stored in the TrapFrame Esp member.
537 GspGetEspFromTrapFrame(PKTRAP_FRAME TrapFrame
)
539 return KeGetPreviousMode() == KernelMode
540 ? (LONG
) &TrapFrame
->Gpr1
: (LONG
)TrapFrame
->Gpr1
;
545 GspGetRegisters(PCHAR Address
,
546 PKTRAP_FRAME TrapFrame
)
552 ULONG_PTR
*KernelStack
;
554 if (NULL
== GspDbgThread
)
556 Thread
= PsGetCurrentThread();
560 TrapFrame
= GspDbgThread
->Tcb
.TrapFrame
;
561 Thread
= GspDbgThread
;
564 if (Waiting
== Thread
->Tcb
.State
)
566 KernelStack
= Thread
->Tcb
.KernelStack
;
567 for (i
= 0; i
< sizeof(GspRegisters
) / sizeof(GspRegisters
[0]); i
++)
572 Address
= GspMem2Hex((PCHAR
) &Value
, Address
, GspRegisters
[i
].Size
,
578 for (i
= 0; i
< sizeof(GspRegisters
) / sizeof(GspRegisters
[0]); i
++)
584 Value
= GspGetEspFromTrapFrame(TrapFrame
);
588 p
= (PULONG
)((ULONG_PTR
) TrapFrame
+
589 GspRegisters
[i
].OffsetInTF
);
596 * This thread has not been sheduled yet so assume it
597 * is still in PsBeginThreadWithContextInternal().
599 Value
= (ULONG
)KiThreadStartup
;
605 Address
= GspMem2Hex((PCHAR
) &Value
, Address
,
606 GspRegisters
[i
].Size
, FALSE
);
613 GspSetRegistersInTrapFrame(PCHAR Address
,
615 PKTRAP_FRAME TrapFrame
)
628 for (i
= 0; i
< NUMREGS
; i
++)
630 if (GspRegisters
[i
].SetInContext
)
632 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[i
].OffsetInContext
);
636 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[i
].OffsetInTF
);
639 Buffer
= GspHex2Mem(Buffer
, (PCHAR
) &Value
, GspRegisters
[i
].Size
, FALSE
);
646 GspSetSingleRegisterInTrapFrame(PCHAR Address
,
649 PKTRAP_FRAME TrapFrame
)
659 if (GspRegisters
[Number
].SetInContext
)
661 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[Number
].OffsetInContext
);
665 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[Number
].OffsetInTF
);
668 GspHex2Mem(Address
, (PCHAR
) &Value
, GspRegisters
[Number
].Size
, FALSE
);
674 GspFindThread(PCHAR Data
,
677 PETHREAD ThreadInfo
= NULL
;
679 if (strcmp (Data
, "-1") == 0)
688 PCHAR ptr
= &Data
[0];
690 GspHex2Long(&ptr
, (PLONG
) &uThreadId
);
691 ThreadId
= (HANDLE
)uThreadId
;
693 if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId
, &ThreadInfo
)))
699 *Thread
= ThreadInfo
;
705 GspSetThread(PCHAR Request
)
708 PCHAR ptr
= &Request
[1];
712 case 'c': /* Run thread */
713 if (GspFindThread(ptr
, &ThreadInfo
))
715 GspOutBuffer
[0] = 'O';
716 GspOutBuffer
[1] = 'K';
718 if (NULL
!= GspRunThread
)
720 ObDereferenceObject(GspRunThread
);
722 GspRunThread
= ThreadInfo
;
723 if (NULL
!= GspRunThread
)
725 ObReferenceObject(GspRunThread
);
730 GspOutBuffer
[0] = 'E';
733 case 'g': /* Debug thread */
734 if (GspFindThread(ptr
, &ThreadInfo
))
736 GspOutBuffer
[0] = 'O';
737 GspOutBuffer
[1] = 'K';
739 if (NULL
!= GspDbgThread
)
741 ObDereferenceObject(GspDbgThread
);
744 if (ThreadInfo
== PsGetCurrentThread())
747 ObDereferenceObject(ThreadInfo
);
751 GspDbgThread
= ThreadInfo
;
756 GspOutBuffer
[0] = 'E';
766 GspQuery(PCHAR Request
)
770 if (strncmp(Request
, "C", 1) == 0)
772 PCHAR ptr
= &GspOutBuffer
[2];
774 /* Get current thread id */
775 GspOutBuffer
[0] = 'Q';
776 GspOutBuffer
[1] = 'C';
777 if (NULL
!= GspDbgThread
)
779 Value
= (ULONG
) GspDbgThread
->Cid
.UniqueThread
;
783 Value
= (ULONG
) PsGetCurrentThread()->Cid
.UniqueThread
;
785 GspLong2Hex(&ptr
, Value
);
787 else if (strncmp(Request
, "fThreadInfo", 11) == 0)
790 PLIST_ENTRY AThread
, AProcess
;
791 PCHAR ptr
= &GspOutBuffer
[1];
793 /* Get first thread id */
794 GspEnumThread
= NULL
;
795 AProcess
= PsActiveProcessHead
.Flink
;
796 while(AProcess
!= &PsActiveProcessHead
)
798 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
799 AThread
= Process
->ThreadListHead
.Flink
;
800 if (AThread
!= &Process
->ThreadListHead
)
802 GspEnumThread
= CONTAINING_RECORD(Process
->ThreadListHead
.Flink
,
803 ETHREAD
, ThreadListEntry
);
806 AProcess
= AProcess
->Flink
;
808 if(GspEnumThread
!= NULL
)
810 GspOutBuffer
[0] = 'm';
811 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
812 GspLong2Hex(&ptr
, Value
);
816 /* FIXME - what to do here? This case should never happen though, there
817 should always be at least one thread on the system... */
818 /* GspOutBuffer[0] = 'l'; */
821 else if (strncmp(Request
, "sThreadInfo", 11) == 0)
824 PLIST_ENTRY AThread
, AProcess
;
825 PCHAR ptr
= &GspOutBuffer
[1];
827 /* Get next thread id */
828 if (GspEnumThread
!= NULL
)
830 /* find the next thread */
831 Process
= GspEnumThread
->ThreadsProcess
;
832 if(GspEnumThread
->ThreadListEntry
.Flink
!= &Process
->ThreadListHead
)
834 GspEnumThread
= CONTAINING_RECORD(GspEnumThread
->ThreadListEntry
.Flink
,
835 ETHREAD
, ThreadListEntry
);
839 PETHREAD Thread
= NULL
;
840 AProcess
= Process
->ActiveProcessLinks
.Flink
;
841 while(AProcess
!= &PsActiveProcessHead
)
843 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
844 AThread
= Process
->ThreadListHead
.Flink
;
845 if (AThread
!= &Process
->ThreadListHead
)
847 Thread
= CONTAINING_RECORD(Process
->ThreadListHead
.Flink
,
848 ETHREAD
, ThreadListEntry
);
851 AProcess
= AProcess
->Flink
;
853 GspEnumThread
= Thread
;
856 if (GspEnumThread
!= NULL
)
859 GspOutBuffer
[0] = 'm';
860 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
861 GspLong2Hex(&ptr
, Value
);
865 GspOutBuffer
[0] = 'l';
870 GspOutBuffer
[0] = 'l';
873 else if (strncmp(Request
, "ThreadExtraInfo", 15) == 0)
877 /* Get thread information */
878 if (GspFindThread(Request
+ 16, &ThreadInfo
))
883 Proc
= (PEPROCESS
) ThreadInfo
->ThreadsProcess
;
888 sprintf(Buffer
, "%s [%d:0x%x], ", Proc
->ImageFileName
,
889 (int) Proc
->UniqueProcessId
,
890 (int) ThreadInfo
->Cid
.UniqueThread
);
892 strcpy(Buffer
+ strlen(Buffer
),
893 GspThreadStates
[ThreadInfo
->Tcb
.State
]);
895 ObDereferenceObject(ThreadInfo
);
897 GspMem2Hex(Buffer
, &GspOutBuffer
[0], strlen(Buffer
), FALSE
);
903 GspQueryThreadStatus(PCHAR Request
)
906 PCHAR ptr
= &Request
[0];
908 if (GspFindThread(ptr
, &ThreadInfo
))
910 ObDereferenceObject(ThreadInfo
);
912 GspOutBuffer
[0] = 'O';
913 GspOutBuffer
[1] = 'K';
914 GspOutBuffer
[2] = '\0';
918 GspOutBuffer
[0] = 'E';
919 GspOutBuffer
[1] = '\0';
923 #define DR7_L0 0x00000001 /* Local breakpoint 0 enable */
924 #define DR7_G0 0x00000002 /* Global breakpoint 0 enable */
925 #define DR7_L1 0x00000004 /* Local breakpoint 1 enable */
926 #define DR7_G1 0x00000008 /* Global breakpoint 1 enable */
927 #define DR7_L2 0x00000010 /* Local breakpoint 2 enable */
928 #define DR7_G2 0x00000020 /* Global breakpoint 2 enable */
929 #define DR7_L3 0x00000040 /* Local breakpoint 3 enable */
930 #define DR7_G3 0x00000080 /* Global breakpoint 3 enable */
931 #define DR7_LE 0x00000100 /* Local exact breakpoint enable (old) */
932 #define DR7_GE 0x00000200 /* Global exact breakpoint enable (old) */
933 #define DR7_GD 0x00002000 /* General detect enable */
934 #define DR7_TYPE0_MASK 0x00030000 /* Breakpoint 0 condition */
935 #define DR7_LEN0_MASK 0x000c0000 /* Breakpoint 0 length */
936 #define DR7_TYPE1_MASK 0x00300000 /* Breakpoint 1 condition */
937 #define DR7_LEN1_MASK 0x00c00000 /* Breakpoint 1 length */
938 #define DR7_TYPE2_MASK 0x03000000 /* Breakpoint 2 condition */
939 #define DR7_LEN2_MASK 0x0c000000 /* Breakpoint 2 length */
940 #define DR7_TYPE3_MASK 0x30000000 /* Breakpoint 3 condition */
941 #define DR7_LEN3_MASK 0xc0000000 /* Breakpoint 3 length */
942 #define DR7_GLOBAL_ENABLE(Bp) (2 << (2 * (Bp)))
943 #define DR7_TYPE(Bp, Type) ((Type) << (16 + 4 * (Bp)))
944 #define DR7_LEN(Bp, Len) ((Len) << (18 + 4 * (Bp)))
946 #define I386_BP_TYPE_EXECUTE 0
947 #define I386_BP_TYPE_DATA_WRITE 1
948 #define I386_BP_TYPE_DATA_READWRITE 3
950 #define I386_OPCODE_INT3 0xcc
952 #define GDB_ZTYPE_MEMORY_BREAKPOINT 0
953 #define GDB_ZTYPE_HARDWARE_BREAKPOINT 1
954 #define GDB_ZTYPE_WRITE_WATCHPOINT 2
955 #define GDB_ZTYPE_READ_WATCHPOINT 3
956 #define GDB_ZTYPE_ACCESS_WATCHPOINT 4
958 typedef struct _GSPHWBREAKPOINT
965 #define MAX_HW_BREAKPOINTS 4
966 static unsigned GspHwBreakpointCount
= 0;
967 static GSPHWBREAKPOINT GspHwBreakpoints
[MAX_HW_BREAKPOINTS
];
969 typedef struct _GSPSWBREAKPOINT
976 #define MAX_SW_BREAKPOINTS 64
977 static unsigned GspSwBreakpointCount
= 0;
978 static GSPSWBREAKPOINT GspSwBreakpoints
[MAX_SW_BREAKPOINTS
];
981 GspSetHwBreakpoint(ULONG Type
, ULONG_PTR Address
, ULONG Length
)
983 DPRINT("GspSetHwBreakpoint(%lu, 0x%p, %lu)\n", Type
, Address
, Length
);
985 if (GDB_ZTYPE_READ_WATCHPOINT
== Type
)
987 DPRINT1("Read watchpoint not supported\n");
988 strcpy(GspOutBuffer
, "E22");
990 else if (GDB_ZTYPE_HARDWARE_BREAKPOINT
== Type
&& 1 != Length
)
992 DPRINT1("Invalid length %lu for hardware breakpoint\n", Length
);
993 strcpy(GspOutBuffer
, "E22");
995 else if (1 != Length
&& 2 != Length
&& 4 != Length
)
997 DPRINT1("Invalid length %lu for GDB Z type %lu\n", Length
, Type
);
998 strcpy(GspOutBuffer
, "E22");
1000 else if (0 != (Address
& (Length
- 1)))
1002 DPRINT1("Invalid alignment for address 0x%p and length %d\n",
1004 strcpy(GspOutBuffer
, "E22");
1006 else if (MAX_HW_BREAKPOINTS
== GspHwBreakpointCount
)
1008 DPRINT1("Trying to set too many hardware breakpoints\n");
1009 strcpy(GspOutBuffer
, "E22");
1013 DPRINT("Stored at index %u\n", GspHwBreakpointCount
);
1014 GspHwBreakpoints
[GspHwBreakpointCount
].Type
= Type
;
1015 GspHwBreakpoints
[GspHwBreakpointCount
].Address
= Address
;
1016 GspHwBreakpoints
[GspHwBreakpointCount
].Length
= Length
;
1017 GspHwBreakpointCount
++;
1018 strcpy(GspOutBuffer
, "OK");
1023 GspRemoveHwBreakpoint(ULONG Type
, ULONG_PTR Address
, ULONG Length
)
1027 DPRINT("GspRemoveHwBreakpoint(%lu, 0x%p, %lu)\n", Type
, Address
, Length
);
1028 for (Index
= 0; Index
< GspHwBreakpointCount
; Index
++)
1030 if (GspHwBreakpoints
[Index
].Type
== Type
&&
1031 GspHwBreakpoints
[Index
].Address
== Address
&&
1032 GspHwBreakpoints
[Index
].Length
== Length
)
1034 DPRINT("Found match at index %u\n", Index
);
1035 if (Index
+ 1 < GspHwBreakpointCount
)
1037 memmove(GspHwBreakpoints
+ Index
,
1038 GspHwBreakpoints
+ (Index
+ 1),
1039 (GspHwBreakpointCount
- Index
- 1) *
1040 sizeof(GSPHWBREAKPOINT
));
1042 GspHwBreakpointCount
--;
1043 strcpy(GspOutBuffer
, "OK");
1048 DPRINT1("Not found\n");
1049 strcpy(GspOutBuffer
, "E22");
1053 GspSetSwBreakpoint(ULONG_PTR Address
)
1055 DPRINT("GspSetSwBreakpoint(0x%p)\n", Address
);
1057 if (MAX_SW_BREAKPOINTS
== GspSwBreakpointCount
)
1059 DPRINT1("Trying to set too many software breakpoints\n");
1060 strcpy(GspOutBuffer
, "E22");
1064 DPRINT("Stored at index %u\n", GspSwBreakpointCount
);
1065 GspSwBreakpoints
[GspSwBreakpointCount
].Address
= Address
;
1066 GspSwBreakpoints
[GspSwBreakpointCount
].Active
= FALSE
;
1067 GspSwBreakpointCount
++;
1068 strcpy(GspOutBuffer
, "OK");
1073 GspRemoveSwBreakpoint(ULONG_PTR Address
)
1077 DPRINT("GspRemoveSwBreakpoint(0x%p)\n", Address
);
1078 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1080 if (GspSwBreakpoints
[Index
].Address
== Address
)
1082 DPRINT("Found match at index %u\n", Index
);
1083 ASSERT(! GspSwBreakpoints
[Index
].Active
);
1084 if (Index
+ 1 < GspSwBreakpointCount
)
1086 memmove(GspSwBreakpoints
+ Index
,
1087 GspSwBreakpoints
+ (Index
+ 1),
1088 (GspSwBreakpointCount
- Index
- 1) *
1089 sizeof(GSPSWBREAKPOINT
));
1091 GspSwBreakpointCount
--;
1092 strcpy(GspOutBuffer
, "OK");
1097 DPRINT1("Not found\n");
1098 strcpy(GspOutBuffer
, "E22");
1102 GspLoadHwBreakpoint(PKTRAP_FRAME TrapFrame
,
1108 DPRINT("GspLoadHwBreakpoint(0x%p, %d, 0x%p, %d)\n", TrapFrame
, BpIndex
,
1111 /* Set the DR7_Gx bit to globally enable the breakpoint */
1112 TrapFrame
->Dr7
|= DR7_GLOBAL_ENABLE(BpIndex
) |
1113 DR7_LEN(BpIndex
, Length
) |
1114 DR7_TYPE(BpIndex
, Type
);
1119 DPRINT("Setting DR0 to 0x%p\n", Address
);
1120 TrapFrame
->Dr0
= Address
;
1124 DPRINT("Setting DR1 to 0x%p\n", Address
);
1125 TrapFrame
->Dr1
= Address
;
1129 DPRINT("Setting DR2 to 0x%p\n", Address
);
1130 TrapFrame
->Dr2
= Address
;
1134 DPRINT("Setting DR3 to 0x%p\n", Address
);
1135 TrapFrame
->Dr3
= Address
;
1141 GspLoadBreakpoints(PKTRAP_FRAME TrapFrame
)
1146 DPRINT("GspLoadBreakpoints\n");
1147 DPRINT("DR7 on entry: 0x%08x\n", TrapFrame
->Dr7
);
1148 /* Remove all breakpoints */
1149 TrapFrame
->Dr7
&= ~(DR7_L0
| DR7_L1
| DR7_L2
| DR7_L3
|
1150 DR7_G0
| DR7_G1
| DR7_G2
| DR7_G3
|
1151 DR7_TYPE0_MASK
| DR7_LEN0_MASK
|
1152 DR7_TYPE1_MASK
| DR7_LEN1_MASK
|
1153 DR7_TYPE2_MASK
| DR7_LEN2_MASK
|
1154 DR7_TYPE3_MASK
| DR7_LEN3_MASK
);
1156 for (Index
= 0; Index
< GspHwBreakpointCount
; Index
++)
1158 switch(GspHwBreakpoints
[Index
].Type
)
1160 case GDB_ZTYPE_HARDWARE_BREAKPOINT
:
1161 i386Type
= I386_BP_TYPE_EXECUTE
;
1163 case GDB_ZTYPE_WRITE_WATCHPOINT
:
1164 i386Type
= I386_BP_TYPE_DATA_WRITE
;
1166 case GDB_ZTYPE_ACCESS_WATCHPOINT
:
1167 i386Type
= I386_BP_TYPE_DATA_READWRITE
;
1171 i386Type
= I386_BP_TYPE_EXECUTE
;
1175 GspLoadHwBreakpoint(TrapFrame
, Index
, GspHwBreakpoints
[Index
].Address
,
1176 GspHwBreakpoints
[Index
].Length
- 1, i386Type
);
1179 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1181 if (GspHwBreakpointCount
+ Index
< MAX_HW_BREAKPOINTS
)
1183 DPRINT("Implementing software interrupt using hardware register\n");
1184 GspLoadHwBreakpoint(TrapFrame
, GspHwBreakpointCount
+ Index
,
1185 GspSwBreakpoints
[Index
].Address
, 0,
1186 I386_BP_TYPE_EXECUTE
);
1187 GspSwBreakpoints
[Index
].Active
= FALSE
;
1191 DPRINT("Using real software breakpoint\n");
1192 GspMemoryError
= FALSE
;
1193 GspSwBreakpoints
[Index
].PrevContent
= GspReadMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
);
1194 if (! GspMemoryError
)
1196 GspWriteMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
, I386_OPCODE_INT3
);
1198 GspSwBreakpoints
[Index
].Active
= ! GspMemoryError
;
1201 DPRINT1("Failed to set software breakpoint at 0x%p\n",
1202 GspSwBreakpoints
[Index
].Address
);
1206 DPRINT("Successfully set software breakpoint at 0x%p\n",
1207 GspSwBreakpoints
[Index
].Address
);
1208 DPRINT1("Successfully set software breakpoint at 0x%p\n", GspSwBreakpoints
[Index
].Address
);
1213 DPRINT("Final DR7 value 0x%08x\n", TrapFrame
->Dr7
);
1217 GspUnloadBreakpoints(PKTRAP_FRAME TrapFrame
)
1221 DPRINT("GspUnloadHwBreakpoints\n");
1223 for (Index
= 0; Index
< GspSwBreakpointCount
; Index
++)
1225 if (GspSwBreakpoints
[Index
].Active
)
1227 GspMemoryError
= FALSE
;
1228 GspWriteMemSafe((PCHAR
) GspSwBreakpoints
[Index
].Address
,
1229 GspSwBreakpoints
[Index
].PrevContent
);
1230 GspSwBreakpoints
[Index
].Active
= FALSE
;
1233 DPRINT1("Failed to remove software breakpoint from 0x%p\n",
1234 GspSwBreakpoints
[Index
].Address
);
1238 DPRINT("Successfully removed software breakpoint from 0x%p\n",
1239 GspSwBreakpoints
[Index
].Address
);
1245 static BOOLEAN gdb_attached_yet
= FALSE
;
1247 * This function does all command procesing for interfacing to gdb.
1251 KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord
,
1253 PKTRAP_FRAME TrapFrame
)
1262 /* FIXME: Stop on other CPUs too */
1264 if (STATUS_ACCESS_VIOLATION
== (NTSTATUS
) ExceptionRecord
->ExceptionCode
&&
1265 NULL
!= GspAccessLocation
&&
1266 (ULONG_PTR
) GspAccessLocation
==
1267 (ULONG_PTR
) ExceptionRecord
->ExceptionInformation
[1])
1269 GspAccessLocation
= NULL
;
1270 GspMemoryError
= TRUE
;
1271 //Context->Eip += 3;
1275 DPRINT("Thread %p entering stub\n", PsGetCurrentThread());
1276 /* Can only debug 1 thread at a time... */
1277 ExAcquireFastMutex(&GspLock
);
1278 DPRINT("Thread %p acquired mutex\n", PsGetCurrentThread());
1280 /* Disable hardware debugging while we are inside the stub */
1282 GspUnloadBreakpoints(TrapFrame
);
1284 /* Make sure we're debugging the current thread. */
1285 if (NULL
!= GspDbgThread
)
1287 DPRINT1("Internal error: entering stub with non-NULL GspDbgThread\n");
1288 ObDereferenceObject(GspDbgThread
);
1289 GspDbgThread
= NULL
;
1292 /* ugly hack to avoid attempting to send status at the very
1293 * beginning, right when GDB is trying to query the stub */
1294 if (gdb_attached_yet
)
1299 /* reply to host that an exception has occurred */
1300 SigVal
= GspComputeSignal(ExceptionRecord
->ExceptionCode
);
1304 *ptr
++ = 'T'; /* notify gdb with signo, PC, FP and SP */
1305 *ptr
++ = HexChars
[(SigVal
>> 4) & 0xf];
1306 *ptr
++ = HexChars
[SigVal
& 0xf];
1308 *ptr
++ = HexChars
[ESP
];
1311 Esp
= GspGetEspFromTrapFrame(TrapFrame
); /* SP */
1312 ptr
= GspMem2Hex((PCHAR
) &Esp
, ptr
, 4, 0);
1315 *ptr
++ = HexChars
[EBP
];
1317 //ptr = GspMem2Hex((PCHAR) &TrapFrame->Ebp, ptr, 4, 0); /* FP */
1320 *ptr
++ = HexChars
[PC
];
1322 //ptr = GspMem2Hex((PCHAR) &TrapFrame->Eip, ptr, 4, 0); /* PC */
1327 GspPutPacket(&GspOutBuffer
[0]);
1331 gdb_attached_yet
= 1;
1338 /* Zero the buffer now so we don't have to worry about the terminating zero character */
1339 memset(GspOutBuffer
, 0, sizeof(GspInBuffer
));
1340 ptr
= GspGetPacket();
1345 /* a little hack to send more complete status information */
1347 GspOutBuffer
[0] = 'S';
1348 GspOutBuffer
[1] = HexChars
[SigVal
>> 4];
1349 GspOutBuffer
[2] = HexChars
[SigVal
% 16];
1350 GspOutBuffer
[3] = 0;
1353 GspRemoteDebug
= !GspRemoteDebug
; /* toggle debug flag */
1355 case 'g': /* return the value of the CPU Registers */
1356 GspGetRegisters(GspOutBuffer
, TrapFrame
);
1358 case 'G': /* set the value of the CPU Registers - return OK */
1359 if (NULL
!= GspDbgThread
)
1361 GspSetRegistersInTrapFrame(ptr
, Context
, GspDbgThread
->Tcb
.TrapFrame
);
1365 GspSetRegistersInTrapFrame(ptr
, Context
, TrapFrame
);
1367 strcpy(GspOutBuffer
, "OK");
1369 case 'P': /* set the value of a single CPU register - return OK */
1373 if ((GspHex2Long(&ptr
, &Register
)) && (*ptr
++ == '='))
1375 if ((Register
>= 0) && (Register
< NUMREGS
))
1379 GspSetSingleRegisterInTrapFrame(ptr
, Register
,
1381 GspDbgThread
->Tcb
.TrapFrame
);
1385 GspSetSingleRegisterInTrapFrame(ptr
, Register
,
1386 Context
, TrapFrame
);
1388 strcpy(GspOutBuffer
, "OK");
1393 strcpy(GspOutBuffer
, "E01");
1397 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1399 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
1400 if (GspHex2Long(&ptr
, &Address
) &&
1402 GspHex2Long(&ptr
, &Length
))
1404 PEPROCESS DbgProcess
= NULL
;
1407 if (NULL
!= GspDbgThread
&&
1408 PsGetCurrentProcess() != GspDbgThread
->ThreadsProcess
)
1410 DbgProcess
= GspDbgThread
->ThreadsProcess
;
1411 KeAttachProcess(&DbgProcess
->Pcb
);
1413 GspMemoryError
= FALSE
;
1414 GspMem2Hex((PCHAR
) Address
, GspOutBuffer
, Length
, 1);
1415 if (NULL
!= DbgProcess
)
1421 strcpy(GspOutBuffer
, "E03");
1422 DPRINT("Fault during memory read\n");
1428 strcpy(GspOutBuffer
, "E01");
1432 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
1434 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
1435 if (GspHex2Long(&ptr
, &Address
))
1437 if (*(ptr
++) == ',' &&
1438 GspHex2Long(&ptr
, &Length
) &&
1441 PEPROCESS DbgProcess
= NULL
;
1443 if (NULL
!= GspDbgThread
&&
1444 PsGetCurrentProcess() != GspDbgThread
->ThreadsProcess
)
1446 DbgProcess
= GspDbgThread
->ThreadsProcess
;
1447 KeAttachProcess(&DbgProcess
->Pcb
);
1449 GspMemoryError
= FALSE
;
1450 GspHex2Mem(ptr
, (PCHAR
) Address
, Length
, TRUE
);
1451 if (NULL
!= DbgProcess
)
1457 strcpy(GspOutBuffer
, "E03");
1458 DPRINT("Fault during memory write\n");
1462 strcpy(GspOutBuffer
, "OK");
1470 strcpy(GspOutBuffer
, "E02");
1474 /* cAA..AA Continue at address AA..AA(optional) */
1475 /* sAA..AA Step one instruction from AA..AA(optional) */
1480 ULONG BreakpointNumber
;
1483 /* try to read optional parameter, pc unchanged if no parm */
1484 if (GspHex2Long (&ptr
, &Address
))
1486 //Context->Eip = Address;
1489 //NewPC = Context->Eip;
1491 /* clear the trace bit */
1492 //Context->EFlags &= 0xfffffeff;
1494 /* set the trace bit if we're Stepping */
1497 //Context->EFlags |= 0x100;
1500 // XXX arty load dr6
1501 if (!(dr6_
& 0x4000))
1503 for (BreakpointNumber
= 0; BreakpointNumber
< 4; ++BreakpointNumber
)
1505 if (dr6_
& (1 << BreakpointNumber
))
1507 if (GspHwBreakpoints
[BreakpointNumber
].Type
== 0)
1509 /* Set restore flag */
1510 //Context->EFlags |= 0x10000;
1516 GspLoadBreakpoints(TrapFrame
);
1518 if (NULL
!= GspDbgThread
)
1520 ObDereferenceObject(GspDbgThread
);
1521 GspDbgThread
= NULL
;
1524 DPRINT("Thread %p releasing mutex\n", PsGetCurrentThread());
1525 ExReleaseFastMutex(&GspLock
);
1526 DPRINT("Thread %p leaving stub\n", PsGetCurrentThread());
1531 case 'k': /* kill the program */
1532 strcpy(GspOutBuffer
, "OK");
1534 /* kill the program */
1536 case 'H': /* Set thread */
1540 case 'q': /* Query */
1544 case 'T': /* Query thread status */
1545 GspQueryThreadStatus(ptr
);
1554 GspHex2Long(&ptr
, &Type
);
1556 GspHex2Long(&ptr
, &Address
);
1558 GspHex2Long(&ptr
, &Length
);
1561 GspSetSwBreakpoint((ULONG_PTR
) Address
);
1565 GspSetHwBreakpoint(Type
, (ULONG_PTR
) Address
, Length
);
1576 GspHex2Long(&ptr
, &Type
);
1578 GspHex2Long(&ptr
, &Address
);
1580 GspHex2Long(&ptr
, &Length
);
1583 GspRemoveSwBreakpoint((ULONG_PTR
) Address
);
1587 GspRemoveHwBreakpoint(Type
, (ULONG_PTR
) Address
, Length
);
1596 /* reply to the request */
1597 GspPutPacket(GspOutBuffer
);
1604 if (NULL
!= GspDbgThread
)
1606 ObDereferenceObject(GspDbgThread
);
1607 GspDbgThread
= NULL
;
1616 GspBreakIn(PKINTERRUPT Interrupt
,
1617 PVOID ServiceContext
)
1619 PKTRAP_FRAME TrapFrame
;
1625 DPRINT("Break In\n");
1628 while (KdPortGetByteEx(&GdbPortInfo
, &Value
))
1637 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
1639 TrapFrame
= PsGetCurrentThread()->Tcb
.TrapFrame
;
1641 KeTrapFrameToContext(TrapFrame
, NULL
, &Context
);
1643 KdpGdbEnterDebuggerException(NULL
, &Context
, TrapFrame
);
1645 KeContextToTrapFrame(&Context
, NULL
, TrapFrame
, Context
.ContextFlags
, KernelMode
);
1647 KeLowerIrql(OldIrql
);
1654 KdpGdbDebugPrint(PCH Message
, ULONG Length
)
1658 /* Initialize the GDB stub */
1661 KdpGdbStubInit(PKD_DISPATCH_TABLE WrapperTable
,
1664 if (!KdDebuggerEnabled
|| !KdpDebugMode
.Gdb
)
1669 ExInitializeFastMutex(&GspLock
);
1671 /* Write out the functions that we support for now */
1672 WrapperTable
->KdpInitRoutine
= KdpGdbStubInit
;
1673 WrapperTable
->KdpPrintRoutine
= KdpGdbDebugPrint
;
1674 WrapperTable
->KdpExceptionRoutine
= KdpGdbEnterDebuggerException
;
1676 /* Initialize the Port */
1677 KdPortInitializeEx(&GdbPortInfo
, GdbPortNumber
);
1678 // KdpPort = GdbPortInfo.ComPort;
1680 else if (BootPhase
== 1)
1682 GspInitialized
= TRUE
;
1684 GspRunThread
= NULL
;
1685 GspDbgThread
= NULL
;
1686 GspEnumThread
= NULL
;
1688 HalDisplayString("Waiting for GDB to attach\r\n");
1689 DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C
);
1691 else if (BootPhase
== 2)
1693 HalDisplayString("\r\n GDB debugging enabled\r\n\r\n");