• C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management

Open In App

  • GDB (Step by Step Introduction)
  • Introduction to TensorFlow
  • Introduction to CUDA Programming
  • Introduction to PostgreSQL PL/pgSQL
  • Introduction to Google Cloud Platform
  • Introduction to Google Cloud Bigtable
  • Introduction to Google Compute Engine
  • Introduction of Unified Database Language (UDL)
  • Introduction of Relational Model and Codd Rules in DBMS
  • Google Cloud Platform - Introduction to Cloud Spanner
  • Google Cloud Platform - User Defined Functions in BigQuery
  • Introduction to Google Cloud Trace and Google Cloud Debugger
  • Morgan Stanley Interview | Set 22 (For PPO)
  • Morgan Stanley Interview | Set 16 (On-Campus)
  • Expedia Interview | Set 4 (On-Campus)
  • The Ultimate Beginner's Guide For DSA
  • Cadence India Interview | Set 1
  • Getting started with Databases
  • OATS Systems Interview Experience | Set 1 (On-Campus)
  • Amazon Interview | Set 50 (On-campus for SDE)

Last Updated : 22 Dec, 2023

Improve

Improve

Like Article

Like

Save

Report

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

GDB (Step by Step Introduction) - GeeksforGeeks (1)

uname -a

Let’s learn by doing: –

Start GDB

Go to your Linux command prompt and type “gdb”.

gdb

GDB (Step by Step Introduction) - GeeksforGeeks (2)

gdb

Gdb open prompt lets you know that it is ready for commands. To exit out of gdb, type quit or q.

GDB (Step by Step Introduction) - GeeksforGeeks (3)

To quit

Compile the code

Below is a program that shows undefined behavior when compiled using C99. GDB (Step by Step Introduction) - GeeksforGeeks (4)

Note: 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

GDB (Step by Step Introduction) - GeeksforGeeks (5)

gcc -std=c99 -g -o test test.C

Run GDB with the generated executable

Type the following command to start GDB with the compiled executable.

gdb ./test

GDB (Step by Step Introduction) - GeeksforGeeks (6)

gdb ./test

Useful GDB commands:

Here are a few useful commands to get started with GDB.

CommandDescription
run or rExecutes the program from start to end.
break or bSets a breakpoint on a particular line.
disableDisables a breakpoint
enableEnables a disabled breakpoint.
next or nExecutes the next line of code without diving into functions.
stepGoes to the next instruction, diving into the function.
list or lDisplays the code.
print or pDisplays the value of a variable.
quit or qExits out of GDB.
clearClears all breakpoints.
continueContinues normal execution

Display the code

Now, type “l” at gdb prompt to display the code.

GDB (Step by Step Introduction) - GeeksforGeeks (7)

Display the code

Set a breakpoint

Let’s introduce a break point, say line 5.

GDB (Step by Step Introduction) - GeeksforGeeks (8)

Set a breakpoint

If 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”.

GDB (Step by Step Introduction) - GeeksforGeeks (9)

View breakpoints

Disable a breakpoint

Having done the above, let’s say you changed your mind and you want to revert. Type “disable b”.

GDB (Step by Step Introduction) - GeeksforGeeks (10)

Disable a breakpoint

Re-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”.

GDB (Step by Step Introduction) - GeeksforGeeks (11)

Re-enable a disabled breakpoint

Run 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.

GDB (Step by Step Introduction) - GeeksforGeeks (12)

Run the code

Print variable values

To see the value of variable, type “print variable_name or p variable_name“.

GDB (Step by Step Introduction) - GeeksforGeeks (13)

Print variable values

The 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.

GDB (Step by Step Introduction) - GeeksforGeeks (14)

Debugging output

GDB 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.



T

thelittleguy046

Improve

Next Article

Introduction to TensorFlow

Please Login to comment...

Similar Reads

How to find Segmentation Error in C & C++ ? (Using GDB) What is Segmentation Error ? - It is the runtime error caused because of the memory access violation. For Eg :-Stackoverflow, read violation etc.. We often face this problem when working out with pointers in c++/c. In this example we will see how to find the segmentation error in the program. We will find which lines causes the segmentation fault e 3 min read Analyzing BufferOverflow with GDB Pre-requisite: GDB (Step by Step Introduction) A BufferOverflow often occurs when the content inside the defined variable is copied to another variable without doing Bound Checks or considering the size of the buffer. Let's analyze buffer overflow with the help GNU Debugger (GDB) which is inbuilt every Linux system. The motive of this exercise is t 3 min read Step by Step guide to install Intellij Idea IntelliJ is an integrated development environment(IDE) written in Java. It is used for developing computer software. This IDE is developed by Jetbrains and is available as an Apache 2 Licensed community edition and a commercial edition. In this article, we will see how to install IntelliJ Idea for the different operating systems . Installation on M 2 min read A Step by Step Guide for Placement Preparation | Set 2 Prerequisite - Set 1 Tech giants look for candidates who generally are excellent in algorithm designing and coding. You can surely not skip data structures and algorithms if you are looking to prepare for companies like Google, Facebook, Microsoft, Amazon, etc. Revise the working and use of popular Data Structures: Apart from set 1, more data struc 3 min read A Step by Step Guide for Placement Preparation | Set 1 Campus placements season is beginning at almost all the colleges and each one of us wants to prepare to do our best. A lot of students have been asking on different forums "How to use GeeksforGeeks for placement preparation"? In this article, a step-by-step guide for placement preparation is discussed. Steps:Aptitude TestCourse materialPractice Tes 3 min read How to use Flatpak? Step-By-Step Guide Flatpak is a framework for creating desktop packages in many Linux distributions. It has been developed by users with extensive history of working with the Linux computer device and runs as an unbiased open-source project. Flatpak’s strength lies in its up-to-date approach to software software delivery. It removes traditional constraints and provid 4 min read Count ways to reach the Nth stair using any step from the given array Given N stairs and a person standing at the bottom wants to reach the top. He could climb any number of steps from the given array arr[] of positive integers. The task is to find the count of all possible ways to reach the top. Examples: Input: arr[] = {1, 3, 5}, N = 5 Output: 5 (0 -> 1 -> 2 -> 3 -> 4 -> 5), (0 -> 1 -> 2 -> 10 min read Step Wise Guide to start with Open Source OPEN SOURCE CONTRIBUTION What exactly is Open Source Software? It is definitely not correct when someone says that free software is an open source software. An open source software may or may not be free software. Open source software is one with free redistribution, open source code where one can do some tinkering and change stuff based on require 2 min read Google Summer Trainee Engineering Program(STEP) Interview Experience Telephonic Interview 1. (Telephonic + Google Docs Shared) Q1. Given an array of integers, you need to find the local maxima. Example : [1 3 5 4 7 10 6] Output: 5 or 10 Explanation: Any of the local maxima can be the output. Here 5 is greater than 3 and 4, 10 is greater than 7 and 6. Hint : Refer this article. Q2. Given a sequence of brackets, how w 2 min read Number of steps to sort the array by changing order of three elements in each step Given an array arr[] of size N consisting of unique elements in the range [0, N-1], the task is to find K which is the number of steps required to sort the given array by selecting three distinct elements and rearranging them. And also, print the indices selected in those K steps in K lines. For example, in the array {5, 4, 3, 2, 1, 0}, one possibl 15+ min read

Article Tags :

  • GCC
  • linux-command
  • C Language
  • C Programs

Practice Tags :

  • CPP
  • Misc

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

GDB (Step by Step Introduction) - GeeksforGeeks (15)

GDB (Step by Step Introduction) - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 6008

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.