GDB (Step by Step Introduction)
Last Updated :
10 Jan, 2025
GDB stands for GNU Project Debugger and is a powerful debugging tool for C (along with other languages like C++). It helps you to poke around inside your C programs while they are executing and also allows you to see what exactly happens when your program crashes. GDB operates on executable files which are binary files produced by the compilation process.
For demo purposes, the example below is executed on a Linux machine with the below specs.
uname -a
uname -a
Let's learn by doing: -
Start GDB
Go to your Linux command prompt and type "gdb".
gdb
gdb Gdb open prompt lets you know that it is ready for commands. To exit out of gdb, type quit or q.
To quitCompile the code
Below is a program that shows undefined behavior when compiled using C99. [caption id="attachment_646191" align="alignnone"]
cat test.cNote: If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate, where the indeterminate value is either an unspecified value or a trap representation.
Now compile the code. (here test.c). g flag means you can see the proper names of variables and functions in your stack frames, get line numbers and see the source as you step around in the executable. -std=C99 flag implies use standard C99 to compile the code. -o flag writes the build output to an output file.
gcc -std=c99 -g -o test test.C
gcc -std=c99 -g -o test test.CRun GDB with the generated executable
Type the following command to start GDB with the compiled executable.
gdb ./test
gdb ./testUseful GDB commands:
Here are a few useful commands to get started with GDB.
Command | Description |
---|
run or r | Executes the program from start to end. |
---|
break or b | Sets a breakpoint on a particular line. |
---|
disable | Disables a breakpoint |
---|
enable | Enables a disabled breakpoint. |
---|
next or n | Executes the next line of code without diving into functions. |
---|
step | Goes to the next instruction, diving into the function. |
---|
list or l | Displays the code. |
---|
print or p | Displays the value of a variable. |
---|
quit or q | Exits out of GDB. |
---|
clear | Clears all breakpoints. |
---|
continue | Continues normal execution |
---|
Display the code
Now, type "l" at gdb prompt to display the code.
Display the codeSet a breakpoint
Let's introduce a break point, say line 5.
Set a breakpointIf you want to put breakpoint at different lines, you can type "b line_number".By default "list or l" display only first 10 lines.
View breakpoints
In order to see the breakpoints, type "info b".
View breakpointsDisable a breakpoint
Having done the above, let's say you changed your mind and you want to revert. Type "disable b".
Disable a breakpointRe-enable a disabled breakpoint
As marked in the blue circle, Enb becomes n for disabled. 9. To re-enable the recent disabled breakpoint. Type "enable b".
Re-enable a disabled breakpointRun the code
Run the code by typing "run or r".If you haven't set any breakpoints, the run command will simply execute the full program.
Run the codePrint variable values
To see the value of variable, type "print variable_name or p variable_name".
Print variable valuesThe above shows the values stored at x at time of execution.
Change variable values
To change the value of variable in gdb and continue execution with changed value, type "set variable_name".
Debugging output
Below screenshot shows the values of variables from which it's quite understandable the reason why we got a garbage value as output. At every execution of ./test we will be receiving a different output.
Exercise: Try using set x = 0 in gdb at first run and see the output of c.
Debugging outputGDB offers many more ways to debug and understand your code like examining stack, memory, threads, manipulating the program, etc. I hope the above example helps you get started with gdb.
Conclusion
In this article we have discussed GDB (GNU Debugger) which is a powerful tool in Linux used for debugging C programs. We have discussed some of the following steps so that we can compile your code with debugging information, run GDB, set breakpoint, examine variables, and analyze program behavior. We have also discussed GDB's features, such as code examination, breakpoint management, variable manipulation, and program execution control which allow us to efficiently debug and issue resolution.
Similar Reads
C Functions Practice Problems Functions are the basic building block of C programs. They enhance the modularity and code reusability by separating the logic of a particular task from the main code and using it whenever required. Functions are extensively used in almost all programs, so it is best for programmers to be familiar i
2 min read
C Fundamental Practice Problems Fundamentals concepts teach you the absolute basics of the programming. It is the bare minimum that you should know about the programming language to create basic programs. So, it is very important to have good understanding of the fundamentals to have strong foundationSolving practice problems is t
2 min read
C Exercises - Practice Questions with Solutions for C Programming The best way to learn C programming language is by hands-on practice. This C Exercise page contains the top 30 C exercise questions with solutions that are designed for both beginners and advanced programmers. It covers all major concepts like arrays, pointers, for-loop, and many more.So, Keep it Up
12 min read
Your First C Program Like in most of the programming languages, program to write the text "Hello, World!" is treated as the first program to learn in C. This step-by-step guide shows you how to create and run your first C program.Table of ContentSetting Up Your EnvironmentCreating a Source Code FileNavigating to the Sou
4 min read
C Programs To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and
8 min read
C Hello World Program The âHello Worldâ program is the first step towards learning any programming language. It is also one of the simplest programs that is used to introduce aspiring programmers to the programming language. It typically outputs the text "Hello, World!" to the console screen.C Program to Print "Hello Wor
1 min read
Go Programming Language (Introduction) Go is a procedural programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language. Programs are assembled by using packages, for efficient management of dependencies. This language also supports env
11 min read
Steps to Install Geany IDE Geany is a lightweight, high-performance Integrated Development Environment system that supports the most used programming languages, i.e C/C++, Java, Python2.x/3/x, etc. It was developed in such a manner that it has the least dependency on other packages, thus it is quite fast, as compared to code-
4 min read
Structure of the C Program The basic structure of a C program is divided into 6 parts which makes it easy to read, modify, document, and understand in a particular format. C program must follow the below-mentioned outline in order to successfully compile and execute. Debugging is easier in a well-structured C program.Sections
5 min read
C++ Function Practice Problems Functions are the basic building block of the program. They are the block of code that performs a specific task. Function can be executed from anywhere in the program any number of times. They increase the modularity and reusability of the code.Practicing problems is one of the most effective ways t
2 min read