2026年9月GitHub热榜拆解:本地优先、AI工作流与边缘计算选型指南
2026/10/22 6:06:55
__attribute__((no_instrument_function))是 GCC 编译器的一个特性,主要用于禁用函数插桩(instrumentation)。在嵌入式开发中,它在以下场景特别有用:
当使用某些调试/分析工具(如-finstrument-functions)时,编译器会在每个函数的入口和出口插入额外的跟踪代码。这个属性可以排除特定函数。
对于插桩函数本身(如__cyg_profile_func_enter和__cyg_profile_func_exit),如果它们也被插桩,会导致无限递归调用。
// 这些函数本身不应该被插桩,否则会递归调用void__attribute__((no_instrument_function))__cyg_profile_func_enter(void*func,void*caller){// 函数入口的跟踪代码}void__attribute__((no_instrument_function))__cyg_profile_func_exit(void*func,void*caller){// 函数出口的跟踪代码}// 中断服务例程 - 需要最小化延迟void__attribute__((no_instrument_function))TIMER_IRQ_Handler(void){// 中断处理代码}// 内存操作函数 - 避免跟踪开销void*__attribute__((no_instrument_function))memcpy(void*dest,constvoid*src,size_tn){// 内存拷贝实现}// 实时控制循环void__attribute__((no_instrument_function))control_loop(void){// 精确的时间控制代码// 不能有任何额外的插桩开销}-finstrument-functions配合使用编译时启用函数插桩:
gcc -finstrument-functions -o program main.c instrument.c在代码中:
// instrument.c// 插桩回调函数必须标记为 no_instrument_functionvoid__attribute__((no_instrument_function))__cyg_profile_func_enter(void*this_fn,void*call_site){log_function_entry(this_fn,call_site);}void__attribute__((no_instrument_function))__cyg_profile_func_exit(void*this_fn,void*call_site){log_function_exit(this_fn,call_site);}// main.c// 普通函数会被自动插桩voidnormal_function(void){// 这个函数会自动插入跟踪代码}// 关键函数禁用插桩void__attribute__((no_instrument_function))critical_function(void){// 这个函数不会有额外的跟踪代码}-finstrument-functions编译选项一起使用在嵌入式开发中,合理使用这个属性可以在保持调试能力的同时,确保关键代码的性能和实时性。