13

How can I run something like gdb -e path/to/exe -ex 'run --argnamae argvalue'?

Let's assume a recent version of gfb, within the past year or two.

Gdb runs and prints responses but not interactively.

John Cashew
  • 1,078
  • 2
  • 12
  • 28
  • 1
    "Of course i get the useless error 'ImportError: ... but that is par for the course." -- It is *not* par for the course. It indicates an installation problem. If you want to get rid of it, you should probably ask a separate question, and edit this one to remove references to ImportError. – Employed Russian Sep 20 '15 at 16:23
  • Does this answer your question? [How do I run a program with commandline arguments using GDB within a Bash script?](https://stackoverflow.com/questions/6121094/how-do-i-run-a-program-with-commandline-arguments-using-gdb-within-a-bash-script) – imz -- Ivan Zakharyaschev Mar 05 '20 at 15:37

3 Answers3

21

I think you want gdb --args path/to/exe command line arguments

which will start gdb debugging path/to/exe pass three command line arguments to your exe command, line, and arguments, you can then interact with gdb before issuing the run command.

As for the ImportError: No module named 'libstdcxx' I believe this is already answered here which points to a bug report here.

It appears some versions of GCC have a broken pretty printers python script, you might need to adjust the python sys.path with (gdb) python sys.path.append("/usr/share/gcc-4.8/python"), adjust the path to match whatever GCC version is actually present on your system. You could probably add a command like this to your .gdbinit file to save typing it every time.

Community
  • 1
  • 1
Andrew
  • 3,770
  • 15
  • 22
  • Good to know! But what about `gdb --args path/to/exe command line="can have whitespace in" arguments`? When running gdb is reporting `path/to/exe can't handle command-line argument containing whitespace`. Any solution for handling arguments with whitespaces? – ILCAI Feb 19 '18 at 21:35
  • That error only occurs (I think) when `set startup-with-shell 0` has been used. Right now I don't see a work around (https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=gdb/infcmd.c;h=331fd8751b16a16d7b78b7e912eb25d3d3c1d69d;hb=HEAD#l360) other than not disabling `startup-with-shell` – Andrew Feb 20 '18 at 16:10
9

How can I run something like ...

You can do this:

gdb path/to/exe -ex 'set args arg1 arg2 arg3'

Or use a shorthand notation for the above:

gdb --args path/to/exe arg1 arg2 arg3
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
2

If you want to pass arguments from file,

try this

(gdb) run < the_file_contains_data
galian
  • 832
  • 6
  • 12