[c] How do I pass a command line argument while starting up GDB in Linux?

I have to debug a program that has errors in it as part of my assignment. However, I must first pass command line arguments in order to solve this problem.

I do:

gdb -tui InsertionSortWithErrors

which works, but after that I don't know how to pass arguments. I used gdb -help and it says something about --args which I also tried and it didn't work.

I want to be able to get the debugger+the GUIand pass command line arguments.

This question is related to c linux debugging gdb command-line-arguments

The answer is


Try

gdb --args InsertionSortWithErrors arg1toinsort arg2toinsort

I'm using GDB7.1.1, as --help shows:

gdb [options] --args executable-file [inferior-arguments ...]

IMHO, the order is a bit unintuitive at first.


Another option, once inside the GDB shell, before running the program, you can do

(gdb) set args file1 file2

and inspect it with:

(gdb) show args

Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to debugging

How do I enable logging for Spring Security? How to run or debug php on Visual Studio Code (VSCode) How do you debug React Native? How do I debug "Error: spawn ENOENT" on node.js? How can I inspect the file system of a failed `docker build`? Swift: print() vs println() vs NSLog() JavaScript console.log causes error: "Synchronous XMLHttpRequest on the main thread is deprecated..." How to debug Spring Boot application with Eclipse? Unfortunately MyApp has stopped. How can I solve this? 500 internal server error, how to debug

Examples related to gdb

How to install gdb (debugger) in Mac OSX El Capitan? Step out of current function with GDB How do I get the backtrace for all the threads in GDB? Counter exit code 139 when running, but gdb make it through gdb: how to print the current line or find the current line number? How do I pass a command line argument while starting up GDB in Linux? GDB: break if variable equal value How to attach a process in gdb gdb fails with "Unable to find Mach task port for process-id" error gdb: "No symbol table is loaded"

Examples related to command-line-arguments

How to pass arguments to Shell Script through docker run How can I pass variable to ansible playbook in the command line? Use Robocopy to copy only changed files? mkdir's "-p" option What is Robocopy's "restartable" option? How do you run a .exe with parameters using vba's shell()? Bash command line and input limit Check number of arguments passed to a Bash script Parsing boolean values with argparse Import SQL file by command line in Windows 7