clinuxltrace

Alternative to ltrace that works on binaries linked with `-z now`?


ltrace doesn't work on binaries linked with the -z now option, which is the default on my Ubuntu 19.10 system. It only works on binaries linked with -z lazy.

Is there any alternative to ltrace that does the same job, but works on now binaries also?


Solution

  • You can use uftrace utility written by Namhyung Kim. It's available as a package in Ubuntu although I built the code from master branch manually to make sure I use the newest vanilla version. Example main.c:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      puts("Hello World");
    
      return EXIT_SUCCESS;
    }
    

    Build with -z now:

    gcc -O2 main.c -z now -o main
    

    ltrace doesn't work:

    $ ltrace ./main
    Hello World
    +++ exited (status 0) +++
    

    But uftrace does:

    $ LD_LIBRARY_PATH=~/uftrace/libmcount ~/uftrace/uftrace -a --force ./main
    Hello World
    # DURATION     TID     FUNCTION
      58.231 us [ 16283] | puts("Hello World") = 12;
    

    See this thread on project's site on Github: tracing library calls even if it has no PLT #592.