216

XDebug offers the configuration directive xdebug.profiler_enable_trigger that allows to activate profiling by passing the GET or POST parameter "XDEBUG_PROFILE" when calling a script via HTTP. This is handy if you don't want profiling for ALL of your scripts but only for a few special cases without always changing your PHP configuration.

Is there a way to achieve the same behavior for command line PHP programs? I tried to pass the XDEBUG_PROFILE as a command line argument but it didn't work.

In general, profiling command line PHP works well, but I'd like to have the same per-call-flexibility as with a browser and HTTP server.

yivi
  • 42,438
  • 18
  • 116
  • 138
selfawaresoup
  • 15,473
  • 7
  • 36
  • 47

11 Answers11

274

You can pass INI settings with the -d flag: php -d xdebug.profiler_enable=On script.php.

jou
  • 5,824
  • 3
  • 22
  • 24
  • 42
    On *nix based systems, you can make this a bit easier to use by creating an alias, such as: `alias phpp="php -d xdebug.profiler_enable=1"`. That way when you don't want to profile just use `php` and when you do use `phpp`. – J.C. Yamokoski Nov 29 '12 at 17:28
  • 16
    I had to specify which directory I wanted the profiler output to be saved in as well, so this woked for me: `alias xphp="php -d xdebug.profiler_enable=On -d xdebug.profiler_output_dir=."`. That always stores the debug output in your current working directory. – Lars Nyström Jun 14 '15 at 13:30
  • yeah, I'm under Eclipse and XAMPP in Win7 enviroment. It works. – gouchaoer Dec 27 '15 at 06:24
  • 4
    On Windows, you may create in your PATH dir a `phpp.cmd` file, containing `php -d xdebug.profiler_enable=On %*` then you can simply run `phpp `. – Gras Double Oct 14 '16 at 01:00
  • 1
    You also can add your parameters to the Shebang-command in a file like `#!/usr/bin/php -d xdebug.start_with_request=On` – Steve Kirsch May 01 '21 at 18:55
  • 2
    In Xdebug3 this still works, the setting is renamed to `-d xdebug.mode=profile` – Tobias K. Feb 23 '23 at 06:48
  • 1
    @LarsNyström The setting was renamed in Xdebug v3: you need to use `xdebug.output_dir` instead of `xdebug.profiler_output_dir` now – Alfred Bez Mar 15 '23 at 10:08
50

I got this working on Ubuntu/Netbeans by:

  • copying the xdebug config lines from the /etc/php5/apache2/php.ini file into /etc/php5/cli/php.ini
  • setting an environment variable with the name of the debug session (you can get this from the query string in the url of the page netbeans launches when you start debugging) the command is: export XDEBUG_CONFIG="idekey=netbeans-xdebug"

Then it's simply a case of starting debugging in netbeans and doing "php myscript.php" at the command line.

Andrew Hancox
  • 2,330
  • 1
  • 20
  • 28
35

with PhpStorm on remote webserver i use this command:

XDEBUG_CONFIG="idekey=PHPSTORM" PHP_IDE_CONFIG="serverName=server_name" php -dxdebug.remote_host=`echo $SSH_CLIENT | cut -d "=" -f 2 | awk '{print $1}'` myscript.php

where server_name stands for name of the server in PhpStorm project conifuguration

oliver nadj
  • 788
  • 7
  • 21
  • 5
    i added this to .bash_aliases `alias xphp="XDEBUG_CONFIG="idekey=PHPSTORM" PHP_IDE_CONFIG="serverName=example.com" php -d memory_limit=1G -d xdebug.remote_host=`echo $SSH_CLIENT | cut -d \"=\" -f 2 | awk '{print $1}'`"` so when i wanna debug i use xphp instead of php – oliver nadj Aug 01 '14 at 07:24
27

As described on the Xdebug Remote Debugging page, profiling can also be enabled via the XDEBUG_CONFIG environment variable by inluding a "profile_enable=1" directive:

XDEBUG_CONFIG="profiler_enable=1" php ...

For ease of use, the above command line can be written as an alias:

alias xphp='XDEBUG_CONFIG="profiler_enable=1" php'

The alias can be added to one of your shell's (interactive) startup scripts, such as ~/.bash_aliases or ~/.bashrc (as appropriate to your system).

outis
  • 75,655
  • 22
  • 151
  • 221
  • 1
    This one should be the accepted answer as it's the most accessible method. PHP binary might be wrapped, it might be set as interpreter #!/x/php at places you don't want to touch. Using the env variable it can be selectively enabled – John Dec 20 '19 at 16:45
12

Similar, but different process for getting it to work with Netbeans while developing on a VM.

Need to pass in the remote enabled flag, the auto start flag, the ide flag, and the name of your remote host.

php -dxdebug.remote_enable=1 -dxdebug.remote_autostart=On -dxdebug.idekey=netbeans-xdebug -dxdebug.remote_host=NAME.OF.HOST script.php
Joshua Dance
  • 8,847
  • 4
  • 67
  • 72
4

I created a shell script to handle client debugging.

script name: phpdebug

#!/usr/bin/ksh
php -dxdebug.remote_host=`echo $SSH_CLIENT | cut -d "=" -f 2 | awk '{print $1}'` $*

I placed this script in /usr/bin and gave it execute permissions.

The script takes the arguments passed into phpdebug and calls php with the xdebug arguments and appends the arguments passed into the shell script, the $* on the end.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • Yes, in my case anyway, all I needed was to add the INI setting for my remote host. That was all that was required to do debugging in my IDE from a PHP CLI command. – Spencer Williams Sep 13 '17 at 17:10
  • Actually, I did also need to set the `XDEBUG_CONFIG` environment variable. Just that and the `xdebug.remote_host` setting made it work. – Spencer Williams Sep 14 '17 at 19:56
4

For Xdebug 3

Now enabling Xdebug for a single script would easily be accomplished using the XDEBUG_MODE environment variable:

XDEBUG_MODE=profile php script_to_be_profiled.php
Daniel Sloof
  • 12,568
  • 14
  • 72
  • 106
yivi
  • 42,438
  • 18
  • 116
  • 138
2

In PhpStorm 7 using WAMP I got this to work by copying my already working xdebug settings from C:\wamp\bin\apache\apache2.2.22\bin\php.ini to the xdebug section of C:\wamp\bin\php\phpX.Y.Z\php.ini. Then I ran my script like so:

php -d xdebug.idekey=PHPSTORM script.php

This even worked for debugging laravel artisan scripts

php -d xdebug.idekey=PHPSTORM artisan db:seed --force
Chukky Nze
  • 720
  • 3
  • 13
1

Documentation from Jetbrains

To start the script with debugging using PHP command line switches Set an environment variable that would tell XDebug to connect to IDE:

Windows / MacOS / Linux

export XDEBUG_CONFIG="idekey=123"  

Here idekey should have a random value.

Launch PHP with the following command-line options:

php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 -dxdebug.remote_connect_back=0 path/to/script.php

You may use 10.0.2.2 instead of 127.0.0.1 with Vagrant (see related SO question).

Vaibhav Vishal
  • 6,576
  • 7
  • 27
  • 48
adm1n
  • 29
  • 3
0

Welcome to xdebug 3!

It is now:

xdebug.mode=profile

therefore:

php -d xdebug.mode=profile script.php
Fabian Blechschmidt
  • 4,113
  • 22
  • 39
0

I needed to add the following to my php.ini:

xdebug.mode=debug

https://xdebug.org/docs/step_debug#configure

And the following to my .zshrc

export XDEBUG_SESSION=1

https://xdebug.org/docs/step_debug#activate-debugger-cmd

Nikki
  • 261
  • 1
  • 4
  • 10