Skip to main content
In this lesson, we explore a powerful debugging tool tailored for Rust development: Rust LLDB. Although debugging might seem challenging at first, this guide will walk you through the essential concepts and procedures step by step.

What is LLDB?

LLDB (Low-Level Debugger) is an efficient and user-friendly debugger that is part of the LLVM project. As the default debugger on macOS, it enables you to pause program execution, inspect variables, and step through code interactively.
The image introduces "lldb" with an illustration of a person interacting with a laptop displaying code, highlighting features like being "Fast" and "User Friendly."

Introducing Rust LLDB

Rust LLDB is a customized version of LLDB designed specifically for Rust. It is configured to understand Rust’s unique memory management, data types, and error handling. This Rust-friendly debugger is integrated seamlessly into the Rust toolchain. If you installed Rust via Rustup, Rust LLDB is already available. Confirm your installation with:
If version information is displayed, Rust LLDB is installed and ready to use.
The image describes "Rust lldb," a tool designed for a smoother debugging experience with Rust code, highlighting its configuration for Rust's unique features. It includes two arrows pointing in opposite directions.
Rust LLDB is ready to use out of the box as it comes bundled with the Rust toolchain.
The image describes "Rust lldb" as a tool that is Rust-friendly and built for Rust developers, highlighting its ease of use and integration with the Rust toolchain.

Launching Rust LLDB

To start debugging, launch Rust LLDB by executing the following command with the compiled executable from your project’s debug directory:
For demonstration, consider a simple Rust program that calculates the factorial of a number.

Writing and Compiling a Factorial Program

Before debugging, compile your Rust program with debug information. Below is a basic example of a factorial function:
Compile the program using Cargo:
After a successful build, the executable will be located in your project’s debug folder.

Introducing a Bug for Debugging

To demonstrate the debugging process, let’s intentionally introduce a subtle bug. Instead of subtracting 1 in the recursive call, we accidentally add 1:
Rebuild the project with:
This error causes the recursion never to satisfy the base case, leading to a stack overflow. Next, navigate to your project directory (for example, “debug-rust”) and launch Rust LLDB with:
Once launched, you’ll see the LLDB prompt. Type help for a list of available commands.

Setting Breakpoints and Starting a Debug Session

You can set breakpoints to inspect your code execution. To set a breakpoint at the beginning of the factorial function, enter:
Alternatively, set a breakpoint at a specific line (e.g., line 5):
A sample output might look like:
Now, start the debugging session by running:
When the first breakpoint is hit, you will see output similar to:
At this point, inspect variables like n:
Stepping through the code reveals that the value of n increases rather than decreasing. For example, after stepping into the recursive call, you might see:
This confirms that n becomes 6 and continues increasing, which prevents the recursion from reaching its base case and ultimately leads to a stack overflow.
Using an incorrect recursive call (i.e., adding instead of subtracting) will cause an infinite loop, leading to a stack overflow error. Always verify the logic of recursive functions.

Fixing the Bug

Exit the debugger and correct the error by replacing n + 1 with n - 1 in the recursive call. The fixed code is:
Compile and run your project:
The output should now correctly display:
For comparison, running the buggy version would result in a stack overflow error:
By using Rust LLDB to step through your code and inspect variable values, you can efficiently pinpoint and resolve subtle bugs in your Rust applications.
Happy debugging!

Watch Video

Practice Lab