How to Trace Dynamic Library Calls in Linux with ltrace

Quick Linux Tip #71:

Question: My binary calls a function - which library actually provides it?

Try: ltrace -e 'malloc+free' ls /tmp 2>&1 | head -20

Info: ltrace tracks dynamic library calls, similar to how strace tracks system calls. The -e option filters specific functions, showing arguments, return values, and dynamic library interactions.

Examples:

  • $ ltrace -c ./program  # Generate a summary report of library calls
  • $ ltrace -e '*@libssl.so.3' curl https://example.com  # Filter functions from a specific library
  • $ ltrace -S ./program  # Trace both library calls and system calls

Note: Install via sudo apt install ltrace. Programs run significantly slower while being traced. ltrace is useful for debugging memory-related behavior, examining dynamic library calls, and understanding shared library interactions.




LinuxTeck.com
linuxteck@ubuntu:~$ ltrace -e 'malloc+free' ls /tmp 2>&1 | head -20
ls->malloc(120)                            = 0x55d1a2b3c000
ls->malloc(48)                             = 0x55d1a2b3c080
ls->malloc(4096)                           = 0x55d1a2b3c0c0
ls->free(0x55d1a2b3c000)                   = <void>
ls->malloc(256)                            = 0x55d1a2b3c400
ls->free(0x55d1a2b3c080)                   = <void>
ls->malloc(1024)                           = 0x55d1a2b3c500
ls->free(0x55d1a2b3c400)                   = <void>
ls->free(0x55d1a2b3c500)                   = <void>
ls->free(0x55d1a2b3c0c0)                   = <void>
+++ exited (status 0) +++
linuxteck@ubuntu:~$

PREVIOUS ARTICLE Quick Linux Tip #70: Run Daily Tasks with Linux systemd Timers NEXT ARTICLE Quick Linux Tip #72: How to Find Files Changed During a Specific Time in Linux
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.