Hello all.
It seems a bug of gdb or ld.
If you know something about this behavior, please let me know.
Problem:
As you know there is a global variable 'optind' in the c library. This
variable is updated by getopt(3). And this simple program, a.c just
calls getopt(3) and print the value of 'optind' by printf(3). The
value is correct, but the value is incorrect in the debugger.
The following is a copy of gdb session, you can see the value is 2
with pritnf(3) but it is 1 in the gdb. Why gdb shows a wrong value?
Thanx in advance.
Junjiro Okajima
----------------------------------------------------------------------
% uname -a
Linux brabra 2.2.10 #89 Tue Mar 28 13:50:48 JST 2000 i686 unknown
% cat a.c
#include <unistd.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
extern int optind;
while (getopt(argc, argv, "abc") != EOF);
printf("optind is %d\n", optind);
return 0;
}
% cc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
% cc -Wall -g a.c
% gdb a.out
GNU gdb 4.17.0.11 with Linux support
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux"...
(gdb) b main
Breakpoint 1 at 0x8048433: file a.c, line 10.
(gdb) r -a b
Starting program: /home/jo/a.out -a b
Breakpoint 1, main (argc=3, argv=0xbffffd94) at a.c:10
10 while (getopt(argc, argv, "abc") != EOF);
(gdb) n
11 printf("optind is %d\n", optind);
(gdb)
optind is 2
12 return 0;
(gdb) p optind
$1 = 1
(gdb) quit
% ldd a.out
libc.so.6 => /lib/libc.so.6 (0x40019000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
% ls -l /lib/libc.so.6
ls -l /lib/libc.so.6
lrwxrwxrwx 1 root root 13 Apr 6 15:05 /lib/libc.so.6 -> libc-2.1.3.so