问题描述
我们都知道内联函数会使调试变得更加棘手,因为它们可以从堆栈跟踪等中删除.但是假设我想从 gdb 中调用一个内联函数,并且我知道它的名称和参数.我想我应该能够做到这一点,但我明白了:
We all know that inline functions can make debugging trickier, as they can be elided from stack traces etc. But suppose I want to call an inline function from within gdb, and I know its name and its arguments. I think I should be able to do that, but I get this:
Cannot evaluate function -- may be inlined
我用nm
列出了我正在使用的共享库中的符号,发现我要调用的函数不在里面.没什么大惊喜.我想要的是一种生成这些内联函数的可见定义的方法.我可以访问当前包含内联定义的头文件,但我无法真正修改这些头文件.也许有某种方法可以告诉编译器发出它在翻译单元中看到的所有内联函数的定义?或者其他一些可以更轻松地调用和检查 gdb 中内联函数结果的技巧?
I used nm
to list the symbols in the shared library I'm using, and found that the functions I want to call are not in there. No big surprise. What I'd like is a way to generate visible definitions of these inline functions. I have access to the header files which contain the inline definitions currently, but I can't really modify those headers. Perhaps there's some way to tell the compiler to emit definitions of all the inline functions it sees in a translation unit? Or some other trick that can make it easier to call and inspect the results of inline functions in gdb?
我在 Linux 上使用 GCC 4.7.2 和 GDB 7.5.1.而且我不能真正切换到未优化的构建,因为我正在调试生产中的核心转储(在开发中我会关闭优化,然后事情就更容易了).
I'm using GCC 4.7.2 and GDB 7.5.1 on Linux. And I can't really switch to a non-optimized build because I'm debugging core dumps from production (in development I would just turn off optimization, then things are easier).
推荐答案
让编译器生成内联函数的可调用版本的一种方法是包含获取函数地址的代码.您还可以为 gcc
提供一个选项.来自内联函数的gcc文档:
One way to get the compiler to generate a callable version of an inline function is to include code that takes the address of the function. There is also an option you can give to gcc
. From the gcc documentation on inline functions:
当一个函数既是内联函数又是静态函数时,如果对该函数的所有调用都是集成到调用者中,并且从不使用函数的地址,则函数自己的汇编代码永远不会被引用.在这种情况下,GCC 不实际上输出函数的汇编代码,除非你指定 选项-fkeep-inline-functions.
When a function is both inline and static, if all calls to the function are integrated into the caller, and the function's address is never used, then the function's own assembler code is never referenced. In this case, GCC does not actually output assembler code for the function, unless you specify the option -fkeep-inline-functions.
这篇关于可以在 gdb 中调用内联函数和/或使用 GCC 发出它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!