blob: 5f045f9667bad2fc045ffd293d337477c99a2add [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001.\" Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
2.\" Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
3.\" Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. The name of the author may not be used to endorse or promote products
15.\" derived from this software without specific prior written permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27.\"
28.\" $Id$
29.\"
30.de CW
31.sp
32.nf
33.ft CW
34..
35.de CE
36.ft
37.fi
38.sp
39..
40.TH STRACE 1 "96/02/13"
41.SH NAME
42strace \- trace system calls and signals
43.SH SYNOPSIS
44.B strace
45[
46.B \-dffhiqrtttTvxx
47]
48[
49.BI \-a column
50]
51[
52.BI \-e expr
53]
54\&...
55[
56.BI \-o file
57]
58[
59.BI \-p pid
60]
61\&...
62[
63.BI \-s strsize
64]
65[
66.BI \-u username
67]
68[
69.I command
70[
71.I arg
72\&...
73]
74]
75.sp
76.B strace
77.B \-c
78[
79.BI \-e expr
80]
81\&...
82[
83.BI \-O overhead
84]
85[
86.BI \-S sortby
87]
88[
89.I command
90[
91.I arg
92\&...
93]
94]
95.SH DESCRIPTION
96.IX "strace command" "" "\fLstrace\fR command"
97.LP
98In the simplest case
99.B strace
100runs the specified
101.I command
102until it exits.
103It intercepts and records the system calls which are called
104by a process and the signals which are received by a process.
105The name of each system call, its arguments and its return value
106are printed on standard error or to the file specified with the
107.B \-o
108option.
109.LP
110.B strace
111is a useful diagnositic, instructional, and debugging tool.
112System adminstrators, diagnosticians and trouble-shooters will find
113it invaluable for solving problems with
114programs for which the source is not readily available since
115they do not need to be recompiled in order to trace them.
116Students, hackers and the overly-curious will find that
117a great deal can be learned about a system and its system calls by
118tracing even ordinary programs. And programmers will find that
119since system calls and signals are events that happen at the user/kernel
120interface, a close examination of this boundary is very
121useful for bug isolation, sanity checking and
122attempting to capture race conditions.
123.LP
124Each line in the trace contains the system call name, followed
125by its arguments in parentheses and its return value.
126An example from stracing the command ``cat /dev/null'' is:
127.CW
128open("/dev/null", O_RDONLY) = 3
129.CE
130Errors (typically a return value of \-1) have the errno symbol
131and error string appended.
132.CW
133open("/foo/bar", O_RDONLY) = -1 ENOENT (No such file or directory)
134.CE
135Signals are printed as a signal symbol and a signal string.
136An excerpt from stracing and interrupting the command ``sleep 666'' is:
137.CW
138sigsuspend([] <unfinished ...>
139--- SIGINT (Interrupt) ---
140+++ killed by SIGINT +++
141.CE
142Arguments are printed in symbolic form with a passion.
143This example shows the shell peforming ``>>xyzzy'' output redirection:
144.CW
145open("xyzzy", O_WRONLY|O_APPEND|O_CREAT, 0666) = 3
146.CE
147Here the three argument form of open is decoded by breaking down the
148flag argument into its three bitwise-OR constituents and printing the
149mode value in octal by tradition. Where traditional or native
150usage differs from ANSI or POSIX, the latter forms are preferred.
151In some cases, strace output has proven to be more readable than
152the source.
153.LP
154Structure pointers are dereferenced and the members are displayed
155as appropriate. In all cases arguments are formatted in the most C-like
156fashion possible.
157For example, the essence of the command ``ls \-l /dev/null'' is captured as:
158.CW
159lstat("/dev/null", {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
160.CE
161Notice how the `struct stat' argument is dereferenced and how each member is
162displayed symbolically. In particular, observe how the st_mode member
163is carefully decoded into a bitwise-OR of symbolic and numeric values.
164Also notice in this example that the first argument to lstat is an input
165to the system call and the second argument is an output. Since output
166arguments not modified if the system call fails, arguments may not
167always be dereferenced. For example, retrying the ``ls \-l'' example
168with a non-existent file produces the following line:
169.CW
170lstat("/foo/bar", 0xb004) = -1 ENOENT (No such file or directory)
171.CE
172In this case the porch light is on but nobody is home.
173.LP
174Character pointers are dereferenced and printed as C strings.
175Non-printing characters in strings are normally represented by
176ordinary C escape codes.
177Only the first
178.I strsize
179(32 by default) bytes of strings are printed;
180longer strings have an ellipsis appended following the closing quote.
181Here is a line from ``ls \-l'' where the getpwuid library routine is
182reading the password file:
183.CW
184read(3, "root::0:0:System Administrator:/"..., 1024) = 422
185.CE
186While structures are annotated using curly braces, simple pointers
187and arrays are printed using square brackets with commas separating
188elements. Here is an example from the command ``id'' on a system with
189supplementary group ids:
190.CW
191getgroups(32, [100, 0]) = 2
192.CE
193On the other hand, bit-sets are also shown using square brackets
194but set elements are separated only by a space. Here is the shell
195preparing to execute an external command:
196.CW
197sigprocmask(SIG_BLOCK, [CHLD TTOU], []) = 0
198.CE
199Here the second argument is a bit-set of two signals, SIGCHLD and SIGTTOU.
200In some cases the bit-set is so full that printing out the unset
201elements is more valuable. In that case, the bit-set is prefixed by
202a tilde like this:
203.CW
204sigprocmask(SIG_UNBLOCK, ~[], NULL) = 0
205.CE
206Here the second argument represents the full set of all signals.
207.SH OPTIONS
208.TP 12
209.TP
210.B \-c
211Count time, calls, and errors for each system call and report a
212summary on program exit.
213.TP
214.B \-d
215Show some debugging output of strace itself on
216.I stderr .
217.TP
218.B \-f
219Trace child processes as they are created by currently traced
220processes as a result of the fork(2) system call. The new process is
221attached to as soon as its pid is known (through the return value of
222fork(2) in the parent process). This means that such children may run
223uncontrolled for a while (especially in the case of a vfork(2)), until
224the parent is scheduled again to complete its (v)fork(2) call.
225If the parent process decides to wait(2) for a child that is currently
226being traced, it is suspended until an appropriate child process either
227terminates or incurs a signal that would cause it to terminate (as
228determined from the child's current signal disposition).
229.TP
230.B \-ff
231If the
232.B \-o
233.I filename
234option is in effect, each processes trace is written to
235.I filename.pid
236where pid is the numeric process id of each process.
237.TP
238.B \-F
239On SunOS 4.x, this option has the effect of attempting to follow
240vforks by performing some dynamic linking trickery. Otherwise,
241vforks will not be followed even if
242.B \-f
243has been given.
244.TP
245.B \-h
246Print the help summary.
247.TP
248.B \-i
249Print the instruction pointer at the time of the system call.
250.TP
251.B \-q
252Suppress messages about attaching, detaching etc. This happens
253automatically when output is redirected to a file and the command
254is run directly instead of attaching.
255.TP
256.B \-r
257Print a relative timestamp upon entry to each system call. This
258records the time difference between the beginning of successive
259system calls.
260.TP
261.B \-t
262Prefix each line of the trace with the time of day.
263.TP
264.B \-tt
265If given twice, the time printed will include the microseconds.
266.TP
267.B \-ttt
268If given thrice, the time printed will include the microseconds
269and the leading portion will be printed as the number
270of seconds since the epoch.
271.TP
272.B \-T
273Show the time spent in system calls. This records the time
274difference between the beginning and the end of each system call.
275.TP
276.B \-v
277Print unabbreviated versions of environment, stat, termios, etc.
278calls. These structures are very common in calls and so the default
279behavior displays a reasonable subset of structure members. Use
280this option to get all of the gory details.
281.TP
282.B \-V
283Print the version number of strace.
284.TP
285.B \-x
286Print all non-ascii strings in hexadecimal string format.
287.TP
288.B \-xx
289Print all strings in hexadecimal string format.
290.TP
291.BI "\-a " column
292Align return values in a secific column (default column 40).
293.TP
294.BI "\-e " expr
295A qualifying expression which modifies which events to trace
296or how to trace them. The format of the expression is:
297.br
298[qualifier=][!]value1[,value2]...
299.br
300where qualifier is one of trace, abbrev, verbose, raw, signal, read, or write
301and value is a qualifier-dependent symbol or number. The default
302qualifier is trace. Using an exclamation mark negates the set of values.
303For example \-eopen means literally \-e trace=open which in turn means
304trace only the open system call. By contrast, \-etrace=!open means
305to trace every system call except open. In addition the special values
306all and none have the obvious meanings.
307.LP
308Note that some shells use the exclamation point for history
309expansion; even inside quoted arguments. If so, you must escape
310the exclamation point with a backslash.
311.TP
312.BI "\-e trace=" set
313Trace only the specified set of system calls. The
314.B \-c
315option is useful for determining which system calls might be useful
316to trace. For example, trace=open,close,read,write means to only
317trace those four system calls. Be careful when making inferences
318about the user/kernel boundary if only a subset of system calls
319are being monitored. The default is trace=all.
320.TP
321.B "\-e trace=file"
322Trace all system calls which take a file name as an argument. You
323can think of this as an abbreviation for
324.BR "\-e trace=open,stat,chmod,unlink," ...
325which is useful to seeing what files the process is referencing.
326Furthermore, using the abbreviation will ensure that you don't
327accidentally forget to include a call like
328.B lstat
329in the list. Betchya woulda forgot that one.
330.TP
331.B "\-e trace=process"
332Trace all system calls which involve process management. This
333is useful for watching the fork, wait, and exec steps of a process.
334.TP
335.B "\-e trace=network"
336Trace all the network related system calls.
337.TP
338.B "\-e trace=signal"
339Trace all signal related system calls.
340.TP
341.B "\-e trace=ipc"
342Trace all IPC related system calls.
343.TP
344.BI "\-e abbrev=" set
345Abbreviate the output from printing each member of large structures.
346The default is abbrev=all. The
347.B \-v
348option has the effect of abbrev=none.
349.TP
350.BI "\-e verbose=" set
351Dereference structures for the specified set of system calls. The
352default is verbose=all.
353.TP
354.BI "\-e raw=" set
355Print raw, undecoded arguments for the specifed set of system calls.
356This option has the effect of causing all arguments to be printed
357in hexadecimal. This is mostly useful if you don't trust the
358decoding or you need to know the actual numeric value of an
359argument.
360.TP
361.BI "\-e signal=" set
362Trace only the specified subset of signals. The default is signal=all.
363For example signal=!SIGIO (or signal=!io) causes SIGIO signals not to
364be traced.
365.TP
366.BI "\-e read=" set
367Perform a full hexadecimal and ascii dump of all the data read from
368file descriptors listed in the specified set. For example, to see
369all input activity on file descriptors 3 and 5 use
370.BR "\-e read=3,5" .
371Note that this is independent from the normal tracing of the read
372system call which is controlled by the option
373.BR "\-e trace=read" .
374.TP
375.BI "\-e write=" set
376Perform a full hexadecimal and ascii dump of all the data written to
377file descriptors listed in the specified set. For example, to see
378all output activity on file descriptors 3 and 5 use
379.BR "\-e write=3,5" .
380Note that this is independent from the normal tracing of the write
381system call which is controlled by the option
382.BR "\-e trace=write" .
383.TP
384.BI "\-o " filename
385Write the trace output to the file
386.I filename
387rather than to stderr.
388Use
389.I filename.pid
390if
391.B \-ff
392is used.
393If the argument begins with `|' or with `!' then the rest of the
394argument is treated as a command and all output is piped to it.
395This is convenient for piping the debugging output to a program
396without affecting the redirections of executed programs.
397.TP
398.BI "\-O " overhead
399Set the overhead for tracing system calls to overhead microseconds.
400This is useful for overriding the default heuristic for guessing
401how much time is spent in mere measuring when timing system calls using
402the
403.B \-c
404option. The acuracy of the heuristic can be gauged by timing a given
405program run without tracing (using time(1)) and comparing the accumulated
406system call time to the total produced using
407.B \-c .
408.TP
409.BI "\-p " pid
410Attach to the process with the process
411.SM ID
412.I pid
413and begin tracing.
414The trace may be terminated
415at any time by a keyboard interrupt signal (\c
416.SM CTRL\s0-C).
417.B strace
418will respond by detaching itself from the traced process(es)
419leaving it (them) to continue running.
420Multiple
421.B \-p
422options can be used to attach to up to 32 processes in addition to
423.I command
424(which is optional if at least one
425.B \-p
426option is given).
427.TP
428.BI "\-s " strsize
429Specify the maximum string size to print (the default is 32). Note
430that filenames are not considered strings and are always printed in
431full.
432.TP
433.BI "\-S " sortby
434Sort the output of the histogram printed by the
435.B \-c
436option by the specified critereon. Legal values are
437time, calls, name, and nothing (default time).
438.TP
439.BI "\-u " username
440Run command with the userid, groupid and supplementary groups of
441.IR username .
442This option is only useful when running as root and enables the
443correct execution of setuid and/or setgid binaries.
444Unless this option is used setuid and setgid programs are executed
445without effective privileges.
446.SH "SETUID INSTALLATION"
447If
448.B strace
449is installed setuid to root then the invoking user will be able to
450attach to and trace processes owned by any user.
451In addition setuid and setgid programs will be executed and traced
452with the correct effective privileges.
453Since only users trusted with full root privileges should be allowed
454to do these things,
455it only makes sense to install
456.B strace
457as setuid to root when the users who can execute it are restricted
458to those users who have this trust.
459For example, it makes sense to install a special version of
460.B
461strace
462with mode `rwsr-xr--', user root and group trace,
463where members of the trace group are trusted users.
464If you do use this feature, please remember to install
465a non-setuid version of strace for ordinary lusers to use.
466.SH "SEE ALSO"
467.BR ptrace(2) ,
468.BR proc(4) ,
469.BR time(1) ,
470.BR trace(1) ,
471.BR truss(1)
472.SH NOTES
473It is a pity that so much tracing clutter is produced by systems
474employing shared libraries.
475.LP
476It is instructive to think about system call inputs and outputs
477as data-flow across the user/kernel boundary. Because user-space
478and kernel-space are separate and address-protected, it is
479sometimes possible to make deductive inferences about process
480behavior using inputs and outputs as propositions.
481.LP
482In some cases, a system call will differ from the documented behavior
483or have a different name. For example, on System V derived systems
484the true time(2) system call does not take an argument and the stat
485function is called xstat and takes an extra leading argument. These
486discrepancies are normal but idiosyncratic characteristics of the
487system call interface and are accounted for by C library wrapper
488functions.
489.LP
490On some platforms a process that has a system call trace applied
491to it with the
492.B \-p
493option will receive a
494.BR \s-1SIGSTOP\s0 .
495This signal may interrupt a system call that is not restartable.
496This may have an unpredictable effect on the process
497if the process takes no action to restart the system call.
498.SH BUGS
499Programs that use the
500.I setuid
501bit do not have
502effective user
503.SM ID
504privileges while being traced.
505.LP
506A traced process ignores
507.SM SIGSTOP
508except of SVR4 platforms.
509.LP
510A traced process which tries to block SIGTRAP will be sent a SIGSTOP
511in an attempt to force continuation of tracing.
512.LP
513A traced process runs slowly.
514.LP
515Traced processes which are descended from
516.I command
517may be left running after an interrupt signal (\c
518.SM CTRL\s0-C).
519.LP
520On Linux, exciting as it would be, tracing the init process is forbidden.
521.LP
522The
523.B \-i
524option is weakly supported.
525.SH HISTORY
526.B strace
527The original strace was written by Paul Kranenburg
528for SunOS and was inspired by its trace utility.
529The SunOS version of strace was ported to Linux and enhanced
530by Branko Lankester, who also wrote the Linux kernel support.
531Even though Paul released strace 2.5 in 1992,
532Branko's work was based on Paul's strace 1.5 release from 1991.
533In 1993, Rick Sladkey merged strace 2.5 for SunOS and the
534second release of strace for Linux, added many of the features of
535truss from SVR4, and produced an strace that worked on both platforms.
536In 1994 Rick ported strace to SVR4 and Solaris and wrote the
537automatic configuration support. In 1995 he ported strace to Irix
538and tired of writing about himself in the third person.
539.SH PROBLEMS
540Problems with
541.B strace
542should be reported to the current
543.B strace
544maintainer, Rick Sladkey, at <jrs@world.std.com>.