How do I run a program with commandline arguments using GDB within a Bash script?
Categories:
Running GDB with Command-Line Arguments in a Bash Script
Learn how to effectively debug programs that require command-line arguments using GDB within an automated Bash script environment.
Debugging programs that accept command-line arguments can be tricky, especially when you need to automate the debugging process or ensure consistent argument passing. GDB (GNU Debugger) provides powerful features for this, and integrating it into a Bash script allows for seamless execution and analysis. This article will guide you through the process, covering the essential GDB commands and Bash scripting techniques.
Understanding GDB's Argument Handling
GDB offers a straightforward way to pass arguments to your target program. The run
command in GDB can accept arguments directly after it. For example, if your program my_program
expects two arguments, arg1
and arg2
, you would launch it in GDB using run arg1 arg2
. When automating this in a script, it's crucial to understand how to construct these commands dynamically.
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Program started.\n");
printf("Number of arguments: %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
A simple C program that prints its command-line arguments.
Passing Arguments in a Bash Script
To run GDB with arguments in a Bash script, you'll typically use a heredoc or pipe commands directly into GDB. Heredocs (<<EOF
) are particularly useful for multi-line GDB commands, allowing you to set breakpoints, pass arguments, and then run the program in a single block. Ensure your program is compiled with debugging symbols (-g
flag with GCC) for GDB to be effective.
#!/bin/bash
# Compile the C program with debugging symbols
gcc -g my_program.c -o my_program
# Define the arguments for the program
PROGRAM_ARGS="first_arg \"second arg with spaces\" 123"
# Start GDB and pass commands via heredoc
gdb -batch -ex "b main" -ex "run $PROGRAM_ARGS" ./my_program
Bash script to compile, run GDB, and pass arguments to my_program
.
-batch
with GDB is essential for non-interactive execution within scripts. The -ex
flag allows you to execute GDB commands directly. For more complex scenarios or multiple breakpoints, a heredoc is often more readable than a long chain of -ex
flags.Advanced Argument Handling and Scripting
For more dynamic scenarios, you might want to pass script arguments directly to your GDB-controlled program. This can be achieved by using Bash variables for the arguments. You can also capture GDB's output for automated analysis using redirection. Remember to properly quote arguments, especially those containing spaces or special characters, to ensure they are passed correctly to both Bash and GDB.
#!/bin/bash
# Compile the C program with debugging symbols
gcc -g my_program.c -o my_program
# Check if arguments were provided to the script
if [ "$#" -eq 0 ]; then
echo "Usage: $0 <program_arguments>"
exit 1
fi
# Collect all script arguments as program arguments for GDB
PROGRAM_ARGS="$@"
# Start GDB, set a breakpoint, run with arguments, and quit
gdb -batch <<EOF
b main
run $PROGRAM_ARGS
quit
EOF
# Capture GDB output (example: if you want to parse it)
# gdb -batch -ex "b main" -ex "run $PROGRAM_ARGS" -ex "info registers" ./my_program > gdb_output.txt
A Bash script demonstrating how to pass script arguments dynamically to GDB and the target program.
Flowchart of GDB execution with arguments in Bash.
"arg with spaces"
) when defining PROGRAM_ARGS
in Bash, and GDB will correctly interpret them when passed via the run
command.1. Step 1
Compile your C/C++ program with the -g
flag to include debugging symbols (e.g., gcc -g my_program.c -o my_program
).
2. Step 2
Create a Bash script (e.g., debug.sh
) and make it executable (chmod +x debug.sh
).
3. Step 3
Inside the script, define a variable for your program's command-line arguments, ensuring proper quoting for arguments with spaces (e.g., PROGRAM_ARGS="arg1 'another arg' 123"
).
4. Step 4
Use gdb -batch -ex "b main" -ex "run $PROGRAM_ARGS" ./my_program
or a heredoc for more complex GDB command sequences to execute your program with the specified arguments.