2

I have been trying to find the address of the SHELL environment variable in a program on a Ubuntu 12.04 machine. I have turned off ASLR.

I found similar posts : SO question and blog post

I have tried using the following in gdb

 (gdb) x/s *((char **)environ)

but I get the message :

 (gdb) No symbol "environ" in current context

Is this not valid in Ubuntu 12.04 ? Is there any other way to inspect the address of an environment variable in a process using gdb ?

1
  • Does your program contain the header file unistd.h?
    – Harvinder
    Sep 19, 2014 at 18:26

2 Answers 2

3

Has your binary been stripped of its symbols? If so, there will be no symbol table and you will have no hope of finding this symbol. You can find out with readelf - here my hello binary does have its symbol table:

$ readelf -S hello | grep -i symtab
  [28] .symtab           SYMTAB           0000000000000000  000018f8
$ 

Also when you run GDB, has your program actually started? It looks like this symbol is not resolvable until the symbol table has loaded. It won't be loaded when you first start GDB, but should be by the time you hit main(). You can simply put a breakpoint at main(), run the program, and then inspect the variable when you hit the main() breakpoint:

Reading symbols from hello...(no debugging symbols found)...done.
(gdb) x/s *((char **)environ)
No symbol table is loaded.  Use the "file" command.
(gdb) b main
Breakpoint 1 at 0x4005c8
(gdb) r
Starting program: /home/ubuntu/hello 

Breakpoint 1, 0x00000000004005c8 in main ()
(gdb) x/s *((char **)environ)
0x7fffffffe38f: "XDG_VTNR=7"
(gdb) 
3

In gdb you can use the command show environment. Also as an alternative to examine the environment of a process is:

$ sed 's/\x0/\n/g' /proc/<PID>/environ

The sed command is necessary to convert the NUL separators into newlines for readability.

The advantage of either of these approaches (besides simplicity) is that it doesn't matter if your symbol table is stripped or not :-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .