Demystifying Faulty Code: Step-by-Step Reasoning for Explainable Fault Localization

Ratnadira Widyasari, Jia Wei Ang, Truong Giang Nguyen, Neil Sharma, David Lo

TL;DR

This paper introduces FuseFL, a novel approach that leverages Large Language Models (LLMs) like ChatGPT to provide step-by-step reasoning for pinpointing faulty code. By combining spectrum-based fault localization (SBFL) results, test case outcomes, and code descriptions, FuseFL significantly improves the accuracy of fault localization and generates human-like explanations for why specific code lines are deemed faulty.

What Problem Does This Paper Address?

Identifying the exact lines of code responsible for software failures, known as fault localization, is a crucial but often tedious and time-consuming task for developers. While automated tools can rank potentially faulty code segments, they often lack the "why" – a clear explanation of why a particular piece of code is flagged. Previous research indicates that developers strongly desire these explanations to trust and effectively use automated tools. This paper addresses this gap by exploring how LLMs can be guided to provide not just the location of a fault but also a step-by-step reasoning process behind that identification, making the debugging process more transparent and efficient.

Methods

The core of this work is FuseFL, an approach that enhances LLM capabilities for fault localization. FuseFL takes several pieces of information as input for the LLM:

  • Spectrum-Based Fault Localization (SBFL) Results: These are rankings of code lines based on how frequently they are executed by failing tests versus passing tests.
  • Test Case Execution Outcomes: This includes the inputs, expected outputs, and actual outputs from test runs, as well as any errors encountered.
  • Code Description: A natural language explanation of what a specific piece of code is intended to do.

FuseFL utilizes a chain-of-thought prompting strategy, specifically Zero-shot CoT, by appending "Let's think step by step" to the LLM prompt. This encourages the LLM to break down its reasoning process. The authors also experimented with various prompt combinations, feeding different subsets of the available information to the LLM to determine the optimal configuration.

Datasets

The primary dataset used in this study is the Refactory dataset. This dataset comprises 1,783 real-life faulty Python code submissions from students in an introductory programming course. The authors selected this dataset because the code snippets are typically isolated and self-contained, simplifying the fault localization and explanation process without requiring extensive domain expertise.

Crucially, the authors also created a new dataset of human explanations for faulty code. This dataset consists of 324 faulty code files, with explanations provided for 600 faulty lines. These human-generated explanations serve as a ground truth for evaluating the quality and accuracy of the explanations generated by FuseFL.

Key Insights and Ideas

  • LLMs as Reasoning Engines: The study demonstrates that LLMs, when properly prompted and provided with relevant context, can go beyond simple fault identification to offer valuable step-by-step reasoning.
  • Context is King: Combining multiple sources of information (SBFL, test results, code descriptions) significantly boosts both the accuracy of fault localization and the quality of explanations, outperforming LLMs that rely on single information sources.
  • Explanation Style Matters: While FuseFL's explanations are often correct, their style can differ from human explanations. FuseFL tends to focus more on suggesting fixes, whereas human explanations might delve deeper into the root cause of the error. This difference can impact automated evaluation metrics like BLEURT.
  • Developer Experience Influences Perception: Novice developers found FuseFL's explanations to be more correct, clear, and informative than experienced developers, who sometimes found them too verbose. This suggests a need for adaptive explanation generation.
  • Output Errors are Trickier: FuseFL struggles more with "Output Errors" (where code runs but produces incorrect results) compared to "Runtime Errors" (where code crashes). This is an area ripe for future improvement.

Key Results

  • Improved Fault Localization: FuseFL achieved a 32.3% increase in successfully localized faults at the Top-1 position compared to the best-performing LLM-based baseline (Wu et al. [18]). It also outperformed traditional SBFL techniques by a significant margin.
  • High-Quality Explanations: For successfully localized faults, FuseFL generated explanations that received an average BLEURT score of 0.492. While this score might seem low, the authors argue it reflects stylistic differences rather than incorrectness.
  • Human Evaluation Success: In a manual evaluation, FuseFL generated correct explanations in 22 out of 30 cases. Furthermore, its explanations received high scores for informativeness (5.7) and clarity (5.9) on a 7-level Likert scale, comparable to human-generated explanations.

Limitations and Future Work

  • Generalizability: The study's reliance on the Refactory dataset, consisting of student assignments, might limit the generalizability of findings to more complex, real-world codebases. Future work aims to expand to more complex code.
  • Explanation Style: The difference in explanation style between FuseFL and humans, particularly the focus on fixes versus root causes, is an area for further investigation.
  • Output Errors: FuseFL's performance on Output Errors needs improvement. The authors suggest exploring multi-round interactions with LLMs to enhance accuracy in these cases.
  • LLM Evolution: The rapid evolution of LLMs means that prompt effectiveness can change. The study used a specific version of ChatGPT (GPT-3.5), and future work might need to re-evaluate with newer models.
  • Token Limits: While not an issue in this study, more complex codebases might hit LLM token limits, requiring advanced data processing strategies.

Summary updated

BibTeX
@inproceedings{widyasari2024demystifying,
  author = {Ratnadira Widyasari and Jia Wei Ang and Truong Giang Nguyen and Neil Sharma and David Lo},
  title = {{Demystifying Faulty Code: Step-by-Step Reasoning for Explainable Fault Localization}},
  booktitle = {SANER},
  year = {2024},
  doi = {10.1109/saner60148.2024.00064},
}
Figure