LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-13-2011, 12:25 PM   #1
shibram
LQ Newbie
 
Registered: Oct 2011
Posts: 3

Rep: Reputation: Disabled
problem with gdb


hello all, I have a strange problem with GDB. when i print a value of an integer using 'print' command in gdb it gives an an incorrect value. however if i run the program in console and print the same integer value from my program it gives me the right value.
see the program below, problem is concerning variable 'file_des'

main()
{
int file_des;

//close(1);
file_des = open("output.txt", (O_RDWR | O_APPEND | O_CREAT), 0666);
dup2(file_des, 1);
file_des++;
printf("\nstandard output redirected");
printf("\nfile_des = %d", file_des);
}

at the last line of the program, when i print file_des in gdb, it shows a value 7. when i run the program in console it prints value 3.
does anyone has a clue ??
thanx..
 
Old 10-13-2011, 02:20 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. As you know, "file descriptors" are a number, allocated by the OS.

2. As you probably also know, there are a few "standard descriptors" opened for each Linux program:

STDIN: 0 # keyboard input
STDOUT: 1 # terminal output
STDERR: 2 # terminal output (unbufferred, for error messages)

3. The number Linux creates for a file descriptor is usually just "1 + <the last fd>".

So in the first case, the *ONLY* three files opened by the process were 0, 1 and 2 (the defaults). Hence the
next fd you got was "3".

In the second case, there were (at least) *six* files opened before you did your "dup()". Hence "7".

'Hope that helps .. PSM
 
0 members found this post helpful.
Old 10-14-2011, 10:23 AM   #3
shibram
LQ Newbie
 
Registered: Oct 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
but as u can see in the program no other file was opened. so when I run it in gdb, descriptor value should have been 3, not 7 !!
 
0 members found this post helpful.
Old 10-15-2011, 12:09 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Dude -

Don't mark ME down just because YOU don't understand the answer.

Re-read my answer.

And think: how might gdb communicate with the program under the debugger? Magic? Telepathy? Or do you think maybe - just maybe - there might be something involving opening a new file descriptor?
 
Old 10-15-2011, 12:35 AM   #5
shibram
LQ Newbie
 
Registered: Oct 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
dude....ur sarcasm is very bad...u should culture good attitude..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Problem with gdb pgpython Programming 1 09-27-2009 11:18 AM
normal gdb and spec gdb for kgdb Igor007 Programming 1 09-23-2005 05:15 PM
normal gdb and spec gdb for kgdb Igor007 Linux - Newbie 1 09-23-2005 02:41 PM
gdb .. looking for 32 bit gdb.. for ia64 suse.. nkshirsagar SUSE / openSUSE 0 12-09-2004 04:02 AM
Problem with GDB balteo Linux - General 4 07-01-2002 08:38 PM


All times are GMT -5. The time now is 09:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration