[NTOS:KD/KD64/KDBG] Share some code between our legacy KD/KDBG and KD64.

Our legacy KD module is slowly being phased out for the more recent KD64
Kernel Debugger that supports WinDbg, but at the same time we must retain
support for GCC debugging and the KDBG interface.

For the time being few #ifdef _WINKD_ have been introduced in KD64 so that
some of its code/data does not completely get shared yet with the legacy KD,
until the latter becomes phased out.

KD Modifications:
=================
- Remove the implementation of NtQueryDebugFilterState() /
  NtSetDebugFilterState() that now comes entirely from KD64.

- Remove KD variables that are now shared with KD64.

- Share common code with KD64: KdpMoveMemory(), KdpZeroMemory(),
  KdpCopyMemoryChunks(), KdpPrint(), KdpPrompt().

- KDBG: Remove the duplicated KdpCopyMemoryChunks() function.

- In KdpServiceDispatcher() and KdpEnterDebuggerException(), call the
  KdpPrint() worker function that correctly probes and captures its arguments.

- Temporarily stub out KdEnterDebugger() and KdExitDebugger() that is used
  by the shared code, until KD is removed and only the KD64 version of these
  functions remain.

- Re-implement the KD/KDBG KdpPrompt() function using a custom KdpPromptString()
  helper compatible with KD64, that is called by the KD64 implementation of
  KdpPrompt(). This KdpPromptString() helper now issues the prompt on all
  the KD loggers: e.g. if you use both at the same time COM-port and SCREEN
  debugging, the prompt will appear on both. Before that the prompt was always
  being displayed on COM port even if e.g. a SCREEN-only debug session was used...

- ppc_irq.c: Fix the prototype of KdpServiceDispatcher().

KD64 Fixes:
===========
- Initialize the MaximumLength member of the counted STRING variables
  before using them elsewhere.

- Get rid of alloca() within SEH block in KdpPrint() (addendum to 7b95fcf9).

- Add the ROS-specific handy dump commands in KdSystemDebugControl().
This commit is contained in:
Hermès Bélusca-Maïto 2019-11-17 22:55:36 +01:00
parent 54c03f6965
commit a890fc64d1
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
10 changed files with 297 additions and 516 deletions

View file

