]> granicus.if.org Git - strace/blob - strace.1
2006-11-20 Jakub Jelinek <jakub@redhat.com>
[strace] / strace.1
1 .\" 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 R
37 .fi
38 .sp
39 ..
40 .TH STRACE 1 "2003-01-21"
41 .SH NAME
42 strace \- 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 .BI \-E var=val
70 ]
71 \&...
72 [
73 .BI \-E var
74 ]
75 \&...
76 [
77 .I command
78 [
79 .I arg
80 \&...
81 ]
82 ]
83 .sp
84 .B strace
85 .B \-c
86 [
87 .BI \-e expr
88 ]
89 \&...
90 [
91 .BI \-O overhead
92 ]
93 [
94 .BI \-S sortby
95 ]
96 [
97 .I command
98 [
99 .I arg
100 \&...
101 ]
102 ]
103 .SH DESCRIPTION
104 .IX "strace command" "" "\fLstrace\fR command"
105 .LP
106 In the simplest case
107 .B strace
108 runs the specified
109 .I command
110 until it exits.
111 It intercepts and records the system calls which are called
112 by a process and the signals which are received by a process.
113 The name of each system call, its arguments and its return value
114 are printed on standard error or to the file specified with the
115 .B \-o
116 option.
117 .LP
118 .B strace
119 is a useful diagnostic, instructional, and debugging tool.
120 System administrators, diagnosticians and trouble-shooters will find
121 it invaluable for solving problems with
122 programs for which the source is not readily available since
123 they do not need to be recompiled in order to trace them.
124 Students, hackers and the overly-curious will find that
125 a great deal can be learned about a system and its system calls by
126 tracing even ordinary programs.  And programmers will find that
127 since system calls and signals are events that happen at the user/kernel
128 interface, a close examination of this boundary is very
129 useful for bug isolation, sanity checking and
130 attempting to capture race conditions.
131 .LP
132 Each line in the trace contains the system call name, followed
133 by its arguments in parentheses and its return value.
134 An example from stracing the command ``cat /dev/null'' is:
135 .CW
136 open("/dev/null", O_RDONLY) = 3
137 .CE
138 Errors (typically a return value of \-1) have the errno symbol
139 and error string appended.
140 .CW
141 open("/foo/bar", O_RDONLY) = -1 ENOENT (No such file or directory)
142 .CE
143 Signals are printed as a signal symbol and a signal string.
144 An excerpt from stracing and interrupting the command ``sleep 666'' is:
145 .CW
146 sigsuspend([] <unfinished ...>
147 --- SIGINT (Interrupt) ---
148 +++ killed by SIGINT +++
149 .CE
150 Arguments are printed in symbolic form with a passion.
151 This example shows the shell performing ``>>xyzzy'' output redirection:
152 .CW
153 open("xyzzy", O_WRONLY|O_APPEND|O_CREAT, 0666) = 3
154 .CE
155 Here the three argument form of open is decoded by breaking down the
156 flag argument into its three bitwise-OR constituents and printing the
157 mode value in octal by tradition.  Where traditional or native
158 usage differs from ANSI or POSIX, the latter forms are preferred.
159 In some cases,
160 .B strace
161 output has proven to be more readable than the source.
162 .LP
163 Structure pointers are dereferenced and the members are displayed
164 as appropriate.  In all cases arguments are formatted in the most C-like
165 fashion possible.
166 For example, the essence of the command ``ls \-l /dev/null'' is captured as:
167 .CW
168 lstat("/dev/null", {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
169 .CE
170 Notice how the `struct stat' argument is dereferenced and how each member is
171 displayed symbolically.  In particular, observe how the st_mode member
172 is carefully decoded into a bitwise-OR of symbolic and numeric values.
173 Also notice in this example that the first argument to lstat is an input
174 to the system call and the second argument is an output.  Since output
175 arguments are not modified if the system call fails, arguments may not
176 always be dereferenced.  For example, retrying the ``ls \-l'' example
177 with a non-existent file produces the following line:
178 .CW
179 lstat("/foo/bar", 0xb004) = -1 ENOENT (No such file or directory)
180 .CE
181 In this case the porch light is on but nobody is home.
182 .LP
183 Character pointers are dereferenced and printed as C strings.
184 Non-printing characters in strings are normally represented by
185 ordinary C escape codes.
186 Only the first
187 .I strsize
188 (32 by default) bytes of strings are printed;
189 longer strings have an ellipsis appended following the closing quote.
190 Here is a line from ``ls \-l'' where the
191 .B getpwuid
192 library routine is reading the password file:
193 .CW
194 read(3, "root::0:0:System Administrator:/"..., 1024) = 422
195 .CE
196 While structures are annotated using curly braces, simple pointers
197 and arrays are printed using square brackets with commas separating
198 elements.  Here is an example from the command ``id'' on a system with
199 supplementary group ids:
200 .CW
201 getgroups(32, [100, 0]) = 2
202 .CE
203 On the other hand, bit-sets are also shown using square brackets
204 but set elements are separated only by a space.  Here is the shell
205 preparing to execute an external command:
206 .CW
207 sigprocmask(SIG_BLOCK, [CHLD TTOU], []) = 0
208 .CE
209 Here the second argument is a bit-set of two signals, SIGCHLD and SIGTTOU.
210 In some cases the bit-set is so full that printing out the unset
211 elements is more valuable.  In that case, the bit-set is prefixed by
212 a tilde like this:
213 .CW
214 sigprocmask(SIG_UNBLOCK, ~[], NULL) = 0
215 .CE
216 Here the second argument represents the full set of all signals.
217 .SH OPTIONS
218 .TP 12
219 .TP
220 .B \-c
221 Count time, calls, and errors for each system call and report a summary on
222 program exit.  On Linux, this attempts to show system time (CPU time spent
223 running in the kernel) independent of wall clock time.  If -c is used with
224 -f or -F (below), only aggregate totals for all traced processes are kept.
225 .TP
226 .B \-d
227 Show some debugging output of
228 .B strace
229 itself on the standard error.
230 .TP
231 .B \-f
232 Trace child processes as they are created by currently traced
233 processes as a result of the
234 .BR fork (2)
235 system call.  The new process is
236 attached to as soon as its pid is known (through the return value of
237 .BR fork (2)
238 in the parent process). This means that such children may run
239 uncontrolled for a while (especially in the case of a
240 .BR vfork (2)),
241 until the parent is scheduled again to complete its
242 .RB ( v ) fork (2)
243 call.
244 If the parent process decides to
245 .BR wait (2)
246 for a child that is currently
247 being traced, it is suspended until an appropriate child process either
248 terminates or incurs a signal that would cause it to terminate (as
249 determined from the child's current signal disposition).
250 .TP
251 .B \-ff
252 If the
253 .B \-o
254 .I filename
255 option is in effect, each processes trace is written to
256 .I filename.pid
257 where pid is the numeric process id of each process.
258 This is incompatible with -c, since no per-process counts are kept.
259 .TP
260 .B \-F
261 Attempt to follow
262 .BR vfork s.
263 (On SunOS 4.x, this is accomplished with
264 some dynamic linking trickery.)
265 Otherwise,
266 .BR vfork s
267 will
268 not be followed even if
269 .B \-f
270 has been given.
271 .TP
272 .B \-h
273 Print the help summary.
274 .TP
275 .B \-i
276 Print the instruction pointer at the time of the system call.
277 .TP
278 .B \-q
279 Suppress messages about attaching, detaching etc.  This happens
280 automatically when output is redirected to a file and the command
281 is run directly instead of attaching.
282 .TP
283 .B \-r
284 Print a relative timestamp upon entry to each system call.  This
285 records the time difference between the beginning of successive
286 system calls.
287 .TP
288 .B \-t
289 Prefix each line of the trace with the time of day.
290 .TP
291 .B \-tt
292 If given twice, the time printed will include the microseconds.
293 .TP
294 .B \-ttt
295 If given thrice, the time printed will include the microseconds
296 and the leading portion will be printed as the number
297 of seconds since the epoch.
298 .TP
299 .B \-T
300 Show the time spent in system calls. This records the time
301 difference between the beginning and the end of each system call.
302 .TP
303 .B \-v
304 Print unabbreviated versions of environment, stat, termios, etc.
305 calls.  These structures are very common in calls and so the default
306 behavior displays a reasonable subset of structure members.  Use
307 this option to get all of the gory details.
308 .TP
309 .B \-V
310 Print the version number of
311 .BR strace .
312 .TP
313 .B \-x
314 Print all non-ASCII strings in hexadecimal string format.
315 .TP
316 .B \-xx
317 Print all strings in hexadecimal string format.
318 .TP
319 .BI "\-a " column
320 Align return values in a specific column (default column 40).
321 .TP
322 .BI "\-e " expr
323 A qualifying expression which modifies which events to trace
324 or how to trace them.  The format of the expression is:
325 .RS 15
326 .IP
327 [\fIqualifier\fB=\fR][\fB!\fR]\fIvalue1\fR[\fB,\fIvalue2\fR]...
328 .RE
329 .IP
330 where
331 .I qualifier
332 is one of
333 .BR trace ,
334 .BR abbrev ,
335 .BR verbose ,
336 .BR raw ,
337 .BR signal ,
338 .BR read ,
339 or
340 .B write
341 and
342 .I value
343 is a qualifier-dependent symbol or number.  The default
344 qualifier is
345 .BR trace .
346 Using an exclamation mark negates the set of values.  For example,
347 .B \-eopen
348 means literally
349 .B "\-e trace=open"
350 which in turn means trace only the
351 .B open
352 system call.  By contrast,
353 .B "\-etrace=!open"
354 means to trace every system call except
355 .BR open .
356 In addition, the special values
357 .B all
358 and
359 .B none
360 have the obvious meanings.
361 .IP
362 Note that some shells use the exclamation point for history
363 expansion even inside quoted arguments.  If so, you must escape
364 the exclamation point with a backslash.
365 .TP
366 .BI "\-e trace=" set
367 Trace only the specified set of system calls.  The
368 .B \-c
369 option is useful for determining which system calls might be useful
370 to trace.  For example,
371 .B trace=open,close,read,write
372 means to only
373 trace those four system calls.  Be careful when making inferences
374 about the user/kernel boundary if only a subset of system calls
375 are being monitored.  The default is
376 .BR trace=all .
377 .TP
378 .B "\-e trace=file"
379 Trace all system calls which take a file name as an argument.  You
380 can think of this as an abbreviation for
381 .BR "\-e\ trace=open,stat,chmod,unlink," ...
382 which is useful to seeing what files the process is referencing.
383 Furthermore, using the abbreviation will ensure that you don't
384 accidentally forget to include a call like
385 .B lstat
386 in the list.  Betchya woulda forgot that one.
387 .TP
388 .B "\-e trace=process"
389 Trace all system calls which involve process management.  This
390 is useful for watching the fork, wait, and exec steps of a process.
391 .TP
392 .B "\-e trace=network"
393 Trace all the network related system calls.
394 .TP
395 .B "\-e trace=signal"
396 Trace all signal related system calls.
397 .TP
398 .B "\-e trace=ipc"
399 Trace all IPC related system calls.
400 .TP
401 .B "\-e trace=desc"
402 Trace all file descriptor related system calls.
403 .TP
404 .BI "\-e abbrev=" set
405 Abbreviate the output from printing each member of large structures.
406 The default is
407 .BR abbrev=all .
408 The
409 .B \-v
410 option has the effect of
411 .BR abbrev=none .
412 .TP
413 .BI "\-e verbose=" set
414 Dereference structures for the specified set of system calls.  The
415 default is
416 .BR verbose=all .
417 .TP
418 .BI "\-e raw=" set
419 Print raw, undecoded arguments for the specified set of system calls.
420 This option has the effect of causing all arguments to be printed
421 in hexadecimal.  This is mostly useful if you don't trust the
422 decoding or you need to know the actual numeric value of an
423 argument.
424 .TP
425 .BI "\-e signal=" set
426 Trace only the specified subset of signals.  The default is
427 .BR signal=all .
428 For example,
429 .B signal=!SIGIO
430 (or
431 .BR signal=!io )
432 causes SIGIO signals not to be traced.
433 .TP
434 .BI "\-e read=" set
435 Perform a full hexadecimal and ASCII dump of all the data read from
436 file descriptors listed in the specified set.  For example, to see
437 all input activity on file descriptors 3 and 5 use
438 .BR "\-e read=3,5" .
439 Note that this is independent from the normal tracing of the
440 .BR read (2)
441 system call which is controlled by the option
442 .BR "\-e trace=read" .
443 .TP
444 .BI "\-e write=" set
445 Perform a full hexadecimal and ASCII dump of all the data written to
446 file descriptors listed in the specified set.  For example, to see
447 all output activity on file descriptors 3 and 5 use
448 .BR "\-e write=3,5" .
449 Note that this is independent from the normal tracing of the
450 .BR write (2)
451 system call which is controlled by the option
452 .BR "\-e trace=write" .
453 .TP
454 .BI "\-o " filename
455 Write the trace output to the file
456 .I filename
457 rather than to stderr.
458 Use
459 .I filename.pid
460 if
461 .B \-ff
462 is used.
463 If the argument begins with `|' or with `!' then the rest of the
464 argument is treated as a command and all output is piped to it.
465 This is convenient for piping the debugging output to a program
466 without affecting the redirections of executed programs.
467 .TP
468 .BI "\-O " overhead
469 Set the overhead for tracing system calls to
470 .I overhead
471 microseconds.
472 This is useful for overriding the default heuristic for guessing
473 how much time is spent in mere measuring when timing system calls using
474 the
475 .B \-c
476 option.  The accuracy of the heuristic can be gauged by timing a given
477 program run without tracing (using
478 .BR time (1))
479 and comparing the accumulated
480 system call time to the total produced using
481 .BR \-c .
482 .TP
483 .BI "\-p " pid
484 Attach to the process with the process
485 .SM ID
486 .I pid
487 and begin tracing.
488 The trace may be terminated
489 at any time by a keyboard interrupt signal (\c
490 .SM CTRL\s0-C).
491 .B strace
492 will respond by detaching itself from the traced process(es)
493 leaving it (them) to continue running.
494 Multiple
495 .B \-p
496 options can be used to attach to up to 32 processes in addition to
497 .I command
498 (which is optional if at least one
499 .B \-p
500 option is given).
501 .TP
502 .BI "\-s " strsize
503 Specify the maximum string size to print (the default is 32).  Note
504 that filenames are not considered strings and are always printed in
505 full.
506 .TP
507 .BI "\-S " sortby
508 Sort the output of the histogram printed by the
509 .B \-c
510 option by the specified criterion.  Legal values are
511 .BR time ,
512 .BR calls ,
513 .BR name ,
514 and
515 .B nothing
516 (default
517 .BR time ).
518 .TP
519 .BI "\-u " username
520 Run command with the user \s-1ID\s0, group \s-2ID\s0, and
521 supplementary groups of
522 .IR username .
523 This option is only useful when running as root and enables the
524 correct execution of setuid and/or setgid binaries.
525 Unless this option is used setuid and setgid programs are executed
526 without effective privileges.
527 .TP
528 .BI "\-E " var=val
529 Run command with
530 .IR var=val
531 in its list of environment variables.
532 .TP
533 .BI "\-E " var
534 Remove
535 .IR var
536 from the inherited list of environment variables before passing it on to
537 the command.
538 .SH "SETUID INSTALLATION"
539 If
540 .B strace
541 is installed setuid to root then the invoking user will be able to
542 attach to and trace processes owned by any user.
543 In addition setuid and setgid programs will be executed and traced
544 with the correct effective privileges.
545 Since only users trusted with full root privileges should be allowed
546 to do these things,
547 it only makes sense to install
548 .B strace
549 as setuid to root when the users who can execute it are restricted
550 to those users who have this trust.
551 For example, it makes sense to install a special version of
552 .B strace
553 with mode `rwsr-xr--', user
554 .B root
555 and group
556 .BR trace ,
557 where members of the
558 .B trace
559 group are trusted users.
560 If you do use this feature, please remember to install
561 a non-setuid version of
562 .B strace
563 for ordinary lusers to use.
564 .SH "SEE ALSO"
565 .BR ltrace (1),
566 .BR time (1),
567 .BR ptrace (2),
568 .BR proc (5)
569 .SH NOTES
570 It is a pity that so much tracing clutter is produced by systems
571 employing shared libraries.
572 .LP
573 It is instructive to think about system call inputs and outputs
574 as data-flow across the user/kernel boundary.  Because user-space
575 and kernel-space are separate and address-protected, it is
576 sometimes possible to make deductive inferences about process
577 behavior using inputs and outputs as propositions.
578 .LP
579 In some cases, a system call will differ from the documented behavior
580 or have a different name.  For example, on System V-derived systems
581 the true
582 .BR time (2)
583 system call does not take an argument and the
584 .B stat
585 function is called
586 .B xstat
587 and takes an extra leading argument.  These
588 discrepancies are normal but idiosyncratic characteristics of the
589 system call interface and are accounted for by C library wrapper
590 functions.
591 .LP
592 On some platforms a process that has a system call trace applied
593 to it with the
594 .B \-p
595 option will receive a
596 .BR \s-1SIGSTOP\s0 .
597 This signal may interrupt a system call that is not restartable.
598 This may have an unpredictable effect on the process
599 if the process takes no action to restart the system call.
600 .SH BUGS
601 Programs that use the
602 .I setuid
603 bit do not have
604 effective user
605 .SM ID
606 privileges while being traced.
607 .LP
608 A traced process ignores
609 .SM SIGSTOP
610 except on SVR4 platforms.
611 .LP
612 A traced process which tries to block SIGTRAP will be sent a SIGSTOP
613 in an attempt to force continuation of tracing.
614 .LP
615 A traced process runs slowly.
616 .LP
617 Traced processes which are descended from
618 .I command
619 may be left running after an interrupt signal (\c
620 .SM CTRL\s0-C).
621 .LP
622 On Linux, exciting as it would be, tracing the init process is forbidden.
623 .LP
624 The
625 .B \-i
626 option is weakly supported.
627 .SH HISTORY
628 .B strace
629 The original
630 .B strace
631 was written by Paul Kranenburg
632 for SunOS and was inspired by its trace utility.
633 The SunOS version of
634 .B strace
635 was ported to Linux and enhanced
636 by Branko Lankester, who also wrote the Linux kernel support.
637 Even though Paul released
638 .B strace
639 2.5 in 1992,
640 Branko's work was based on Paul's
641 .B strace
642 1.5 release from 1991.
643 In 1993, Rick Sladkey merged
644 .B strace
645 2.5 for SunOS and the second release of
646 .B strace
647 for Linux, added many of the features of
648 .BR truss (1)
649 from SVR4, and produced an
650 .B strace
651 that worked on both platforms.  In 1994 Rick ported
652 .B strace
653 to SVR4 and Solaris and wrote the
654 automatic configuration support.  In 1995 he ported
655 .B strace
656 to Irix
657 and tired of writing about himself in the third person.
658 .SH PROBLEMS
659 Problems with
660 .B strace
661 should be reported via the Debian Bug Tracking System,
662 or to the
663 .B strace
664 mailing list at <strace-devel@lists.sourceforge.net>.