BizHawk icon indicating copy to clipboard operation
BizHawk copied to clipboard

Add indentation to tracelogs

Open vadosnaprimer opened this issue 1 year ago • 3 comments

FCEUX and latest Gens have this, and it's super useful for quickly determining subroutines (and skipping over them as needed). A core can determine indentation based on stack size, and pass the number to the tracer, which would then pad each line by that size.

Example code for gpgx (to show how it'd look):

protected override void TraceFromCallback(uint addr, uint value, uint flags)
{
	var regs = DebuggableCore.GetCpuFlagsAndRegisters();
	uint pc = (uint)regs["M68K PC"].Value & 0xFFFFFF;
	var disasm = Disassembler.Disassemble(MemoryDomains.SystemBus, pc, out _);

	var stackBase = MemoryDomains.SystemBus.PeekUint(0, true);
	var stackPointer = regs["M68K A7"].Value;
	var stackSize = (int)((stackBase - stackPointer) / 4) & 63;

	var sb = new StringBuilder();

	foreach (var r in regs)
	{
		if (r.Key.StartsWith("M68K")) // drop Z80 regs until it has its own debugger/tracer
		{
			if (r.Key != "M68K SP" && r.Key != "M68K ISP" && // copies of a7
				r.Key != "M68K PC" && // already present in every line start
				r.Key != "M68K IR") // copy of last opcode, already shown in raw bytes
			{
				sb.Append($"{r.Key.Replace("M68K", "").Trim()}:{r.Value.Value.ToHexString(r.Value.BitSize / 4)} ");
			}
		}
	}

	var sr = regs["M68K SR"].Value;
	sb.Append(string.Concat(
		(sr & 16) > 0 ? "X" : "x",
		(sr &  8) > 0 ? "N" : "n",
		(sr &  4) > 0 ? "Z" : "z",
		(sr &  2) > 0 ? "V" : "v",
		(sr &  1) > 0 ? "C" : "c"));

	this.Put(new(disassembly: $"{new string(' ', stackSize)}{pc:X6}:  {disasm}".PadRight(50+stackSize), registerInfo: sb.ToString().Trim()));
}

изображение

vadosnaprimer avatar Oct 09 '24 16:10 vadosnaprimer

If implemented, it might be worth making it optional. Column-aligned is easier to read sometimes. To be fair it's not that important because unindenting is probably a pretty easy operation with a simple regex replace like '^ +' to ' ' (in a text editor or otherwise).

RetroEdit avatar Oct 11 '24 04:10 RetroEdit

How well does it handle say the stack pointer going off the rails due to some crash, or possibly tricks manipulating the stack pointer (e.g. on the NES and GB (and probably other systems) a "popslide" technique can be used to quickly copy video memory, typically setting the stack pointer to ROM and popping off the ""stack"" to read memory as fast as possible, then writing the results to VRAM; setting the stack pointer to ROM might also be used in some ACE exploits as a way to redirect execution to more controllable memory).

CasualPokePlayer avatar Oct 11 '24 04:10 CasualPokePlayer

If implemented, it might be worth making it optional.

Forgot to mention, yeah.

Column-aligned is easier to read sometimes. To be fair it's not that important because unindenting is probably a pretty easy operation with a simple regex replace like '^ +' to ' ' (in a text editor or otherwise).

I'd expect many text editors to be able to remove leading spaces.

How well does it handle say the stack pointer going off the rails due to some crash

My initial code for a Gens mod didn't handle this in any way, but r57shell's version simply does &63 with the indent size, I think it's a fair solution.

vadosnaprimer avatar Oct 11 '24 08:10 vadosnaprimer

You can have indentation and aligned columns.

How well does it handle say the stack pointer going off the rails due to some crash

My initial code for a Gens mod didn't handle this in any way, but r57shell's version simply does &63 with the indent size, I think it's a fair solution.

Masking means the value will wrap, which IMO is better than clamping it.


On the subject of ITraceable redesigns, I managed to dig up this mockup of how colour can be used to aid readability (see also #1741): mockup

YoshiRulz avatar May 25 '25 13:05 YoshiRulz

You can have indentation and aligned columns.

How? ...I guess you could have extra right-padding after the commands themselves, and not worry about the address and bytecode alignment; that's very reasonable.

RetroEdit avatar May 25 '25 16:05 RetroEdit