LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-06-2003, 10:41 AM   #1
captainstorm
Member
 
Registered: Jun 2003
Location: Oricola, Italy
Distribution: RH 9, so far
Posts: 261

Rep: Reputation: 31
Question a question about gdb


I am using RH9.0 with the default gdb program (I did not remember the version but I'll find it out and give it here).
I've been following a programming tutorial by redhat on the usage of gdb by a sample program. The sample program is attached below. Running the program would get a "segment fault" error, because the char *ttydata was not initialized.

However, when I set a breakpoint at any place in the program: gdb>break 'some line'
and then run the program:
gdb>run
The program pauses at somewhere, and I continue the program:
gdb>c
I got the program pass through! and then remove the breakpoint:
gdb>delete 'some line'
and run again, I still got this "segment fault" error.

Is it a bug, or I neglected something I should pay attention?

Thank you all!
 
Old 10-06-2003, 10:42 AM   #2
captainstorm
Member
 
Registered: Jun 2003
Location: Oricola, Italy
Distribution: RH 9, so far
Posts: 261

Original Poster
Rep: Reputation: 31
oh, and here is the sample program:

#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <pwd.h>

int massivehello(char* message)
{
int uid; //current user's id number
static int fd; //file descriptor id
FILE* fp;
char *ttydata, *user; //buffers for terminal info, and username
struct passwd *pwd; //password data structure
struct utmp *utmp; //process information structure

uid = getuid();
pwd = getpwuid(uid);

setutent(); //go to the beginning of the process list

user = pwd ? pwd->pw_name : "root"; //figure out who is running this

// go through the process list and for each entry our 'user' owns, output
// our message

while ((utmp = getutent()) != NULL)
{
if (utmp->ut_type != USER_PROCESS || //make sure it's a user process
utmp->ut_user[0] == 0 || // and there is a valid user
(strcmp(utmp->ut_user, user))) // and that user is our 'user'
{
continue; // skip this entry if any of that stuff failed
}
sprintf(ttydata, "/dev/%s", utmp->ut_line);

//ttydata is the name of the terminal to write to
fd = open(ttydata, O_WRONLY | O_NDELAY);
if ((fp = fdopen(fd,"w")) != NULL)
{
fputs(message, fp);
fclose(fp);
}
else
{
close(fd);
}
}
endutent(); //close the utmp file
return 0;
}

int main()
{
int retval = 0;
retval = massivehello("Hello World!\n");
return(retval);
}
 
Old 10-06-2003, 12:21 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I've tried it, and the same thing happens on my Debian sarge/testing with gdb version 5.3-debian

Sometimes a program doesn't segfault when it should because memory is allocated in blocks. So, it would be possible that ttydata points to a place in memory that is part of a block that actually belongs to the program. I imagine that maybe gdb arranges for some things that cause this to happen. After all, it is little data that is being stored in ttydata.

Not at all sure about this. It's just a guess.
 
Old 10-06-2003, 12:39 PM   #4
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
can you supply the backtrace when the segmentation fault occured.
it is most likely because of uninitialised ttydata as you have mentioned...
try doing a malloc and then running the program, inside gdb and outside..

it should run fine..in case it doesnt , please do show the backtrace in gdb.
 
Old 10-07-2003, 01:50 AM   #5
captainstorm
Member
 
Registered: Jun 2003
Location: Oricola, Italy
Distribution: RH 9, so far
Posts: 261

Original Poster
Rep: Reputation: 31
Thank you all, really. I am not sure how I could get the backtrace but I'll see the help and put them here when going back.

And Hko, I would feel the same way that gdb makes 'some' arrangements by breakpoint. But just a thinking: Would it be an obstacle to debug the program and tackle down the problem?

Thanks again.
 
Old 10-07-2003, 11:13 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by captainstorm
But just a thinking: Would it be an obstacle to debug the program and tackle down the problem?
Yes, I think so. After all, you do miss the bug this way.
I wouldn't know of a solution, except for running it without a breakpoint, and display a stacktrace (type "bt" for that in gdb, just after the segfault).
 
  


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
fortran debugging question with g77 and gdb miasmak Programming 1 08-16-2006 07:03 AM
normal gdb and spec gdb for kgdb Igor007 Programming 1 09-23-2005 04:15 PM
normal gdb and spec gdb for kgdb Igor007 Linux - Newbie 1 09-23-2005 01:41 PM
gdb .. looking for 32 bit gdb.. for ia64 suse.. nkshirsagar SUSE / openSUSE 0 12-09-2004 03:02 AM
How to use GDB ? indian Programming 4 09-06-2004 05:11 AM

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

All times are GMT -5. The time now is 07:23 AM.

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