Open In App

gdb command in Linux with examples

Improve
Improve
Like Article
Like
Save
Share
Report

gdb is the acronym for GNU Debugger. This tool helps to debug the programs written in C, C++, Ada, Fortran, etc. The console can be opened using the gdb command on terminal.

Syntax:

gdb [-help] [-nx] [-q] [-batch] [-cd=dir] [-f] [-b bps] [-tty=dev] [-s symfile] [-e prog] [-se prog] [-c core] [-x cmds] [-d dir] [prog[core|procID]]

Example:

The program to be debugged should be compiled with -g option. The below given C++ file that is saved as gfg.cpp. We are going to use this file in this article.




#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
  
int findSquare(int a)
{
    return a * a;
}
  
int main(int n, char** args)
{
    for (int i = 1; i < n; i++) 
    {
        int a = atoi(args[i]);
        cout << findSquare(a) << endl;
    }
    return 0;
}


Compile the above C++ program using the command:

g++ -g -o gfg gfg.cpp

To start the debugger of the above gfg executable file, enter the command gdb gfg. It opens the gdb console of the current program, after printing the version information.

  1. run [args] : This command runs the current executable file. In the below image, the program was executed twice, one with the command line argument 10 and another with the command line argument 1, and their corresponding outputs were printed.

  2. quit or q : To quit the gdb console, either quit or q can be used.
  3. help : It launches the manual of gdb along with all list of classes of individual commands.
  4. break : The command break [function name] helps to pause the program during execution when it starts to execute the function. It helps to debug the program at that point. Multiple breakpoints can be inserted by executing the command wherever necessary. b findSquare command makes the gfg executable pause when the debugger starts to execute the findSquare function.
    b
    break [function name]
    break [file name]:[line number]
    break [line number]
    break *[address]
    break ***any of the above arguments*** if [condition]
    b ***any of the above arguments*** 
    

    In the above example, the program that was being executed(run 10 100), paused when it encountered findSquare function call. The program pauses whenever the function is called. Once the command is successful, it prints the breakpoint number, information of the program counter, file name, and the line number. As it encounters any breakpoint during execution, it prints the breakpoint number, function name with the values of the arguments, file name, and line number. The breakpoint can be set either with the address of the instruction(in hexadecimal form preceded with *0x) or the line number and it can be combined with if condition(if the condition fails, the breakpoint will not be set) For example, break findSquare if a == 10.

  5. continue : This command helps to resume the current executable after it is paused by the breakpoint. It executes the program until it encounters any breakpoint or runs time error or the end of the program. If there is an integer in the argument(repeat count), it will consider it as the continue repeat count and will execute continue command “repeat count” number of times.
    continue [repeat count]
    c [repeat count]
    

  6. next or n : This command helps to execute the next instruction after it encounters the breakpoint.

    Whenever it encounters the above command, it executes the next instruction of the executable by printing the line in execution.

  7. delete : This command helps to deletes the breakpoints and checkpoints. If the delete command is executed without any arguments, it deletes all the breakpoints without modifying any of the checkpoints. Similarly, if the checkpoint of the parent process is deleted, all the child checkpoints are automatically deleted.
    d
    delete
    delete [breakpoint number 1] [breakpoint number 2] ...
    delete checkpoint [checkpoint number 1] [checkpoint number 2] ...
    

    In the above example, two breakpoints were defined, one at the main and the other at the findSquare. Using the above command findSquare breakpoint was deleted. If there is no argument after the command, the command deletes all the breakpoints.

  8. clear : This command deletes the breakpoint which is at a particular function with the name FUNCTION_NAME. If the argument is a number, then it deletes the breakpoint that lies in that particular line.
    clear [line number] 
    clear [FUNCTION_NAME]
    

    In the above example, once the clear command is executed, the breakpoint is deleted after printing the breakpoint number.

  9. disable [breakpoint number 1] [breakpoint number 2] …. : Instead of deleting or clearing the breakpoints, they can be disabled and can be enabled whenever they are necessary.
  10. enable [breakpoint number 1] [breakpoint number 2] …. : To enable the disabled breakpoints, this command is used.
  11. info : When the info breakpoints in invoked, the breakpoint number, type, display, status, address, the location will be displayed. If the breakpoint number is specified, only the information about that particular breakpoint will be displayed. Similarly, when the info checkpoints are invoked, the checkpoint number, the process id, program counter, file name, and line number are displayed.
    info breakpoints [breakpoint number 1] [breakpoint number 2] ... 
    info checkpoints [checkpoint number 1] [checkpoint number 2] ...
    

  12. checkpoint command and restart command : These command creates a new process and keep that process in the suspended mode and prints the created process’s process id.

    For example, in the above execution, the breakpoint is kept at function findSquare and the program was executed with the arguments “1 10 100”. When the function is called initially with a = 1, the breakpoint happens. Now we create a checkpoint and hence gdb returns a process id(4272), keeps it in the suspended mode and resumes the original thread once the continue command is invoked. Now the breakpoint happens with a = 10 and another checkpoint(pid = 4278) is created. From the info checkpoint information, the asterisk mentions the process that will run if the gdb encounters a continue. To resume a specific process, restart command is used with the argument that specifies the serial number of the process. If all the process are finished executing, the info checkpoint command returns nothing.

  13. set args [arg1] [arg2] … : This command creates the argument list and it passes the specified arguments as the command line arguments whenever the run command without any argument is invoked. If the run command is executed with arguments after set args, the arguments are updated. Whenever the run command is ran without the arguments, the arguments are set by default.

  14. show args : The show args prints the default arguments that will passed if the run command is executed. If either set args or run command is executed with the arguments, the default arguments will get updated, and can be viewed using the above show args command.

  15. display [/format specifier] [expression] and undisplay [display id1] [display id2] … : These command enables automatic displaying of expressions each time whenever the execution encounters a breakpoint or the n command. The undisplay command is used to remove display expressions. Valid format specifiers are as follows:
    o - octal
    x - hexadecimal
    d - decimal
    u - unsigned decimal
    t - binary
    f - floating point
    a - address
    c - char
    s - string
    i - instruction
    

    In the above example, the breakpoint is set at line 12 and ran with the arguments 1 10 100. Once the breakpoint is encountered, display command is executed to print the value of i in hexadecimal form and value of args[i] in the string form. After then, whenever the command n or a breakpoint is encountered, the values are displayed again until they are disabled using undisplay command.

  16. print : This command prints the value of a given expression. The display command prints all the previously displayed values whenever it encounters a breakpoint or the next command, whereas the print command saves all the previously displayed values and prints whenever it is called.
    print [Expression]
    print $[Previous value number]
    print {[Type]}[Address]
    print [First element]@[Element count]
    print /[Format] [Expression]
    

  17. file : gdb console can be opened using the command gdb command. To debug the executables from the console, file [executable filename] command is used.



Last Updated : 20 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads