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


i debugging program in gdb on linux. using getenv , setenv calls read , set environment variables. example calling setenv("tz", "utc", 1); set tz environment variable timezone.

to check if env variable set using gdb command show environment. prints environment variables , values. dose not show tz being set.

even command show environment tz says environment variable "tz" not defined.

is way check environment of debugged program?

p *(char *) getenv("tz") reuturns correct value utc.

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

calling getenv seems totally reasonable approach printing running program's environment.

note

gdb maintains environment array, copied own environment, uses start each new child process. show environment , set environment work on environment, set environment change environment variable next time start program being debugged. once program started, loader have copied environment program's address space, , changes made setenv apply array, not 1 maintained gdb.

addendum: how print debugged program's entire environment

on linux, every process's environment available through pseudofile /proc/pid/environ, pid replaced pid of process. value of file list of null-terminated strings, printing out takes small amount of work.

inside gdb, once you've started running program debugged, can pid info proc , use print 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, have done outside of gdb, different terminal.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -