For gcc there's a similar solution to the MSVC one someone else posted as an answer:
#include <iostream>
int depth=-1;
extern "C" {
void __cyg_profile_func_enter (void *, void *) __attribute__((no_instrument_function));
void __cyg_profile_func_exit (void *, void *) __attribute__((no_instrument_function));
void __cyg_profile_func_enter (void *func, void *caller)
{
depth++;
}
void __cyg_profile_func_exit (void *func, void *caller)
{
depth--;
}
}
class Foo {
public:
void bar() {
std::cout << "bar: " << depth << std::endl;
}
};
int main() {
Foo f;
f.bar();
return 0;
}
Compile with g++ -Wall -Wextra -finstrument-functions
. Be careful not to call an instrumented function from within the instrument hooks though! (See the man page for ways of excluding things)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…