이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 3. lldb


lldb is a command line tool you can use to debug programs written in various programming languages. It allows you to inspect memory within the code being debugged, control the execution state of the code, detect the execution of particular sections of code, and much more.

Clang and LLVM Toolset is distributed with lldb 7.0.1.

Note

You can execute any command using the scl utility on Red Hat Enterprise Linux 7, causing it to be run with the Clang and LLVM Toolset binaries available. To use Clang and LLVM Toolset on Red Hat Enterprise Linux 7 without a need to use scl enable with every command, run a shell session with:

$ scl enable llvm-toolset-7.0 'bash'
Copy to Clipboard Toggle word wrap

3.1. Installing lldb

The lldb tool is provided by the llvm-toolset-7.0-lldb package and is automatically installed with the llvm-toolset-7.0 package. See Section 1.4, “Installing Clang and LLVM Toolset”.

3.2. Preparing a Program for Debugging

To compile a C or C++ program with debugging information that lldb can read, make sure the compiler you use is instructed to create debug information.

3.3. Running lldb

To run lldb on a program you want to debug:

  • For Red Hat Enterprise Linux 7:

    $ scl enable llvm-toolset-7.0 'lldb program_file_name'
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ lldb program_file_name
    Copy to Clipboard Toggle word wrap

This command starts lldb in an interactive mode and displays the default prompt, (lldb).

To quit the debugging session and return to the shell prompt, run the following command at any time:

(lldb) quit
Copy to Clipboard Toggle word wrap

Example 3.1. Running the lldb Utility on the fibonacci Binary File

Consider a C source file named fibonacci.c with the following content:

#include <stdio.h>
#include <limits.h>

int main (int argc, char *argv[]) {
  unsigned long int a = 0;
  unsigned long int b = 1;
  unsigned long int sum;

  while (b < LONG_MAX) {
    printf("%ld ", b);
    sum = a + b;
    a = b;
    b = sum;
  }
  return 0;
}
Copy to Clipboard Toggle word wrap

Enable the debug information and compile the fibonacci.c with the following command:

  • For Red Hat Enterprise Linux 7:

    $ scl enable llvm-toolset-7.0 'clang -g -o fibonacci fibonacci.c'
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ clang -g -o fibonacci fibonacci.c
    Copy to Clipboard Toggle word wrap
Note

Refer to Section 3.2, “Preparing a Program for Debugging” for information about controlling debug information using GCC or clang.

Start debugging the program with lldb:

  • For Red Hat Enterprise Linux 7:

    $ scl enable llvm-toolset-7.0 'lldb fibonacci'
    (lldb) target create "fibonacci"
    Current executable set to 'fibonacci' (x86_64).
    (lldb)
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ lldb fibonacci
    (lldb) target create "fibonacci"
    Current executable set to 'fibonacci' (x86_64).
    (lldb)
    Copy to Clipboard Toggle word wrap

The output indicates that the program fibonacci is ready for debugging.

3.4. Listing Source Code

To view the source code of the program you are debugging:

(lldb) list
Copy to Clipboard Toggle word wrap

As a result, the first ten lines of the source code are displayed.

To display the code from a particular line:

(lldb) list source_file_name:line_number
Copy to Clipboard Toggle word wrap

Additionally, lldb displays source code listing automatically in the following situations:

  • Before you start the execution of the program you are debugging, lldb displays the first ten lines of the source code.
  • Each time the execution of the program is stopped, lldb displays the lines that surround the line on which the execution stops.

3.5. Using Breakpoints

Setting a New Breakpoint

To set a new breakpoint at a certain line:

(lldb) breakpoint source_file_name:line_number
Copy to Clipboard Toggle word wrap

To set a breakpoint on a certain function:

(lldb) breakpoint source_file_name:function_name
Copy to Clipboard Toggle word wrap

Example 3.2. Setting a New Breakpoint

This example assumes that you have successfully compiled the fibonacci.c file as shown in Example 3.1, “Running the lldb Utility on the fibonacci Binary File”

Set two breakpoints at line 10 by running the following commands:

(lldb) b 10
Breakpoint 1: where = fibonacci`main + 33 at fibonacci.c:10, address = 0x000000000040054e
Copy to Clipboard Toggle word wrap
(lldb)  breakpoint set -f fibonacci.c --line 10
Breakpoint 2: where = fibonacci`main + 33 at fibonacci.c:10, address = 0x000000000040054e
Copy to Clipboard Toggle word wrap
Note

In lldb, the command b is not an alias to breakpoint. You can use both commands to set breakpoints, but b uses a subset of the syntax supported by gdb’s break command, and breakpoint uses lldb syntax for setting breakpoints.

Listing Breakpoints

To display a list of currently set breakpoints:

(lldb) breakpoint list
Copy to Clipboard Toggle word wrap

Example 3.3. Listing Breakpoints

This example assumes that you have successfully followed the instructions in Example 3.2, “Setting a New Breakpoint”.

Display the list of currently set breakpoints:

(lldb) breakpoint list
Current breakpoints:
1: file = 'fibonacci.c', line = 10, exact_match = 0, locations = 1
  1.1: where = fibonacci`main + 33 at fibonacci.c:10, address = fibonacci[0x000000000040054e], unresolved, hit count = 0

2: file = 'fibonacci.c', line = 10, exact_match = 0, locations = 1
  2.1: where = fibonacci`main + 33 at fibonacci.c:10, address = fibonacci[0x000000000040054e], unresolved, hit count = 0
