Howto strace PHP-FPM processes

When debugging PHP applications, either running them in a classic LAMP setup, or in a docker container, sometimes we need to debug them and see what’s really happening under the hood to get a better understanding.

This is were strace comes in handy and allows us to collect all the data from running processes.

strace  -s 100000 -o strace.log -yyyttt -f $(pgrep php-fpm | paste -s | sed -e 's/\([0-9]\+\)/-p \1/g' -e 's/\t/ /g')
JavaScript

This will write the output from all the PHP-FPM processes, and first column on the left will be process ID. That can be used to filter out the PHP process.

strace and Docker containers

To get output for Docker containers, you’d need to run the command in the host node, not in the docker container.

How to debug

First of all, you need to know what you are looking for, so I would usually send a unique identifier as part of the cURL call, for instance:

curl localhost/uri/with/problem?problemr

curl -H "debug-me: strace" localhost/uri/with/problem/
JavaScript

After that you can just open the log file with less and search for the keywords like problemr or debug-me . One you have the line which matches your identifier, you have the first line of the PHP process ID ( PID ).

Happy hunting!