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