Skip to content

Instantly share code, notes, and snippets.

@gregmalcolm
Last active September 8, 2021 10:47
Show Gist options
  • Save gregmalcolm/9042234 to your computer and use it in GitHub Desktop.
Save gregmalcolm/9042234 to your computer and use it in GitHub Desktop.
Debugging C with llvm's clang and lldb

Assuming you llvm installed (comes as standard on Mac OS Mavrick xtools)

Create a helloworld.c file:

  #include<stdio.h>
  
  int main()
  {
    int x=3;
    char *name="bob";
    float y=4.3f;
    printf("Hello World\n");
  }%

Compile with -g option for extra debug info:

$ clang -g helloworld.c -o helloworld && ./helloworld

Debug:

$ lldb helloworld
Current executable set to 'helloworld' (x86_64).
(lldb) image dump line-table helloworld.c
Line table for /Users/greg/git/rust-me-presentation/code/helloworld.c in `helloworld
0x0000000100000f20: /Users/greg/git/rust-me-presentation/code/helloworld.c:4
0x0000000100000f3e: /Users/greg/git/rust-me-presentation/code/helloworld.c:5
0x0000000100000f45: /Users/greg/git/rust-me-presentation/code/helloworld.c:6
0x0000000100000f49: /Users/greg/git/rust-me-presentation/code/helloworld.c:7
0x0000000100000f4e: /Users/greg/git/rust-me-presentation/code/helloworld.c:8
0x0000000100000f5a: /Users/greg/git/rust-me-presentation/code/helloworld.c:9
0x0000000100000f65: /Users/greg/git/rust-me-presentation/code/helloworld.c:9
(lldb) b helloworld.c:8
Breakpoint 1: where = helloworld`main + 46 at helloworld.c:8, address = 0x0000000100000f4e
(lldb) b main
Breakpoint 2: where = helloworld`main + 30 at helloworld.c:5, address = 0x0000000100000f3e
(lldb) run
Process 2032 launched: '/Users/greg/git/rust-me-presentation/code/helloworld' (x86_64)
Process 2032 stopped
* thread #1: tid = 0x8abf, 0x0000000100000f3e helloworld`main + 30 at helloworld.c:5, queue = 'com.apple.main-thread, stop reason = breakpoint 6.1
    frame #0: 0x0000000100000f3e helloworld`main + 30 at helloworld.c:5
   2   	
   3   	int main()
   4   	{
-> 5   	  int x=3;
   6   	  char *name="bob";
   7   	  float y=4.3f;
   8   	  printf("Hello World\n");
(lldb)

... and so on.

The point of this exercise was to establish how to compile something with debugging info and to setup breakpoints in the c file in lldb.

@rickyrattlesnake
Copy link

oh man can't believe I forgot the -g command

@kennyeric2022
Copy link

Thanks@!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment