📄 gdbstub.c
字号:
/****************************************************************************
THIS SOFTWARE IS NOT COPYRIGHTED
HP offers the following for use in the public domain. HP makes no
warranty with regard to the software or it's performance and the
user accepts the software "AS IS" with all faults.
HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
****************************************************************************/
/****************************************************************************
* Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
*
* Module name: remcom.c $
* Revision: 1.34 $
* Date: 91/03/09 12:29:49 $
* Contributor: Lake Stevens Instrument Division$
*
* Description: low level support for gdb debugger. $
*
* Considerations: only works on target hardware $
*
* Written by: Glenn Engel $
* ModuleState: Experimental $
*
* NOTES: See Below $
*
* Modified for 386 by Jim Kingdon, Cygnus Support.
* Modified for ReactOS by Casper S. Hornstrup <chorns@users.sourceforge.net>
*
* To enable debugger support, two things need to happen. One, setting
* up a routine so that it is in the exception path, is necessary in order
* to allow any breakpoints or error conditions to be properly intercepted
* and reported to gdb.
* Two, a breakpoint needs to be generated to begin communication.
*
* Because gdb will sometimes write to the stack area to execute function
* calls, this program cannot rely on using the supervisor stack so it
* uses it's own stack area.
*
*************
*
* The following gdb commands are supported:
*
* command function Return value
*
* g return the value of the CPU Registers hex data or ENN
* G set the value of the CPU Registers OK or ENN
*
* mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
* MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
*
* c Resume at current address SNN ( signal NN)
* cAA..AA Continue at address AA..AA SNN
*
* s Step one instruction SNN
* sAA..AA Step one instruction from AA..AA SNN
*
* k kill
*
* ? What was the last sigval ? SNN (signal NN)
*
* All commands and responses are sent with a packet which includes a
* Checksum. A packet consists of
*
* $<packet info>#<Checksum>.
*
* where
* <packet info> :: <characters representing the command or response>
* <Checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
*
* When a packet is received, it is first acknowledged with either '+' or '-'.
* '+' indicates a successful transfer. '-' indicates a failed transfer.
*
* Example:
*
* Host: Reply:
* $m0,10#2a +$00010203040506070809101112131415#42
*
****************************************************************************/
#include <ntoskrnl.h>
#define NDEBUG
#include <internal/debug.h>
/************************************************************************/
/* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
/* at least NUMREGBYTES*2 are needed for register packets */
#define BUFMAX 1000
static BOOLEAN GspInitialized;
static BOOLEAN GspRemoteDebug;
static CONST CHAR HexChars[]="0123456789abcdef";
static PETHREAD GspRunThread; /* NULL means run all threads */
static PETHREAD GspDbgThread;
static PETHREAD GspEnumThread;
static FAST_MUTEX GspLock;
extern LIST_ENTRY PsActiveProcessHead;
KD_PORT_INFORMATION GdbPortInfo = { 2, 115200, 0 }; /* FIXME hardcoded for COM2, 115200 baud */
/* Number of Registers. */
#define NUMREGS 16
enum REGISTER_NAMES
{
EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI,
PC /* also known as eip */,
PS /* also known as eflags */,
CS, SS, DS, ES, FS, GS
};
typedef struct _CPU_REGISTER
{
ULONG Size;
ULONG OffsetInTF;
ULONG OffsetInContext;
BOOLEAN SetInContext;
} CPU_REGISTER, *PCPU_REGISTER;
static CPU_REGISTER GspRegisters[NUMREGS] =
{
{ 4, FIELD_OFFSET(KTRAP_FRAME, Eax), FIELD_OFFSET(CONTEXT, Eax), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, Ecx), FIELD_OFFSET(CONTEXT, Ecx), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, Edx), FIELD_OFFSET(CONTEXT, Edx), FALSE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, Ebx), FIELD_OFFSET(CONTEXT, Ebx), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, HardwareEsp ), FIELD_OFFSET(CONTEXT, Esp), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, DbgEbp), FIELD_OFFSET(CONTEXT, Ebp), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, Esi), FIELD_OFFSET(CONTEXT, Esi), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, Edi), FIELD_OFFSET(CONTEXT, Edi), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, DbgEip), FIELD_OFFSET(CONTEXT, Eip), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, EFlags), FIELD_OFFSET(CONTEXT, EFlags), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, SegCs), FIELD_OFFSET(CONTEXT, SegCs), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, HardwareSegSs), FIELD_OFFSET(CONTEXT, SegSs), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, SegDs), FIELD_OFFSET(CONTEXT, SegDs), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, SegEs), FIELD_OFFSET(CONTEXT, SegEs), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, SegFs), FIELD_OFFSET(CONTEXT, SegFs), TRUE },
{ 4, FIELD_OFFSET(KTRAP_FRAME, SegGs), FIELD_OFFSET(CONTEXT, SegGs), TRUE }
};
static PCHAR GspThreadStates[DeferredReady+1] =
{
"Initialized",
"Ready",
"Running",
"Standby",
"Terminated",
"Waiting",
"Transition",
"DeferredReady"
};
LONG
HexValue(CHAR ch)
{
if ((ch >= '0') && (ch <= '9'))
{
return (ch - '0');
}
if ((ch >= 'a') && (ch <= 'f'))
{
return (ch - 'a' + 10);
}
if ((ch >= 'A') && (ch <= 'F'))
{
return (ch - 'A' + 10);
}
return -1;
}
static CHAR GspInBuffer[BUFMAX];
static CHAR GspOutBuffer[BUFMAX];
VOID
GdbPutChar(UCHAR Value)
{
KdPortPutByteEx(&GdbPortInfo, Value);
}
UCHAR
GdbGetChar(VOID)
{
UCHAR Value;
while (!KdPortGetByteEx(&GdbPortInfo, &Value))
;
return Value;
}
/* scan for the sequence $<data>#<Checksum> */
PCHAR
GspGetPacket()
{
PCHAR Buffer = &GspInBuffer[0];
CHAR Checksum;
CHAR XmitChecksum;
ULONG Count;
CHAR ch;
while (TRUE)
{
/* wait around for the start character, ignore all other characters */
while ((ch = GdbGetChar ()) != '$')
;
retry:
Checksum = 0;
XmitChecksum = -1;
Count = 0;
/* now, read until a # or end of Buffer is found */
while (Count < BUFMAX)
{
ch = GdbGetChar();
if (ch == '$')
{
goto retry;
}
if (ch == '#')
{
break;
}
Checksum = Checksum + ch;
Buffer[Count] = ch;
Count = Count + 1;
}
Buffer[Count] = 0;
if (ch == '#')
{
ch = GdbGetChar();
XmitChecksum = (CHAR)(HexValue(ch) << 4);
ch = GdbGetChar();
XmitChecksum += (CHAR)(HexValue(ch));
if (Checksum != XmitChecksum)
{
GdbPutChar('-'); /* failed checksum */
}
else
{
GdbPutChar('+'); /* successful transfer */
return &Buffer[0];
}
}
}
}
/* send the packet in Buffer. */
VOID
GspPutPacket(PCHAR Buffer)
{
CHAR Checksum;
LONG Count;
CHAR ch;
/* $<packet info>#<Checksum>. */
do
{
GdbPutChar('$');
Checksum = 0;
Count = 0;
while ((ch = Buffer[Count]))
{
GdbPutChar(ch);
Checksum += ch;
Count += 1;
}
GdbPutChar('#');
GdbPutChar(HexChars[(Checksum >> 4) & 0xf]);
GdbPutChar(HexChars[Checksum & 0xf]);
}
while (GdbGetChar() != '+');
}
VOID
GspPutPacketNoWait(PCHAR Buffer)
{
CHAR Checksum;
LONG Count;
CHAR ch;
/* $<packet info>#<Checksum>. */
GdbPutChar('$');
Checksum = 0;
Count = 0;
while ((ch = Buffer[Count]))
{
GdbPutChar(ch);
Checksum += ch;
Count += 1;
}
GdbPutChar('#');
GdbPutChar(HexChars[(Checksum >> 4) & 0xf]);
GdbPutChar(HexChars[Checksum & 0xf]);
}
/* Indicate to caller of GspMem2Hex or GspHex2Mem that there has been an
error. */
static volatile BOOLEAN GspMemoryError = FALSE;
static volatile void *GspAccessLocation = NULL;
static CHAR
GspReadMemSafe(PCHAR Address)
{
CHAR ch;
if (NULL == Address)
{
GspMemoryError = TRUE;
return '\0';
}
GspAccessLocation = Address;
ch = *Address;
GspAccessLocation = NULL;
return ch;
}
/* Convert the memory pointed to by Address into hex, placing result in Buffer */
/* Return a pointer to the last char put in Buffer (null) */
/* If MayFault is TRUE, then we should set GspMemoryError in response to
a fault; if FALSE treat a fault like any other fault in the stub. */
static PCHAR
GspMem2Hex(PCHAR Address,
PCHAR Buffer,
LONG Count,
BOOLEAN MayFault)
{
ULONG i;
CHAR ch;
for (i = 0; i < (ULONG) Count; i++)
{
if (MayFault)
{
ch = GspReadMemSafe(Address);
if (GspMemoryError)
{
return Buffer;
}
}
else
{
ch = *Address;
}
*Buffer++ = HexChars[(ch >> 4) & 0xf];
*Buffer++ = HexChars[ch & 0xf];
Address++;
}
*Buffer = 0;
return Buffer;
}
static ULONG
GspWriteMem(PCHAR Address,
ULONG Count,
BOOLEAN MayFault,
CHAR (*GetContent)(PVOID Context, ULONG Offset),
PVOID Context)
{
PCHAR Current;
PCHAR Page;
ULONG CountInPage;
ULONG i;
CHAR ch;
ULONG OldProt = 0;
Current = Address;
while (Current < Address + Count)
{
Page = (PCHAR)PAGE_ROUND_DOWN(Current);
if (Address + Count <= Page + PAGE_SIZE)
{
/* Fits in this page */
CountInPage = Count;
}
else
{
/* Flows into next page, handle only current page in this iteration */
CountInPage = PAGE_SIZE - (Address - Page);
}
if (MayFault)
{
OldProt = MmGetPageProtect(NULL, Address);
MmSetPageProtect(NULL, Address, PAGE_EXECUTE_READWRITE);
}
for (i = 0; i < CountInPage && ! GspMemoryError; i++)
{
ch = (*GetContent)(Context, Current - Address);
if (MayFault)
{
GspAccessLocation = Current;
}
*Current = ch;
if (MayFault)
{
GspAccessLocation = NULL;
}
Current++;
}
if (MayFault)
{
MmSetPageProtect(NULL, Page, OldProt);
if (GspMemoryError)
{
return Current - Address;
}
}
}
return Current - Address;
}
static CHAR
GspHex2MemGetContent(PVOID Context, ULONG Offset)
{
return (CHAR)((HexValue(*((PCHAR) Context + 2 * Offset)) << 4) +
HexValue(*((PCHAR) Context + 2 * Offset + 1)));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -