C++ Debugging - How To Use GDB [Code + Command Line Walkthrough] (2024)

Introduction - Debugging?

Debugging is necessary for any program of any size. It's inevitable that our code will return incorrect results, and we'll want to know what values are along the way to see where things are going wrong. For Linux, "gdb" (GNU Debugger) is one tool used for debugging C and C++ code. It can be used for larger programs, but we're going to keep things simple in this tutorial by showing how to use gdp with basic commands, for a simple program.

Installation

We'll assume that you're running Ubuntu 16.04 for this gdb installation, but the requirements are very basic. (We'll also assume that you have GCC installed to compile our C++ programs.

Run the following command in your terminal to install gdb and necessary dependencies:

sudo apt-get install libc6-dbg gdb valgrind 

And that's it.

A Basic C++ Program (With Some Bugs)

We're going to create a basic C++ program, and it's going to have a few bugs. We'll need to go through the code (with a debugger) to see why we aren't getting the expected result.

First of all, in your project directory, use the the following commands to create a makefile and our cpp file.

touch main.cpptouch makefile

Open "makefile" and add the following code to it:

1234567
all: g++ main.cpp -g -o run ./runcompile: g++ main.cpp -g -o runrun: ./run

(If you don't know about makefiles, look at this quick tutorial.)

Notice the extra -g flag for our compile command. This will tell the compiler to leave our code intact in the executable for debugging.

Our C++ program will ask the user for a number, and will calculate the factorial of that number.

​Open "main.cpp" and add the following code to it.

 1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930313233
// Copyright srcmake.com 2018.// A simple C++ with a bug or two in it.#include <iostream>using namespace std;int main() { // A factorial program to calculate n!. cout << "Enter a number, and we'll calculate the factorial of it.\n"; cout << "Number: "; int n; cin >> n; // The result of n!. Holds the running product of multiplying 1 to n. int factorial; // Go through each number from 1 to n. for(int i = 1; i < n; i++) { factorial = factorial * i; } // Output the result. cout << n << "! is " << factorial << ".\n"; /* Two noteable bugs: 1. "factorial" is never initialized. 2. "factorial" is muliplied from 1 to (n-1). It's not multiplied by n. */ return 0; }

Our code is complete.

​In your terminal, run "make" to test the code out.

make

Obviously, the answer our program outputs is wrong. Why is it wrong? Well, we could inspect the code to find out, but we could also use gdb to see what's happening in our code.

C++ Debugging - How To Use GDB [Code + Command Line Walkthrough] (1)

Using gdb to Debug The Program

We're going to use gdb to debug our program. Let's go through the commands that we need.

First of all, start gdb. (This still start a gdb shell session.)

gdb

​Next, we're going to tell gdb that our executable file (named "run") is the one we want to debug.

file run

Now we're going to set a breakpoint at main. (A breakpoint is basically where we tell our code "stop here so we can look at it".)

break main

Next, we start the program (executable) to see what's going on in the code.

run

We're going to say "next" (or "n") to go through the code line by line (as it's executed, not necessarily as it's written. For example, for loops will be called a lot).

next

At any time, to see the value of a variable, you can use "p variablename".

p factorial

This whole process will look something like this.

C++ Debugging - How To Use GDB [Code + Command Line Walkthrough] (2)

To quit out of the gdb shell sessions.

quit

And that's it. Use gdb to find out what's going on in your code.

Conclusion

In this tutorial, we had a simple C++ program, with a few bugs. To find out what was going on in our code, we used gdb to go through the code line by line to see what the values of variables were and how the code was executing.

We did a basic tutorial, but gdb can be used for much larger examples. To proceed further, familiarize yourself with all of the gdb commands, and use them as you need to for your own program.

Like this content and want more? Feel free tolook around and find another blog post that interests you. You can also contact me through one of the various social media channels.

Twitter:@srcmake
Discord:srcmake#3644
Youtube:srcmake
Twitch:www.twitch.tv/srcmake
​Github:srcmake

C++ Debugging - How To Use GDB [Code + Command Line Walkthrough] (2024)

FAQs

How to use GDB debugger command line? ›

Let's learn by doing: –
  1. Start GDB. Go to your Linux command prompt and type “gdb”. ...
  2. Compile the code. Below is a program that shows undefined behavior when compiled using C99. ...
  3. Run GDB with the generated executable. ...
  4. Display the code. ...
  5. Set a breakpoint. ...
  6. View breakpoints. ...
  7. Disable a breakpoint. ...
  8. Re-enable a disabled breakpoint.
Jul 12, 2024

How to use GDB to step through code? ›

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

How to debug a C++ file in terminal? ›

For the C++ (GDB/LLDB) debugging environment, you can execute GDB, LLDB and LLDB-MI commands directly through the debug console with the -exec command, but be careful, executing commands directly in the debug console is untested and might crash VS Code in some cases.

How to debug using command line? ›

To debug applications on a running correlator, run the following command: engine_debug [ [ global-options ] [command [options]] ... ] To obtain a usage message, run the command with the help option. Sending events to the correlator continues to put events on the input queue of each public context.

How do I run a debugger code? ›

To run or debug a simple app in VS Code, select Run and Debug on the Debug start view or press F5 and VS Code will try to run your currently active file. However, for most debugging scenarios, creating a launch configuration file is beneficial because it allows you to configure and save debugging setup details.

How to do remote debugging in GDB? ›

To start remote debugging, run GDB on the host machine, and specify as an executable file the program that is running in the remote machine. This tells GDB how to find your program's symbols and the contents of its pure text. Start GDB on the host, and connect to the target (see section Connecting to a remote target).

How do I debug a running process in GDB? ›

Procedure
  1. Start GDB attached to the application or library you want to debug. ...
  2. Exit GDB without proceeding further: type q and Enter . ...
  3. Run the command suggested by GDB to install the needed debuginfo packages: ...
  4. In case GDB is not able to suggest the debuginfo package, follow the procedure in Section 20.1.

How to debug C program using GDB in 6 simple steps? ›

How to Debug C Program using gdb in 6 Simple Steps
  1. Write a sample C program with errors for debugging purpose. ...
  2. Compile the C program with debugging option -g. ...
  3. Launch gdb. ...
  4. Set up a break point inside C program. ...
  5. Execute the C program in gdb debugger. ...
  6. Printing the variable values inside gdb debugger.
Sep 28, 2018

How to set commands in GDB? ›

To create a user-defined command, we use the GDB command define , and give it a command name, which in our example is bugreport followed by a set of GDB commands that you want to execute or capture the output from.

How to call a function from GDB? ›

What does it mean to call a C function from gdb?
  1. pause the program (because it is already running code!)
  2. find the address of the function you want to call (using the symbol table)
  3. convince the program (the “target program”) to jump to that address.
Jan 4, 2018

What does GDB stand for? ›

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.

How to debug the code in C++? ›

It is the most basic feature in debugging.
  1. To set the breakpoint, click in the gutter to the left of the doWork function call (or select the line of code and press F9).
  2. Now press F5 (or choose Debug > Start Debugging). The debugger pauses where you set the breakpoint.
Jan 11, 2024

How to compile and use GDB? ›

Basic Usage. All program to be debugged in gdb must be compiled by gcc with the option "-g" turning on. Continue with the "garbage" example, if we want to debug the program "garbage", we can simply start gdb by: gdb ./garbage.

Does GDB work with G++? ›

Most installations of the GNU c++ compiler (g++) also include the GNU debugger, GDB. This page is meant to be a guide to doing some basic debugging with GDB. Be aware that there are many other features available, this is just a basic introduction.

How to pass command line arguments with GDB? ›

You can optionally have gdb pass any arguments after the executable file to the inferior using --args . This option stops option processing. This will cause gdb to debug gcc , and to set gcc 's command-line arguments (see Arguments) to ' -O2 -c foo. c '.

How do I run debug command line in go? ›

To debug a program, execute the dlv debug command. Attach the filename at the end of this command, and debugging will start. For example, if you want to debug the main.go file, run the command dlv debug main.go . It is also known as “delve server” because this is a running process waiting for instructions.

How to use GDB debugger with GCC? ›

Basic Usage

All program to be debugged in gdb must be compiled by gcc with the option "-g" turning on. Continue with the "garbage" example, if we want to debug the program "garbage", we can simply start gdb by: gdb ./garbage.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Madonna Wisozk

Last Updated:

Views: 6452

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.