Skip to content

Memory Observer improvements: #1927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 56 additions & 11 deletions src/gui/widgets/memory_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,313 +95,358 @@
ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV;

if (ImGui::BeginTabItem(_("Plain search"))) {
bool gotEnter = ImGui::InputText(_("Pattern"), &m_plainSearchString, ImGuiInputTextFlags_EnterReturnsTrue);
bool gotEnter = ImGui::InputText(
_("Pattern"), &m_plainSearchString,
ImGuiInputTextFlags_EnterReturnsTrue |
(m_plainHex ? ImGuiInputTextFlags_CharsHexadecimal : ImGuiInputTextFlags_CharsDecimal));
ImGui::Checkbox(_("Hex"), &m_plainHex);
auto needleSize = 0;
std::string needle;
bool valid = true;
if (m_plainHex) {
char n = 0;
bool gotOne = false;
auto maybePushOne = [&]() {
if (gotOne) {
needle += n;
gotOne = false;
needleSize++;
n = 0;
} else {
gotOne = true;
}
};
for (auto c : m_plainSearchString) {
if (c >= '0' && c <= '9') {
n <<= 4;
n |= c - '0';
maybePushOne();
} else if (c >= 'a' && c <= 'f') {
n <<= 4;
n |= c - 'a' + 10;
maybePushOne();
} else if (c >= 'A' && c <= 'F') {
n <<= 4;
n |= c - 'A' + 10;
maybePushOne();
} else if (c == ' ') {
if (gotOne) {
needle += n;
gotOne = false;
needleSize++;
n = 0;
}
} else {
valid = false;
break;
}
}
if (gotOne) {
needle += n;
needleSize++;
}
} else {
needleSize = m_plainSearchString.size();
needle = m_plainSearchString;
}
if (!valid) {
ImGui::BeginDisabled();
}
if (ImGui::Button(_("Search")) || (gotEnter && valid)) {
auto ptr = memData;
m_plainAddresses.clear();
while (true) {
auto found = reinterpret_cast<const uint8_t*>(
memmem(ptr, memData + memSize - ptr, needle.c_str(), needleSize));
if (found) {
m_plainAddresses.push_back(memBase + static_cast<uint32_t>(found - memData));
ptr = reinterpret_cast<const uint8_t*>(found) + 1;
} else {
break;
}
}
}
if (!valid) {
ImGui::EndDisabled();
}
if (ImGui::BeginTable(_("Found values"), 2, tableFlags)) {
if (ImGui::BeginTable(_("Found values"), 3, tableFlags)) {
ImGui::TableSetupColumn(_("Address"));
ImGui::TableSetupColumn(_("Access"));
ImGui::TableSetupColumn(_("Remove"));
ImGui::TableHeadersRow();

ImGuiListClipper clipper;
clipper.Begin(m_plainAddresses.size());
while (clipper.Step()) {
for (int row = clipper.DisplayStart; row < clipper.DisplayEnd; ++row) {
const auto& currentAddress = m_plainAddresses[row];

ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text("%x", currentAddress);
ImGui::TableSetColumnIndex(1);
auto buttonName = fmt::format(f_("Show in memory editor##{}"), row);
if (ImGui::Button(buttonName.c_str())) {
const uint32_t editorAddress = currentAddress - memBase;
g_system->m_eventBus->signal(
PCSX::Events::GUI::JumpToMemory{editorAddress, static_cast<unsigned>(needleSize)});
}
ImGui::TableSetColumnIndex(2);
auto removeButtonName = fmt::format(f_("╳##{}"), row);
if (ImGui::Button(removeButtonName.c_str())) {
m_plainAddresses.erase(m_plainAddresses.begin() + row);
clipper.Begin(m_plainAddresses.size());
break;
}
}
}
ImGui::EndTable();
}
ImGui::EndTabItem();
}

if (ImGui::BeginTabItem(_("Delta-over-time search"))) {
const auto stride = getStrideFromValueType(m_scanValueType);

if (m_addressValuePairs.empty() && ImGui::Button(_("First scan"))) {
int64_t memValue = 0;

for (uint32_t i = 0; i < memSize; ++i) {
if (i != 0 && i % stride == 0) {
switch (m_scanType) {
case ScanType::ExactValue:
if (memValue == m_value) {
m_addressValuePairs.push_back({memBase + i - stride, memValue});
}
break;
case ScanType::GreaterThan:
if (memValue > m_value) {
m_addressValuePairs.push_back({memBase + i - stride, memValue});
}
break;
case ScanType::LessThan:
if (memValue < m_value) {
m_addressValuePairs.push_back({memBase + i - stride, memValue});
}
break;
case ScanType::Changed:
case ScanType::Unchanged:
case ScanType::Increased:
case ScanType::Decreased:
break;
case ScanType::UnknownInitialValue:
m_addressValuePairs.push_back({memBase + i - stride, memValue});
break;
}

memValue = 0;
}

const uint8_t currentByte = memData[i];
const uint8_t leftShift = 8 * (i % stride);
const uint32_t mask = 0xffffffff ^ (0xff << leftShift);
const int byteToWrite = currentByte << leftShift;
memValue = (memValue & mask) | byteToWrite;
memValue = getValueAsSelectedType(memValue);
}
}

if (!m_addressValuePairs.empty() && ImGui::Button(_("Next scan"))) {
auto doesntMatchCriterion = [this, memData, memSize, stride](const AddressValuePair& addressValuePair) {
const uint32_t address = addressValuePair.address;
const int64_t memValue =
getValueAsSelectedType(getMemValue(address, memData, memSize, memBase, stride));

switch (m_scanType) {
case ScanType::ExactValue:
return memValue != m_value;
case ScanType::GreaterThan:
return memValue <= m_value;
case ScanType::LessThan:
return memValue >= m_value;
case ScanType::Changed:
return memValue == addressValuePair.scannedValue;
case ScanType::Unchanged:
return memValue != addressValuePair.scannedValue;
case ScanType::Increased:
return memValue <= addressValuePair.scannedValue;
case ScanType::Decreased:
return memValue >= addressValuePair.scannedValue;
case ScanType::UnknownInitialValue:
return true;
}

return true;
};

std::erase_if(m_addressValuePairs, doesntMatchCriterion);

if (m_addressValuePairs.empty()) {
m_scanType = ScanType::ExactValue;
} else {
for (auto& addressValuePair : m_addressValuePairs) {
addressValuePair.scannedValue = getValueAsSelectedType(
getMemValue(addressValuePair.address, memData, memSize, memBase, stride));
}
}
}

if (!m_addressValuePairs.empty() && ImGui::Button(_("New scan"))) {
m_addressValuePairs.clear();
m_scanType = ScanType::ExactValue;
}

ImGui::Checkbox(_("Hex"), &m_hex);

if (!m_hex && stride > 1) {
ImGui::SameLine();
ImGui::Checkbox(_("Display as fixed-point values"), &m_fixedPoint);
}

ImGui::InputScalar(_("Value"), ImGuiDataType_S64, &m_value, NULL, NULL, m_hex ? "%x" : "%i",
m_hex ? ImGuiInputTextFlags_CharsHexadecimal : ImGuiInputTextFlags_CharsDecimal);
m_value = getValueAsSelectedType(m_value);

const auto currentScanValueType = magic_enum::enum_name(m_scanValueType);
if (ImGui::BeginCombo(_("Value type"), currentScanValueType.data())) {
for (auto v : magic_enum::enum_values<ScanValueType>()) {
bool selected = (v == m_scanValueType);
auto name = magic_enum::enum_name(v);
if (ImGui::Selectable(name.data(), selected)) {
m_scanValueType = v;
}
if (selected) {
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}

const auto currentScanType = magic_enum::enum_name(m_scanType);
if (ImGui::BeginCombo(_("Scan type"), currentScanType.data())) {
for (auto v : magic_enum::enum_values<ScanType>()) {
bool selected = (v == m_scanType);
auto name = magic_enum::enum_name(v);
if (ImGui::Selectable(name.data(), selected)) {
m_scanType = v;
}
if (selected) {
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}

if (!m_hex && stride > 1) {
ImGui::Checkbox(_("Display as fixed-point values"), &m_fixedPoint);
ImGui::Separator();
if (ImGui::Button(_("Freeze all"))) {
for (auto& addressValuePair : m_addressValuePairs) {
addressValuePair.frozen = true;
addressValuePair.frozenValue = getValueAsSelectedType(getMemValue(
addressValuePair.address, memData, memSize, memBase, getStrideFromValueType(m_scanValueType)));
}
}
ImGui::SameLine();
if (ImGui::Button(_("Unfreeze all"))) {
for (auto& addressValuePair : m_addressValuePairs) {
addressValuePair.frozen = false;
}
}
ImGui::SameLine();
if (ImGui::Button(_("Remove all frozen addresses"))) {
m_addressValuePairs.erase(std::remove_if(m_addressValuePairs.begin(), m_addressValuePairs.end(),
[](const AddressValuePair& pair) { return pair.frozen; }),
m_addressValuePairs.end());
}

if (ImGui::BeginTable(_("Found values"), 6, tableFlags)) {
ImGui::TableSetupColumn(_("Address"));
ImGui::TableSetupColumn(_("Current value"));
ImGui::TableSetupColumn(_("Scanned value"));
ImGui::TableSetupColumn(_("Access"));
ImGui::TableSetupColumn(_("Read breakpoint"));
ImGui::TableSetupColumn(_("Write breakpoint"));
ImGui::TableSetupColumn(_("Add breakpoint"));
ImGui::TableSetupColumn(_("Remove"));
ImGui::TableHeadersRow();

bool as_uint = (m_scanValueType == ScanValueType::Uint);
const auto valueDisplayFormat = m_hex ? "%x"
: (m_fixedPoint && stride > 1) ? (as_uint ? "%u.%u" : "%i.%i")
: (as_uint ? "%u" : "%i");

ImGuiListClipper clipper;
clipper.Begin(m_addressValuePairs.size());
while (clipper.Step()) {
for (int row = clipper.DisplayStart; row < clipper.DisplayEnd; ++row) {
auto& addressValuePair = m_addressValuePairs[row];
const uint32_t currentAddress = addressValuePair.address;
const auto memValue =
getValueAsSelectedType(getMemValue(currentAddress, memData, memSize, memBase, stride));
const auto scannedValue = addressValuePair.scannedValue;
const bool displayAsFixedPoint = !m_hex && m_fixedPoint && stride > 1;

ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text("%x", currentAddress);
ImGui::TableSetColumnIndex(1);
if (displayAsFixedPoint) {
ImGui::Text(valueDisplayFormat, memValue >> 12, memValue & 0xfff);
} else {
ImGui::Text(valueDisplayFormat, memValue);
}
ImGui::SameLine();
auto CheckboxName = fmt::format(f_("Freeze##{}"), row);
if (ImGui::Checkbox(CheckboxName.c_str(), &addressValuePair.frozen)) {
addressValuePair.frozenValue = memValue;
}
ImGui::TableSetColumnIndex(2);
if (displayAsFixedPoint) {
ImGui::Text(valueDisplayFormat, scannedValue >> 12, scannedValue & 0xfff);
} else {
ImGui::Text(valueDisplayFormat, scannedValue);
}
ImGui::TableSetColumnIndex(3);
auto showInMemEditorButtonName = fmt::format(f_("Show in memory editor##{}"), row);
if (ImGui::Button(showInMemEditorButtonName.c_str())) {
const uint32_t editorAddress = currentAddress - memBase;
g_system->m_eventBus->signal(PCSX::Events::GUI::JumpToMemory{editorAddress, stride});
}
ImGui::TableSetColumnIndex(4);
auto addReadBreakpointButtonName = fmt::format(f_("Add read breakpoint##{}"), row);
auto addReadBreakpointButtonName = fmt::format(f_("Read##{}"), row);
if (ImGui::Button(addReadBreakpointButtonName.c_str())) {
g_emulator->m_debug->addBreakpoint(currentAddress, Debug::BreakpointType::Read, stride,
_("Memory Observer"));
}
ImGui::TableSetColumnIndex(5);
auto addWriteBreakpointButtonName = fmt::format(f_("Add write breakpoint##{}"), row);
ImGui::SameLine();
auto addWriteBreakpointButtonName = fmt::format(f_("Write##{}"), row);
if (ImGui::Button(addWriteBreakpointButtonName.c_str())) {
g_emulator->m_debug->addBreakpoint(currentAddress, Debug::BreakpointType::Write, stride,
_("Memory Observer"));
}
ImGui::TableSetColumnIndex(5);
auto removeButtonName = fmt::format(f_("╳##{}"), row);
if (ImGui::Button(removeButtonName.c_str())) {
m_addressValuePairs.erase(m_addressValuePairs.begin() + row);
clipper.Begin(m_addressValuePairs.size());
break;
}
}
}
ImGui::EndTable();
}

ImGui::EndTabItem();
}

if (ImGui::BeginTabItem(_("Pattern search"))) {
if (m_useSIMD) {
ImGui::TextUnformatted(_("Sequence size: "));
ImGui::SameLine();
ImGui::RadioButton(_("8 bytes (fast)"), &m_sequenceSize, 8);
if (ImGui::RadioButton(_("8 bytes (fast)"), &m_sequenceSize, 8) && strlen(m_sequence) > 8) {
m_sequence[8] = '\0';
}
ImGui::SameLine();
ImGui::RadioButton(_("16 bytes (fast)"), &m_sequenceSize, 16);
if (ImGui::RadioButton(_("16 bytes (fast)"), &m_sequenceSize, 16) && strlen(m_sequence) > 16) {
m_sequence[16] = '\0';
}

Check warning on line 449 in src/gui/widgets/memory_observer.cc

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

PCSX::Widgets::MemoryObserver::draw increases in cyclomatic complexity from 99 to 111, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
ImGui::SameLine();
ImGui::RadioButton(_("Arbitrary"), &m_sequenceSize, 255);
}
Expand Down
Loading