在看CPUE中的一道习题中说:
UNIX提供了一条命令,它允许我们执行另一条命令,并可以跟踪该命令执行的所有系统调用。
在SVR4中该命令为truss(1),在4.3 + BSD中为ktrace(1)和kdump(1),
下例使用SunOS的trace(1)跟踪file命令中的系统调用:
trace file a.out
下面是f i l e调用的函数:
lstat ("a.out", 0xf7fff650) = 0
open ("a.out", 0, 0) = 3
read (3,"".., 512) = 512
fstat (3,0xf7fff160) = 0
write (1, "a.out: demand paged execu" .., 44) = 44
a.out: demand paged executable not stripped
utime ("a.out", 0xf7fff1b0) = 0
上网上找了下,linux下的为 strace
strace会跟踪命令过程中所执行的系统调用……
示例:
[13:46:05]$strace date
execve("/bin/date", ["date"], [/* 66 vars */]) = 0
uname({sys="Linux", node="zzyw1", ...}) = 0
brk(0) = 0x8053000
open("/etc/ld.so.preload", O_RDONLY) = -1 ENOENT (No such file or directory)
old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40017000
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=101266, ...}) = 0
old_mmap(NULL, 101266, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40018000
close(3) = 0
open("/lib/tls/librt.so.1", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\340\37"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=36411, ...}) = 0
old_mmap(NULL, 26120, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x40031000
madvise(0x40031000, 26120, MADV_SEQUENTIAL|0x1) = 0
old_mmap(0x40037000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3, 0x6000) = 0x40037000
close(3) = 0
open("/lib/tls/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0PS\1\000"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=1345705, ...}) = 0
old_mmap(NULL, 1133004, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x40038000
madvise(0x40038000, 1133004, MADV_SEQUENTIAL|0x1) = 0
old_mmap(0x40142000, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3, 0x109000) = 0x40142000
old_mmap(0x4014a000, 10700, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x4014a000
close(3) = 0
open("/lib/tls/libpthread.so.0", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\360I\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=88315, ...}) = 0
old_mmap(NULL, 64972, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x4014d000
madvise(0x4014d000, 64972, MADV_SEQUENTIAL|0x1) = 0
old_mmap(0x4015a000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3, 0xd000) = 0x4015a000
old_mmap(0x4015b000, 7628, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x4015b000
close(3) = 0
old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x4015d000
set_thread_area({entry_number:-1 -> 6, base_addr:0x4015d2a0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0
munmap(0x40018000, 101266) = 0
set_tid_address(0x4015d2e8) = 11696
rt_sigaction(SIGRTMIN, {0x40151950, [], SA_SIGINFO}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN], NULL, 8) = 0
getrlimit(RLIMIT_STACK, {rlim_cur=RLIM_INFINITY, rlim_max=RLIM_INFINITY}) = 0
_sysctl({{CTL_KERN, KERN_VERSION}, 2, 0xbffff088, 31, (nil), 0}) = 0
brk(0) = 0x8053000
brk(0x8074000) = 0x8074000
brk(0) = 0x8074000
open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/locale.alias", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=2528, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40018000
read(3, "# Locale name alias data base.\n#"..., 4096) = 2528
read(3, "", 4096) = 0
close(3) = 0
munmap(0x40018000, 4096) = 0
open("/usr/lib/locale/zh_CN.gbk/LC_IDENTIFICATION", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=306, ...}) = 0
mmap2(NULL, 306, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40018000
close(3) = 0
open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=21512, ...}) = 0
mmap2(NULL, 21512, PROT_READ, MAP_SHARED, 3, 0) = 0x40019000
close(3) = 0
futex(0x40149f0c, FUTEX_WAKE, 2147483647) = 0
open("/usr/lib/locale/zh_CN.gbk/LC_MEASUREMENT", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=21, ...}) = 0
mmap2(NULL, 21, PROT_READ, MAP_PRIVATE, 3, 0) = 0x4001f000
close(3) = 0
open("/usr/lib/locale/zh_CN.gbk/LC_TELEPHONE", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=55, ...}) = 0
mmap2(NULL, 55, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40020000
close(3) = 0
open("/usr/lib/locale/zh_CN.gbk/LC_ADDRESS", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=150, ...}) = 0
mmap2(NULL, 150, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40021000
close(3) = 0
open("/usr/lib/locale/zh_CN.gbk/LC_NAME", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=72, ...}) = 0
mmap2(NULL, 72, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40022000
close(3) = 0
open("/usr/lib/locale/zh_CN.gbk/LC_PAPER", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=32, ...}) = 0
mmap2(NULL, 32, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40023000
close(3) = 0
open("/usr/lib/locale/zh_CN.gbk/LC_MESSAGES", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
close(3) = 0
open("/usr/lib/locale/zh_CN.gbk/LC_MESSAGES/SYS_LC_MESSAGES", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=52, ...}) = 0
mmap2(NULL, 52, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40024000
close(3) = 0
open("/usr/lib/locale/zh_CN.gbk/LC_MONETARY", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=288, ...}) = 0
mmap2(NULL, 288, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40025000
close(3) = 0
open("/usr/lib/locale/zh_CN.gbk/LC_COLLATE", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=846344, ...}) = 0
mmap2(NULL, 846344, PROT_READ, MAP_PRIVATE, 3, 0) = 0x4015e000
close(3) = 0
open("/usr/lib/locale/zh_CN.gbk/LC_TIME", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=2149, ...}) = 0
mmap2(NULL, 2149, PROT_READ, MAP_PRIVATE, 3, 0) = 0x4022d000
close(3) = 0
open("/usr/lib/locale/zh_CN.gbk/LC_NUMERIC", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=52, ...}) = 0
mmap2(NULL, 52, PROT_READ, MAP_PRIVATE, 3, 0) = 0x4022e000
close(3) = 0
open("/usr/lib/locale/zh_CN.gbk/LC_CTYPE", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=189852, ...}) = 0
mmap2(NULL, 189852, PROT_READ, MAP_PRIVATE, 3, 0) = 0x4022f000
close(3) = 0
open("/usr/share/locale/zh_CN.gbk/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/zh_CN/LC_MESSAGES/coreutils.mo", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=62133, ...}) = 0
mmap2(NULL, 62133, PROT_READ, MAP_PRIVATE, 3, 0) = 0x4025e000
close(3) = 0
open("/usr/lib/gconv/GBK.so", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0p\6\0\000"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=109540, ...}) = 0
old_mmap(NULL, 112300, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x4026e000
madvise(0x4026e000, 112300, MADV_SEQUENTIAL|0x1) = 0
old_mmap(0x40289000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3, 0x1a000) = 0x40289000
close(3) = 0
open("/usr/share/locale/zh.gbk/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/zh/LC_MESSAGES/coreutils.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
clock_gettime(CLOCK_REALTIME, {1207547170, 744588000}) = 0
open("/etc/localtime", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=165, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x4028a000
read(3, "TZif\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\3\0\0\0\3\0"..., 4096) = 165
close(3) = 0
munmap(0x4028a000, 4096) = 0
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x4028a000
write(1, "\322\273 4\324\302 7 13:46:10 CST 2008\n", 29一 4月 7 13:46:10 CST 2008
) = 29
munmap(0x4028a000, 4096) = 0
exit_group(0) = ?