Kyle R . Burton on Tue, 24 Sep 2002 16:40:11 +0200


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

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


Something else that gcc/g++ offers is: __PRETTY_FUNCTION__
(or G_GNUC_PRETTY_FUNCTION) which is the name of the function at
compile time.  So you could use a couple of macros to trace entry/exit
of your functions:

  #include <stdio.h>
  
  #if defined ( _TRACE_FUNCTION_CALLS )
    #define _ENT fprintf( stderr, "ENTER %s(%d) %s\n",__FILE__,__LINE__,__PRETTY_FUNCTION__ )
    #define _EXT(a)  fprintf( stderr, "EXIT %s(%d) %s\n",__FILE__,__LINE__,__PRETTY_FUNCTION__ ); a
  #else
    #define _ENT 
    #define _EXT(a) a
  #endif
  
  int some_function( char *s )
  {
    _ENT;
    /* do something */
    printf("in some funciton: %s\n",s);
    _EXT( return 1 );
  }
  
  int main( int argc, char** argv )
  {
    some_function( "foo" );
    return 0;
  }


If you really want to get fancy, you could additionaly use these two macros to
manage a stack in thread local storage of which functions you're in and how
deep you are at any given time.  That would add alot of overhead to function
calls, but you could do more pre-processor tricks to only enable it for
'debug' builds...


Just wanted to point out gcc's __PRETTY_FUNCTION__ feature, I was happy
when I found out about it.


Kyle


-- 

------------------------------------------------------------------------------
Wisdom and Compassion are inseparable.
        -- Christmas Humphreys
mortis@voicenet.com                            http://www.voicenet.com/~mortis
------------------------------------------------------------------------------
_________________________________________________________________________
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