ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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);
}
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.
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.
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?
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.