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.
We will need the following before working with LLDB:
llvm/tools
folder:svn co http://llvm.org/svn/llvm-project/lldb/trunk lldb
Perform the following steps:
$ 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; }
–g
flag to generate the debug information:$ clang -g lldbexample.c
$ lldb a.out (lldb) target create "a.out" Current executable set to 'a.out' (x86_64).
(lldb) breakpoint set --name main Breakpoint 1: where = a.out'main + 15 at lldbexample.c:20, address = 0x00000000004005bf
(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
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
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
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
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)'
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
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)
(lldb) exit
3.133.128.145