29

I haven't quite figured this out. EVERY piece of documentation I've found covers how to use xdebug to debug scripts running in Apache. I need to debug a php CLI script.

So, for instance, how do I pass the XDEBUG_SESSION_START variable in to get xdebug to kick on?

I'm specifically trying to debug a CakePHP shell. So if anyone has any additional insight into that I'd be very appreciative.

Thanks.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
Laran Evans
  • 1,283
  • 5
  • 15
  • 31
  • You may also want to look here: http://stackoverflow.com/questions/2288612/how-to-trigger-xdebug-for-a-command-line-php-script – selfawaresoup May 23 '10 at 10:33

5 Answers5

41

There is a couple of notes about that in Xdebug's manual, like, for instance (quoting) :

export XDEBUG_CONFIG="idekey=session_name"
php myscript.php

If you are using Eclipse PDT to develop and debug your PHP scripts, there is not much difference between Apache or CLI : the configuration lloks quite the same, you just don't have to configure a web server, nor indicate an URL ; instead, you have to indicate the path to the PHP executable.

About the XDEBUG_SESSION_START variable : well, you launch the whole script in "debug-mode", so you don't have any notion of "debugging-session", I'd say.


For instance, here's what Window > Preference > PHP > PHP executables looks like for me right now, and, on the right, what I get when clicking on the Edit button of the first one :


(source: pascal-martin.fr)
   
(source: pascal-martin.fr)

And the debug configurations window :


(source: pascal-martin.fr)

And launching the debugging: it just works :


(source: pascal-martin.fr)


Hope this helps :-)

Else, what specific problem do you encounter ?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
9

If you're using bash (or similar shell), this little script might come in handy:

alias drush-debug=drd
function drd {
    export XDEBUG_CONFIG="idekey=cli_session"
    export SERVER_NAME="developer.machine"
    export SERVER_PORT="9000"
    drush "$@"
    unset XDEBUG_CONFIG
    unset SERVER_NAME
    unset SERVER_PORT
};

or as suggested by the commentators below

alias drd='XDEBUG_CONFIG="idekey=PHPSTORM" drush "$@"'

This way you don't have to manually set and unset the trigger variable each time you want to debug.

Plamen
  • 165
  • 2
  • 10
  • 7
    This is logically equivalent to putting the environment variable on the same line as the command in bash, i.e.: "XDEBUG_CONFIG='idekey=cli_session' drush $*". There's no need for a function which sets and then unsets the variable, the shell will export the variable automatically to the command. – Stabledog Jun 14 '13 at 20:32
  • 1
    @Stabledog This, however, if they're not unset xDebug will be enabled for all subsequent drush commands in the current shell, if I'm not mistaken. The idea behind this function was to temporarily enable PHP CLI debugging, but it has its caveats. – Plamen Sep 24 '14 at 17:01
  • 1
    @Plamen use `drush "$@"` instead. bash will expand each argument with quotes so that `drush-debug cc "theme registry"` would work. It's not the most intuitive aspect of bash, but it's nice to have. – Justin C Sep 26 '14 at 15:19
  • @Stabledog I'll take your word for it. However, I find it much easier to type `drd` instead of prepending a bunch of variables each time the command is invoked. – Plamen Nov 24 '14 at 19:15
  • @Stabledog is correct: all you need is `alias drd='XDEBUG_CONFIG="idekey=PHPSTORM" drush "$@"'`. Note there's no semicolon between the variable assignment and the drush call. The variable will be set only for the duration of the drush call and will not be exported to the shell. – ksenzee Feb 16 '16 at 22:45
4

simply put the following section to your php.ini

[XDebug]
xdebug.max_nesting_level = 200
xdebug.remote_enable=1
xdebug.remote_port=9000
;xdebug.profiler_enable=1
xdebug.idekey=PHPSTORM
xdebug.remote_autostart=1

and replace PHPSTORM with your ide key

Niels
  • 49
  • 1
1

For Windows and Visual Studio Code here's how to proceed:

  1. Download xdebug from https://xdebug.org/download. For example php 7.4 Windows 64bit https://xdebug.org/files/php_xdebug-2.9.5-7.4-vc15-nts-x86_64.dll

  2. Copy the xdebug dll to your php extensions dir (ext).

  3. Add to the end of php.ini

    [XDebug]
    zend_extension=php_xdebug-2.9.5-7.4-vc15-nts-x86_64.dll

    xdebug.remote_enable=1
    xdebug.remote_autostart=1
  1. Open VSCode and install https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug

  2. Open the project workspace in VSCode, go to Run tab, click the cogwheel and add these lines

    {
      "name": "listen CLI",
      "type": "php",
      "request": "launch",
      "port": 9000
    },
    {
      "name": "run CLI",
      "type": "php",
      "request": "launch",
      "program": "${file}",
      "cwd": "${fileDirname}",
      "port": 9000
    }
  1. Place a break point in the script you want to debug

  2. Select "run CLI" and click "Start Debugging"

Happy debugging!

8ctopus
  • 2,617
  • 2
  • 18
  • 25
  • Thanks for this. This helped me set up the php.ini that my VSC Powershell was accessing. It was the "xdebug.remote_enable=1" that the php.ini was missing, after which the debugger worked fine with PHPUnit in my VSC. – quinny Jul 14 '20 at 20:16
0

PHP configuration:

zend_extension=xdebug.so
xdebug.remote_handler=dbgp
xdebug.mode=debug

On my system it is file /etc/php/conf.d/xdebug.ini. Could be in the main php.ini too.

If I want to run a script with XDebug, I pass an environment variable inline:

XDEBUG_SESSION=1 php arguments
Michas
  • 8,534
  • 6
  • 38
  • 62