strace

strace cheatsheet by Thamizhiniyan C S

Introduction

strace is a command-line linux tool used to trace system calls and signals

Use Cases:

  • Debugging Programs

  • Troubleshooting Programs

  • Intercept System calls by a process

  • Record system calls by a process

  • Signals received by a process

  • Trace running processes


Installation

# Debian
sudo apt install strace

# Fedora
yum install strace

Syntax

strace [OPTIONS] [EXECUTABLE_FILE]


Important Flags

FlagDescription

-c, --summary-only

count time, calls, and errors for each syscall and report summary

-C, --summary

like -c, but also print the regular output

-e trace=TRACE

trace only specified syscalls

-e signal=SET, --signal=SIGNALS

trace only the specified set of signals. print only the signals from SET

-e status=SET, --status=STATUS

print only system calls with the return statuses in SET

-f

Follow threads and child processes that are created.

-r

print relative timestamp

-i

print instruction pointer at time of syscall

-T, --syscall-times[=PRECISION]

print time spent in each syscall [precision: default is microseconds]

-t, --fields=LIS

print absolute timestamp of each system call (wall clock time)

-o FILE, --output=FILE

send trace output to FILE instead of stderr

-p PID, --attach=PID

trace process with process id PID, may be repeated

-u USERNAME, --user=USERNAME

run command as USERNAME handling setuid and/or setgid

-s [size]

Print [size] characters per string displayed. This is useful if you are trying to trace what a program is writing to a file descriptor.

TRACE values

open, close, write, network, signal

STATUS values

successful, failed, unfinished, unavailable, detached

PRECISION values

s, ms, us, ns


Examples

CommandDescription

Trace system calls of the `ls` command.

Count the number of system calls.

Trace only the `write` system calls of the `ls` command.

Trace network-related system calls of the `nc` command.

Trace signal-related system calls of the `nc` command.

Print the timestamp of each system call.

Print time spent on each system call.

Print wall clock time of each system call.

Print the instruction pointer of each system call.

Trace a running process by PID.

Trace a running process by PID.

Trace a running process and its threads.

Trace a running process, print first 80 characters of strings.

Trace a program.

Trace a program and its threads.

Trace a program, print first 80 characters of strings.

To exclude futex calls

Trace specific system calls (open, read, write) for a program and its threads.


References

https://www.geeksforgeeks.org/strace-command-in-linux-with-examples/

https://blog.packagecloud.io/strace-cheat-sheet/

Last updated