From 7daa792a3edfc6f53c85124ae17e6481091b14fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9=20van=20Geldorp?= Date: Fri, 14 Oct 2005 19:28:18 +0000 Subject: [PATCH] Only whitespace changes, no logic svn path=/trunk/; revision=18455 --- reactos/ntoskrnl/kd/wrappers/gdbstub.c | 776 +++++++++++++------------ 1 file changed, 401 insertions(+), 375 deletions(-) diff --git a/reactos/ntoskrnl/kd/wrappers/gdbstub.c b/reactos/ntoskrnl/kd/wrappers/gdbstub.c index 5e61ab105a9..e999291b4a8 100644 --- a/reactos/ntoskrnl/kd/wrappers/gdbstub.c +++ b/reactos/ntoskrnl/kd/wrappers/gdbstub.c @@ -94,9 +94,6 @@ #define BUFMAX 1000 static BOOLEAN GspInitialized; -#if 0 -static PKINTERRUPT GspInterrupt; -#endif static BOOLEAN GspRemoteDebug; @@ -132,26 +129,27 @@ typedef struct _CPU_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, Esp), FIELD_OFFSET (CONTEXT, Esp), TRUE }, - { 4, FIELD_OFFSET (KTRAP_FRAME, DebugEbp), 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, DebugEip), FIELD_OFFSET (CONTEXT, Eip), TRUE }, - { 4, FIELD_OFFSET (KTRAP_FRAME, Eflags), FIELD_OFFSET (CONTEXT, EFlags), TRUE }, - { 4, FIELD_OFFSET (KTRAP_FRAME, Cs), FIELD_OFFSET (CONTEXT, SegCs), TRUE }, - { 4, FIELD_OFFSET (KTRAP_FRAME, Ss), FIELD_OFFSET (CONTEXT, SegSs), TRUE }, - { 4, FIELD_OFFSET (KTRAP_FRAME, Ds), FIELD_OFFSET (CONTEXT, SegDs), TRUE }, - { 4, FIELD_OFFSET (KTRAP_FRAME, Es), FIELD_OFFSET (CONTEXT, SegEs), TRUE }, - { 4, FIELD_OFFSET (KTRAP_FRAME, Fs), FIELD_OFFSET (CONTEXT, SegFs), TRUE }, - { 4, FIELD_OFFSET (KTRAP_FRAME, Gs), FIELD_OFFSET (CONTEXT, SegGs), TRUE } + { 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, Esp), FIELD_OFFSET(CONTEXT, Esp), TRUE }, + { 4, FIELD_OFFSET(KTRAP_FRAME, DebugEbp), 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, DebugEip), FIELD_OFFSET(CONTEXT, Eip), TRUE }, + { 4, FIELD_OFFSET(KTRAP_FRAME, Eflags), FIELD_OFFSET(CONTEXT, EFlags), TRUE }, + { 4, FIELD_OFFSET(KTRAP_FRAME, Cs), FIELD_OFFSET(CONTEXT, SegCs), TRUE }, + { 4, FIELD_OFFSET(KTRAP_FRAME, Ss), FIELD_OFFSET(CONTEXT, SegSs), TRUE }, + { 4, FIELD_OFFSET(KTRAP_FRAME, Ds), FIELD_OFFSET(CONTEXT, SegDs), TRUE }, + { 4, FIELD_OFFSET(KTRAP_FRAME, Es), FIELD_OFFSET(CONTEXT, SegEs), TRUE }, + { 4, FIELD_OFFSET(KTRAP_FRAME, Fs), FIELD_OFFSET(CONTEXT, SegFs), TRUE }, + { 4, FIELD_OFFSET(KTRAP_FRAME, Gs), FIELD_OFFSET(CONTEXT, SegGs), TRUE } }; static PCHAR GspThreadStates[DeferredReady+1] = -{ "Initialized", +{ + "Initialized", "Ready", "Running", "Standby", @@ -163,12 +161,22 @@ static PCHAR GspThreadStates[DeferredReady+1] = LONG -HexValue (CHAR ch) +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); + 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]; @@ -177,7 +185,7 @@ static CHAR GspOutBuffer[BUFMAX]; VOID GdbPutChar(UCHAR Value) { - KdPortPutByteEx (&GdbPortInfo, Value); + KdPortPutByteEx(&GdbPortInfo, Value); } UCHAR @@ -185,7 +193,8 @@ GdbGetChar(VOID) { UCHAR Value; - while (!KdPortGetByteEx (&GdbPortInfo, &Value)); + while (!KdPortGetByteEx(&GdbPortInfo, &Value)) + ; return Value; } @@ -204,7 +213,8 @@ GspGetPacket() while (TRUE) { /* wait around for the start character, ignore all other characters */ - while ((ch = GdbGetChar ()) != '$'); + while ((ch = GdbGetChar ()) != '$') + ; retry: Checksum = 0; @@ -214,11 +224,15 @@ GspGetPacket() /* now, read until a # or end of Buffer is found */ while (Count < BUFMAX) { - ch = GdbGetChar (); + ch = GdbGetChar(); if (ch == '$') - goto retry; + { + goto retry; + } if (ch == '#') - break; + { + break; + } Checksum = Checksum + ch; Buffer[Count] = ch; Count = Count + 1; @@ -227,18 +241,18 @@ GspGetPacket() if (ch == '#') { - ch = GdbGetChar (); - XmitChecksum = (CHAR)(HexValue (ch) << 4); - ch = GdbGetChar (); - XmitChecksum += (CHAR)(HexValue (ch)); + ch = GdbGetChar(); + XmitChecksum = (CHAR)(HexValue(ch) << 4); + ch = GdbGetChar(); + XmitChecksum += (CHAR)(HexValue(ch)); if (Checksum != XmitChecksum) { - GdbPutChar ('-'); /* failed checksum */ + GdbPutChar('-'); /* failed checksum */ } else { - GdbPutChar ('+'); /* successful transfer */ + GdbPutChar('+'); /* successful transfer */ return &Buffer[0]; } @@ -249,7 +263,7 @@ GspGetPacket() /* send the packet in Buffer. */ VOID -GspPutPacket (PCHAR Buffer) +GspPutPacket(PCHAR Buffer) { CHAR Checksum; LONG Count; @@ -258,47 +272,47 @@ GspPutPacket (PCHAR Buffer) /* $#. */ do { - GdbPutChar ('$'); + GdbPutChar('$'); Checksum = 0; Count = 0; while ((ch = Buffer[Count])) { - GdbPutChar (ch); + GdbPutChar(ch); Checksum += ch; Count += 1; } - GdbPutChar ('#'); - GdbPutChar (HexChars[(Checksum >> 4) & 0xf]); - GdbPutChar (HexChars[Checksum & 0xf]); + GdbPutChar('#'); + GdbPutChar(HexChars[(Checksum >> 4) & 0xf]); + GdbPutChar(HexChars[Checksum & 0xf]); } - while (GdbGetChar () != '+'); + while (GdbGetChar() != '+'); } VOID -GspPutPacketNoWait (PCHAR Buffer) +GspPutPacketNoWait(PCHAR Buffer) { CHAR Checksum; LONG Count; CHAR ch; /* $#. */ - GdbPutChar ('$'); + GdbPutChar('$'); Checksum = 0; Count = 0; while ((ch = Buffer[Count])) { - GdbPutChar (ch); + GdbPutChar(ch); Checksum += ch; Count += 1; } - GdbPutChar ('#'); - GdbPutChar (HexChars[(Checksum >> 4) & 0xf]); - GdbPutChar (HexChars[Checksum & 0xf]); + GdbPutChar('#'); + GdbPutChar(HexChars[(Checksum >> 4) & 0xf]); + GdbPutChar(HexChars[Checksum & 0xf]); } /* Indicate to caller of GspMem2Hex or GspHex2Mem that there has been an @@ -312,7 +326,7 @@ static volatile void *GspAccessLocation = 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. */ PCHAR -GspMem2Hex (PCHAR Address, +GspMem2Hex(PCHAR Address, PCHAR Buffer, LONG Count, BOOLEAN MayFault) @@ -329,26 +343,28 @@ GspMem2Hex (PCHAR Address, for (i = 0; i < (ULONG) Count; i++) { if (MayFault) - GspAccessLocation = Address; + { + GspAccessLocation = Address; + } ch = *Address; GspAccessLocation = NULL; if (MayFault && GspMemoryError) { - return (Buffer); + return Buffer; } *Buffer++ = HexChars[(ch >> 4) & 0xf]; *Buffer++ = HexChars[ch & 0xf]; Address++; } *Buffer = 0; - return (Buffer); + return Buffer; } /* Convert the hex array pointed to by Buffer into binary to be placed at Address */ /* Return a pointer to the character AFTER the last byte read from Buffer */ PCHAR -GspHex2Mem (PCHAR Buffer, +GspHex2Mem(PCHAR Buffer, PCHAR Address, ULONG Count, BOOLEAN MayFault) @@ -363,7 +379,7 @@ GspHex2Mem (PCHAR Buffer, current = Address; while ( current < Address + Count ) { - page = (PCHAR)PAGE_ROUND_DOWN (current); + page = (PCHAR)PAGE_ROUND_DOWN(current); if (Address + Count <= page + PAGE_SIZE) { /* Fits in this page */ @@ -376,14 +392,14 @@ GspHex2Mem (PCHAR Buffer, } if (MayFault) { - oldprot = MmGetPageProtect (NULL, Address); - MmSetPageProtect (NULL, Address, PAGE_EXECUTE_READWRITE); + oldprot = MmGetPageProtect(NULL, Address); + MmSetPageProtect(NULL, Address, PAGE_EXECUTE_READWRITE); } for (i = 0; i < countinpage && ! GspMemoryError; i++) { - ch = (CHAR)(HexValue (*Buffer++) << 4); - ch = (CHAR)(ch + HexValue (*Buffer++)); + ch = (CHAR)(HexValue(*Buffer++) << 4); + ch = (CHAR)(ch + HexValue(*Buffer++)); GspAccessLocation = current; *current = ch; @@ -392,22 +408,22 @@ GspHex2Mem (PCHAR Buffer, } if (MayFault) { - MmSetPageProtect (NULL, page, oldprot); + MmSetPageProtect(NULL, page, oldprot); if (GspMemoryError) { - return (Buffer); + return Buffer; } } } - return (Buffer); + return Buffer; } /* This function takes the 386 exception vector and attempts to translate this number into a unix compatible signal value */ ULONG -GspComputeSignal (NTSTATUS ExceptionCode) +GspComputeSignal(NTSTATUS ExceptionCode) { ULONG SigVal; @@ -427,11 +443,6 @@ GspComputeSignal (NTSTATUS ExceptionCode) case STATUS_ILLEGAL_INSTRUCTION: SigVal = 4; /* Invalid opcode */ break; -#if 0 - case STATUS_FLT_INVALID_OPERATION: - SigVal = 8; - break; -#endif case STATUS_STACK_OVERFLOW: case STATUS_DATATYPE_MISALIGNMENT: case STATUS_ACCESS_VIOLATION: @@ -449,7 +460,7 @@ GspComputeSignal (NTSTATUS ExceptionCode) /* RETURN NUMBER OF CHARS PROCESSED */ /**********************************************/ LONG -GspHex2Long (PCHAR *Address, +GspHex2Long(PCHAR *Address, PLONG Value) { LONG NumChars = 0; @@ -459,7 +470,7 @@ GspHex2Long (PCHAR *Address, while (**Address) { - Hex = HexValue (**Address); + Hex = HexValue(**Address); if (Hex >= 0) { *Value = (*Value << 4) | Hex; @@ -473,12 +484,12 @@ GspHex2Long (PCHAR *Address, (*Address)++; } - return (NumChars); + return NumChars; } VOID -GspLong2Hex (PCHAR *Address, +GspLong2Hex(PCHAR *Address, LONG Value) { LONG Save; @@ -487,7 +498,7 @@ GspLong2Hex (PCHAR *Address, (((Value >> 8) & 0xff) << 16) | (((Value >> 16) & 0xff) << 8) | (((Value >> 24) & 0xff) << 0); - *Address = GspMem2Hex ((PCHAR) &Save, *Address, 4, FALSE); + *Address = GspMem2Hex((PCHAR) &Save, *Address, 4, FALSE); } @@ -500,7 +511,6 @@ GspLong2Hex (PCHAR *Address, static LONG GspGetEspFromTrapFrame(PKTRAP_FRAME TrapFrame) { - return KeGetPreviousMode() == KernelMode ? (LONG) &TrapFrame->Esp : (LONG)TrapFrame->Esp; } @@ -517,34 +527,34 @@ GspGetRegistersFromTrapFrame(PCHAR Address, DWORD i; Buffer = Address; - for (i = 0; i < sizeof (GspRegisters) / sizeof (GspRegisters[0]); i++) - { - if (TrapFrame) + for (i = 0; i < sizeof(GspRegisters) / sizeof(GspRegisters[0]); i++) { - if (ESP == i) - { - Value = GspGetEspFromTrapFrame (TrapFrame); - } + if (TrapFrame) + { + if (ESP == i) + { + Value = GspGetEspFromTrapFrame(TrapFrame); + } + else + { + p = (PULONG)((ULONG_PTR) TrapFrame + GspRegisters[i].OffsetInTF); + Value = *p; + } + } + else if (i == EIP_REGNO) + { + /* + * This thread has not been sheduled yet so assume it + * is still in PsBeginThreadWithContextInternal(). + */ + Value = (ULONG)KiThreadStartup; + } else - { - p = (PULONG) ((ULONG_PTR) TrapFrame + GspRegisters[i].OffsetInTF); - Value = *p; - } + { + Value = 0; + } + Buffer = GspMem2Hex((PCHAR) &Value, Buffer, GspRegisters[i].Size, FALSE); } - else if (i == EIP_REGNO) - { - /* - * This thread has not been sheduled yet so assume it - * is still in PsBeginThreadWithContextInternal(). - */ - Value = (ULONG)KiThreadStartup; - } - else - { - Value = 0; - } - Buffer = GspMem2Hex ((PCHAR) &Value, Buffer, GspRegisters[i].Size, FALSE); - } } @@ -559,19 +569,25 @@ GspSetRegistersInTrapFrame(PCHAR Address, DWORD i; if (!TrapFrame) - return; + { + return; + } Buffer = Address; for (i = 0; i < NUMREGS; i++) - { - if (GspRegisters[i].SetInContext) - p = (PULONG) ((ULONG_PTR) Context + GspRegisters[i].OffsetInContext); - else - p = (PULONG) ((ULONG_PTR) TrapFrame + GspRegisters[i].OffsetInTF); - Value = 0; - Buffer = GspHex2Mem (Buffer, (PCHAR) &Value, GspRegisters[i].Size, FALSE); - *p = Value; - } + { + if (GspRegisters[i].SetInContext) + { + p = (PULONG) ((ULONG_PTR) Context + GspRegisters[i].OffsetInContext); + } + else + { + p = (PULONG) ((ULONG_PTR) TrapFrame + GspRegisters[i].OffsetInTF); + } + Value = 0; + Buffer = GspHex2Mem(Buffer, (PCHAR) &Value, GspRegisters[i].Size, FALSE); + *p = Value; + } } @@ -585,14 +601,20 @@ GspSetSingleRegisterInTrapFrame(PCHAR Address, PULONG p; if (!TrapFrame) - return; + { + return; + } if (GspRegisters[Number].SetInContext) - p = (PULONG) ((ULONG_PTR) Context + GspRegisters[Number].OffsetInContext); + { + p = (PULONG) ((ULONG_PTR) Context + GspRegisters[Number].OffsetInContext); + } else - p = (PULONG) ((ULONG_PTR) TrapFrame + GspRegisters[Number].OffsetInTF); + { + p = (PULONG) ((ULONG_PTR) TrapFrame + GspRegisters[Number].OffsetInTF); + } Value = 0; - GspHex2Mem (Address, (PCHAR) &Value, GspRegisters[Number].Size, FALSE); + GspHex2Mem(Address, (PCHAR) &Value, GspRegisters[Number].Size, FALSE); *p = Value; } @@ -608,16 +630,16 @@ GspFindThread(PCHAR Data, /* All threads */ ThreadInfo = NULL; } - else + else { ULONG uThreadId; HANDLE ThreadId; PCHAR ptr = &Data[0]; - GspHex2Long (&ptr, (PLONG) &uThreadId); + GspHex2Long(&ptr, (PLONG) &uThreadId); ThreadId = (HANDLE)uThreadId; - if (!NT_SUCCESS (PsLookupThreadByThreadId (ThreadId, &ThreadInfo))) + if (!NT_SUCCESS(PsLookupThreadByThreadId(ThreadId, &ThreadInfo))) { *Thread = NULL; return FALSE; @@ -658,17 +680,25 @@ GspSetThread(PCHAR Request) } break; case 'g': /* Debug thread */ - if (GspFindThread (ptr, &ThreadInfo)) + if (GspFindThread(ptr, &ThreadInfo)) { GspOutBuffer[0] = 'O'; GspOutBuffer[1] = 'K'; - if (GspDbgThread) + if (NULL != GspDbgThread) { ObDereferenceObject(GspDbgThread); } - GspDbgThread = ThreadInfo; + if (ThreadInfo == PsGetCurrentThread()) + { + GspDbgThread = NULL; + ObDereferenceObject(ThreadInfo); + } + else + { + GspDbgThread = ThreadInfo; + } } else { @@ -687,140 +717,135 @@ GspQuery(PCHAR Request) ULONG Value; if (strncmp(Request, "C", 1) == 0) - { - PCHAR ptr = &GspOutBuffer[2]; + { + PCHAR ptr = &GspOutBuffer[2]; - /* Get current thread id */ - GspOutBuffer[0] = 'Q'; - GspOutBuffer[1] = 'C'; - if (NULL != GspDbgThread) - { - Value = (ULONG) GspDbgThread->Cid.UniqueThread; - } - else - { - Value = (ULONG) PsGetCurrentThread()->Cid.UniqueThread; - } - GspLong2Hex (&ptr, Value); - } - else if (strncmp (Request, "fThreadInfo", 11) == 0) - { - PEPROCESS Process; - PLIST_ENTRY AThread, AProcess; - PCHAR ptr = &GspOutBuffer[1]; - - /* Get first thread id */ - GspEnumThread = NULL; - AProcess = PsActiveProcessHead.Flink; - while(AProcess != &PsActiveProcessHead) - { - Process = CONTAINING_RECORD(AProcess, EPROCESS, ActiveProcessLinks); - AThread = Process->ThreadListHead.Flink; - if(AThread != &Process->ThreadListHead) - { - GspEnumThread = CONTAINING_RECORD (Process->ThreadListHead.Flink, - ETHREAD, ThreadListEntry); - break; - } - AProcess = AProcess->Flink; - } - if(GspEnumThread != NULL) - { - GspOutBuffer[0] = 'm'; - Value = (ULONG) GspEnumThread->Cid.UniqueThread; - GspLong2Hex (&ptr, Value); - } - else - { - /* FIXME - what to do here? This case should never happen though, there - should always be at least one thread on the system... */ - /* GspOutBuffer[0] = 'l'; */ - } - } - else if (strncmp (Request, "sThreadInfo", 11) == 0) - { - PEPROCESS Process; - PLIST_ENTRY AThread, AProcess; - PCHAR ptr = &GspOutBuffer[1]; - - /* Get next thread id */ - if (GspEnumThread != NULL) - { - /* find the next thread */ - Process = GspEnumThread->ThreadsProcess; - if(GspEnumThread->ThreadListEntry.Flink != &Process->ThreadListHead) - { - GspEnumThread = CONTAINING_RECORD (GspEnumThread->ThreadListEntry.Flink, - ETHREAD, ThreadListEntry); - } + /* Get current thread id */ + GspOutBuffer[0] = 'Q'; + GspOutBuffer[1] = 'C'; + if (NULL != GspDbgThread) + { + Value = (ULONG) GspDbgThread->Cid.UniqueThread; + } else - { - PETHREAD Thread = NULL; - AProcess = Process->ActiveProcessLinks.Flink; - while(AProcess != &PsActiveProcessHead) + { + Value = (ULONG) PsGetCurrentThread()->Cid.UniqueThread; + } + GspLong2Hex(&ptr, Value); + } + else if (strncmp(Request, "fThreadInfo", 11) == 0) + { + PEPROCESS Process; + PLIST_ENTRY AThread, AProcess; + PCHAR ptr = &GspOutBuffer[1]; + + /* Get first thread id */ + GspEnumThread = NULL; + AProcess = PsActiveProcessHead.Flink; + while(AProcess != &PsActiveProcessHead) { Process = CONTAINING_RECORD(AProcess, EPROCESS, ActiveProcessLinks); AThread = Process->ThreadListHead.Flink; - if(AThread != &Process->ThreadListHead) - { - Thread = CONTAINING_RECORD (Process->ThreadListHead.Flink, - ETHREAD, ThreadListEntry); - break; - } + if (AThread != &Process->ThreadListHead) + { + GspEnumThread = CONTAINING_RECORD(Process->ThreadListHead.Flink, + ETHREAD, ThreadListEntry); + break; + } AProcess = AProcess->Flink; } - GspEnumThread = Thread; - } - if(GspEnumThread != NULL) - { - /* return the ID */ - GspOutBuffer[0] = 'm'; - Value = (ULONG) GspEnumThread->Cid.UniqueThread; - GspLong2Hex (&ptr, Value); - } + { + GspOutBuffer[0] = 'm'; + Value = (ULONG) GspEnumThread->Cid.UniqueThread; + GspLong2Hex(&ptr, Value); + } else - { - GspOutBuffer[0] = 'l'; - } + { + /* FIXME - what to do here? This case should never happen though, there + should always be at least one thread on the system... */ + /* GspOutBuffer[0] = 'l'; */ + } } - else + else if (strncmp(Request, "sThreadInfo", 11) == 0) { - GspOutBuffer[0] = 'l'; - } - } - else if (strncmp (Request, "ThreadExtraInfo", 15) == 0) - { - PETHREAD ThreadInfo; + PEPROCESS Process; + PLIST_ENTRY AThread, AProcess; + PCHAR ptr = &GspOutBuffer[1]; - /* Get thread information */ - if (GspFindThread(Request + 16, &ThreadInfo)) + /* Get next thread id */ + if (GspEnumThread != NULL) + { + /* find the next thread */ + Process = GspEnumThread->ThreadsProcess; + if(GspEnumThread->ThreadListEntry.Flink != &Process->ThreadListHead) + { + GspEnumThread = CONTAINING_RECORD(GspEnumThread->ThreadListEntry.Flink, + ETHREAD, ThreadListEntry); + } + else + { + PETHREAD Thread = NULL; + AProcess = Process->ActiveProcessLinks.Flink; + while(AProcess != &PsActiveProcessHead) + { + Process = CONTAINING_RECORD(AProcess, EPROCESS, ActiveProcessLinks); + AThread = Process->ThreadListHead.Flink; + if (AThread != &Process->ThreadListHead) + { + Thread = CONTAINING_RECORD(Process->ThreadListHead.Flink, + ETHREAD, ThreadListEntry); + break; + } + AProcess = AProcess->Flink; + } + GspEnumThread = Thread; + } + + if (GspEnumThread != NULL) + { + /* return the ID */ + GspOutBuffer[0] = 'm'; + Value = (ULONG) GspEnumThread->Cid.UniqueThread; + GspLong2Hex(&ptr, Value); + } + else + { + GspOutBuffer[0] = 'l'; + } + } + else + { + GspOutBuffer[0] = 'l'; + } + } + else if (strncmp(Request, "ThreadExtraInfo", 15) == 0) { - char Buffer[64]; - PEPROCESS Proc; + PETHREAD ThreadInfo; - Proc = (PEPROCESS) ThreadInfo->Tcb.ApcState.Process; + /* Get thread information */ + if (GspFindThread(Request + 16, &ThreadInfo)) + { + char Buffer[64]; + PEPROCESS Proc; - Buffer[0] = '\0'; - if (NULL != Proc ) - { - sprintf(Buffer, "%s [%d:0x%x], ", Proc->ImageFileName, - (int) Proc->UniqueProcessId, - (int) ThreadInfo->Cid.UniqueThread); - } - strcpy(Buffer + strlen(Buffer), GspThreadStates[ThreadInfo->Tcb.State]); + Proc = (PEPROCESS) ThreadInfo->Tcb.ApcState.Process; - ObDereferenceObject(ThreadInfo); + Buffer[0] = '\0'; + if (NULL != Proc ) + { + sprintf(Buffer, "%s [%d:0x%x], ", Proc->ImageFileName, + (int) Proc->UniqueProcessId, + (int) ThreadInfo->Cid.UniqueThread); + } + strcpy(Buffer + strlen(Buffer), + GspThreadStates[ThreadInfo->Tcb.State]); - GspMem2Hex(Buffer, &GspOutBuffer[0], strlen(Buffer), FALSE); + ObDereferenceObject(ThreadInfo); + + GspMem2Hex(Buffer, &GspOutBuffer[0], strlen(Buffer), FALSE); + } } - } -#if 0 - else if (strncmp (Request, "Offsets", 7) == 0) - { - strcpy (GspOutBuffer, "Text=0;Data=0;Bss=0"); - } -#endif } VOID @@ -829,19 +854,19 @@ GspQueryThreadStatus(PCHAR Request) PETHREAD ThreadInfo; PCHAR ptr = &Request[0]; - if (GspFindThread (ptr, &ThreadInfo)) - { - ObDereferenceObject(ThreadInfo); + if (GspFindThread(ptr, &ThreadInfo)) + { + ObDereferenceObject(ThreadInfo); - GspOutBuffer[0] = 'O'; - GspOutBuffer[1] = 'K'; - GspOutBuffer[2] = '\0'; - } + GspOutBuffer[0] = 'O'; + GspOutBuffer[1] = 'K'; + GspOutBuffer[2] = '\0'; + } else - { - GspOutBuffer[0] = 'E'; - GspOutBuffer[1] = '\0'; - } + { + GspOutBuffer[0] = 'E'; + GspOutBuffer[1] = '\0'; + } } @@ -1061,11 +1086,10 @@ KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord, } else { - /* Don't switch threads */ - - /* Always use the current thread when entering the exception handler */ + /* Make sure we're debugging the current thread. */ if (NULL != GspDbgThread) { + DPRINT1("Internal error: entering stub with non-NULL GspDbgThread\n"); ObDereferenceObject(GspDbgThread); GspDbgThread = NULL; } @@ -1078,7 +1102,7 @@ KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord, stop_reply: /* reply to host that an exception has occurred */ - SigVal = GspComputeSignal (ExceptionRecord->ExceptionCode); + SigVal = GspComputeSignal(ExceptionRecord->ExceptionCode); ptr = &GspOutBuffer[0]; @@ -1089,13 +1113,13 @@ KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord, *ptr++ = HexChars[ESP]; *ptr++ = ':'; - Esp = GspGetEspFromTrapFrame (TrapFrame); /* SP */ - ptr = GspMem2Hex ((PCHAR) &Esp, ptr, 4, 0); + Esp = GspGetEspFromTrapFrame(TrapFrame); /* SP */ + ptr = GspMem2Hex((PCHAR) &Esp, ptr, 4, 0); *ptr++ = ';'; *ptr++ = HexChars[EBP]; *ptr++ = ':'; - ptr = GspMem2Hex ((PCHAR) &TrapFrame->Ebp, ptr, 4, 0); /* FP */ + ptr = GspMem2Hex((PCHAR) &TrapFrame->Ebp, ptr, 4, 0); /* FP */ *ptr++ = ';'; *ptr++ = HexChars[PC]; @@ -1105,7 +1129,7 @@ KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord, *ptr = '\0'; - GspPutPacket (&GspOutBuffer[0]); + GspPutPacket(&GspOutBuffer[0]); } else { @@ -1117,10 +1141,10 @@ KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord, while (TRUE) { /* Zero the buffer now so we don't have to worry about the terminating zero character */ - memset (GspOutBuffer, 0, sizeof (GspInBuffer)); - ptr = GspGetPacket (); + memset(GspOutBuffer, 0, sizeof(GspInBuffer)); + ptr = GspGetPacket(); - switch (*ptr++) + switch(*ptr++) { case '?': /* a little hack to send more complete status information */ @@ -1136,94 +1160,105 @@ KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord, case 'g': /* return the value of the CPU Registers */ if (NULL != GspDbgThread) { - GspGetRegistersFromTrapFrame (&GspOutBuffer[0], Context, GspDbgThread->Tcb.TrapFrame); + GspGetRegistersFromTrapFrame(&GspOutBuffer[0], Context, GspDbgThread->Tcb.TrapFrame); } else { - GspGetRegistersFromTrapFrame (&GspOutBuffer[0], Context, TrapFrame); + GspGetRegistersFromTrapFrame(&GspOutBuffer[0], Context, TrapFrame); } break; case 'G': /* set the value of the CPU Registers - return OK */ if (NULL != GspDbgThread) { - GspSetRegistersInTrapFrame (ptr, Context, GspDbgThread->Tcb.TrapFrame); + GspSetRegistersInTrapFrame(ptr, Context, GspDbgThread->Tcb.TrapFrame); } else { - GspSetRegistersInTrapFrame (ptr, Context, TrapFrame); + GspSetRegistersInTrapFrame(ptr, Context, TrapFrame); } - strcpy (GspOutBuffer, "OK"); + strcpy(GspOutBuffer, "OK"); break; case 'P': /* set the value of a single CPU register - return OK */ { LONG Register; - if ((GspHex2Long (&ptr, &Register)) && (*ptr++ == '=')) - if ((Register >= 0) && (Register < NUMREGS)) - { - if (GspDbgThread) - { - GspSetSingleRegisterInTrapFrame(ptr, Register, - Context, GspDbgThread->Tcb.TrapFrame); - } - else - { - GspSetSingleRegisterInTrapFrame (ptr, Register, Context, TrapFrame); - } - strcpy (GspOutBuffer, "OK"); - break; - } + if ((GspHex2Long(&ptr, &Register)) && (*ptr++ == '=')) + { + if ((Register >= 0) && (Register < NUMREGS)) + { + if (GspDbgThread) + { + GspSetSingleRegisterInTrapFrame(ptr, Register, + Context, + GspDbgThread->Tcb.TrapFrame); + } + else + { + GspSetSingleRegisterInTrapFrame(ptr, Register, + Context, TrapFrame); + } + strcpy(GspOutBuffer, "OK"); + break; + } + } - strcpy (GspOutBuffer, "E01"); + strcpy(GspOutBuffer, "E01"); break; } /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */ case 'm': /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */ - if (GspHex2Long (&ptr, &Address)) - if (*(ptr++) == ',') - if (GspHex2Long (&ptr, &Length)) + if (GspHex2Long(&ptr, &Address) && + *(ptr++) == ',' && + GspHex2Long(&ptr, &Length)) + { + ptr = 0; + GspMemoryError = FALSE; + GspMem2Hex((PCHAR) Address, GspOutBuffer, Length, 1); + if (GspMemoryError) { - ptr = 0; - GspMemoryError = FALSE; - GspMem2Hex ((PCHAR) Address, GspOutBuffer, Length, 1); - if (GspMemoryError) - { - strcpy (GspOutBuffer, "E03"); - DPRINT ("Fault during memory read\n"); - } + strcpy(GspOutBuffer, "E03"); + DPRINT("Fault during memory read\n"); } + } if (ptr) - strcpy (GspOutBuffer, "E01"); + { + strcpy(GspOutBuffer, "E01"); + } break; /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */ case 'M': /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */ - if (GspHex2Long (&ptr, &Address)) - if (*(ptr++) == ',') - if (GspHex2Long (&ptr, &Length)) - if (*(ptr++) == ':') - { - GspMemoryError = FALSE; - GspHex2Mem (ptr, (PCHAR) Address, Length, TRUE); + if (GspHex2Long(&ptr, &Address)) + { + if (*(ptr++) == ',' && + GspHex2Long(&ptr, &Length) && + *(ptr++) == ':') + { + GspMemoryError = FALSE; + GspHex2Mem(ptr, (PCHAR) Address, Length, TRUE); - if (GspMemoryError) - { - strcpy (GspOutBuffer, "E03"); - DPRINT ("Fault during memory write\n"); - } - else - { - strcpy (GspOutBuffer, "OK"); - } + if (GspMemoryError) + { + strcpy(GspOutBuffer, "E03"); + DPRINT("Fault during memory write\n"); + } + else + { + strcpy(GspOutBuffer, "OK"); + } - ptr = NULL; - } - if (ptr) - strcpy (GspOutBuffer, "E02"); + ptr = NULL; + } + } + + if (NULL != ptr) + { + strcpy(GspOutBuffer, "E02"); + } break; /* cAA..AA Continue at address AA..AA(optional) */ @@ -1237,7 +1272,9 @@ KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord, /* try to read optional parameter, pc unchanged if no parm */ if (GspHex2Long (&ptr, &Address)) - Context->Eip = Address; + { + Context->Eip = Address; + } NewPC = Context->Eip; @@ -1246,7 +1283,9 @@ KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord, /* set the trace bit if we're Stepping */ if (Stepping) - Context->EFlags |= 0x100; + { + Context->EFlags |= 0x100; + } #if defined(__GNUC__) asm volatile ("movl %%db6, %0\n" : "=r" (dr6_) : ); @@ -1279,25 +1318,31 @@ KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord, #error Unknown compiler for inline assembler #endif + if (NULL != GspDbgThread) + { + ObDereferenceObject(GspDbgThread); + GspDbgThread = NULL; + } + return kdContinue; break; } case 'k': /* kill the program */ - strcpy (GspOutBuffer, "OK"); + strcpy(GspOutBuffer, "OK"); break; /* kill the program */ case 'H': /* Set thread */ - GspSetThread (ptr); + GspSetThread(ptr); break; case 'q': /* Query */ - GspQuery (ptr); + GspQuery(ptr); break; case 'T': /* Query thread status */ - GspQueryThreadStatus (ptr); + GspQueryThreadStatus(ptr); break; case 'Y': @@ -1308,20 +1353,20 @@ KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord, LONG Address; ptr = &GspOutBuffer[1]; - GspHex2Long (&ptr, &Number); + GspHex2Long(&ptr, &Number); ptr++; - GspHex2Long (&ptr, &Type); + GspHex2Long(&ptr, &Type); ptr++; - GspHex2Long (&ptr, &Length); + GspHex2Long(&ptr, &Length); ptr++; - GspHex2Long (&ptr, &Address); - if (GspSetHwBreakpoint (Number & 0x3, Type & 0x3 , Length & 0x3, Address) == 0) + GspHex2Long(&ptr, &Address); + if (GspSetHwBreakpoint(Number & 0x3, Type & 0x3 , Length & 0x3, Address) == 0) { - strcpy (GspOutBuffer, "OK"); + strcpy(GspOutBuffer, "OK"); } else { - strcpy (GspOutBuffer, "E"); + strcpy(GspOutBuffer, "E"); } break; } @@ -1333,13 +1378,13 @@ KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord, ptr = &GspOutBuffer[1]; GspHex2Long(&ptr, &Number); - if (GspRemoveHwBreakpoint (Number & 0x3) == 0) + if (GspRemoveHwBreakpoint(Number & 0x3) == 0) { - strcpy (GspOutBuffer, "OK"); + strcpy(GspOutBuffer, "OK"); } else { - strcpy (GspOutBuffer, "E"); + strcpy(GspOutBuffer, "E"); } break; } @@ -1349,14 +1394,20 @@ KdpGdbEnterDebuggerException(PEXCEPTION_RECORD ExceptionRecord, } /* reply to the request */ - GspPutPacket (&GspOutBuffer[0]); + GspPutPacket(&GspOutBuffer[0]); } /* not reached */ ASSERT(0); } - return kdContinue; + if (NULL != GspDbgThread) + { + ObDereferenceObject(GspDbgThread); + GspDbgThread = NULL; + } + + return kdContinue; } @@ -1371,80 +1422,55 @@ GspBreakIn(PKINTERRUPT Interrupt, KIRQL OldIrql; UCHAR Value; - DPRINT ("Break In\n"); + DPRINT("Break In\n"); DoBreakIn = FALSE; - while (KdPortGetByteEx (&GdbPortInfo, &Value)) + while (KdPortGetByteEx(&GdbPortInfo, &Value)) { if (Value == 0x03) - DoBreakIn = TRUE; + { + DoBreakIn = TRUE; + } } if (!DoBreakIn) - return TRUE; + { + return TRUE; + } - KeRaiseIrql (HIGH_LEVEL, &OldIrql); + KeRaiseIrql(HIGH_LEVEL, &OldIrql); TrapFrame = PsGetCurrentThread()->Tcb.TrapFrame; - KeTrapFrameToContext (TrapFrame, NULL, &Context); + KeTrapFrameToContext(TrapFrame, NULL, &Context); - KdpGdbEnterDebuggerException (NULL, &Context, TrapFrame); + KdpGdbEnterDebuggerException(NULL, &Context, TrapFrame); - KeContextToTrapFrame (&Context, NULL, TrapFrame, KernelMode); + KeContextToTrapFrame(&Context, NULL, TrapFrame, KernelMode); - KeLowerIrql (OldIrql); + KeLowerIrql(OldIrql); return TRUE; } - -extern ULONG KdpPortIrq; - - VOID STDCALL KdpGdbDebugPrint(PCH Message, ULONG Length) { } -extern LIST_ENTRY ModuleListHead; - -VOID -KdGdbListModules() -{ - PLIST_ENTRY CurrentEntry; - PLDR_DATA_TABLE_ENTRY Current; - ULONG ModuleCount; - - DPRINT1("\n"); - - ModuleCount = 0; - - CurrentEntry = ModuleListHead.Flink; - while (CurrentEntry != (&ModuleListHead)) - { - Current = CONTAINING_RECORD (CurrentEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderModuleList); - - DbgPrint ("Module %wZ Base 0x%.08x Length 0x%.08x\n", - &Current->BaseDllName, Current->DllBase, Current->SizeOfImage); - - ModuleCount++; - CurrentEntry = CurrentEntry->Flink; - } - - DbgPrint ("%d modules listed\n", ModuleCount); -} - /* Initialize the GDB stub */ VOID STDCALL KdpGdbStubInit(PKD_DISPATCH_TABLE WrapperTable, ULONG BootPhase) { - if (!KdDebuggerEnabled || !KdpDebugMode.Gdb) return; + if (!KdDebuggerEnabled || !KdpDebugMode.Gdb) + { + return; + } - if (BootPhase == 0) + if (BootPhase == 0) { /* Write out the functions that we support for now */ WrapperTable->KdpInitRoutine = KdpGdbStubInit; @@ -1454,7 +1480,7 @@ KdpGdbStubInit(PKD_DISPATCH_TABLE WrapperTable, /* Initialize the Port */ KdPortInitializeEx(&GdbPortInfo, 0, 0); } - else if (BootPhase == 1) + else if (BootPhase == 1) { GspInitialized = TRUE; @@ -1464,9 +1490,9 @@ KdpGdbStubInit(PKD_DISPATCH_TABLE WrapperTable, HalDisplayString("Waiting for GDB to attach\n"); DbgPrint("Module 'hal.dll' loaded at 0x%.08x.\n", LdrHalBase); - DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C); + DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C); } - else if (BootPhase == 2) + else if (BootPhase == 2) { HalDisplayString("\n GDB debugging enabled\n\n"); }