3.3.2. Monitoring an application’s system calls with strace
To monitor the system (kernel) calls performed by an application, use the strace tool.
Prerequisites
Procedure
Identify the system calls to monitor.
Start
straceand attach it to the program.If the program you want to monitor is not running, start
straceand specify the program:$ strace -fvttTyy -s 256 -e trace=call programIf the program is already running, find its process id (pid):
$ ps -C programAttach
straceto the process:$ strace -fvttTyy -s 256 -e trace=call -ppid
-
Replace call with the system calls to be displayed. You can use the
-e trace=calloption multiple times. If left out,stracewill display all system call types. See the strace(1) manual page for more information. -
If you do not want to trace any forked processes or threads, omit the
-foption.
The
stracetool displays the system calls made by the application and their details.In most cases, an application and its libraries make a large number of calls and
straceoutput displays immediately, if no filter for system calls is set.The
stracetool exits when the program exits.To terminate the monitoring before the traced program exits, press .
-
If
stracestarted the program, the program terminates together withstrace. -
If you attached
straceto an already running program, the program terminates together withstrace.
-
If
Analyze the list of system calls done by the application.
- Problems with resource access or availability are present in the log as calls returning errors.
- Values passed to the system calls and patterns of call sequences provide insight into the causes of the application’s behaviour.
- If the application crashes, the important information is probably at the end of log.
The output contains a large amount of unnecessary information. However, you can construct a more precise filter for the system calls of interest and repeat the procedure.
注意It is advantageous to both see the output and save it to a file. Use the
teecommand to achieve this:$ strace ... |& tee your_log_file.log