1830619 Members
2627 Online
110015 Solutions
New Discussion

Environment of a process

 
Jose M. del Rio
Frequent Advisor

Environment of a process

When a process is launched, a pointer to the environment is passed to it:
int execve(const char *path, char * const argv[], char * const envp[]);
or
extern char **environ;

Given a running process, I would like to know what its environment is.

As a temptative approach, I have developed the following program:
int main(int argc, char* argv[], char* environ[])
{
int i;

printf("environ = 0x%p\n", environ);
for(i=0; environ[i]; i++)
printf("environ[%d] = %s\n", i, environ[i]);
puts("Press any key...");
getchar();

return 0;
}



whose output is:
environ = 0x800003ffbfff07a0
environ[0] = _=./JMR
environ[1] = TMPDIR=/tmp
...
Press any key...

If I get a core of the process with gdb, when it is waiting at the "Press any key" event, then I can later dump the array at 0x800003ffbfff07a0 and (double-)check the environment strings:

(gdb) x /2xw 0x800003ffbfff07a0
0x800003ffbfff07a0: 0x800003ff 0xbfff0006
(gdb) p (char*) 0x800003ffbfff0006
$1 = 0x800003ffbfff0006 "_=./JMR"

So another way of posing the question could be:
given a running process, how can I know the address of its environment pointer?