How to Pass Command Line Arguments to GDB in a Linux Environment?
Last Updated :
24 Apr, 2025
The GNU Debugger, commonly known as GDB, is a powerful tool used for debugging and analyzing programs in Linux and other Unix-like operating systems. While GDB is commonly used to debug programs, it can also be used to launch programs with specific command line arguments. This article will walk you through the process of passing command line arguments when starting GDB in a Linux environment.
Why Pass Command Line Arguments to GDB?
When debugging a program using GDB, you might encounter situations where you need to pass specific command line arguments to the program. These arguments could be essential for reproducing a specific bug or for testing the program with different input values. By passing command line arguments when starting GDB, you can set up your debugging environment with the desired initial conditions.
Passing command line arguments to GDB
Syntax:
# Compiling program with -g flag for debugging
g++ -g -o myprogram myprogram.cpp
# Running compiled program with gdb
gdb ./myprogram
# Passing CLI arguments
run arg1 arg2
Overview:
- Compile the C++ program with the -g flag for debugging: g++ -g -o myprogram myprogram.cpp
- Run GDB by specifying the compiled program as an argument: gdb ./myprogram
- Start the program within GDB using the run command, providing any desired command line arguments: run arg1 arg2.
- Set breakpoints within GDB to pause execution at specific locations in your program.
- Examine variables, step through code, and perform debugging operations in GDB to analyze your program's behavior.
- Use GDB's commands like next, step, print, and backtrace to navigate and troubleshoot your program's execution.
- Once you've identified and fixed issues, continue program execution with the continue command.
- Exit GDB using the quit command when debugging is complete.
Steps to pass command line arguments to GDB
Step 1: Creating a program to debug
The provided code snippet is a C program named "example.c" designed to accept two command-line arguments, namely "arg1" and "arg2", and then display these arguments. In case the required arguments are not provided, the program also offers a usage message.
C
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <arg1> <arg2>\n", argv[0]);
return 1;
}
printf("Argument 1: %s\n", argv[1]);
printf("Argument 2: %s\n", argv[2]);
return 0;
}
Step 2: Compiling the program with -g flag
Launch your terminal and type this command:
Command:
gcc -g -o example example.c
Explanation:
- gcc: This is the command for the GNU Compiler Collection, which is a widely used compiler for the C programming language.
- -g: The -g flag is an option used with the "gcc" command. It instructs the compiler to include debugging information in the compiled executable. This debugging information is essential for effective debugging using tools like GDB (GNU Debugger).
- -o example: This part of the command specifies the name of the output or executable file that will be generated. In this case, it's "example." The -o flag is used to indicate the output file name.
- example.c: This is the input file, which contains the C source code you want to compile.
Output: gcc -g -o example example.c
Step 3: Start GDB with Command Line Arguments
Open the GNU Debugger (GDB) by entering the command gdb ./example in the terminal, here "example" is the compiled output file name.
Command:
gdb ./example
Explanation:
This command sets up GDB to debug the executable file named 'example' located in the current directory. Once inside GDB, you can provide specific command line arguments to your program for testing and debugging purposes.
Output: gdb ./exampleStep 4: Passing the arguments
Within the GDB environment, you can pass command line arguments to your program in the following manner. For the sake of explanation, let's clarify that "Hello" is being designated as the first argument, and "World" is being designated as the second argument.
Command:
run Hello World
Explanation:
- run: This is a command within the GDB (GNU Debugger) environment. It is used to initiate the execution of the program being debugged.
- Hello: This is the first argument being passed to the program.
- World: This is the second argument being passed to the program.
Now, GDB will start the example program with the provided arguments "Hello" and "World".
Output: run Hello WorldNote: Entering arguments containing spaces
If your program's path or any of the arguments contain spaces, enclose them in double quotes.
Example:
run "Hello GFG" "World"
Entering arguments containing spaceStep 5: Setting the breakpoint
Now that GDB is running your program with the specified command line arguments, you can set a breakpoint and start debugging.
Command:
break main
Explanation:
- break: This is a command used in the GDB environment to set breakpoints within your program. Breakpoints are specific points in your code where you want the program's execution to pause for debugging.
- main: In this context, "main" refers to the main function of your program.
Setting breakpoint at beginning of the main functionStep 6: Start debugging the program
Now, type run to start debugging the program.The program will run until it reaches the breakpoint. You can then step through the code, inspect variables, and perform other debugging tasks.
Here are some commonly used commands for further navigation and debugging:
- next (or 'n'): This command allows you to execute the next line of code in your program, skipping over function calls.
- step (or 's'): Use this command to step into functions, meaning you will move to the next line within the called function.
- continue (or 'c'): It resumes the program's execution until it encounters the next breakpoint or reaches the end.
- print (or 'p'): This command lets you inspect the values of variables and expressions during debugging.
- info locals: Provides information about local variables in the current scope.
- backtrace (or 'bt'): Displays a stack trace showing the sequence of function calls that led to the current point in the program.
- list: Shows a few lines of source code around the current execution point.
To learn more checkout this article on GDB commands.
These commands help you navigate through your program, inspect its state, and identify issues during the debugging process.
Here's an example of the expected output. I am utilizing the 'n' (next) command to debug the entire program, proceeding line by line until it reaches the program's end.
Complete debugging outputStep 7: Quit GDB
After debugging is complete, type quit to exit GDB.
Exiting GDB using quit commandConclusion
Passing command line arguments to GDB in a Linux environment is a straightforward process that allows you to set up your debugging sessions with specific initial conditions. By specifying the program's path and appending the desired arguments, you can effectively debug your program with the input parameters you need. This capability is invaluable for diagnosing and fixing bugs, testing different scenarios, and improving the reliability of your software.
Similar Reads
How to Parse Command Line Arguments in Node ?
Command-line arguments, in the context of a command-line interface (CLI), are text strings that provide extra information to a program when it is executed.In Nodejs, these arguments are accessible through an array known as argv (arguments values), where the shell passes all received command-line arg
3 min read
C# Program to Get and Print the Command Line Arguments Using Environment Class
In C#, Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that it retrieves command-line arguments information, exit codes information,
2 min read
How to pass a list as a command-line argument with argparse?
Argparse is a Python library used to parse command-line arguments in a user-friendly manner. It makes it easy to write user-friendly command-line interfaces, and it is widely used in Python applications. In this tutorial, we will discuss how to pass a list as a command-line argument using the Argpar
5 min read
How to Print Second Command line Argument in shell?
Command line arguments are the parameters we pass to a command or script when we run it from the terminal. These arguments are passed as strings and can be accessed within your program to perform various operations. How to Print Second Command Line Argument in Shell?The shell stores the command line
5 min read
How to print command line arguments passed to the script in Node.js ?
Node.js is an open-source and cross-platform runtime environment built on Chrome's V8 engine that enables us to use JavaScript outside the browser. Node.js helps us to use build server-side applications using JavaScript. In Node.js if you want to print the command line arguments then we can access
2 min read
Bash Script - How to use Command Line Arguments
In this article, let us see about Command Line Arguments usage in Bash script. Arguments are inputs that are necessary to process the flow. Instead of getting input from a shell program or assigning it to the program, the arguments are passed in the execution part. Positional Parameters Command-line
4 min read
Command Line Arguments in Golang
Command-line arguments are a way to provide the parameters or arguments to the main function of a program. Similarly, In Go, we use this technique to pass the arguments at the run time of a program. In Golang, we have a package called as os package that contains an array called as "Args". Args is an
2 min read
How To Export and Import a .SQL File From Command Line With Options?
Structured Query Language is a computer language that we use to interact with a relational database.SQL is a tool for organizing, managing, and retrieving archived data from a computer database. In this article , we will learn to export and import .SQL files with command line options. Export:You can
2 min read
How to Find Number of Function Arguments in MATLAB?
The number of function arguments passed in MATLAB will be determined in the following article. Unlike C, C++, and Java, MATLAB can accommodate a variable amount of parameters provided into a function without throwing an error. We'll go through how we determine the actual amount of parameters supplie
4 min read
Command Line Argument in Scala
The arguments which are passed by the user or programmer to the main() method are termed as Command-Line Arguments. main() method is the entry point of execution of a program. main() method accepts an array of strings. runtime. But it never accepts parameters from any other method in the program. Sy
2 min read