2

I have a GDB script which I pass to GDB:

gdb -x my_script.gdb

Inside my_script.gdb I have a variable, say foo, which I use as a parameter. Right now, I manually change foo inside the script whenever I want to try a different value for foo.

What I would like to do, is pass a command-line parameter when launching GDB with the command above, so that foo takes the value of that parameter. Is this possible?

Amplify
  • 865
  • 1
  • 8
  • 18
  • Have a look at https://stackoverflow.com/questions/6121094/how-do-i-run-a-program-with-commandline-arguments-using-gdb-within-a-bash-script – cup Apr 06 '20 at 11:55
  • 2
    But this is for passing arguments to the program, isn't it? I want to pass a parameter to the script itself – Amplify Apr 06 '20 at 12:02
  • Perhaps this is the documentation you are looking for: https://sourceware.org/gdb/current/onlinedocs/gdb/objfile_002dgdbdotext-file.html#objfile_002dgdbdotext-file It seems that you can pass a Python file to `gdb` in which case you can definitely have arguments if the `gdb` script doesn't let you. Alternatively, you can write a Bash script that outputs line-separated commands and you can pipe that to `gdb`. – peachykeen Apr 06 '20 at 13:37

1 Answers1

6

You can emulate this behavior with User-defined Commands. You can define some function in my_script.gdb script and call it with arguments from command line:

$ cat my_script.gdb
define func
   print $arg0
end
$ gdb -q -x my_script.gdb -ex "func 123"
$1 = 123
(gdb) 
ks1322
  • 33,961
  • 14
  • 109
  • 164