如何评估 GDB 中的函数? [英] How to evaluate functions in GDB?

查看:15
本文介绍了如何评估 GDB 中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么评估函数在 gdb 中不起作用?在我的源文件中,在 gdb 中调试时,这些示例是错误的评估.

I wonder why evaluate function doesn't work in gdb? In my source file I include, when debugging in gdb, these examples are wrong evaluations.

(gdb) p pow(3,2)

$10 = 1

(gdb) p pow(3,3)

$11 = 1

(gdb) p sqrt(9)

$12 = 0

推荐答案

我的猜测是编译器和链接器对这些特定功能做了一些魔术.最有可能提高性能.

My guess is that the compiler and linker does some magic with those particular functions. Most likely to increase performance.

如果您绝对需要 pow() 在 gdb 中可用,那么您可以创建自己的包装函数:

If you absolutely need pow() to be available in gdb then you can create your own wrapper function:

double mypow(double a, double b)
{
    return pow(a,b);
}

也许还可以将其包装成 #ifdef DEBUG 或其他东西,以免最终二进制文件混乱.

Maybe also wrap it into a #ifdef DEBUG or something to not clutter the final binary.

顺便说一句,您会注意到可以调用其他库函数(并打印它们的返回值),例如:

BTW, you will notice that other library functions can be called (and their return value printed), for instance:

(gdb) print printf("hello world")
$4 = 11

这篇关于如何评估 GDB 中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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