How to access environment variables inside .gdbinit and inside gdb itself?

后端 未结 4 1195
夕颜
夕颜 2021-02-18 18:20

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:

d         


        
相关标签:
4条回答
  • 2021-02-18 18:48

    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
    
    0 讨论(0)
  • 2021-02-18 18:49

    (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

    0 讨论(0)
  • 2021-02-18 18:55

    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

    0 讨论(0)
  • 2021-02-18 19:01

    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
    
    0 讨论(0)
提交回复
热议问题