Learn C the Hard Way – Using LLDB

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.

screenshot_3676.png

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 ex3
(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:

(lldb) run
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
     2
     3   int main()

     4   {
-> 5     int age = 10;
    6     int height = 72;
    7
    8     printf(“I am %d years old.\n”, age);

Target 0: (ex3) stopped.

Step forward into function calls:

(lldb) step

Or step over them:

(lldb) next

Print expressions. For example:

(lldb) print age
(int) $1 = 10

Quit when done debugging:

(lldb) quit

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s