preliminary comment out the self-modifying code for RtlPrefetchMemoryNonTemporal
[reactos.git] / reactos / ntoskrnl / kdbg / kdb_cli.c
index 5af3341..ac4be5b 100644 (file)
 /* INCLUDES ******************************************************************/
 
 #include <ntoskrnl.h>
-#include <ctype.h>
-#include <internal/kdb.h>
 
 #define NDEBUG
 #include <internal/debug.h>
 
 /* DEFINES *******************************************************************/
 
-/* FIXME: NDK headers */
-#define TempEsp TempEip
-#define TempSegSs TempCs
-
 #define KEY_BS          8
 #define KEY_ESC         27
 #define KEY_DEL         127
@@ -91,6 +85,7 @@ STATIC BOOLEAN KdbpCmdHelp(ULONG Argc, PCHAR Argv[]);
 /* GLOBALS *******************************************************************/
 
 STATIC BOOLEAN KdbUseIntelSyntax = FALSE; /* Set to TRUE for intel syntax */
+STATIC BOOLEAN KdbBreakOnModuleLoad = FALSE; /* Set to TRUE to break into KDB when a module is loaded */
 
 STATIC CHAR KdbCommandHistoryBuffer[2048]; /* Command history string ringbuffer */
 STATIC PCHAR KdbCommandHistory[sizeof(KdbCommandHistoryBuffer) / 8] = { NULL }; /* Command history ringbuffer */
@@ -200,7 +195,7 @@ KdbpEvaluateExpression(
 STATIC BOOLEAN
 KdbpCmdEvalExpression(ULONG Argc, PCHAR Argv[])
 {
-   INT i, len;
+   UINT i, len;
    ULONGLONG Result = 0;
    ULONG ul;
    LONG l = 0;
@@ -384,28 +379,28 @@ KdbpCmdRegs(ULONG Argc, PCHAR Argv[])
                 "   ECX  0x%08x   EDX  0x%08x\n"
                 "   ESI  0x%08x   EDI  0x%08x\n"
                 "   EBP  0x%08x\n",
-                Tf->Cs & 0xFFFF, Tf->Eip,
-                Tf->Ss, Tf->Esp,
+                Tf->SegCs & 0xFFFF, Tf->Eip,
+                Tf->HardwareSegSs, Tf->HardwareEsp,
                 Tf->Eax, Tf->Ebx,
                 Tf->Ecx, Tf->Edx,
                 Tf->Esi, Tf->Edi,
                 Tf->Ebp);
-      KdbpPrint("EFLAGS  0x%08x ", Tf->Eflags);
+      KdbpPrint("EFLAGS  0x%08x ", Tf->EFlags);
       for (i = 0; i < 32; i++)
       {
          if (i == 1)
          {
-            if ((Tf->Eflags & (1 << 1)) == 0)
+            if ((Tf->EFlags & (1 << 1)) == 0)
                KdbpPrint(" !BIT1");
          }
          else if (i == 12)
          {
-            KdbpPrint(" IOPL%d", (Tf->Eflags >> 12) & 3);
+            KdbpPrint(" IOPL%d", (Tf->EFlags >> 12) & 3);
          }
          else if (i == 13)
          {
          }
-         else if ((Tf->Eflags & (1 << i)) != 0)
+         else if ((Tf->EFlags & (1 << i)) != 0)
             KdbpPrint(EflagsBits[i]);
       }
       KdbpPrint("\n");
@@ -413,10 +408,7 @@ KdbpCmdRegs(ULONG Argc, PCHAR Argv[])
    else if (Argv[0][0] == 'c') /* cregs */
    {
       ULONG Cr0, Cr2, Cr3, Cr4;
-      struct __attribute__((packed)) {
-         USHORT Limit;
-         ULONG Base;
-      } Gdtr, Ldtr, Idtr;
+      KDESCRIPTOR Gdtr, Ldtr, Idtr;
       ULONG Tr;
       STATIC CONST PCHAR Cr0Bits[32] = { " PE", " MP", " EM", " TS", " ET", " NE", NULL, NULL,
                                          NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
@@ -433,9 +425,9 @@ KdbpCmdRegs(ULONG Argc, PCHAR Argv[])
       Cr4 = KdbCurrentTrapFrame->Cr4;
 
       /* Get descriptor table regs */
-      asm volatile("sgdt %0" : : "m"(Gdtr));
-      asm volatile("sldt %0" : : "m"(Ldtr));
-      asm volatile("sidt %0" : : "m"(Idtr));
+      asm volatile("sgdt %0" : : "m"(Gdtr.Limit));
+      asm volatile("sldt %0" : : "m"(Ldtr.Limit));
+      asm volatile("sidt %0" : : "m"(Idtr.Limit));
 
       /* Get the task register */
       asm volatile("str %0" : "=g"(Tr));
@@ -469,18 +461,18 @@ KdbpCmdRegs(ULONG Argc, PCHAR Argv[])
    else if (Argv[0][0] == 's') /* sregs */
    {
       KdbpPrint("CS  0x%04x  Index 0x%04x  %cDT RPL%d\n",
-                Tf->Cs & 0xffff, (Tf->Cs & 0xffff) >> 3,
-                (Tf->Cs & (1 << 2)) ? 'L' : 'G', Tf->Cs & 3);
+                Tf->SegCs & 0xffff, (Tf->SegCs & 0xffff) >> 3,
+                (Tf->SegCs & (1 << 2)) ? 'L' : 'G', Tf->SegCs & 3);
       KdbpPrint("DS  0x%04x  Index 0x%04x  %cDT RPL%d\n",
-                Tf->Ds, Tf->Ds >> 3, (Tf->Ds & (1 << 2)) ? 'L' : 'G', Tf->Ds & 3);
+                Tf->SegDs, Tf->SegDs >> 3, (Tf->SegDs & (1 << 2)) ? 'L' : 'G', Tf->SegDs & 3);
       KdbpPrint("ES  0x%04x  Index 0x%04x  %cDT RPL%d\n",
-                Tf->Es, Tf->Es >> 3, (Tf->Es & (1 << 2)) ? 'L' : 'G', Tf->Es & 3);
+                Tf->SegEs, Tf->SegEs >> 3, (Tf->SegEs & (1 << 2)) ? 'L' : 'G', Tf->SegEs & 3);
       KdbpPrint("FS  0x%04x  Index 0x%04x  %cDT RPL%d\n",
-                Tf->Fs, Tf->Fs >> 3, (Tf->Fs & (1 << 2)) ? 'L' : 'G', Tf->Fs & 3);
+                Tf->SegFs, Tf->SegFs >> 3, (Tf->SegFs & (1 << 2)) ? 'L' : 'G', Tf->SegFs & 3);
       KdbpPrint("GS  0x%04x  Index 0x%04x  %cDT RPL%d\n",
-                Tf->Gs, Tf->Gs >> 3, (Tf->Gs & (1 << 2)) ? 'L' : 'G', Tf->Gs & 3);
+                Tf->SegGs, Tf->SegGs >> 3, (Tf->SegGs & (1 << 2)) ? 'L' : 'G', Tf->SegGs & 3);
       KdbpPrint("SS  0x%04x  Index 0x%04x  %cDT RPL%d\n",
-                Tf->Ss, Tf->Ss >> 3, (Tf->Ss & (1 << 2)) ? 'L' : 'G', Tf->Ss & 3);
+                Tf->HardwareSegSs, Tf->HardwareSegSs >> 3, (Tf->HardwareSegSs & (1 << 2)) ? 'L' : 'G', Tf->HardwareSegSs & 3);
    }
    else /* dregs */
    {
@@ -577,7 +569,7 @@ KdbpCmdBackTrace(ULONG Argc, PCHAR Argv[])
          break;
       if (!NT_SUCCESS(KdbpSafeReadMemory(&Address, (PVOID)(Frame + sizeof(ULONG_PTR)), sizeof (ULONG_PTR))))
       {
-         KdbpPrint("Couldn't access memory at 0x%x!\n", Frame + sizeof(ULONG_PTR));
+         KdbpPrint("Couldn't access memory at 0x%p!\n", Frame + sizeof(ULONG_PTR));
          break;
       }
       if (!KdbSymPrintAddress((PVOID)Address))
@@ -588,7 +580,7 @@ KdbpCmdBackTrace(ULONG Argc, PCHAR Argv[])
          break;
       if (!NT_SUCCESS(KdbpSafeReadMemory(&Frame, (PVOID)Frame, sizeof (ULONG_PTR))))
       {
-         KdbpPrint("Couldn't access memory at 0x%x!\n", Frame);
+         KdbpPrint("Couldn't access memory at 0x%p!\n", Frame);
          break;
       }
    }
@@ -775,7 +767,7 @@ KdbpCmdBreakPoint(ULONG Argc, PCHAR Argv[])
    KDB_BREAKPOINT_TYPE Type;
    UCHAR Size = 0;
    KDB_ACCESS_TYPE AccessType = 0;
