ltrace

ltrace does not show sin() in the output


I wanted to list the functions used in my application program using ltrace. It works but does not list "sin()" in the output.

#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;
}

Output:

[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) +++

Solution

  • This is because your sin call is a constant value and gcc optimizes it out (even when compiling with -O0 and without -lm). This is the result of running disass main in 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  
    

    There is no call for sin here.

    Changing your code to read:

    #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;
    }
    

    will make you need -lm when compiling:

    $ gcc -Wall -Wextra -O0 -g 1.c -lm
    

    and now you'll see this disassembled output:

       ...
       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>
       ...
    

    and the call in 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) +++