Merge pull request #1636 from guodongxiaren/master

translated
This commit is contained in:
Xingyu.Wang 2014-09-17 13:34:57 +08:00
commit 0e845c0308
2 changed files with 113 additions and 113 deletions

View File

@ -1,113 +0,0 @@
guodongxiaren 翻译中
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](http://linoxide.com/wp-content/uploads/2014/08/01.strace_ls.png)
This output shows the first few lines for strace command. The rest of the output is truncated.
![Strace write system call (ls)](http://linoxide.com/wp-content/uploads/2014/08/02.strace_ls_write.png)
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](http://linoxide.com/wp-content/uploads/2014/08/03.ls_.png)
#### 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](http://linoxide.com/wp-content/uploads/2014/08/04.strace_php_configuration.png)
#### 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)](http://linoxide.com/wp-content/uploads/2014/08/05.strace_open_systemcall.png)
#### 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](http://linoxide.com/wp-content/uploads/2014/08/06.strace_process.png)
#### 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](http://linoxide.com/wp-content/uploads/2014/08/07.strace_summary.png)
#### 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](http://linoxide.com/wp-content/uploads/2014/08/08.strace_output_file.png)
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](http://linoxide.com/wp-content/uploads/2014/08/09.strace_timestamp.png)
#### 1.7 The Finer timestamp ####
The -tt option displays timestamp followed by microsecond.
raghu@raghu-Linoxide ~ $ strace -tt ls
![Time - Microseconds](http://linoxide.com/wp-content/uploads/2014/08/010.strace_finer_timestamp.png)
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](http://linoxide.com/wp-content/uploads/2014/08/011.strace_epoch_seconds.png)
#### 1.8 Relative Time ####
The -r option displays the relative timestamp between the system calls.
raghu@raghu-Linoxide ~ $ strace -r ls
![Relative Timestamp](http://linoxide.com/wp-content/uploads/2014/08/012.strace_relative-time.png)
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-command/linux-strace-command-examples/
作者:[Raghu][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://linoxide.com/author/raghu/

View File

@ -0,0 +1,113 @@
8 Options to Trace/Debug Programs using Linux strace Command
================================================================================
在调试的时候strace能帮助你追踪到一个程序所执行的系统调用。当你想知道程序和操作系统如何交互的时候这是极其方便的比如你想知道执行了哪些系统调用并且以何种顺序执行。
这个简单而又强大的工具几乎在所有的Linux操作系统上可用并且可被用来调试大量的程序。
### 1. 命令用法 ###
让我们看看strace命令如何追踪一个程序的执行情况。
最简单的形式strace后面可以跟任何命令。它将列出许许多多的系统调用。一开始我们并不能理解所有的输出但是如果你正在寻找一些特殊的东西那么你应该能从输出中发现它。
让我们来看看简单命令ls的系统调用跟踪情况。
raghu@raghu-Linoxide ~ $ strace ls
![Stracing ls command](http://linoxide.com/wp-content/uploads/2014/08/01.strace_ls.png)
这是strace命令输出的前几行。其他输出被截去了。
![Strace write system call (ls)](http://linoxide.com/wp-content/uploads/2014/08/02.strace_ls_write.png)
上面的输出部分展示了write系统调用它把当前目录的列表输出到标准输出。
下面的图片展示了使用ls命令列出的目录内容没有使用strace
raghu@raghu-Linoxide ~ $ ls
![ls command output](http://linoxide.com/wp-content/uploads/2014/08/03.ls_.png)
#### 1.1 寻找被程序读取的配置文件 ####
一个有用的跟踪(除了调试某些问题以外)是你能找到被一个程序读取的配置文件。例如,
raghu@raghu-Linoxide ~ $ strace php 2>&1 | grep php.ini
![Strace config file read by program](http://linoxide.com/wp-content/uploads/2014/08/04.strace_php_configuration.png)
#### 1.2 跟踪指定的系统调用 ####
strace命令的-e选项仅仅被用来展示特定的系统调用例如openwrite等等
让我们跟踪一下cat命令的open系统调用。
raghu@raghu-Linoxide ~ $ strace -e open cat dead.letter
![Stracing specific system call (open here)](http://linoxide.com/wp-content/uploads/2014/08/05.strace_open_systemcall.png)
#### 1.3 用于进程 ####
strace不但能用在命令上而且通过使用-p选项能用在运行的进程上。
raghu@raghu-Linoxide ~ $ sudo strace -p 1846
![Strace a process](http://linoxide.com/wp-content/uploads/2014/08/06.strace_process.png)
#### 1.4 strace的统计概要 ####
包括系统调用的概要,执行时间,错误等等。使用-c选项能够以一种整洁的方式展示
raghu@raghu-Linoxide ~ $ strace -c ls
![Strace summary display](http://linoxide.com/wp-content/uploads/2014/08/07.strace_summary.png)
#### 1.5 保存输出结果 ####
通过使用-o选项可以把strace命令的输出结果保存到一个文件中。
raghu@raghu-Linoxide ~ $ sudo strace -o process_strace -p 3229
![Strace a process](http://linoxide.com/wp-content/uploads/2014/08/08.strace_output_file.png)
之所以以sudo来运行上面的命令是为了防止用户ID与所查看进程的所有者ID不匹配的情况。
### 1.6 显示时间戳 ###
使用-t选项可以在每行的输出之前添加时间戳。
raghu@raghu-Linoxide ~ $ strace -t ls
![Timestamp before each output line](http://linoxide.com/wp-content/uploads/2014/08/09.strace_timestamp.png)
#### 1.7 更好的时间戳 ####
-tt选项可以展示微秒级别的时间戳。
raghu@raghu-Linoxide ~ $ strace -tt ls
![Time - Microseconds](http://linoxide.com/wp-content/uploads/2014/08/010.strace_finer_timestamp.png)
-ttt也可以向上面那样展示微秒级的时间戳但是它并不是打印当前时间而是显示自从epoch译注1970年1月1日00:00:00 UTC以来的所经过的秒数。
raghu@raghu-Linoxide ~ $ strace -ttt ls
![Seconds since epoch](http://linoxide.com/wp-content/uploads/2014/08/011.strace_epoch_seconds.png)
#### 1.8 Relative Time ####
-r选项展示系统调用之间的相对时间戳。
raghu@raghu-Linoxide ~ $ strace -r ls
![Relative Timestamp](http://linoxide.com/wp-content/uploads/2014/08/012.strace_relative-time.png)
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-command/linux-strace-command-examples/
作者:[Raghu][a]
译者:[guodongxiaren](https://github.com/guodongxiaren)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://linoxide.com/author/raghu/