Skip to content

Commit

Permalink
Merge pull request #19656 from hrydgard/misc-debugger
Browse files Browse the repository at this point in the history
ImDebugger: Add a basic kernel object list
  • Loading branch information
hrydgard authored Nov 26, 2024
2 parents 2352258 + 1eadea2 commit a265119
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 52 deletions.
2 changes: 2 additions & 0 deletions Common/Input/KeyCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ enum InputKeyCode {
NKCODE_EXT_MOUSEWHEEL_UP = 1008,
NKCODE_EXT_MOUSEWHEEL_DOWN = 1009,

NKCODE_EXT_PRINTSCREEN = 1010,

NKCODE_MAX
};

Expand Down
2 changes: 1 addition & 1 deletion Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ void Core_Break(const char *reason, u32 relatedAddress) {
// Stop the tracer
{
std::lock_guard<std::mutex> lock(g_stepMutex);
if (!g_stepCommand.empty()) {
if (!g_stepCommand.empty() && Core_IsStepping()) {
// If we're in a failed step that uses a temp breakpoint, we need to be able to override it here.
switch (g_stepCommand.type) {
case CPUStepType::Over:
Expand Down
3 changes: 2 additions & 1 deletion Core/FileSystems/BlobFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class BlobFileSystem : public IFileSystem {
bool OwnsHandle(u32 handle) override;
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
PSPDevType DevType(u32 handle) override;
FileSystemFlags Flags() override { return FileSystemFlags::FLASH; }
FileSystemFlags Flags() const override { return FileSystemFlags::FLASH; }

bool MkDir(const std::string &dirname) override;
bool RmDir(const std::string &dirname) override;
Expand All @@ -55,6 +55,7 @@ class BlobFileSystem : public IFileSystem {
u64 FreeDiskSpace(const std::string &path) override;

bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
void Describe(char *buf, size_t size) const override { snprintf(buf, size, "%s", "Blob"); }

private:
// File positions.
Expand Down
6 changes: 4 additions & 2 deletions Core/FileSystems/DirectoryFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ class DirectoryFileSystem : public IFileSystem {
bool RmDir(const std::string &dirname) override;
int RenameFile(const std::string &from, const std::string &to) override;
bool RemoveFile(const std::string &filename) override;
FileSystemFlags Flags() override { return flags; }
FileSystemFlags Flags() const override { return flags; }
u64 FreeDiskSpace(const std::string &path) override;

bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override;
void Describe(char *buf, size_t size) const override { snprintf(buf, size, "Dir: %s", basePath.c_str()); }

private:
struct OpenFileEntry {
Expand Down Expand Up @@ -128,10 +129,11 @@ class VFSFileSystem : public IFileSystem {
bool RmDir(const std::string &dirname) override;
int RenameFile(const std::string &from, const std::string &to) override;
bool RemoveFile(const std::string &filename) override;
FileSystemFlags Flags() override { return FileSystemFlags::FLASH; }
FileSystemFlags Flags() const override { return FileSystemFlags::FLASH; }
u64 FreeDiskSpace(const std::string &path) override { return 0; }

bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
void Describe(char *buf, size_t size) const override { snprintf(buf, size, "VFS: %s", basePath.c_str()); }

private:
struct OpenFileEntry {
Expand Down
6 changes: 4 additions & 2 deletions Core/FileSystems/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ class IFileSystem {
virtual bool RemoveFile(const std::string &filename) = 0;
virtual int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) = 0;
virtual PSPDevType DevType(u32 handle) = 0;
virtual FileSystemFlags Flags() = 0;
virtual FileSystemFlags Flags() const = 0;
virtual u64 FreeDiskSpace(const std::string &path) = 0;
virtual bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) = 0;
virtual void Describe(char *buf, size_t size) const = 0;
};


Expand Down Expand Up @@ -174,9 +175,10 @@ class EmptyFileSystem : public IFileSystem {
bool RemoveFile(const std::string &filename) override {return false;}
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override { return SCE_KERNEL_ERROR_ERRNO_FUNCTION_NOT_SUPPORTED; }
PSPDevType DevType(u32 handle) override { return PSPDevType::INVALID; }
FileSystemFlags Flags() override { return FileSystemFlags::NONE; }
FileSystemFlags Flags() const override { return FileSystemFlags::NONE; }
u64 FreeDiskSpace(const std::string &path) override { return 0; }
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
void Describe(char *buf, size_t size) const override { snprintf(buf, size, "%s", "Empty"); }
};


2 changes: 1 addition & 1 deletion Core/FileSystems/ISOFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ PSPDevType ISOFileSystem::DevType(u32 handle) {
return type;
}

FileSystemFlags ISOFileSystem::Flags() {
FileSystemFlags ISOFileSystem::Flags() const {
// TODO: Here may be a good place to force things, in case users recompress games
// as PBP or CSO when they were originally the other type.
return blockDevice->IsDisc() ? FileSystemFlags::UMD : FileSystemFlags::CARD;
Expand Down
7 changes: 5 additions & 2 deletions Core/FileSystems/ISOFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ISOFileSystem : public IFileSystem {
bool OwnsHandle(u32 handle) override;
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
PSPDevType DevType(u32 handle) override;
FileSystemFlags Flags() override;
FileSystemFlags Flags() const override;
u64 FreeDiskSpace(const std::string &path) override { return 0; }

size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;
Expand All @@ -55,6 +55,7 @@ class ISOFileSystem : public IFileSystem {
bool RemoveFile(const std::string &filename) override { return false; }

bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
void Describe(char *buf, size_t size) const override { snprintf(buf, size, "ISO"); } // TODO: Ask the fileLoader about the origins

private:
struct TreeEntry {
Expand Down Expand Up @@ -145,7 +146,7 @@ class ISOBlockSystem : public IFileSystem {
PSPDevType DevType(u32 handle) override {
return isoFileSystem_->DevType(handle);
}
FileSystemFlags Flags() override { return isoFileSystem_->Flags(); }
FileSystemFlags Flags() const override { return isoFileSystem_->Flags(); }
u64 FreeDiskSpace(const std::string &path) override { return isoFileSystem_->FreeDiskSpace(path); }

size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override {
Expand All @@ -161,6 +162,8 @@ class ISOBlockSystem : public IFileSystem {

bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }

void Describe(char *buf, size_t size) const override { snprintf(buf, size, "ISOBlock"); }

private:
std::shared_ptr<IFileSystem> isoFileSystem_;
};
4 changes: 3 additions & 1 deletion Core/FileSystems/MetaFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class MetaFileSystem : public IHandleAllocator, public IFileSystem {
bool RemoveFile(const std::string &filename) override;
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
PSPDevType DevType(u32 handle) override;
FileSystemFlags Flags() override { return FileSystemFlags::NONE; }
FileSystemFlags Flags() const override { return FileSystemFlags::NONE; }
u64 FreeDiskSpace(const std::string &path) override;

// Convenience helper - returns < 0 on failure.
Expand All @@ -155,6 +155,8 @@ class MetaFileSystem : public IHandleAllocator, public IFileSystem {
}
}

void Describe(char *buf, size_t size) const override { snprintf(buf, size, "Meta"); }

private:
int64_t RecursiveSize(const std::string &dirPath);
};
4 changes: 3 additions & 1 deletion Core/FileSystems/VirtualDiscFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class VirtualDiscFileSystem: public IFileSystem {
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
PSPDevType DevType(u32 handle) override;
std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;
FileSystemFlags Flags() override { return FileSystemFlags::UMD; }
FileSystemFlags Flags() const override { return FileSystemFlags::UMD; }
u64 FreeDiskSpace(const std::string &path) override { return 0; }

// unsupported operations
Expand All @@ -56,6 +56,8 @@ class VirtualDiscFileSystem: public IFileSystem {

bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }

void Describe(char *buf, size_t size) const override { snprintf(buf, size, "VirtualDisc: %s", basePath.ToVisualString().c_str()); } // TODO: Ask the fileLoader about the origins

private:
void LoadFileListIndex();
// Warning: modifies input string.
Expand Down
8 changes: 0 additions & 8 deletions Core/HLE/sceKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,6 @@ SceUID KernelObjectPool::Create(KernelObject *obj, int rangeBottom, int rangeTop
return 0;
}

bool KernelObjectPool::IsValid(SceUID handle) const {
int index = handle - handleOffset;
if (index < 0 || index >= maxCount)
return false;
else
return occupied[index];
}

void KernelObjectPool::Clear() {
for (int i = 0; i < maxCount; i++) {
// brutally clear everything, no validation
Expand Down
10 changes: 8 additions & 2 deletions Core/HLE/sceKernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,13 @@ class KernelObjectPool {
return error;
};

bool IsValid(SceUID handle) const;
bool IsValid(SceUID handle) const {
int index = handle - handleOffset;
if (index < 0 || index >= maxCount)
return false;
else
return occupied[index];
}

template <class T>
T* Get(SceUID handle, u32 &outError) {
Expand Down Expand Up @@ -530,12 +536,12 @@ class KernelObjectPool {
void Clear();
int GetCount() const;

private:
enum {
maxCount = 4096,
handleOffset = 0x100,
initialNextID = 0x10
};
private:
KernelObject *pool[maxCount];
bool occupied[maxCount];
int nextID;
Expand Down
92 changes: 80 additions & 12 deletions UI/ImDebugger/ImDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void DrawRegisterView(MIPSDebugInterface *mipsDebug, bool *open) {

auto gprLine = [&](const char *regname, int value) {
ImGui::TableSetColumnIndex(0);
ImGui::Text("%s", regname);
ImGui::TextUnformatted(regname);
ImGui::TableSetColumnIndex(1);
ImGui::Text("%08x", value);
if (value >= -1000000 && value <= 1000000) {
Expand Down Expand Up @@ -84,7 +84,7 @@ void DrawRegisterView(MIPSDebugInterface *mipsDebug, bool *open) {
u32 fivalue;
memcpy(&fivalue, &fvalue, sizeof(fivalue));
ImGui::TableSetColumnIndex(0);
ImGui::Text("%s", mipsDebug->GetRegName(1, i).c_str());
ImGui::TextUnformatted(mipsDebug->GetRegName(1, i).c_str());
ImGui::TableSetColumnIndex(1);
ImGui::Text("%0.7f", fvalue);
ImGui::TableSetColumnIndex(2);
Expand Down Expand Up @@ -154,10 +154,20 @@ void DrawThreadView(ImConfig &cfg) {
ImGui::TableNextColumn();
ImGui::Text("%d", thread.priority);
ImGui::TableNextColumn();
ImGui::Text("%s", ThreadStatusToString(thread.status));
ImGui::TextUnformatted(ThreadStatusToString(thread.status));
if (ImGui::BeginPopup("threadPopup")) {
DebugThreadInfo &thread = info[i];
ImGui::Text("Thread: %s", thread.name);
if (ImGui::MenuItem("Copy entry to clipboard")) {
char temp[64];
snprintf(temp, sizeof(temp), "%08x", thread.entrypoint);
System_CopyStringToClipboard(temp);
}
if (ImGui::MenuItem("Copy PC to clipboard")) {
char temp[64];
snprintf(temp, sizeof(temp), "%08x", thread.curPC);
System_CopyStringToClipboard(temp);
}
if (ImGui::MenuItem("Kill thread")) {
sceKernelTerminateThread(thread.id);
}
Expand Down Expand Up @@ -198,7 +208,11 @@ static void DrawFilesystemBrowser(ImConfig &cfg) {

for (auto &fs : pspFileSystem.GetMounts()) {
std::string path;
if (ImGui::TreeNode(fs.prefix.c_str())) {
char desc[256];
fs.system->Describe(desc, sizeof(desc));
char fsTitle[256];
snprintf(fsTitle, sizeof(fsTitle), "%s - %s", fs.prefix.c_str(), desc);
if (ImGui::TreeNode(fsTitle)) {
auto system = fs.system;
RecurseFileSystem(system.get(), path);
ImGui::TreePop();
Expand All @@ -208,6 +222,55 @@ static void DrawFilesystemBrowser(ImConfig &cfg) {
ImGui::End();
}

static void DrawKernelObjects(ImConfig &cfg) {
if (!ImGui::Begin("Kernel Objects", &cfg.filesystemBrowserOpen)) {
ImGui::End();
return;
}
if (ImGui::BeginTable("kos", 5, ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersH)) {
ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Summary", ImGuiTableColumnFlags_WidthStretch);

ImGui::TableHeadersRow();

for (int i = 0; i < (int)KernelObjectPool::maxCount; i++) {
int id = i + KernelObjectPool::handleOffset;
if (!kernelObjects.IsValid(id)) {
continue;
}
KernelObject *obj = kernelObjects.GetFast<KernelObject>(id);
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::PushID(i);
if (ImGui::Selectable("", cfg.selectedThread == i, ImGuiSelectableFlags_AllowDoubleClick | ImGuiSelectableFlags_SpanAllColumns)) {
cfg.selectedThread = i;
}
ImGui::SameLine();
/*
if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
cfg.selectedThread = i;
ImGui::OpenPopup("kernelObjectPopup");
}
*/
ImGui::Text("%04x", id);
ImGui::TableNextColumn();
ImGui::TextUnformatted(obj->GetTypeName());
ImGui::TableNextColumn();
ImGui::TextUnformatted(obj->GetName());
ImGui::TableNextColumn();
char qi[128];
obj->GetQuickInfo(qi, sizeof(qi));
ImGui::TextUnformatted(qi);
ImGui::PopID();
}

ImGui::EndTable();
}
ImGui::End();
}

static const char *MemCheckConditionToString(MemCheckCondition cond) {
switch (cond) {
case MEMCHECK_READ: return "Read";
Expand Down Expand Up @@ -293,7 +356,7 @@ static void DrawBreakpointsView(MIPSDebugInterface *mipsDebug, ImConfig &cfg) {
ImGui::TextUnformatted("-"); // opcode
ImGui::TableNextColumn();
if (bp.hasCond) {
ImGui::Text("%s", bp.cond.expressionString.c_str());
ImGui::TextUnformatted(bp.cond.expressionString.c_str());
} else {
ImGui::TextUnformatted("-"); // condition
}
Expand All @@ -315,7 +378,7 @@ static void DrawBreakpointsView(MIPSDebugInterface *mipsDebug, ImConfig &cfg) {
ImGui::SameLine();
ImGui::CheckboxFlags("", (int *)&mc.result, BREAK_ACTION_PAUSE);
ImGui::TableNextColumn();
ImGui::Text("%s", MemCheckConditionToString(mc.cond));
ImGui::TextUnformatted(MemCheckConditionToString(mc.cond));
ImGui::TableNextColumn();
ImGui::Text("%08x", mc.start);
ImGui::TableNextColumn();
Expand Down Expand Up @@ -443,13 +506,13 @@ void DrawCallStacks(MIPSDebugInterface *debug, bool *open) {
const std::string entrySym = g_symbolMap->GetLabelString(frame.entry);

ImGui::TableSetColumnIndex(0);
ImGui::Text("%s", entrySym.c_str());
ImGui::TextUnformatted(entrySym.c_str());
ImGui::TableSetColumnIndex(1);
ImGui::Text("%08x", frame.entry);
ImGui::TableSetColumnIndex(2);
ImGui::Text("%08x", frame.pc);
ImGui::TableSetColumnIndex(3);
ImGui::Text("%s", "N/A"); // opcode, see the old debugger
ImGui::TextUnformatted("N/A"); // opcode, see the old debugger
ImGui::TableSetColumnIndex(4);
ImGui::Text("%08x", frame.sp);
ImGui::TableSetColumnIndex(5);
Expand Down Expand Up @@ -491,7 +554,7 @@ void DrawModules(MIPSDebugInterface *debug, ImConfig &cfg) {
ImGui::TableNextColumn();
ImGui::Text("%08x", module.size);
ImGui::TableNextColumn();
ImGui::Text("%s", module.active ? "yes" : "no");
ImGui::TextUnformatted(module.active ? "yes" : "no");
}

ImGui::EndTable();
Expand Down Expand Up @@ -614,8 +677,9 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug, GPUDebugInterface *gpuDebu
}
if (ImGui::BeginMenu("OS HLE")) {
ImGui::MenuItem("File System Browser", nullptr, &cfg_.filesystemBrowserOpen);
ImGui::MenuItem("HLE Threads", nullptr, &cfg_.threadsOpen);
ImGui::MenuItem("HLE Modules",nullptr, &cfg_.modulesOpen);
ImGui::MenuItem("Kernel Objects", nullptr, &cfg_.kernelObjectsOpen);
ImGui::MenuItem("Threads", nullptr, &cfg_.threadsOpen);
ImGui::MenuItem("Modules",nullptr, &cfg_.modulesOpen);
ImGui::MenuItem("sceAtrac", nullptr, &cfg_.atracOpen);
ImGui::EndMenu();
}
Expand Down Expand Up @@ -663,6 +727,10 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug, GPUDebugInterface *gpuDebu
DrawFilesystemBrowser(cfg_);
}

if (cfg_.kernelObjectsOpen) {
DrawKernelObjects(cfg_);
}

if (cfg_.threadsOpen) {
DrawThreadView(cfg_);
}
Expand Down Expand Up @@ -835,7 +903,7 @@ void ImDisasmWindow::Draw(MIPSDebugInterface *mipsDebug, bool *open, CoreState c
disasmView_.Draw(ImGui::GetWindowDrawList());
ImGui::EndTable();

ImGui::Text("%s", disasmView_.StatusBarText().c_str());
ImGui::TextUnformatted(disasmView_.StatusBarText().c_str());
}
ImGui::End();
}
Loading

0 comments on commit a265119

Please sign in to comment.