1 /****************************************************************************
3 THIS SOFTWARE IS NOT COPYRIGHTED
5 HP offers the following for use in the public domain. HP makes no
6 warranty with regard to the software or it's performance and the
7 user accepts the software "AS IS" with all faults.
9 HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
10 TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
11 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 ****************************************************************************/
15 /****************************************************************************
16 * Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
18 * Module name: remcom.c $
20 * Date: 91/03/09 12:29:49 $
21 * Contributor: Lake Stevens Instrument Division$
23 * Description: low level support for gdb debugger. $
25 * Considerations: only works on target hardware $
27 * Written by: Glenn Engel $
28 * ModuleState: Experimental $
32 * Modified for 386 by Jim Kingdon, Cygnus Support.
33 * Modified for ReactOS by Casper S. Hornstrup <chorns@users.sourceforge.net>
35 * To enable debugger support, two things need to happen. One, setting
36 * up a routine so that it is in the exception path, is necessary in order
37 * to allow any breakpoints or error conditions to be properly intercepted
38 * and reported to gdb.
39 * Two, a breakpoint needs to be generated to begin communication.
41 * Because gdb will sometimes write to the stack area to execute function
42 * calls, this program cannot rely on using the supervisor stack so it
43 * uses it's own stack area.
47 * The following gdb commands are supported:
49 * command function Return value
51 * g return the value of the CPU Registers hex data or ENN
52 * G set the value of the CPU Registers OK or ENN
54 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
55 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
57 * c Resume at current address SNN ( signal NN)
58 * cAA..AA Continue at address AA..AA SNN
60 * s Step one instruction SNN
61 * sAA..AA Step one instruction from AA..AA SNN
65 * ? What was the last sigval ? SNN (signal NN)
67 * All commands and responses are sent with a packet which includes a
68 * Checksum. A packet consists of
70 * $<packet info>#<Checksum>.
73 * <packet info> :: <characters representing the command or response>
74 * <Checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
76 * When a packet is received, it is first acknowledged with either '+' or '-'.
77 * '+' indicates a successful transfer. '-' indicates a failed transfer.
82 * $m0,10#2a +$00010203040506070809101112131415#42
84 ****************************************************************************/
88 #include <internal/debug.h>
91 /************************************************************************/
92 /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
93 /* at least NUMREGBYTES*2 are needed for register packets */
96 static BOOLEAN GspInitialized
;
98 static PKINTERRUPT GspInterrupt
;
101 static BOOLEAN GspRemoteDebug
;
103 static CONST CHAR HexChars
[]="0123456789abcdef";
105 static PETHREAD GspRunThread
; /* NULL means run all threads */
106 static PETHREAD GspDbgThread
;
107 static PETHREAD GspEnumThread
;
109 extern LIST_ENTRY PsProcessListHead
;
111 /* Number of Registers. */
116 EAX
, ECX
, EDX
, EBX
, ESP
, EBP
, ESI
, EDI
,
117 PC
/* also known as eip */,
118 PS
/* also known as eflags */,
119 CS
, SS
, DS
, ES
, FS
, GS
122 typedef struct _CPU_REGISTER
126 DWORD OffsetInContext
;
127 BOOLEAN SetInContext
;
128 } CPU_REGISTER
, *PCPU_REGISTER
;
130 #define KTRAP_FRAME_X86 KTRAP_FRAME
134 static CPU_REGISTER GspRegisters
[NUMREGS
] =
136 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Eax
), FIELD_OFFSET (CONTEXT
, Eax
), TRUE
},
137 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Ecx
), FIELD_OFFSET (CONTEXT
, Ecx
), TRUE
},
138 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Edx
), FIELD_OFFSET (CONTEXT
, Edx
), FALSE
},
139 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Ebx
), FIELD_OFFSET (CONTEXT
, Ebx
), TRUE
},
140 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Esp
), FIELD_OFFSET (CONTEXT
, Esp
), TRUE
},
141 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Ebp
), FIELD_OFFSET (CONTEXT
, Ebp
), TRUE
},
142 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Esi
), FIELD_OFFSET (CONTEXT
, Esi
), TRUE
},
143 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Edi
), FIELD_OFFSET (CONTEXT
, Edi
), TRUE
},
144 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Eip
), FIELD_OFFSET (CONTEXT
, Eip
), TRUE
},
145 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Eflags
), FIELD_OFFSET (CONTEXT
, EFlags
), TRUE
},
146 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Cs
), FIELD_OFFSET (CONTEXT
, SegCs
), TRUE
},
147 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Ss
), FIELD_OFFSET (CONTEXT
, SegSs
), TRUE
},
148 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Ds
), FIELD_OFFSET (CONTEXT
, SegDs
), TRUE
},
149 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Es
), FIELD_OFFSET (CONTEXT
, SegEs
), TRUE
},
150 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Fs
), FIELD_OFFSET (CONTEXT
, SegFs
), TRUE
},
151 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Gs
), FIELD_OFFSET (CONTEXT
, SegGs
), TRUE
}
154 static PCHAR GspThreadStates
[THREAD_STATE_MAX
] =
156 "Initialized", /* THREAD_STATE_INITIALIZED */
157 "Ready", /* THREAD_STATE_READY */
158 "Running", /* THREAD_STATE_RUNNING */
159 "Suspended", /* THREAD_STATE_SUSPENDED */
160 "Frozen", /* THREAD_STATE_FROZEN */
161 "Terminated 1", /* THREAD_STATE_TERMINATED_1 */
162 "Terminated 2", /* THREAD_STATE_TERMINATED_2 */
163 "Blocked" /* THREAD_STATE_BLOCKED */
167 strtok(char *s
, const char *delim
)
175 if (s
== NULL
&& (s
= last
) == NULL
)
179 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
183 for (spanp
= delim
; (sc
= *spanp
++) != 0;) {
188 if (c
== 0) { /* no non-delimiter characters */
195 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
196 * Note that delim must have one NUL; we stop if we see that, too.
202 if ((sc
= *spanp
++) == c
) {
219 if ((ch
>= '0') && (ch
<= '9')) return (ch
- '0');
220 if ((ch
>= 'a') && (ch
<= 'f')) return (ch
- 'a' + 10);
221 if ((ch
>= 'A') && (ch
<= 'F')) return (ch
- 'A' + 10);
225 static CHAR GspInBuffer
[BUFMAX
];
226 static CHAR GspOutBuffer
[BUFMAX
];
228 /* scan for the sequence $<data>#<Checksum> */
233 PCHAR Buffer
= &GspInBuffer
[0];
241 /* wait around for the start character, ignore all other characters */
242 while ((ch
= KdGetChar ()) != '$');
249 /* now, read until a # or end of Buffer is found */
250 while (Count
< BUFMAX
)
257 Checksum
= Checksum
+ ch
;
266 XmitChecksum
= (CHAR
)(HexValue (ch
) << 4);
268 XmitChecksum
+= (CHAR
)(HexValue (ch
));
270 if (Checksum
!= XmitChecksum
)
272 KdPutChar ('-'); /* failed checksum */
276 KdPutChar ('+'); /* successful transfer */
278 /* if a sequence char is present, reply the sequence ID */
279 if (Buffer
[2] == ':')
281 KdPutChar (Buffer
[0]);
282 KdPutChar (Buffer
[1]);
293 /* send the packet in Buffer. */
296 GspPutPacket (PCHAR Buffer
)
302 /* $<packet info>#<Checksum>. */
309 while ((ch
= Buffer
[Count
]))
317 KdPutChar (HexChars
[(Checksum
>> 4) & 0xf]);
318 KdPutChar (HexChars
[Checksum
& 0xf]);
320 while (KdGetChar () != '+');
325 GspPutPacketNoWait (PCHAR Buffer
)
331 /* $<packet info>#<Checksum>. */
336 while ((ch
= Buffer
[Count
]))
344 KdPutChar (HexChars
[(Checksum
>> 4) & 0xf]);
345 KdPutChar (HexChars
[Checksum
& 0xf]);
348 /* Indicate to caller of GspMem2Hex or GspHex2Mem that there has been an
350 static volatile BOOLEAN GspMemoryError
= FALSE
;
351 static volatile void *GspAccessLocation
= NULL
;
354 /* Convert the memory pointed to by Address into hex, placing result in Buffer */
355 /* Return a pointer to the last char put in Buffer (null) */
356 /* If MayFault is TRUE, then we should set GspMemoryError in response to
357 a fault; if FALSE treat a fault like any other fault in the stub. */
359 GspMem2Hex (PCHAR Address
,
367 if (NULL
== Address
&& MayFault
)
369 GspMemoryError
= TRUE
;
373 for (i
= 0; i
< (ULONG
) Count
; i
++)
376 GspAccessLocation
= Address
;
378 GspAccessLocation
= NULL
;
379 if (MayFault
&& GspMemoryError
)
381 *Buffer
++ = HexChars
[(ch
>> 4) & 0xf];
382 *Buffer
++ = HexChars
[ch
& 0xf];
390 /* Convert the hex array pointed to by Buffer into binary to be placed at Address */
391 /* Return a pointer to the character AFTER the last byte read from Buffer */
393 GspHex2Mem (PCHAR Buffer
,
406 while ( current
< Address
+ Count
)
408 page
= (PCHAR
)PAGE_ROUND_DOWN (current
);
409 if (Address
+ Count
<= page
+ PAGE_SIZE
)
411 /* Fits in this page */
416 /* Flows into next page, handle only current page in this iteration */
417 countinpage
= PAGE_SIZE
- (Address
- page
);
421 oldprot
= MmGetPageProtect (NULL
, Address
);
422 MmSetPageProtect (NULL
, Address
, PAGE_EXECUTE_READWRITE
);
425 for (i
= 0; i
< countinpage
&& ! GspMemoryError
; i
++)
427 ch
= (CHAR
)(HexValue (*Buffer
++) << 4);
428 ch
= (CHAR
)(ch
+ HexValue (*Buffer
++));
430 GspAccessLocation
= Address
;
432 GspAccessLocation
= NULL
;
437 MmSetPageProtect (NULL
, page
, oldprot
);
447 /* This function takes the 386 exception vector and attempts to
448 translate this number into a unix compatible signal value */
450 GspComputeSignal (NTSTATUS ExceptionCode
)
454 switch (ExceptionCode
)
456 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
458 break; /* divide by zero */
459 case STATUS_SINGLE_STEP
:
460 /* debug exception */
461 case STATUS_BREAKPOINT
:
463 break; /* breakpoint */
464 case STATUS_INTEGER_OVERFLOW
:
465 /* into instruction (overflow) */
466 case STATUS_ARRAY_BOUNDS_EXCEEDED
:
468 break; /* bound instruction */
469 case STATUS_ILLEGAL_INSTRUCTION
:
471 break; /* Invalid opcode */
473 case STATUS_FLT_INVALID_OPERATION
:
475 break; /* coprocessor not available */
477 case STATUS_STACK_OVERFLOW
:
478 /* stack exception */
479 case STATUS_DATATYPE_MISALIGNMENT
:
481 case STATUS_ACCESS_VIOLATION
:
482 SigVal
= 11; /* access violation */
485 SigVal
= 7; /* "software generated" */
491 /**********************************************/
492 /* WHILE WE FIND NICE HEX CHARS, BUILD A LONG */
493 /* RETURN NUMBER OF CHARS PROCESSED */
494 /**********************************************/
496 GspHex2Long (PCHAR
*Address
,
506 Hex
= HexValue (**Address
);
509 *Value
= (*Value
<< 4) | Hex
;
523 GspLong2Hex (PCHAR
*Address
,
528 Save
= (((Value
>> 0) & 0xff) << 24) |
529 (((Value
>> 8) & 0xff) << 16) |
530 (((Value
>> 16) & 0xff) << 8) |
531 (((Value
>> 24) & 0xff) << 0);
532 *Address
= GspMem2Hex ((PCHAR
) &Save
, *Address
, 4, FALSE
);
537 * When coming from kernel mode, Esp is not stored in the trap frame.
538 * Instead, it was pointing to the location of the TrapFrame Esp member
539 * when the exception occured. When coming from user mode, Esp is just
540 * stored in the TrapFrame Esp member.
543 GspGetEspFromTrapFrame(PKTRAP_FRAME TrapFrame
)
546 return KeGetPreviousMode() == KernelMode
547 ? (LONG
) &TrapFrame
->Esp
: TrapFrame
->Esp
;
552 GspGetRegistersFromTrapFrame(PCHAR Address
,
554 PKTRAP_FRAME TrapFrame
)
562 for (i
= 0; i
< sizeof (GspRegisters
) / sizeof (GspRegisters
[0]); i
++)
568 Value
= GspGetEspFromTrapFrame (TrapFrame
);
572 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[i
].OffsetInTF
);
576 else if (i
== EIP_REGNO
)
579 * This thread has not been sheduled yet so assume it
580 * is still in PsBeginThreadWithContextInternal().
582 Value
= (ULONG
) PsBeginThreadWithContextInternal
;
588 Buffer
= GspMem2Hex ((PCHAR
) &Value
, Buffer
, GspRegisters
[i
].Size
, FALSE
);
594 GspSetRegistersInTrapFrame(PCHAR Address
,
596 PKTRAP_FRAME TrapFrame
)
607 for (i
= 0; i
< NUMREGS
; i
++)
609 if (GspRegisters
[i
].SetInContext
)
610 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[i
].OffsetInContext
);
612 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[i
].OffsetInTF
);
614 Buffer
= GspHex2Mem (Buffer
, (PCHAR
) &Value
, GspRegisters
[i
].Size
, FALSE
);
621 GspSetSingleRegisterInTrapFrame(PCHAR Address
,
624 PKTRAP_FRAME TrapFrame
)
632 if (GspRegisters
[Number
].SetInContext
)
633 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[Number
].OffsetInContext
);
635 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[Number
].OffsetInTF
);
637 GspHex2Mem (Address
, (PCHAR
) &Value
, GspRegisters
[Number
].Size
, FALSE
);
643 GspFindThread(PCHAR Data
,
646 PETHREAD ThreadInfo
= NULL
;
648 if (strcmp (Data
, "-1") == 0)
656 PCHAR ptr
= &Data
[0];
658 GspHex2Long (&ptr
, (PLONG
) &ThreadId
);
660 if (!NT_SUCCESS (PsLookupThreadByThreadId ((PVOID
) ThreadId
, &ThreadInfo
)))
666 *Thread
= ThreadInfo
;
672 GspSetThread(PCHAR Request
)
675 PCHAR ptr
= &Request
[1];
679 case 'c': /* Run thread */
680 if (GspFindThread (ptr
, &ThreadInfo
))
682 GspOutBuffer
[0] = 'O';
683 GspOutBuffer
[1] = 'K';
685 if(GspRunThread
) ObDereferenceObject(GspRunThread
);
687 GspRunThread
= ThreadInfo
;
688 if (GspRunThread
) ObReferenceObject(GspRunThread
);
692 GspOutBuffer
[0] = 'E';
695 case 'g': /* Debug thread */
696 if (GspFindThread (ptr
, &ThreadInfo
))
698 GspOutBuffer
[0] = 'O';
699 GspOutBuffer
[1] = 'K';
701 if(GspDbgThread
) ObDereferenceObject(GspDbgThread
);
703 GspDbgThread
= ThreadInfo
;
707 GspOutBuffer
[0] = 'E';
717 GspQuery(PCHAR Request
)
722 Command
= strtok (Request
, ",");
723 if (strncmp (Command
, "C", 1) == 0)
725 PCHAR ptr
= &GspOutBuffer
[2];
727 /* Get current thread id */
728 GspOutBuffer
[0] = 'Q';
729 GspOutBuffer
[1] = 'C';
730 if (NULL
!= GspDbgThread
)
732 Value
= (ULONG
) GspDbgThread
->Cid
.UniqueThread
;
736 Value
= (ULONG
) PsGetCurrentThread()->Cid
.UniqueThread
;
738 GspLong2Hex (&ptr
, Value
);
740 else if (strncmp (Command
, "fThreadInfo", 11) == 0)
743 PLIST_ENTRY AThread
, AProcess
;
744 PCHAR ptr
= &GspOutBuffer
[1];
746 /* Get first thread id */
747 GspEnumThread
= NULL
;
748 AProcess
= PsProcessListHead
.Flink
;
749 while(AProcess
!= &PsProcessListHead
)
751 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ProcessListEntry
);
752 AThread
= Process
->ThreadListHead
.Flink
;
753 if(AThread
!= &Process
->ThreadListHead
)
755 GspEnumThread
= CONTAINING_RECORD (Process
->ThreadListHead
.Flink
,
756 ETHREAD
, ThreadListEntry
);
759 AProcess
= AProcess
->Flink
;
761 if(GspEnumThread
!= NULL
)
763 GspOutBuffer
[0] = 'm';
764 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
765 GspLong2Hex (&ptr
, Value
);
769 /* FIXME - what to do here? This case should never happen though, there
770 should always be at least one thread on the system... */
771 /* GspOutBuffer[0] = 'l'; */
774 else if (strncmp (Command
, "sThreadInfo", 11) == 0)
777 PLIST_ENTRY AThread
, AProcess
;
778 PCHAR ptr
= &GspOutBuffer
[1];
780 /* Get next thread id */
781 if (GspEnumThread
!= NULL
)
783 /* find the next thread */
784 Process
= GspEnumThread
->ThreadsProcess
;
785 if(GspEnumThread
->ThreadListEntry
.Flink
!= &Process
->ThreadListHead
)
787 GspEnumThread
= CONTAINING_RECORD (GspEnumThread
->ThreadListEntry
.Flink
,
788 ETHREAD
, ThreadListEntry
);
792 PETHREAD Thread
= NULL
;
793 AProcess
= Process
->ProcessListEntry
.Flink
;
794 while(AProcess
!= &PsProcessListHead
)
796 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ProcessListEntry
);
797 AThread
= Process
->ThreadListHead
.Flink
;
798 if(AThread
!= &Process
->ThreadListHead
)
800 Thread
= CONTAINING_RECORD (Process
->ThreadListHead
.Flink
,
801 ETHREAD
, ThreadListEntry
);
804 AProcess
= AProcess
->Flink
;
806 GspEnumThread
= Thread
;
809 if(GspEnumThread
!= NULL
)
812 GspOutBuffer
[0] = 'm';
813 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
814 GspLong2Hex (&ptr
, Value
);
818 GspOutBuffer
[0] = 'l';
823 GspOutBuffer
[0] = 'l';
826 else if (strncmp (Command
, "ThreadExtraInfo", 15) == 0)
829 PCHAR ptr
= &Command
[15];
831 /* Get thread information */
832 if (GspFindThread (ptr
, &ThreadInfo
))
834 PCHAR String
= GspThreadStates
[ThreadInfo
->Tcb
.State
];
836 ObDereferenceObject(ThreadInfo
);
838 GspMem2Hex (String
, &GspOutBuffer
[0], strlen (String
), FALSE
);
842 else if (strncmp (Command
, "L", 1) == 0)
844 PLIST_ENTRY CurrentEntry
;
846 ULONG MaxThreads
= 0;
848 ULONG ThreadCount
= 0;
851 GspHex2Mem (&Request
[1], (PCHAR
) &MaxThreads
, 2, TRUE
);
852 GspHex2Mem (&Request
[3], (PCHAR
) &Value
, 4, TRUE
);
853 GspHex2Mem (&Request
[11], (PCHAR
) &ThreadId
, 4, TRUE
);
855 GspOutBuffer
[0] = 'q';
856 GspOutBuffer
[1] = 'M';
858 GspMem2Hex ((PCHAR
) &Value
, &GspOutBuffer
[5], 4, TRUE
);
859 GspMem2Hex ((PCHAR
) &ThreadId
, &GspOutBuffer
[13], 4, TRUE
);
861 CurrentEntry
= PiThreadListHead
.Flink
;
862 while ((CurrentEntry
!= &PiThreadListHead
) && (ThreadCount
< MaxThreads
))
864 Current
= CONTAINING_RECORD (CurrentEntry
, ETHREAD
, Tcb
.ThreadListEntry
);
866 GspMem2Hex ((PCHAR
) &Value
, &GspOutBuffer
[21+ThreadCount
*16], 4, TRUE
);
867 Value
= (ULONG
) Current
->Cid
.UniqueThread
;
868 GspMem2Hex ((PCHAR
) &Value
, &GspOutBuffer
[21+ThreadCount
*16+8], 4, TRUE
);
869 CurrentEntry
= CurrentEntry
->Flink
;
873 if (CurrentEntry
!= &PiThreadListHead
)
875 GspOutBuffer
[4] = '0';
879 GspOutBuffer
[4] = '1';
882 GspMem2Hex ((PCHAR
) &ThreadCount
, &GspOutBuffer
[2], 1, TRUE
);
885 else if (strncmp (Command
, "Offsets", 7) == 0)
887 strcpy (GspOutBuffer
, "Text=0;Data=0;Bss=0");
892 GspQueryThreadStatus(PCHAR Request
)
895 PCHAR ptr
= &Request
[0];
897 if (GspFindThread (ptr
, &ThreadInfo
))
899 ObDereferenceObject(ThreadInfo
);
901 GspOutBuffer
[0] = 'O';
902 GspOutBuffer
[1] = 'K';
903 GspOutBuffer
[2] = '\0';
907 GspOutBuffer
[0] = 'E';
908 GspOutBuffer
[1] = '\0';
913 typedef struct _GsHwBreakPoint
921 #if defined(__GNUC__)
922 GsHwBreakPoint GspBreakpoints
[4] =
930 GsHwBreakPoint GspBreakpoints
[4] =
940 GspCorrectHwBreakpoint()
942 ULONG BreakpointNumber
;
947 #if defined(__GNUC__)
949 "movl %%db7, %0\n" : "=r" (dr7_
) : );
952 ULONG addr0
, addr1
, addr2
, addr3
;
959 : "=r" (addr0
), "=r" (addr1
),
960 "=r" (addr2
), "=r" (addr3
) : );
962 #elif defined(_MSC_VER)
965 mov eax
, dr7
; mov dr7_
, eax
;
966 mov eax
, dr0
; mov addr0
, eax
;
967 mov eax
, dr1
; mov addr1
, eax
;
968 mov eax
, dr2
; mov addr2
, eax
;
969 mov eax
, dr3
; mov addr3
, eax
;
972 #error Unknown compiler for inline assembler
975 for (BreakpointNumber
= 0; BreakpointNumber
< 3; BreakpointNumber
++)
977 Bit
= 2 << (BreakpointNumber
<< 1);
978 if (!(dr7_
& Bit
) && GspBreakpoints
[BreakpointNumber
].Enabled
) {
981 dr7_
&= ~(0xf0000 << (BreakpointNumber
<< 2));
982 dr7_
|= (((GspBreakpoints
[BreakpointNumber
].Length
<< 2) |
983 GspBreakpoints
[BreakpointNumber
].Type
) << 16) << (BreakpointNumber
<< 2);
984 switch (BreakpointNumber
) {
985 #if defined(__GNUC__)
987 asm volatile ("movl %0, %%dr0\n"
988 : : "r" (GspBreakpoints
[BreakpointNumber
].Address
) );
992 asm volatile ("movl %0, %%dr1\n"
993 : : "r" (GspBreakpoints
[BreakpointNumber
].Address
) );
997 asm volatile ("movl %0, %%dr2\n"
998 : : "r" (GspBreakpoints
[BreakpointNumber
].Address
) );
1002 asm volatile ("movl %0, %%dr3\n"
1003 : : "r" (GspBreakpoints
[BreakpointNumber
].Address
) );
1005 #elif defined(_MSC_VER)
1008 ULONG addr
= GspBreakpoints
[BreakpointNumber
].Address
;
1009 __asm mov eax
, addr
;
1015 ULONG addr
= GspBreakpoints
[BreakpointNumber
].Address
;
1016 __asm mov eax
, addr
;
1022 ULONG addr
= GspBreakpoints
[BreakpointNumber
].Address
;
1023 __asm mov eax
, addr
;
1029 ULONG addr
= GspBreakpoints
[BreakpointNumber
].Address
;
1030 __asm mov eax
, addr
;
1035 #error Unknown compiler for inline assembler
1039 else if ((dr7_
& Bit
) && !GspBreakpoints
[BreakpointNumber
].Enabled
)
1043 dr7_
&= ~(0xf0000 << (BreakpointNumber
<< 2));
1048 #if defined(__GNUC__)
1049 asm volatile ( "movl %0, %%db7\n" : : "r" (dr7_
));
1050 #elif defined(_MSC_VER)
1051 __asm mov eax
, dr7_
;
1054 #error Unknown compiler for inline assembler
1060 GspRemoveHwBreakpoint(ULONG BreakpointNumber
)
1062 if (!GspBreakpoints
[BreakpointNumber
].Enabled
)
1066 GspBreakpoints
[BreakpointNumber
].Enabled
= 0;
1072 GspSetHwBreakpoint(ULONG BreakpointNumber
,
1077 if (GspBreakpoints
[BreakpointNumber
].Enabled
)
1081 GspBreakpoints
[BreakpointNumber
].Enabled
= TRUE
;
1082 GspBreakpoints
[BreakpointNumber
].Type
= Type
;
1083 GspBreakpoints
[BreakpointNumber
].Length
= Length
;
1084 GspBreakpoints
[BreakpointNumber
].Address
= Address
;
1090 * This function does all command procesing for interfacing to gdb.
1093 KdEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord
,
1095 PKTRAP_FRAME TrapFrame
)
1106 /* FIXME: Stop on other CPUs too */
1107 /* Disable hardware debugging while we are inside the stub */
1108 #if defined(__GNUC__)
1109 __asm__("movl %0,%%db7" : /* no output */ : "r" (0));
1110 #elif defined(_MSC_VER)
1111 __asm mov eax
, 0 __asm mov dr7
, eax
1113 #error Unknown compiler for inline assembler
1116 if (STATUS_ACCESS_VIOLATION
== (NTSTATUS
) ExceptionRecord
->ExceptionCode
&&
1117 NULL
!= GspAccessLocation
&&
1118 (ULONG_PTR
) GspAccessLocation
==
1119 (ULONG_PTR
) ExceptionRecord
->ExceptionInformation
[1])
1121 GspAccessLocation
= NULL
;
1122 GspMemoryError
= TRUE
;
1127 /* Don't switch threads */
1128 OldIrql
= KeGetCurrentIrql();
1129 if (OldIrql
< DISPATCH_LEVEL
)
1131 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
1134 /* Always use the current thread when entering the exception handler */
1135 if (NULL
!= GspDbgThread
)
1137 ObDereferenceObject(GspDbgThread
);
1138 GspDbgThread
= NULL
;
1141 /* reply to host that an exception has occurred */
1142 SigVal
= GspComputeSignal (ExceptionRecord
->ExceptionCode
);
1144 ptr
= &GspOutBuffer
[0];
1146 *ptr
++ = 'T'; /* notify gdb with signo, PC, FP and SP */
1147 *ptr
++ = HexChars
[(SigVal
>> 4) & 0xf];
1148 *ptr
++ = HexChars
[SigVal
& 0xf];
1150 *ptr
++ = HexChars
[ESP
];
1153 Esp
= GspGetEspFromTrapFrame (TrapFrame
); /* SP */
1154 ptr
= GspMem2Hex ((PCHAR
) &Esp
, ptr
, 4, 0);
1157 *ptr
++ = HexChars
[EBP
];
1159 ptr
= GspMem2Hex ((PCHAR
) &TrapFrame
->Ebp
, ptr
, 4, 0); /* FP */
1162 *ptr
++ = HexChars
[PC
];
1164 ptr
= GspMem2Hex((PCHAR
) &TrapFrame
->Eip
, ptr
, 4, 0); /* PC */
1169 GspPutPacket (&GspOutBuffer
[0]);
1175 /* Zero the buffer now so we don't have to worry about the terminating zero character */
1176 memset (GspOutBuffer
, 0, sizeof (GspInBuffer
));
1177 ptr
= GspGetPacket ();
1182 GspOutBuffer
[0] = 'S';
1183 GspOutBuffer
[1] = HexChars
[SigVal
>> 4];
1184 GspOutBuffer
[2] = HexChars
[SigVal
% 16];
1185 GspOutBuffer
[3] = 0;
1188 GspRemoteDebug
= !GspRemoteDebug
; /* toggle debug flag */
1190 case 'g': /* return the value of the CPU Registers */
1191 if (NULL
!= GspDbgThread
)
1193 GspGetRegistersFromTrapFrame (&GspOutBuffer
[0], Context
, GspDbgThread
->Tcb
.TrapFrame
);
1197 GspGetRegistersFromTrapFrame (&GspOutBuffer
[0], Context
, TrapFrame
);
1200 case 'G': /* set the value of the CPU Registers - return OK */
1201 if (NULL
!= GspDbgThread
)
1203 GspSetRegistersInTrapFrame (ptr
, Context
, GspDbgThread
->Tcb
.TrapFrame
);
1207 GspSetRegistersInTrapFrame (ptr
, Context
, TrapFrame
);
1209 strcpy (GspOutBuffer
, "OK");
1211 case 'P': /* set the value of a single CPU register - return OK */
1215 if ((GspHex2Long (&ptr
, &Register
)) && (*ptr
++ == '='))
1216 if ((Register
>= 0) && (Register
< NUMREGS
))
1220 GspSetSingleRegisterInTrapFrame(ptr
, Register
,
1221 Context
, GspDbgThread
->Tcb
.TrapFrame
);
1225 GspSetSingleRegisterInTrapFrame (ptr
, Register
, Context
, TrapFrame
);
1227 strcpy (GspOutBuffer
, "OK");
1231 strcpy (GspOutBuffer
, "E01");
1235 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1237 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
1238 if (GspHex2Long (&ptr
, &Address
))
1239 if (*(ptr
++) == ',')
1240 if (GspHex2Long (&ptr
, &Length
))
1243 GspMemoryError
= FALSE
;
1244 GspMem2Hex ((PCHAR
) Address
, GspOutBuffer
, Length
, 1);
1247 strcpy (GspOutBuffer
, "E03");
1248 DPRINT ("Fault during memory read\n");
1253 strcpy (GspOutBuffer
, "E01");
1256 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
1258 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
1259 if (GspHex2Long (&ptr
, &Address
))
1260 if (*(ptr
++) == ',')
1261 if (GspHex2Long (&ptr
, &Length
))
1262 if (*(ptr
++) == ':')
1264 GspMemoryError
= FALSE
;
1265 GspHex2Mem (ptr
, (PCHAR
) Address
, Length
, TRUE
);
1269 strcpy (GspOutBuffer
, "E03");
1270 DPRINT ("Fault during memory write\n");
1274 strcpy (GspOutBuffer
, "OK");
1280 strcpy (GspOutBuffer
, "E02");
1283 /* cAA..AA Continue at address AA..AA(optional) */
1284 /* sAA..AA Step one instruction from AA..AA(optional) */
1289 ULONG BreakpointNumber
;
1292 /* try to read optional parameter, pc unchanged if no parm */
1293 if (GspHex2Long (&ptr
, &Address
))
1294 Context
->Eip
= Address
;
1296 NewPC
= Context
->Eip
;
1298 /* clear the trace bit */
1299 Context
->EFlags
&= 0xfffffeff;
1301 /* set the trace bit if we're Stepping */
1303 Context
->EFlags
|= 0x100;
1305 #if defined(__GNUC__)
1306 asm volatile ("movl %%db6, %0\n" : "=r" (dr6_
) : );
1307 #elif defined(_MSC_VER)
1308 __asm mov eax
, dr6 __asm mov dr6_
, eax
;
1310 #error Unknown compiler for inline assembler
1312 if (!(dr6_
& 0x4000))
1314 for (BreakpointNumber
= 0; BreakpointNumber
< 4; ++BreakpointNumber
)
1316 if (dr6_
& (1 << BreakpointNumber
))
1318 if (GspBreakpoints
[BreakpointNumber
].Type
== 0)
1320 /* Set restore flag */
1321 Context
->EFlags
|= 0x10000;
1327 GspCorrectHwBreakpoint();
1328 #if defined(__GNUC__)
1329 asm volatile ("movl %0, %%db6\n" : : "r" (0));
1330 #elif defined(_MSC_VER)
1331 __asm mov eax
, 0 __asm mov dr6
, eax
;
1333 #error Unknown compiler for inline assembler
1335 if (OldIrql
< DISPATCH_LEVEL
)
1337 KeLowerIrql(OldIrql
);
1340 KeContextToTrapFrame(Context
, TrapFrame
);
1341 return ((SigVal
== 5) ? (kdContinue
) : (kdHandleException
));
1345 case 'k': /* kill the program */
1346 strcpy (GspOutBuffer
, "OK");
1348 /* kill the program */
1350 case 'H': /* Set thread */
1354 case 'q': /* Query */
1358 case 'T': /* Query thread status */
1359 GspQueryThreadStatus (ptr
);
1369 ptr
= &GspOutBuffer
[1];
1370 GspHex2Long (&ptr
, &Number
);
1372 GspHex2Long (&ptr
, &Type
);
1374 GspHex2Long (&ptr
, &Length
);
1376 GspHex2Long (&ptr
, &Address
);
1377 if (GspSetHwBreakpoint (Number
& 0x3, Type
& 0x3 , Length
& 0x3, Address
) == 0)
1379 strcpy (GspOutBuffer
, "OK");
1383 strcpy (GspOutBuffer
, "E");
1388 /* Remove hardware breakpoint */
1393 ptr
= &GspOutBuffer
[1];
1394 GspHex2Long(&ptr
, &Number
);
1395 if (GspRemoveHwBreakpoint (Number
& 0x3) == 0)
1397 strcpy (GspOutBuffer
, "OK");
1401 strcpy (GspOutBuffer
, "E");
1410 /* reply to the request */
1411 GspPutPacket (&GspOutBuffer
[0]);
1417 return kdHandleException
;
1423 GspBreakIn(PKINTERRUPT Interrupt
,
1424 PVOID ServiceContext
)
1426 PKTRAP_FRAME TrapFrame
;
1432 DPRINT ("Break In\n");
1435 while (KdPortGetByteEx (&GdbPortInfo
, &Value
))
1444 KeRaiseIrql (HIGH_LEVEL
, &OldIrql
);
1446 TrapFrame
= PsGetCurrentThread()->Tcb
.TrapFrame
;
1448 KeTrapFrameToContext (TrapFrame
, &Context
);
1450 KdEnterDebuggerException (NULL
, &Context
, TrapFrame
);
1452 KeContextToTrapFrame (&Context
, TrapFrame
);
1454 KeLowerIrql (OldIrql
);
1460 extern ULONG KdpPortIrq
;
1462 /* Initialize the GDB stub */
1464 KdGdbStubInit(ULONG Phase
)
1475 DbgPrint("Module 'hal.dll' loaded at 0x%.08x.\n", LdrHalBase
);
1477 GspInitialized
= TRUE
;
1478 GspRunThread
= PsGetCurrentThread();
1480 ObReferenceObject(GspRunThread
);
1482 /* GspDbgThread = PsGetCurrentThread(); */
1483 GspDbgThread
= NULL
;
1484 GspEnumThread
= NULL
;
1486 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C
);
1488 else if (Phase
== 1)
1491 /* Hook an interrupt handler to allow the debugger to break into
1493 MappedIrq
= HalGetInterruptVector (Internal
,
1500 Status
= IoConnectInterrupt(&GspInterrupt
,
1511 if (!NT_SUCCESS (Status
))
1513 DPRINT1("Could not connect to IRQ line %d (0x%x)\n",
1514 KdpPortIrq
, Status
);
1518 KdPortEnableInterrupts();
1520 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C
);
1527 KdGdbDebugPrint(LPSTR Message
)
1530 /* This can be quite annoying! */
1535 GspOutBuffer
[0] = 'O';
1536 GspOutBuffer
[1] = '\0';
1537 strcat (&GspOutBuffer
[0], Message
);
1538 Length
= strlen (Message
);
1539 GspOutBuffer
[2 + Length
] = '\n';
1540 GspOutBuffer
[3 + Length
] = '\0';
1541 GspPutPacketNoWait (&GspOutBuffer
[0]);
1544 HalDisplayString(Message
);
1549 extern LIST_ENTRY ModuleListHead
;
1554 PLIST_ENTRY CurrentEntry
;
1555 PMODULE_OBJECT Current
;
1562 CurrentEntry
= ModuleListHead
.Flink
;
1563 while (CurrentEntry
!= (&ModuleListHead
))
1565 Current
= CONTAINING_RECORD (CurrentEntry
, MODULE_OBJECT
, ListEntry
);
1567 DbgPrint ("Module %S Base 0x%.08x Length 0x%.08x\n",
1568 Current
->BaseName
.Buffer
, Current
->Base
, Current
->Length
);
1571 CurrentEntry
= CurrentEntry
->Flink
;
1574 DbgPrint ("%d modules listed\n", ModuleCount
);