@ -585,40 +585,15 @@ KdpScreenInit(PKD_DISPATCH_TABLE DispatchTable,
/* GENERAL FUNCTIONS *********************************************************/
ULONG
BOOLEAN
NTAPI
KdpPrintString(
_In_reads_bytes_(Length) PCHAR UnsafeString,
_In_ ULONG Length,
_In_ KPROCESSOR_MODE PreviousMode)
_In_ PSTRING Output)
{
PLIST_ENTRY CurrentEntry;
PKD_DISPATCH_TABLE CurrentTable;
PCHAR String;
CHAR StringBuffer[512];
if (!KdpDebugMode.Value) return 0;
Length = min(Length, sizeof(StringBuffer));
if (PreviousMode != KernelMode)
{
_SEH2_TRY
{
ProbeForRead(UnsafeString, Length, 1);
String = StringBuffer;
RtlCopyMemory(String, UnsafeString, Length);
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
return 0;
}
_SEH2_END;
}
else
{
String = UnsafeString;
}
if (!KdpDebugMode.Value) return FALSE;
/* Call the registered handlers */
CurrentEntry = KdProviders.Flink;
@ -630,7 +605,7 @@ KdpPrintString(
KdProvidersList);
/* Call it */
CurrentTable->KdpPrintRoutine(String, Length);
CurrentTable->KdpPrintRoutine(Output->Buffer, Output->Length);
/* Next Table */
CurrentEntry = CurrentEntry->Flink;
@ -638,10 +613,116 @@ KdpPrintString(
/* Call the Wrapper Routine */
if (WrapperTable.KdpPrintRoutine)
WrapperTable.KdpPrintRoutine(String, Length);
WrapperTable.KdpPrintRoutine(Output->Buffer, Output->Length);
/* Return the Length */
return Length;
return FALSE;
}
extern STRING KdbPromptString;
BOOLEAN
NTAPI
KdpPromptString(
_In_ PSTRING PromptString,
_In_ PSTRING ResponseString)
{
KIRQL OldIrql;
STRING StringChar;
CHAR Response;
USHORT i;
ULONG DummyScanCode;
StringChar.Buffer = &Response;
StringChar.Length = StringChar.MaximumLength = sizeof(Response);
/* Display the string and print a new line for log neatness */
KdpPrintString(PromptString);
*StringChar.Buffer = '\n';
KdpPrintString(&StringChar);
/* Print the kdb prompt */
KdpPrintString(&KdbPromptString);
// TODO: Use an improved KdbpReadCommand() function for our purposes.
/* Acquire the printing spinlock without waiting at raised IRQL */
OldIrql = KdpAcquireLock(&KdpSerialSpinLock);
if (!(KdbDebugState & KD_DEBUG_KDSERIAL))
KbdDisableMouse();
/* Loop the whole string */
for (i = 0; i < ResponseString->MaximumLength; i++)
{
/* Check if this is serial debugging mode */
if (KdbDebugState & KD_DEBUG_KDSERIAL)
{
/* Get the character from serial */
do
{
Response = KdbpTryGetCharSerial(MAXULONG);
} while (Response == -1);
}
else
{
/* Get the response from the keyboard */
do
{
Response = KdbpTryGetCharKeyboard(&DummyScanCode, MAXULONG);
} while (Response == -1);
}
/* Check for return */
if (Response == '\r')
{
/*
* We might need to discard the next '\n'.
* Wait a bit to make sure we receive it.
*/
KeStallExecutionProcessor(100000);
/* Check the mode */
if (KdbDebugState & KD_DEBUG_KDSERIAL)
{
/* Read and discard the next character, if any */
KdbpTryGetCharSerial(5);
}
else
{
/* Read and discard the next character, if any */
KdbpTryGetCharKeyboard(&DummyScanCode, 5);
}
/*
* Null terminate the output string -- documentation states that
* DbgPrompt does not null terminate, but it does
*/
*(PCHAR)(ResponseString->Buffer + i) = 0;
break;
}
/* Write it back and print it to the log */
*(PCHAR)(ResponseString->Buffer + i) = Response;
KdpReleaseLock(&KdpSerialSpinLock, OldIrql);
KdpPrintString(&StringChar);
OldIrql = KdpAcquireLock(&KdpSerialSpinLock);
}
/* Return the length */
ResponseString->Length = i;
if (!(KdbDebugState & KD_DEBUG_KDSERIAL))
KbdEnableMouse();
/* Release the spinlock */
KdpReleaseLock(&KdpSerialSpinLock, OldIrql);
/* Print a new line */
*StringChar.Buffer = '\n';
KdpPrintString(&StringChar);
/* Success; we don't need to resend */
return FALSE;
}
/* EOF */

View file

@ -11,6 +11,38 @@
#define NDEBUG
#include <debug.h>
//
// Retrieves the ComponentId and Level for BREAKPOINT_PRINT
// and OutputString and OutputStringLength for BREAKPOINT_PROMPT.
//
#if defined(_X86_)
//
// EBX/EDI on x86
//
#define KdpGetParameterThree(Context) ((Context)->Ebx)
#define KdpGetParameterFour(Context) ((Context)->Edi)
#elif defined(_AMD64_)
//
// R8/R9 on AMD64
//
#define KdpGetParameterThree(Context) ((Context)->R8)
#define KdpGetParameterFour(Context) ((Context)->R9)
#elif defined(_ARM_)
//
// R3/R4 on ARM
//
#define KdpGetParameterThree(Context) ((Context)->R3)
#define KdpGetParameterFour(Context) ((Context)->R4)
#else
#error Unsupported Architecture
#endif
/* VARIABLES ***************************************************************/
BOOLEAN KdDebuggerEnabled = FALSE;
@ -19,22 +51,12 @@ BOOLEAN KdDebuggerNotPresent = TRUE;
BOOLEAN KdBreakAfterSymbolLoad = FALSE;
BOOLEAN KdPitchDebugger = TRUE;
BOOLEAN KdIgnoreUmExceptions = FALSE;
KD_CONTEXT KdpContext;
ULONG Kd_WIN2000_Mask;
LONG KdpTimeSlipPending;
KDDEBUGGER_DATA64 KdDebuggerDataBlock;
VOID NTAPI PspDumpThreads(BOOLEAN SystemThreads);
typedef struct
{
ULONG ComponentId;
ULONG Level;
} KD_COMPONENT_DATA;
#define MAX_KD_COMPONENT_TABLE_ENTRIES 128
KD_COMPONENT_DATA KdpComponentTable[MAX_KD_COMPONENT_TABLE_ENTRIES];
ULONG KdComponentTableEntries = 0;
#if 0
ULONG Kd_DEFAULT_MASK = 1 << DPFLTR_ERROR_LEVEL;
#endif
/* PRIVATE FUNCTIONS *********************************************************/
@ -50,8 +72,19 @@ KdpServiceDispatcher(ULONG Service,
switch (Service)
{
case BREAKPOINT_PRINT: /* DbgPrint */
Result = KdpPrintString(Buffer1, Buffer1Length, PreviousMode);
{
/* Call KDBG */
BOOLEAN Handled;
Result = KdpPrint(MAXULONG,
DPFLTR_INFO_LEVEL,
(PCHAR)Buffer1,
(USHORT)Buffer1Length,
PreviousMode,
NULL, // TrapFrame,
NULL, // ExceptionFrame,
&Handled);
break;
}
#if DBG
case ' soR': /* ROS-INTERNAL */
@ -144,14 +177,20 @@ KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame,
/* Check if this is a debug print */
if (ExceptionCommand == BREAKPOINT_PRINT)
{
/* Print the string */
KdpServiceDispatcher(BREAKPOINT_PRINT,
(PVOID)ExceptionRecord->ExceptionInformation[1],
ExceptionRecord->ExceptionInformation[2],
PreviousMode);
/* Call KDBG */
NTSTATUS ReturnStatus;
BOOLEAN Handled;
ReturnStatus = KdpPrint((ULONG)KdpGetParameterThree(Context),
(ULONG)KdpGetParameterFour(Context),
(PCHAR)ExceptionRecord->ExceptionInformation[1],
(USHORT)ExceptionRecord->ExceptionInformation[2],
PreviousMode,
TrapFrame,
ExceptionFrame,
&Handled);
/* Return success */
KeSetContextReturnRegister(Context, STATUS_SUCCESS);
/* Update the return value for the caller */
KeSetContextReturnRegister(Context, ReturnStatus);
}
#ifdef KDBG
else if (ExceptionCommand == BREAKPOINT_LOAD_SYMBOLS)
@ -168,7 +207,7 @@ KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame,
ProbeForRead(SymbolsInfo,
sizeof(*SymbolsInfo),
1);
RtlCopyMemory(&CapturedSymbolsInfo,
KdpMoveMemory(&CapturedSymbolsInfo,
SymbolsInfo,
sizeof(*SymbolsInfo));
SymbolsInfo = &CapturedSymbolsInfo;
@ -191,27 +230,18 @@ KdpEnterDebuggerException(IN PKTRAP_FRAME TrapFrame,
}
else if (ExceptionCommand == BREAKPOINT_PROMPT)
{
ULONG ReturnValue;
LPSTR OutString;
USHORT OutStringLength;
/* Get the response string and length */
OutString = (LPSTR)Context->Ebx;
OutStringLength = (USHORT)Context->Edi;
/* Call KDBG */
ReturnValue = KdpPrompt((LPSTR)ExceptionRecord->
ExceptionInformation[1],
(USHORT)ExceptionRecord->
ExceptionInformation[2],
OutString,
OutStringLength,
PreviousMode,
TrapFrame,
ExceptionFrame);
ULONG ReturnLength;
ReturnLength = KdpPrompt((PCHAR)ExceptionRecord->ExceptionInformation[1],
(USHORT)ExceptionRecord->ExceptionInformation[2],
(PCHAR)KdpGetParameterThree(Context),
(USHORT)KdpGetParameterFour(Context),
PreviousMode,
TrapFrame,
ExceptionFrame);
/* Return the number of characters that we received */
Context->Eax = ReturnValue;
/* Update the return value for the caller */
KeSetContextReturnRegister(Context, ReturnLength);
}
#endif
@ -274,6 +304,20 @@ KdUpdateDataBlock(VOID)
{
}
BOOLEAN
NTAPI
KdEnterDebugger(IN PKTRAP_FRAME TrapFrame,
IN PKEXCEPTION_FRAME ExceptionFrame)
{
return FALSE;
}
VOID
NTAPI
KdExitDebugger(IN BOOLEAN Enable)
{
}
/*
* @implemented
*/
@ -381,91 +425,6 @@ KdChangeOption(IN KD_OPTION Option,
return STATUS_NOT_IMPLEMENTED;
}
NTSTATUS
NTAPI
NtQueryDebugFilterState(IN ULONG ComponentId,
IN ULONG Level)
{
ULONG i;
/* Convert Level to mask if it isn't already one */
if (Level < 32)
Level = 1 << Level;
/* Check if it is not the default component */
if (ComponentId != MAXULONG)
{
/* No, search for an existing entry in the table */
for (i = 0; i < KdComponentTableEntries; i++)
{
/* Check if it is the right component */
if (ComponentId == KdpComponentTable[i].ComponentId)
{
/* Check if mask are matching */
return (Level & KdpComponentTable[i].Level) ? TRUE : FALSE;
}
}
}
/* Entry not found in the table, use default mask */
return (Level & Kd_DEFAULT_MASK) ? TRUE : FALSE;
}
NTSTATUS
NTAPI
NtSetDebugFilterState(IN ULONG ComponentId,
IN ULONG Level,
IN BOOLEAN State)
{
ULONG i;
/* Convert Level to mask if it isn't already one */
if (Level < 32)
Level = 1 << Level;
Level &= ~DPFLTR_MASK;
/* Check if it is the default component */
if (ComponentId == MAXULONG)
{
/* Yes, modify the default mask */
if (State)
Kd_DEFAULT_MASK |= Level;
else
Kd_DEFAULT_MASK &= ~Level;
return STATUS_SUCCESS;
}
/* Search for an existing entry */
for (i = 0; i < KdComponentTableEntries; i++ )
{
if (ComponentId == KdpComponentTable[i].ComponentId)
break;
}
/* Check if we have found an existing entry */
if (i == KdComponentTableEntries)
{
/* Check if we have enough space in the table */
if (i == MAX_KD_COMPONENT_TABLE_ENTRIES)
return STATUS_INVALID_PARAMETER_1;
/* Add a new entry */
++KdComponentTableEntries;
KdpComponentTable[i].ComponentId = ComponentId;
KdpComponentTable[i].Level = Kd_DEFAULT_MASK;
}
/* Update entry table */
if (State)
KdpComponentTable[i].Level |= Level;
else
KdpComponentTable[i].Level &= ~Level;
return STATUS_SUCCESS;
}
/*
* @unimplemented
*/