C gdb Commands 🎯

beginner
19 min

C gdb Commands 🎯

Welcome to our comprehensive guide on C gdb commands! This tutorial is designed for both beginners and intermediate learners who wish to master debugging C programs using the GNU Debugger (gdb). Let's dive in!

What is gdb? 📝

GNU Debugger (gdb) is a powerful debugging tool used in C programming to help identify and fix errors (bugs) in your code. It allows you to step through your program line by line, inspect variables, and modify data while the program is running.

Installing gdb 💡

On most Linux distributions, gdb is pre-installed. For Windows and MacOS, you can download it from the official GNU website.

Basic gdb Commands ✅

Starting gdb

bash
gdb your_program

Replace your_program with the name of your C program.

Running the Program

bash
run

Quitting gdb

bash
quit

Navigating Through Your Program 💡

Continuing Execution

bash
continue

Stepping Over (Next Line)

bash
next

Stepping Into (Next Function Call)

bash
step

Displaying Variables

bash
print variable_name

Breakpoints 💡

Setting a Breakpoint

bash
break line_number

Listing Breakpoints

bash
info breakpoints

Deleting a Breakpoint

bash
delete breakpoint_number

Quiz 📝

Quick Quiz
Question 1 of 1

What command is used to continue execution after a breakpoint is hit?

Advanced gdb Features 💡

Source Code Navigation

bash
list

Viewing Call Stack

bash
backtrace

Setting Watchpoints

bash
watch variable_name

Code Example 🎯

Here's a simple C program demonstrating some of the discussed gdb commands:

c
#include <stdio.h> int main() { int a = 5; int b = 10; printf("Sum: %d\n", a + b); return 0; }

Save this code as example.c and compile it using the command:

bash
gcc example.c -g

The -g flag enables debugging information for gdb. Now, let's debug the program using gdb:

bash
gdb a.out

Once in gdb, you can execute the following commands to navigate through the program:

bash
list break 6 run next print a next print b next

This sequence of commands will take you step by step through the program, showing you the current state of a and b after each line execution. You can experiment with different gdb commands to explore the program further.

Happy debugging! 😃