project64/Source/Project64/UserInterface/Debugger/Debugger-MemoryDump.cpp

201 lines
6.3 KiB
C++
Raw Normal View History

2016-01-27 09:11:59 +00:00
#include "stdafx.h"
#include "DebuggerUI.h"
#include <Project64-core/N64System/Mips/R4300iInstruction.h>
2017-08-18 05:08:22 +00:00
2016-01-27 09:11:59 +00:00
CDumpMemory::CDumpMemory(CDebuggerUI * debugger) :
CDebugDialog<CDumpMemory>(debugger)
{
}
CDumpMemory::~CDumpMemory()
{
}
2019-12-25 00:41:20 +00:00
LRESULT CDumpMemory::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
2016-01-27 09:11:59 +00:00
{
DlgSavePos_Init(DebuggerUI_MemoryDumpPos);
2016-01-27 09:11:59 +00:00
m_StartAddress.Attach(GetDlgItem(IDC_E_START_ADDR));
m_EndAddress.Attach(GetDlgItem(IDC_E_END_ADDR));
m_PC.Attach(GetDlgItem(IDC_E_ALT_PC));
2019-12-25 00:41:20 +00:00
m_FormatList.Attach(GetDlgItem(IDC_FORMAT));
m_FileName.Attach(GetDlgItem(IDC_FILENAME));
2016-01-27 09:11:59 +00:00
m_StartAddress.SetDisplayType(CEditNumber32::DisplayHex);
m_EndAddress.SetDisplayType(CEditNumber32::DisplayHex);
m_PC.SetDisplayType(CEditNumber32::DisplayHex);
2016-01-27 09:11:59 +00:00
2019-12-25 00:41:20 +00:00
uint32_t startAddress = 0x80000000;
uint32_t endAddress = startAddress + (g_MMU ? g_MMU->RdramSize() : 0x400000);
2017-08-18 05:08:22 +00:00
m_StartAddress.SetValue(startAddress, DisplayMode::AllHex);
m_EndAddress.SetValue(endAddress, DisplayMode::AllHex);
2017-08-18 05:08:22 +00:00
m_PC.SetValue(startAddress);
2019-12-25 00:41:20 +00:00
2020-05-12 12:19:05 +00:00
int nIndex = m_FormatList.AddString(L"TEXT - Disassembly + PC");
2019-12-25 00:41:20 +00:00
m_FormatList.SetItemData(nIndex, (DWORD_PTR)DisassemblyWithPC);
2017-08-18 05:08:22 +00:00
2020-05-12 12:19:05 +00:00
nIndex = m_FormatList.AddString(L"RAW - Big Endian (N64)");
2019-12-25 00:41:20 +00:00
m_FormatList.SetItemData(nIndex, (LPARAM)RawBigEndian);
2017-08-18 05:08:22 +00:00
2019-12-25 00:41:20 +00:00
m_FormatList.SetCurSel(0);
2016-01-27 09:11:59 +00:00
LoadWindowPos();
2016-01-27 09:11:59 +00:00
WindowCreated();
return TRUE;
}
void CDumpMemory::OnExitSizeMove(void)
{
SaveWindowPos(0);
}
LRESULT CDumpMemory::OnClicked(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
2016-01-27 09:11:59 +00:00
{
switch (wID)
{
case IDCANCEL:
EndDialog(0);
break;
case IDC_BTN_CHOOSE_FILE:
{
g_BaseSystem->ExternalEvent(SysEvent_PauseCPU_DumpMemory);
2019-12-25 00:41:20 +00:00
int CurrentFormatSel = m_FormatList.GetCurSel();
DumpFormat Format = (DumpFormat)m_FormatList.GetItemData(CurrentFormatSel);
2017-08-18 05:08:22 +00:00
2019-12-25 00:41:20 +00:00
const char* FileFilter = "All files (*.*)\0*.*\0";
2017-08-18 05:08:22 +00:00
2019-12-25 00:41:20 +00:00
if (Format == RawBigEndian)
{
FileFilter = "Binary file (*.bin)\0*.bin;\0All files (*.*)\0*.*\0";
}
else if (Format == DisassemblyWithPC)
{
FileFilter = "Text file (*.txt)\0*.txt;\0All files (*.*)\0*.*\0";
}
2017-08-18 05:08:22 +00:00
CPath FileName;
2019-12-25 00:41:20 +00:00
2017-08-18 05:08:22 +00:00
if (FileName.SelectFile(m_hWnd, CPath(CPath::MODULE_DIRECTORY), FileFilter, false))
2016-01-27 09:11:59 +00:00
{
if (FileName.GetExtension().length() == 0)
2016-01-27 09:11:59 +00:00
{
2017-08-18 05:08:22 +00:00
FileName.SetExtension(Format == RawBigEndian ? "bin" : "txt");
2020-05-12 12:19:05 +00:00
m_FileName.SetWindowText(stdstr((const char *)FileName).ToUTF16().c_str());
2016-01-27 09:11:59 +00:00
}
}
g_BaseSystem->ExternalEvent(SysEvent_ResumeCPU_DumpMemory);
}
break;
case IDOK:
{
TCHAR FileName[MAX_PATH];
2017-08-18 05:08:22 +00:00
int CurrentFormatSel = m_FormatList.GetCurSel();
DumpFormat Format = (DumpFormat) m_FormatList.GetItemData(CurrentFormatSel);
2019-12-25 00:41:20 +00:00
2016-01-27 09:11:59 +00:00
DWORD StartPC = m_StartAddress.GetValue();
DWORD EndPC = m_EndAddress.GetValue();
DWORD DumpPC = m_PC.GetValue();
GetDlgItemText(IDC_FILENAME, FileName, sizeof(FileName));
2020-05-12 12:19:05 +00:00
if (wcslen(FileName) == 0)
2016-01-27 09:11:59 +00:00
{
g_Notify->DisplayWarning("Please choose target file");
2016-01-27 09:11:59 +00:00
::SetFocus(GetDlgItem(IDC_FILENAME));
return false;
}
if (SendDlgItemMessage(IDC_USE_ALT_PC, BM_GETSTATE, 0, 0) != BST_CHECKED)
{
2017-08-18 05:08:22 +00:00
DumpPC = StartPC;
2016-01-27 09:11:59 +00:00
}
// Disable buttons
2016-01-27 09:11:59 +00:00
::EnableWindow(GetDlgItem(IDC_E_START_ADDR), FALSE);
::EnableWindow(GetDlgItem(IDC_E_END_ADDR), FALSE);
::EnableWindow(GetDlgItem(IDC_E_ALT_PC), FALSE);
::EnableWindow(GetDlgItem(IDC_USE_ALT_PC), FALSE);
::EnableWindow(GetDlgItem(IDC_FILENAME), FALSE);
::EnableWindow(GetDlgItem(IDC_BTN_CHOOSE_FILE), FALSE);
::EnableWindow(GetDlgItem(IDC_FORMAT), FALSE);
::EnableWindow(GetDlgItem(IDOK), FALSE);
::EnableWindow(GetDlgItem(IDCANCEL), FALSE);
g_BaseSystem->ExternalEvent(SysEvent_PauseCPU_DumpMemory);
if (!DumpMemory(FileName, Format, StartPC, EndPC, DumpPC))
{
// Enable buttons
2016-01-27 09:11:59 +00:00
g_BaseSystem->ExternalEvent(SysEvent_ResumeCPU_DumpMemory);
return false;
}
g_BaseSystem->ExternalEvent(SysEvent_ResumeCPU_DumpMemory);
}
EndDialog(0);
break;
}
return FALSE;
}
2020-05-12 12:19:05 +00:00
bool CDumpMemory::DumpMemory(LPCTSTR FileName, DumpFormat Format, DWORD StartPC, DWORD EndPC, DWORD DumpPC)
2016-01-27 09:11:59 +00:00
{
2020-05-12 12:19:05 +00:00
stdstr strFile = stdstr().FromUTF16(FileName);
2019-12-25 00:41:20 +00:00
if (Format == DisassemblyWithPC)
{
CLog LogFile;
2020-05-12 12:19:05 +00:00
if (!LogFile.Open(strFile.c_str()))
2019-12-25 00:41:20 +00:00
{
2020-05-12 12:19:05 +00:00
g_Notify->DisplayError(stdstr_f("Failed to open\n%s", strFile.c_str()).c_str());
2019-12-25 00:41:20 +00:00
return false;
}
LogFile.SetFlush(false);
LogFile.SetTruncateFile(false);
for (uint32_t pc = StartPC; pc < EndPC; pc += 4, DumpPC += 4)
{
R4300iOpcode opcode;
m_Debugger->DebugLoad_VAddr(pc, opcode.Value);
2019-12-25 00:41:20 +00:00
R4300iInstruction Instruction(DumpPC, opcode.Value);
LogFile.LogF("%X: %-15s%s\r\n", DumpPC, Instruction.Name(), Instruction.Param());
2019-12-25 00:41:20 +00:00
}
m_StartAddress.SetValue(StartPC, DisplayMode::AllHex);
m_EndAddress.SetValue(EndPC, DisplayMode::AllHex);
2019-12-25 00:41:20 +00:00
return true;
}
if (Format == RawBigEndian)
{
CFile dumpFile;
2020-05-12 12:19:05 +00:00
if (!dumpFile.Open(strFile.c_str(), CFile::modeCreate | CFile::modeWrite))
2019-12-25 00:41:20 +00:00
{
2020-05-12 12:19:05 +00:00
g_Notify->DisplayError(stdstr_f("Failed to open\n%s", strFile.c_str()).c_str());
2019-12-25 00:41:20 +00:00
return false;
}
uint32_t dumpLen = EndPC - StartPC;
2021-05-25 02:41:22 +00:00
std::unique_ptr<uint8_t[]> dumpBuf = std::make_unique<uint8_t[]>(dumpLen);
2019-12-25 00:41:20 +00:00
uint32_t dumpIdx = 0;
for (uint32_t pc = StartPC; pc < EndPC; pc++, dumpIdx++)
{
2021-04-22 00:00:27 +00:00
bool bReadable = m_Debugger->DebugLoad_VAddr(pc, dumpBuf.get()[dumpIdx]);
2019-12-25 00:41:20 +00:00
if (!bReadable)
{
2020-05-12 12:19:05 +00:00
g_Notify->DisplayError(stdstr_f("Address error\n%s", strFile.c_str()).c_str());
2019-12-25 00:41:20 +00:00
dumpFile.Close();
return false;
}
}
dumpFile.SeekToBegin();
2021-04-22 00:00:27 +00:00
dumpFile.Write(dumpBuf.get(), dumpLen);
2019-12-25 00:41:20 +00:00
dumpFile.Close();
return true;
}
2017-08-18 05:08:22 +00:00
2016-01-27 09:11:59 +00:00
return false;
}