我正在使用 gcc 的-finstrument-functions
选项。为了最大限度地减少开销,我只想检测几个函数。但是,gcc 只允许您将函数列入黑名单(使用no_instrument_function
属性,或通过提供路径列表)。它不允许您将功能列入白名单。
所以我写了一个小gcc插件添加一个instrument_function
属性。这让我可以为特定功能设置检测“标志”(或者,更确切地说,清除无检测标志):
tree handle_instrument_function_attribute(
tree * node,
tree name,
tree args,
int flags,
bool * no_add_attrs)
{
tree decl = *node;
DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT(decl) = 0;
return NULL_TREE;
}
但是,据我了解,这是行不通的。查看 gcc 源代码,要让这个标志真正做任何事情,您还需要使用-finstrument-functions
. 见gcc/gimplify.c:14436
:
...
/* If we're instrumenting function entry/exit, then prepend the call to
the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
catch the exit hook. */
/* ??? Add some way to ignore exceptions for this TFE. */
if (flag_instrument_function_entry_exit
&& !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
/* Do not instrument extern inline functions. */
&& !(DECL_DECLARED_INLINE_P (fndecl)
&& DECL_EXTERNAL (fndecl)
&& DECL_DISREGARD_INLINE_LIMITS (fndecl))
&& !flag_instrument_functions_exclude_p (fndecl))
...
它首先检查-finstrument-functions
是否启用了全局标志。然后它检查特定功能的标志,据我了解,默认情况下启用该标志。所以所有其他没有我的instrument_function
属性的函数仍然会被检测。
有没有办法先清除所有函数的这个标志,然后处理我的instrument_function
属性以只为这些函数设置标志?