[Linux]straceでシステムコールを確認する
$ strace echo hello
execve("/bin/echo", ["echo", "hello"], [/* 26 vars */]) = 0
brk(0) = 0x17074000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2af0fb434000
uname({sys="Linux", node="l********", ...}) = 0
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=57146, ...}) = 0
mmap(NULL, 57146, PROT_READ, MAP_PRIVATE, 3, 0) = 0x2af0fb435000
close(3) = 0
open("/lib64/libc.so.6", 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\220\332\1\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1712216, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2af0fb443000
mmap(NULL, 3498328, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x2af0fb635000
mprotect(0x2af0fb783000, 2093056, PROT_NONE) = 0
mmap(0x2af0fb982000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x14d000) = 0x2af0fb982000
mmap(0x2af0fb987000, 16728, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x2af0fb987000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2af0fb98c000
arch_prctl(ARCH_SET_FS, 0x2af0fb98c6e0) = 0
mprotect(0x2af0fb982000, 16384, PROT_READ) = 0
mprotect(0x2af0fb633000, 4096, PROT_READ) = 0
munmap(0x2af0fb435000, 57146) = 0
brk(0) = 0x17074000
brk(0x17095000) = 0x17095000
open("/usr/lib/locale/locale-archive", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=56409856, ...}) = 0
mmap(NULL, 56409856, PROT_READ, MAP_PRIVATE, 3, 0) = 0x2af0fb98d000
close(3) = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 41), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2af0fef59000
write(1, "hello\n", 6hello
) = 6
close(1) = 0
munmap(0x2af0fef59000, 4096) = 0
exit_group(0) = ?
実際の「標準出力にhelloを出力する処理」は、最後の方の
write(1, "hello\n"
というやつですね。
システムコールのマニュアルを見るには、
man 2 write
とタイプします。それを見ると、
- write()がシステムコールであること
- 引数1個目の1は、ファイル・ディスクリプタ番号であること
open,closeなども同じようにシステムコールですので、
man 2 open
などすればマニュアルを見ることができます。
カテゴリ:
Linux