可以在gdb中调用内联函数和/或使用GCC发出它们吗? [英] Possible to call inline functions in gdb and/or emit them using GCC?

查看:334
本文介绍了可以在gdb中调用内联函数和/或使用GCC发出它们吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道,内联函数可以使调试更加棘手,因为它们可以从堆栈跟踪等中删除。但是假设我想从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?

我正在使用GCC 4.7.2和GDB 7.5.1 Linux。而且我不能真正切换到非优化的构建,因为我正在调试生产中的核心转储(在开发中,我将关闭优化,然后事情会变得容易)。

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:


当一个函数既是内联函数又是静态函数时,如果所有对该函数的调用都集成到调用程序中的
中,并且从不使用该函数的地址,则
函数自己的汇编代码从不引用。在这种情况下,除非指定选项
-fkeep-inline-functions
,否则GCC不会实际为该函数输出汇编代码。

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发出它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