Keywords: GDB debugging | argument passing | input redirection
Abstract: This article provides an in-depth exploration of techniques for effectively passing command-line arguments and redirecting standard input within the GDB debugging environment. By comparing multiple implementation approaches, it focuses on the efficient workflow of using the run command internally in GDB for direct argument passing and input redirection, while also introducing the supplementary usage of the --args startup parameter. The article details applicable scenarios, operational procedures, and potential considerations for each method, offering comprehensive debugging solutions for C++ and other language developers.
In software development, debugging is an indispensable process, and GDB, as a powerful debugger, has its parameter passing mechanisms significantly impacting debugging efficiency. The traditional command-line execution method ./a.out arg1 arg2 <file requires special handling in debugging environments. This article systematically explains the techniques for argument passing and input redirection in GDB.
Direct Argument Passing via GDB's Internal run Command
The most straightforward and recommended approach is using the run command (abbreviated as r) within the GDB interactive environment to simultaneously pass arguments and redirect input. The core advantage of this method lies in its intuitiveness and immediacy, allowing developers to flexibly adjust parameter configurations after starting a debugging session.
The specific operational workflow is as follows: first, launch GDB and load the executable via gdb ./a.out, then at the GDB prompt, enter r arg1 arg2 < input_file. The syntax here is completely consistent with regular command-line execution, where the < operator redirects the content of the specified file as the program's standard input.
From a technical implementation perspective, when executing the r < t command, GDB spawns a child process and sets its standard input descriptor to point to file t. This process involves file descriptor redirection at the operating system level, where GDB uses fork() and exec() system calls while employing dup2() to copy the file descriptor to standard input (file descriptor 0).
$ gdb ./a.out
(gdb) r arg1 arg2 < input.txt
Starting program: /path/to/a.out arg1 arg2 < input.txt
Notable advantages of this method include: parameter modifications without restarting GDB sessions, support for dynamic adjustments; redirect files can be changed at any time; command syntax is highly consistent with regular execution environments, reducing learning costs. However, attention must be paid to correct file paths and access permissions, otherwise redirection may fail.
Supplementary Method: The --args Startup Option
In addition to specifying arguments within GDB, parameters can be pre-set when launching GDB using the --args option. This approach is suitable for scenarios with fixed parameters requiring quick entry into debugging states.
The specific syntax is: gdb --args /path/to/executable arg1 arg2 < input_file. The key here is that all content following the --args option (including redirection symbols) is parsed by GDB as startup parameters for the program to be debugged.
gdb --args ./a.out -every -arg you=need < data.txt
From an implementation mechanism perspective, the --args option instructs GDB's startup routine to store subsequent parameters in internal structures. When the user executes the run command, these parameters are automatically passed to the debugged program. While convenient, this method lacks flexibility—once GDB is launched, these preset parameters cannot be modified unless the debugging session is restarted.
Technical Details and Best Practices
Understanding the underlying mechanisms of argument passing helps avoid common debugging issues. When using < for redirection, GDB actually modifies the child process's file descriptor table before calling execve(). This means if the redirected file doesn't exist or is unreadable, the program may receive EOF instead of expected input data.
For complex argument scenarios, recommendations include:
- Arguments containing special characters should be wrapped in quotes, e.g.,
r "arg with spaces" < file - Input file paths are best specified as absolute paths to avoid ambiguities from relative paths
- When debugging scripted programs, common parameter combinations can be defined as GDB user commands
- Use the
show argscommand to verify currently set parameters
Comparing the two main methods: internal run commands are more suitable for exploratory debugging and parameter adjustments, while the --args startup approach is better for automated testing and repetitive debugging tasks. In practical development, developers can flexibly choose or combine these two methods based on different debugging phase requirements.
By mastering these GDB argument passing techniques, developers can debug programs more efficiently, particularly when dealing with C++ applications requiring complex inputs and parameter configurations. Proper argument passing not only improves debugging efficiency but also helps more accurately reproduce and locate program defects.