웹사이트 검색

초보자를 위한 Linux strace 명령 자습서(예제 8개)


이 페이지에서

  1. Q1. strace 명령을 사용하는 방법?\n
  2. Q2. strace 출력을 이해하는 방법은 무엇입니까?\n
  3. Q3. strace 인쇄 명령 포인터를 만드는 방법은 무엇입니까?
  4. Q4. 각 시스템 호출에 대해 strace 인쇄 타임 스탬프를 만드는 방법은 무엇입니까?\n
  5. Q5. 각 출력 라인에 시계 시간을 접두사로 지정하는 방법은 무엇입니까?\n
  6. Q6. strace가 시스템 호출에 소요된 시간을 표시하는 방법은 무엇입니까?\n
  7. Q7. strace가 일반적인 출력 대신 요약을 인쇄하는 방법은 무엇입니까?\n
  8. 결론

Linux 명령줄은 소프트웨어 개발자에게 유용한 많은 도구를 제공합니다. 그중 하나는 Linux 시스템 호출 및 신호를 추적하는 명령인 strace입니다. 이해하기 쉬운 몇 가지 예를 사용하여 이 자습서에서 잘 논의되는 strace 기본 사항입니다.

그러나 그 전에 이 기사의 모든 예제는 Ubuntu 22.04 LTS 및 Debian 11 시스템에서 테스트되었음을 언급할 가치가 있습니다.

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가 제공하는 두 가지 관련 옵션이 더 있습니다.

-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

따라서 요약은 각 시스템 호출에 대한 시간 관련 정보뿐만 아니라 시스템 호출당 수행된 호출 수에 대한 아이디어를 제공합니다.

결론

strace 명령은 다른 많은 기능도 제공하므로 여기에서 표면을 긁었습니다. 여기에서 논의한 내용을 연습했으면 strace 매뉴얼 페이지로 이동하여 도구에 대해 자세히 알아보세요.