Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux ltrace Command

Linux ltrace Command

Linux Command Reference Linux Command Reference


ltrace is a utility in Linux used to trace and record the dynamic library functions called by a program at runtime. It is a powerful tool for debugging and analyzing program behavior, especially suited to the following scenarios:

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


Installing ltrace

Most Linux distributions do not install ltrace by default; you can install it using a package manager:

Example

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 of ltrace is:

ltrace [选项] 要跟踪的程序 [程序参数]

Or attach to a running process:

ltrace -p PID


Commonly used options

Option Description
-c Count function calls and their timing, then output a summary at the end
-e Trace only specified functions (wildcards supported)
-f Trace child processes
-i Print the instruction pointer (IP)
-l Trace only functions in a specified library
-n Specify the indentation level of output lines
-o Write the output to a file
-p Attach to a running process
-r Print relative timestamps
-S Trace system calls as well
-t Prepend a timestamp to each line
-T Show the time spent on each call
-u Run as a specified user

Usage examples

Basic Tracing Examples

Trace the 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 -c The option retrieves 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

Trace only malloc and free functions as follows:

ltrace -e "malloc,free" ./my_program

Attach to a Running Process

Trace the process with PID 1234:

ltrace -p 1234

Display Call Timings

Use -T The option displays the time spent on each call:

ltrace -T ./my_program

Example output:

malloc(1024)                                       = 0x55a1a2e2e260 

free(0x55a1a2e2e260)                               =  


Practical application cases

Case 1: Analyzing Memory Allocation

ltrace -e "malloc,free" ./memory_intensive_program

With this command, you can see a program's memory allocation and deallocation patterns, helping you spot memory leaks or overallocation.

Case 2: Debugging Network Programs

ltrace -e "connect,send,recv" ./network_program

This helps you understand how a network program interacts with sockets, including connection parameters and data transfers.

Case 3: Performance Analysis

ltrace -c -T ./performance_critical_program

combined with -c and -T With the option, you can identify the most time-consuming library function calls in a program.


Advanced techniques

1. Filtering the Output

Filter ltrace output using grep:

ltrace ./my_program 2>&1 | grep "interesting_function"

2. Tracing System Calls as Well

Use -S The option traces both system calls and library functions:

ltrace -S ./my_program

3. Customizing the Output Format

Use -n Control indentation,-t and add timestamps:

ltrace -n 2 -ttt ./my_program

4. Tracing a Specific Library

Trace only functions in the libcrypto library:

ltrace -l libcrypto.so ./my_program


Frequently Asked Questions

Q1: What Is the Difference Between ltrace and strace?

Q2: Why Does ltrace Not Work on Some Programs?

Possible reasons:

  1. The program is statically linked (does not depend on dynamic libraries)
  2. The program uses techniques ltrace cannot trace (such as direct system calls)
  3. Insufficient permissions (try using sudo)

Q3: How Do I Trace a C++ Program?

C++ function names are mangled; you can use -C the option to try to demangle them:

ltrace -C ./cpp_program

Or use the c++filt tool to demangle the output.


Best Practices

  1. Start Simple: First use basic commands to see the overall picture, then gradually add options
  2. Combine with Other Tools: Use ltrace together with tools such as gdb and valgrind
  3. Be Aware of Performance Impact: ltrace significantly slows down programs and is not suitable for production environments
  4. Record the Output: When using -o The option saves output to a file for easier analysis
  5. Understand the Context: Combine with the source code to understand function call relationships

Summary

ltrace is an indispensable tool in a Linux developer's toolbox, offering a unique perspective on program behavior at runtime. By mastering ltrace, you can:

Readers are encouraged to practice the various uses of ltrace on their own projects and gradually master this powerful debugging tool.


Linux Command Reference Linux Command Reference