Chapter 3. The LLDB debugger
The LLDB debugger is a command-line tool for debugging C and C++ programs. Use LLDB to inspect memory within the code being debugged, control the execution state of the code, and detect the execution of particular sections of code.
LLVM Toolset is distributed with LLDB 16.0.6.
3.1. Prerequisites
-
LLVM Toolset is installed.
For more information, see Installing LLVM Toolset. -
Your compiler is configured to create debug information.
For instructions on configuring the Clang compiler, see Controlling Debug Information in the Clang Compiler User’s Manual.
For instructions on configuring the GCC compiler, see Preparing a Program for Debugging in the Red Hat Developer Toolset User Guide.
3.2. Starting a debugging session
Use LLDB to start an interactive debugging session.
Procedure
To run LLDB on a program you want to debug, use the following command:
On Red Hat Enterprise Linux 8:
$ lldb <binary_file_name>
Replace
<binary_file>
with the name of your compiled program.You have started your LLDB debugging session in interactive mode. Your command-line terminal now displays the default prompt
(lldb)
.
On Red Hat Enterprise Linux 9:
$ lldb <binary_file>
Replace
<binary_file>
with the name of your compiled program.You have started your LLDB debugging session in interactive mode. Your command-line terminal now displays the default prompt
(lldb)
.
- To quit the debugging session and return to the shell prompt, run the following command:
(lldb) quit
3.3. Executing your program during a debugging session
Use LLDB to execute your program during your debugging session. The execution of your program stops when the first breakpoint is reached, when an error occurs, or when the program terminates.
Prerequisites
-
You have started an interactive debugging session.
For more information, see Starting a debugging session with LLDB.
Procedure
To execute the program you are debugging, run:
(lldb) run
To execute the program you are debugging using a specific argument, run:
(lldb) run <argument>
-
Replace
<argument>
with the command-line argument you want to use.
-
Replace
3.4. Using breakpoints
Use breakpoints to pause the execution of your program at a set point in your source code.
Prerequisites
-
You have started an interactive debugging session.
For more information, see Starting a debugging session with LLDB.
Procedure
To set a new breakpoint on a specific line, run the following command:
(lldb) breakpoint set --file <source_file_name> --line <line_number>
-
Replace
<source_file_name>
with the name of your source file and<line_number>
with the line number you want to set your breakpoint at.
-
Replace
To set a breakpoint on a specific function, run the following command:
(lldb) breakpoint set --name <function_name>
-
Replace
<function_name>
with the name of the function you want to set your breakpoint at.
-
Replace
To display a list of currently set breakpoints, run the following command:
(lldb) breakpoint list
To delete a breakpoint, run:
(lldb) breakpoint clear -f <source_file_name> -l <line_number>
-
Replace
<source_file_name>
with the name of your source file and<line_number>
with line number of the breakpoint you want to delete.
-
Replace
To resume the execution of your program after it reached a breakpoint, run:
(lldb) continue
To skip a specific number of breakpoints, run the following command:
(lldb) continue -i <breakpoints_to_skip>
Replace
<breakpoints_to_skip>
with the number of breakpoints you want to skip.NoteTo skip a loop, set the
<breakpoints_to_skip>
to match the loop iteration count.
3.5. Stepping through code
You can use LLDB to step through the code of your program to execute only one line of code after the line pointer.
Prerequisites
-
You have started an interactive debugging session.
For more information, see Starting a debugging session with LLDB.
Procedure
To step through one line of code:
- Set your line pointer to the line you want to execute.
Run the following command:
(lldb) step
To step through a specific number of lines of code:
- Set your line pointer to the line you want to execute.
Run the following command:
(lldb) step -c <number>
-
Replace
<number>
with the number of lines you want to execute.
-
Replace
3.6. Listing source code
Before you execute the program you are debugging, the LLDB debugger automatically displays the first 10 lines of source code. Each time the execution of the program is stopped, LLDB displays the line of source code on which it stopped as well as its surrounding lines. You can use LLDB to manually trigger the display of source code during your debugging session.
Prerequisites
-
You have started an interactive debugging session.
For more information, see Starting a debugging session with LLDB.
Procedure
To list the first 10 lines of the source code of the program you are debugging, run:
(lldb) list
To display the source code from a specific line, run:
(lldb) list <source_file_name>:<line_number>
-
Replace
<source_file_name>
with the name of your source file and<line_number>
with the number of the line you want to display.
-
Replace
3.7. Displaying current program data
The LLDB debugger provides data on variables of any complexity, any valid expressions, and function call return values. You can use LLDB to display data relevant to the program state.
Prerequisites
-
You have started an interactive debugging session.
For more information, see Starting a debugging session with LLDB.
Procedure
To display the current value of a certain variable, expression, or return value, run:
(lldb) print <data_name>
-
Replace
<data_name>
with data you want to display.
3.8. Additional resources
- For more information on the LLDB debugger, see the official LLDB documentation LLDB Tutorial.
- For a list of GDB commands and their LLDB equivalents, see the GDB to LLDB Command Map.