Sign in to follow this  
Prozak

C++ Trick I can't remember...

Recommended Posts

Does anyone know that C++ trick that allowed us to code a function that would be called each time we called a function? Let me explain, in a normal program, Function A calls Function B. Using this system, A would call our pre-defined code, and then B. From what I remember some people used it as a tracer... Any ideas?

Share this post


Link to post
Share on other sites
Quote:
Original post by Prozak
Does anyone know that C++ trick that allowed us to code a function that would be called each time we called a function?

Let me explain, in a normal program, Function A calls Function B. Using this system, A would call our pre-defined code, and then B.

From what I remember some people used it as a tracer...

Any ideas?
It doesn't work exactly like you specify, but in MSVC there is _penter and _pexit which are inserted in the beginning and end of each function, such that the sequence would be
Function A's Code -> Function B -> _penter -> Function B's Code -> _pexit -> Back to Function A's Code

The distinction from what you said is that the calls to _penter and _pexit are inserted inside function B, so if function B is inside a library that was compiled with different settings, _penter and _pexit will not be called.

Share this post


Link to post
Share on other sites
Replace function B with a class where function B is the ()operator. pre call code would go in the constructor post call code would go in the destructor.

Share this post


Link to post
Share on other sites
Link.

Not sure if it's what you want exactly, but you might find it interesting anyway.

[Edited by - Gage64 on October 24, 2008 12:30:21 PM]

Share this post


Link to post
Share on other sites
Similar to the MSVC suggestion from above, gcc supports -finstrument-functions, which will generate calls to user-defined prologue/epilogue functions for every function.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this