Kevin Brosius on Tue, 24 Sep 2002 11:30:12 -0400


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] [C QUESTION] how to tell caller of a function


Bill Jonas wrote:
> 
> On Tue, Sep 24, 2002 at 09:49:20AM -0400, Kevin Brosius wrote:
> > You can easily add printf() functions inside the code which print out
> > the function name, or variables inside the program.
> 
> Well, yes, but the problem is how a function can get the name of the
> function which is calling it so that you can print it in the first
> place.
> 

Hmmm, guess I wasn't clear enough.

You add printf's to the function in question and the callers, then the
log shows up like:

parent_fn()
target_fn()
parent_fn2()
target_fn()

That allows you to see who called 'target_fn()' by inspecting the
output.  You'll want to add a little more detail to the printouts if you
are chasing a specific failure case, so you can identify it.  Maybe
print out a variable that indicates what state the function is in. 
Anything that tags the occurrence of your problem against the printout's
in the log.

Often, this is used with a macro setup so that it can be switched on and
off at compile time.  That prevents you having to remove all the calls
manually later.

Mental makes a good point about a debugger being a good thing to learn. 
However, there are situations where a debugger is not usable, so the
printf method is valid also.  (Different tools for different
situations.)

-- 
Kevin Brosius
_________________________________________________________________________
Philadelphia Linux Users Group        --       http://www.phillylinux.org
Announcements - http://lists.netisland.net/mailman/listinfo/plug-announce
General Discussion  --   http://lists.netisland.net/mailman/listinfo/plug