Bug 13800 - gdb does not print right values of getopt-related values
Summary: gdb does not print right values of getopt-related values
Status: RESOLVED DUPLICATE of bug 8588
Alias: None
Product: gdb
Classification: Unclassified
Component: exp (show other bugs)
Version: 7.4
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-04 17:21 UTC by Sebastián Puebla
Modified: 2013-02-04 20:18 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
C99 source file which prints the getopt-related variables (360 bytes, text/x-csrc)
2012-03-04 17:21 UTC, Sebastián Puebla
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastián Puebla 2012-03-04 17:21:40 UTC
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".
Comment 1 Tom Tromey 2012-03-05 19:41:52 UTC
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.
Comment 2 Tom Tromey 2012-03-06 19:02:32 UTC
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.
Comment 3 Samuel Bronson 2013-02-04 20:18:47 UTC
This is a duplicate of PR 8588.

*** This bug has been marked as a duplicate of bug 8588 ***