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

Started by
4 comments, last by osmanb 15 years, 6 months ago
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?
Advertisement
I'm sure I'm missing what you want to do, but couldn't you stick a function call to Function B inside the body of Function A?
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.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
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.
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]
Similar to the MSVC suggestion from above, gcc supports -finstrument-functions, which will generate calls to user-defined prologue/epilogue functions for every function.

This topic is closed to new replies.

Advertisement