[NTVDM]
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sat, 17 May 2014 23:49:35 +0000 (23:49 +0000)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sat, 17 May 2014 23:49:35 +0000 (23:49 +0000)
Add a memory dump facility.

svn path=/trunk/; revision=63342

reactos/subsystems/ntvdm/emulator.c
reactos/subsystems/ntvdm/emulator.h
reactos/subsystems/ntvdm/lang/cs-CZ.rc
reactos/subsystems/ntvdm/lang/de-DE.rc
reactos/subsystems/ntvdm/lang/en-US.rc
reactos/subsystems/ntvdm/lang/es-ES.rc
reactos/subsystems/ntvdm/lang/fr-FR.rc
reactos/subsystems/ntvdm/lang/it-IT.rc
reactos/subsystems/ntvdm/lang/pl-PL.rc
reactos/subsystems/ntvdm/ntvdm.c
reactos/subsystems/ntvdm/resource.h

index 094d156..6503a01 100644 (file)
@@ -337,6 +337,87 @@ static VOID WINAPI PitChan2Out(LPVOID Param, BOOLEAN State)
 
 /* PUBLIC FUNCTIONS ***********************************************************/
 
+VOID DumpMemory(VOID)
+{
+    static ULONG DumpNumber = 0;
+
+    HANDLE hFile;
+    WCHAR  FileName[MAX_PATH];
+
+#define LINE_SIZE   75 + 2
+    ULONG  i;
+    PBYTE  Ptr1, Ptr2;
+    CHAR   LineBuffer[LINE_SIZE];
+    PCHAR  Line;
+    SIZE_T LineSize;
+
+    /* Build a suitable file name */
+    _snwprintf(FileName, MAX_PATH, L"memdump%lu.txt", DumpNumber);
+    ++DumpNumber;
+
+    /* Always create the dump file */
+    hFile = CreateFileW(FileName,
+                        GENERIC_WRITE,
+                        0,
+                        NULL,
+                        CREATE_ALWAYS,
+                        FILE_ATTRIBUTE_NORMAL,
+                        NULL);
+
+    if (hFile == INVALID_HANDLE_VALUE)
+    {
+        DPRINT1("Error when creating '%S' for memory dumping, GetLastError() = %u\n",
+                FileName, GetLastError());
+        return;
+    }
+
+    /* Dump the VM memory */
+    SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
+    Ptr1 = Ptr2 = REAL_TO_PHYS(NULL);
+    while (MAX_ADDRESS - (ULONG_PTR)PHYS_TO_REAL(Ptr1) > 0)
+    {
+        Ptr1 = Ptr2;
+        Line = LineBuffer;
+
+        /* Print the address */
+        Line += snprintf(Line, LINE_SIZE + LineBuffer - Line, "%08x ", PHYS_TO_REAL(Ptr1));
+
+        /* Print up to 16 bytes... */
+
+        /* ... in hexadecimal form first... */
+        i = 0;
+        while (i++ <= 0x0F && (MAX_ADDRESS - (ULONG_PTR)PHYS_TO_REAL(Ptr1) > 0))
+        {
+            Line += snprintf(Line, LINE_SIZE + LineBuffer - Line, " %02x", *Ptr1);
+            ++Ptr1;
+        }
+
+        /* ... align with spaces if needed... */
+        RtlFillMemory(Line, 0x0F + 4 - i, ' ');
+        Line += 0x0F + 4 - i;
+
+        /* ... then in character form. */
+        i = 0;
+        while (i++ <= 0x0F && (MAX_ADDRESS - (ULONG_PTR)PHYS_TO_REAL(Ptr2) > 0))
+        {
+            Line += snprintf(Line, LINE_SIZE + LineBuffer - Line, "%c",
+                            (*Ptr2 >= 0x20 && *Ptr2 <= 0x7E) || (*Ptr2 >= 0x80 && *Ptr2 < 0xFF) ? *Ptr2 : '.');
+            ++Ptr2;
+        }
+
+        /* Newline */
+        *Line++ = '\r';
+        *Line++ = '\n';
+
+        /* Finally write the line to the file */
+        LineSize = Line - LineBuffer;
+        WriteFile(hFile, LineBuffer, LineSize, &LineSize, NULL);
+    }
+
+    /* Close the file */
+    CloseHandle(hFile);
+}
+
 DWORD WINAPI PumpConsoleInput(LPVOID Parameter);
 
 BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput)
index 1b1337f..38450ec 100644 (file)
@@ -98,6 +98,8 @@ extern BOOLEAN VdmRunning;
 
 /* FUNCTIONS ******************************************************************/
 
+VOID DumpMemory(VOID);
+
 VOID WINAPI EmulatorReadMemory
 (
     PFAST486_STATE State,
index b620634..20d62ec 100644 (file)
@@ -15,5 +15,6 @@ END
 
 STRINGTABLE
 BEGIN
-    IDS_VDM_QUIT,   "&Ukončit ReactOS VDM"
+    IDS_VDM_DUMPMEM,    "Dump &Memory"
+    IDS_VDM_QUIT   ,    "&Ukončit ReactOS VDM"
 END
index 1f56de3..b6ee81a 100644 (file)
@@ -9,5 +9,6 @@ END
 
 STRINGTABLE
 BEGIN
-    IDS_VDM_QUIT,   "ReactOS VDM b&eenden"
+    IDS_VDM_DUMPMEM,    "Dump &Memory"
+    IDS_VDM_QUIT   ,    "ReactOS VDM b&eenden"
 END
index 4ec9d71..a6373b8 100644 (file)
@@ -9,5 +9,6 @@ END
 
 STRINGTABLE
 BEGIN
-    IDS_VDM_QUIT,   "&Quit the ReactOS VDM"
+    IDS_VDM_DUMPMEM,    "Dump &Memory"
+    IDS_VDM_QUIT   ,    "&Quit the ReactOS VDM"
 END
index 19d0591..00f6909 100644 (file)
@@ -9,5 +9,6 @@ END
 
 STRINGTABLE
 BEGIN
-    IDS_VDM_QUIT,   "&Salir de ReactOS VDM"
+    IDS_VDM_DUMPMEM,    "Dump &Memory"
+    IDS_VDM_QUIT   ,    "&Salir de ReactOS VDM"
 END
index 853aa52..01a1d52 100644 (file)
@@ -9,5 +9,6 @@ END
 
 STRINGTABLE
 BEGIN
-    IDS_VDM_QUIT,   "&Quitter la ReactOS VDM"
+    IDS_VDM_DUMPMEM,    "Vidage &Mémoire"
+    IDS_VDM_QUIT   ,    "&Quitter la ReactOS VDM"
 END
index ff3a096..9bce376 100644 (file)
@@ -9,5 +9,6 @@ END
 
 STRINGTABLE
 BEGIN
-    IDS_VDM_QUIT,   "&Esci da ReactOS VDM"
+    IDS_VDM_DUMPMEM,    "Dump &Memory"
+    IDS_VDM_QUIT   ,    "&Esci da ReactOS VDM"
 END
index e23ee40..84701cf 100644 (file)
@@ -11,5 +11,6 @@ END
 
 STRINGTABLE
 BEGIN
-    IDS_VDM_QUIT,   "&Wyjdź z ReactOS VDM"
+    IDS_VDM_DUMPMEM,    "Dump &Memory"
+    IDS_VDM_QUIT   ,    "&Wyjdź z ReactOS VDM"
 END
index 300c34f..d0e1293 100644 (file)
@@ -51,7 +51,8 @@ typedef struct _VDM_MENUITEM
 
 static const VDM_MENUITEM VdmMenuItems[] =
 {
-    { IDS_VDM_QUIT, NULL, ID_VDM_QUIT },
+    { IDS_VDM_DUMPMEM, NULL, ID_VDM_DUMPMEM },
+    { IDS_VDM_QUIT   , NULL, ID_VDM_QUIT    },
 
     { 0, NULL, 0 }      /* End of list */
 };
@@ -368,6 +369,10 @@ PumpConsoleInput(LPVOID Parameter)
                         ShowPointer = !ShowPointer;
                         break;
 
+                    case ID_VDM_DUMPMEM:
+                        DumpMemory();
+                        break;
+
                     case ID_VDM_QUIT:
                         /* Stop the VDM */
                         EmulatorTerminate();
index f7a6f3c..2534ff9 100644 (file)
@@ -1,14 +1,19 @@
 #pragma once
 
+/* Menu IDs */
 #define ID_SHOWHIDE_MOUSE   1000
-#define ID_VDM_QUIT         1001
+#define ID_VDM_DUMPMEM      1001
+#define ID_VDM_QUIT         1002
 
+/* String IDs */
 #define IDS_HIDE_MOUSE  100
 #define IDS_SHOW_MOUSE  101
 #define IDS_VDM_MENU    102
 
-#define IDS_VDM_QUIT    200
+#define IDS_VDM_DUMPMEM 200
+#define IDS_VDM_QUIT    201
 
+/* Icon */
 #define IDI_APPICON 1
 
 /* EOF */