LLI Wiki — My Digital Knowledge Base

Tracing system calls with strace

Use strace to trace a program's file access and network calls, and save a timestamped trace to a log file.

strace prints the system calls a program makes. It is useful to see which files a program opens or which network connections it makes, without access to its source code.

Trace file access

strace -e open,access -o trace.txt <command> [arguments]

For example, to see which files an Oracle client reads:

strace -e open,access -o trace.txt sqlplus user/<password>@<service>

Trace network calls only

strace -e poll,select,connect,recvfrom,sendto nc example.com 80
strace -f -e trace=network <command> 2>&1 | grep sin_addr
strace -f -e trace=network -p <PID> 2>&1 | grep sin_addr

The grep sin_addr filter shows the destination addresses of the connect calls. -f follows child processes and -p attaches to a running process.

Full trace with timestamps and a log file

strace -f -tt <program> <arguments> 2>&1 | tee strace-<program>.log

-tt adds a microsecond timestamp to each line.

Reference

Notes

Mastodon LLIMastodon SocialFosstodon