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

459 lines
13 KiB
C++
Raw Normal View History

2017-08-18 05:08:22 +00:00
#include "stdafx.h"
#include "DebuggerUI.h"
#include <stdio.h>
#include <Common/path.h>
#include "Symbols.h"
const CSetValueDlg::ComboItem CDebugSymbols::ModalChangeTypeItems[] = {
{ "code", SYM_CODE },
{ "data", SYM_DATA },
2020-02-11 03:42:25 +00:00
{ "uint8", SYM_U8 },
{ "int8", SYM_S8 },
{ "uint16", SYM_U16 },
{ "int16", SYM_S16 },
{ "uint32", SYM_U32 },
{ "int32", SYM_S32 },
{ "uint64", SYM_U64 },
{ "int64", SYM_S64 },
{ "float", SYM_FLOAT },
{ "double", SYM_DOUBLE },
{ "v2", SYM_VECTOR2 },
{ "v3", SYM_VECTOR3 },
{ "v4", SYM_VECTOR4 },
2021-04-12 11:35:39 +00:00
{ nullptr, 0 }
};
2017-08-18 05:08:22 +00:00
CDebugSymbols::CDebugSymbols(CDebuggerUI * debugger) :
CDebugDialog<CDebugSymbols>(debugger),
m_SymbolTable(debugger->SymbolTable()),
m_SymbolCacheStartIndex(0),
m_bFiltering(false)
{
memset(m_FilterText, 0, sizeof(m_FilterText));
2017-08-18 05:08:22 +00:00
}
LRESULT CDebugSymbols::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
2017-08-18 05:08:22 +00:00
{
DlgResize_Init(false, true);
DlgSavePos_Init(DebuggerUI_SymbolsPos);
m_SymbolsListView.Attach(GetDlgItem(IDC_SYMBOLS_LIST));
m_SymbolsListView.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER);
2017-08-18 05:08:22 +00:00
2020-05-12 12:19:05 +00:00
m_SymbolsListView.AddColumn(L"Address", 0);
m_SymbolsListView.AddColumn(L"Type", 1);
m_SymbolsListView.AddColumn(L"Name", 2);
m_SymbolsListView.AddColumn(L"Value", 3);
m_SymbolsListView.AddColumn(L"Description", 4);
2017-08-18 05:08:22 +00:00
m_SymbolsListView.SetColumnWidth(0, 70);
m_SymbolsListView.SetColumnWidth(1, 40);
m_SymbolsListView.SetColumnWidth(2, 120);
m_SymbolsListView.SetColumnWidth(3, 100);
m_SymbolsListView.SetColumnWidth(4, 120);
2017-08-18 05:08:22 +00:00
::SetWindowText(GetDlgItem(IDC_FILTER_EDIT), m_FilterText);
Refresh();
2017-08-18 05:08:22 +00:00
2021-04-12 11:35:39 +00:00
SetTimer(TIMER_ID_AUTO_REFRESH, 100, nullptr);
2017-08-18 05:08:22 +00:00
2019-12-25 00:41:20 +00:00
LoadWindowPos();
WindowCreated();
return 0;
2017-08-18 05:08:22 +00:00
}
void CDebugSymbols::OnExitSizeMove(void)
{
SaveWindowPos(true);
}
2017-08-18 05:08:22 +00:00
LRESULT CDebugSymbols::OnDestroy(void)
{
2019-12-25 00:41:20 +00:00
KillTimer(TIMER_ID_AUTO_REFRESH);
m_SymbolsListView.Detach();
return 0;
2017-08-18 05:08:22 +00:00
}
2019-12-25 00:41:20 +00:00
void CDebugSymbols::OnTimer(UINT_PTR nIDEvent)
2017-08-18 05:08:22 +00:00
{
2019-12-25 00:41:20 +00:00
if (nIDEvent == TIMER_ID_AUTO_REFRESH)
{
Refresh();
}
2017-08-18 05:08:22 +00:00
}
LRESULT CDebugSymbols::OnClicked(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
switch (wID)
{
case IDCANCEL:
EndDialog(0);
break;
case IDC_ADDSYMBOL_BTN:
m_AddSymbolDlg.DoModal(m_Debugger);
break;
case IDC_REMOVESYMBOL_BTN:
2019-12-25 00:41:20 +00:00
{
int nItem = m_SymbolsListView.GetSelectedIndex();
int id = -1;
if (m_bFiltering)
{
id = m_FilteredSymbols[nItem].m_Id;
}
else
{
CSymbol symbol;
m_SymbolTable->GetSymbolByIndex(nItem, &symbol);
id = symbol.m_Id;
}
if (id != -1)
{
m_SymbolTable->RemoveSymbolById(id);
m_SymbolTable->Save();
Refresh();
}
2019-12-25 00:41:20 +00:00
}
break;
}
return FALSE;
2017-08-18 05:08:22 +00:00
}
LRESULT CDebugSymbols::OnListDblClicked(NMHDR* pNMHDR)
2017-08-18 05:08:22 +00:00
{
NMITEMACTIVATE* pIA = reinterpret_cast<NMITEMACTIVATE*>(pNMHDR);
2020-02-11 03:42:25 +00:00
if (pIA->iItem == -1)
2020-02-11 03:42:25 +00:00
{
return 0;
2020-02-11 03:42:25 +00:00
}
int id = GetListItemSymbolId(pIA->iItem);
2020-02-11 03:42:25 +00:00
CSymbol symbol;
if (!m_SymbolTable->GetSymbolById(id, &symbol))
2020-02-11 03:42:25 +00:00
{
return 0;
}
POINT mousePt;
GetCursorPos(&mousePt);
int nSelectedCol = ColumnHitTest(mousePt);
2020-02-11 03:42:25 +00:00
switch (nSelectedCol)
{
case SymbolsListView_Col_Address:
// Open it in memory viewer/commands viewer
if (symbol.m_Type == SYM_CODE) // Code
2020-02-11 03:42:25 +00:00
{
m_Debugger->Debug_ShowCommandsLocation(symbol.m_Address, true);
}
else // Data/number
2020-02-11 03:42:25 +00:00
{
m_Debugger->Debug_ShowMemoryLocation(symbol.m_Address, true);
}
break;
case SymbolsListView_Col_Type:
if (m_SetValueDlg.DoModal("Change type", "New type:", symbol.m_Type, ModalChangeTypeItems))
{
ValueType t = (ValueType)m_SetValueDlg.GetEnteredData();
// TODO: Is there a better way?
m_SymbolTable->RemoveSymbolById(id);
m_SymbolTable->AddSymbol(t, symbol.m_Address, symbol.m_Name, symbol.m_Description);
2020-02-11 03:42:25 +00:00
}
break;
case SymbolsListView_Col_Name:
if (m_SetValueDlg.DoModal("Set name", "New name:", symbol.m_Name))
{
m_SymbolTable->RemoveSymbolById(id);
m_SymbolTable->AddSymbol(symbol.m_Type, symbol.m_Address, m_SetValueDlg.GetEnteredString().c_str(), symbol.m_Description);
2020-02-11 03:42:25 +00:00
}
break;
case SymbolsListView_Col_Value:
char szValue[256];
2020-05-12 12:19:05 +00:00
const char* x;
const char* y;
m_SymbolTable->GetValueString(szValue, &symbol);
if (m_SetValueDlg.DoModal("Change value", "New value:", szValue))
{
const std::string & EnteredString = m_SetValueDlg.GetEnteredString();
2020-05-12 12:19:05 +00:00
2020-02-11 03:42:25 +00:00
switch (symbol.m_Type)
{
case SYM_U8:
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<uint8_t>(symbol.m_Address, (uint8_t)atoi(EnteredString.c_str()));
2020-02-11 03:42:25 +00:00
break;
case SYM_U16:
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<uint16_t>(symbol.m_Address, (uint16_t)atoi(EnteredString.c_str()));
2020-02-11 03:42:25 +00:00
break;
case SYM_U32:
2020-05-12 12:19:05 +00:00
m_Debugger->DebugStore_VAddr<uint32_t>(symbol.m_Address, atoi(EnteredString.c_str()));
2020-02-11 03:42:25 +00:00
break;
case SYM_U64:
2020-05-12 12:19:05 +00:00
m_Debugger->DebugStore_VAddr<uint64_t>(symbol.m_Address, atoll(EnteredString.c_str()));
2020-02-11 03:42:25 +00:00
break;
case SYM_S8:
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<int8_t>(symbol.m_Address, (int8_t)atoi(EnteredString.c_str()));
2020-02-11 03:42:25 +00:00
break;
case SYM_S16:
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<int16_t>(symbol.m_Address, (int16_t)atoi(EnteredString.c_str()));
2020-02-11 03:42:25 +00:00
break;
case SYM_S32:
2020-05-12 12:19:05 +00:00
m_Debugger->DebugStore_VAddr<int>(symbol.m_Address, atoi(EnteredString.c_str()));
2020-02-11 03:42:25 +00:00
break;
case SYM_S64:
2020-05-12 12:19:05 +00:00
m_Debugger->DebugStore_VAddr<int64_t>(symbol.m_Address, atoll(EnteredString.c_str()));
2020-02-11 03:42:25 +00:00
break;
case SYM_FLOAT:
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<float>(symbol.m_Address, (float)atof(EnteredString.c_str()));
2020-02-11 03:42:25 +00:00
break;
case SYM_DOUBLE:
2020-05-12 12:19:05 +00:00
m_Debugger->DebugStore_VAddr<double>(symbol.m_Address, atof(EnteredString.c_str()));
2020-02-11 03:42:25 +00:00
break;
case SYM_VECTOR2:
2020-05-12 12:19:05 +00:00
x = EnteredString.c_str();
y = strchr(x, ',');
memcpy(szValue, x, y - x);
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<float>(symbol.m_Address, (float)atof(szValue));
x = x + (y - x) + 1;
memcpy(szValue, x, strlen(x));
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<float>(symbol.m_Address + sizeof(float), (float)atof(szValue));
break;
case SYM_VECTOR3:
2020-05-12 12:19:05 +00:00
x = EnteredString.c_str();
y = strchr(x, ',');
memcpy(szValue, x, y - x);
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<float>(symbol.m_Address, (float)atof(szValue));
x = x + (y - x) + 1;
y = strchr(x, ',');
memcpy(szValue, x, y - x);
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<float>(symbol.m_Address + sizeof(float), (float)atof(szValue));
x = x + (y - x) + 1;
memcpy(szValue, x, strlen(x));
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<float>(symbol.m_Address + (sizeof(float) * 2), (float)atof(szValue));
break;
case SYM_VECTOR4:
2020-05-12 12:19:05 +00:00
x = EnteredString.c_str();
y = strchr(x, ',');
memcpy(szValue, x, y - x);
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<float>(symbol.m_Address, (float)atof(szValue));
x = x + (y - x) + 1;
y = strchr(x, ',');
memcpy(szValue, x, y - x);
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<float>(symbol.m_Address + sizeof(float), (float)atof(szValue));
x = x + (y - x) + 1;
y = strchr(x, ',');
memcpy(szValue, x, y - x);
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<float>(symbol.m_Address + (sizeof(float) * 2), (float)atof(szValue));
x = x + (y - x) + 1;
memcpy(szValue, x, strlen(x));
2021-01-19 05:58:59 +00:00
m_Debugger->DebugStore_VAddr<float>(symbol.m_Address + (sizeof(float) * 3), (float)atof(szValue));
break;
2020-02-11 03:42:25 +00:00
}
}
2020-02-11 03:42:25 +00:00
break;
case SymbolsListView_Col_Description:
if (m_SetValueDlg.DoModal("Set description", "New description:", symbol.m_Description))
{
m_SymbolTable->RemoveSymbolById(id);
m_SymbolTable->AddSymbol(symbol.m_Type, symbol.m_Address, symbol.m_Name, m_SetValueDlg.GetEnteredString().c_str());
2020-02-11 03:42:25 +00:00
}
break;
}
m_SymbolTable->Save();
2020-02-11 03:42:25 +00:00
Refresh();
return 0;
2017-08-18 05:08:22 +00:00
}
LRESULT CDebugSymbols::OnListGetDispInfo(NMHDR* pNMHDR)
2017-08-18 05:08:22 +00:00
{
NMLVDISPINFO* plvdi = (NMLVDISPINFO*)pNMHDR;
int index = plvdi->item.iItem;
if (index == -1)
{
return TRUE;
}
std::vector<SymbolCacheItem>& cache = m_bFiltering ? m_FilteredSymbols : m_SymbolCache;
if (!m_bFiltering)
{
index -= m_SymbolCacheStartIndex;
}
switch (plvdi->item.iSubItem)
{
case SymbolsListView_Col_Address:
plvdi->item.pszText = cache[index].m_Address;
break;
case SymbolsListView_Col_Type:
plvdi->item.pszText = cache[index].m_Type;
break;
case SymbolsListView_Col_Name:
plvdi->item.pszText = cache[index].m_Name;
break;
case SymbolsListView_Col_Value:
plvdi->item.pszText = cache[index].m_Value;
break;
case SymbolsListView_Col_Description:
plvdi->item.pszText = cache[index].m_Description;
break;
}
return TRUE;
}
LRESULT CDebugSymbols::OnListCacheHint(NMHDR * pNMHDR)
{
NMLVCACHEHINT* plvch = (NMLVCACHEHINT*)pNMHDR;
if (m_bFiltering)
{
return 0;
}
m_SymbolCacheStartIndex = plvch->iFrom;
m_SymbolCache.clear();
for (int index = plvch->iFrom; index <= plvch->iTo; index++)
{
CSymbol symbol;
if (!m_SymbolTable->GetSymbolByIndex(index, &symbol))
{
break;
}
SymbolCacheItem item(symbol, m_SymbolTable);
m_SymbolCache.push_back(item);
}
return 0;
2017-08-18 05:08:22 +00:00
}
LRESULT CDebugSymbols::OnFilterChanged(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
2017-08-18 05:08:22 +00:00
{
if (::GetWindowTextLength(GetDlgItem(IDC_FILTER_EDIT)) == 0)
{
m_bFiltering = false;
Refresh();
return FALSE;
}
2019-12-25 00:41:20 +00:00
m_bFiltering = true;
::GetWindowText(GetDlgItem(IDC_FILTER_EDIT), m_FilterText, sizeof(m_FilterText) / sizeof(wchar_t));
UpdateFilteredSymbols();
m_SymbolsListView.SetItemCount(m_FilteredSymbols.size());
return FALSE;
}
int CDebugSymbols::ColumnHitTest(POINT& pt)
{
int nHitCol = -1;
RECT listRect;
m_SymbolsListView.GetWindowRect(&listRect);
int mouseX = pt.x - listRect.left;
for (int nCol = 0, colX = 0; nCol < SymbolsListView_Num_Columns; nCol++)
{
int colWidth = m_SymbolsListView.GetColumnWidth(nCol);
if (mouseX >= colX && mouseX <= colX + colWidth)
{
nHitCol = nCol;
break;
}
colX += colWidth;
}
return nHitCol;
}
void CDebugSymbols::UpdateFilteredSymbols()
{
m_FilteredSymbols.clear();
2019-12-25 00:41:20 +00:00
CSymbol symbol;
size_t index = 0;
while (m_SymbolTable->GetSymbolByIndex(index++, &symbol))
{
std::wstring strName = stdstr(symbol.m_Name).ToUTF16();
std::wstring strDesc = stdstr(symbol.m_Description).ToUTF16();
if (strName.find(m_FilterText) != std::wstring::npos ||
strDesc.find(m_FilterText) != std::wstring::npos)
{
SymbolCacheItem item(symbol, m_SymbolTable);
m_FilteredSymbols.push_back(item);
}
}
}
void CDebugSymbols::Refresh()
{
if (g_Settings->LoadStringVal(Game_GameName) != "")
{
stdstr windowTitle = stdstr_f("Symbols - %s", g_Settings->LoadStringVal(Game_GameName).c_str());
SetWindowText(windowTitle.ToUTF16().c_str());
}
else
{
SetWindowText(L"Symbols");
}
if (m_SymbolsListView.m_hWnd == nullptr)
{
return;
}
2017-08-18 05:08:22 +00:00
int numSymbols = 0;
if (m_bFiltering)
{
UpdateFilteredSymbols();
numSymbols = m_FilteredSymbols.size();
}
else
{
numSymbols = m_SymbolTable->GetCount();
}
m_SymbolsListView.SetItemCountEx(numSymbols, LVSICF_NOSCROLL);
}
2017-08-18 05:08:22 +00:00
int CDebugSymbols::GetListItemSymbolId(int nItem)
{
if (m_bFiltering)
{
if (nItem >= m_FilteredSymbols.size())
2019-12-25 00:41:20 +00:00
{
g_Notify->BreakPoint(__FILE__, __LINE__);
2019-12-25 00:41:20 +00:00
}
2017-08-18 05:08:22 +00:00
return m_FilteredSymbols[nItem].m_Id;
}
2017-08-18 05:08:22 +00:00
CSymbol symbol;
if (!m_SymbolTable->GetSymbolByIndex(nItem, &symbol))
{
g_Notify->BreakPoint(__FILE__, __LINE__);
}
return symbol.m_Id;
2019-12-25 00:41:20 +00:00
}