Strace and You

Strace is a powerful tool, which can be used to quickly diagnose many Magento errors and at least get you headed in the right direction to diagnose performance if not outright solve the problem.  It is common to all Linux systems and works for not just PHP threads.

A page is generating an error

If you have page with a definite path, something like , you can actually call this page specifically and see what is going on. Use this command , adjust as needed. This works for Magento 1 and 2 but will not work if your site is running over HTTPS/SSL REQUEST_URI=/someproduct/page.html strace -s 1000 -e trace=sendto,connect,open,write php index.php > /dev/null

 this is the path trailing the URL, somewhat self explantoryHTTP_HOST this must always be the base URL, if the site is loading on www, use the www version. otherwise your trace will end up short

-s this the number of characters in a given call that are shown. 1000 will show most of if not all of a call, like a large MySQL query

-e  this restricts the calls shown. This is important or you will see a stream of incomprehensible munmap stuff.

php index this is the actual command being traced. note you can strace for just about anything or any other process.

> /dev/null  this just pipes output

After running this , you will see a lot of files opened, some select statements, and the various interworkings of Magento doing their thing.  This is a lot of information and you can log it by replacing the > /dev/null with &> /home/loglocations.txt

Whether any of the output is interesting depends on what you are looking for and there are many other flags/options for strace to get specific data (read the man page or other documentation).

Site is slow

If you are tracing for performance, the report generator is a more readable output, but in general look for things like this :

  • pauses in the trace, it should be very fast even with thousands of lines, a noticeable pause is cause for alarm
  • repetitious patterns. Typically this is many calls to cache or repeated SELECT queries that go on for dozens-hundreds-thousands of lines

The output will be hard to interpret at first, but if you can visibly see some of the output freeze, that is likely a problem point. Superfast lines are a good sign but if it stops long enough to read, its worth going back and checking it, especially MySQL queries.

Page or request is generating some error

  • Check near the end of the log, this is where a given error is likely to appear, especially if a blank page is coming up (also use display_errors On for the PHP options in the MHM vhost settings)
  • Look for output to log files or random stoppages at the end that do not seem to follow a logical progression. A segfault or other fatal error should be somewhat obvious here

I need to catch a request in the admin or over HTTPS, how do I trace that?

This is harder but it can be done, using –

you can watch all active php-fpm threads. However, this means you’ll capture other random requests so you may need/want to ask the customer if you can restrict their site to just your IP so you know that any traces logged are from your request.

From there, make the request and run the command. You should see whatever you want come up and then try to dig out any errors/clues.

In general, strace takes some practice and it is a bit like reading The Matrix.

Strace anything in Magento

For those stubborn hard to reach places like api calls or https places you can inject the following code into the index.php to catch the call. Change the allowed ip to whatever ip you’re accessign the site from.

Add the following just below the <? in the index.php

Add this at the end of the index.php

This will output an strace to the strace.out file in the webroot any time a call is made from the allowed ip. Any subsequent call will overwrite the strace.out

Strace a Stratus site

To use strace on stratus, you will need to add the following variables: SERVER_ADDR, REMOTE_ADDR & SERVER_NAME.

HTTPS=”on” REQUEST_URI=/someproduct/somepage.html SERVER_ADDR= REMOTE_ADDR= strace -s 1024 -tt -e trace=sendto,connect,open,write php index.php > /dev/null

SERVER_NAME will be the same as HTTP_HOST. You can use the internal IP of your stratus server for the other 2 variables: SERVER_ADDR & REMOTE_ADDR. Your internal IP can retrieved by using the command ip a

It will be the 192.168.x.x address listed under eth0

Other tricks

Cull specific data i.e. grep the live output, similar to above but we have to split it off

Updated on March 6, 2018

Was this article helpful?

Related Articles