How to set environment variable within GDB using shell command?

人走茶凉 提交于 2020-03-21 16:04:25

问题


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?


回答1:


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.




回答2:


When starting gdb from shell command-line, you can specify which program to run, with which arguments (with --args), and even modify the environment of the program with the help of env!

I just did it successfully like this:

gdb --ex=run --args env LD_BIND=now LD_DEBUG=libs \
apt-get install --yes $(cat pkgs-to-install-to-crash-apt)

--ex=run is to ask gdb to run it immediately.

In your case, you would do env -i.

It differs from the suggested env -i VAR=... gdb program in that only your examined program is under the special environment, but not gdb.



来源:https://stackoverflow.com/questions/55593045/how-to-set-environment-variable-within-gdb-using-shell-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!