Common options for using the Linux strace command to track/debug programs

Source: Internet
Author: User

Translation: Lctt https://linux.cn/article-3935-1.html Translator: Guodongxiaren

During debugging, Strace can help you trace the system calls that a program executes. This is extremely handy when you want to know how the program interacts with the operating system, for example, you want to know what system calls are being executed and in what order.

This simple and powerful tool is available on almost all Linux operating systems and can be used to debug a large number of programs.

Command usage

Let's see how the strace command tracks the execution of a program.

In the simplest form, strace can be followed by any command. It will list dozens of system calls. At first, we don't understand all the output, but if you're looking for something special, you should be able to find it from the output.

Let's take a look at the simple command LS system call tracking situation.

    1. [Email protected] -linoxide ~ $ strace ls

Stracing ls command

This is the first few lines of the strace command output. The other outputs are truncated.

Strace Write system call (LS)

The output section above shows the write system call, which outputs the list of current directories to standard output.

The following picture shows the contents of the directory listed using the LS command (no strace is used).

    1. [Email protected] -linoxide ~ $ ls

ls command output

Option 1 Find the configuration file read by the program

One of the uses of Strace (in addition to debugging some problems) is that you can find a configuration file that is read by a program. For example

    1. [Email protected] -linoxide ~ $ strace php 2>&1 | grep php. INI

Strace config file read by program

Option 2 tracks the specified system call

The-e option of the Strace command is only used to show specific system calls (for example, open,write, etc.)

Let's trace the ' open ' system call to the Cat command.

    1. [Email protected] -linoxide ~ $ strace -e open cat dead. Letter

Stracing Specific system call (open here)

Option 3 Trace Process

Strace can be used not only on commands, but also on running processes by using the-p option.

    1. [Email protected] -linoxide ~ $ sudo strace -p 1846

Strace a process

Option 4 Statistical Summary of Strace

It includes a summary of system calls, execution time, errors, and so on. Use the-C option to display in a neat way:

    1. [Email protected] -linoxide ~ $ strace -c ls

Strace Summary Display

Option 5 Saving output results

You can save the output of the Strace command to a file by using the-o option.

    1. [Email protected] -linoxide ~ $ sudo strace -o process_strace -p 3229

Strace a process

The above command is run with Sudo to prevent a situation where the user ID does not match the owner ID of the process being viewed.

Option 6 shows timestamps

With the-t option, you can add a timestamp before the output of each row.

    1. [Email protected] -linoxide ~ $ strace -t ls

Timestamp before each output line

Option 7 Finer Timestamp

The-TT option can display a microsecond-level timestamp.

    1. [Email protected] -linoxide ~ $ strace -tt ls

Time-microseconds

-TTT can also show microsecond timestamps to the above, but instead of printing the current time, it shows the number of seconds that have elapsed since the epoch (January 1, 1970, 00:00:00 UTC).

    1. [Email protected] -linoxide ~ $ strace -ttt ls

Seconds since epoch

Option 8 Relative time

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

    1. [Email protected] -linoxide ~ $ strace -r ls

Relative Timestamp

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

Raghu Translator: Guodongxiaren proofreading: Wxy

This article by LCTT original translation, Linux China honors launch

Source: https://linux.cn/article-3935-1.html

Common options for using the Linux strace command to track/debug programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.