TranslateProject/sources/tech/20140819 8 Options to Trace or Debug Programs using Linux strace Command.md
2014-08-19 16:30:30 +08:00

4.7 KiB
Raw Blame History

8 Options to Trace/Debug Programs using Linux strace Command

The strace is the tool that helps in debugging issues by tracing system calls executed by a program. It is handy when you want to see how the program interacts with the operating system, like what system calls are executed in what order.

This simple yet very powerful tool is available for almost all the Linux based operating systems and can be used to debug a large number of programs.

1. Command Usage

Lets see how we can use strace command to trace the execution of a program.

In the simplest form, any command can follow strace. It will list a whole lot of system calls. Not all of it would make sence at first, but if youre really looking for something particular, then you should be able to figure something out of this output. Lets see the system calls trace for simple ls command.

raghu@raghu-Linoxide ~ $ strace ls

Stracing ls command

This output shows the first few lines for strace command. The rest of the output is truncated.

Strace write system call (ls)

The above part of the output shows the write system call where it outputs to STDOUT the current directorys listing. Following image shows the listing of the directoy by ls command (without strace).

raghu@raghu-Linoxide ~ $ ls

ls command output

1.1 Find configuration file read by program

One use of strace (Except debugging some problem) is that you can find out which configuration files are read by a program. For example,

raghu@raghu-Linoxide ~ $ strace php 2>&1 | grep php.ini

Strace config file read by program

1.2 Trace specific system call

The -e option to strace command can be used to display certain system calls only (for example, open, write etc.)

Lets trace only open system call for cat command.

raghu@raghu-Linoxide ~ $ strace -e open cat dead.letter

Stracing specific system call (open here)

1.3 Stracing a process

The strace command can not only be used on the commands, but also on the running processes with -p option.

raghu@raghu-Linoxide ~ $ sudo strace -p 1846

Strace a process

1.4 Statistical summary of strace

The summary of the system calls, time of execution, errors etc. can be displayed in a neat manner with -c option:

raghu@raghu-Linoxide ~ $ strace -c ls

Strace summary display

1.5 Saving output

The output of strace command can be saved into a file with -o option.

raghu@raghu-Linoxide ~ $ sudo strace -o process_strace -p 3229

Strace a process

The above command is run with sudo as it will display error in case the user ID does not match with the process owner.

1.6 Displaying timestamp

The timestamp can be displayed before each output line with -t option.

raghu@raghu-Linoxide ~ $ strace -t ls

Timestamp before each output line

1.7 The Finer timestamp

The -tt option displays timestamp followed by microsecond.

raghu@raghu-Linoxide ~ $ strace -tt ls

Time - Microseconds

The -ttt displays microseconds like above, but instead of printing surrent time, it displays the number of seconds since the epoch.

raghu@raghu-Linoxide ~ $ strace -ttt ls

Seconds since epoch

1.8 Relative Time

The -r option displays the relative timestamp between the system calls.

raghu@raghu-Linoxide ~ $ strace -r ls

Relative Timestamp


via: http://linoxide.com/linux-command/linux-strace-command-examples/

作者:Raghu 译者:译者ID 校对:校对者ID

本文由 LCTT 原创翻译,Linux中国 荣誉推出