Files
ppsspp/Core/Debugger/LineInfo.h
T
Henrik RydgårdandClaude Opus 5 c5e7c4890a Debugger: decode DWARF line info, and show source locations
Homebrew commonly ships its unstripped ELF next to the EBOOT, which is already
how the symbol loader turns z_un_08841f98 into a function name. That same ELF
carries a DWARF .debug_line section, so the addresses can be mapped to source
files and lines too - and a backtrace stops being four hex numbers:

  08841f98  move sp,fp          mesh.zig:163
  0883afa4  li v0,0x0           MenuState.zig:821
  088260d8  andi at,v0,0xFFFF   State.zig:40
  0882a27c  andi at,v0,0xFFFF   engine.zig:468

Surfaced in three places: per frame in hle.backtrace, in the "hit" object that
cpu.breakpoint.hit and cpu.stepping share, and appended to the disassembly
window's status bar. The breakpoint case keys on the pc rather than the address,
since for a memory breakpoint the useful source location is the instruction that
did the access, not the data it touched.

Storage is a plain sorted table of absolute addresses per module. SymbolMap
keeps module-relative addresses because its .ppsym files are meant to be
reloaded by a different game that pulls in the same module; none of this is ever
written anywhere - it's regenerated from the ELF each boot - so there'd be
nothing for relative addresses to buy. Each module owns its own rows and file
names outright and is keyed the way SymbolMap::UnloadModule is, so unloading one
module drops its lines and nobody else's.

The subtle part is end-of-sequence markers. Without them a lookup for an address
in a gap - a compilation unit built without debug info - confidently reports the
last line of an unrelated file. A prototype run over one test binary
mis-attributed 70 of its 349 functions that way, so sequence ends are recorded
as rows with line 0 and a lookup landing on one reports nothing instead.

DWARF 2 through 4 are decoded (psp-gcc emits 2, Zig 4). Version 5 re-encoded the
file table, so those units are skipped with a warning rather than mis-parsed -
nothing targeting the PSP produces it today.

Scope, since it's narrower than it sounds: PRX conversion strips every .debug
section. I checked all 437 pspautotests .prx and CrossCraft's own app.prx -
none have any. Of 24 installed homebrew EBOOTs, zero carry debug info; CrossCraft
only does because it ships app.elf separately. So this helps someone developing
homebrew, and does nothing at all for a commercial game.

Costs about 1.2 MB for a large Zig binary (98383 rows, 438 files) and nothing
for anything without debug info. Follows bAutoSaveLoadSymbols like the symbols
do.

pspautotests 314/314, UnitTest 55/55, CoreUWP builds.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GZq8ZtJmFY7bkX5FVkr3P9
2026-08-18 13:42:14 +02:00

82 lines
3.2 KiB
C++

// Copyright (c) 2026- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#pragma once
#include <string>
#include <string_view>
#include <vector>
#include "Common/CommonTypes.h"
// Source line information decoded from an ELF's DWARF .debug_line section.
//
// Only available where an unstripped ELF is: PRX conversion drops every .debug section, so this
// never applies to a commercial game, and in practice it means homebrew that ships its app.elf
// next to the EBOOT (the same thing the companion symbol loader relies on).
//
// Addresses are stored absolute, relocated to wherever the module was loaded. Unlike SymbolMap,
// which keeps module-relative addresses so a saved .ppsym can be reloaded by a different game that
// pulls in the same module, none of this is ever written anywhere - it's regenerated from the ELF
// on every boot - so there'd be nothing for relative addresses to buy.
struct LineEntry {
u32 address;
// 0 marks the end of a sequence: the address is one past the last instruction the preceding
// rows describe. Without these, a lookup for an address in a gap (a compilation unit built
// without debug info, say) silently reports the last line of an unrelated source file - it
// mis-attributed 70 of 349 functions in one test binary before they were recorded.
u32 line;
u32 fileIndex;
};
class LineInfoMap {
public:
// Parses .debug_line out of an unstripped ELF image and relocates it to moduleStart.
// Returns the number of rows kept, or 0 if the ELF has nothing usable. Replaces whatever was
// held for the same module.
int AddModule(std::string_view elfData, u32 moduleStart, u32 moduleSize);
// Keyed the same way SymbolMap::UnloadModule is, so that unloading one module drops only its
// own lines. Each module owns its rows and its file names outright - there's no shared table
// for an unload to have to pick apart.
void RemoveModule(u32 moduleStart, u32 moduleSize);
void Clear();
bool IsEmpty() const { return modules_.empty(); }
// The source location of the instruction at this address. False when no loaded module owns the
// address, or when it falls in a gap between sequences.
bool Lookup(u32 address, std::string *file, int *line) const;
// "file.c:123", or empty if unknown. For status bars and log lines.
std::string LookupString(u32 address) const;
private:
struct ModuleLines {
u32 start = 0;
u32 size = 0;
std::vector<std::string> files;
std::vector<LineEntry> entries; // Sorted by address.
};
const ModuleLines *FindModule(u32 address) const;
std::vector<ModuleLines> modules_;
};
extern LineInfoMap g_lineInfo;