How to get environment of a program while debugging it in GDB

≯℡__Kan透↙ 提交于 2020-12-30 05:10:45

问题


I am debugging a program in GDB on linux. I am using getenv and setenv calls to read and set environment variables. For example I am calling setenv("TZ", "UTC", 1); to set the TZ environment variable for timezone.

To check if the env variable is set I am using GDB command show environment. This prints all the environment variables and their values. But it dose not show TZ being set.

Even command show environment TZ says Environment variable "TZ" not defined.

Is their another way to check the environment of the debugged program?

p *(char *) getenv("TZ") reuturns correct value UTC.


回答1:


The gdb command show environment shows an environment which belongs to gdb [see note], not the environment of the program being debugged.

Calling getenv seems like a totally reasonable approach to printing the running program's environment.

Note

Gdb maintains an environment array, initially copied from its own environment, which it uses to start each new child process. show environment and set environment work on this environment, so set environment will change an environment variable for the next time you start the program being debugged. Once the program is started, the loader will have copied the environment into the program's address space, and any changes made with setenv apply to that array, not the one maintained by gdb.

Addendum: How to print the debugged program's entire environment

On Linux, every process's environment is available through the pseudofile /proc/PID/environ, where PID is replaced by the pid of the process. The value of that file is a list of null-terminated strings, so printing it out takes a small amount of work.

Inside gdb, once you've started running the program to be debugged, you can get its pid with info proc and then use that to print the entire environment:

(gdb) info proc
process 6074
...
(gdb) shell xargs -0 printf %s\\n < /proc/6074/environ
XDG_VTNR=7
KDE_MULTIHEAD=false
...

Of course, I could have done that just as easily outside of gdb, from a different terminal.




回答2:


You can alter GDB's view of the environment with set environment TZ =UTC but that doesn't affect a running program, only the environment that will be used next time an inferior process is started.

You can inspect a running inferior process' current environment via the global variable environ



来源:https://stackoverflow.com/questions/32917033/how-to-get-environment-of-a-program-while-debugging-it-in-gdb

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