Debugging C with Clang compiler and LLDB on MacOS
Introduction
Debugging is an important part of software development, which helps developers identify and fix errors in their code. Clang is an open-source C compiler that is used by many developers on macOS. In this blog post, we will discuss how to use Clang and LLDB (the debugger that comes with Clang) to debug C programs on macOS.
Prepare llvm
brew install llvm --with-clang --with-asan
Example
#include <stdio.h>
int main()
{
int x = 1;
int y = 5;
char *name = "Bob";
x += 1;
x += y;
printf("Name is %s\n", name);
printf("Number x is %d\n", x);
}
Compile
clang -g test.c -o test
Debug with lldb
lldb test
b 10
b main
run
br list
br del 1
p x
expr x = 5
f
Display current linebt
Using backtrace