简体   繁体   English

如何使用 shell 命令在 GDB 中设置环境变量?

[英]How to set environment variable within GDB using shell command?

I want to run gdb with only a single variable MyVar in its environment.我想在其环境中仅使用一个变量MyVar运行gdb

However, the variable's contents are rather complex, containing non-printable ASCII, and so is best set using eg MyVar=$(python -c 'print("\\xff...")') .但是,变量的内容相当复杂,包含不可打印的 ASCII,因此最好使用例如MyVar=$(python -c 'print("\\xff...")')

I'm left with two options:我有两个选择:

  1. Set the MyVar in bash before running gdb .在运行gdb之前在bash设置MyVar Then inside gdb , remove all other environment variables individually using unset environment NAME ( very tedious! ).然后在gdb ,使用未unset environment NAME非常乏味! )单独删除所有其他环境变量。

  2. Clear all environment variables using unset environment .使用unset environment清除所有环境变量。 Then inside gdb , set MyVar using a shell command (as above) ( how?)然后在gdb ,使用 shell 命令设置MyVar (如上所述)(如何?)

Any ideas?有任何想法吗?

Option 2 is possible .选项 2 是可能的

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

Option 1 can be done with env(1) .选项 1 可以使用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.然后你只需要清除 LINES 和 COLUMNS。

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 !从 shell 命令行启动 gdb 时,您可以指定要运行的程序,使用哪些参数(使用--args ,甚至可以在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. --ex=run是让 gdb 立即运行。

In your case, you would do env -i .在你的情况下,你会做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.它与建议的env -i VAR=... gdb program不同之处在于,只有您检查的程序在特殊环境下,而不是 gdb。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM