LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2011, 03:16 AM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2
gdb does not print right value!


Hello, I find the value printed by gdb does not consist with the right value.The following is the output.
Code:
(gdb) 
7	    while ( ( optc = getopt(argc, argv, ":b:B:h" ) ) != -1 ) {
(gdb) 
8	        printf( "%c    %d    %s\n", optc, optind, optarg);
(gdb) 
B    5    1-2
7	    while ( ( optc = getopt(argc, argv, ":b:B:h" ) ) != -1 ) {
(gdb) p optind
$1 = 1
(gdb)
The optind printed on stdout is 5, however, it is 1 printed by gdb. What,s wrong?
my source file is:
Code:
#include <stdio.h>
#include <getopt.h>
void main( int argc, char** argv) {
    int i = 0;
    char optc = 0;
    i = 1;
    while ( ( optc = getopt(argc, argv, ":b:B:h" ) ) != -1 ) {
        printf( "%c    %d    %s\n", optc, optind, optarg);
    }

   if (optind < argc) {
       printf("non-option ARGV-elements: ");
       while (optind < argc)
           printf("%s ", argv[optind++]);
       printf("\n");
   }

}
Thanks!
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-17-2011, 06:23 AM   #2
meenakshi.khurana
LQ Newbie
 
Registered: Mar 2010
Location: India
Distribution: Fedora,Redhat
Posts: 19

Rep: Reputation: 5
Here, gdb is resolving wrong optind variable.

Change your loop to

Code:
while ( ( optc = getopt(argc, argv, ":b:B:h" ) ) != -1 ) {
        printf( "%c    %d    %s %x %x\n", optc, optind, optarg,&optc,&optind);
    }
And then run in gdb.

Now print the address of optind & optc in gdb (print &optind) as well as via this loop. You will get to know that address of optc is same but of optind, its something else.So, your program and gdb resolved to different addresses when they want to access the optind variable

Here, your optind is actually linked to optind@@GLIBC_2.0. In gdb write this:

(gdb) p optind

and hit the tab key (twice), it'll autocomplete the symbols. Here you can see, you have optind and optind@@GLIBC_2.0.

Now print address and value of optind@@GLIBC_2.0

(gdb) print 'optind@@GLIBC_2.0'

(gdb) print &'optind@@GLIBC_2.0'
 
1 members found this post helpful.
Old 10-17-2011, 07:22 AM   #3
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Thank for your help, but I don't know why it's address is not the same! Can you explain in detail?
Code:
(gdb) p &optind
$2 = (int *) 0x4c8a90fc
(gdb) n
B    4    1-2 bffff1db 8049848
 
Old 10-17-2011, 11:14 AM   #4
meenakshi.khurana
LQ Newbie
 
Registered: Mar 2010
Location: India
Distribution: Fedora,Redhat
Posts: 19

Rep: Reputation: 5
Write following command at gdb prompt:

<CODE>
(gdb) info var optind
All variables matching regular expression "optind":

File /usr/include/getopt.h:
int optind;

Non-debugging symbols:
0x08049860 optind@@GLIBC_2.0
(gdb)
</CODE>

From the above output, you can view gdb thinks there are two copies. One is in getopt.c in libc.so. That's the one that GDB is printing. The other is in your binary.

This is basically a bug and and it stands for any variable defined in a shared library and used in the executable.

For further details, please refer following link:

http://sourceware.org/ml/gdb/2003-12/msg00165.html
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Automating 'stop', 'print' and 'continue' in GDB nayankk Programming 4 06-03-2009 04:06 AM
syntax for gdb to print elements of 2D array? johnpaulodonnell Programming 4 03-26-2007 07:24 AM
normal gdb and spec gdb for kgdb Igor007 Linux - Newbie 1 09-23-2005 01:41 PM
gdb + print struct alaios Programming 1 08-22-2005 12:16 PM
gdb .. looking for 32 bit gdb.. for ia64 suse.. nkshirsagar SUSE / openSUSE 0 12-09-2004 03:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration