Crusader_Decomp/docs/jp-remorse-windows-debugging.md
2026-10-11 18:49:46 +02:00

64 lines
No EOL
8.8 KiB
Markdown

# Japanese No Remorse Windows Debugging
## Executive Summary
The JP executable can be debugged as a normal 32-bit Win32 process with an external debugger. Its own debug facilities are narrower: `-debug` enables a timeline-like diagnostic buffer, and the executable imports `DebugBreak` for selected invariant/error paths. No JP equivalent of the DOS `CRUDEBUG` environment switch, classic hidden USECODE debugger, or app-specific debugger-attach protocol was found.
Ghidra 12.1.4 includes live debugger agents for Microsoft `dbgeng` and x64dbg, including local attach launchers. Its Decompiler can be used as an address-oriented view while the native executable runs. The reconstructed C is not executable source: breakpoints and stepping operate on machine instructions, with pseudocode synchronized to those addresses where possible.
## JP Game-Side Facilities
### The `-debug` option
`handle_commandline_args_jp` at `00466774` maps `-debug` to the handler at `004668b4`. The handler prints `Debugging mode ON` and sets bytes at `004957d9` and `004957eb`. The latter gates `debug_mode_update_timeline_buffer` at `00455eac`; that helper builds and copies a 512-byte timeline-like marker buffer from object counters. Its exact visible consumer is unresolved. This is engine instrumentation, not a debugger connection, breakpoint UI, or recovered USECODE break-state system. The flag at `004957d9` has no recovered reader.
The separate [hidden debugger investigation](jp-remorse-hidden-debugger-investigation.md) found no classic JP USECODE debugger UI/bootstrap or break-state lifecycle. JP's metadata opcodes do not reproduce the DOS debugger's source-line and current-entry recording behavior.
### Environment variables
The DOS build's `Kernel_Init` reads `CRUDEBUG` through its environment lookup helper and sets a debug-enabled global. In the JP PE, an exact ASCII byte search for `CRUDEBUG` found no hit. `GetEnvironmentStrings` is in the import table, but Ghidra found no code call site to it beyond the import entry. No JP environment-variable equivalent was recovered.
The JP `-debug` command-line option is the nearest identified counterpart, but it is not behaviorally equivalent to DOS `CRUDEBUG`: it enables the timeline-buffer instrumentation described above. Absence of the literal and direct environment lookup is strong negative evidence, not proof that no encoded or dynamically constructed environment check exists.
### Breaks, assertions, and crashes
The PE imports `DebugBreak`. One confirmed use is in `usecode_reference_table_decrement_refcount` at the call site `00467da6`: if the reference lookup fails, the code shows the game window/cursor, calls `DebugBreak`, retries the lookup, and then continues using the result. An attached debugger can stop at this breakpoint exception. If the lookup still fails after continuing, an invalid access can follow; this is an invariant trap, not a crash-recovery handler.
Other `DebugBreak` references occur in `process_choose_direction_to_coords` at `00453c64`. The call at `00453985` belongs to the recovered `process_update_direction_toward_target` at `004538c0`: it reads its associated item's current X/Y from the global item tables and delegates 8-way direction selection to the helper. The helper tests direction candidates and updates direction/turn state; it can return `0x10` when a packed process-state flag is set or `0xff` through a separate special-action path. These breakpoints are gated by global `004955e8`. Static xrefs still show reads of this global but no in-image writes, so how it becomes nonzero and whether ordinary gameplay reaches the breaks remain unresolved. They are diagnostic traps, not evidence of a user-facing stepping or inspection facility.
The imports include CRT unwind support (`RtlUnwind`) and the image contains C++ exception-runtime messages. The inspected import table has no explicit `SetUnhandledExceptionFilter` or `UnhandledExceptionFilter` import, and no custom JP crash-reporting path was established. This does not mean crashes cannot be debugged: a native debugger can stop on an access violation or breakpoint exception, provided it is attached before the fault or launches the process under debugging. Exact first-chance/second-chance behavior depends on the debugger backend and its exception policy.
No imports or strings were found for common app-side attach/detection APIs such as `IsDebuggerPresent`, `CheckRemoteDebuggerPresent`, `DebugActiveProcess`, or `OutputDebugString`. Such APIs are not required for an external debugger to attach; process attachment is provided by Windows and the debugger backend.
## Debugging With Ghidra
### Live native debugging
The installed Ghidra 12.1.4 distribution contains:
- `Debugger-agent-dbgeng`, with local launch and attach launchers using `dbgeng.dll`.
- `Debugger-agent-x64dbg`, with local launch and attach launchers using `x64dbg.dll` and x64dbg's automation interface. The agent has 32-bit register/context support; for this x86 game, use the x32 debugger variant where appropriate.
Ghidra's setup notes specify Python 3.9-3.14 for debugger agents; WinDbg/DbgEng support uses Pybag and protobuf. The bundled launcher F1 help describes the local setup and dependencies. The attach launchers accept a process ID, so the JP game can be started normally and attached, or launched from Ghidra under the selected backend.
With a live target, Ghidra can provide dynamic disassembly, registers, memory, stack/call-stack state, breakpoints, and trace snapshots, subject to what the backend reports. The bundled Debugger course documents setting an address breakpoint from a static or dynamic listing, including the Decompiler window. A breakpoint selected on pseudocode resolves to a machine address; it does not create a source-line breakpoint in original game source.
### What “debug the decompiled code” means
- **Supported:** stop the native process at an address represented in the Decompiler, inspect the synchronized instruction/pseudocode, and use value hovers to evaluate recovered locals, parameters, globals, or fields against live state when Ghidra can map them.
- **Not source-level execution:** Ghidra cannot execute a decompiler C statement as C or step it independently of the binary. A pseudocode statement can correspond to several instructions, and recovered types, names, and variable lifetimes are hypotheses unless confirmed by runtime state. Without matching source/debug symbols, use the disassembly, registers, stack, and memory as the ground truth.
- **Emulation is different:** Ghidra's emulator steps the instruction semantics represented as p-code, which is also used by the decompiler. It does not run the decompiled C and does not automatically reproduce the full Windows/DirectDraw/DirectSound environment. It is useful for bounded functions or paths, but unsupported p-code userops and external API behavior may require modeling or stubbing.
### Practical workflow
1. Open the JP `/ja/CRUSADER.EXE` program in the Ghidra Debugger tool and choose the local `dbgeng attach` or x64dbg attach launcher, or launch the game under that backend. Confirm the target module is mapped to the analyzed image before trusting static-address breakpoints.
2. Set a breakpoint from the Decompiler/disassembly at the code of interest. For the known invariant trap, `00467da6` is a candidate; use a narrow breakpoint rather than breaking on every `DebugBreak` invocation.
3. To investigate a crash, attach before reproducing it and configure the backend to stop on the relevant access violation/breakpoint exception. Capture the exception address, registers, stack, and nearby memory before resuming or terminating the process.
4. To test `-debug`, launch with that argument and observe the `00455bac` gate and `00455eac` helper. Do not infer classic USECODE debugger behavior from the “Debugging mode ON” message.
5. Keep the game executable and data unchanged during initial debugging. Static analysis and live debugging are read-only unless an explicit dynamic edit/patch action is chosen.
## Evidence Boundary
The JP findings above come from live Ghidra analysis of `/ja/CRUSADER.EXE`, including its import table, `-debug` handler, timeline-buffer consumer, and `DebugBreak` xrefs. The no-`CRUDEBUG` result is based on the absence of the ASCII byte pattern and the lack of a direct `GetEnvironmentStrings` code reference. The exception/crash discussion is static; no JP crash was reproduced in a live debugger during this investigation.
Ghidra capability claims were checked against the installed 12.1.4 distribution: `docs/GhidraClass/Debugger/A3-Breakpoints.md`, `A4-MachineState.md`, and `B2-Emulation.md`; the `Debugger-agent-dbgeng` and `Debugger-agent-x64dbg` launchers; and the Debugger notes in `docs/GettingStarted.md`. The attach integration is available in this installation, but an actual JP live-debugger session has not yet been tested.