YouTip LogoYouTip

Linux Comm Ltrace

Linux ltrace Command |

Image 1: Linux Command Encyclopedia Linux Command Encyclopedia


ltrace is a utility in Linux systems used to track and record dynamic library functions called during program execution. It is a powerful tool for debugging and analyzing program behavior, especially suitable for the following scenarios:

  • View which library functions the program calls
  • Understand the parameters and return values of function calls
  • Diagnose interaction issues between the program and library functions
  • Analyze program performance bottlenecks

Unlike the strace command (which tracks system calls), ltrace focuses on user-space library function calls.


Install ltrace

Most Linux distributions do not have ltrace installed by default. You can install it using the package manager:

Examples

Debian/Ubuntu

sudo apt-get install ltrace

CentOS/RHEL

sudo yum install ltrace

Fedora

sudo dnf install ltrace

Arch Linux

sudo pacman -S ltrace

Basic Syntax

The basic command format for ltrace is:

ltrace  program_to_trace 

Or attach to a running process:

ltrace -p PID

Common Options

OptionDescription
-cCount function calls and time, output summary at the end
-eOnly trace specified functions (supports wildcards)
-fTrace child processes
-iPrint instruction pointer (IP)
-lOnly trace functions from specified library
-nSpecify indentation level for output lines
-oWrite output to file
-pAttach to running process
-rPrint relative timestamp
-SAlso trace system calls
-tAdd time prefix to each line
-TShow time spent in each call
-uRun as specified user

Usage Examples

Basic Tracing Example

Trace library function calls of a simple program:

ltrace ./my_program

Example output:

printf("Hello, World!n") = 13
malloc(1024) = 0x55a1a2e2e260
free(0x55a1a2e2e260) =

Count Function Calls

Use the -c option to get statistics on function calls:

ltrace -c ./my_program

Example output:

% time     seconds  usecs/call     calls      function
------ ----------- ----------- --------- --------------------
 45.23    0.123456         123      1000      malloc
 32.12    0.087654          87      1000      free
 22.65    0.061728          61      1000      printf

Trace Specific Functions

Only trace malloc and free functions:

ltrace -e "malloc,free" ./my_program

Attach to Running Process

Trace process with PID 1234:

ltrace -p 1234

Show Call Duration

Use the -T option to show time spent in each call:

ltrace -T ./my_program

Example output:

malloc(1024) = 0x55a1a2e2e260 <0.000123>
free(0x55a1a2e2e260) = <0.000045>

Practical Application Cases

Case 1: Analyze Memory Allocation

ltrace -e "malloc,free" ./memory_intensive_program

Through this command, you

← Linux Comm ZypperLinux Comm Pgrep β†’