C Programming - Beginner to Brilliance / Common Errors and Debugging

Debugging with gdb

🎯 Learn to trace, inspect, and fix C programs like a professional using the gdb tool.


🧠 What is gdb?

gdb (GNU Debugger) is a powerful tool that lets you:

βœ… Pause execution of a program

βœ… Inspect variables and memory

βœ… Step through code line-by-line

βœ… Set breakpoints at any point

βœ… Trace bugs like segmentation faults

It's essential for C programmers β€” especially for runtime errors like crashes, invalid memory access, and logic bugs.


🧰 Getting Started with gdb

πŸ› οΈ Step 1: Compile with g flag

βœ… Purpose:

The -g flag tells the compiler to include debugging information in the compiled binary (myprogram). This debugging info maps source code lines to machine instructions, including:

  • Variable names

  • Line numbers

  • Function boundaries

  • Stack frames

πŸ” Why It's Important:

Without -g, the binary will run, but gdb won’t be able to give meaningful debugging information like variable values or exact source code lines.

This includes debugging information in the executable:

gcc -g program.c -o program

πŸ” Without -g, gdb won’t show variable names or source code lines.


🐞 Step 2: Launch gdb

βœ… Purpose:

This launches the GNU Debugger with your compiled binary (myprogram). In this interactive tool, you can:

  • Set breakpoints at specific lines or functions

  • Step through code line-by-line (next, step)

  • Inspect variables and memory

  • View the call stack when crashes or errors occur

  • Find segmentation faults and other bugs

gdb ./program

This will open an interactive terminal prompt like:

(gdb)

🧾 Resultant Outcome:

Action

Outcome

Compile with -g

Creates a binary with full debug symbols

Launch gdb

Opens a debugging environment

Set breakpoints

Pauses execution at desired points to inspect program state

Step execution

Helps you trace code logic, loops, and function calls line-by-line

Inspect variables

See the actual values of variables at runtime

Crash detection

Find exact location of segmentation faults, buffer overflows, etc.

πŸš€ Final Outcome:

Using these steps, you gain fine-grained control over your program's execution and can diagnose even complex bugs effectively. It turns your compiled program into an interactive, debuggable environment.


πŸ”§ Common gdb Commands (Must Know)

πŸ› οΈ Command

πŸ’‘ Purpose

run / r

Starts executing your program

break main / b main

Sets a breakpoint at the main() function

break 15

Sets a breakpoint at line number 15

next / n

Runs next line (without entering functions)

step / s

Steps into functions line-by-line

continue / c

Continues execution until next breakpoint

print var

Prints the value of variable var

list / l

Lists source code lines

backtrace / bt

Shows the function call stack

quit / q

Exits gdb


🎯 Sample Debugging Workflow

Let’s debug this simple C program with a bug:

You’ll now clearly see where and why it crashed: division by zero.


πŸ”¬ Advanced gdb Features

watch x

Pauses program execution whenever variable x is modified.

break 20 if x > 100

Breaks only if the condition is true β€” powerful for loops or large input cases.

πŸ“‚ If your program crashes and generates a core dump file:

gdb ./program core

This helps post-mortem analysis β€” you can inspect the exact line and variable states at crash time.


⚠️ gdb vs printf Debugging

βœ… Use printf() when...

🧠 Use gdb when...

You want to see quick variable output

Program crashes unexpectedly

Code is simple & small

You need to step through logic flow

For beginner tracing

Deep memory/pointer issues involved


πŸ§ͺ Bonus: gdb Shortcuts

πŸ“Œ Alias

πŸ”Ž Meaning

n

next

s

step

c

continue

q

quit

bt

backtrace

p var

print variable


πŸŽ“ Real-World Debugging with gdb

Scenario: A program crashes on user input.

Steps:

  1. Compile with g

  2. Reproduce the crash under gdb

  3. Use break, step, print, and backtrace

  4. Locate null pointer or logic flaw

  5. Fix the source, recompile, retest!