Search

straceコマンド

概要

プロセスの調査などに使える。
プロセスが呼び出すシステムコールをトレースすることができる。
その内容を確認することで、エラー箇所の確認やプロセスの状況(ハング等)の判断材料とすることができる。

使い方

strace (command)
strace -p (対象プロセスID)

使用例

– コマンドのシステムコールをトレース
# strace ls -l

– プロセスのシステムコールをトレース
# strace -f -p (対象プロセスID) -o /tmp/strace_log_201508050000.log

表示例

# strace ls
execve("/bin/ls", ["ls"], [/* 22 vars */]) = 0
brk(0)                                  = 0x239e000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2e354b5000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=36014, ...}) = 0
mmap(NULL, 36014, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f2e354ac000
close(3)                                = 0
open("/lib64/libselinux.so.1", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0PX\300\2011\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=124624, ...}) = 0
mmap(0x3181c00000, 2221912, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x3181c00000
mprotect(0x3181c1d000, 2093056, PROT_NONE) = 0
mmap(0x3181e1c000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c000) = 0x3181e1c000
mmap(0x3181e1e000, 1880, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x3181e1e000
close(3)                                = 0
open("/lib64/librt.so.1", O_RDONLY)     = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@!\200\2011\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=47112, ...}) = 0
mmap(0x3181800000, 2128816, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x3181800000
mprotect(0x3181807000, 2093056, PROT_NONE) = 0
mmap(0x3181a06000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x6000) = 0x3181a06000
close(3)                                = 0
open("/lib64/libcap.so.2", O_RDONLY)    = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0000\23\300\2031\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=19016, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2e354ab000
mmap(0x3183c00000, 2111776, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x3183c00000
mprotect(0x3183c04000, 2093056, PROT_NONE) = 0
mmap(0x3183e03000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x3000) = 0x3183e03000
close(3)                                = 0
open("/lib64/libacl.so.1", O_RDONLY)    = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200\36@\2031\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=33816, ...}) = 0
mmap(0x3183400000, 2126416, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x3183400000
mprotect(0x3183407000, 2093056, PROT_NONE) = 0
mmap(0x3183606000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x6000) = 0x3183606000
close(3)                                = 0
open("/lib64/libc.so.6", O_RDONLY)      = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0p\356A\2001\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1926760, ...}) = 0
mmap(0x3180400000, 3750152, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x3180400000
mprotect(0x318058a000, 2097152, PROT_NONE) = 0
mmap(0x318078a000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x18a000) = 0x318078a000
mmap(0x318078f000, 18696, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x318078f000
close(3)                                = 0
open("/lib64/libdl.so.2", O_RDONLY)     = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340\r\300\2001\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=22536, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2e354aa000
mmap(0x3180c00000, 2109696, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x3180c00000
mprotect(0x3180c02000, 2097152, PROT_NONE) = 0
mmap(0x3180e02000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x3180e02000
close(3)                                = 0
open("/lib64/libpthread.so.0", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340]\200\2001\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=145896, ...}) = 0
mmap(0x3180800000, 2212848, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x3180800000
mprotect(0x3180817000, 2097152, PROT_NONE) = 0
mmap(0x3180a17000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x17000) = 0x3180a17000
mmap(0x3180a19000, 13296, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x3180a19000
close(3)                                = 0
open("/lib64/libattr.so.1", O_RDONLY)   = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200\23@\2051\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=21152, ...}) = 0
mmap(0x3185400000, 2113888, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x3185400000
mprotect(0x3185404000, 2093056, PROT_NONE) = 0
mmap(0x3185603000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x3000) = 0x3185603000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2e354a9000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2e354a7000
arch_prctl(ARCH_SET_FS, 0x7f2e354a77a0) = 0
mprotect(0x3181e1c000, 4096, PROT_READ) = 0
mprotect(0x3181a06000, 4096, PROT_READ) = 0
mprotect(0x3183606000, 4096, PROT_READ) = 0
mprotect(0x318078a000, 16384, PROT_READ) = 0
mprotect(0x3180e02000, 4096, PROT_READ) = 0
mprotect(0x318021f000, 4096, PROT_READ) = 0
mprotect(0x3180a17000, 4096, PROT_READ) = 0
mprotect(0x3185603000, 4096, PROT_READ) = 0
munmap(0x7f2e354ac000, 36014)           = 0
set_tid_address(0x7f2e354a7a70)         = 3164
set_robust_list(0x7f2e354a7a80, 0x18)   = 0
futex(0x7fffc4c2a27c, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x7fffc4c2a27c, FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME, 1, NULL, 7f2e354a77a0) = -1 EAGAIN (Resource temporarily unavailable)
rt_sigaction(SIGRTMIN, {0x3180805c60, [], SA_RESTORER|SA_SIGINFO, 0x318080f710}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {0x3180805cf0, [], SA_RESTORER|SA_RESTART|SA_SIGINFO, 0x318080f710}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
getrlimit(RLIMIT_STACK, {rlim_cur=10240*1024, rlim_max=RLIM_INFINITY}) = 0
statfs("/selinux", {f_type="EXT2_SUPER_MAGIC", f_bsize=4096, f_blocks=51026379, f_bfree=46916420, f_bavail=44324408, f_files=12967936, f_ffree=12741645, f_fsid={2135385419, -447885984}, f_namelen=255, f_frsize=4096}) = 0
brk(0)                                  = 0x239e000
brk(0x23bf000)                          = 0x23bf000
open("/proc/filesystems", O_RDONLY)     = 3
fstat(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2e354b4000
read(3, "nodev\tsysfs\nnodev\trootfs\nnodev\tb"..., 1024) = 304
read(3, "", 1024)                       = 0
close(3)                                = 0
munmap(0x7f2e354b4000, 4096)            = 0
open("/usr/lib/locale/locale-archive", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=99154480, ...}) = 0
mmap(NULL, 99154480, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f2e2f617000
close(3)                                = 0
ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
ioctl(1, TIOCGWINSZ, {ws_row=70, ws_col=118, ws_xpixel=736, ws_ypixel=960}) = 0
open(".", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
fcntl(3, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
getdents(3, /* 3 entries */, 32768)     = 80
getdents(3, /* 0 entries */, 32768)     = 0
close(3)                                = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2e354b4000
write(1, "test.txt\n", 9test.txt
)               = 9
close(1)                                = 0
munmap(0x7f2e354b4000, 4096)            = 0
close(2)                                = 0
exit_group(0)                           = ?

取り急ぎは、上記コマンドでトレースは可能。
不足の場合は、manを見ること。