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.

2
  • Have you tried shell "echo $my_arg" or 'echo $my_arg' (I'm a noob to gdb, it's just a guess)
    – Franko
    Jun 20, 2013 at 14:36
  • Yes I did. It does not work.
    – carlodef
    Jun 20, 2013 at 15:33

2 Answers 2

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.

2
  • This is exactly what I was looking for, thank you! It works on the version of gdb shipped with the android ndk r8 releases.
    – carlodef
    Jun 21, 2013 at 12:21
  • Does anyone know if there is a way to do this in lldb?
    – quad16
    Oct 26, 2016 at 8:30
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)
1
  • 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, 2013 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.