我想使用ltrace列出我的应用程序中使用的函数。它有效,但没有在输出中列出“sin()”。
#include<stdio.h>
#include<math.h>
int main()
{
float x=0;
printf("Hello World!!\n");
x=sin(2);
printf("sin(2)=%f\n",x);
return 0;
}
输出:
[kashi@localhost TestPrgms]$ gcc -o ltrace_test ltrace_test.c -lm
[kashi@localhost TestPrgms]$ ltrace ./ltrace_test
__libc_start_main(0x80484e0, 1, 0xbfddbfd4, 0x8048530 <unfinished ...>
puts("Hello World!!"Hello World!!
) = 14
printf("sin(2)=%f\n", 0.909297sin(2)=0.909297
) = 16
+++ exited (status 0) +++
答案 0 :(得分:2)
这是因为您的sin
调用是一个常量值,gcc
将其优化(即使使用-O0
进行编译且没有-lm
)。这是在disass main
中运行gdb
的结果:
0x0000000000400580 <+0>: push %rbp
0x0000000000400581 <+1>: mov %rsp,%rbp
0x0000000000400584 <+4>: sub $0x10,%rsp
0x0000000000400588 <+8>: mov 0xee(%rip),%eax # 0x40067c
0x000000000040058e <+14>: mov %eax,-0x4(%rbp)
0x0000000000400591 <+17>: mov $0x400660,%edi
0x0000000000400596 <+22>: callq 0x400450 <puts@plt>
0x000000000040059b <+27>: mov 0xdf(%rip),%eax # 0x400680
0x00000000004005a1 <+33>: mov %eax,-0x4(%rbp)
0x00000000004005a4 <+36>: movss -0x4(%rbp),%xmm0
0x00000000004005a9 <+41>: cvtps2pd %xmm0,%xmm0
0x00000000004005ac <+44>: mov $0x40066e,%edi
0x00000000004005b1 <+49>: mov $0x1,%eax
0x00000000004005b6 <+54>: callq 0x400460 <printf@plt>
0x00000000004005bb <+59>: mov $0x0,%eax
0x00000000004005c0 <+64>: leaveq
0x00000000004005c1 <+65>: retq
此处没有sin
的电话。
将代码更改为:
#include<stdio.h>
#include<math.h>
int main()
{
float x, y;
scanf("%f", &x);
y=sin(x);
printf("sin(%f)=%f\n", x, y);
return 0;
}
编译时将使您需要-lm
:
$ gcc -Wall -Wextra -O0 -g 1.c -lm
现在你会看到这个反汇编的输出:
...
0x00000000004006c9 <+25>: callq 0x4005b0 <__isoc99_scanf@plt>
0x00000000004006ce <+30>: movss -0x8(%rbp),%xmm0
0x00000000004006d3 <+35>: unpcklps %xmm0,%xmm0
0x00000000004006d6 <+38>: cvtps2pd %xmm0,%xmm0
0x00000000004006d9 <+41>: callq 0x4005a0 <sin@plt>
...
以及ltrace
中的来电:
__libc_start_main(0x4006b0, 1, 0x7fffd25ecff8, 0x400720 <unfinished ...>
__isoc99_scanf(0x4007b0, 0x7fffd25ecf08, 0x7fffd25ed008, 0x400720) = 1
sin(0x7fffd25ec920, 0x7fa1a6388a20, 1, 16) = 0x7fa1a643b780
printf("sin(%f)=%f\n", 3.000000, 0.141120sin(3.000000) =0.141120
) = 23
+++ exited (status 0) +++