10

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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Sumit Ghosh
  • 1,033
  • 10
  • 29
  • 1
    https://github.com/namhyung/uftrace like described here https://github.com/namhyung/uftrace/issues/592. I tried and it works for me with ELF built with `-z now` on Linux. You can use code from master. – Arkadiusz Drabczyk May 01 '20 at 21:46
  • @ArkadiuszDrabczyk doesn't really seem to work for me. How should you call it? I tried `./uftrace -a --force ./test` and `./uftrace live -P -- ./test`, doesn't seem to work. – Marco Bonelli May 03 '20 at 18:47
  • I built it manually but didn't install. I run `LD_LIBRARY_PATH=/media/data/uftrace/libmcount /media/data/uftrace/uftrace -a --force ./main`. – Arkadiusz Drabczyk May 03 '20 at 19:03
  • ltrace works for `-z now` binaries on the latest OpenSUSE Tumbleweed. I suspect it's due to [this binutils patch](https://build.opensuse.org/package/view_file/openSUSE:Factory/binutils/binutils-revert-plt32-in-branches.diff?expand=1). – Mikel Rychliski May 05 '20 at 13:36
  • @ArkadiuszDrabczyk ah, I was missing that `LD_LIBRARY_PATH`. It seems to work. You should post an answer. – Marco Bonelli May 05 '20 at 16:29

1 Answers1

9

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.

Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38