Abstract
Large Language Models (LLMs) have shown remarkable capabilities in code generation but often falter when faced with complex logical requirements. Existing self-debugging techniques typically treat programs as black boxes, relying on final outputs or error messages for refinement. This paper presents LDB (Large Language Model Debugger), a framework that enables LLMs to debug code with human-like granularity. By decomposing programs into basic blocks and inspecting runtime variable states, LDB grounds the debugging process in actual execution data. Experimental results demonstrate that LDB significantly enhances code generation accuracy, achieving a state-of-the-art 95.1% Pass@1 on HumanEval when paired with advanced generators.
Introduction: The Need for Granular Debugging
While models like GPT-4 are proficient at generating initial code drafts, they often struggle to self-correct when the logic is flawed. Current "self-debugging" methods usually present the LLM with the entire program and the final execution result, asking it to identify and fix errors in one go. This approach is sub-optimal because it forces the model to mentally simulate execution, a process prone to "hallucinations."
In contrast, human developers use debuggers to set breakpoints and examine variable values at specific execution points. LDB bridges this gap by providing LLMs with the same runtime information a human would have, allowing for precise, step-by-step verification of program logic.
Methodology: The LDB Workflow
LDB implements a structured four-step process to refine generated code:
- Profiling: The "seed" program is analyzed to build a Control Flow Graph (CFG), which segments the code into basic blocks—sequences of instructions with a single entry and exit point.
- Execution Trace: The program is executed against a failing test case. LDB captures the "Runtime States," recording the values of all variables before and after each basic block.
- Step-by-Step Debugging: The LLM is presented with individual blocks alongside their corresponding runtime variables and the original task description. It acts as a verifier, providing a "True/False" verdict and a natural language explanation for each block's correctness.
- Regeneration: The LLM receives the original program and the collected block-level feedback to produce a corrected version.
To maintain efficiency, LDB employs Selective Debugging and Batch Debugging, ensuring that the context remains within the LLM's limits while minimizing token expenditure.
Experimental Evaluation
LDB was evaluated across three major benchmarks: HumanEval (164 Python problems), MBPP (500 basic Python tasks), and TransCoder (C++ to Python translation). The framework was tested using various LLM backbones, including GPT-3.5, CodeLlama-34B, and StarCoder-15B.
Key Insights
The Importance of Block-Level Granularity
The researchers compared different decomposition levels: line-level, block-level, and function-level. They found that block-level decomposition is the most effective. Line-level analysis often breaks the semantic flow of operations, while function-level analysis is too coarse to isolate specific logical errors.
Grounding Reasoning in Runtime Reality
LLMs often miscalculate variable states when performing "mental" dry runs. By providing actual runtime values, LDB eliminates these hallucinations, ensuring the model's reasoning is based on the program's real behavior.
Sustained Improvement through Iteration
Unlike traditional self-debugging methods that often plateau quickly, LDB demonstrates a "continuing growth potential." Because it receives fresh, accurate runtime data at each iteration, it can consistently refine the code over multiple debugging cycles.
Results and Performance
LDB consistently outperforms existing self-debugging baselines. Key results include:
- Significant Accuracy Gains: LDB improved GPT-3.5's performance by 9.1% on HumanEval and 8.4% on MBPP.
- State-of-the-Art Benchmarks: Combined with the Reflexion generator, LDB achieved a record 95.1% Pass@1 on HumanEval.
- Effectiveness on Complex Tasks: The most substantial improvements were observed in "Hard" difficulty problems, where runtime information is most critical for resolving non-trivial logic.
Conclusion and Future Directions
LDB represents a significant step toward more robust and human-like automated debugging. While highly effective, it currently depends on the availability of at least one visible test case to generate execution traces. Future research may explore "test-case-free" debugging, where LLMs identify logical inconsistencies by analyzing intermediate states against task descriptions without knowing the final expected output.
Summary updated