系统命令位于C库的更上层,是利用C库实现的可执行程序,比如最为常用的ls、cd等命令。
In the simplest case strace runs the specified command until it exits. It intercepts and records the system calls which are called by a process and the signals which are received by a process. The name of each system call, its arguments and its return value are printed on standard error or to the file specified with the -o option.
strace is a useful diagnostic, instructional, and debugging tool. System administrators, diagnosticians and trouble-shooters will find it invaluable for solving problems with programs for which the source is not readily available since they do not need to be recompiled in order to trace them. Students, hackers and the overly-curious will find that a great deal can be learned about a system and its system calls by tracing even ordinary programs. And programmers will find that since system calls and signals are events that happen at the user/kernel interface, a close examination of this boundary is very useful for bug isolation, sanity checking and attempting to capture race conditions.
strace工具可以跟踪命令的执行,使用希望跟踪的命令为参数,并显示出该命令执行过程中所使用到的所有系统调用。比如,如果希望了解在执行pwd命令时都调用了哪些系统调用,可以使用下面的命令:
- $strace pwd
结果会产生大量的信息,显示出pwd命令执行过程中所调用到的各个系统调用:
- ……
- write(1, "/usr/src/linux-2.6.23\n", 22/usr/src/linux-2.6.23) = 22
- close(1) = 0
- munmap(0xb7f5a000, 4096) = 0
- exit_group(0)