Copy to Clipboard Toggle word wrap

Deleting Existing Breakpoints

To delete a breakpoint that is set at a certain line:

(lldb) breakpoint clear -f source_file_name -l line_number
Copy to Clipboard Toggle word wrap

Example 3.4. Deleting an Existing Breakpoint

This example assumes that you have successfully compiled the fibonacci.c file.

Set a new breakpoint at line 7:

(lldb) b 7
Breakpoint 3: where = fibonacci`main + 31 at fibonacci.c:9, address = 0x000000000040054c
Copy to Clipboard Toggle word wrap

Remove this breakpoint:

(lldb) breakpoint clear -l 7 -f fibonacci.c
1 breakpoints cleared:
3: file = 'fibonacci.c', line = 7, exact_match = 0, locations = 1
Copy to Clipboard Toggle word wrap

3.6. Starting Execution

To start an execution of the program you are debugging:

(lldb) run
Copy to Clipboard Toggle word wrap

If the program accepts command-line arguments, you can provide them as arguments to the run command:

(lldb) run argument
Copy to Clipboard Toggle word wrap

The execution stops when the first breakpoint is reached, when an error occurs, or when the program terminates.

Example 3.5. Executing the fibonacci Binary File in lldb

This example assumes that you have successfully followed the instructions in Example 3.2, “Setting a New Breakpoint”.

Execute the fibonacci binary file in lldb:

(lldb) run
Process 21054 launched: 'fibonacci' (x86_64)
Process 21054 stopped
* thread #1, name = 'fibonacci', stop reason = breakpoint 1.1
    frame #0: fibonacci`main(argc=1, argv=0x00007fffffffdeb8) at fibonacci.c:10
   7      unsigned long int sum;
   8
   9      while (b < LONG_MAX) {
-> 10       printf("%ld ", b);
   11       sum = a + b;
   12       a = b;
   13       b = sum;
Copy to Clipboard Toggle word wrap

Execution of the program stops at the breakpoint set in Example 3.2, “Setting a New Breakpoint”.

3.7. Displaying Current Program Data

The lldb tool enables you to display data relevant to the program state, including:

  • Variables of any complexity
  • Any valid expressions
  • Function call return values

The common usage is to display the value of a variable. To display the current value of a certain variable:

(lldb) print variable_name
Copy to Clipboard Toggle word wrap

Example 3.6. Displaying the Current Values of Variables

This example assumes that you have successfully followed the instructions in Example 3.5, “Executing the fibonacci Binary File in lldb”. Execution of the fibonacci binary stopped after reaching the breakpoint at line 10.

Display the current values of variables a and b:

(lldb) print a
$0 = 0
(lldb) print b
$1 = 1
Copy to Clipboard Toggle word wrap

3.8. Continuing Execution after a Breakpoint

To resume the execution of the program you are debugging after it reached a breakpoint:

(lldb) continue
Copy to Clipboard Toggle word wrap

The execution stops again when it reaches another breakpoint.

To skip a certain number of breakpoints, typically when you are debugging a loop, run the continue command in the following form:

(lldb) continue -i number_of_breakpoints_to_skip
Copy to Clipboard Toggle word wrap
Note

If the breakpoint is set on a loop, in order to skip the whole loop, you will have to set the number_of_breakpoints_to_skip to match the loop iteration count.

The lldb tool enables you to execute a single line of code from the current line pointer with step:

(lldb) step
Copy to Clipboard Toggle word wrap

To execute a certain number of lines:

(lldb) step -c number
Copy to Clipboard Toggle word wrap

Example 3.7. Continuing the Execution of the fibonacci Binary File after a Breakpoint

This example assumes that you have successfully followed the instructions in Example 3.5, “Executing the fibonacci Binary File in lldb”. The execution of the fibonacci binary stopped after reaching the breakpoint at line 10.

Resume the execution:

(lldb) continue
Process 21580 resuming
Process 21580 stopped
* thread #1, name = 'fibonacci', stop reason = breakpoint 1.1
    frame #0: fibonacci`main(argc=1, argv=0x00007fffffffdeb8) at fibonacci.c:10
   7      unsigned long int sum;
   8
   9      while (b < LONG_MAX) {
-> 10       printf("%ld ", b);
   11       sum = a + b;
   12       a = b;
   13       b = sum;
Copy to Clipboard Toggle word wrap

The execution stops the next time it reaches a breakpoint. (In this case it is the same breakpoint.) Execute the next three lines of code:

(lldb) step -c 3
Process 21580 stopped
* thread #1, name = 'fibonacci', stop reason = step in
    frame #0: fibonacci`main(argc=1, argv=0x00007fffffffdeb8) at fibonacci.c:11
   8
   9      while (b < LONG_MAX) {
   10       printf("%ld ", b);
-> 11       sum = a + b;
   12       a = b;
   13       b = sum;
   14     }
Copy to Clipboard Toggle word wrap

Verify the current value of the sum variable:

(lldb) print sum
$2 = 2
Copy to Clipboard Toggle word wrap

3.9. Additional Resources

A detailed description of the lldb debugger and all its features is beyond the scope of this book. For more information, see the resources listed below.

Online Documentation

See Also

맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat