Xdebug Cachegrind and MAMP Pro on Mac OSX

Are you running MAMP on OSX? Want to profile your PHP code with Xdebug? It’s easy and here are the steps.

How to run Xdebug on Mac OSX using MAMP

1 Start MAMP

2 Edit php.ini template file through MAMP to enable the extension. Edit the template file via File -> Edit Template -> PHP -> PHP [php version you are using] php.ini

edit php.ini template for mamp on mac osx

3 Edit bottom of php.ini template file so that it ends up looking like if you want profile output

[xdebug]
zend_extension="/Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
xdebug.profiler_enable = 1
xdebug.profiler_output_dir = "/tmp"
; DONT REMOVE: MAMP PRO php5.3.6.ini template compatibility version: 1

If you don’t want profile output and just want xdebug running then use

[xdebug]
zend_extension="/Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
xdebug.profiler_enable = 0
xdebug.profiler_output_dir = "/tmp"
; DONT REMOVE: MAMP PRO php5.3.6.ini template compatibility version: 1

Now when you have errors, if they are sent to standard out, you will see something like this

My php.ini file:

edited php.ini file to run xdebug on mamp mac osx

4 Save edited template and close edit window

5 Restart MAMP

6 Open MAMP’s WebStart page and navigate to PHPInfo tab. Check to make sure that Xdebug is running. Doing a search in the browser window for “Xdebug” makes this easy.

confirm xdebug running on mamp on mac osx

7 If you used the same settings that I have above, when you run PHP code, Xdebug will put the cachegrind.out files in your ‘/tmp’ directory. Open your ‘/temp’ directory and run one of your PHP files to make sure it is working correctly. You can open the ‘/tmp’ directory in finder by opening the terminal and running

open /tmp

cachegrind.out output in /temp folder for mamp on mac osx

8 Now you can use any app that understands those cachegrind.out files to view the profile data. Apps like KCacheGrind (Linux/Windows, KDE), WinCacheGrind (Windows), xdebugtoolkit, and Webgrind.  I went the simple route and used webgrind. Webgrind is a simple web based application that you can run locally on MAMP and it will look for the cachegrind.out files automatically with just one click. Continue for steps on setting up with webgrind.

9 Download Webgrind

10 Setup Webgrind host on MAMP to run Webgrind

Setup webgrind as host on mamp to process xdebug php profile output

11 Visit webgrind url setup on your local MAMP installation. Mine was simply webgrind/

12 If you already have cachegrind output files you should be able to select the file in the “Auto (newest)” dropdown or leave it select at Auto and click update which will reveal the profile data

13 Throw a celebratory fist pump

 

I think it’s awesome that the MAMP guys have included Xdebug with their release of MAMP 2.01. I wrote this blog post because I wanted to be able to profile some of my projects to find areas where they could be improved. After finally figuring it out it was super simple. The problem was that it took me a long time to set up because of all of the incorrect and outdated information on the internet. I hope this makes it easier for others to get setup. If this post becomes outdated please leave a comment or contact me about what needs to be updated.

— UPDATE —

I am LOVING Xdebug! Just realized that when you do a var_dump of some variable, Xdebug formats it for you so it’s actually readable! AMAZING!!!!

Before:

After:

Things I searched for to find my answers:

php profile

xdebug osx

mamp xdebug

php xdebug osx

webgrind

maccallgrind

cachegrind osx

cachegrind

valgrind

xdebug mamp

php.ini add extension mamp