您的位置:首页 > 运维架构 > Linux

一个好用的linux命令:strace

2009-05-15 10:37 501 查看
strace可以用来跟踪可执行程序,列出可执行程序使用的系统调用,当程序崩溃时,可用它来得到一定的启发。

看看wikipedia的描述:

strace is a debugging utility in Linux to monitor the system calls
used by a program and all the signals it receives, similar to "truss"
utility in other Unix systems. This is made possible by a kernel
feature known as ptrace.



The most common usage is to start a program using strace, which
prints a list of system calls made by the program. This is useful if
the program continually crashes, or does not behave as expected; for
example using strace may reveal that the program is attempting to
access a file which does not exist or cannot be read.

An alternative application is to use the -p flag to attach to a
running process. This is useful if a process has stopped responding,
and might reveal, for example, that the process is blocking whilst
attempting to make a network connection.

As strace only details system calls it cannot be used to detect as many problems as a code debugger such as Gdb. It is, however, easier to use than a code debugger, and is an extremely useful tool for system administrators.





一个例子:

[pfm-rhel1.corp.cnb:/home/biaozhan/cpptest]$ strace ./a.out
execve("./a.out", ["./a.out"], [/* 26 vars */]) = 0
uname({sys="Linux", node="pfm-rhel1.corp.cnb.yahoo.com", ...}) = 0
brk(0) = 0x8529000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=83638, ...}) = 0
old_mmap(NULL, 83638, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7feb000
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/0/20/357"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=1454462, ...}) = 0
old_mmap(0x29a000, 1219772, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x29a000
old_mmap(0x3be000, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x124000) = 0x3be000
old_mmap(0x3c2000, 7356, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x3c2000
close(3) = 0
old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fea000
mprotect(0x3be000, 4096, PROT_READ) = 0
mprotect(0x296000, 4096, PROT_READ) = 0
set_thread_area({entry_number:-1 -> 6, base_addr:0xb7fea940, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0
munmap(0xb7feb000, 83638) = 0
brk(0) = 0x8529000
brk(0x854a000) = 0x854a000
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fff000
write(1, "4/n", 24
) = 2
munmap(0xb7fff000, 4096) = 0
exit_group(0) = ?
Process 8279 detached
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: