Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evaluate an expression to be used for a gdb monitor command?

Tags:

gdb

monitor

Inside a scripted gdb session I want to use monitor <cmd> where cmd should contain the address of a symbol. For example:

monitor foobar &myVariable

should become to:

monitor foobar 0x00004711

Because the remote side cannot evaluate the expression. Whatever I tried, the string "&myVariable" gets sent instead of the address.

I played around with convenience variables and stuff, but this is the only workaround I found:

# write the command into a file and execute this file
# didn't find a direct way to include the address into the monitor command
set logging overwrite on
set logging on command.tmp
printf "monitor foobar 0x%08x\n", &myVariable
set logging off
source command.tmp

Any ideas to solve this in a more elegant way?

like image 804
Thomas Giesel Avatar asked Sep 13 '25 01:09

Thomas Giesel


1 Answers

The simplest way to do this is to use the gdb eval command, which was introduced for exactly this purpose. It works a bit like printf:

(gdb) eval "monitor foobar %p", &myVariable

(I didn't actually try this, so caution.)

If your gdb doesn't have eval, then it is on the old side. I would suggest upgrading.

If you can't upgrade, then you can also script this using Python.

If your gdb doesn't have Python, then it is either very old (upgrade!) or compiled without Python support (recompile!).

If you can't manage to get either of these features, then I am afraid the "write out a script and source it" approach is all that is left.

like image 104
Tom Tromey Avatar answered Sep 17 '25 20:09

Tom Tromey