Created attachment 6260 [details] C99 source file which prints the getopt-related variables GDB 7.4 cannot print the right values for getopt-related valued for the program being debugged in Ubuntu 10.04 LTS but fixed values are shown instead. How to reproduce this bug: - Compile the attached C99 source file (with debugging information, of course). - Start gdb and load the program built from the attached source, then set a breakpoint in the line containing the first statement in the do loop. - Once the breakpoint was reached, invoke the print command to show the values currently set to getopt-related variables: (gdb) print optarg (gdb) print opterr (gdb) print optind (gdb) print /c optopt - You can also see the addresses of these global variables: (gdb) print &optarg (gdb) print &opterr (gdb) print &optind (gdb) print &optopt - Let execution of the program being debugged continue, and it will print the values set to those variables by it itselt. - Repeat the previous steps until the program finishes. It is expected that gdb's print command shows the values set to optind, optopt, and optarg by getopt. However, gdb prints the same values for such variables: (gdb) print optarg $1 = 0x00 (gdb) print opterr $2 = 1 (gdb) print optind $3 = 1 (gdb) print /c optopt $4 = '?' That's not all, the memory addresses of such variables printed by gdb differs from the addresses printed by the program being debugged. This bug does not exists in Windows Vista. A gdb process running in that OS always shows the right values (and addresses) of those variables for the process being debugged. The same gdb version are installed in both systems. In Ubuntu, gdb was configured as "i686-pc-linux-gnu".
I was able to reproduce on Fedora 16. I debugged it a little and it seems that we get the correct address for these symbols in elfread.c, but then we apparently have the wrong offset in the relevant target section, so we compute the wrong address for the minsym.
Another weird thing this uncovered is our handling of LOC_UNRESOLVED. Currently in the main objfile, things like optind are LOC_UNRESOLVED. This causes them to be looked up as minsyms to find their values. But in my case I have glibc debuginfo installed, and it seems to me that it would be better to first defer to their definitions coming from there.
This is a duplicate of PR 8588. *** This bug has been marked as a duplicate of bug 8588 ***