-   INT AddressArgIndex, ConditionArgIndex, i;
+   UINT AddressArgIndex, ConditionArgIndex, i;
    BOOLEAN Global = TRUE;
 
    if (Argv[0][2] == 'x') /* software breakpoint */
@@ -948,10 +940,10 @@ KdbpCmdThread(ULONG Argc, PCHAR Argv[])
 
          if (Thread->Tcb.TrapFrame != NULL)
          {
-            if (Thread->Tcb.TrapFrame->PreviousMode == KernelMode)
+            if (Thread->Tcb.TrapFrame->PreviousPreviousMode == KernelMode)
                Esp = (PULONG)Thread->Tcb.TrapFrame->TempEsp;
             else
-               Esp = (PULONG)Thread->Tcb.TrapFrame->Esp;
+               Esp = (PULONG)Thread->Tcb.TrapFrame->HardwareEsp;
             Ebp = (PULONG)Thread->Tcb.TrapFrame->Ebp;
             Eip = Thread->Tcb.TrapFrame->Eip;
          }
@@ -1089,8 +1081,8 @@ KdbpCmdProc(ULONG Argc, PCHAR Argv[])
             str2 = "";
          }
 
-         State = ((Process->Pcb.State == PROCESS_STATE_TERMINATED) ? "Terminated" :
-                 ((Process->Pcb.State == PROCESS_STATE_ACTIVE) ? "Active" : "Unknown"));
+         State = ((Process->Pcb.State == ProcessInMemory) ? "In Memory" :
+                 ((Process->Pcb.State == ProcessOutOfMemory) ? "Out of Memory" : "In Transition"));
 
          KdbpPrint(" %s0x%08x  %-10s  %s%s\n",
                    str1,
@@ -1143,8 +1135,8 @@ KdbpCmdProc(ULONG Argc, PCHAR Argv[])
          }
       }
 
-      State = ((Process->Pcb.State == PROCESS_STATE_TERMINATED) ? "Terminated" :
-              ((Process->Pcb.State == PROCESS_STATE_ACTIVE) ? "Active" : "Unknown"));
+      State = ((Process->Pcb.State == ProcessInMemory) ? "In Memory" :
+              ((Process->Pcb.State == ProcessOutOfMemory) ? "Out of Memory" : "In Transition"));
       KdbpPrint("%s"
                 "  PID:             0x%08x\n"
                 "  State:           %s (0x%x)\n"
@@ -1187,7 +1179,7 @@ KdbpCmdMod(ULONG Argc, PCHAR Argv[])
 
       if (!KdbpSymFindModuleByAddress((PVOID)Address, &Info))
       {
-         KdbpPrint("No module containing address 0x%x found!\n", Address);
+         KdbpPrint("No module containing address 0x%p found!\n", Address);
          return TRUE;
       }
       DisplayOnlyOneModule = TRUE;
@@ -1455,9 +1447,9 @@ KdbpCmdPcr(ULONG Argc, PCHAR Argv[])
              "  VdmAlert:                  0x%08x\n"
              "  L2CacheSize:               0x%08x\n"
              "  InterruptMode:             0x%08x\n",
-             Pcr->Tib.ExceptionList, Pcr->Tib.StackBase, Pcr->Tib.StackLimit,
-             Pcr->Tib.SubSystemTib, Pcr->Tib.FiberData, Pcr->Tib.ArbitraryUserPointer,
-             Pcr->Tib.Self, Pcr->Self, Pcr->Prcb, Pcr->Irql, Pcr->IRR, Pcr->IrrActive,
+             Pcr->NtTib.ExceptionList, Pcr->NtTib.StackBase, Pcr->NtTib.StackLimit,
+             Pcr->NtTib.SubSystemTib, Pcr->NtTib.FiberData, Pcr->NtTib.ArbitraryUserPointer,
+             Pcr->NtTib.Self, Pcr->Self, Pcr->Prcb, Pcr->Irql, Pcr->IRR, Pcr->IrrActive,
              Pcr->IDR, Pcr->KdVersionBlock, Pcr->IDT, Pcr->GDT, Pcr->TSS,
              Pcr->MajorVersion, Pcr->MinorVersion, Pcr->SetMember, Pcr->StallScaleFactor,
              Pcr->Number, Pcr->L2CacheAssociativity,
@@ -1474,35 +1466,15 @@ KdbpCmdTss(ULONG Argc, PCHAR Argv[])
    KTSS *Tss = KeGetCurrentKPCR()->TSS;
 
    KdbpPrint("Current TSS is at 0x%08x.\n", (INT)Tss);
-   KdbpPrint("  PreviousTask:  0x%08x\n"
-             "  Ss0:Esp0:      0x%04x:0x%08x\n"
-             "  Ss1:Esp1:      0x%04x:0x%08x\n"
-             "  Ss2:Esp2:      0x%04x:0x%08x\n"
-             "  Cr3:           0x%08x\n"
-             "  Eip:           0x%08x\n"
-             "  Eflags:        0x%08x\n"
-             "  Eax:           0x%08x\n"
-             "  Ecx:           0x%08x\n"
-             "  Edx:           0x%08x\n"
-             "  Ebx:           0x%08x\n"
-             "  Esp:           0x%08x\n"
-             "  Ebp:           0x%08x\n"
-             "  Esi:           0x%08x\n"
-             "  Edi:           0x%08x\n"
+   KdbpPrint("  Eip:           0x%08x\n"
              "  Es:            0x%04x\n"
              "  Cs:            0x%04x\n"
              "  Ss:            0x%04x\n"
              "  Ds:            0x%04x\n"
              "  Fs:            0x%04x\n"
              "  Gs:            0x%04x\n"
-             "  Ldt:           0x%04x\n"
-             "  Trap:          0x%04x\n"
              "  IoMapBase:     0x%04x\n",
-             Tss->PreviousTask, Tss->Ss0, Tss->Esp0, Tss->Ss1, Tss->Esp1,
-             Tss->Ss2, Tss->Esp2, Tss->Cr3, Tss->Eip, Tss->Eflags, Tss->Eax,
-             Tss->Ecx, Tss->Edx, Tss->Ebx, Tss->Esp, Tss->Ebp, Tss->Esi,
-             Tss->Edi, Tss->Es, Tss->Cs, Tss->Ss, Tss->Ds, Tss->Fs, Tss->Gs,
-             Tss->Ldt, Tss->Trap, Tss->IoMapBase);
+             Tss->Eip, Tss->Es, Tss->Cs, Tss->Ds, Tss->Fs, Tss->Gs, Tss->IoMapBase);
    return TRUE;
 }
 
