ablog

不器用で落着きのない技術者のメモ

macOS でシステムコールトレースを取得する

macOSLinux の strace のようにシステムコールトレースを取得したい場合は、dtruss で同等のことができる。

実行例

% sudo dtruss -deflo sudo -u $(id -u -n) sleep 1
	PID/THRD  RELATIVE  ELAPSD    CPU SYSCALL(args) 		 = return
83868/0x3967d6:      1084      30     27 open("/dev/dtracehelper\0", 0x2, 0x7FFF5DA57840)		 = 3 0
83868/0x3967d6:      1382     298    296 ioctl(0x3, 0x80086804, 0x7FFF5DA577C8)		 = 0 0
83868/0x3967d6:      1387       5      4 close(0x3)		 = 0 0
83868/0x3967d6:      1440       1      0 thread_selfid(0x3, 0x80086804, 0x7FFF5DA577C8)		 = 3762134 0
83868/0x3967d6:      1446       6      5 bsdthread_register(0x7FFFBBB0A080, 0x7FFFBBB0A070, 0x2000)		 = 1073741919 0
83868/0x3967d6:      1488       2      0 ulock_wake(0x1, 0x7FFF5DA56F2C, 0x0)		 = -1 Err#2
83868/0x3967d6:      1500       1      0 issetugid(0x1, 0x7FFF5DA56F2C, 0x0)		 = 0 0
83868/0x3967d6:      1573       3      2 mprotect(0x102205000, 0x88, 0x1)		 = 0 0
  • sudo dtruss sudo -u $(id -u -n) で dtruss は root でコマンドは実行ユーザーで実行している。
  • d: 相対タイムスタンプ(単位:マイクロ秒)
  • e: 経過時間(単位:マイクロ秒)
  • f: 子プロセスもトレースする
  • l: プロセスIDとスレッドIDを毎行表示する
  • o: CPU時間を表示 (単位:マイクロ秒)

man

NAME
       dtruss - process syscall details. Uses DTrace.

SYNOPSIS
       dtruss [-acdeflhoLs] [-t syscall] { -p PID | -n name | command }

DESCRIPTION
       dtruss prints details on process system calls. It is like a DTrace version of truss, and has been designed to be less intrusive
       than truss.

       Of particular interest is the elapsed times and on cpu times, which can identify both system calls that are slow  to  complete,
       and those which are consuming CPU cycles.

       Since this uses DTrace, only users with root privileges can run this command.

OPTIONS
       -a     print all details

       -b bufsize
              dynamic  variable  buffer  size.  Increase  this  if  you notice dynamic variable drop errors. The default is "4m" for 4
              megabytes per CPU.

       -c     print system call counts

       -d     print relative timestamps, us

       -e     print elapsed times, us

       -f     follow children as they are forked

       -l     force printing of pid/lwpid per line

       -L     don't print pid/lwpid per line

       -n name
              examine processes with this name

       -o     print on-cpu times, us

       -s     print stack backtraces

       -p PID examine this PID

       -t syscall
              examine this syscall only

環境