[prog] Re: [Techtalk] GDB and C question

Jacinta Richardson jarich at perltraining.com.au
Wed Oct 23 18:13:14 EST 2002


> > I, being a miserable programmer, am stuck debugging a problem with a
> > rather old piece of in-house software written in C.  It's segfaulting
> > to be precise, with (shocker) no good error messages being generated.

Code that segfaults can't produce good error messages.

> > I've run the program through gdb, and from what I can see from the
> > stack trace it looks like the segfault's happening when it tries to
> > open a file.  Makes sense; the error gdb originally reported was a no
> > such file or directory error in some low-level library.

That's sensible.  Does it give the line number that the open call was
from?

>  I have
> > narrowed the function call down generating the error, and it's a
> > fprintf statement with a file and a bunch of strings as characters.

Sounds to me that the file is failing to be opened and therefore you're
printing into oblivion.  Try adding the following:

/* at the top of your file */
#include <assert.h>;	/* very useful for debugging */

/* down at the fprintf call: */
assert(some_file != NULL);
fprintf(some_file, "Stuff to print here.....");


And then run your program.  If your assert fails then you know that you
can add something like the following to your code (if you can't (be
bothered) find(ing) the location where fopen's return value isn't being
checked).

/* down at the fprintf call: */
if(some_file == NULL)
{
     perror("File error:");
     exit;
}
fprintf(some_file, "Stuff to print here.....");


perror prints a system error message on STDERR.  After the text you put
in the parentheses the routine will add the last error encountered dring a
call to a system or library function (which will hopefully be your file
does not exist error).

> My
> > question is, if I know the name of the file pointer (in this case
> > parm_fp), is it possible to get the name of the file associated with
> > that pointer?

No, it isn't possible.

> > Any way to get the filename from the pointer or address?  I realize
> > the best way would probably be to go through the source code and find
> > the original fopen command for the file in question, but the code is
> > massive and hard to follow.


Alternately you can improve each and every call to fopen.  Making them
look more like this:

if((FH = fopen(some_file_variable, "r")) == NULL)
{
    perror("Failed to open %s:", some_file_variable);
    exit;
}

(assuming FH is your file pointer of type FILE *, some_file_name is a
string containing the filename and you want to open for reading.  Adjust
accordingly)

>   Any advice would be appreciated.
> > Unfortunately, it's part of our financial software and (shock again)
> > the finance stuff needs to get generated by the end of the week.
> > *sigh*, why doesn't anyone ever find problems when they DON'T matter?

They do.  They just don't think it's important enough to mention them if
they're not mattering.  ;)

	Jacinta


--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson	    |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001 	    |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |




More information about the Programming mailing list