Strace

為什麼我在壓力命令上執行 strace 時看不到 malloc 和 free systemcall

  • October 12, 2018

--vm指揮旗stress說,

-m, –vm N spawn N 個在 malloc()/free() 上旋轉的工人

我想看看它實際上在做什麼,所以我執行了這個命令,

strace stress --vm 1

我得到以下輸出,

execve("/bin/stress", ["stress", "--vm", "1"], [/* 27 vars */]) = 0
brk(NULL)                               = 0x55f05bef2000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fce9f15a000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=40388, ...}) = 0
mmap(NULL, 40388, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fce9f150000
close(3)                                = 0
open("/lib64/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0pS\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1139680, ...}) = 0
mmap(NULL, 3150136, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fce9ec38000
mprotect(0x7fce9ed39000, 2093056, PROT_NONE) = 0
mmap(0x7fce9ef38000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x100000) = 0x7fce9ef38000
close(3)                                = 0
open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P%\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2173512, ...}) = 0
mmap(NULL, 3981792, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fce9e86b000
mprotect(0x7fce9ea2e000, 2093056, PROT_NONE) = 0
mmap(0x7fce9ec2d000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c2000) = 0x7fce9ec2d000
mmap(0x7fce9ec33000, 16864, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fce9ec33000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fce9f14f000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fce9f14d000
arch_prctl(ARCH_SET_FS, 0x7fce9f14d740) = 0
mprotect(0x7fce9ec2d000, 16384, PROT_READ) = 0
mprotect(0x7fce9ef38000, 4096, PROT_READ) = 0
mprotect(0x55f05abdb000, 4096, PROT_READ) = 0
mprotect(0x7fce9f15b000, 4096, PROT_READ) = 0
munmap(0x7fce9f150000, 40388)           = 0
getpid()                                = 5305
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) = 0x7fce9f159000
write(1, "stress: info: [5305] dispatching"..., 64stress: info: [5305] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
) = 64
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fce9f14da10) = 5306
wait4(-1

輸出只是停留在這裡,我也沒有看到任何malloc()呼叫。我怎樣才能看到它進行malloc()和系統free()呼叫?

malloc並且free是庫呼叫而不是系統呼叫。這就是為什麼它們出現在手冊的第 3 節中

然而壓力不使用 malloc 和 free,而是通過mmapand呼叫核心分配器munmap

因為壓力在子程序中執行此操作,您需要指定 -f 來 strace 以使這些呼叫可見。

strace -f stress --vm 1

引用自:https://unix.stackexchange.com/questions/474950