我对__attribute__
和__cyg_profile_function_enter
知之甚少。当然,它们都是GNU C功能。
我正在学习C ++来编译Nginx模块。我总是尝试将C代码示例转换为C ++。
这里是C的简单示例:
#include <stdio.h>
int depth_ = -1;
#ifdef __GNUC__
void __cyg_profile_func_enter(void *, void *) __attribute__((no_instrument_function));
void _cyg_profile_func_enter(void *, void *) __attribute__((no_instrument_function));
#define sustainable(fn, caller) \
do {\
printf("%d, %s: fn = %p, caller = %p\n", depth_, __FUNCTION__, fn, caller); \
} while(0)
void __cyg_profile_func_enter(void *fn, void *caller){
printf("Enter:\n");
depth_++;
sustainable(fn, caller);
}
void __cyg_profile_func_exit(void *fn, void *caller){
printf("Exit:\n");
depth_--;
sustainable(fn, caller);
}
#endif
void sustain(){
depth_ = 100;
}
int main(){
depth_ = 10;
sustain();
//Suture suture;
//suture.sultry();
return 0;
}
sh $ gcc -finstrument-functions ...
循环显示Enter: 101, __cyg_profile_func_enter: fn = 0x400645, caller = 0x4006ba
和Exit: 100, __cyg_profile_func_exit: fn = 0x400645, caller = 0x4006ba
这是C ++代码:
#include <iostream>
using namespace std;
int depth_ = -1;
#ifdef __GNUC__
extern "C" {
void __cyg_profile_func_enter(void *, void *) __attribute__((no_instrument_function));
void __cyg_profile_func_exit(void *, void *) __attribute__((no_instrument_function));
#define sustainable(fn, caller) \
do{ \
printf("%d, %s: fn = %p, caller = %p\n", depth_, __FUNCTION__, fn, caller); \
} while(0)
void __cyg_profile_func_enter(void *fn, void *caller){
printf("Enter:\n");
depth_++;
sustainable(fn, caller);
}
void __cyg_profile_func_exit(void *fn, void *caller){
printf("Exit:\n");
depth_--;
sustainable(fn, caller);
}
}
#endif
void sustain(){
depth_ = 100;
}
class Suture
{
public:
void sultry(){
}
};
int main(int argc, char **argv){
sustain();
Suture suture;
suture.sultry;
}
然后我用...编译
sh$ g++ -std=c++11 -finstrument-functions ....
循环显示Enter: 2, __cyg_profile_func_enter: fn = 0x400925, caller = 0x40099b
和Exit: 1, __cyg_profile_func_exit: fn = 0x400925, caller = 0x40099b
。
很奇怪。为什么depth_ = 100
适用于gcc但不适用于g ++?
您希望fn
是什么功能?我没有看到main()
中的函数调用应该在分配depth_
之后发生。
如果您根本看不到重复的调用,那一定是由于库函数引起的,这些函数很可能在depth_ = 100;
分配之前执行。