Using LLDB

In this recipe, you will learn how to use the debugger known as LLDB, provided by LLVM. LLDB is a next-generation, high-performance debugger. It is essentially built as a set of reusable components that have advantages over the existing libraries in the larger LLVM project. You might find it quite similar to the gdb debugging tool.

Getting ready

We will need the following before working with LLDB:

  1. To use LLDB, we need to check out the LLDB source code in the llvm/tools folder:
    svn co http://llvm.org/svn/llvm-project/lldb/trunk lldb
    
  2. Build and install LLVM, which will also build LLDB simultaneously.

How to do it…

Perform the following steps:

  1. Write a test case for a simple example using LLDB:
    $ cat lldbexample.c
    #include<stdio.h>
    int globalvar = 0;
    
    int func2(int a, int b) {
    globalvar++;
    return a*b;
    }
    
    int func1(int a, int b) {
    globalvar++;
    int d = a + b;
    int e = a - b;
    int f = func2(d, e);
    return f;
    }
    
    int main() {
    globalvar++;
    int a = 5;
    int b = 3;
    
    int c = func1(a,b);
    printf("%d", c);
    return c;
    }
    
  2. Compile the code using Clang with the –g flag to generate the debug information:
    $ clang -g lldbexample.c
    
  3. Debug the output file generated in the previous file with LLDB. To load the output file, we need to pass its name to LLDB:
    $ lldb a.out
    (lldb) target create "a.out"
    Current executable set to 'a.out' (x86_64).
    
  4. Set a breakpoint in the main function:
    (lldb) breakpoint set --name main
    Breakpoint 1: where = a.out'main + 15 at lldbexample.c:20, address = 0x00000000004005bf
    
  5. To look at the list of breakpoints set, use the following command:
    (lldb) breakpoint list
    Current breakpoints:
    1: name = 'main', locations = 1
      1.1: where = a.out'main + 15 at lldbexample.c:20, address = a.out[0x00000000004005bf], unresolved, hit count = 0
    
  6. Add a command to be executed when a breakpoint is hit. Here, let's add the back trace bt command when the breakpoint on the main function is hit:
    (lldb) breakpoint command add 1.1
    Enter your debugger command(s).  Type 'DONE' to end.
    > bt 
    > DONE
    
  7. Run the executable using the following command. This will hit the breakpoint on the main function and execute the back trace(bt) command, as set in the earlier step:
    (lldb) process launch
    Process 2999 launched: '/home/mayur/book/chap9/a.out' (x86_64)
    Process 2999 stopped
    * thread #1: tid = 2999, 0x00000000004005bf a.out'main + 15 at lldbexample.c:20, name = 'a.out', stop reason = breakpoint 1.1
        frame #0: 0x00000000004005bf a.out'main + 15 at lldbexample.c:20
       17
       18
       19    int main() {
    -> 20    globalvar++;
       21    int a = 5;
       22    int b = 3;
       23
    (lldb)  bt
    * thread #1: tid = 2999, 0x00000000004005bf a.out'main + 15 at lldbexample.c:20, name = 'a.out', stop reason = breakpoint 1.1
      * frame #0: 0x00000000004005bf a.out'main + 15 at lldbexample.c:20
        frame #1: 0x00007ffff7a35ec5 libc.so.6'__libc_start_main(main=0x00000000004005b0, argc=1, argv=0x00007fffffffda18, init=<unavailable>, fini=<unavailable>, rtld_fini=<unavailable>, stack_end=0x00007fffffffda08) + 245 at libc-start.c:287
        frame #2: 0x0000000000400469 a.out
    
  8. To set watchpoint on the global variable, use the following command:
    (lldb) watch set var globalvar
    Watchpoint created: Watchpoint 1: addr = 0x00601044 size = 4 state = enabled type = w
        declare @ '/home/mayur/book/chap9/lldbexample.c:2'
        watchpoint spec = 'globalvar'
        new value: 0
    
  9. To stop the execution when the value of globalvar becomes 3, use the watch command:
    (lldb) watch modify -c '(globalvar==3)'
    To view list of all watch points:
    (lldb) watch list
    Number of supported hardware watchpoints: 4
    Current watchpoints:
    Watchpoint 1: addr = 0x00601044 size = 4 state = enabled type = w
        declare @ '/home/mayur/book/chap9/lldbexample.c:2'
        watchpoint spec = 'globalvar'
        new value: 0
        condition = '(globalvar==3)'
    
  10. To continue execution after the main function, use the following command. The executable will stop when the value of globalvar becomes 3, inside the func2 function:
    (lldb) thread step-over
    (lldb) Process 2999 stopped
    * thread #1: tid = 2999, 0x000000000040054b a.out'func2(a=8, b=2) + 27 at lldbexample.c:6, name = 'a.out', stop reason = watchpoint 1
        frame #0: 0x000000000040054b a.out'func2(a=8, b=2) + 27 at lldbexample.c:6
       3
       4     int func2(int a, int b) {
       5     globalvar++;
    -> 6     return a*b;
       7     }
       8
       9
    
    Watchpoint 1 hit:
    old value: 0
    new value: 3
    (lldb) bt
    * thread #1: tid = 2999, 0x000000000040054b a.out'func2(a=8, b=2) + 27 at lldbexample.c:6, name = 'a.out', stop reason = watchpoint 1
      * frame #0: 0x000000000040054b a.out'func2(a=8, b=2) + 27 at lldbexample.c:6
        frame #1: 0x000000000040059c a.out'func1(a=5, b=3) + 60 at lldbexample.c:14
        frame #2: 0x00000000004005e9 a.out'main + 57 at lldbexample.c:24
        frame #3: 0x00007ffff7a35ec5 libc.so.6'__libc_start_main(main=0x00000000004005b0, argc=1, argv=0x00007fffffffda18, init=<unavailable>, fini=<unavailable>, rtld_fini=<unavailable>, stack_end=0x00007fffffffda08) + 245 at libc-start.c:287
        frame #4: 0x0000000000400469 a.out
    
  11. To continue the execution of the executable use the thread continue command, which will execute till the end as no other breakpoints are met:
    (lldb) thread continue
    Resuming thread 0x0bb7 in process 2999
    Process 2999 resuming
    Process 2999 exited with status = 16 (0x00000010)
    
  12. To exit LLDB, use the following command:
    (lldb)  exit
    

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.128.145