23

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?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
user1284631
  • 4,446
  • 36
  • 61

4 Answers4

21

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
user1284631
  • 4,446
  • 36
  • 61
9

(6 year late!)

Don't use .gdbinit for this purpose. It does not expand env vars. Instead, use this commandline to launch gdb:

gdb --init-eval-command="set dir $SOURCESROOT/src"

(gdb) show dir
/path/to/src

FYI this technique can be used to set other critical variables, e.g

gdb --eval-command="set sysroot $SYSROOTDIR"

Which sets sysroot and solib-absolute-prefix in gdb

FractalSpace
  • 5,577
  • 3
  • 42
  • 47
1

If you don't want to involve python, then this could work?

"show environment [varname] Print the value of environment variable varname to be given to your program when it starts. If you do not supply varname, print the names and values of all environment variables to be given to your program. You can abbreviate environment as env."

ftp://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_19.html

Maybe they could be used for conditions as well:

https://www.adacore.com/gems/gem-119-gdb-scripting-part-1

Anton Krug
  • 1,555
  • 2
  • 19
  • 32
0

In my case I'd like to set global history in common $HOME/.gdbinit, so I used

set history filename ~/.gdb_history

instead of

set history filename $HOME/.gdb_history
Andrey Starodubtsev
  • 5,139
  • 3
  • 32
  • 46