- KDBG: Correct the use of PsLookupThread/ProcessByThread/ProcessId -- the caller...
[reactos.git] / reactos / ntoskrnl / kdbg / kdb_cli.c
index 2eaf264..58d08a7 100644 (file)
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id$
- *
+/*
  * PROJECT:         ReactOS kernel
  * FILE:            ntoskrnl/dbg/kdb_cli.c
  * PURPOSE:         Kernel debugger command line interface
  * PROGRAMMER:      Gregor Anich (blight@blight.eu.org)
+ *                  HervĂ© Poussineau
  * UPDATE HISTORY:
  *                  Created 16/01/2005
  */
@@ -31,7 +31,7 @@
 #include <ntoskrnl.h>
 
 #define NDEBUG
-#include <internal/debug.h>
+#include <debug.h>
 
 /* DEFINES *******************************************************************/
 
@@ -78,6 +78,7 @@ STATIC BOOLEAN KdbpCmdPcr(ULONG Argc, PCHAR Argv[]);
 STATIC BOOLEAN KdbpCmdTss(ULONG Argc, PCHAR Argv[]);
 
 STATIC BOOLEAN KdbpCmdBugCheck(ULONG Argc, PCHAR Argv[]);
+STATIC BOOLEAN KdbpCmdFilter(ULONG Argc, PCHAR Argv[]);
 STATIC BOOLEAN KdbpCmdSet(ULONG Argc, PCHAR Argv[]);
 STATIC BOOLEAN KdbpCmdHelp(ULONG Argc, PCHAR Argv[]);
 
@@ -98,6 +99,7 @@ STATIC LONG KdbNumberOfRowsTerminal = -1;
 STATIC LONG KdbNumberOfColsTerminal = -1;
 
 PCHAR KdbInitFileBuffer = NULL; /* Buffer where KDBinit file is loaded into during initialization */
+BOOLEAN KdbpBugCheckRequested = FALSE;
 
 STATIC CONST struct
 {
@@ -146,12 +148,139 @@ STATIC CONST struct
    /* Others */
    { NULL, NULL, "Others", NULL },
    { "bugcheck", "bugcheck", "Bugchecks the system.", KdbpCmdBugCheck },
+   { "filter", "filter [error|warning|trace|info|level]+|-[componentname|default]", "Enable/disable debug channels", KdbpCmdFilter },
    { "set", "set [var] [value]", "Sets var to value or displays value of var.", KdbpCmdSet },
    { "help", "help", "Display help screen.", KdbpCmdHelp }
 };
 
 /* FUNCTIONS *****************************************************************/
 
+/*!\brief Transform a component name to an integer
+ *
+ * \param ComponentName  The name of the component.
+ * \param ComponentId    Receives the component id on success.
+ *
+ * \retval TRUE   Success.
+ * \retval FALSE  Failure.
+ */
+STATIC BOOLEAN
+KdbpGetComponentId(
+   IN  PCCH ComponentName,
+   OUT PULONG ComponentId)
+{
+   ULONG i;
+
+   static struct {
+      PCCH Name;
+      ULONG Id;
+   } ComponentTable[] = {
+       { "DEFAULT", DPFLTR_DEFAULT_ID },
+       { "SYSTEM", DPFLTR_SYSTEM_ID },
+       { "SMSS", DPFLTR_SMSS_ID },
+       { "SETUP", DPFLTR_SETUP_ID },
+       { "NTFS", DPFLTR_NTFS_ID },
+       { "FSTUB", DPFLTR_FSTUB_ID },
+       { "CRASHDUMP", DPFLTR_CRASHDUMP_ID },
+       { "CDAUDIO", DPFLTR_CDAUDIO_ID },
+       { "CDROM", DPFLTR_CDROM_ID },
+       { "CLASSPNP", DPFLTR_CLASSPNP_ID },
+       { "DISK", DPFLTR_DISK_ID },
+       { "REDBOOK", DPFLTR_REDBOOK_ID },
+       { "STORPROP", DPFLTR_STORPROP_ID },
+       { "SCSIPORT", DPFLTR_SCSIPORT_ID },
+       { "SCSIMINIPORT", DPFLTR_SCSIMINIPORT_ID },
+       { "CONFIG", DPFLTR_CONFIG_ID },
+       { "I8042PRT", DPFLTR_I8042PRT_ID },
+       { "SERMOUSE", DPFLTR_SERMOUSE_ID },
+       { "LSERMOUS", DPFLTR_LSERMOUS_ID },
+       { "KBDHID", DPFLTR_KBDHID_ID },
+       { "MOUHID", DPFLTR_MOUHID_ID },
+       { "KBDCLASS", DPFLTR_KBDCLASS_ID },
+       { "MOUCLASS", DPFLTR_MOUCLASS_ID },
+       { "TWOTRACK", DPFLTR_TWOTRACK_ID },
+       { "WMILIB", DPFLTR_WMILIB_ID },
+       { "ACPI", DPFLTR_ACPI_ID },
+       { "AMLI", DPFLTR_AMLI_ID },
+       { "HALIA64", DPFLTR_HALIA64_ID },
+       { "VIDEO", DPFLTR_VIDEO_ID },
+       { "SVCHOST", DPFLTR_SVCHOST_ID },
+       { "VIDEOPRT", DPFLTR_VIDEOPRT_ID },
+       { "TCPIP", DPFLTR_TCPIP_ID },
+       { "DMSYNTH", DPFLTR_DMSYNTH_ID },
+       { "NTOSPNP", DPFLTR_NTOSPNP_ID },
+       { "FASTFAT", DPFLTR_FASTFAT_ID },
+       { "SAMSS", DPFLTR_SAMSS_ID },
+       { "PNPMGR", DPFLTR_PNPMGR_ID },
+       { "NETAPI", DPFLTR_NETAPI_ID },
+       { "SCSERVER", DPFLTR_SCSERVER_ID },
+       { "SCCLIENT", DPFLTR_SCCLIENT_ID },
+       { "SERIAL", DPFLTR_SERIAL_ID },
+       { "SERENUM", DPFLTR_SERENUM_ID },
+       { "UHCD", DPFLTR_UHCD_ID },
+       { "BOOTOK", DPFLTR_BOOTOK_ID },
+       { "BOOTVRFY", DPFLTR_BOOTVRFY_ID },
+       { "RPCPROXY", DPFLTR_RPCPROXY_ID },
+       { "AUTOCHK", DPFLTR_AUTOCHK_ID },
+       { "DCOMSS", DPFLTR_DCOMSS_ID },
+       { "UNIMODEM", DPFLTR_UNIMODEM_ID },
+       { "SIS", DPFLTR_SIS_ID },
+       { "FLTMGR", DPFLTR_FLTMGR_ID },
+       { "WMICORE", DPFLTR_WMICORE_ID },
+       { "BURNENG", DPFLTR_BURNENG_ID },
+       { "IMAPI", DPFLTR_IMAPI_ID },
+       { "SXS", DPFLTR_SXS_ID },
+       { "FUSION", DPFLTR_FUSION_ID },
+       { "IDLETASK", DPFLTR_IDLETASK_ID },
+       { "SOFTPCI", DPFLTR_SOFTPCI_ID },
+       { "TAPE", DPFLTR_TAPE_ID },
+       { "MCHGR", DPFLTR_MCHGR_ID },
+       { "IDEP", DPFLTR_IDEP_ID },
+       { "PCIIDE", DPFLTR_PCIIDE_ID },
+       { "FLOPPY", DPFLTR_FLOPPY_ID },
+       { "FDC", DPFLTR_FDC_ID },
+       { "TERMSRV", DPFLTR_TERMSRV_ID },
+       { "W32TIME", DPFLTR_W32TIME_ID },
+       { "PREFETCHER", DPFLTR_PREFETCHER_ID },
+       { "RSFILTER", DPFLTR_RSFILTER_ID },
+       { "FCPORT", DPFLTR_FCPORT_ID },
+       { "PCI", DPFLTR_PCI_ID },
+       { "DMIO", DPFLTR_DMIO_ID },
+       { "DMCONFIG", DPFLTR_DMCONFIG_ID },
+       { "DMADMIN", DPFLTR_DMADMIN_ID },
+       { "WSOCKTRANSPORT", DPFLTR_WSOCKTRANSPORT_ID },
+       { "VSS", DPFLTR_VSS_ID },
+       { "PNPMEM", DPFLTR_PNPMEM_ID },
+       { "PROCESSOR", DPFLTR_PROCESSOR_ID },
+       { "DMSERVER", DPFLTR_DMSERVER_ID },
+       { "SR", DPFLTR_SR_ID },
+       { "INFINIBAND", DPFLTR_INFINIBAND_ID },
+       { "IHVDRIVER", DPFLTR_IHVDRIVER_ID },
+       { "IHVVIDEO", DPFLTR_IHVVIDEO_ID },
+       { "IHVAUDIO", DPFLTR_IHVAUDIO_ID },
+       { "IHVNETWORK", DPFLTR_IHVNETWORK_ID },
+       { "IHVSTREAMING", DPFLTR_IHVSTREAMING_ID },
+       { "IHVBUS", DPFLTR_IHVBUS_ID },
+       { "HPS", DPFLTR_HPS_ID },
+       { "RTLTHREADPOOL", DPFLTR_RTLTHREADPOOL_ID },
+       { "LDR", DPFLTR_LDR_ID },
+       { "TCPIP6", DPFLTR_TCPIP6_ID },
+       { "ISAPNP", DPFLTR_ISAPNP_ID },
+       { "SHPC", DPFLTR_SHPC_ID },
+       { "STORPORT", DPFLTR_STORPORT_ID },
+       { "STORMINIPORT", DPFLTR_STORMINIPORT_ID },
+       { "PRINTSPOOLER", DPFLTR_PRINTSPOOLER_ID },
+   };
+
+   for (i = 0; i < sizeof(ComponentTable) / sizeof(ComponentTable[0]); i++)
+       if (_stricmp(ComponentName, ComponentTable[i].Name) == 0)
+       {
+           *ComponentId = ComponentTable[i].Id;
+           return TRUE;
+       }
+
+   return FALSE;
+}
+
 /*!\brief Evaluates an expression...
  *
  * Much like KdbpRpnEvaluateExpression, but prints the error message (if any)
@@ -194,7 +323,7 @@ KdbpEvaluateExpression(
 STATIC BOOLEAN
 KdbpCmdEvalExpression(ULONG Argc, PCHAR Argv[])
 {
-   UINT i, len;
+   ULONG i, len;
    ULONGLONG Result = 0;
    ULONG ul;
    LONG l = 0;
@@ -244,6 +373,85 @@ KdbpCmdEvalExpression(ULONG Argc, PCHAR Argv[])
    return TRUE;
 }
 
+/*!\brief Display list of active debug channels
+ */
+STATIC BOOLEAN
+KdbpCmdFilter(ULONG Argc, PCHAR Argv[])
+{
+    ULONG i, j, ComponentId, Level;
+    ULONG set = DPFLTR_MASK, clear = DPFLTR_MASK;
+    PCHAR pend;
+    LPCSTR opt, p;
+    static struct {
+        LPCSTR Name;
+        ULONG Level;
+    } debug_classes[] = {
+        { "error", 1 << DPFLTR_ERROR_LEVEL },
+        { "warning", 1 << DPFLTR_WARNING_LEVEL },
+        { "trace", 1 << DPFLTR_TRACE_LEVEL },
+        { "info", 1 << DPFLTR_INFO_LEVEL },
+    };
+
+    for (i = 1; i < Argc; i++)
+    {
+        opt = Argv[i];
+        p = opt + strcspn(opt, "+-");
+        if (!p[0]) p = opt; /* assume it's a debug channel name */
+
+        if (p > opt)
+        {
+            for (j = 0; j < sizeof(debug_classes) / sizeof(debug_classes[0]); j++)
+            {
+                SIZE_T len = strlen(debug_classes[j].Name);
+                if (len != (p - opt))
+                    continue;
+                if (_strnicmp(opt, debug_classes[j].Name, len) == 0) /* found it */
+                {
+                    if (*p == '+')
+                        set |= debug_classes[j].Level;
+                    else
+                        clear |= debug_classes[j].Level;
+                    break;
+                }
+            }
+            if (j == sizeof(debug_classes) / sizeof(debug_classes[0]))
+            {
+                Level = strtoul(opt, &pend, 0);
+                if (pend != p)
+                {
+                    KdbpPrint("filter: bad class name '%.*s'\n", p - opt, opt);
+                    continue;
+                }
+                if (*p == '+')
+                    set |= Level;
+                else
+                    clear |= Level;
+            }
+        }
+        else
+        {
+            if (*p == '-')
+                clear = ~0;
+            else
+                set = ~0;
+        }
+        if (*p == '+' || *p == '-')
+            p++;
+
+        if (!KdbpGetComponentId(p, &ComponentId))
+        {
+            KdbpPrint("filter: '%s' is not a valid component name!\n", p);
+            return TRUE;
+        }
+
+        /* Get current mask value */
+        NtSetDebugFilterState(ComponentId, set, TRUE);
+        NtSetDebugFilterState(ComponentId, clear, FALSE);
+    }
+
+    return TRUE;
+}
+
 /*!\brief Disassembles 10 instructions at eip or given address or
  *        displays 16 dwords from memory at given address.
  */
@@ -407,8 +615,8 @@ KdbpCmdRegs(ULONG Argc, PCHAR Argv[])
    else if (Argv[0][0] == 'c') /* cregs */
    {
       ULONG Cr0, Cr2, Cr3, Cr4;
-      KDESCRIPTOR Gdtr, Ldtr, Idtr;
-      ULONG Tr;
+      KDESCRIPTOR Gdtr = {0}, Ldtr = {0}, Idtr = {0};
+      ULONG Tr = 0;
       STATIC CONST PCHAR Cr0Bits[32] = { " PE", " MP", " EM", " TS", " ET", " NE", NULL, NULL,
                                          NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                                          " WP", NULL, " AM", NULL, NULL, NULL, NULL, NULL,
@@ -424,12 +632,12 @@ KdbpCmdRegs(ULONG Argc, PCHAR Argv[])
       Cr4 = KdbCurrentTrapFrame->Cr4;
 
       /* Get descriptor table regs */
-      asm volatile("sgdt %0" : : "m"(Gdtr.Limit));
-      asm volatile("sldt %0" : : "m"(Ldtr.Limit));
-      asm volatile("sidt %0" : : "m"(Idtr.Limit));
+      Ke386GetGlobalDescriptorTable(*(PKDESCRIPTOR)&Gdtr.Limit);
+      Ke386GetLocalDescriptorTable(Ldtr.Limit);
+      Ke386GetInterruptDescriptorTable(*(PKDESCRIPTOR)&Idtr.Limit);
 
       /* Get the task register */
-      asm volatile("str %0" : "=g"(Tr));
+      Ke386GetTr(Tr);
 
       /* Display the control registers */
       KdbpPrint("CR0  0x%08x ", Cr0);
@@ -766,7 +974,8 @@ KdbpCmdBreakPoint(ULONG Argc, PCHAR Argv[])
    KDB_BREAKPOINT_TYPE Type;
    UCHAR Size = 0;
    KDB_ACCESS_TYPE AccessType = 0;
-   UINT AddressArgIndex, ConditionArgIndex, i;
+   ULONG AddressArgIndex, i;
+   LONG ConditionArgIndex;
    BOOLEAN Global = TRUE;
 
    if (Argv[0][2] == 'x') /* software breakpoint */
@@ -881,6 +1090,7 @@ KdbpCmdThread(ULONG Argc, PCHAR Argv[])
    PLIST_ENTRY Entry;
    PETHREAD Thread = NULL;
    PEPROCESS Process = NULL;
+   BOOLEAN ReferencedThread = FALSE, ReferencedProcess = FALSE;
    PULONG Esp;
    PULONG Ebp;
    ULONG Eip;
@@ -909,6 +1119,9 @@ KdbpCmdThread(ULONG Argc, PCHAR Argv[])
             KdbpPrint("thread: Invalid process id!\n");
             return TRUE;
          }
+         
+         /* Remember our reference */
+         ReferencedProcess = TRUE;
       }
 
       Entry = Process->ThreadListHead.Flink;
@@ -918,6 +1131,7 @@ KdbpCmdThread(ULONG Argc, PCHAR Argv[])
             KdbpPrint("No threads in process 0x%08x!\n", ul);
          else
             KdbpPrint("No threads in current process!\n");
+         if (ReferencedProcess) ObDereferenceObject(Process);
          return TRUE;
       }
 
@@ -972,6 +1186,9 @@ KdbpCmdThread(ULONG Argc, PCHAR Argv[])
          Entry = Entry->Flink;
       }
       while (Entry != &Process->ThreadListHead);
+
+      /* Release our reference, if any */
+      if (ReferencedProcess) ObDereferenceObject(Process);
    }
    else if (Argc >= 2 && _stricmp(Argv[1], "attach") == 0)
    {
@@ -1010,6 +1227,9 @@ KdbpCmdThread(ULONG Argc, PCHAR Argv[])
             KdbpPrint("thread: Invalid thread id!\n");
             return TRUE;
          }
+         
+         /* Remember our reference */
+         ReferencedThread = TRUE;
       }
 
       if (Thread->Tcb.State < (DeferredReady + 1))
@@ -1039,6 +1259,8 @@ KdbpCmdThread(ULONG Argc, PCHAR Argv[])
                 Thread->Tcb.TrapFrame,
                 NPX_STATE_TO_STRING(Thread->Tcb.NpxState), Thread->Tcb.NpxState);
 
