Using the Linux ltrace utility to trace library calls


While debugging an application on a Linux server this week, I needed to view the library calls (specifically malloc and free) that were being called by a process. This was super easy to do with the Linux ltrace(1) utility:

$ ltrace /bin/cat

__libc_start_main(0x8048f49, 1, 0xbfbec464, 0x804ae98, 0x804aeec
setlocale(6, "") = "en_US.UTF-8"
bindtextdomain("coreutils", "/usr/share/locale") = "/usr/share/locale"
textdomain("coreutils") = "coreutils"
__cxa_atexit(0x8048f1f, 0, 0, 0x804cb70, 0xbfbec3d8) = 0
getopt_long(1, 0xbfbec464, "benstuvAET", 0x804c9c0, NULL) = -1
__fxstat64(3, 1, 0xbfbec360) = 0
__fxstat64(3, 0, 0xbfbec360) = 0
malloc(1024) = 0x8c5f858
read(0,

ltrace(1) also allows you to selectively trace library calls when executed with the “-e” option and a set of calls to trace:

$ ltrace -e malloc /bin/cat

malloc(1024) = 0x8883858

If you need to view the library calls from a live process, you can use ltrace(1)’s “-p” option:

$ ps auxw | grep [g]am_server

root 2644 0.0 0.2 2212 1064 ? S 12:51 0:00 /usr/libexec/gam_server

$ ltrace -p 2644

--- SIGSTOP (Stopped (signal)) ---
--- SIGSTOP (Stopped (signal)) ---
time(NULL) = 1130003202
__xstat64(3, "/etc/xdg/menus/server-settings-m"..., 0xbfe342bc) = -1
__errno_location() = 0xb7f266a0
memcpy(0x84a56e0, "|q221", 96) = 0x84a56e0
g_dir_open(0x84a5748, 0, 0, 0x8b59c1, 0x8499fe8) = 0
__xstat64(3, "/root/.config/menus/server-setti"..., 0xbfe342bc) = -1
__errno_location() = 0xb7f266a0
memcpy(0x84a55f8, "330B343277_i256", 96) = 0x84a55f8
g_dir_open(0x84a5660, 0, 0, 0x8b59c1, 0x8499fe8) = 0
__xstat64(3, "/etc/xdg/menus/system-settings-m"..., 0xbfe342bc) = -1

This is a super cool utility, and is invaluable tool for debugging application problems on Linux systems.

This article was posted by Matty on 2005-10-22 13:51:00 -0400 EDT