GNU/Linux >> Linux の 問題 >  >> Linux

初心者向けのLinuxstraceコマンドチュートリアル(8例)

Linuxコマンドラインは、ソフトウェア開発者に役立つ多くのツールを提供します。その1つがstraceです 、このチュートリアルでは、わかりやすい例を使用して基本について説明します。

ただし、その前に、この記事のすべての例がUbuntu18.04LTSおよびDebian10マシンでテストされていることを言及する価値があります。

Linuxのstraceコマンドを使用すると、システムコールとシグナルをトレースできます。その構文は次のとおりです。

strace [OPTIONS] command

そして、ツールのマニュアルページで説明されているのは次のとおりです。

       In  the simplest case strace runs the specified command until it exits.
       It intercepts and records the  system  calls  which  are  called  by  a
       process  and  the signals which are received by a process.  The name of
       each system call, its arguments and its return  value  are  printed  on
       standard error or to the file specified with the -o option.

strace is a useful diagnostic, instructional, and debugging tool.  Sys?
       tem administrators, diagnosticians and trouble-shooters  will  find  it
       invaluable  for  solving problems with programs for which the source is
       not readily available since they do not need to be recompiled in  order
       to trace them.  Students, hackers and the overly-curious will find that
       a great deal can be learned about a system  and  its  system  calls  by
       tracing  even  ordinary programs.  And programmers will find that since
       system calls and signals are events  that  happen  at  the  user/kernel
       interface,  a close examination of this boundary is very useful for bug
       isolation, sanity checking and attempting to capture race conditions.

以下は、straceコマンドがどのように機能するかをよりよく理解するためのQ&Aスタイルの例です。

straceコマンドのインストール

straceコマンドは、ほとんどのシステムにデフォルトでインストールされていません。DebianおよびUbuntuにインストールするには、次のコマンドを実行します。

sudo apt-get install strace

Q1。 straceコマンドの使用方法は?

基本的な使い方は簡単で、コマンドを入力として「strace」を実行するだけです。たとえば、lsコマンドで使用しました:

strace ls

そして、これが私のシステムで生成された出力です:

Q2。 strace出力を理解する方法は?

前のセクションのスクリーンショットでわかるように、straceコマンドは大量の出力を生成します。したがって、それを理解する方法を知っておく必要があります。

マニュアルページからの次の抜粋は、要点を説明しています。

       Each line in the trace contains the system call name, followed  by  its
       arguments  in parentheses and its return value.  An example from strac?
       ing the command "cat /dev/null" is:

           open("/dev/null", O_RDONLY) = 3

       Errors (typically a return value of -1) have the errno symbol and error
       string appended.

           open("/foo/bar", O_RDONLY) = -1 ENOENT (No such file or directory)

       Signals are printed as signal symbol and decoded siginfo structure.  An
       excerpt from stracing and interrupting the command "sleep 666" is:

           sigsuspend([] <unfinished ...>
           --- SIGINT {si_signo=SIGINT, si_code=SI_USER, si_pid=...} ---
           +++ killed by SIGINT +++

Q3。 strace印刷命令ポインタを作成するにはどうすればよいですか?

システムコール時に命令ポインタを出力するようにstraceに指示するオプション-iがあります。

例:

strace -i ls

出力は次のとおりです:

これで、出力の各行に命令ポインターが出力されたことがわかります。

Q4。各システムコールのstrace印刷タイムスタンプを作成するにはどうすればよいですか?

各システムコールへのエントリ時に相対タイムスタンプを表示するようにstraceに指示する-rコマンドラインオプションがあります。ツールのマニュアルページには、連続するシステムコールの開始間の時間差が記録されていると記載されています。

例:

strace -r ls

このコマンドによって生成される出力は次のとおりです。

したがって、相対タイムスタンプがすべての行の先頭に生成されたことがわかります。

Q5。各出力行の前にクロック時間を付けるにはどうすればよいですか?

strace出力の各行をクロック時間で開始する場合は、-tコマンドラインオプションを使用してこれを実行できます。

例:

strace -t ls

これが私のシステムでのこのコマンドの出力です:

したがって、各行の先頭にシステム時刻が出力されていることがわかります。

straceが提供する関連オプションがさらに2つあることに注意してください:

-tt         
If given twice, the time printed will include the microseconds.

-ttt       
If given thrice, the  time  printed  will  include  the microseconds and the leading portion will
be printed as the number of seconds since the epoch.

Q6。システムコールに費やされた時間をstraceに表示するにはどうすればよいですか?

これは、-Tコマンドラインオプションを使用して実現できます。

例:

strace -T ls

出力は次のとおりです。

システムコールに費やされた時間が各行の最後に印刷されていることがわかります。

Q7。 straceに通常の出力の代わりに要約を出力させる方法は?

ツールで要約を生成する場合は、-cコマンドライン出力を使用できます。

たとえば、次のコマンド:

strace -c ls

私のシステムでこの出力を生成しました:

% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 93.66    0.000133           5        28           write
  6.34    0.000009           1        11           close
  0.00    0.000000           0         7           read
  0.00    0.000000           0        10           fstat
  0.00    0.000000           0        17           mmap
  0.00    0.000000           0        12           mprotect
  0.00    0.000000           0         1           munmap
  0.00    0.000000           0         3           brk
  0.00    0.000000           0         2           rt_sigaction
  0.00    0.000000           0         1           rt_sigprocmask
  0.00    0.000000           0         2           ioctl
  0.00    0.000000           0         8         8 access
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         2           getdents
  0.00    0.000000           0         2         2 statfs
  0.00    0.000000           0         1           arch_prctl
  0.00    0.000000           0         1           set_tid_address
  0.00    0.000000           0         9           openat
  0.00    0.000000           0         1           set_robust_list
  0.00    0.000000           0         1           prlimit64
------ ----------- ----------- --------- --------- ----------------
100.00    0.000142                   120        10 total

そのため、概要から、syscallごとに行われた呼び出しの数と、各syscallの時間関連情報がわかります。

結論

straceコマンドは他にも多くの機能を提供するため、ここで表面をかじったところです。ここで説明したすべての練習が終わったら、straceのmanページにアクセスして、ツールの詳細を確認してください。


Linux
  1. 初心者向けのLinuxcdコマンドチュートリアル(8例)

  2. 初心者向けのLinux通信コマンドチュートリアル(5例)

  3. 初心者向けのLinux日付コマンドチュートリアル(8例)

  1. 初心者向けのLinuxdfコマンドチュートリアル(8例)

  2. 初心者向けのLinuxduコマンドチュートリアル(10例)

  3. 初心者向けのLinuxechoコマンドチュートリアル(5つの例)

  1. 初心者向けのLinuxenvコマンドチュートリアル(5つの例)

  2. 初心者向けのLinuxサイズのコマンドチュートリアル(6つの例)

  3. 初心者向けのLinuxkillコマンドチュートリアル(5つの例)