dladdr() 出现问题的宏
我已经使用 gcc 的 -finstrument-functions 选项和这个(简化的)代码实现了跟踪行为:
void __cyg_profile_func_enter(void *this_fn, void *call_site)
{
Dl_info di;
if(dladdr(this_fn, &di))
printf("entered %s\n", (di.dli_sname?di_dli_sname:"<unknown>"));
}
这非常有效,除了一件事:宏也被处理,但该函数打印的信息包含宏的函数。
因此包含宏的函数会多次打印其信息(这当然是不希望的)。
有什么东西可以检测宏正在被处理吗?或者是否可以完全关闭检测宏?
PS sizeof()
也会出现同样的问题
编辑: 澄清一下:我正在寻找一种解决方案来防止宏与检测函数混淆(他们不应该这样做)。不适用于跟踪宏、函数和/或其他事物的方法。
I have implemented tracing behavior using the -finstrument-functions
option of gcc and this (simplified) code:
void __cyg_profile_func_enter(void *this_fn, void *call_site)
{
Dl_info di;
if(dladdr(this_fn, &di))
printf("entered %s\n", (di.dli_sname?di_dli_sname:"<unknown>"));
}
This works great, except for one thing: macros are processed as well, but the function prints the information of the function which contains the macro.
So functions containing macros have their information printed multiple times (which is of course undesired).
Is there anything to detect that a macro is being processed? Or is is possible to turn off instrumenting macros at all?
PS Same problems occur with sizeof()
Edit: To clarify: I am looking for a solution to prevent macros messing with the instrumented functions (which they should not be doing). Not for methods to trace macros, functions and/or other things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
宏由预处理器内联扩展,因此无法区分直接从代码调用的函数和从宏调用的函数。
解决这个问题的唯一可能的方法是让您的宏设置一个全局标志,您的跟踪函数将检查该标志。
这当然不是万无一失的,因为从宏调用的函数完成的任何调用也将以相同的方式出现。
Macros are expanded inline by the preprocessor, therefore there is no way to distinguish between a function called directly from the code and called from a macro.
The only possible way around this would be to have your macros set a global flag, which your tracing function will check.
This is of course less than foolproof, since any calls done by a function called from a macro will also appear the same way.
如果你真的想深入研究它,你可以看到我对细分c++代码大小。 C++ 模板实际上只是更正式的宏,因此这可能适合您。
也可能不会,因为宏中的 LINE 和 FILE 对应于调用者。
编辑
根据我对此的评论:
预处理通过管道传输到 gcc 中,期望在标准输入上进行预处理输入
If you really want to dig into it you can see my response to breakdown c++ code size. C++ templates are really just more formal macros, so this may work for you.
It also may not, since LINE and FILE within a macro correspond to the caller.
edit
from my comment on this:
preprocess piped into gcc expecting preprocessed input on standard input