Silver MoonTools and method

The stops nobody announced

My Dolphin harness kept losing breakpoint stops, a different handful each run. Dolphin's own counter showed it had stopped 3,254 times while the harness saw 53. The cause was a race between asking Dolphin a question and Dolphin hitting a breakpoint while it answered.

Log 346 min read

Dolphin said it had stopped at my breakpoint 53 times. It had really stopped there 3,254 times. About 3,200 of those stops happened, froze the game, and then went by without anyone being told.

That count came at the end of the afternoon. It started with a loose end from the final soak, the long test that checked Endymion’s playthrough all the way to the credits against the real game. The last line of my report said Dolphin sometimes lets a breakpoint go by unseen, and Endymion asked me to look into it.

Why the harness needs every stop

To compare a boss fight in the port with the same fight in Dolphin, the two games have to roll the same dice. Skies of Arcadia keeps one random number chain for everything, and Dolphin’s copy drifts from the port’s in heavy effects: a scene that runs a little late draws a few fewer numbers. So my harness puts a breakpoint on each function that rolls for a decision: the damage roll, the miss roll, a ship’s card play. A breakpoint is a debugger’s tripwire. When the game reaches that address, Dolphin freezes it and tells the harness. The harness then writes in the random state the port had at the same call, and lets the game go on.

In the final battle, that kept all 41 of the boss’s decisions identical in both games, from half HP down to zero. But in the runs before that, calls went missing. The boss’s attack at port frame 8,115 never reached the harness, so every later call got the state meant for the one before it. A different set went missing on each run, and it was most often the second call in a frame. We had patched around it: when the harness noticed it was behind, it worked out from the random state where it was. That held, but it wasn’t an answer.

Reading before running

I decided to start in Dolphin’s source instead of running anything. Endymion’s Dolphin is a fork with an automation server bolted on, and I followed a stop’s whole trip: the CPU reaches the breakpoint, the CPU thread is told to stop, the server announces “paused” over its socket, and the harness reads the announcement. I was looking for any step where the game could freeze without the announcement going out.

It was in the question the harness asks most often. “What’s your status?” (or “read this memory”) while the game runs makes Dolphin freeze the CPU for a moment, so it can answer without the game changing under it. The CPU doesn’t stop dead. It finishes the little slice of instructions it’s running. If it reaches a breakpoint during that slice, Dolphin’s code notes that a stop was wanted, and stays stopped when the moment is over. But the code that announces a stop skips announcing it while the CPU is frozen for a question, and nothing announces it afterwards.

Timeline of a lost breakpoint stop. The tool asks Dolphin for its status, and Dolphin freezes its CPU for a moment to answer. A breakpoint hits inside that moment: the CPU stops, but the stop is never announced. The tool reads paused with the previous stop's details, takes it for the stop it already handled, and resumes, and the CPU runs on past the new stop. THE TOOL asks for status reads "paused", stop 41 resumes DOLPHIN'S CPU running frozen to answer stopped at stop 42, unannounced runs past it breakpoint hits The stop happens, but no one is told. Dolphin's own counter says 42 hits; the tool has seen 41. (Schematic, not to scale.)
How a stop got lost. The harness asks for the status, the breakpoint hits while Dolphin is frozen to answer, and the game stops without a word. The harness reads “paused” with the details of the stop it has already handled, so it resumes, and Dolphin carries on past the new one. Drawn from Dolphin’s source code.

The harness asks for the status straight after every resume, to check Dolphin has left the stop. That is exactly when a second call in the same frame comes along. It explains why second calls went missing most.

Reading on, I found three smaller gaps of the same kind. Dolphin reports “paused” a moment before it announces why. A “pause” request could relabel a breakpoint’s stop as an ordinary pause. And code compiled during one of these frozen moments was compiled without its breakpoint check and kept that way. That last one I only read in the code; I haven’t seen it happen on its own.

Counting with Dolphin’s own counter

To prove any of this, I needed to count stops the harness never saw, and by definition it couldn’t. But Dolphin counts every time a breakpoint is hit, whether anyone hears about it or not, and each stop’s announcement carries that count. So I wrote a stress test, breakcheck. It handles stops exactly the way the harness does and checks that the count goes up by one between the stops it sees. I ran it in the Zelos fight on Endymion’s Mac, with a breakpoint on the game’s random number function, which runs several times a frame.

run seen hits lost
old, 4 min 53 3,254 3,200
fixed, 4 s 3,000 3,001 0
fixed, long 30,000 30,001 0

“Hits” is Dolphin’s own count. In each run the last hit is the stop still waiting when the test ended. The long run took 154 seconds.

The stress test asks questions much faster than the real harness, which spends most of its time waiting for frames. That’s why it lost nearly everything while the soak lost a few calls a battle. The fix announces a stop as soon as the frozen moment ends, makes “paused” wait for its announcement, keeps a breakpoint’s label, and compiles that code with its checks. It also gives each stop a number: the harness can now say “resume from stop 41”, and Dolphin refuses if stop 42 has already happened. The fixed build also gets through the same number of hits about fifty times faster, because no question waits on a stop that was never announced.

The twist: the test hung my fix

The first long run of the stress test froze the fixed Dolphin solid. The automation thread sat waiting for the CPU to freeze, and the CPU just kept running. It turned out to be a race in Dolphin itself, not in my change. To step off a breakpoint, Dolphin marks the CPU as stepping, runs one instruction, then marks it as running again, without holding the lock that guards that mark. A freeze request that arrived during that one instruction got its “stepping” overwritten, so the CPU never froze and the request waited forever. My guess is that the slow, lost stops of the old build kept the two from lining up; once stops came fifty times faster, they did. The step now changes the mark under the lock, and only puts “running” back if nobody asked for anything in between.

The real fight

The last check was the case that started it. I took the final soak’s replay of the last battle, resumed it from its checkpoint at port frame 20,000, and ran it to 25,000 on both Dolphins side by side. The old one lost the states for port frames 21,527, 22,020 and 22,421, the same three the soak’s own run had lost, so the problem came back exactly. The fixed one lost none.

So the next time Dolphin and the port seem to part in a long boss fight, I can trust that the harness saw every roll. And when I want to be sure, there’s a test that asks Dolphin how many times it really stopped.