blob: 84e11d052dec21705f61eb29c3015c2f301f1214 [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
Nate Sammonsb4aa1131999-03-31 05:59:04 +0000111is a useful diagnostic, instructional, and debugging tool.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000112System 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
Nate Sammonsccd8f211999-03-29 22:57:54 +0000239Attempt to follow vforks. (On SunOS 4.x, this is accomplished with
240some dynamic linking trickery. On Linux, it requires some kernel
241functionality not yet in the standard kernel.) Otherwise, vforks will
242not be followed even if
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000243.B \-f
244has been given.
245.TP
246.B \-h
247Print the help summary.
248.TP
249.B \-i
250Print the instruction pointer at the time of the system call.
251.TP
252.B \-q
253Suppress messages about attaching, detaching etc. This happens
254automatically when output is redirected to a file and the command
255is run directly instead of attaching.
256.TP
257.B \-r
258Print a relative timestamp upon entry to each system call. This
259records the time difference between the beginning of successive
260system calls.
261.TP
262.B \-t
263Prefix each line of the trace with the time of day.
264.TP
265.B \-tt
266If given twice, the time printed will include the microseconds.
267.TP
268.B \-ttt
269If given thrice, the time printed will include the microseconds
270and the leading portion will be printed as the number
271of seconds since the epoch.
272.TP
273.B \-T
274Show the time spent in system calls. This records the time
275difference between the beginning and the end of each system call.
276.TP
277.B \-v
278Print unabbreviated versions of environment, stat, termios, etc.
279calls. These structures are very common in calls and so the default
280behavior displays a reasonable subset of structure members. Use
281this option to get all of the gory details.
282.TP
283.B \-V
284Print the version number of strace.
285.TP
286.B \-x
287Print all non-ascii strings in hexadecimal string format.
288.TP
289.B \-xx
290Print all strings in hexadecimal string format.
291.TP
292.BI "\-a " column
293Align return values in a secific column (default column 40).
294.TP
295.BI "\-e " expr
296A qualifying expression which modifies which events to trace
297or how to trace them. The format of the expression is:
298.br
299[qualifier=][!]value1[,value2]...
300.br
301where qualifier is one of trace, abbrev, verbose, raw, signal, read, or write
302and value is a qualifier-dependent symbol or number. The default
303qualifier is trace. Using an exclamation mark negates the set of values.
304For example \-eopen means literally \-e trace=open which in turn means
305trace only the open system call. By contrast, \-etrace=!open means
306to trace every system call except open. In addition the special values
307all and none have the obvious meanings.
308.LP
309Note that some shells use the exclamation point for history
310expansion; even inside quoted arguments. If so, you must escape
311the exclamation point with a backslash.
312.TP
313.BI "\-e trace=" set
314Trace only the specified set of system calls. The
315.B \-c
316option is useful for determining which system calls might be useful
317to trace. For example, trace=open,close,read,write means to only
318trace those four system calls. Be careful when making inferences
319about the user/kernel boundary if only a subset of system calls
320are being monitored. The default is trace=all.
321.TP
322.B "\-e trace=file"
323Trace all system calls which take a file name as an argument. You
324can think of this as an abbreviation for
325.BR "\-e trace=open,stat,chmod,unlink," ...
326which is useful to seeing what files the process is referencing.
327Furthermore, using the abbreviation will ensure that you don't
328accidentally forget to include a call like
329.B lstat
330in the list. Betchya woulda forgot that one.
331.TP
332.B "\-e trace=process"
333Trace all system calls which involve process management. This
334is useful for watching the fork, wait, and exec steps of a process.
335.TP
336.B "\-e trace=network"
337Trace all the network related system calls.
338.TP
339.B "\-e trace=signal"
340Trace all signal related system calls.
341.TP
342.B "\-e trace=ipc"
343Trace all IPC related system calls.
344.TP
345.BI "\-e abbrev=" set
346Abbreviate the output from printing each member of large structures.
347The default is abbrev=all. The
348.B \-v
349option has the effect of abbrev=none.
350.TP
351.BI "\-e verbose=" set
352Dereference structures for the specified set of system calls. The
353default is verbose=all.
354.TP
355.BI "\-e raw=" set
356Print raw, undecoded arguments for the specifed set of system calls.
357This option has the effect of causing all arguments to be printed
358in hexadecimal. This is mostly useful if you don't trust the
359decoding or you need to know the actual numeric value of an
360argument.
361.TP
362.BI "\-e signal=" set
363Trace only the specified subset of signals. The default is signal=all.
364For example signal=!SIGIO (or signal=!io) causes SIGIO signals not to
365be traced.
366.TP
367.BI "\-e read=" set
368Perform a full hexadecimal and ascii dump of all the data read from
369file descriptors listed in the specified set. For example, to see
370all input activity on file descriptors 3 and 5 use
371.BR "\-e read=3,5" .
372Note that this is independent from the normal tracing of the read
373system call which is controlled by the option
374.BR "\-e trace=read" .
375.TP
376.BI "\-e write=" set
377Perform a full hexadecimal and ascii dump of all the data written to
378file descriptors listed in the specified set. For example, to see
379all output activity on file descriptors 3 and 5 use
380.BR "\-e write=3,5" .
381Note that this is independent from the normal tracing of the write
382system call which is controlled by the option
383.BR "\-e trace=write" .
384.TP
385.BI "\-o " filename
386Write the trace output to the file
387.I filename
388rather than to stderr.
389Use
390.I filename.pid
391if
392.B \-ff
393is used.
394If the argument begins with `|' or with `!' then the rest of the
395argument is treated as a command and all output is piped to it.
396This is convenient for piping the debugging output to a program
397without affecting the redirections of executed programs.
398.TP
399.BI "\-O " overhead
400Set the overhead for tracing system calls to overhead microseconds.
401This is useful for overriding the default heuristic for guessing
402how much time is spent in mere measuring when timing system calls using
403the
404.B \-c
405option. The acuracy of the heuristic can be gauged by timing a given
406program run without tracing (using time(1)) and comparing the accumulated
407system call time to the total produced using
408.B \-c .
409.TP
410.BI "\-p " pid
411Attach to the process with the process
412.SM ID
413.I pid
414and begin tracing.
415The trace may be terminated
416at any time by a keyboard interrupt signal (\c
417.SM CTRL\s0-C).
418.B strace
419will respond by detaching itself from the traced process(es)
420leaving it (them) to continue running.
421Multiple
422.B \-p
423options can be used to attach to up to 32 processes in addition to
424.I command
425(which is optional if at least one
426.B \-p
427option is given).
428.TP
429.BI "\-s " strsize
430Specify the maximum string size to print (the default is 32). Note
431that filenames are not considered strings and are always printed in
432full.
433.TP
434.BI "\-S " sortby
435Sort the output of the histogram printed by the
436.B \-c
437option by the specified critereon. Legal values are
438time, calls, name, and nothing (default time).
439.TP
440.BI "\-u " username
441Run command with the userid, groupid and supplementary groups of
442.IR username .
443This option is only useful when running as root and enables the
444correct execution of setuid and/or setgid binaries.
445Unless this option is used setuid and setgid programs are executed
446without effective privileges.
447.SH "SETUID INSTALLATION"
448If
449.B strace
450is installed setuid to root then the invoking user will be able to
451attach to and trace processes owned by any user.
452In addition setuid and setgid programs will be executed and traced
453with the correct effective privileges.
454Since only users trusted with full root privileges should be allowed
455to do these things,
456it only makes sense to install
457.B strace
458as setuid to root when the users who can execute it are restricted
459to those users who have this trust.
460For example, it makes sense to install a special version of
461.B
462strace
463with mode `rwsr-xr--', user root and group trace,
464where members of the trace group are trusted users.
465If you do use this feature, please remember to install
466a non-setuid version of strace for ordinary lusers to use.
467.SH "SEE ALSO"
468.BR ptrace(2) ,
469.BR proc(4) ,
470.BR time(1) ,
471.BR trace(1) ,
472.BR truss(1)
473.SH NOTES
474It is a pity that so much tracing clutter is produced by systems
475employing shared libraries.
476.LP
477It is instructive to think about system call inputs and outputs
478as data-flow across the user/kernel boundary. Because user-space
479and kernel-space are separate and address-protected, it is
480sometimes possible to make deductive inferences about process
481behavior using inputs and outputs as propositions.
482.LP
483In some cases, a system call will differ from the documented behavior
484or have a different name. For example, on System V derived systems
485the true time(2) system call does not take an argument and the stat
486function is called xstat and takes an extra leading argument. These
487discrepancies are normal but idiosyncratic characteristics of the
488system call interface and are accounted for by C library wrapper
489functions.
490.LP
491On some platforms a process that has a system call trace applied
492to it with the
493.B \-p
494option will receive a
495.BR \s-1SIGSTOP\s0 .
496This signal may interrupt a system call that is not restartable.
497This may have an unpredictable effect on the process
498if the process takes no action to restart the system call.
499.SH BUGS
500Programs that use the
501.I setuid
502bit do not have
503effective user
504.SM ID
505privileges while being traced.
506.LP
507A traced process ignores
508.SM SIGSTOP
Nate Sammonsb4aa1131999-03-31 05:59:04 +0000509except on SVR4 platforms.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000510.LP
511A traced process which tries to block SIGTRAP will be sent a SIGSTOP
512in an attempt to force continuation of tracing.
513.LP
514A traced process runs slowly.
515.LP
516Traced processes which are descended from
517.I command
518may be left running after an interrupt signal (\c
519.SM CTRL\s0-C).
520.LP
521On Linux, exciting as it would be, tracing the init process is forbidden.
522.LP
523The
524.B \-i
525option is weakly supported.
526.SH HISTORY
527.B strace
528The original strace was written by Paul Kranenburg
529for SunOS and was inspired by its trace utility.
530The SunOS version of strace was ported to Linux and enhanced
531by Branko Lankester, who also wrote the Linux kernel support.
532Even though Paul released strace 2.5 in 1992,
533Branko's work was based on Paul's strace 1.5 release from 1991.
534In 1993, Rick Sladkey merged strace 2.5 for SunOS and the
535second release of strace for Linux, added many of the features of
536truss from SVR4, and produced an strace that worked on both platforms.
537In 1994 Rick ported strace to SVR4 and Solaris and wrote the
538automatic configuration support. In 1995 he ported strace to Irix
539and tired of writing about himself in the third person.
540.SH PROBLEMS
541Problems with
542.B strace
543should be reported to the current
544.B strace
Wichert Akkermanf9d3dcb1999-05-15 00:29:13 +0000545maintainer, Wichert Akkerman, at <wakkerma@debian.org>.