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>
89 #include <internal/ps.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 PsActiveProcessHead
;
110 KD_PORT_INFORMATION GdbPortInfo
= { 2, 115200, 0 }; /* FIXME hardcoded for COM2, 115200 baud */
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 DWORD OffsetInContext
;
128 BOOLEAN SetInContext
;
129 } CPU_REGISTER
, *PCPU_REGISTER
;
131 #define KTRAP_FRAME_X86 KTRAP_FRAME
135 static CPU_REGISTER GspRegisters
[NUMREGS
] =
137 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Eax
), FIELD_OFFSET (CONTEXT
, Eax
), TRUE
},
138 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Ecx
), FIELD_OFFSET (CONTEXT
, Ecx
), TRUE
},
139 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Edx
), FIELD_OFFSET (CONTEXT
, Edx
), FALSE
},
140 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Ebx
), FIELD_OFFSET (CONTEXT
, Ebx
), TRUE
},
141 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Esp
), FIELD_OFFSET (CONTEXT
, Esp
), TRUE
},
142 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Ebp
), FIELD_OFFSET (CONTEXT
, Ebp
), TRUE
},
143 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Esi
), FIELD_OFFSET (CONTEXT
, Esi
), TRUE
},
144 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Edi
), FIELD_OFFSET (CONTEXT
, Edi
), TRUE
},
145 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Eip
), FIELD_OFFSET (CONTEXT
, Eip
), TRUE
},
146 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Eflags
), FIELD_OFFSET (CONTEXT
, EFlags
), TRUE
},
147 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Cs
), FIELD_OFFSET (CONTEXT
, SegCs
), TRUE
},
148 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Ss
), FIELD_OFFSET (CONTEXT
, SegSs
), TRUE
},
149 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Ds
), FIELD_OFFSET (CONTEXT
, SegDs
), TRUE
},
150 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Es
), FIELD_OFFSET (CONTEXT
, SegEs
), TRUE
},
151 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Fs
), FIELD_OFFSET (CONTEXT
, SegFs
), TRUE
},
152 { 4, FIELD_OFFSET (KTRAP_FRAME_X86
, Gs
), FIELD_OFFSET (CONTEXT
, SegGs
), TRUE
}
155 static PCHAR GspThreadStates
[DeferredReady
+1] =
170 if ((ch
>= '0') && (ch
<= '9')) return (ch
- '0');
171 if ((ch
>= 'a') && (ch
<= 'f')) return (ch
- 'a' + 10);
172 if ((ch
>= 'A') && (ch
<= 'F')) return (ch
- 'A' + 10);
176 static CHAR GspInBuffer
[BUFMAX
];
177 static CHAR GspOutBuffer
[BUFMAX
];
180 GdbPutChar(UCHAR Value
)
182 KdPortPutByteEx (&GdbPortInfo
, Value
);
190 while (!KdPortGetByteEx (&GdbPortInfo
, &Value
));
195 /* scan for the sequence $<data>#<Checksum> */
200 PCHAR Buffer
= &GspInBuffer
[0];
208 /* wait around for the start character, ignore all other characters */
209 while ((ch
= GdbGetChar ()) != '$');
216 /* now, read until a # or end of Buffer is found */
217 while (Count
< BUFMAX
)
224 Checksum
= Checksum
+ ch
;
233 XmitChecksum
= (CHAR
)(HexValue (ch
) << 4);
235 XmitChecksum
+= (CHAR
)(HexValue (ch
));
237 if (Checksum
!= XmitChecksum
)
239 GdbPutChar ('-'); /* failed checksum */
243 GdbPutChar ('+'); /* successful transfer */
245 /* if a sequence char is present, reply the sequence ID */
246 if (Buffer
[2] == ':')
248 GdbPutChar (Buffer
[0]);
249 GdbPutChar (Buffer
[1]);
260 /* send the packet in Buffer. */
263 GspPutPacket (PCHAR Buffer
)
269 /* $<packet info>#<Checksum>. */
276 while ((ch
= Buffer
[Count
]))
284 GdbPutChar (HexChars
[(Checksum
>> 4) & 0xf]);
285 GdbPutChar (HexChars
[Checksum
& 0xf]);
287 while (GdbGetChar () != '+');
292 GspPutPacketNoWait (PCHAR Buffer
)
298 /* $<packet info>#<Checksum>. */
303 while ((ch
= Buffer
[Count
]))
311 GdbPutChar (HexChars
[(Checksum
>> 4) & 0xf]);
312 GdbPutChar (HexChars
[Checksum
& 0xf]);
315 /* Indicate to caller of GspMem2Hex or GspHex2Mem that there has been an
317 static volatile BOOLEAN GspMemoryError
= FALSE
;
318 static volatile void *GspAccessLocation
= NULL
;
321 /* Convert the memory pointed to by Address into hex, placing result in Buffer */
322 /* Return a pointer to the last char put in Buffer (null) */
323 /* If MayFault is TRUE, then we should set GspMemoryError in response to
324 a fault; if FALSE treat a fault like any other fault in the stub. */
326 GspMem2Hex (PCHAR Address
,
334 if (NULL
== Address
&& MayFault
)
336 GspMemoryError
= TRUE
;
340 for (i
= 0; i
< (ULONG
) Count
; i
++)
343 GspAccessLocation
= Address
;
345 GspAccessLocation
= NULL
;
346 if (MayFault
&& GspMemoryError
)
350 *Buffer
++ = HexChars
[(ch
>> 4) & 0xf];
351 *Buffer
++ = HexChars
[ch
& 0xf];
359 /* Convert the hex array pointed to by Buffer into binary to be placed at Address */
360 /* Return a pointer to the character AFTER the last byte read from Buffer */
362 GspHex2Mem (PCHAR Buffer
,
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
= (CHAR
)(HexValue (*Buffer
++) << 4);
397 ch
= (CHAR
)(ch
+ HexValue (*Buffer
++));
399 GspAccessLocation
= current
;
401 GspAccessLocation
= NULL
;
406 MmSetPageProtect (NULL
, page
, oldprot
);
418 /* This function takes the 386 exception vector and attempts to
419 translate this number into a unix compatible signal value */
421 GspComputeSignal (NTSTATUS ExceptionCode
)
425 switch (ExceptionCode
)
427 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
429 break; /* divide by zero */
430 case STATUS_SINGLE_STEP
:
431 /* debug exception */
432 case STATUS_BREAKPOINT
:
434 break; /* breakpoint */
435 case STATUS_INTEGER_OVERFLOW
:
436 /* into instruction (overflow) */
437 case STATUS_ARRAY_BOUNDS_EXCEEDED
:
439 break; /* bound instruction */
440 case STATUS_ILLEGAL_INSTRUCTION
:
442 break; /* Invalid opcode */
444 case STATUS_FLT_INVALID_OPERATION
:
446 break; /* coprocessor not available */
448 case STATUS_STACK_OVERFLOW
:
449 /* stack exception */
450 case STATUS_DATATYPE_MISALIGNMENT
:
452 case STATUS_ACCESS_VIOLATION
:
453 SigVal
= 11; /* access violation */
456 SigVal
= 7; /* "software generated" */
462 /**********************************************/
463 /* WHILE WE FIND NICE HEX CHARS, BUILD A LONG */
464 /* RETURN NUMBER OF CHARS PROCESSED */
465 /**********************************************/
467 GspHex2Long (PCHAR
*Address
,
477 Hex
= HexValue (**Address
);
480 *Value
= (*Value
<< 4) | Hex
;
494 GspLong2Hex (PCHAR
*Address
,
499 Save
= (((Value
>> 0) & 0xff) << 24) |
500 (((Value
>> 8) & 0xff) << 16) |
501 (((Value
>> 16) & 0xff) << 8) |
502 (((Value
>> 24) & 0xff) << 0);
503 *Address
= GspMem2Hex ((PCHAR
) &Save
, *Address
, 4, FALSE
);
508 * When coming from kernel mode, Esp is not stored in the trap frame.
509 * Instead, it was pointing to the location of the TrapFrame Esp member
510 * when the exception occured. When coming from user mode, Esp is just
511 * stored in the TrapFrame Esp member.
514 GspGetEspFromTrapFrame(PKTRAP_FRAME TrapFrame
)
517 return KeGetPreviousMode() == KernelMode
518 ? (LONG
) &TrapFrame
->Esp
: TrapFrame
->Esp
;
523 GspGetRegistersFromTrapFrame(PCHAR Address
,
525 PKTRAP_FRAME TrapFrame
)
533 for (i
= 0; i
< sizeof (GspRegisters
) / sizeof (GspRegisters
[0]); i
++)
539 Value
= GspGetEspFromTrapFrame (TrapFrame
);
543 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[i
].OffsetInTF
);
547 else if (i
== EIP_REGNO
)
550 * This thread has not been sheduled yet so assume it
551 * is still in PsBeginThreadWithContextInternal().
553 Value
= (ULONG
)KiThreadStartup
;
559 Buffer
= GspMem2Hex ((PCHAR
) &Value
, Buffer
, GspRegisters
[i
].Size
, FALSE
);
565 GspSetRegistersInTrapFrame(PCHAR Address
,
567 PKTRAP_FRAME TrapFrame
)
578 for (i
= 0; i
< NUMREGS
; i
++)
580 if (GspRegisters
[i
].SetInContext
)
581 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[i
].OffsetInContext
);
583 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[i
].OffsetInTF
);
585 Buffer
= GspHex2Mem (Buffer
, (PCHAR
) &Value
, GspRegisters
[i
].Size
, FALSE
);
592 GspSetSingleRegisterInTrapFrame(PCHAR Address
,
595 PKTRAP_FRAME TrapFrame
)
603 if (GspRegisters
[Number
].SetInContext
)
604 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[Number
].OffsetInContext
);
606 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[Number
].OffsetInTF
);
608 GspHex2Mem (Address
, (PCHAR
) &Value
, GspRegisters
[Number
].Size
, FALSE
);
614 GspFindThread(PCHAR Data
,
617 PETHREAD ThreadInfo
= NULL
;
619 if (strcmp (Data
, "-1") == 0)
628 PCHAR ptr
= &Data
[0];
630 GspHex2Long (&ptr
, (PLONG
) &uThreadId
);
631 ThreadId
= (HANDLE
)uThreadId
;
633 if (!NT_SUCCESS (PsLookupThreadByThreadId (ThreadId
, &ThreadInfo
)))
639 *Thread
= ThreadInfo
;
645 GspSetThread(PCHAR Request
)
648 PCHAR ptr
= &Request
[1];
652 case 'c': /* Run thread */
653 if (GspFindThread (ptr
, &ThreadInfo
))
655 GspOutBuffer
[0] = 'O';
656 GspOutBuffer
[1] = 'K';
658 if (NULL
!= GspRunThread
)
660 ObDereferenceObject(GspRunThread
);
662 GspRunThread
= ThreadInfo
;
663 if (NULL
!= GspRunThread
)
665 ObReferenceObject(GspRunThread
);
670 GspOutBuffer
[0] = 'E';
673 case 'g': /* Debug thread */
674 if (GspFindThread (ptr
, &ThreadInfo
))
676 GspOutBuffer
[0] = 'O';
677 GspOutBuffer
[1] = 'K';
679 if(GspDbgThread
) ObDereferenceObject(GspDbgThread
);
681 GspDbgThread
= ThreadInfo
;
685 GspOutBuffer
[0] = 'E';
695 GspQuery(PCHAR Request
)
700 Command
= strtok (Request
, ",");
701 if (strncmp (Command
, "C", 1) == 0)
703 PCHAR ptr
= &GspOutBuffer
[2];
705 /* Get current thread id */
706 GspOutBuffer
[0] = 'Q';
707 GspOutBuffer
[1] = 'C';
708 if (NULL
!= GspDbgThread
)
710 Value
= (ULONG
) GspDbgThread
->Cid
.UniqueThread
;
714 Value
= (ULONG
) PsGetCurrentThread()->Cid
.UniqueThread
;
716 GspLong2Hex (&ptr
, Value
);
718 else if (strncmp (Command
, "fThreadInfo", 11) == 0)
721 PLIST_ENTRY AThread
, AProcess
;
722 PCHAR ptr
= &GspOutBuffer
[1];
724 /* Get first thread id */
725 GspEnumThread
= NULL
;
726 AProcess
= PsActiveProcessHead
.Flink
;
727 while(AProcess
!= &PsActiveProcessHead
)
729 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
730 AThread
= Process
->ThreadListHead
.Flink
;
731 if(AThread
!= &Process
->ThreadListHead
)
733 GspEnumThread
= CONTAINING_RECORD (Process
->ThreadListHead
.Flink
,
734 ETHREAD
, ThreadListEntry
);
737 AProcess
= AProcess
->Flink
;
739 if(GspEnumThread
!= NULL
)
741 GspOutBuffer
[0] = 'm';
742 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
743 GspLong2Hex (&ptr
, Value
);
747 /* FIXME - what to do here? This case should never happen though, there
748 should always be at least one thread on the system... */
749 /* GspOutBuffer[0] = 'l'; */
752 else if (strncmp (Command
, "sThreadInfo", 11) == 0)
755 PLIST_ENTRY AThread
, AProcess
;
756 PCHAR ptr
= &GspOutBuffer
[1];
758 /* Get next thread id */
759 if (GspEnumThread
!= NULL
)
761 /* find the next thread */
762 Process
= GspEnumThread
->ThreadsProcess
;
763 if(GspEnumThread
->ThreadListEntry
.Flink
!= &Process
->ThreadListHead
)
765 GspEnumThread
= CONTAINING_RECORD (GspEnumThread
->ThreadListEntry
.Flink
,
766 ETHREAD
, ThreadListEntry
);
770 PETHREAD Thread
= NULL
;
771 AProcess
= Process
->ActiveProcessLinks
.Flink
;
772 while(AProcess
!= &PsActiveProcessHead
)
774 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
775 AThread
= Process
->ThreadListHead
.Flink
;
776 if(AThread
!= &Process
->ThreadListHead
)
778 Thread
= CONTAINING_RECORD (Process
->ThreadListHead
.Flink
,
779 ETHREAD
, ThreadListEntry
);
782 AProcess
= AProcess
->Flink
;
784 GspEnumThread
= Thread
;
787 if(GspEnumThread
!= NULL
)
790 GspOutBuffer
[0] = 'm';
791 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
792 GspLong2Hex (&ptr
, Value
);
796 GspOutBuffer
[0] = 'l';
801 GspOutBuffer
[0] = 'l';
804 else if (strncmp (Command
, "ThreadExtraInfo", 15) == 0)
807 PCHAR ptr
= &Command
[15];
809 /* Get thread information */
810 if (GspFindThread (ptr
, &ThreadInfo
))
812 PCHAR String
= GspThreadStates
[ThreadInfo
->Tcb
.State
];
814 ObDereferenceObject(ThreadInfo
);
816 GspMem2Hex (String
, &GspOutBuffer
[0], strlen (String
), FALSE
);
820 else if (strncmp (Command
, "L", 1) == 0)
822 PLIST_ENTRY CurrentEntry
;
824 ULONG MaxThreads
= 0;
826 ULONG ThreadCount
= 0;
829 GspHex2Mem (&Request
[1], (PCHAR
) &MaxThreads
, 2, TRUE
);
830 GspHex2Mem (&Request
[3], (PCHAR
) &Value
, 4, TRUE
);
831 GspHex2Mem (&Request
[11], (PCHAR
) &ThreadId
, 4, TRUE
);
833 GspOutBuffer
[0] = 'q';
834 GspOutBuffer
[1] = 'M';
836 GspMem2Hex ((PCHAR
) &Value
, &GspOutBuffer
[5], 4, TRUE
);
837 GspMem2Hex ((PCHAR
) &ThreadId
, &GspOutBuffer
[13], 4, TRUE
);
839 CurrentEntry
= PiThreadListHead
.Flink
;
840 while ((CurrentEntry
!= &PiThreadListHead
) && (ThreadCount
< MaxThreads
))
842 Current
= CONTAINING_RECORD (CurrentEntry
, ETHREAD
, Tcb
.ThreadListEntry
);
844 GspMem2Hex ((PCHAR
) &Value
, &GspOutBuffer
[21+ThreadCount
*16], 4, TRUE
);
845 Value
= (ULONG
) Current
->Cid
.UniqueThread
;
846 GspMem2Hex ((PCHAR
) &Value
, &GspOutBuffer
[21+ThreadCount
*16+8], 4, TRUE
);
847 CurrentEntry
= CurrentEntry
->Flink
;
851 if (CurrentEntry
!= &PiThreadListHead
)
853 GspOutBuffer
[4] = '0';
857 GspOutBuffer
[4] = '1';
860 GspMem2Hex ((PCHAR
) &ThreadCount
, &GspOutBuffer
[2], 1, TRUE
);
863 else if (strncmp (Command
, "Offsets", 7) == 0)
865 strcpy (GspOutBuffer
, "Text=0;Data=0;Bss=0");
870 GspQueryThreadStatus(PCHAR Request
)
873 PCHAR ptr
= &Request
[0];
875 if (GspFindThread (ptr
, &ThreadInfo
))
877 ObDereferenceObject(ThreadInfo
);
879 GspOutBuffer
[0] = 'O';
880 GspOutBuffer
[1] = 'K';
881 GspOutBuffer
[2] = '\0';
885 GspOutBuffer
[0] = 'E';
886 GspOutBuffer
[1] = '\0';
891 typedef struct _GsHwBreakPoint
899 #if defined(__GNUC__)
900 GsHwBreakPoint GspBreakpoints
[4] =
908 GsHwBreakPoint GspBreakpoints
[4] =
918 GspCorrectHwBreakpoint()
920 ULONG BreakpointNumber
;
925 #if defined(__GNUC__)
927 "movl %%db7, %0\n" : "=r" (dr7_
) : );
930 ULONG addr0
, addr1
, addr2
, addr3
;
937 : "=r" (addr0
), "=r" (addr1
),
938 "=r" (addr2
), "=r" (addr3
) : );
940 #elif defined(_MSC_VER)
943 mov eax
, dr7
; mov dr7_
, eax
;
944 mov eax
, dr0
; mov addr0
, eax
;
945 mov eax
, dr1
; mov addr1
, eax
;
946 mov eax
, dr2
; mov addr2
, eax
;
947 mov eax
, dr3
; mov addr3
, eax
;
950 #error Unknown compiler for inline assembler
953 for (BreakpointNumber
= 0; BreakpointNumber
< 3; BreakpointNumber
++)
955 Bit
= 2 << (BreakpointNumber
<< 1);
956 if (!(dr7_
& Bit
) && GspBreakpoints
[BreakpointNumber
].Enabled
) {
959 dr7_
&= ~(0xf0000 << (BreakpointNumber
<< 2));
960 dr7_
|= (((GspBreakpoints
[BreakpointNumber
].Length
<< 2) |
961 GspBreakpoints
[BreakpointNumber
].Type
) << 16) << (BreakpointNumber
<< 2);
962 switch (BreakpointNumber
) {
963 #if defined(__GNUC__)
965 asm volatile ("movl %0, %%dr0\n"
966 : : "r" (GspBreakpoints
[BreakpointNumber
].Address
) );
970 asm volatile ("movl %0, %%dr1\n"
971 : : "r" (GspBreakpoints
[BreakpointNumber
].Address
) );
975 asm volatile ("movl %0, %%dr2\n"
976 : : "r" (GspBreakpoints
[BreakpointNumber
].Address
) );
980 asm volatile ("movl %0, %%dr3\n"
981 : : "r" (GspBreakpoints
[BreakpointNumber
].Address
) );
983 #elif defined(_MSC_VER)
986 ULONG addr
= GspBreakpoints
[BreakpointNumber
].Address
;
993 ULONG addr
= GspBreakpoints
[BreakpointNumber
].Address
;
1000 ULONG addr
= GspBreakpoints
[BreakpointNumber
].Address
;
1001 __asm mov eax
, addr
;
1007 ULONG addr
= GspBreakpoints
[BreakpointNumber
].Address
;
1008 __asm mov eax
, addr
;
1013 #error Unknown compiler for inline assembler
1017 else if ((dr7_
& Bit
) && !GspBreakpoints
[BreakpointNumber
].Enabled
)
1021 dr7_
&= ~(0xf0000 << (BreakpointNumber
<< 2));
1026 #if defined(__GNUC__)
1027 asm volatile ( "movl %0, %%db7\n" : : "r" (dr7_
));
1028 #elif defined(_MSC_VER)
1029 __asm mov eax
, dr7_
;
1032 #error Unknown compiler for inline assembler
1038 GspRemoveHwBreakpoint(ULONG BreakpointNumber
)
1040 if (!GspBreakpoints
[BreakpointNumber
].Enabled
)
1044 GspBreakpoints
[BreakpointNumber
].Enabled
= 0;
1050 GspSetHwBreakpoint(ULONG BreakpointNumber
,
1055 if (GspBreakpoints
[BreakpointNumber
].Enabled
)
1059 GspBreakpoints
[BreakpointNumber
].Enabled
= TRUE
;
1060 GspBreakpoints
[BreakpointNumber
].Type
= Type
;
1061 GspBreakpoints
[BreakpointNumber
].Length
= Length
;
1062 GspBreakpoints
[BreakpointNumber
].Address
= Address
;
1068 * This function does all command procesing for interfacing to gdb.
1072 KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord
,
1074 PKTRAP_FRAME TrapFrame
)
1084 /* FIXME: Stop on other CPUs too */
1085 /* Disable hardware debugging while we are inside the stub */
1086 #if defined(__GNUC__)
1087 __asm__("movl %0,%%db7" : /* no output */ : "r" (0));
1088 #elif defined(_MSC_VER)
1089 __asm mov eax
, 0 __asm mov dr7
, eax
1091 #error Unknown compiler for inline assembler
1094 if (STATUS_ACCESS_VIOLATION
== (NTSTATUS
) ExceptionRecord
->ExceptionCode
&&
1095 NULL
!= GspAccessLocation
&&
1096 (ULONG_PTR
) GspAccessLocation
==
1097 (ULONG_PTR
) ExceptionRecord
->ExceptionInformation
[1])
1099 GspAccessLocation
= NULL
;
1100 GspMemoryError
= TRUE
;
1101 TrapFrame
->Eip
+= 2;
1105 /* Don't switch threads */
1107 /* Always use the current thread when entering the exception handler */
1108 if (NULL
!= GspDbgThread
)
1110 ObDereferenceObject(GspDbgThread
);
1111 GspDbgThread
= NULL
;
1114 /* reply to host that an exception has occurred */
1115 SigVal
= GspComputeSignal (ExceptionRecord
->ExceptionCode
);
1117 ptr
= &GspOutBuffer
[0];
1119 *ptr
++ = 'T'; /* notify gdb with signo, PC, FP and SP */
1120 *ptr
++ = HexChars
[(SigVal
>> 4) & 0xf];
1121 *ptr
++ = HexChars
[SigVal
& 0xf];
1123 *ptr
++ = HexChars
[ESP
];
1126 Esp
= GspGetEspFromTrapFrame (TrapFrame
); /* SP */
1127 ptr
= GspMem2Hex ((PCHAR
) &Esp
, ptr
, 4, 0);
1130 *ptr
++ = HexChars
[EBP
];
1132 ptr
= GspMem2Hex ((PCHAR
) &TrapFrame
->Ebp
, ptr
, 4, 0); /* FP */
1135 *ptr
++ = HexChars
[PC
];
1137 ptr
= GspMem2Hex((PCHAR
) &TrapFrame
->Eip
, ptr
, 4, 0); /* PC */
1142 GspPutPacket (&GspOutBuffer
[0]);
1148 /* Zero the buffer now so we don't have to worry about the terminating zero character */
1149 memset (GspOutBuffer
, 0, sizeof (GspInBuffer
));
1150 ptr
= GspGetPacket ();
1155 GspOutBuffer
[0] = 'S';
1156 GspOutBuffer
[1] = HexChars
[SigVal
>> 4];
1157 GspOutBuffer
[2] = HexChars
[SigVal
% 16];
1158 GspOutBuffer
[3] = 0;
1161 GspRemoteDebug
= !GspRemoteDebug
; /* toggle debug flag */
1163 case 'g': /* return the value of the CPU Registers */
1164 if (NULL
!= GspDbgThread
)
1166 GspGetRegistersFromTrapFrame (&GspOutBuffer
[0], Context
, GspDbgThread
->Tcb
.TrapFrame
);
1170 GspGetRegistersFromTrapFrame (&GspOutBuffer
[0], Context
, TrapFrame
);
1173 case 'G': /* set the value of the CPU Registers - return OK */
1174 if (NULL
!= GspDbgThread
)
1176 GspSetRegistersInTrapFrame (ptr
, Context
, GspDbgThread
->Tcb
.TrapFrame
);
1180 GspSetRegistersInTrapFrame (ptr
, Context
, TrapFrame
);
1182 strcpy (GspOutBuffer
, "OK");
1184 case 'P': /* set the value of a single CPU register - return OK */
1188 if ((GspHex2Long (&ptr
, &Register
)) && (*ptr
++ == '='))
1189 if ((Register
>= 0) && (Register
< NUMREGS
))
1193 GspSetSingleRegisterInTrapFrame(ptr
, Register
,
1194 Context
, GspDbgThread
->Tcb
.TrapFrame
);
1198 GspSetSingleRegisterInTrapFrame (ptr
, Register
, Context
, TrapFrame
);
1200 strcpy (GspOutBuffer
, "OK");
1204 strcpy (GspOutBuffer
, "E01");
1208 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1210 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
1211 if (GspHex2Long (&ptr
, &Address
))
1212 if (*(ptr
++) == ',')
1213 if (GspHex2Long (&ptr
, &Length
))
1216 GspMemoryError
= FALSE
;
1217 GspMem2Hex ((PCHAR
) Address
, GspOutBuffer
, Length
, 1);
1220 strcpy (GspOutBuffer
, "E03");
1221 DPRINT ("Fault during memory read\n");
1226 strcpy (GspOutBuffer
, "E01");
1229 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
1231 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
1232 if (GspHex2Long (&ptr
, &Address
))
1233 if (*(ptr
++) == ',')
1234 if (GspHex2Long (&ptr
, &Length
))
1235 if (*(ptr
++) == ':')
1237 GspMemoryError
= FALSE
;
1238 GspHex2Mem (ptr
, (PCHAR
) Address
, Length
, TRUE
);
1242 strcpy (GspOutBuffer
, "E03");
1243 DPRINT ("Fault during memory write\n");
1247 strcpy (GspOutBuffer
, "OK");
1253 strcpy (GspOutBuffer
, "E02");
1256 /* cAA..AA Continue at address AA..AA(optional) */
1257 /* sAA..AA Step one instruction from AA..AA(optional) */
1262 ULONG BreakpointNumber
;
1265 /* try to read optional parameter, pc unchanged if no parm */
1266 if (GspHex2Long (&ptr
, &Address
))
1267 Context
->Eip
= Address
;
1269 NewPC
= Context
->Eip
;
1271 /* clear the trace bit */
1272 Context
->EFlags
&= 0xfffffeff;
1274 /* set the trace bit if we're Stepping */
1276 Context
->EFlags
|= 0x100;
1278 #if defined(__GNUC__)
1279 asm volatile ("movl %%db6, %0\n" : "=r" (dr6_
) : );
1280 #elif defined(_MSC_VER)
1281 __asm mov eax
, dr6 __asm mov dr6_
, eax
;
1283 #error Unknown compiler for inline assembler
1285 if (!(dr6_
& 0x4000))
1287 for (BreakpointNumber
= 0; BreakpointNumber
< 4; ++BreakpointNumber
)
1289 if (dr6_
& (1 << BreakpointNumber
))
1291 if (GspBreakpoints
[BreakpointNumber
].Type
== 0)
1293 /* Set restore flag */
1294 Context
->EFlags
|= 0x10000;
1300 GspCorrectHwBreakpoint();
1301 #if defined(__GNUC__)
1302 asm volatile ("movl %0, %%db6\n" : : "r" (0));
1303 #elif defined(_MSC_VER)
1304 __asm mov eax
, 0 __asm mov dr6
, eax
;
1306 #error Unknown compiler for inline assembler
1309 KeContextToTrapFrame(Context
, TrapFrame
);
1310 return ((SigVal
== 5) ? (kdContinue
) : (kdHandleException
));
1314 case 'k': /* kill the program */
1315 strcpy (GspOutBuffer
, "OK");
1317 /* kill the program */
1319 case 'H': /* Set thread */
1323 case 'q': /* Query */
1327 case 'T': /* Query thread status */
1328 GspQueryThreadStatus (ptr
);
1338 ptr
= &GspOutBuffer
[1];
1339 GspHex2Long (&ptr
, &Number
);
1341 GspHex2Long (&ptr
, &Type
);
1343 GspHex2Long (&ptr
, &Length
);
1345 GspHex2Long (&ptr
, &Address
);
1346 if (GspSetHwBreakpoint (Number
& 0x3, Type
& 0x3 , Length
& 0x3, Address
) == 0)
1348 strcpy (GspOutBuffer
, "OK");
1352 strcpy (GspOutBuffer
, "E");
1357 /* Remove hardware breakpoint */
1362 ptr
= &GspOutBuffer
[1];
1363 GspHex2Long(&ptr
, &Number
);
1364 if (GspRemoveHwBreakpoint (Number
& 0x3) == 0)
1366 strcpy (GspOutBuffer
, "OK");
1370 strcpy (GspOutBuffer
, "E");
1379 /* reply to the request */
1380 GspPutPacket (&GspOutBuffer
[0]);
1387 return kdDoNotHandleException
;
1393 GspBreakIn(PKINTERRUPT Interrupt
,
1394 PVOID ServiceContext
)
1396 PKTRAP_FRAME TrapFrame
;
1402 DPRINT ("Break In\n");
1405 while (KdPortGetByteEx (&GdbPortInfo
, &Value
))
1414 KeRaiseIrql (HIGH_LEVEL
, &OldIrql
);
1416 TrapFrame
= PsGetCurrentThread()->Tcb
.TrapFrame
;
1418 KeTrapFrameToContext (TrapFrame
, &Context
);
1420 KdpGdbEnterDebuggerException (NULL
, &Context
, TrapFrame
);
1422 KeContextToTrapFrame (&Context
, TrapFrame
);
1424 KeLowerIrql (OldIrql
);
1430 extern ULONG KdpPortIrq
;
1435 KdpGdbDebugPrint(PCH Message
)
1438 /* This can be quite annoying! */
1443 GspOutBuffer
[0] = 'O';
1444 GspOutBuffer
[1] = '\0';
1445 strcat (&GspOutBuffer
[0], Message
);
1446 Length
= strlen (Message
);
1447 GspOutBuffer
[2 + Length
] = '\n';
1448 GspOutBuffer
[3 + Length
] = '\0';
1449 GspPutPacketNoWait (&GspOutBuffer
[0]);
1455 extern LIST_ENTRY ModuleListHead
;
1460 PLIST_ENTRY CurrentEntry
;
1461 PMODULE_OBJECT Current
;
1468 CurrentEntry
= ModuleListHead
.Flink
;
1469 while (CurrentEntry
!= (&ModuleListHead
))
1471 Current
= CONTAINING_RECORD (CurrentEntry
, MODULE_OBJECT
, ListEntry
);
1473 DbgPrint ("Module %S Base 0x%.08x Length 0x%.08x\n",
1474 Current
->BaseName
.Buffer
, Current
->Base
, Current
->Length
);
1477 CurrentEntry
= CurrentEntry
->Flink
;
1480 DbgPrint ("%d modules listed\n", ModuleCount
);
1483 /* Initialize the GDB stub */
1486 KdpGdbStubInit(PKD_DISPATCH_TABLE WrapperTable
,
1489 if (!KdDebuggerEnabled
|| !KdpDebugMode
.Gdb
) return;
1493 /* Write out the functions that we support for now */
1494 WrapperTable
->KdpInitRoutine
= KdpGdbStubInit
;
1495 WrapperTable
->KdpPrintRoutine
= KdpGdbDebugPrint
;
1496 WrapperTable
->KdpExceptionRoutine
= KdpGdbEnterDebuggerException
;
1498 /* Initialize the Port */
1499 KdPortInitializeEx(&GdbPortInfo
, 0, 0);
1501 else if (BootPhase
== 1)
1503 GspInitialized
= TRUE
;
1505 GspRunThread
= NULL
;
1506 GspDbgThread
= NULL
;
1507 GspEnumThread
= NULL
;
1509 HalDisplayString("Waiting for GDB to attach\n");
1510 DbgPrint("Module 'hal.dll' loaded at 0x%.08x.\n", LdrHalBase
);
1511 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C
);
1513 else if (BootPhase
== 2)
1515 HalDisplayString("\n GDB debugging enabled\n\n");