From ad69b4a3c0807f646dbf0cf8359e9a0aa659262a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Sat, 17 May 2014 23:49:35 +0000 Subject: [PATCH] [NTVDM] Add a memory dump facility. svn path=/trunk/; revision=63342 --- reactos/subsystems/ntvdm/emulator.c | 81 ++++++++++++++++++++++++++ reactos/subsystems/ntvdm/emulator.h | 2 + reactos/subsystems/ntvdm/lang/cs-CZ.rc | 3 +- reactos/subsystems/ntvdm/lang/de-DE.rc | 3 +- reactos/subsystems/ntvdm/lang/en-US.rc | 3 +- reactos/subsystems/ntvdm/lang/es-ES.rc | 3 +- reactos/subsystems/ntvdm/lang/fr-FR.rc | 3 +- reactos/subsystems/ntvdm/lang/it-IT.rc | 3 +- reactos/subsystems/ntvdm/lang/pl-PL.rc | 3 +- reactos/subsystems/ntvdm/ntvdm.c | 7 ++- reactos/subsystems/ntvdm/resource.h | 9 ++- 11 files changed, 110 insertions(+), 10 deletions(-) diff --git a/reactos/subsystems/ntvdm/emulator.c b/reactos/subsystems/ntvdm/emulator.c index 094d156df7d..6503a0183a9 100644 --- a/reactos/subsystems/ntvdm/emulator.c +++ b/reactos/subsystems/ntvdm/emulator.c @@ -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) diff --git a/reactos/subsystems/ntvdm/emulator.h b/reactos/subsystems/ntvdm/emulator.h index 1b1337f3726..38450ecbd0e 100644 --- a/reactos/subsystems/ntvdm/emulator.h +++ b/reactos/subsystems/ntvdm/emulator.h @@ -98,6 +98,8 @@ extern BOOLEAN VdmRunning; /* FUNCTIONS ******************************************************************/ +VOID DumpMemory(VOID); + VOID WINAPI EmulatorReadMemory ( PFAST486_STATE State, diff --git a/reactos/subsystems/ntvdm/lang/cs-CZ.rc b/reactos/subsystems/ntvdm/lang/cs-CZ.rc index b620634924c..20d62ec5197 100644 --- a/reactos/subsystems/ntvdm/lang/cs-CZ.rc +++ b/reactos/subsystems/ntvdm/lang/cs-CZ.rc @@ -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 diff --git a/reactos/subsystems/ntvdm/lang/de-DE.rc b/reactos/subsystems/ntvdm/lang/de-DE.rc index 1f56de3559c..b6ee81afa1b 100644 --- a/reactos/subsystems/ntvdm/lang/de-DE.rc +++ b/reactos/subsystems/ntvdm/lang/de-DE.rc @@ -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 diff --git a/reactos/subsystems/ntvdm/lang/en-US.rc b/reactos/subsystems/ntvdm/lang/en-US.rc index 4ec9d710cbb..a6373b8e908 100644 --- a/reactos/subsystems/ntvdm/lang/en-US.rc +++ b/reactos/subsystems/ntvdm/lang/en-US.rc @@ -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 diff --git a/reactos/subsystems/ntvdm/lang/es-ES.rc b/reactos/subsystems/ntvdm/lang/es-ES.rc index 19d0591e7b8..00f69091b92 100644 --- a/reactos/subsystems/ntvdm/lang/es-ES.rc +++ b/reactos/subsystems/ntvdm/lang/es-ES.rc @@ -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 diff --git a/reactos/subsystems/ntvdm/lang/fr-FR.rc b/reactos/subsystems/ntvdm/lang/fr-FR.rc index 853aa5271a9..01a1d525f16 100644 --- a/reactos/subsystems/ntvdm/lang/fr-FR.rc +++ b/reactos/subsystems/ntvdm/lang/fr-FR.rc @@ -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 diff --git a/reactos/subsystems/ntvdm/lang/it-IT.rc b/reactos/subsystems/ntvdm/lang/it-IT.rc index ff3a0960de5..9bce376dbac 100644 --- a/reactos/subsystems/ntvdm/lang/it-IT.rc +++ b/reactos/subsystems/ntvdm/lang/it-IT.rc @@ -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 diff --git a/reactos/subsystems/ntvdm/lang/pl-PL.rc b/reactos/subsystems/ntvdm/lang/pl-PL.rc index e23ee40e887..84701cf04ee 100644 --- a/reactos/subsystems/ntvdm/lang/pl-PL.rc +++ b/reactos/subsystems/ntvdm/lang/pl-PL.rc @@ -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 diff --git a/reactos/subsystems/ntvdm/ntvdm.c b/reactos/subsystems/ntvdm/ntvdm.c index 300c34fb9cc..d0e1293cec7 100644 --- a/reactos/subsystems/ntvdm/ntvdm.c +++ b/reactos/subsystems/ntvdm/ntvdm.c @@ -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(); diff --git a/reactos/subsystems/ntvdm/resource.h b/reactos/subsystems/ntvdm/resource.h index f7a6f3c2a1b..2534ff9b80d 100644 --- a/reactos/subsystems/ntvdm/resource.h +++ b/reactos/subsystems/ntvdm/resource.h @@ -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 */