@@ -1520,7 +1492,7 @@ KdbpCmdBugCheck(ULONG Argc, PCHAR Argv[])
 STATIC BOOLEAN
 KdbpCmdSet(ULONG Argc, PCHAR Argv[])
 {
-   LONG l;
+   ULONG l;
    BOOLEAN First;
    PCHAR pend = 0;
    KDB_ENTER_CONDITION ConditionFirst = KdbDoNotEnter;
@@ -1536,6 +1508,7 @@ KdbpCmdSet(ULONG Argc, PCHAR Argv[])
       KdbpPrint("Available settings:\n");
       KdbpPrint("  syntax [intel|at&t]\n");
       KdbpPrint("  condition [exception|*] [first|last] [never|always|kmode|umode]\n");
+      KdbpPrint("  break_on_module_load [true|false]\n");
    }
    else if (strcmp(Argv[1], "syntax") == 0)
    {
@@ -1579,7 +1552,7 @@ KdbpCmdSet(ULONG Argc, PCHAR Argv[])
             l = -1;
          else
          {
-            l = (LONG)strtoul(Argv[2], &pend, 0);
+            l = strtoul(Argv[2], &pend, 0);
             if (Argv[2] == pend)
             {
                for (l = 0; l < RTL_NUMBER_OF(ExceptionNames); l++)
@@ -1650,6 +1623,22 @@ KdbpCmdSet(ULONG Argc, PCHAR Argv[])
          }
       }
    }
+   else if (strcmp(Argv[1], "break_on_module_load") == 0)
+   {
+      if (Argc == 2)
+         KdbpPrint("break_on_module_load = %s\n", KdbBreakOnModuleLoad ? "enabled" : "disabled");
+      else if (Argc >= 3)
+      {
+         if (_stricmp(Argv[2], "enable") == 0 || _stricmp(Argv[2], "enabled") == 0 ||
+             _stricmp(Argv[2], "true") == 0)
+            KdbBreakOnModuleLoad = TRUE;
+         else if (_stricmp(Argv[2], "disable") == 0 || _stricmp(Argv[2], "disabled") == 0 ||
+                  _stricmp(Argv[2], "false") == 0)
+            KdbBreakOnModuleLoad = FALSE;
+         else
+            KdbpPrint("Unknown setting '%s'.\n", Argv[2]);
+      }
+   }
    else
       KdbpPrint("Unknown setting '%s'.\n", Argv[1]);
 
@@ -1701,8 +1690,8 @@ KdbpPrint(
    STATIC BOOLEAN TerminalReportsSize = TRUE;
    CHAR c = '\0';
    PCHAR p, p2;
-   INT Length;
-   INT i, j;
+   UINT Length;
+   UINT i, j;
    INT RowsPrintedByTerminal;
    ULONG ScanCode;
    va_list ap;
@@ -1740,7 +1729,7 @@ KdbpPrint(
    if ((KdbNumberOfRowsTerminal < 0) || (KdbNumberOfColsTerminal < 0) ||
        (KdbNumberOfRowsPrinted) == 0) /* Refresh terminal size each time when number of rows printed is 0 */
    {
-      if ((KdbDebugState & KD_DEBUG_KDSERIAL) && TerminalReportsSize)
+      if ((KdbDebugState & KD_DEBUG_KDSERIAL) && TerminalConnected && TerminalReportsSize)
       {
          /* Try to query number of rows from terminal. A reply looks like "\x1b[8;24;80t" */
          TerminalReportsSize = FALSE;
@@ -1814,7 +1803,8 @@ KdbpPrint(
       /*DbgPrint("!%d!%d!%d!%d!", KdbNumberOfRowsPrinted, KdbNumberOfColsPrinted, i, RowsPrintedByTerminal);*/
 
       /* Display a prompt if we printed one screen full of text */
-      if ((KdbNumberOfRowsPrinted + RowsPrintedByTerminal) >= KdbNumberOfRowsTerminal)
+      if (KdbNumberOfRowsTerminal > 0 &&
+          (LONG)(KdbNumberOfRowsPrinted + RowsPrintedByTerminal) >= KdbNumberOfRowsTerminal)
       {
          if (KdbNumberOfColsPrinted > 0)
             DbgPrint("\n");
@@ -1899,8 +1889,8 @@ STATIC VOID
 KdbpCommandHistoryAppend(
    IN PCHAR Command)
 {
-   LONG Length1 = strlen(Command) + 1;
-   LONG Length2 = 0;
+   ULONG Length1 = strlen(Command) + 1;
+   ULONG Length2 = 0;
    INT i;
    PCHAR Buffer;
 
@@ -1916,7 +1906,7 @@ KdbpCommandHistoryAppend(
    /* Calculate Length1 and Length2 */
    Buffer = KdbCommandHistoryBuffer + KdbCommandHistoryBufferIndex;
    KdbCommandHistoryBufferIndex += Length1;
-   if (KdbCommandHistoryBufferIndex >= RTL_NUMBER_OF(KdbCommandHistoryBuffer))
+   if (KdbCommandHistoryBufferIndex >= (LONG)RTL_NUMBER_OF(KdbCommandHistoryBuffer))
    {
       KdbCommandHistoryBufferIndex -= RTL_NUMBER_OF(KdbCommandHistoryBuffer);
       Length2 = KdbCommandHistoryBufferIndex;
@@ -2019,7 +2009,7 @@ KdbpReadCommand(
          NextKey = '\0';
       }
 
-      if ((Buffer - Orig) >= (Size - 1))
+      if ((ULONG)(Buffer - Orig) >= (Size - 1))
       {
          /* Buffer is full, accept only newlines */
          if (Key != '\n')
@@ -2105,7 +2095,7 @@ KdbpReadCommand(
          if (CmdHistIndex > 0 && CmdHistIndex != KdbCommandHistoryIndex)
          {
             i = CmdHistIndex + 1;
-            if (i >= RTL_NUMBER_OF(KdbCommandHistory))
+            if (i >= (INT)RTL_NUMBER_OF(KdbCommandHistory))
                i = 0;
             if (KdbCommandHistory[i] != NULL)
             {
@@ -2255,7 +2245,8 @@ KdbpCliMainLoop(
 VOID
 KdbpCliModuleLoaded(IN PUNICODE_STRING Name)
 {
-   return;
+   if (!KdbBreakOnModuleLoad)
+      return;
 
    DbgPrint("Module %wZ loaded.\n", Name);
    DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C);
@@ -2369,7 +2360,7 @@ KdbpCliInit()
       DPRINT("Could not read KDBinit file into memory (Status 0x%lx)\n", Status);
       return;
    }
-   FileSize = min(FileSize, Iosb.Information);
+   FileSize = min(FileSize, (INT)Iosb.Information);
    FileBuffer[FileSize] = '\0';
 
    /* Enter critical section */