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

Last Updated : 12 Jul, 2024

Comments

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.

GDB (Step by Step Introduction) – FAQs

How to go step by step in GDB?

To step through a program in GDB, you can use the following commands:

  • step (or s): Step into functions.
  • next (or n): Step over functions.
  • continue (or c): Continue execution until the next breakpoint.

Example:

gdb ./myprogram
(gdb) break main
(gdb) run
(gdb) step
(gdb) next

What is the overview of GDB?

GDB (GNU Debugger) is a powerful debugging tool for C, C++, and other programming languages. It allows developers to see what is happening inside their programs while they are running or what the program was doing at the moment it crashed. GDB provides features like breakpoints, stepping through code, inspecting variables, and changing program execution flow.

How to start a process in GDB?

To start a process in GDB, follow these steps:

  1. Compile your program with the -g flag to include debugging information.
  2. Start GDB with your program.
  3. Run the program inside GDB.
gcc -g -o myprogram myprogram.c
gdb ./myprogram
(gdb) run

How to program with GDB?

To debug a program with GDB:

  1. Compile the program with debugging information.
  2. Start GDB.
  3. Set breakpoints, run the program, and use debugging commands.
gcc -g -o myprogram myprogram.c
gdb ./myprogram
(gdb) break main
(gdb) run
(gdb) step
(gdb) next
(gdb) print variable

How to generate core file in GDB?

To generate a core file in GDB:

  1. Enable core dumps in your shell session.
  2. Run your program until it crashes.
  3. A core file (core or core.<pid>) will be generated in the current directory.
  4. Load the core file into GDB for analysis.
ulimit -c unlimited
./myprogram
gdb ./myprogram core


T

thelittleguy046

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

Improve

Previous Article

Coriolis Interview Experience

Next Article

multimap empty() function in C++ STL

Please Login to comment...

GDB (Step by Step Introduction) - GeeksforGeeks (2024)
Top Articles
Business Income & Receipts Tax (BIRT) | Services
The Procrastinator's Guide to Disputing Your Philadelphia Property Tax Assessment
Public Opinion Obituaries Chambersburg Pa
Umbc Baseball Camp
Part time Jobs in El Paso; Texas that pay $15, $25, $30, $40, $50, $60 an hour online
Apex Rank Leaderboard
America Cuevas Desnuda
Bluegabe Girlfriend
Lost Pizza Nutrition
All Obituaries | Ashley's J H Williams & Sons, Inc. | Selma AL funeral home and cremation
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Tight Tiny Teen Scouts 5
Nashville Predators Wiki
Caresha Please Discount Code
“In my day, you were butch or you were femme”
Moonshiner Tyler Wood Net Worth
Panorama Charter Portal
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
NBA 2k23 MyTEAM guide: Every Trophy Case Agenda for all 30 teams
V-Pay: Sicherheit, Kosten und Alternativen - BankingGeek
Bella Bodhi [Model] - Bio, Height, Body Stats, Family, Career and Net Worth 
Craigslist Personals Jonesboro
Scheuren maar: Ford Sierra Cosworth naar de veiling
Craigslist Illinois Springfield
Valic Eremit
Craigs List Jonesboro Ar
Cpt 90677 Reimbursem*nt 2023
Synergy Grand Rapids Public Schools
Meet the Characters of Disney’s ‘Moana’
R Baldurs Gate 3
Eegees Gift Card Balance
Ryujinx Firmware 15
Why Are The French So Google Feud Answers
Phone number detective
Navigating change - the workplace of tomorrow - key takeaways
Craigslist Neworleans
About Us | SEIL
Etowah County Sheriff Dept
American Bully Xxl Black Panther
Ticket To Paradise Showtimes Near Marshall 6 Theatre
1v1.LOL Game [Unblocked] | Play Online
Jason Brewer Leaving Fox 25
Hkx File Compatibility Check Skyrim/Sse
Sallisaw Bin Store
Po Box 101584 Nashville Tn
Gon Deer Forum
Lesly Center Tiraj Rapid
Erica Mena Net Worth Forbes
Rubmaps H
Karen Kripas Obituary
Gainswave Review Forum
Cbs Scores Mlb
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 6425

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.