Debug

with netbeans and Xdebug

Outline

  • Terms
  • Installation
  • Creating project
  • Setup
  • Debugging
  • Demo

Terms

  • Break point: where the code execution must be stop.
  • Call stack: how the current line of code is called.
  • Local debug: apache2 + php + xdebug + ide + source code are all in your local machine.
  • Remote debug: apache2 + php + xdebug + source code are in your VPS, ide is in your local machine, you must download source code.

Installation (Netbeans)

  • 7.2.1 for stable, local debugging
  • 8.0.2 for new features, remote debugging

Installation (xdebug)

  • Always use newest version (2.2.6)
  • Be careful with the configuration

Xdebug config

[xdebug]
;zend_extension=/usr/lib/php5/20121212/xdebug.so

; This is for local debugging
; xdebug.remote_host=127.0.0.1
; xdebug.remote_connect_back=0
; This is for virtual host (cli)
; xdebug.remote_host=192.168.56.1
; xdebug.remote_connect_back=0

; This is for remote debugging
xdebug.remote_connect_back=1

xdebug.remote_enable=1
xdebug.remote_autostart=0
xdebug.remote_cookie_expire_time=86400
xdebug.remote_port=9000
xdebug.idekey=”netbeans-xdebug”
xdebug.profiler_enable=0
xdebug.profiler_enable_trigger=1
xdebug.profiler_output_dir=/tmp
xdebug.profiler_output_name=cachegrind.out.%p.%R.%r

remote_enable

If it is set to 0, we can not debug at all

remote_autostart

If it is set to 1, we can not debug simpletest test cases

$response = \file_get_contents('http://dev.gocatalyze.com/crm/people');

remote_autostart

If it is set to 0, we can not debug from console, but we can overcome it

bash, shell:
  vim ~/.bashrc
  export XDEBUG_CONFIG="idekey=netbeans-xdebug"
  exec bash
fish:
  vim ~/.config/fish/config.fish
  set -x XDEBUG_CONFIG "idekey=netbeans-xdebug"
  exec fish

sudo service apache2 restart (if needed)

remote_connect_back

If it is set to 0, we have to make sure our local machine's ip is static and does not change every time we start debug session

Static ip/single developer

Unknown ip/multiple developers

remote_cookie_expire_time

If we want to wait for a long time before press 'Continue' button, we have to set it to a big number

Creating project

Creating project

Creating project

Creating project

Creating project

Creating project

Setup

Debugging

Debugging

Debugging

Debugging

Debugging

Debugging

Debugging

Debugging

Debugging

Debugging

Conditional breakpoint

Not available in netbeans for PHP, but we can do it manually

if ($var == 123 || in_array($item, $array)) {
  // breakpoint goes below.
  $a = 'b';
}

Demo

  • Debug web pages
  • Debug script on command line

References

xdebug

By Tiến Võ Xuân