0% found this document useful (0 votes)
6 views3 pages

GDB Debugger

GDB (GNU Debugger) is a crucial tool for debugging programs in C and C++ on Linux, allowing users to trace execution, inspect variables, and analyze crashes. To use GDB, programs must be compiled with the -g flag for debugging symbols, and it provides various commands for running programs, setting breakpoints, and examining memory. Its importance is highlighted in embedded systems for debugging firmware logic and understanding memory issues.

Uploaded by

Sunny Patil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

GDB Debugger

GDB (GNU Debugger) is a crucial tool for debugging programs in C and C++ on Linux, allowing users to trace execution, inspect variables, and analyze crashes. To use GDB, programs must be compiled with the -g flag for debugging symbols, and it provides various commands for running programs, setting breakpoints, and examining memory. Its importance is highlighted in embedded systems for debugging firmware logic and understanding memory issues.

Uploaded by

Sunny Patil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

GDB Debugger

The GDB debugger (GNU Debugger) is a powerful tool used in Linux to debug programs written in
languages like C, C++, and others. It helps you understand what your program is doing while it runs—
or why it crashes.

What is GDB?

GDB (GNU Debugger) is part of the GNU Project and is widely used on Linux systems to:

• Debug runtime errors

• Trace program execution

• Inspect variables and memory

• Analyze crashes (segmentation faults)

Compile Program for Debugging

Before using GDB, compile your program with the -g flag:

gcc -g program.c -o program

This includes debugging symbols.

Starting GDB

gdb ./program

Basic GDB Commands

Run the Program

run

Set Breakpoints

break main // at main function


break 10 // at line 10
break function_name // at function

View Source Code

list
Step Execution

next // execute next line (skip function details)


step // go inside function

Print Variable Values

print x

Continue Execution

continue

Exit GDB

quit

Example Debugging

Sample Code (with bug)

#include <stdio.h>

int main() {

int a = 5, b = 0;

int c = a / b; // error: division by zero

printf("%d\n", c);

return 0;

Debug Steps

gcc -g test.c -o test

gdb ./test

(gdb) run

When it crashes:

(gdb) bt // backtrace
(gdb) print a
(gdb) print b

Useful Advanced Commands


Backtrace (Call Stack)

bt

Watch Variable Changes

watch x

Inspect Memory

x/10x &x

Change Variable Value

set var x = 10

Core Dump Debugging

If program crashes:

ulimit -c unlimited
./program // crash generates core file
gdb ./program core

Why GDB is Important (especially for you )

Since you're working on embedded systems and C/C++, GDB is essential for:

• Debugging firmware logic

• Tracking pointer errors

• Understanding memory issues

• Analyzing real-time behavior (with gdbserver in embedded targets)

You might also like