Hi,
  I'm debugging initdb using gdb.
  I found that I could not step in the function getopt_long in line 2572 in
initdb.c.
  I also found that the value of VAR optind never be changed. VAR optind is
always equal to 1 but how could optind be larger than the value of argc(the
value of argc is 6) in line 2648 and 2654.

I was so confused. Could someone give me some help? Thank you~

here is my configure:
./configure CFLAGS=-O0 --enable-debug --enable-depend --enable-cassert
--prefix=/home/pgsql/pgsql

follows is my debug log by gdb:

[pgsql@vmlinux postgresql-9.1.2]$ gdb initdb
GNU gdb Red Hat Linux (6.3.0.0-1.63rh)
Copyright 2004 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-gnu"...Using host
libthread_db library "/lib/tls/libthread_db.so.1".

(gdb) set args -U pgsql -W -D /home/pgsql/pg_data
(gdb) b main
Breakpoint 1 at 0x804d133: file initdb.c, line 2553.
(gdb) b 2572
Breakpoint 2 at 0x804d20c: file initdb.c, line 2572.
(gdb) run
Starting program: /home/pgsql/pgsql/bin/initdb -U pgsql -W -D
/home/pgsql/pg_data

Breakpoint 1, main (argc=6, argv=0xbfec0ad4) at initdb.c:2553
2553            progname = get_progname(argv[0]);
(gdb) c
Continuing.

Breakpoint 2, main (argc=6, argv=0xbfec0ad4) at initdb.c:2572
2572            while ((c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:",
long_options, &option_index)) != -1)
(gdb) p optind
$1 = 1
(gdb) s
2574                    switch (c)
(gdb) n
2589                                    username = xstrdup(optarg);
(gdb)
2590                                    break;
(gdb) p optind
$2 = 1
(gdb) n

Breakpoint 2, main (argc=6, argv=0xbfec0ad4) at initdb.c:2572
2572            while ((c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:",
long_options, &option_index)) != -1)
(gdb) p optind
$3 = 1
(gdb) n
2574                    switch (c)
(gdb) p optind
$4 = 1
(gdb) n
2586                                    pwprompt = true;
(gdb)
2587                                    break;
(gdb)

Breakpoint 2, main (argc=6, argv=0xbfec0ad4) at initdb.c:2572
2572            while ((c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:",
long_options, &option_index)) != -1)
(gdb) p optind
$5 = 1
(gdb) n
2574                    switch (c)
(gdb)
2580                                    pg_data = xstrdup(optarg);
(gdb) p optarg
$6 = 0x0
(gdb) n
2581                                    break;
(gdb) p optarg
$7 = 0x0
(gdb) n

Breakpoint 2, main (argc=6, argv=0xbfec0ad4) at initdb.c:2572
2572            while ((c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:",
long_options, &option_index)) != -1)
(gdb) p pg_data
$8 = 0x9d328e8 "/home/pgsql/pg_data"
(gdb) n
2648            if (optind < argc)
(gdb) p optind
$9 = 1
(gdb) p argc
$10 = 6
(gdb) n
2654            if (optind < argc)
(gdb) p optind
$11 = 1
(gdb) p argc
$12 = 6
(gdb) n
2663            if (pwprompt && pwfilename)
(gdb)

-- 
Clover White

Reply via email to