9

I want to execute a shell command from gdb, this shell command needs an argument, and this argument is a gdb variable. How to make gdb interpret this variable before passing the command and argument to the shell interpreter ?

For example, in a gdb prompt:

(gdb) set $my_arg = 2
(gdb) shell echo $my_arg

I would like this to print 2 on stdout, but actually it does print a blank line.

Edit: I'm using ndk-gdb, which doesn't support python scripting.

carlodef
  • 2,402
  • 3
  • 16
  • 21

2 Answers2

13

With a new version of gdb, you can use "eval":

(gdb) set $val = 2
(gdb) eval "shell echo %d", $val
2

If you have an older version of gdb, then the best you can do is use "set logging" to write things to a file, then use shell utilities like sed to rewrite the file into the form you want.

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63
6

Update This answer is not suitable for ndk-gdb. only for gdb.

It seems that printing a convenience variable in the shell command is not supported directly. However you can use gdb python to achieve it:

(gdb) set $my_arg = 2
(gdb) python gdb.execute("shell echo " + str(gdb.parse_and_eval("$my_arg")))
2
(gdb)
  • Thank you! It does work with a standard version of gdb. Unfortunately I am working with the ndk-gdb (ie a version of gdb provided with the android ndk) and python scripting is not supported in that copy of gdb. I will edit my question to add this detail. – carlodef Jun 20 '13 at 15:30