≡ Menu

Debugging Tip: Trace the Process and See What It is Doing with strace

strace is a useful diagnostic, instructional, and debugging tool. It can save lots of headache. 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. This is also useful to submit bug reports to open source developers.

Each line in the trace contains the system call name, followed by its arguments in parentheses and its return value.

Run strace against /bin/foo and capture its output to a text file in output.txt:
$ strace -o output.txt /bin/foo
You can strace the webserver process and see what it’s doing. For example, strace php5 fastcgi process, enter:
$ strace -p 22254 -s 80 -o /tmp/debug.lighttpd.txt
To see only a trace of the open, read system calls, enter :
$ strace -e trace=open,read -p 22254 -s 80 -o debug.webserver.txt

  • -o filename : Write the trace output to the file filename rather than to screen (stderr).
  • -p PID : Attach to the process with the process ID pid and begin tracing. The trace may be terminated at any time by a keyboard interrupt signal (hit CTRL-C). strace will respond by detaching itself from the traced process(es) leaving it (them) to continue running. Multiple -p options can be used to attach to up to 32 processes in addition to command (which is optional if at least one -p option is given).
  • -s SIZE : Specify the maximum string size to print (the default is 32).

Refer to strace man page for more information:
$ man strace

Share this on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 6 comments… add one }
  • Nebojsa December 11, 2009, 8:32 am

    I have problem with one fortran script called in shell.
    Here’s the output when this script stops to work:

    ./new_prep.sh: line 92: 7514 Segmentation fault ../exe/initbc.exe > initbc.out
    value of err is 139

    Any idea? Thanks!

    • Zeinab December 28, 2013, 3:33 pm

      I have the same error, what did you do?

  • thinker December 16, 2009, 7:42 am

    hmmm……”something” is definitely failing.

  • mathwn proc February 25, 2010, 1:39 pm

    Nice stuff guys :)

  • hazel February 11, 2011, 10:19 am

    Thank you very much. The system looks amazing. We are waiting

  • Yogesh August 23, 2016, 10:02 am

    strace -f -p

Leave a Comment

   Tagged with: , , , , , , , , , , , , ,