Linux – How to access environment variables inside .gdbinit and inside gdb itself

debugginggdblinux

I am looking to set up the path for the source code when debugging with gdb. I chose to do that with a .gdbinit file.

Basically, it contains a command:

directory="/path/to/src".

However, I would like to be able to specify that command as:

directory="$SOURCESROOT/src"

where SOURCESROOT is an environment variable. And, if possible, being able to do that inside gdb debuuging session too, by entering directory=$SOURCESROOT/folder.

Basically, I am looking to access inside gdb (or inside .gdbinit) the environment variables.

But not the environment of the debugee (set env and so on), but the environment of the gdb itself (ie. of the bash prompt where I type in the first place "gdb program").

While typing shell $SOURCESROOT inside gdb session shows the content of the environment variable, this is quite useless, as I cannot enter: directory=shell $SOURCESROOT.

PS: Anybody found an ideal setup for Linux (Debian) to download the sources with "apt-get source", to update those with some kind of "apt-get update" utopic command and to install them so that gdb will automatically find these sources?

Best Answer

Nevermind, I found how to do it by using Python scripting.

My .gdbinit file is now:

python
import os
gdb.execute('directory' + os.environ['SOURCES'] + '/package_name/src')
end

show directories