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 ****************************************************************************/
87 #include <internal/kd.h>
88 #include <internal/ke.h>
89 #include <internal/ps.h>
90 #include <internal/module.h>
91 #include <internal/ldr.h>
94 #include <internal/debug.h>
96 extern LIST_ENTRY PiThreadListHead;
99 /************************************************************************/
100 /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
101 /* at least NUMREGBYTES*2 are needed for register packets */
104 static BOOLEAN GspInitialized;
106 static PKINTERRUPT GspInterrupt;
109 static BOOLEAN GspRemoteDebug;
111 static CONST CHAR HexChars[]="0123456789abcdef";
113 static PETHREAD GspRunThread; /* NULL means run all threads */
114 static PETHREAD GspDbgThread;
115 static PETHREAD GspEnumThread;
117 /* Number of Registers. */
122 EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI,
123 PC /* also known as eip */,
124 PS /* also known as eflags */,
125 CS, SS, DS, ES, FS, GS
128 typedef struct _CPU_REGISTER
132 } CPU_REGISTER, *PCPU_REGISTER;
134 #define KTRAP_FRAME_X86 KTRAP_FRAME
138 static CPU_REGISTER GspRegisters[NUMREGS] =
140 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Eax) },
141 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Ecx) },
142 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Edx) },
143 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Ebx) },
144 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Esp) },
145 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Ebp) },
146 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Esi) },
147 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Edi) },
148 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Eip) },
149 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Eflags) },
150 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Cs) },
151 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Ss) },
152 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Ds) },
153 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Es) },
154 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Fs) },
155 { 4, FIELD_OFFSET (KTRAP_FRAME_X86, Gs) }
158 static PCHAR GspThreadStates[THREAD_STATE_MAX] =
160 "Initialized", /* THREAD_STATE_INITIALIZED */
161 "Ready", /* THREAD_STATE_READY */
162 "Running", /* THREAD_STATE_RUNNING */
163 "Suspended", /* THREAD_STATE_SUSPENDED */
164 "Frozen", /* THREAD_STATE_FROZEN */
165 "Terminated 1", /* THREAD_STATE_TERMINATED_1 */
166 "Terminated 2", /* THREAD_STATE_TERMINATED_2 */
167 "Blocked" /* THREAD_STATE_BLOCKED */
171 strtok(char *s, const char *delim)
179 if (s == NULL && (s = last) == NULL)
183 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
187 for (spanp = delim; (sc = *spanp++) != 0;) {
192 if (c == 0) { /* no non-delimiter characters */
199 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
200 * Note that delim must have one NUL; we stop if we see that, too.
206 if ((sc = *spanp++) == c) {
223 if ((ch >= '0') && (ch <= '9')) return (ch - '0');
224 if ((ch >= 'a') && (ch <= 'f')) return (ch - 'a' + 10);
225 if ((ch >= 'A') && (ch <= 'F')) return (ch - 'A' + 10);
229 static CHAR GspInBuffer[BUFMAX];
230 static CHAR GspOutBuffer[BUFMAX];
232 /* scan for the sequence $<data>#<Checksum> */
237 PCHAR Buffer = &GspInBuffer[0];
245 /* wait around for the start character, ignore all other characters */
246 while ((ch = KdGetChar ()) != '$');
253 /* now, read until a # or end of Buffer is found */
254 while (Count < BUFMAX)
261 Checksum = Checksum + ch;
270 XmitChecksum = HexValue (ch) << 4;
272 XmitChecksum += HexValue (ch);
274 if (Checksum != XmitChecksum)
276 KdPutChar ('-'); /* failed checksum */
280 KdPutChar ('+'); /* successful transfer */
282 /* if a sequence char is present, reply the sequence ID */
283 if (Buffer[2] == ':')
285 KdPutChar (Buffer[0]);
286 KdPutChar (Buffer[1]);
297 /* send the packet in Buffer. */
300 GspPutPacket (PCHAR Buffer)
306 /* $<packet info>#<Checksum>. */
313 while ((ch = Buffer[Count]))
321 KdPutChar (HexChars[(Checksum >> 4) & 0xf]);
322 KdPutChar (HexChars[Checksum & 0xf]);
324 while (KdGetChar () != '+');
329 GspPutPacketNoWait (PCHAR Buffer)
335 /* $<packet info>#<Checksum>. */
340 while ((ch = Buffer[Count]))
348 KdPutChar (HexChars[(Checksum >> 4) & 0xf]);
349 KdPutChar (HexChars[Checksum & 0xf]);
354 GspDebugError(LPSTR Message)
356 DbgPrint ("%s\n", Message);
359 /* Address of a routine to RTE to if we get a memory fault. */
360 static VOID (*volatile MemoryFaultRoutine) () = NULL;
362 /* Indicate to caller of GspMem2Hex or GspHex2Mem that there has been an
364 static volatile BOOLEAN GspMemoryError = FALSE;
367 /* Currently not used */
371 GspMemoryError = TRUE;
375 /* These are separate functions so that they are so short and sweet
376 that the compiler won't save any Registers (if there is a fault
377 to MemoryFaultRoutine, they won't get restored, so there better
378 not be any saved). */
380 GspGetChar (PCHAR Address)
387 GspSetChar (PCHAR Address,
394 /* Convert the memory pointed to by Address into hex, placing result in Buffer */
395 /* Return a pointer to the last char put in Buffer (null) */
396 /* If MayFault is TRUE, then we should set GspMemoryError in response to
397 a fault; if FALSE treat a fault like any other fault in the stub. */
399 GspMem2Hex (PCHAR Address,
408 MemoryFaultRoutine = GspSetMemoryError;
409 for (i = 0; i < (ULONG) Count; i++)
411 ch = GspGetChar (Address++);
412 if (MayFault && GspMemoryError)
414 *Buffer++ = HexChars[(ch >> 4) & 0xf];
415 *Buffer++ = HexChars[ch & 0xf];
419 MemoryFaultRoutine = NULL;
424 /* Convert the hex array pointed to by Buffer into binary to be placed at Address */
425 /* Return a pointer to the character AFTER the last byte read from Buffer */
427 GspHex2Mem (PCHAR Buffer,
436 MemoryFaultRoutine = GspSetMemoryError;
437 for (i = 0; i < Count; i++)
439 ch = HexValue (*Buffer++) << 4;
440 ch = ch + HexValue (*Buffer++);
441 GspSetChar (Address++, ch);
442 if (MayFault && GspMemoryError)
446 MemoryFaultRoutine = NULL;
451 /* This function takes the 386 exception vector and attempts to
452 translate this number into a unix compatible signal value */
454 GspComputeSignal (NTSTATUS ExceptionCode)
458 switch (ExceptionCode)
460 case STATUS_INTEGER_DIVIDE_BY_ZERO:
462 break; /* divide by zero */
463 case STATUS_SINGLE_STEP:
465 break; /* debug exception */
466 case STATUS_BREAKPOINT:
468 break; /* breakpoint */
469 case STATUS_INTEGER_OVERFLOW:
471 break; /* into instruction (overflow) */
472 case STATUS_ARRAY_BOUNDS_EXCEEDED:
474 break; /* bound instruction */
475 case STATUS_ILLEGAL_INSTRUCTION:
477 break; /* Invalid opcode */
479 case STATUS_FLT_INVALID_OPERATION:
481 break; /* coprocessor not available */
483 case STATUS_STACK_OVERFLOW:
485 break; /* stack exception */
486 case STATUS_DATATYPE_MISALIGNMENT:
488 break; /* page fault */
490 SigVal = 7; /* "software generated" */
496 /**********************************************/
497 /* WHILE WE FIND NICE HEX CHARS, BUILD A LONG */
498 /* RETURN NUMBER OF CHARS PROCESSED */
499 /**********************************************/
501 GspHex2Long (PCHAR *Address,
511 Hex = HexValue (**Address);
514 *Value = (*Value << 4) | Hex;
528 GspLong2Hex (PCHAR *Address,
533 Save = (((Value >> 0) & 0xf) << 24) |
534 (((Value >> 8) & 0xf) << 16) |
535 (((Value >> 16) & 0xf) << 8) |
536 (((Value >> 24) & 0xf) << 0);
537 *Address = GspMem2Hex ((PCHAR) &Save, *Address, 4, FALSE);
542 GspGetRegistersFromTrapFrame(PCHAR Address,
543 PKTRAP_FRAME TrapFrame)
552 Regs = &GspRegisters[0];
553 for (i = 0; i < NUMREGS; i++)
557 p = (PULONG) ((ULONG_PTR) TrapFrame + Regs[i].OffsetInTF);
560 else if (i == EIP_REGNO)
563 * This thread has not been sheduled yet so assume it
564 * is still in PsBeginThreadWithContextInternal().
566 Value = (ULONG) PsBeginThreadWithContextInternal;
572 Buffer = GspMem2Hex ((PCHAR) &Value, Buffer, Regs[i].Size, FALSE);
578 GspSetRegistersInTrapFrame(PCHAR Address,
579 PKTRAP_FRAME TrapFrame)
591 Regs = &GspRegisters[0];
592 for (i = 0; i < NUMREGS; i++)
594 p = (PULONG) ((ULONG_PTR) TrapFrame + Regs[i].OffsetInTF);
596 Buffer = GspHex2Mem (Buffer, (PCHAR) &Value, Regs[i].Size, FALSE);
603 GspSetSingleRegisterInTrapFrame(PCHAR Address,
605 PKTRAP_FRAME TrapFrame)
613 p = (PULONG) ((ULONG_PTR) TrapFrame + GspRegisters[Number].OffsetInTF);
615 GspHex2Mem (Address, (PCHAR) &Value, GspRegisters[Number].Size, FALSE);
621 GspFindThread(PCHAR Data,
624 PETHREAD ThreadInfo = NULL;
626 if (strcmp (Data, "-1") == 0)
631 else if (strcmp (Data, "0") == 0)
633 /* Pick any thread, pick the first thread,
634 * which is what most people are interested in
636 ThreadInfo = CONTAINING_RECORD (PiThreadListHead.Flink,
637 ETHREAD, Tcb.ThreadListEntry);
642 PCHAR ptr = &Data[0];
644 GspHex2Long (&ptr, (PLONG) &ThreadId);
646 if (!NT_SUCCESS (PsLookupThreadByThreadId ((PVOID) ThreadId, &ThreadInfo)))
652 *Thread = ThreadInfo;
658 GspSetThread(PCHAR Request)
661 PCHAR ptr = &Request[1];
665 case 'c': /* Run thread */
666 if (GspFindThread (ptr, &ThreadInfo))
668 GspOutBuffer[0] = 'O';
669 GspOutBuffer[1] = 'K';
670 GspRunThread = ThreadInfo;
674 GspOutBuffer[0] = 'E';
677 case 'g': /* Debug thread */
678 if (GspFindThread (ptr, &ThreadInfo))
680 GspOutBuffer[0] = 'O';
681 GspOutBuffer[1] = 'K';
682 GspDbgThread = ThreadInfo;
686 GspOutBuffer[0] = 'E';
696 GspQuery(PCHAR Request)
701 Command = strtok (Request, ",");
702 if (strncmp (Command, "C", 1) == 0)
704 PCHAR ptr = &GspOutBuffer[2];
706 /* Get current thread id */
707 GspOutBuffer[0] = 'Q';
708 GspOutBuffer[1] = 'C';
709 Value = (ULONG) GspDbgThread->Cid.UniqueThread;
710 GspLong2Hex (&ptr, Value);
712 else if (strncmp (Command, "fThreadInfo", 11) == 0)
714 PCHAR ptr = &GspOutBuffer[1];
716 /* Get first thread id */
717 GspOutBuffer[0] = 'm';
718 GspEnumThread = CONTAINING_RECORD (PiThreadListHead.Flink,
719 ETHREAD, Tcb.ThreadListEntry);
720 Value = (ULONG) GspEnumThread->Cid.UniqueThread;
721 GspLong2Hex (&ptr, Value);
723 else if (strncmp (Command, "sThreadInfo", 11) == 0)
725 PCHAR ptr = &GspOutBuffer[1];
727 /* Get next thread id */
728 if ((GspEnumThread) && (GspEnumThread->Tcb.ThreadListEntry.Flink != PiThreadListHead.Flink))
730 GspEnumThread = CONTAINING_RECORD (GspEnumThread->Tcb.ThreadListEntry.Flink,
731 ETHREAD, Tcb.ThreadListEntry);
732 GspOutBuffer[0] = 'm';
733 Value = (ULONG) GspEnumThread->Cid.UniqueThread;
734 GspLong2Hex (&ptr, Value);
738 GspOutBuffer[0] = '1';
741 else if (strncmp (Command, "ThreadExtraInfo", 15) == 0)
744 PCHAR ptr = &Command[15];
746 /* Get thread information */
747 if (GspFindThread (ptr, &ThreadInfo))
749 PCHAR String = GspThreadStates[ThreadInfo->Tcb.State];
750 GspMem2Hex (String, &GspOutBuffer[0], strlen (String), FALSE);
754 else if (strncmp (Command, "L", 1) == 0)
756 PLIST_ENTRY CurrentEntry;
758 ULONG MaxThreads = 0;
760 ULONG ThreadCount = 0;
763 GspHex2Mem (&Request[1], (PCHAR) &MaxThreads, 2, TRUE);
764 GspHex2Mem (&Request[3], (PCHAR) &Value, 4, TRUE);
765 GspHex2Mem (&Request[11], (PCHAR) &ThreadId, 4, TRUE);
767 GspOutBuffer[0] = 'q';
768 GspOutBuffer[1] = 'M';
770 GspMem2Hex ((PCHAR) &Value, &GspOutBuffer[5], 4, TRUE);
771 GspMem2Hex ((PCHAR) &ThreadId, &GspOutBuffer[13], 4, TRUE);
773 CurrentEntry = PiThreadListHead.Flink;
774 while ((CurrentEntry != &PiThreadListHead) && (ThreadCount < MaxThreads))
776 Current = CONTAINING_RECORD (CurrentEntry, ETHREAD, Tcb.ThreadListEntry);
778 GspMem2Hex ((PCHAR) &Value, &GspOutBuffer[21+ThreadCount*16], 4, TRUE);
779 Value = (ULONG) Current->Cid.UniqueThread;
780 GspMem2Hex ((PCHAR) &Value, &GspOutBuffer[21+ThreadCount*16+8], 4, TRUE);
781 CurrentEntry = CurrentEntry->Flink;
785 if (CurrentEntry != &PiThreadListHead)
787 GspOutBuffer[4] = '0';
791 GspOutBuffer[4] = '1';
794 GspMem2Hex ((PCHAR) &ThreadCount, &GspOutBuffer[2], 1, TRUE);
797 else if (strncmp (Command, "Offsets", 7) == 0)
799 strcpy (GspOutBuffer, "Text=0;Data=0;Bss=0");
804 GspQueryThreadStatus(PCHAR Request)
807 PCHAR ptr = &Request[0];
809 if (GspFindThread (ptr, &ThreadInfo))
811 GspOutBuffer[0] = 'O';
812 GspOutBuffer[1] = 'K';
813 GspOutBuffer[2] = '\0';
817 GspOutBuffer[0] = 'E';
818 GspOutBuffer[1] = '\0';
823 typedef struct _GsHwBreakPoint
831 GsHwBreakPoint GspBreakpoints[4] =
840 GspCorrectHwBreakpoint()
842 ULONG BreakpointNumber;
848 "movl %%db7, %0\n" : "=r" (dr7) : );
851 ULONG addr0, addr1, addr2, addr3;
858 : "=r" (addr0), "=r" (addr1),
859 "=r" (addr2), "=r" (addr3) : );
862 for (BreakpointNumber = 0; BreakpointNumber < 3; BreakpointNumber++)
864 Bit = 2 << (BreakpointNumber << 1);
865 if (!(dr7 & Bit) && GspBreakpoints[BreakpointNumber].Enabled) {
868 dr7 &= ~(0xf0000 << (BreakpointNumber << 2));
869 dr7 |= (((GspBreakpoints[BreakpointNumber].Length << 2) |
870 GspBreakpoints[BreakpointNumber].Type) << 16) << (BreakpointNumber << 2);
871 switch (BreakpointNumber) {
873 asm volatile ("movl %0, %%dr0\n"
874 : : "r" (GspBreakpoints[BreakpointNumber].Address) );
878 asm volatile ("movl %0, %%dr1\n"
879 : : "r" (GspBreakpoints[BreakpointNumber].Address) );
883 asm volatile ("movl %0, %%dr2\n"
884 : : "r" (GspBreakpoints[BreakpointNumber].Address) );
888 asm volatile ("movl %0, %%dr3\n"
889 : : "r" (GspBreakpoints[BreakpointNumber].Address) );
893 else if ((dr7 & Bit) && !GspBreakpoints[BreakpointNumber].Enabled)
897 dr7 &= ~(0xf0000 << (BreakpointNumber << 2));
902 asm volatile ( "movl %0, %%db7\n" : : "r" (dr7));
907 GspRemoveHwBreakpoint(ULONG BreakpointNumber)
909 if (!GspBreakpoints[BreakpointNumber].Enabled)
913 GspBreakpoints[BreakpointNumber].Enabled = 0;
919 GspSetHwBreakpoint(ULONG BreakpointNumber,
924 if (GspBreakpoints[BreakpointNumber].Enabled)
928 GspBreakpoints[BreakpointNumber].Enabled = TRUE;
929 GspBreakpoints[BreakpointNumber].Type = Type;
930 GspBreakpoints[BreakpointNumber].Length = Length;
931 GspBreakpoints[BreakpointNumber].Address = Address;
937 * This function does all command procesing for interfacing to gdb.
940 KdEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord,
942 PKTRAP_FRAME TrapFrame)
951 /* FIXME: Stop on other CPUs too */
952 /* Disable hardware debugging while we are inside the stub */
953 __asm__("movl %0,%%db7" : /* no output */ : "r" (0));
955 /* reply to host that an exception has occurred */
956 SigVal = GspComputeSignal (ExceptionRecord->ExceptionCode);
958 ptr = &GspOutBuffer[0];
960 *ptr++ = 'T'; /* notify gdb with signo, PC, FP and SP */
961 *ptr++ = HexChars[(SigVal >> 4) & 0xf];
962 *ptr++ = HexChars[SigVal & 0xf];
964 *ptr++ = HexChars[ESP];
966 ptr = GspMem2Hex ((PCHAR) &TrapFrame->Esp, ptr, 4, 0); /* SP */
969 *ptr++ = HexChars[EBP];
971 ptr = GspMem2Hex ((PCHAR) &TrapFrame->Ebp, ptr, 4, 0); /* FP */
974 *ptr++ = HexChars[PC];
976 ptr = GspMem2Hex((PCHAR) &TrapFrame->Eip, ptr, 4, 0); /* PC */
981 GspPutPacket (&GspOutBuffer[0]);
987 /* Zero the buffer now so we don't have to worry about the terminating zero character */
988 memset (GspOutBuffer, 0, sizeof (GspInBuffer));
989 ptr = GspGetPacket ();
994 GspOutBuffer[0] = 'S';
995 GspOutBuffer[1] = HexChars[SigVal >> 4];
996 GspOutBuffer[2] = HexChars[SigVal % 16];
1000 GspRemoteDebug = !GspRemoteDebug; /* toggle debug flag */
1002 case 'g': /* return the value of the CPU Registers */
1004 GspGetRegistersFromTrapFrame (&GspOutBuffer[0], GspDbgThread->Tcb.TrapFrame);
1006 GspGetRegistersFromTrapFrame (&GspOutBuffer[0], TrapFrame);
1008 case 'G': /* set the value of the CPU Registers - return OK */
1010 GspSetRegistersInTrapFrame (ptr, GspDbgThread->Tcb.TrapFrame);
1012 GspSetRegistersInTrapFrame (ptr, TrapFrame);
1013 strcpy (GspOutBuffer, "OK");
1015 case 'P': /* set the value of a single CPU register - return OK */
1019 if ((GspHex2Long (&ptr, &Register)) && (*ptr++ == '='))
1020 if ((Register >= 0) && (Register < NUMREGS))
1023 GspSetSingleRegisterInTrapFrame (ptr, Register,
1024 GspDbgThread->Tcb.TrapFrame);
1026 GspSetSingleRegisterInTrapFrame (ptr, Register, TrapFrame);
1027 strcpy (GspOutBuffer, "OK");
1031 strcpy (GspOutBuffer, "E01");
1035 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1037 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
1038 if (GspHex2Long (&ptr, &Address))
1039 if (*(ptr++) == ',')
1040 if (GspHex2Long (&ptr, &Length))
1043 GspMemoryError = FALSE;
1044 GspMem2Hex ((PCHAR) Address, GspOutBuffer, Length, 1);
1047 strcpy (GspOutBuffer, "E03");
1048 GspDebugError ("memory fault");
1054 strcpy (GspOutBuffer, "E01");
1058 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
1060 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
1061 if (GspHex2Long (&ptr, &Address))
1062 if (*(ptr++) == ',')
1063 if (GspHex2Long (&ptr, &Length))
1064 if (*(ptr++) == ':')
1066 GspMemoryError = FALSE;
1067 GspHex2Mem (ptr, (PCHAR) Address, Length, TRUE);
1071 strcpy (GspOutBuffer, "E03");
1072 GspDebugError ("memory fault");
1076 strcpy (GspOutBuffer, "OK");
1083 strcpy (GspOutBuffer, "E02");
1087 /* cAA..AA Continue at address AA..AA(optional) */
1088 /* sAA..AA Step one instruction from AA..AA(optional) */
1093 ULONG BreakpointNumber;
1096 /* try to read optional parameter, pc unchanged if no parm */
1097 if (GspHex2Long (&ptr, &Address))
1098 Context->Eip = Address;
1100 NewPC = Context->Eip;
1102 /* clear the trace bit */
1103 Context->EFlags &= 0xfffffeff;
1105 /* set the trace bit if we're Stepping */
1107 Context->EFlags |= 0x100;
1109 asm volatile ("movl %%db6, %0\n" : "=r" (dr6) : );
1110 if (!(dr6 & 0x4000))
1112 for (BreakpointNumber = 0; BreakpointNumber < 4; ++BreakpointNumber)
1114 if (dr6 & (1 << BreakpointNumber))
1116 if (GspBreakpoints[BreakpointNumber].Type == 0)
1118 /* Set restore flag */
1119 Context->EFlags |= 0x10000;
1125 GspCorrectHwBreakpoint();
1126 asm volatile ("movl %0, %%db6\n" : : "r" (0));
1128 return kdHandleException;
1131 case 'k': /* kill the program */
1132 strcpy (GspOutBuffer, "OK");
1134 /* kill the program */
1135 case 'H': /* Set thread */
1138 case 'q': /* Query */
1141 case 'T': /* Query thread status */
1142 GspQueryThreadStatus (ptr);
1151 ptr = &GspOutBuffer[1];
1152 GspHex2Long (&ptr, &Number);
1154 GspHex2Long (&ptr, &Type);
1156 GspHex2Long (&ptr, &Length);
1158 GspHex2Long (&ptr, &Address);
1159 if (GspSetHwBreakpoint (Number & 0x3, Type & 0x3 , Length & 0x3, Address) == 0)
1161 strcpy (GspOutBuffer, "OK");
1165 strcpy (GspOutBuffer, "E");
1168 /* Remove hardware breakpoint */
1174 ptr = &GspOutBuffer[1];
1175 GspHex2Long(&ptr, &Number);
1176 if (GspRemoveHwBreakpoint (Number & 0x3) == 0)
1178 strcpy (GspOutBuffer, "OK");
1182 strcpy (GspOutBuffer, "E");
1190 /* reply to the request */
1191 GspPutPacket (&GspOutBuffer[0]);
1194 return kdDoNotHandleException;
1200 GspBreakIn(PKINTERRUPT Interrupt,
1201 PVOID ServiceContext)
1203 PKTRAP_FRAME TrapFrame;
1209 DPRINT ("Break In\n");
1212 while (KdPortGetByteEx (&GdbPortInfo, &Value))
1221 KeRaiseIrql (HIGH_LEVEL, &OldIrql);
1223 TrapFrame = PsGetCurrentThread()->Tcb.TrapFrame;
1225 KeTrapFrameToContext (TrapFrame, &Context);
1227 KdEnterDebuggerException (NULL, &Context, TrapFrame);
1229 KeContextToTrapFrame (&Context, TrapFrame);
1231 KeLowerIrql (OldIrql);
1237 extern ULONG KdpPortIrq;
1239 /* Initialize the GDB stub */
1241 KdGdbStubInit(ULONG Phase)
1252 DbgPrint("Module 'hal.dll' loaded at 0x%.08x.\n", LdrHalBase);
1254 GspInitialized = TRUE;
1255 GspRunThread = PsGetCurrentThread();
1256 GspDbgThread = PsGetCurrentThread();
1257 GspEnumThread = NULL;
1259 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C);
1261 else if (Phase == 1)
1264 /* Hook an interrupt handler to allow the debugger to break into
1266 MappedIrq = HalGetInterruptVector (Internal,
1273 Status = IoConnectInterrupt(&GspInterrupt,
1284 if (!NT_SUCCESS (Status))
1286 DPRINT1("Could not connect to IRQ line %d (0x%x)\n",
1287 KdpPortIrq, Status);
1291 KdPortEnableInterrupts();
1293 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C);
1300 KdGdbDebugPrint(LPSTR Message)
1303 /* This can be quite annoying! */
1308 GspOutBuffer[0] = 'O';
1309 GspOutBuffer[1] = '\0';
1310 strcat (&GspOutBuffer[0], Message);
1311 Length = strlen (Message);
1312 GspOutBuffer[2 + Length] = '\n';
1313 GspOutBuffer[3 + Length] = '\0';
1314 GspPutPacketNoWait (&GspOutBuffer[0]);
1317 HalDisplayString(Message);
1322 extern LIST_ENTRY ModuleListHead;
1327 PLIST_ENTRY CurrentEntry;
1328 PMODULE_OBJECT Current;
1335 CurrentEntry = ModuleListHead.Flink;
1336 while (CurrentEntry != (&ModuleListHead))
1338 Current = CONTAINING_RECORD (CurrentEntry, MODULE_OBJECT, ListEntry);
1340 DbgPrint ("Module %S Base 0x%.08x Length 0x%.08x\n",
1341 Current->BaseName.Buffer, Current->Base, Current->Length);
1344 CurrentEntry = CurrentEntry->Flink;
1347 DbgPrint ("%d modules listed\n", ModuleCount);