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?

3
  • Have a look at stackoverflow.com/questions/6121094/… Commented Apr 6, 2020 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 Commented Apr 6, 2020 at 12:02
  • Perhaps this is the documentation you are looking for: sourceware.org/gdb/current/onlinedocs/gdb/… 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. Commented Apr 6, 2020 at 13:37

1 Answer 1

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) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.