dladdr() 出现问题的宏

发布于 2024-08-28 07:39:40 字数 560 浏览 5 评论 0原文

我已经使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守护在此方 2024-09-04 07:39:41

宏由预处理器内联扩展,因此无法区分直接从代码调用的函数和从宏调用的函数。

解决这个问题的唯一可能的方法是让您的宏设置一个全局标志,您的跟踪函数将检查该标志。
这当然不是万无一失的,因为从宏调用的函数完成的任何调用也将以相同的方式出现。

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.

慕烟庭风 2024-09-04 07:39:41

如果你真的想深入研究它,你可以看到我对细分c++代码大小。 C++ 模板实际上只是更正式的宏,因此这可能适合您。

也可能不会,因为宏中的 LINEFILE 对应于调用者。

编辑
根据我对此的评论:

$ gcc -E foo.c | gcc -x c-cpp-output -c -finstrument-functions - -o foo.o

预处理通过管道传输到 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:

$ gcc -E foo.c | gcc -x c-cpp-output -c -finstrument-functions - -o foo.o

preprocess piped into gcc expecting preprocessed input on standard input

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文