+        /* Release our reference if we had one */
+        if (ReferencedThread) ObDereferenceObject(Thread);
    }
 
    return TRUE;
@@ -1051,6 +1273,7 @@ KdbpCmdProc(ULONG Argc, PCHAR Argv[])
 {
    PLIST_ENTRY Entry;
    PEPROCESS Process;
+   BOOLEAN ReferencedProcess = FALSE;
    PCHAR State, pend, str1, str2;
    ULONG ul;
    extern LIST_ENTRY PsActiveProcessHead;
@@ -1058,7 +1281,7 @@ KdbpCmdProc(ULONG Argc, PCHAR Argv[])
    if (Argc >= 2 && _stricmp(Argv[1], "list") == 0)
    {
       Entry = PsActiveProcessHead.Flink;
-      if (Entry == &PsActiveProcessHead)
+      if (!Entry || Entry == &PsActiveProcessHead)
       {
          KdbpPrint("No processes in the system!\n");
          return TRUE;
@@ -1112,8 +1335,8 @@ KdbpCmdProc(ULONG Argc, PCHAR Argv[])
       {
          return TRUE;
       }
-      KdbpPrint("Attached to process 0x%08x, thread 0x%08x.\n", (UINT)ul,
-                (UINT)KdbCurrentThread->Cid.UniqueThread);
+      KdbpPrint("Attached to process 0x%08x, thread 0x%08x.\n", (ULONG)ul,
+                (ULONG)KdbCurrentThread->Cid.UniqueThread);
    }
    else
    {
@@ -1132,6 +1355,9 @@ KdbpCmdProc(ULONG Argc, PCHAR Argv[])
             KdbpPrint("proc: Invalid process id!\n");
             return TRUE;
          }
+         
+         /* Remember our reference */
+         ReferencedProcess = TRUE;
       }
 
       State = ((Process->Pcb.State == ProcessInMemory) ? "In Memory" :
@@ -1144,6 +1370,9 @@ KdbpCmdProc(ULONG Argc, PCHAR Argv[])
                 Process->UniqueProcessId,
                 State, Process->Pcb.State,
                 Process->ImageFileName);
+
+        /* Release our reference, if any */
+        if (ReferencedProcess) ObDereferenceObject(Process);
    }
 
    return TRUE;
