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 */
246 /* if a sequence char is present, reply the sequence ID */
247 if (Buffer
[2] == ':')
249 GdbPutChar (Buffer
[0]);
250 GdbPutChar (Buffer
[1]);
262 /* send the packet in Buffer. */
265 GspPutPacket (PCHAR Buffer
)
271 /* $<packet info>#<Checksum>. */
278 while ((ch
= Buffer
[Count
]))
286 GdbPutChar (HexChars
[(Checksum
>> 4) & 0xf]);
287 GdbPutChar (HexChars
[Checksum
& 0xf]);
289 while (GdbGetChar () != '+');
294 GspPutPacketNoWait (PCHAR Buffer
)
300 /* $<packet info>#<Checksum>. */
305 while ((ch
= Buffer
[Count
]))
313 GdbPutChar (HexChars
[(Checksum
>> 4) & 0xf]);
314 GdbPutChar (HexChars
[Checksum
& 0xf]);
317 /* Indicate to caller of GspMem2Hex or GspHex2Mem that there has been an
319 static volatile BOOLEAN GspMemoryError
= FALSE
;
320 static volatile void *GspAccessLocation
= NULL
;
323 /* Convert the memory pointed to by Address into hex, placing result in Buffer */
324 /* Return a pointer to the last char put in Buffer (null) */
325 /* If MayFault is TRUE, then we should set GspMemoryError in response to
326 a fault; if FALSE treat a fault like any other fault in the stub. */
328 GspMem2Hex (PCHAR Address
,
336 if (NULL
== Address
&& MayFault
)
338 GspMemoryError
= TRUE
;
342 for (i
= 0; i
< (ULONG
) Count
; i
++)
345 GspAccessLocation
= Address
;
347 GspAccessLocation
= NULL
;
348 if (MayFault
&& GspMemoryError
)
352 *Buffer
++ = HexChars
[(ch
>> 4) & 0xf];
353 *Buffer
++ = HexChars
[ch
& 0xf];
361 /* Convert the hex array pointed to by Buffer into binary to be placed at Address */
362 /* Return a pointer to the character AFTER the last byte read from Buffer */
364 GspHex2Mem (PCHAR Buffer
,
377 while ( current
< Address
+ Count
)
379 page
= (PCHAR
)PAGE_ROUND_DOWN (current
);
380 if (Address
+ Count
<= page
+ PAGE_SIZE
)
382 /* Fits in this page */
387 /* Flows into next page, handle only current page in this iteration */
388 countinpage
= PAGE_SIZE
- (Address
- page
);
392 oldprot
= MmGetPageProtect (NULL
, Address
);
393 MmSetPageProtect (NULL
, Address
, PAGE_EXECUTE_READWRITE
);
396 for (i
= 0; i
< countinpage
&& ! GspMemoryError
; i
++)
398 ch
= (CHAR
)(HexValue (*Buffer
++) << 4);
399 ch
= (CHAR
)(ch
+ HexValue (*Buffer
++));
401 GspAccessLocation
= current
;
403 GspAccessLocation
= NULL
;
408 MmSetPageProtect (NULL
, page
, oldprot
);
420 /* This function takes the 386 exception vector and attempts to
421 translate this number into a unix compatible signal value */
423 GspComputeSignal (NTSTATUS ExceptionCode
)
427 switch (ExceptionCode
)
429 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
431 break; /* divide by zero */
432 case STATUS_SINGLE_STEP
:
433 /* debug exception */
434 case STATUS_BREAKPOINT
:
436 break; /* breakpoint */
437 case STATUS_INTEGER_OVERFLOW
:
438 /* into instruction (overflow) */
439 case STATUS_ARRAY_BOUNDS_EXCEEDED
:
441 break; /* bound instruction */
442 case STATUS_ILLEGAL_INSTRUCTION
:
444 break; /* Invalid opcode */
446 case STATUS_FLT_INVALID_OPERATION
:
448 break; /* coprocessor not available */
450 case STATUS_STACK_OVERFLOW
:
451 /* stack exception */
452 case STATUS_DATATYPE_MISALIGNMENT
:
454 case STATUS_ACCESS_VIOLATION
:
455 SigVal
= 11; /* access violation */
458 SigVal
= 7; /* "software generated" */
464 /**********************************************/
465 /* WHILE WE FIND NICE HEX CHARS, BUILD A LONG */
466 /* RETURN NUMBER OF CHARS PROCESSED */
467 /**********************************************/
469 GspHex2Long (PCHAR
*Address
,
479 Hex
= HexValue (**Address
);
482 *Value
= (*Value
<< 4) | Hex
;
496 GspLong2Hex (PCHAR
*Address
,
501 Save
= (((Value
>> 0) & 0xff) << 24) |
502 (((Value
>> 8) & 0xff) << 16) |
503 (((Value
>> 16) & 0xff) << 8) |
504 (((Value
>> 24) & 0xff) << 0);
505 *Address
= GspMem2Hex ((PCHAR
) &Save
, *Address
, 4, FALSE
);
510 * When coming from kernel mode, Esp is not stored in the trap frame.
511 * Instead, it was pointing to the location of the TrapFrame Esp member
512 * when the exception occured. When coming from user mode, Esp is just
513 * stored in the TrapFrame Esp member.
516 GspGetEspFromTrapFrame(PKTRAP_FRAME TrapFrame
)
519 return KeGetPreviousMode() == KernelMode
520 ? (LONG
) &TrapFrame
->Esp
: (LONG
)TrapFrame
->Esp
;
525 GspGetRegistersFromTrapFrame(PCHAR Address
,
527 PKTRAP_FRAME TrapFrame
)
535 for (i
= 0; i
< sizeof (GspRegisters
) / sizeof (GspRegisters
[0]); i
++)
541 Value
= GspGetEspFromTrapFrame (TrapFrame
);
545 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[i
].OffsetInTF
);
549 else if (i
== EIP_REGNO
)
552 * This thread has not been sheduled yet so assume it
553 * is still in PsBeginThreadWithContextInternal().
555 Value
= (ULONG
)KiThreadStartup
;
561 Buffer
= GspMem2Hex ((PCHAR
) &Value
, Buffer
, GspRegisters
[i
].Size
, FALSE
);
567 GspSetRegistersInTrapFrame(PCHAR Address
,
569 PKTRAP_FRAME TrapFrame
)
580 for (i
= 0; i
< NUMREGS
; i
++)
582 if (GspRegisters
[i
].SetInContext
)
583 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[i
].OffsetInContext
);
585 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[i
].OffsetInTF
);
587 Buffer
= GspHex2Mem (Buffer
, (PCHAR
) &Value
, GspRegisters
[i
].Size
, FALSE
);
594 GspSetSingleRegisterInTrapFrame(PCHAR Address
,
597 PKTRAP_FRAME TrapFrame
)
605 if (GspRegisters
[Number
].SetInContext
)
606 p
= (PULONG
) ((ULONG_PTR
) Context
+ GspRegisters
[Number
].OffsetInContext
);
608 p
= (PULONG
) ((ULONG_PTR
) TrapFrame
+ GspRegisters
[Number
].OffsetInTF
);
610 GspHex2Mem (Address
, (PCHAR
) &Value
, GspRegisters
[Number
].Size
, FALSE
);
616 GspFindThread(PCHAR Data
,
619 PETHREAD ThreadInfo
= NULL
;
621 if (strcmp (Data
, "-1") == 0)
630 PCHAR ptr
= &Data
[0];
632 GspHex2Long (&ptr
, (PLONG
) &uThreadId
);
633 ThreadId
= (HANDLE
)uThreadId
;
635 if (!NT_SUCCESS (PsLookupThreadByThreadId (ThreadId
, &ThreadInfo
)))
641 *Thread
= ThreadInfo
;
647 GspSetThread(PCHAR Request
)
650 PCHAR ptr
= &Request
[1];
654 case 'c': /* Run thread */
655 if (GspFindThread (ptr
, &ThreadInfo
))
657 GspOutBuffer
[0] = 'O';
658 GspOutBuffer
[1] = 'K';
660 if (NULL
!= GspRunThread
)
662 ObDereferenceObject(GspRunThread
);
664 GspRunThread
= ThreadInfo
;
665 if (NULL
!= GspRunThread
)
667 ObReferenceObject(GspRunThread
);
672 GspOutBuffer
[0] = 'E';
675 case 'g': /* Debug thread */
676 if (GspFindThread (ptr
, &ThreadInfo
))
678 GspOutBuffer
[0] = 'O';
679 GspOutBuffer
[1] = 'K';
681 if(GspDbgThread
) ObDereferenceObject(GspDbgThread
);
683 GspDbgThread
= ThreadInfo
;
687 GspOutBuffer
[0] = 'E';
697 GspQuery(PCHAR Request
)
702 Command
= strtok (Request
, ",");
703 if (strncmp (Command
, "C", 1) == 0)
705 PCHAR ptr
= &GspOutBuffer
[2];
707 /* Get current thread id */
708 GspOutBuffer
[0] = 'Q';
709 GspOutBuffer
[1] = 'C';
710 if (NULL
!= GspDbgThread
)
712 Value
= (ULONG
) GspDbgThread
->Cid
.UniqueThread
;
716 Value
= (ULONG
) PsGetCurrentThread()->Cid
.UniqueThread
;
718 GspLong2Hex (&ptr
, Value
);
720 else if (strncmp (Command
, "fThreadInfo", 11) == 0)
723 PLIST_ENTRY AThread
, AProcess
;
724 PCHAR ptr
= &GspOutBuffer
[1];
726 /* Get first thread id */
727 GspEnumThread
= NULL
;
728 AProcess
= PsActiveProcessHead
.Flink
;
729 while(AProcess
!= &PsActiveProcessHead
)
731 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
732 AThread
= Process
->ThreadListHead
.Flink
;
733 if(AThread
!= &Process
->ThreadListHead
)
735 GspEnumThread
= CONTAINING_RECORD (Process
->ThreadListHead
.Flink
,
736 ETHREAD
, ThreadListEntry
);
739 AProcess
= AProcess
->Flink
;
741 if(GspEnumThread
!= NULL
)
743 GspOutBuffer
[0] = 'm';
744 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
745 GspLong2Hex (&ptr
, Value
);
749 /* FIXME - what to do here? This case should never happen though, there
750 should always be at least one thread on the system... */
751 /* GspOutBuffer[0] = 'l'; */
754 else if (strncmp (Command
, "sThreadInfo", 11) == 0)
757 PLIST_ENTRY AThread
, AProcess
;
758 PCHAR ptr
= &GspOutBuffer
[1];
760 /* Get next thread id */
761 if (GspEnumThread
!= NULL
)
763 /* find the next thread */
764 Process
= GspEnumThread
->ThreadsProcess
;
765 if(GspEnumThread
->ThreadListEntry
.Flink
!= &Process
->ThreadListHead
)
767 GspEnumThread
= CONTAINING_RECORD (GspEnumThread
->ThreadListEntry
.Flink
,
768 ETHREAD
, ThreadListEntry
);
772 PETHREAD Thread
= NULL
;
773 AProcess
= Process
->ActiveProcessLinks
.Flink
;
774 while(AProcess
!= &PsActiveProcessHead
)
776 Process
= CONTAINING_RECORD(AProcess
, EPROCESS
, ActiveProcessLinks
);
777 AThread
= Process
->ThreadListHead
.Flink
;
778 if(AThread
!= &Process
->ThreadListHead
)
780 Thread
= CONTAINING_RECORD (Process
->ThreadListHead
.Flink
,
781 ETHREAD
, ThreadListEntry
);
784 AProcess
= AProcess
->Flink
;
786 GspEnumThread
= Thread
;
789 if(GspEnumThread
!= NULL
)
792 GspOutBuffer
[0] = 'm';
793 Value
= (ULONG
) GspEnumThread
->Cid
.UniqueThread
;
794 GspLong2Hex (&ptr
, Value
);
798 GspOutBuffer
[0] = 'l';
803 GspOutBuffer
[0] = 'l';
806 else if (strncmp (Command
, "ThreadExtraInfo", 15) == 0)
809 PCHAR ptr
= &Command
[15];
811 /* Get thread information */
812 if (GspFindThread (ptr
, &ThreadInfo
))
814 PCHAR String
= GspThreadStates
[ThreadInfo
->Tcb
.State
];
816 ObDereferenceObject(ThreadInfo
);
818 GspMem2Hex (String
, &GspOutBuffer
[0], strlen (String
), FALSE
);
822 else if (strncmp (Command
, "L", 1) == 0)
824 PLIST_ENTRY CurrentEntry
;
826 ULONG MaxThreads
= 0;
828 ULONG ThreadCount
= 0;
831 GspHex2Mem (&Request
[1], (PCHAR
) &MaxThreads
, 2, TRUE
);
832 GspHex2Mem (&Request
[3], (PCHAR
) &Value
, 4, TRUE
);
833 GspHex2Mem (&Request
[11], (PCHAR
) &ThreadId
, 4, TRUE
);
835 GspOutBuffer
[0] = 'q';
836 GspOutBuffer
[1] = 'M';
838 GspMem2Hex ((PCHAR
) &Value
, &GspOutBuffer
[5], 4, TRUE
);
839 GspMem2Hex ((PCHAR
) &ThreadId
, &GspOutBuffer
[13], 4, TRUE
);
841 CurrentEntry
= PiThreadListHead
.Flink
;
842 while ((CurrentEntry
!= &PiThreadListHead
) && (ThreadCount
< MaxThreads
))
844 Current
= CONTAINING_RECORD (CurrentEntry
, ETHREAD
, Tcb
.ThreadListEntry
);
846 GspMem2Hex ((PCHAR
) &Value
, &GspOutBuffer
[21+ThreadCount
*16], 4, TRUE
);
847 Value
= (ULONG
) Current
->Cid
.UniqueThread
;
848 GspMem2Hex ((PCHAR
) &Value
, &GspOutBuffer
[21+ThreadCount
*16+8], 4, TRUE
);
849 CurrentEntry
= CurrentEntry
->Flink
;
853 if (CurrentEntry
!= &PiThreadListHead
)
855 GspOutBuffer
[4] = '0';
859 GspOutBuffer
[4] = '1';
862 GspMem2Hex ((PCHAR
) &ThreadCount
, &GspOutBuffer
[2], 1, TRUE
);
866 else if (strncmp (Command
, "Offsets", 7) == 0)
868 strcpy (GspOutBuffer
, "Text=0;Data=0;Bss=0");
874 GspQueryThreadStatus(PCHAR Request
)
877 PCHAR ptr
= &Request
[0];
879 if (GspFindThread (ptr
, &ThreadInfo
))
881 ObDereferenceObject(ThreadInfo
);
883 GspOutBuffer
[0] = 'O';
884 GspOutBuffer
[1] = 'K';
885 GspOutBuffer
[2] = '\0';
889 GspOutBuffer
[0] = 'E';
890 GspOutBuffer
[1] = '\0';
895 typedef struct _GsHwBreakPoint
903 #if defined(__GNUC__)
904 GsHwBreakPoint GspBreakpoints
[4] =
912 GsHwBreakPoint GspBreakpoints
[4] =
922 GspCorrectHwBreakpoint()
924 ULONG BreakpointNumber
;
929 #if defined(__GNUC__)
931 "movl %%db7, %0\n" : "=r" (dr7_
) : );
934 ULONG addr0
, addr1
, addr2
, addr3
;
941 : "=r" (addr0
), "=r" (addr1
),
942 "=r" (addr2
), "=r" (addr3
) : );
944 #elif defined(_MSC_VER)
947 mov eax
, dr7
; mov dr7_
, eax
;
948 mov eax
, dr0
; mov addr0
, eax
;
949 mov eax
, dr1
; mov addr1
, eax
;
950 mov eax
, dr2
; mov addr2
, eax
;
951 mov eax
, dr3
; mov addr3
, eax
;
954 #error Unknown compiler for inline assembler
957 for (BreakpointNumber
= 0; BreakpointNumber
< 3; BreakpointNumber
++)
959 Bit
= 2 << (BreakpointNumber
<< 1);
960 if (!(dr7_
& Bit
) && GspBreakpoints
[BreakpointNumber
].Enabled
) {
963 dr7_
&= ~(0xf0000 << (BreakpointNumber
<< 2));
964 dr7_
|= (((GspBreakpoints
[BreakpointNumber
].Length
<< 2) |
965 GspBreakpoints
[BreakpointNumber
].Type
) << 16) << (BreakpointNumber
<< 2);
966 switch (BreakpointNumber
) {
967 #if defined(__GNUC__)
969 asm volatile ("movl %0, %%dr0\n"
970 : : "r" (GspBreakpoints
[BreakpointNumber
].Address
) );
974 asm volatile ("movl %0, %%dr1\n"
975 : : "r" (GspBreakpoints
[BreakpointNumber
].Address
) );
979 asm volatile ("movl %0, %%dr2\n"
980 : : "r" (GspBreakpoints
[BreakpointNumber
].Address
) );
984 asm volatile ("movl %0, %%dr3\n"
985 : : "r" (GspBreakpoints
[BreakpointNumber
].Address
) );
987 #elif defined(_MSC_VER)
990 ULONG addr
= GspBreakpoints
[BreakpointNumber
].Address
;
997 ULONG addr
= GspBreakpoints
[BreakpointNumber
].Address
;
1004 ULONG addr
= GspBreakpoints
[BreakpointNumber
].Address
;
1005 __asm mov eax
, addr
;
1011 ULONG addr
= GspBreakpoints
[BreakpointNumber
].Address
;
1012 __asm mov eax
, addr
;
1017 #error Unknown compiler for inline assembler
1021 else if ((dr7_
& Bit
) && !GspBreakpoints
[BreakpointNumber
].Enabled
)
1025 dr7_
&= ~(0xf0000 << (BreakpointNumber
<< 2));
1030 #if defined(__GNUC__)
1031 asm volatile ( "movl %0, %%db7\n" : : "r" (dr7_
));
1032 #elif defined(_MSC_VER)
1033 __asm mov eax
, dr7_
;
1036 #error Unknown compiler for inline assembler
1042 GspRemoveHwBreakpoint(ULONG BreakpointNumber
)
1044 if (!GspBreakpoints
[BreakpointNumber
].Enabled
)
1048 GspBreakpoints
[BreakpointNumber
].Enabled
= 0;
1054 GspSetHwBreakpoint(ULONG BreakpointNumber
,
1059 if (GspBreakpoints
[BreakpointNumber
].Enabled
)
1063 GspBreakpoints
[BreakpointNumber
].Enabled
= TRUE
;
1064 GspBreakpoints
[BreakpointNumber
].Type
= Type
;
1065 GspBreakpoints
[BreakpointNumber
].Length
= Length
;
1066 GspBreakpoints
[BreakpointNumber
].Address
= Address
;
1071 static BOOL gdb_attached_yet
= TRUE
;
1073 * This function does all command procesing for interfacing to gdb.
1077 KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord
,
1079 PKTRAP_FRAME TrapFrame
)
1088 /* FIXME: Stop on other CPUs too */
1089 /* Disable hardware debugging while we are inside the stub */
1090 #if defined(__GNUC__)
1091 __asm__("movl %0,%%db7" : /* no output */ : "r" (0));
1092 #elif defined(_MSC_VER)
1093 __asm mov eax
, 0 __asm mov dr7
, eax
1095 #error Unknown compiler for inline assembler
1098 if (STATUS_ACCESS_VIOLATION
== (NTSTATUS
) ExceptionRecord
->ExceptionCode
&&
1099 NULL
!= GspAccessLocation
&&
1100 (ULONG_PTR
) GspAccessLocation
==
1101 (ULONG_PTR
) ExceptionRecord
->ExceptionInformation
[1])
1103 GspAccessLocation
= NULL
;
1104 GspMemoryError
= TRUE
;
1105 TrapFrame
->Eip
+= 3;
1109 /* Don't switch threads */
1111 /* Always use the current thread when entering the exception handler */
1112 if (NULL
!= GspDbgThread
)
1114 ObDereferenceObject(GspDbgThread
);
1115 GspDbgThread
= NULL
;
1118 /* ugly hack to avoid attempting to send status at the very
1119 * beginning, right when GDB is trying to query the stub */
1120 if (gdb_attached_yet
) {
1124 /* reply to host that an exception has occurred */
1125 SigVal
= GspComputeSignal (ExceptionRecord
->ExceptionCode
);
1127 ptr
= &GspOutBuffer
[0];
1129 *ptr
++ = 'T'; /* notify gdb with signo, PC, FP and SP */
1130 *ptr
++ = HexChars
[(SigVal
>> 4) & 0xf];
1131 *ptr
++ = HexChars
[SigVal
& 0xf];
1133 *ptr
++ = HexChars
[ESP
];
1136 Esp
= GspGetEspFromTrapFrame (TrapFrame
); /* SP */
1137 ptr
= GspMem2Hex ((PCHAR
) &Esp
, ptr
, 4, 0);
1140 *ptr
++ = HexChars
[EBP
];
1142 ptr
= GspMem2Hex ((PCHAR
) &TrapFrame
->Ebp
, ptr
, 4, 0); /* FP */
1145 *ptr
++ = HexChars
[PC
];
1147 ptr
= GspMem2Hex((PCHAR
) &TrapFrame
->Eip
, ptr
, 4, 0); /* PC */
1152 GspPutPacket (&GspOutBuffer
[0]);
1154 gdb_attached_yet
= 1;
1161 /* Zero the buffer now so we don't have to worry about the terminating zero character */
1162 memset (GspOutBuffer
, 0, sizeof (GspInBuffer
));
1163 ptr
= GspGetPacket ();
1168 /* a little hack to send more complete status information */
1170 GspOutBuffer
[0] = 'S';
1171 GspOutBuffer
[1] = HexChars
[SigVal
>> 4];
1172 GspOutBuffer
[2] = HexChars
[SigVal
% 16];
1173 GspOutBuffer
[3] = 0;
1176 GspRemoteDebug
= !GspRemoteDebug
; /* toggle debug flag */
1178 case 'g': /* return the value of the CPU Registers */
1179 if (NULL
!= GspDbgThread
)
1181 GspGetRegistersFromTrapFrame (&GspOutBuffer
[0], Context
, GspDbgThread
->Tcb
.TrapFrame
);
1185 GspGetRegistersFromTrapFrame (&GspOutBuffer
[0], Context
, TrapFrame
);
1188 case 'G': /* set the value of the CPU Registers - return OK */
1189 if (NULL
!= GspDbgThread
)
1191 GspSetRegistersInTrapFrame (ptr
, Context
, GspDbgThread
->Tcb
.TrapFrame
);
1195 GspSetRegistersInTrapFrame (ptr
, Context
, TrapFrame
);
1197 strcpy (GspOutBuffer
, "OK");
1199 case 'P': /* set the value of a single CPU register - return OK */
1203 if ((GspHex2Long (&ptr
, &Register
)) && (*ptr
++ == '='))
1204 if ((Register
>= 0) && (Register
< NUMREGS
))
1208 GspSetSingleRegisterInTrapFrame(ptr
, Register
,
1209 Context
, GspDbgThread
->Tcb
.TrapFrame
);
1213 GspSetSingleRegisterInTrapFrame (ptr
, Register
, Context
, TrapFrame
);
1215 strcpy (GspOutBuffer
, "OK");
1219 strcpy (GspOutBuffer
, "E01");
1223 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1225 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
1226 if (GspHex2Long (&ptr
, &Address
))
1227 if (*(ptr
++) == ',')
1228 if (GspHex2Long (&ptr
, &Length
))
1231 GspMemoryError
= FALSE
;
1232 GspMem2Hex ((PCHAR
) Address
, GspOutBuffer
, Length
, 1);
1235 strcpy (GspOutBuffer
, "E03");
1236 DPRINT ("Fault during memory read\n");
1241 strcpy (GspOutBuffer
, "E01");
1244 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
1246 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
1247 if (GspHex2Long (&ptr
, &Address
))
1248 if (*(ptr
++) == ',')
1249 if (GspHex2Long (&ptr
, &Length
))
1250 if (*(ptr
++) == ':')
1252 GspMemoryError
= FALSE
;
1253 GspHex2Mem (ptr
, (PCHAR
) Address
, Length
, TRUE
);
1257 strcpy (GspOutBuffer
, "E03");
1258 DPRINT ("Fault during memory write\n");
1262 strcpy (GspOutBuffer
, "OK");
1268 strcpy (GspOutBuffer
, "E02");
1271 /* cAA..AA Continue at address AA..AA(optional) */
1272 /* sAA..AA Step one instruction from AA..AA(optional) */
1277 ULONG BreakpointNumber
;
1280 /* try to read optional parameter, pc unchanged if no parm */
1281 if (GspHex2Long (&ptr
, &Address
))
1282 Context
->Eip
= Address
;
1284 NewPC
= Context
->Eip
;
1286 /* clear the trace bit */
1287 Context
->EFlags
&= 0xfffffeff;
1289 /* set the trace bit if we're Stepping */
1291 Context
->EFlags
|= 0x100;
1293 #if defined(__GNUC__)
1294 asm volatile ("movl %%db6, %0\n" : "=r" (dr6_
) : );
1295 #elif defined(_MSC_VER)
1296 __asm mov eax
, dr6 __asm mov dr6_
, eax
;
1298 #error Unknown compiler for inline assembler
1300 if (!(dr6_
& 0x4000))
1302 for (BreakpointNumber
= 0; BreakpointNumber
< 4; ++BreakpointNumber
)
1304 if (dr6_
& (1 << BreakpointNumber
))
1306 if (GspBreakpoints
[BreakpointNumber
].Type
== 0)
1308 /* Set restore flag */
1309 Context
->EFlags
|= 0x10000;
1315 GspCorrectHwBreakpoint();
1316 #if defined(__GNUC__)
1317 asm volatile ("movl %0, %%db6\n" : : "r" (0));
1318 #elif defined(_MSC_VER)
1319 __asm mov eax
, 0 __asm mov dr6
, eax
;
1321 #error Unknown compiler for inline assembler
1324 KeContextToTrapFrame(Context
, NULL
, TrapFrame
, KernelMode
);
1325 return ((SigVal
== 5) ? (kdContinue
) : (kdHandleException
));
1329 case 'k': /* kill the program */
1330 strcpy (GspOutBuffer
, "OK");
1332 /* kill the program */
1334 case 'H': /* Set thread */
1338 case 'q': /* Query */
1342 case 'T': /* Query thread status */
1343 GspQueryThreadStatus (ptr
);
1353 ptr
= &GspOutBuffer
[1];
1354 GspHex2Long (&ptr
, &Number
);
1356 GspHex2Long (&ptr
, &Type
);
1358 GspHex2Long (&ptr
, &Length
);
1360 GspHex2Long (&ptr
, &Address
);
1361 if (GspSetHwBreakpoint (Number
& 0x3, Type
& 0x3 , Length
& 0x3, Address
) == 0)
1363 strcpy (GspOutBuffer
, "OK");
1367 strcpy (GspOutBuffer
, "E");
1372 /* Remove hardware breakpoint */
1377 ptr
= &GspOutBuffer
[1];
1378 GspHex2Long(&ptr
, &Number
);
1379 if (GspRemoveHwBreakpoint (Number
& 0x3) == 0)
1381 strcpy (GspOutBuffer
, "OK");
1385 strcpy (GspOutBuffer
, "E");
1394 /* reply to the request */
1395 GspPutPacket (&GspOutBuffer
[0]);
1402 return kdDoNotHandleException
;
1408 GspBreakIn(PKINTERRUPT Interrupt
,
1409 PVOID ServiceContext
)
1411 PKTRAP_FRAME TrapFrame
;
1417 DPRINT ("Break In\n");
1420 while (KdPortGetByteEx (&GdbPortInfo
, &Value
))
1429 KeRaiseIrql (HIGH_LEVEL
, &OldIrql
);
1431 TrapFrame
= PsGetCurrentThread()->Tcb
.TrapFrame
;
1433 KeTrapFrameToContext (TrapFrame
, NULL
, &Context
);
1435 KdpGdbEnterDebuggerException (NULL
, &Context
, TrapFrame
);
1437 KeContextToTrapFrame (&Context
, NULL
, TrapFrame
, KernelMode
);
1439 KeLowerIrql (OldIrql
);
1445 extern ULONG KdpPortIrq
;
1450 KdpGdbDebugPrint(PCH Message
)
1453 /* This can be quite annoying! */
1458 GspOutBuffer
[0] = 'O';
1459 GspOutBuffer
[1] = '\0';
1460 strcat (&GspOutBuffer
[0], Message
);
1461 Length
= strlen (Message
);
1462 GspOutBuffer
[2 + Length
] = '\n';
1463 GspOutBuffer
[3 + Length
] = '\0';
1464 GspPutPacketNoWait (&GspOutBuffer
[0]);
1470 extern LIST_ENTRY ModuleListHead
;
1475 PLIST_ENTRY CurrentEntry
;
1476 PLDR_DATA_TABLE_ENTRY Current
;
1483 CurrentEntry
= ModuleListHead
.Flink
;
1484 while (CurrentEntry
!= (&ModuleListHead
))
1486 Current
= CONTAINING_RECORD (CurrentEntry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderModuleList
);
1488 DbgPrint ("Module %wZ Base 0x%.08x Length 0x%.08x\n",
1489 &Current
->BaseDllName
, Current
->DllBase
, Current
->SizeOfImage
);
1492 CurrentEntry
= CurrentEntry
->Flink
;
1495 DbgPrint ("%d modules listed\n", ModuleCount
);
1498 /* Initialize the GDB stub */
1501 KdpGdbStubInit(PKD_DISPATCH_TABLE WrapperTable
,
1504 if (!KdDebuggerEnabled
|| !KdpDebugMode
.Gdb
) return;
1508 /* Write out the functions that we support for now */
1509 WrapperTable
->KdpInitRoutine
= KdpGdbStubInit
;
1510 WrapperTable
->KdpPrintRoutine
= KdpGdbDebugPrint
;
1511 WrapperTable
->KdpExceptionRoutine
= KdpGdbEnterDebuggerException
;
1513 /* Initialize the Port */
1514 KdPortInitializeEx(&GdbPortInfo
, 0, 0);
1516 else if (BootPhase
== 1)
1518 GspInitialized
= TRUE
;
1520 GspRunThread
= NULL
;
1521 GspDbgThread
= NULL
;
1522 GspEnumThread
= NULL
;
1524 HalDisplayString("Waiting for GDB to attach\n");
1525 DbgPrint("Module 'hal.dll' loaded at 0x%.08x.\n", LdrHalBase
);
1526 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C
);
1528 else if (BootPhase
== 2)
1530 HalDisplayString("\n GDB debugging enabled\n\n");