Using strace to debug what apache/php are doing.


Enter Your Query:
Use '%' for wildcards and quotes for "exact phrases"


Top Level » Apache Related Information

Using strace to debug what apache/php are doing.Last Modified: Aug 6, 2018, 5:37 pm
Trying to find a slowdown in your website and not sure where it is?
Example: WordPress loads slowly and you don't know why.
Use strace!

The idea is that strace can dump all binary calls to a log, so we'll dump them to disk and sift through them later.
You can also use this method for other processes, just swap "httpd" with some other process name in the command below.

Note: this is a fairly high-level debug method for advanced administrators. Root ssh access is required.

  1. Run strace to log:

    cd /root
    mkdir straces
    cd straces
    ps ax | grep httpd | grep -v grep | awk '{ print "strace -f -s500 -o strace."$1".log -p "$1"&" }' | sh

  2. Trigger the slow case.  Load the website that is slow, however you need to duplicate the issue.  Note, it might be best to highlight the URL bar and hit enter, instead of using F5. The reason is we only want to load the site, and not the included images/css/js, etc.. and F5 or ctrl-F5 may reload everything, causing the logs to fill with more info, making the cause harder to find.
  3. End the strace, open a 2nd console and run:

    killall -9 strace

    You'll now have several strace.*.log files, one for each httpd proecss that was running.  Note, if you're debugging a slowdown (or possibly socket timeout), hit ctrl-c before the timeout happens, but enough time where you can let it chew on things for a bit.  This way, the last "chunks" of code that caused the slowdown will be near the end of the log, making it easier to track.
  4. You'll need to find which PID handled your request, so grep out your IP from the logs to figure that out. Change 1.2.3.4 with your actual IP address:

    grep 1.2.3.4 *.log

    This will dump some messy code, but on the far left should be which files that code came from.  Let's say it showed the output in strace.29622.log (your PID number will be different from 29622). Also there could be multiple files, depending on how many requests were made and how apache handled them.  We're only concerned with the "slow" code, and not the other things like images, so you'll need to go through each one to figure out which is which, to get at the important bits you're after.
  5. From there, you can go through the logs, looking at what was happening, eg:

    less strace.29622.log

    This is going to show you quite a lot of code, but you can also search with the / character.

    You can alternatively "grep" for things, eg:

    grep somethingspecific strace.29622.log

    which only shows the lines you're looking for.
From this point, what you're looking for, how you find it, and what you do with it would be up to you.

© 2018 JBMC Software, Suite 173  3-11 Bellerose Drive, St Albert, AB  T8N 1P7  Canada.  Mon-Fri 9AM-5PM MST