diff --git a/CMakeLists.txt b/CMakeLists.txt index eadfe3cb74..f6735209f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1539,6 +1539,8 @@ list(APPEND NativeAppSource UI/ImDebugger/ImMemView.h UI/ImDebugger/ImStructViewer.cpp UI/ImDebugger/ImStructViewer.h + UI/ImDebugger/ImJitViewer.cpp + UI/ImDebugger/ImJitViewer.h UI/DiscordIntegration.cpp UI/NativeApp.cpp UI/BackgroundAudio.h diff --git a/Core/MIPS/IR/IRJit.cpp b/Core/MIPS/IR/IRJit.cpp index 633b3282f8..a11b882630 100644 --- a/Core/MIPS/IR/IRJit.cpp +++ b/Core/MIPS/IR/IRJit.cpp @@ -461,6 +461,9 @@ JitBlockDebugInfo IRBlockCache::GetBlockDebugInfo(int blockNum) const { uint32_t start, size; ir.GetRange(&start, &size); debugInfo.originalAddress = start; // TODO + if (!Memory::IsValid4AlignedAddress(start)) { + return debugInfo; + } debugInfo.origDisasm.reserve(((start + size) - start) / 4); for (u32 addr = start; addr < start + size; addr += 4) { diff --git a/UI/ImDebugger/ImConsole.h b/UI/ImDebugger/ImConsole.h index b6f93fde7d..5a3e80f58d 100644 --- a/UI/ImDebugger/ImConsole.h +++ b/UI/ImDebugger/ImConsole.h @@ -5,6 +5,8 @@ #include "ext/imgui/imgui.h" +struct ImConfig; + // Adapted from the ImGui demo. class ImConsole { public: diff --git a/UI/ImDebugger/ImDebugger.cpp b/UI/ImDebugger/ImDebugger.cpp index 3d84a4b622..ebe6c93a2a 100644 --- a/UI/ImDebugger/ImDebugger.cpp +++ b/UI/ImDebugger/ImDebugger.cpp @@ -2395,6 +2395,7 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug, GPUDebugInterface *gpuDebu ImGui::MenuItem("Callstacks", nullptr, &cfg_.callstackOpen); ImGui::MenuItem("Breakpoints", nullptr, &cfg_.breakpointsOpen); ImGui::MenuItem("Watch", nullptr, &cfg_.watchOpen); + ImGui::MenuItem("JIT viewer", nullptr, &cfg_.jitViewerOpen); ImGui::EndMenu(); } if (ImGui::BeginMenu("Symbols")) { @@ -2621,6 +2622,10 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug, GPUDebugInterface *gpuDebu logWindow_.Draw(cfg_); } + if (cfg_.jitViewerOpen) { + jitViewer_.Draw(cfg_, control); + } + if (cfg_.displayOpen) { DrawDisplayWindow(cfg_, gpuDebug->GetFramebufferManagerCommon()); } @@ -2845,6 +2850,7 @@ void ImConfig::SyncConfig(IniFile *ini, bool save) { sync.Sync("watchOpen", &watchOpen, false); sync.Sync("paramSFOOpen", ¶mSFOOpen, false); sync.Sync("atracToolOpen", &atracToolOpen, false); + sync.Sync("jitViewerOpen", &jitViewerOpen, false); for (int i = 0; i < 4; i++) { char name[64]; snprintf(name, sizeof(name), "memory%dOpen", i + 1); diff --git a/UI/ImDebugger/ImDebugger.h b/UI/ImDebugger/ImDebugger.h index a609afcba5..651c59c188 100644 --- a/UI/ImDebugger/ImDebugger.h +++ b/UI/ImDebugger/ImDebugger.h @@ -20,6 +20,7 @@ #include "UI/ImDebugger/ImDisasmView.h" #include "UI/ImDebugger/ImMemView.h" #include "UI/ImDebugger/ImStructViewer.h" +#include "UI/ImDebugger/ImJitViewer.h" #include "UI/ImDebugger/ImGe.h" #include "UI/ImDebugger/ImConsole.h" @@ -90,6 +91,7 @@ struct ImConfig { bool luaConsoleOpen; bool audioOutOpen; bool paramSFOOpen; + bool jitViewerOpen; // HLE explorer settings // bool filterByUsed = true; @@ -216,6 +218,7 @@ private: ImAtracToolWindow atracToolWindow_; ImConsole luaConsole_; ImLogWindow logWindow_; + ImJitViewerWindow jitViewer_; ImSnapshotState newSnapshot_; ImSnapshotState snapshot_; diff --git a/UI/ImDebugger/ImJitViewer.cpp b/UI/ImDebugger/ImJitViewer.cpp new file mode 100644 index 0000000000..ceb3f984a9 --- /dev/null +++ b/UI/ImDebugger/ImJitViewer.cpp @@ -0,0 +1,210 @@ +#include "ppsspp_config.h" + +#include "UI/ImDebugger/ImJitViewer.h" +#include "UI/ImDebugger/ImDebugger.h" +#include "Core/Config.h" + +#include "Core/MIPS/JitCommon/JitBlockCache.h" +#include "Core/MIPS/JitCommon/JitCommon.h" +#include "Core/MIPS/JitCommon/JitState.h" + +void ImJitViewerWindow::GoToBlockAtAddr(u32 addr) { + for (auto &block : blockList_) { + if (addr >= block.addr && addr < block.addr + (u32)block.sizeInBytes) { + curBlockNum_ = block.blockNum; + break; + } + } +} + +void ImJitViewerWindow::Draw(ImConfig &cfg, ImControl &control) { + ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver); + if (!ImGui::Begin("JitViewer", &cfg.jitViewerOpen)) { + ImGui::End(); + return; + } + + const CPUCore core = (CPUCore)g_Config.iCpuCore; + + if (core == CPUCore::INTERPRETER) { + ImGui::TextUnformatted("JIT Viewer is only available with JIT or IR-based CPU cores."); + ImGui::End(); + return; + } + + // Options menu + if (ImGui::BeginPopup("Options")) { + // ImGui::Checkbox("Auto-scroll", &AutoScroll); + ImGui::EndPopup(); + } + +#if PPSSPP_ARCH(AMD64) + const char *TARGET = "X86-64"; +#elif PPSSPP_ARCH(X86) + const char *TARGET = "X86"; +#elif PPSSPP_ARCH(ARM64) + + const char *TARGET = "ARM64"; +#elif PPSSPP_ARCH(ARM) + const char *TARGET = "ARM32"; +#else + const char *TARGET = "TARGET"; +#endif + + // Three or four columns: One table for blocks that can be sorted in various ways, + // then the remaining are either MIPS/IR/TARGET or MIPS/TARGET or MIPS/IR depending on the JIT type. + int numColumns = 3; + if (core == CPUCore::JIT_IR) { + numColumns = 4; + } + + JitBlockCacheDebugInterface *blockCacheDebug = MIPSComp::jit->GetBlockCacheDebugInterface(); + + if (core_ != (int)core) { + // Core changed, need to refresh. + refresh_ = true; + core_ = (int)core; + } + + if (sortSpecs_ && sortSpecs_->SpecsDirty) { + refresh_ = true; + sortSpecs_->SpecsDirty = false; + } + + // If the CPU moved, and we're not currently running, update the cached blocklist. + const CoreState coreStateCached = coreState; + if (coreStateCached == CORE_STEPPING_CPU || coreStateCached == CORE_NEXTFRAME || refresh_) { + const int cpuStepCount = Core_GetSteppingCounter(); + if (lastCpuStepCount_ != cpuStepCount || refresh_) { + refresh_ = false; + lastCpuStepCount_ = cpuStepCount; + blockList_.clear(); + curBlockNum_ = -1; + const int blockCount = blockCacheDebug->GetNumBlocks(); + blockList_.reserve(blockCount); + INFO_LOG(Log::Debugger, "Updating JIT block list... %d blocks", blockCacheDebug->GetNumBlocks()); + for (int i = 0; i < blockCount; i++) { + if (!blockCacheDebug->IsValidBlock(i)) { + continue; + } + JitBlockMeta meta = blockCacheDebug->GetBlockMeta(i); + if (!meta.valid) { + continue; + } + CachedBlock cb; + cb.addr = meta.addr; + cb.sizeInBytes = meta.sizeInBytes; + cb.blockNum = i; + blockList_.push_back(cb); + } + + std::sort(blockList_.begin(), blockList_.end(), [this](const CachedBlock &a, const CachedBlock &b) { + if (!sortSpecs_ || sortSpecs_->SpecsCount <= 0) { + return a.addr < b.addr; + } + const ImGuiTableColumnSortSpecs *spec = &sortSpecs_->Specs[0]; + int delta = 0; + if (spec->ColumnUserID == 0) { + delta = (int)a.blockNum - (int)b.blockNum; + } else + if (spec->ColumnUserID == 1) { + delta = (int)a.addr - (int)b.addr; + } else if (spec->ColumnUserID == 2) { + delta = a.sizeInBytes - b.sizeInBytes; + } + if (delta == 0) { + return a.addr < b.addr; + } + if (spec->SortDirection == ImGuiSortDirection_Ascending) { + return delta < 0; + } else { + return delta > 0; + } + }); + } + } else { + ImGui::Text("Pause to update block list"); + } + + if (ImGui::BeginTable("columns", numColumns, ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersH | ImGuiTableFlags_Resizable)) { + ImGui::TableSetupColumn("Blocks"); + ImGui::TableSetupColumn("MIPS"); + if (core == CPUCore::JIT_IR || core == CPUCore::IR_INTERPRETER) { + ImGui::TableSetupColumn("IR"); + } + if (core == CPUCore::JIT_IR || core == CPUCore::JIT) { + ImGui::TableSetupColumn(TARGET); + } + + ImGui::TableHeadersRow(); + + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + + // For separate scrolling. + ImGui::BeginChild("LeftPane", ImVec2(0, 0), true, ImGuiWindowFlags_HorizontalScrollbar); + + if (ImGui::BeginTable("blocks", 3, ImGuiTableFlags_Sortable | ImGuiTableFlags_Resizable)) { + ImGui::TableSetupColumn("Num", 0, 0, 0); + ImGui::TableSetupColumn("Addr", 0, 0, 1); + ImGui::TableSetupColumn("Size", 0, 0, 2); + ImGui::TableHeadersRow(); + + sortSpecs_ = ImGui::TableGetSortSpecs(); + + for (int i = 0; i < (int)blockList_.size(); i++) { + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + char label[32]; + snprintf(label, sizeof(label), "%d", blockList_[i].blockNum); + if (ImGui::Selectable(label, blockList_[i].blockNum == curBlockNum_, ImGuiSelectableFlags_SpanAllColumns)) { + curBlockNum_ = blockList_[i].blockNum; + if (curBlockNum_ >= 0 && curBlockNum_ < blockCacheDebug->GetNumBlocks()) { + debugInfo_ = blockCacheDebug->GetBlockDebugInfo(curBlockNum_); + } + } + ImGui::TableNextColumn(); + ImGui::Text("%08x", blockList_[i].addr); + ImGui::TableNextColumn(); + ImGui::Text("%d", blockList_[i].sizeInBytes); + } + ImGui::EndTable(); + } + + ImGui::EndChild(); + + ImGui::TableNextColumn(); + + ImGui::BeginChild("MIPSPane", ImVec2(0, 0), true, ImGuiWindowFlags_HorizontalScrollbar); + + for (const auto &line : debugInfo_.origDisasm) { + ImGui::TextUnformatted(line.c_str()); + } + ImGui::EndChild(); + + if (core == CPUCore::JIT_IR || core == CPUCore::IR_INTERPRETER) { + ImGui::TableNextColumn(); + + ImGui::BeginChild("IRPane", ImVec2(0, 0), true, ImGuiWindowFlags_HorizontalScrollbar); + // TODO : When we have both target and IR, need a third column. + for (const auto &line : debugInfo_.irDisasm) { + ImGui::TextUnformatted(line.c_str()); + } + ImGui::EndChild(); + } + + if (core == CPUCore::JIT_IR || core == CPUCore::JIT) { + ImGui::TableNextColumn(); + + ImGui::BeginChild("TargetPane", ImVec2(0, 0), true, ImGuiWindowFlags_HorizontalScrollbar); + for (const auto &line : debugInfo_.targetDisasm) { + ImGui::TextUnformatted(line.c_str()); + } + ImGui::EndChild(); + } + + ImGui::EndTable(); + } + + ImGui::End(); +} diff --git a/UI/ImDebugger/ImJitViewer.h b/UI/ImDebugger/ImJitViewer.h new file mode 100644 index 0000000000..87a379c21a --- /dev/null +++ b/UI/ImDebugger/ImJitViewer.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include "ext/imgui/imgui.h" + +#include "Core/Debugger/DebugInterface.h" +#include "Core/MIPS/JitCommon/JitBlockCache.h" + +struct ImConfig; +struct ImControl; + +class ImJitViewerWindow { +public: + void Draw(ImConfig &cfg, ImControl &control); + const char *Title() const { + return "JIT Viewer"; + } + + void GoToBlockAtAddr(u32 addr); + +private: + struct CachedBlock { + u32 addr; + int sizeInBytes; + int blockNum; + }; + std::vector blockList_; + int curBlockNum_ = -1; + + int lastCpuStepCount_ = -1; + int blockSortColumn_ = 0; + + bool refresh_ = false; + int core_ = -1; + + ImGuiTableSortSpecs *sortSpecs_ = nullptr; + JitBlockDebugInfo debugInfo_; +}; diff --git a/UI/UI.vcxproj b/UI/UI.vcxproj index 9f1cd0b02c..42ad2df168 100644 --- a/UI/UI.vcxproj +++ b/UI/UI.vcxproj @@ -49,6 +49,7 @@ + @@ -99,6 +100,7 @@ + diff --git a/UI/UI.vcxproj.filters b/UI/UI.vcxproj.filters index 1a2c87e1f4..3d8693dd36 100644 --- a/UI/UI.vcxproj.filters +++ b/UI/UI.vcxproj.filters @@ -126,6 +126,9 @@ Screens + + ImDebugger + @@ -252,6 +255,9 @@ Screens + + ImDebugger + diff --git a/UWP/UI_UWP/UI_UWP.vcxproj b/UWP/UI_UWP/UI_UWP.vcxproj index ca7fdc827a..4d14b3fa83 100644 --- a/UWP/UI_UWP/UI_UWP.vcxproj +++ b/UWP/UI_UWP/UI_UWP.vcxproj @@ -107,6 +107,7 @@ + @@ -157,6 +158,7 @@ + diff --git a/UWP/UI_UWP/UI_UWP.vcxproj.filters b/UWP/UI_UWP/UI_UWP.vcxproj.filters index 423e806d2c..f064528ae2 100644 --- a/UWP/UI_UWP/UI_UWP.vcxproj.filters +++ b/UWP/UI_UWP/UI_UWP.vcxproj.filters @@ -3,43 +3,15 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - ImDebugger @@ -58,50 +30,113 @@ ImDebugger - - + + ImDebugger + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + - - - - - - - - - - - - - - - - - - - - - - - - - - - - ImDebugger @@ -120,13 +155,107 @@ ImDebugger - - + + ImDebugger + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + + + Screens + {4013cb89-6145-451c-9cb1-d63d01f66bd5} + + {38b8413d-438d-4197-9db8-7bd123fc7431} + \ No newline at end of file diff --git a/android/jni/Android.mk b/android/jni/Android.mk index c828ef1469..c74c4c410b 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -895,6 +895,7 @@ LOCAL_SRC_FILES := \ $(SRC)/UI/ImDebugger/ImConsole.cpp \ $(SRC)/UI/ImDebugger/ImDisasmView.cpp \ $(SRC)/UI/ImDebugger/ImMemView.cpp \ + $(SRC)/UI/ImDebugger/ImJitViewer.cpp \ $(SRC)/UI/ImDebugger/ImStructViewer.cpp \ $(SRC)/UI/AudioCommon.cpp \ $(SRC)/UI/BackgroundAudio.cpp \