[Sh] strace PHP debug tips

One of the most important functions used to debug executions and processes is strace.


However, It should be handled carefully when debugging an non trusted app.

An example of usage on a multi-tenant cPanel env is when a website is too slow as if it’s stuck at a specific stage (e.g. db connection or an external call):

/sbin/cagefs_enter_user $USERNAME strace -s 1024 -T php index.php

P.S.: We use cagefs command to isolate tenants and limit permissions

  /sbin/cagefs_enter_user $USERNAME "command"

WHM also provides a GUI strace tool on System Health > Process Manager.

Useful links:

Basic guide for using strace to diagnose software problems on Linux

How to strace cPanel or WHM processes


Specific URLs

Let’s taker as an example PHP when given parameters or when the URL is rewritten while the issue is specifically with these arguments at specific page (for example a WordPress page in which a plugin is invoked), here what I do is:

– Pass the parameters (e.g. $_GET & $_POST) manually in the index.php if I can identify the behavior.

– or Add a sleeping to the code or invoke it from another script, and then get the process ID (using for example ps) and apply the strace directly on the PID.

Enhancing strace effectiveness:

  • Filtering: Use strace filters to focus on specific system calls related to network activity, like connect, read, and write. This will reduce the output to relevant information, streamlining analysis.
  • Conditional tracing: Consider utilizing tools like stracetop or perf to capture system call data only when certain conditions are met (e.g., specific URL accessed). This further streamlines the data you need to analyze.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *