linuxcommand-linegdbenvironment-variablescommand-substitution

How to set environment variable within GDB using shell command?


I want to run gdb with only a single variable MyVar in its environment.

However, the variable's contents are rather complex, containing non-printable ASCII, and so is best set using e.g. MyVar=$(python -c 'print("\xff...")').

I'm left with two options:

  1. Set the MyVar in bash before running gdb. Then inside gdb, remove all other environment variables individually using unset environment NAME (very tedious!).

  2. Clear all environment variables using unset environment. Then inside gdb, set MyVar using a shell command (as above) (how?)

Any ideas?


Solution

  • Option 2 is possible.

    (gdb) unset environment
    (gdb) python gdb.execute("set environment Myvar=\xff")
    (gdb) show environment 
    Myvar=ÿ
    

    Option 1 can be done with env(1).

    $ env -i MyVar=$(python -c 'print("xyz")') gdb
    (gdb) show environment
    MyVar=xyz
    LINES=35
    COLUMNS=80
    

    Then you just have to clear LINES and COLUMNS.