• 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

Last Updated : 22 Dec, 2023

Improve

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

Software Developer (SDE) Interview/Placement Preparation Guide

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 Introduction to Parallel Programming with OpenMP in C++ Parallel programming is the process of breaking down a large task into smaller sub-tasks that can be executed simultaneously, thus utilizing the available computing resources more efficiently. OpenMP is a widely used API for parallel programming in C++. It allows developers to write parallel code easily and efficiently by adding simple compiler dir 5 min read

Article Tags :

  • C Language
  • C Programs
  • C++
  • Misc

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 (16)

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

FAQs

How do you go step by step in GDB? ›

To execute one line of code, type "step" or "s". If the line to be executed is a function call, gdb will step into that function and start executing its code one line at a time. If you want to execute the entire function with one keypress, type "next" or "n".

What is GDB in Linux? ›

GDB stands for the “Gnu DeBugger.” This is a powerful source-level debugging package that lets you see what is going on inside your program. You can step through the code, set breakpoints, examine and change variables, and so on. Like most Linux tools, GDB itself is command line driven, making it rather tedious to use.

What is the introduction of GDB? ›

Gdb is a debugger for C (and C++). It allows you to do things like run the program up to a certain point then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line.

How to run code in GDB? ›

Starting your program. Use the run command to start your program under GDB. You must first specify the program name (except on VxWorks) with an argument to GDB (see section Getting In and Out of GDB), or by using the file or exec-file command (see section Commands to specify files).

Top Articles
How to Access the Second Sea in Blox Fruits
War latest: Belarus leader urges Ukraine and Russia to end conflict; Ukraine 'captures' largest Russian town so far
Spasa Parish
Rentals for rent in Maastricht
159R Bus Schedule Pdf
Sallisaw Bin Store
Black Adam Showtimes Near Maya Cinemas Delano
Espn Transfer Portal Basketball
Pollen Levels Richmond
11 Best Sites Like The Chive For Funny Pictures and Memes
Things to do in Wichita Falls on weekends 12-15 September
Craigslist Pets Huntsville Alabama
Paulette Goddard | American Actress, Modern Times, Charlie Chaplin
What's the Difference Between Halal and Haram Meat & Food?
R/Skinwalker
Rugged Gentleman Barber Shop Martinsburg Wv
Jennifer Lenzini Leaving Ktiv
Justified - Streams, Episodenguide und News zur Serie
Epay. Medstarhealth.org
Olde Kegg Bar & Grill Portage Menu
Cubilabras
Half Inning In Which The Home Team Bats Crossword
Amazing Lash Bay Colony
Juego Friv Poki
Dirt Devil Ud70181 Parts Diagram
Truist Bank Open Saturday
Water Leaks in Your Car When It Rains? Common Causes & Fixes
What’s Closing at Disney World? A Complete Guide
Experience the Convenience of Po Box 790010 St Louis Mo
Fungal Symbiote Terraria
modelo julia - PLAYBOARD
Abby's Caribbean Cafe
Joanna Gaines Reveals Who Bought the 'Fixer Upper' Lake House and Her Favorite Features of the Milestone Project
Tri-State Dog Racing Results
Navy Qrs Supervisor Answers
Trade Chart Dave Richard
Lincoln Financial Field Section 110
Free Stuff Craigslist Roanoke Va
Stellaris Resolution
Wi Dept Of Regulation & Licensing
Pick N Pull Near Me [Locator Map + Guide + FAQ]
Crystal Westbrooks Nipple
Ice Hockey Dboard
Über 60 Prozent Rabatt auf E-Bikes: Aldi reduziert sämtliche Pedelecs stark im Preis - nur noch für kurze Zeit
Wie blocke ich einen Bot aus Boardman/USA - sellerforum.de
Infinity Pool Showtimes Near Maya Cinemas Bakersfield
Hooda Math—Games, Features, and Benefits — Mashup Math
Dermpathdiagnostics Com Pay Invoice
How To Use Price Chopper Points At Quiktrip
Maria Butina Bikini
Busted Newspaper Zapata Tx
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6674

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.