Debugging Legacy Code with LLMs: A Practical Workflow for AI-Assisted Troubleshooting

    This article shares a practical workflow for using large language models to debug and maintain legacy code that no longer runs. Based on a firmware game project that failed to build, it walks through two rounds of AI collaboration: static review to build a hypothesis list, then targeted fixes for a wild pointer caused by stale state after a mode switch and a misconfigured protocol identifier. It also covers a refinement phase focused on input latency, rendering performance, memory reuse, and observability, and closes with four principles for AI-assisted troubleshooting and how to standardize legacy code handoffs into review, reproduction, fix, and refinement stages.

    1. Background: Why Taking Over Legacy Code Is Harder Than Generating from Scratch

    Writing a new feature from scratch and taking over existing code that does not run are two very different tasks in terms of difficulty. The former has a clear goal and a clean context. The latter involves three layers of uncertainty at once: the original author’s design intent is unclear, the runtime environment is already polluted, and several layers of abstraction sit between the observed failure and its root cause.

    In one typical exercise, an AI coding tool was asked to build a small game for a firmware environment. After several hours it still could not produce a runnable program, leaving behind seven files of modular code that looked well structured. A different LLM was then brought in to take over the mess, with the goal of getting it running first and refining it later. The problems surfaced by this process are more instructive than a smooth development run would have been.

    The value of this kind of exercise is not in comparing which model is stronger. It is in distilling a general workflow for AI-assisted troubleshooting. In real organizations, the pain point is rarely generating code from scratch; it is the dirty work of maintenance, migration, and secondary development.

    2. Round One: From “Not Runnable” to “Basically Runnable”

    2.1 Always Start with Static Review, Not Direct Edits

    The first instruction after taking over code should be: review this code and list the likely problems. The model went through the entry point, main loop, game logic, rendering, and input handling module by module, and produced a problem list within minutes.

    The value of this step is establishing a set of hypotheses. The worst thing in troubleshooting is blind editing: jumping on the first suspicious spot, finding the symptom unchanged, and moving to the next, burning all the time on trial and error. Building a list first shrinks the search space from “any code” to “a few candidate root causes,” so every later change either verifies or rules out a hypothesis.

    Practical tip: when asking the model to report problems, require it to cite its evidence, such as the specific line, the calling convention, or the memory lifetime involved, rather than giving conclusions alone. A list without evidence is barely different from random guessing.

    2.2 Symptom One: Exits Immediately on Startup, Caused by a Wild Pointer

    The program compiled and started, printed a log showing it had entered the main game loop, and then exited immediately. This kind of “dies on startup” problem is usually not a logic error but a resource lifetime error.

    After adding finer-grained serial debug output, the crash was traced to a call made right after a graphics output protocol call. The root cause: the information pointer obtained when querying the mode pointed to memory that had become invalid after the mode was set, because the buffer had been reallocated or freed and the pointer was now dangling. The fix was straightforward: query the frame buffer information again after setting the mode instead of reusing the old pointer.

    This yields a general rule: for any three-step flow of “query, change, then use,” be suspicious of the validity of the returned value. In firmware, driver, graphics, and network programming, handles, buffers, and descriptors becoming invalid after a state transition is a common trap. The fix pattern looks schematically like this:

    1. // Wrong: reuse the query result from before the state change
    2. info = query_mode(handle);
    3. set_mode(handle, mode);
    4. use(info->frame_buffer); // info may already be invalid
    5. // Correct: query again after the state change
    6. set_mode(handle, mode);
    7. info = query_mode(handle); // obtain valid information again
    8. use(info->frame_buffer);

    2.3 Symptom Two: Runs but Shows No UI, Caused by a Wrong Identifier

    Once the crash was fixed, the program no longer exited, but nothing appeared on the screen. This kind of silent failure is harder to debug than a crash because there is no exception signal.

    Analysis pointed to the configuration of the identifier used by the graphics output protocol: the same specification defines several similar global unique identifiers that refer to protocols for different purposes, and the wrong one naturally failed to provide the correct graphics output interface. After correcting the identifier, the display worked normally.

    The second general rule: on platforms that rely on “global unique identifier plus protocol interface,” a failure to obtain an interface is often not a logic problem but a mismatch between the identifier and its intended purpose. When troubleshooting, first print whether the interface pointer actually obtained is null and whether the version matches expectations, then look at the business logic.

    3. Round Two: Refinement, from “Runs” to “Usable”

    Getting it running is only a passing grade. The second round of refinement usually focuses on four areas:

    1. Input latency and feel: in a firmware environment, a mismatch between input sampling and render frame rate causes dropped keypresses or sluggish loops, which requires simple input buffering and debouncing.
    2. Rendering performance: full-screen redraws are expensive, so switch to dirty rectangle updates that refresh only the regions that changed.
    3. Resources and memory: check for repeated allocations every frame, and change dynamic allocation to a one-time allocation at startup with reuse during runtime.
    4. Observability: keep a leveled logging switch that can be turned off in production and on during debugging, so serial output does not slow down the main loop.

    This stage shows the real value of an LLM: it can hold the context of multiple modules at once and make consistent cross-file changes, rather than losing track of one thing while fixing another the way manual debugging often does.

    4. Methodology: Four Principles for AI-Assisted Troubleshooting

    • Diagnose before modifying: every change must correspond to one hypothesis on the list, avoiding random trial and error.
    • Observability first: add logs and breakpoints before touching the logic, and let the program tell you where it died.
    • Minimal reproducibility: pin down the environment, inputs, and reproduction steps, otherwise you cannot tell whether a change actually worked.
    • Staged acceptance: get it to build, then to start, then to produce output, and finally to feel smooth, with a clear acceptance criterion at each stage.

    For organizations, this workflow can be written directly into engineering standards: standardize the legacy system handoff into four stages of review, reproduction, fix, and refinement. Combined with container platforms commonly available in the cloud for environment isolation, log services for run records, and monitoring and alerting for anomaly tracking, it turns “firefighting that depends on individual experience” into “stable delivery that depends on process.”

    5. Conclusion

    The real takeaway from this exercise is not that one model won by a few hours. It is a confirmation: the strength of LLMs in legacy code governance lies in broad scanning and cross-file consistent changes, while the irreplaceable role of human developers lies in defining acceptance criteria and making business trade-offs. Chaining the two together through a process is the correct way to use AI in software development.