Debugging Legacy Code with Large Language Models: A Practical Workflow
This article walks through a real-world debugging exercise in which a large language model (LLM) takes over a non-running modular graphics game project and brings it to a stable, maintainable state. It covers a two-round workflow: first establishing a minimal runnable loop through static review, severity-ranked issue lists, explicit failure handling, and re-querying state after mode changes; then hardening input handling, main-loop timing, resource cleanup, observability, and testability. It also draws the boundary of human-AI division of labor, showing how engineers should organize context and design verification loops to avoid changes that look fixed but are not.
1. Background: Legacy Code Is the Real Battleground
In software engineering, generating code for a brand-new project from scratch is rarely the hardest part. The real cost sits in taking over a piece of code someone else left behind that does not run. These situations share a familiar pattern: the structure looks clean and the modules are reasonably separated, but there are no reproducible instructions for the runtime environment, edge cases are unhandled, and low-level API calls happen in the wrong order. The result is a crash, a blank screen, or a feature that silently stops working.
This article follows a hands-on exercise. One AI coding tool first generated a modular graphics mini-game consisting of seven files (entry point, main loop, game logic, rendering, and input handling), but the program never ran correctly because of how the underlying graphics output protocol was used. A second LLM-powered coding tool then took over that “dead code” in a fresh environment and fixed it in two rounds: first to make it runnable, then to make it worth optimizing.
This article does not judge any specific product. Instead, it abstracts a transferable methodology: when LLMs move into maintenance and troubleshooting, how should human engineers organize context, design verification loops, and avoid the trap of “it looks fixed but it is not actually fixed”?
2. Round One: From “It Compiles” to “It Runs Stably”
The goal of the first round is not optimization. It is establishing a minimal runnable loop. Work in the following order.
1. Start with a Static Review, Not Edits
The first instruction should be deliberately restrained: review this code and list the issues that could cause runtime failures, without modifying any files. Have the model inspect the entry function, main loop, game logic, rendering, and input handling module by module, and produce an issue list sorted by severity.
This delivers two benefits. First, it turns “guess-and-patch” into checklist-driven progress. Second, it preserves the original code as a reference, preventing the model from rewriting large portions unprompted and making root causes harder to locate.
A reusable prompt skeleton:
Role: Senior systems-level debugging engineerInput: The directory below contains the complete source code; the environment is newly createdTask: Perform a static review only; do not modify filesOutput: An issue list graded P0/P1/P2, where each item includeshypothesis about the symptom / trigger condition / verification method / suggested fixConstraint: Do not introduce new third-party dependencies
2. First Failure Pattern: Exits Immediately After Launch
The program compiles and starts, prints the log marking the beginning of the main loop, and then exits immediately. This is the most common failure mode in low-level graphics programming. Ask the model to add debugging output over the serial port or standard output, and place progress markers before and after each protocol call. The failing region quickly narrows to “after graphics output protocol initialization.”
The root cause is usually a lifetime issue: the pointer obtained when querying mode information points to memory that becomes invalid after the mode switch, so dereferencing it afterward is a wild pointer access. The correct approach is to query the frame buffer information again after the mode is set, rather than reusing the pointer from before the switch.
The generalized lesson: for any call sequence of “query, then switch, then use,” assume the query result may become invalid and re-read it after the state change. Write it as an explicit constraint:
/* Must re-acquire after a state change; reusing the old pointer is forbidden */status = set_mode(gop, mode);if (!EFI_ERROR(status)) {status = query_mode(gop, mode, &info); /* re-acquire */framebuffer = info->FrameBufferBase;}
3. Second Failure Pattern: It Runs but Nothing Appears on Screen
The process no longer exits, but the screen shows no output. This kind of silent failure is harder to diagnose than a crash because it emits no error signal. Shift attention to the protocol identifier configuration: a low-level framework typically defines several constants related to graphics output, and choosing the wrong one means the protocol cannot be located, the function returns a null pointer, and later logic ignores it.
The recommended troubleshooting sequence has three steps: confirm the protocol was actually installed; confirm you are using the identifier defined in the current platform specification; and add error branches after every nullable return value so failures surface explicitly instead of being swallowed.
if (gop == NULL) {log("gop not located, guid may be wrong");return; /* fail explicitly to avoid a later null pointer */}
3. Round Two: Refinement and Maintainability
Getting it to run is only the starting point. The second round should focus on robustness, following this checklist:
- Input handling: debounce key presses to prevent a single long press from being registered as repeated operations.
- Main-loop timing: drive logic updates with a fixed time step and decouple rendering from logic, so frame-rate fluctuations do not cause inconsistent speed.
- Resources and state: define clear initialization and release paths to avoid resource leaks on mid-run exit.
- Observability: keep a leveled logging switch that can be turned off in production and on during debugging.
- Testability: isolate game logic from platform-specific code so the core logic can be unit-tested in an ordinary environment.
4. Methodology: Where the Human-AI Boundary Lies
The most valuable conclusion from this exercise is a clear view of what AI coding tools can and cannot do. They are good at reading unfamiliar code quickly, producing severity-ranked issue lists, making scoped changes on instruction, and adding debugging markers. They tend to fail when they must guess from experience without runtime feedback, or judge that something is “fixed” in an environment where errors are never surfaced explicitly.
The right engineering posture follows from this: humans define the verification criteria and the runtime environment, while the model iterates inside a well-defined verification loop. Solve one class of problem per round, and require an observable pass criterion for each class. If stronger engineering support is needed, container platforms can pin down the environment, log services can collect output, and monitoring and alerting can catch anomalies, so that “does it actually work after the change” is answered by the system rather than by subjective judgment.
5. Conclusion
Handing non-running legacy code to an LLM is essentially automated maintenance with a verification loop. Success does not depend on the model getting it right on the first try; it depends on whether you establish four disciplines: static review, failure classification, explicit failure, and re-acquiring state. Master this workflow, and an AI coding tool becomes a reliable assistant for day-to-day maintenance and troubleshooting rather than a demo toy.
