I am currently working my way through Zed Shaw‘s book Learn C the Hard Way which I first heard about on Scott Hanselman’s fantastic podcast Hanselminutes.
Apparently there seems to be some controversy over Zed’s content and style of teaching. As I am here to learn and not to judge, especially on a topic I am far away from being an expert in, I couldn’t care less.
Exercise 4 “Using a Debugger” is a brief introduction to using GDB and LLDB. As the book only lists a quick reference card and the accompanying video is a bit of a mess, I decided to put my notes into writing. With the sole focus on LLDB as I am working on macOS.
To make full use of LLDB this tiny piece of code has to be compiled using the -g flag:
gcc -Wall -g ex3.c -o ex3
Now just launch LLDB with the executable as argument:
(lldb) target create “ex3”
Current executable set to ‘ex3’ (x86_64).
Set a breakpoint in main and you are good to go:
(lldb) breakpoint set –name main
Breakpoint 1: where = ex3`main + 15 at ex3.c:5:7, address = 0x0000000100000f1f
Run your program:
Process 23621 launched: ‘/Users/frededison/Learn C/ex3’ (x86_64)
Process 23621 stopped
* thread #1, queue = ‘com.apple.main-thread’, stop reason = breakpoint 1.1
frame #0: 0x0000000100000f1f ex3`main at ex3.c:5:7
3 int main()
-> 5 int age = 10;
6 int height = 72;
8 printf(“I am %d years old.\n”, age);
Target 0: (ex3) stopped.
Step forward into function calls:
Or step over them:
Print expressions. For example:
(lldb) print age
(int) $1 = 10
Quit when done debugging:
A full tutorial on using LLDB can be found here. For me the few commands listed above are enough to proceed to Exercise 5 of Zed’s book.