@@ -1187,8 +1416,10 @@ KdbpCmdMod(ULONG Argc, PCHAR Argv[])
    {
       if (!KdbpSymFindModuleByIndex(0, &Info))
       {
-         KdbpPrint("No modules.\n");
-         return TRUE;
+                 ULONG_PTR ntoskrnlBase = ((ULONG_PTR)KdbpCmdMod) & 0xfff00000;
+                 KdbpPrint("  Base      Size      Name\n");
+                 KdbpPrint("  %08x  %08x  %s\n", ntoskrnlBase, 0, "ntoskrnl.exe");
+                 return TRUE;
       }
       i = 1;
    }
@@ -1213,10 +1444,7 @@ KdbpCmdMod(ULONG Argc, PCHAR Argv[])
 STATIC BOOLEAN
 KdbpCmdGdtLdtIdt(ULONG Argc, PCHAR Argv[])
 {
-   struct __attribute__((packed)) {
-      USHORT Limit;
-      ULONG Base;
-   } Reg;
+   KDESCRIPTOR Reg = {0};
    ULONG SegDesc[2];
    ULONG SegBase;
    ULONG SegLimit;
@@ -1229,7 +1457,7 @@ KdbpCmdGdtLdtIdt(ULONG Argc, PCHAR Argv[])
    if (Argv[0][0] == 'i')
    {
       /* Read IDTR */
-      asm volatile("sidt %0" : : "m"(Reg));
+      Ke386GetInterruptDescriptorTable(*(PKDESCRIPTOR)&Reg.Limit);
 
       if (Reg.Limit < 7)
       {
@@ -1286,14 +1514,14 @@ KdbpCmdGdtLdtIdt(ULONG Argc, PCHAR Argv[])
       if (Argv[0][0] == 'g')
       {
          /* Read GDTR */
-         asm volatile("sgdt %0" : : "m"(Reg));
+         Ke386GetGlobalDescriptorTable(*(PKDESCRIPTOR)&Reg.Limit);
          i = 8;
       }
       else
       {
          ASSERT(Argv[0][0] == 'l');
          /* Read LDTR */
-         asm volatile("sldt %0" : : "m"(Reg));
+         Ke386GetLocalDescriptorTable(Reg.Limit);
          i = 0;
          ul = 1 << 2;
       }
@@ -1417,7 +1645,7 @@ KdbpCmdGdtLdtIdt(ULONG Argc, PCHAR Argv[])
 STATIC BOOLEAN
 KdbpCmdPcr(ULONG Argc, PCHAR Argv[])
 {
-   PKIPCR Pcr = (PKIPCR)KeGetCurrentKPCR();
+   PKIPCR Pcr = (PKIPCR)KeGetPcr();
 
    KdbpPrint("Current PCR is at 0x%08x.\n", (INT)Pcr);
    KdbpPrint("  Tib.ExceptionList:         0x%08x\n"
@@ -1462,7 +1690,7 @@ KdbpCmdPcr(ULONG Argc, PCHAR Argv[])
 STATIC BOOLEAN
 KdbpCmdTss(ULONG Argc, PCHAR Argv[])
 {
-   KTSS *Tss = KeGetCurrentKPCR()->TSS;
+   KTSS *Tss = KeGetPcr()->TSS;
 
    KdbpPrint("Current TSS is at 0x%08x.\n", (INT)Tss);
    KdbpPrint("  Eip:           0x%08x\n"
@@ -1482,8 +1710,9 @@ KdbpCmdTss(ULONG Argc, PCHAR Argv[])
 STATIC BOOLEAN
 KdbpCmdBugCheck(ULONG Argc, PCHAR Argv[])
 {
-   KEBUGCHECK(0xDEADDEAD);
-   return TRUE;
+   /* Set the flag and quit looping */
+   KdbpBugCheckRequested = TRUE;
+   return FALSE;
 }
 
 /*!\brief Sets or displays a config variables value.
@@ -1491,7 +1720,7 @@ KdbpCmdBugCheck(ULONG Argc, PCHAR Argv[])
 STATIC BOOLEAN
 KdbpCmdSet(ULONG Argc, PCHAR Argv[])
 {
-   ULONG l;
+   LONG l;
    BOOLEAN First;
    PCHAR pend = 0;
    KDB_ENTER_CONDITION ConditionFirst = KdbDoNotEnter;
@@ -1689,9 +1918,9 @@ KdbpPrint(
    STATIC BOOLEAN TerminalReportsSize = TRUE;
    CHAR c = '\0';
    PCHAR p, p2;
-   UINT Length;
-   UINT i, j;
-   INT RowsPrintedByTerminal;
+   ULONG Length;
+   ULONG i, j;
+   LONG RowsPrintedByTerminal;
    ULONG ScanCode;
    va_list ap;
 
@@ -1710,6 +1939,8 @@ KdbpPrint(
 
       TerminalInitialized = TRUE;
       Length = 0;
+         KeStallExecutionProcessor(100000);
+
       for (;;)
       {
          c = KdbpTryGetCharSerial(5000);
@@ -1732,6 +1963,7 @@ KdbpPrint(
       {
          /* Try to query number of rows from terminal. A reply looks like "\x1b[8;24;80t" */
          TerminalReportsSize = FALSE;
+                KeStallExecutionProcessor(100000);
          DbgPrint("\x1b[18t");
          c = KdbpTryGetCharSerial(5000);
          if (c == KEY_ESC)
@@ -1763,6 +1995,8 @@ KdbpPrint(
                   }
                }
             }
+                       /* Clear further characters */
+                       while ((c = KdbpTryGetCharSerial(5000)) != -1);
          }
       }
 
@@ -1854,10 +2088,14 @@ KdbpPrint(
                while (!isalpha(p2[j++]));
                strcpy(p2, p2 + j);
             }
+            else
+            {
+                strcpy(p2, p2 + 1);
+            }
          }
       }
 
-      DbgPrint("%s", p);
+         DbgPrint("%s", p);
 
       if (c != '\0')
          p[i + 1] = c;
@@ -2020,13 +2258,14 @@ KdbpReadCommand(
          /* Read the next char - this is to throw away a \n which most clients should
           * send after \r.
           */
+                KeStallExecutionProcessor(100000);
          if (KdbDebugState & KD_DEBUG_KDSERIAL)
             NextKey = KdbpTryGetCharSerial(5);
          else
             NextKey = KdbpTryGetCharKeyboard(&ScanCode, 5);
          if (NextKey == '\n' || NextKey == -1) /* \n or no response at all */
             NextKey = '\0';
-         DbgPrint("\n");
+         KdbpPrint("\n");
         /*
          * Repeat the last command if the user presses enter. Reduces the
          * risk of RSI when single-stepping.
@@ -2051,9 +2290,9 @@ KdbpReadCommand(
             Buffer--;
             *Buffer = 0;
             if (EchoOn)
-               DbgPrint("%c %c", KEY_BS, KEY_BS);
+               KdbpPrint("%c %c", KEY_BS, KEY_BS);
            else
-               DbgPrint(" %c", KEY_BS);
+               KdbpPrint(" %c", KEY_BS);
          }
       }
       else if (ScanCode == KEY_SCAN_UP)
@@ -2078,15 +2317,15 @@ KdbpReadCommand(
                Buffer--;
                *Buffer = 0;
                if (EchoOn)
-                  DbgPrint("%c %c", KEY_BS, KEY_BS);
+                  KdbpPrint("%c %c", KEY_BS, KEY_BS);
                else
-                  DbgPrint(" %c", KEY_BS);
+                  KdbpPrint(" %c", KEY_BS);
             }
             i = min(strlen(KdbCommandHistory[CmdHistIndex]), Size - 1);
             memcpy(Orig, KdbCommandHistory[CmdHistIndex], i);
             Orig[i] = '\0';
             Buffer = Orig + i;
-            DbgPrint("%s", Orig);
+            KdbpPrint("%s", Orig);
          }
       }
       else if (ScanCode == KEY_SCAN_DOWN)
@@ -2104,22 +2343,22 @@ KdbpReadCommand(
                   Buffer--;
                   *Buffer = 0;
                   if (EchoOn)
-                     DbgPrint("%c %c", KEY_BS, KEY_BS);
+                     KdbpPrint("%c %c", KEY_BS, KEY_BS);
                   else
-                     DbgPrint(" %c", KEY_BS);
+                     KdbpPrint(" %c", KEY_BS);
                }
                i = min(strlen(KdbCommandHistory[CmdHistIndex]), Size - 1);
                memcpy(Orig, KdbCommandHistory[CmdHistIndex], i);
                Orig[i] = '\0';
                Buffer = Orig + i;
-               DbgPrint("%s", Orig);
+               KdbpPrint("%s", Orig);
             }
          }
       }
       else
       {
          if (EchoOn)
-            DbgPrint("%c", Key);
+            KdbpPrint("%c", Key);
 
          *Buffer = Key;
          Buffer++;
@@ -2134,7 +2373,7 @@ KdbpReadCommand(
  * \retval TRUE   Don't continue execution.
  * \retval FALSE  Continue execution (leave KDB)
  */
-STATIC BOOL
+STATIC BOOLEAN
 KdbpDoCommand(
    IN PCHAR Command)
 {
@@ -2197,14 +2436,14 @@ KdbpCliMainLoop(
    {
       if (!KdbSymPrintAddress((PVOID)KdbCurrentTrapFrame->Tf.Eip))
       {
-         DbgPrint("<%x>", KdbCurrentTrapFrame->Tf.Eip);
+         KdbpPrint("<%x>", KdbCurrentTrapFrame->Tf.Eip);
       }
-      DbgPrint(": ");
+      KdbpPrint(": ");
       if (KdbpDisassemble(KdbCurrentTrapFrame->Tf.Eip, KdbUseIntelSyntax) < 0)
       {
-         DbgPrint("<INVALID>");
+         KdbpPrint("<INVALID>");
       }
-      DbgPrint("\n");
+      KdbpPrint("\n");
    }
 
    /* Flush the input buffer */
@@ -2222,7 +2461,7 @@ KdbpCliMainLoop(
    do
    {
       /* Print the prompt */
-      DbgPrint("kdb:> ");
+      KdbpPrint("kdb:> ");
 
       /* Read a command and remember it */
       KdbpReadCommand(Command, sizeof (Command));
@@ -2247,7 +2486,7 @@ KdbpCliModuleLoaded(IN PUNICODE_STRING Name)
    if (!KdbBreakOnModuleLoad)
       return;
 
-   DbgPrint("Module %wZ loaded.\n", Name);
+   KdbpPrint("Module %wZ loaded.\n", Name);
    DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C);
 }
 
@@ -2314,7 +2553,7 @@ KdbpCliInit()
    HANDLE hFile = NULL;
    INT FileSize;
    PCHAR FileBuffer;
-   ULONG OldEflags;
+   ULONG OldEflags = 0;
 
    /* Initialize the object attributes */
    RtlInitUnicodeString(&FileName, L"\\SystemRoot\\system32\\drivers\\etc\\KDBinit");
@@ -2364,7 +2603,7 @@ KdbpCliInit()
 
    /* Enter critical section */
    Ke386SaveFlags(OldEflags);
-   Ke386DisableInterrupts();
+   _disable();
 
    /* Interpret the init file... */
    KdbInitFileBuffer = FileBuffer;