Thread: Portable instrumentation

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147

    Portable instrumentation

    Hi,

    GCC has a nice feature about instrumentation which let you call a routine every time a function is called, or every time a function returns.

    Now, I want to create my own system to make it portable to other compilers, and also to allow to instrumentalize the functions I want, so I was thinking in two macro for both situations. I am thinking in making some kind of profile that it is activated only with a define clausule.
    Code:
    	#define FUNCT(t,function_name,...)   \
    		(t) function_name(...) { \
    			(void) *func_pointer = &(function_name); \
    			start_data(func_pointer, myclock());
    
    	#define RETURN(x) {stop_data(func_pointer, myclock()); return (x);}
    
    FUNCT(BOOL, LMP, const int prof, const int nmo))
    	ASSERT(prof > 0);
    	ASSERT(nmo >= 0);
    	if (nmo <= 5 ||
    	    prof > (prof_l / 3)) {
    		RETURN(FALSE);
    	}
    	RETURN(TRUE);
    }
    but I cann't get it to work. Can someone help me with this? or is this a difficult task to accomplish?

    Other alternative that comes to my mind is let the function declare without a macro, and if it is anyway to know the function pointer without knowing its name, something like in VB when you call a Form with Me, with it is a generic alias. is it possible?

    Thanks
    Fermin

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    what does 'can't get it to work' mean?
    you are using the variadic macro arguments wrong. look up 'variadic macro' to see how you use the ... in a macro definition. you don't get the individual names 'prof' and 'nmo' from the FUNCT argument list. you get a single token called __VA_ARGS__ that is like a string that contains the arguments you used instead of '...'. so all you can do is plug in __VA_ARGS__ into something that would take the entire arguments from your macro instantiation.

    I am having trouble explaining it clearly, but here's an example of how it works
    Code:
    #define CHECK(x,...) if (x) {printf(__VA_ARGS__);}
    
    CHECK(1,"%s","hello world");
    the ... turns into __VA_ARGS__ which in this case would be "%s","hello world" which gets plugged into the printf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - Portable?
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 07-15-2007, 09:09 AM
  2. 'portable' package
    By janklojo in forum C++ Programming
    Replies: 2
    Last Post: 12-22-2006, 01:58 PM
  3. _getche() is not portable...
    By siavoshkc in forum C++ Programming
    Replies: 7
    Last Post: 08-14-2006, 04:04 PM
  4. portable functions
    By Whizza in forum C++ Programming
    Replies: 9
    Last Post: 05-26-2006, 09:07 PM
  5. portable getch( )
    By The Brain in forum Windows Programming
    Replies: 2
    Last Post: 07-03-2005, 09:58 PM