Php xdebug command line

Command Line Debug Client

The command line debug client allows you to debug PHP scripts without having to set up an IDE.

Installation #

A binary for Linux, macOS, and Windows is available on the downloads page. You only have to download the binary, which you can then run from a command line.

Command Line Options #

The following command line options are available:

-1 Debug once and then exit
-f Whether act as fully featured DBGp client. If this is enabled, the client will perform certain tasks for you, such as enabling async support. In the future, this mode will turn the dbgpClient in a fully fledged command line debugging client without the need for you to remember all DBGp commands.
-h Show this help
-p value Specify the port to listen on [ 9003 ]. This is the same port that Xdebug should initiate a connection on. On the Xdebug side, this port can be configured with the xdebug.remote_port setting.
-r idekey If the -r option is given, the client will register itself with a debugging proxy (selected with -y ), and then wait for incoming debugging connections as usual on the port configured with -p .
-s Enables SSL. With this option on, the client expects incoming connections on the configured port to be in SSL. This is an experimental feature that is not fully finished. Right now, Xdebug does not support SSL yet, but the dbgpProxy does.
-u idekey If the -u option is given, the client will unregister itself with a debugging proxy (selected with -y ), and then quit.
-v Show version number and exit
-x Show protocol XML. With this option enabled, the client will also show the raw XML data that the debugging engine sends back. This can be useful for debugging issues with the interaction between the debugger engine, for example Xdebug, and this client.
-y host:port Configures the host and port of an DBGp proxy. This option is used with -r and -u only.
Читайте также:  Php curl http client

Usage #

To start the client on the command line on Linux, open a shell, and then run:

If the binary doesn’t start or you get a not found message, please refer to this FAQ entry.

To start the client on the command line on MacOS, open a shell, and then run:

On Windows, open a command a Command Prompt and run:

In all cases, you can add the command line options as described above.

When the debug client starts, it shows some version information. It will then wait until a connection is made to it by a debugging engine (such as Xdebug):

Xdebug Simple DBGp client (0.4.2) Copyright 2019-2020 by Derick Rethans Waiting for debug server to connect on port 9003.

After a connection is received, the debug client shows basic information, and then shows the (cmd) prompt and waits for input:

Connect from [::1]:51884 DBGp/1.0: Xdebug 3.0.0 — For PHP 7.4.3-dev Debugging file:///tmp/xdebug-test-2.php (ID: 5242/dr) (cmd)  

On this command prompt you can then enter the available DBGp commands. With you can auto complete your command. You can use arrow up to a previous line, and use ctrl-R to search through your previous issued commands.

Here we step into the next line (the first line, in our case), and see which variables are available:

(cmd) step_into 1 | step_into > break/ok 1 | file:///tmp/xdebug-test-2.php:14 (cmd) context_get 2 | context_get 2 | uninitialized $a 

The following commands and options are common:

Command Description
breakpoint_set -t line -f file:///xdebug-test-2.php -n 5 Sets a breakpoint on line 5 of file:///xdebug-test-2.php
step_into Steps to the next executable line in the code
run Runs the script until the next breakpoint, or when the script ends
context_get Lists the variables and their values in the current scope
property_get -n $a Retrieves the value of the property $a

There is a full description in the DBGp documentation.

If the client has been started in «fully featured mode» ( -f ), and you’re running the latest Xdebug from GitHub, then it is possible to pause a running debugging session by pressing Ctrl-C . The client will return to the prompt where you can then issue commands as normal.

On the client’s prompt you can abort the debugging session, and then the client, with Ctrl-C .

This site and all of its contents are Copyright © 2002-2023 by Derick Rethans.
All rights reserved.

Источник

Debug a PHP CLI script

PHP applications are not always web applications. Various command line tools, daemons, message queue processing applications and other types of applications typically run in the PHP CLI. There are several ways to start a PHP CLI debugging session. You can start it from within PhpStorm and make it start the script and attach the debugger to it. Alternatively, you can let PhpStorm listen for incoming debugger connections and start the script outside the IDE. We’ll take a look at both options.

Before you start debugging, make sure that you have a debugging engine installed and configured properly. PhpStorm supports debugging with two most popular tools: Xdebug and Zend Debugger. These tools cannot be used simultaneously because they block each other. To avoid this problem, you need to update the corresponding sections in the php.ini file as described in Configure Xdebug and Configure Zend Debugger.

Open the active php.ini file in the editor:

  1. In the Settings dialog ( Ctrl+Alt+S ), click PHP .
  2. On the PHP page that opens, click next to the CLI Interpreter field.
  3. In the CLI Interpreters dialog that opens, the Configuration file read-only field shows the path to the active php.ini file. Click Open in Editor .

Start a debugging session from PhpStorm

To start debugging a PHP CLI script from within PhpStorm, perform the following steps.

Create a Run/Debug Configuration

PhpStorm uses Run/Debug configurations to execute a script from within the IDE. A configuration can define additional arguments for the PHP interpreter as well as launch other commands prior to starting our script. We will need a Run/Debug configuration to start the debugger from within PhpStorm.

Create a Run/Debug configuration for a PHP script manually

  1. Create a new Run/Debug configuration using the Run | Edit Configurations menu.
  2. Add a new configuration of the PHP Script type and provide the required parameters, such as the script to be executed.
  3. Save the created Run/Debug configuration.

Generate a Run/Debug configuration for a PHP script

  • Right-click in the Project tool window, and select Debug | .php from the context menu (make sure to pick the item marked with ). Alternatively, open the script in the editor, press Alt+Shift+F9 , and select the script to be debugged. The IDE will launch the script with the debugger enabled, and open the Debug tool window.

Launch the Debugger

Before launching the debugger, make sure that either a breakpoint is set or the Break at first line in PHP scripts option is enabled on the Debug page of the Settings dialog Ctrl+Alt+S .

  • Click on the PhpStorm toolbar.
  • Press Alt+Shift+F9 .
  • Select Run | Debug from the main menu.

Switch between configured PHP interpreters on the fly

  1. Press Ctrl+Shift+A and start typing Change PHP interpreter . In the suggestions list, select the Change PHP interpreter action. If necessary, you can assign a keyboard shortcut for this action either directly in the suggestions list by pressing Alt+Enter , or at a later point as described in Keyboard shortcuts.
  2. In the popup menu that opens, select one of the configured local or remote PHP interpreters.

The selected interpreter will be set as the default project interpreter on the PHP page of the Settings dialog ( Ctrl+Alt+S ). This will also affect configurations that use the default project interpreter (test frameworks’, quality tools’, and run/debug configurations) and commands run in the PhpStorm terminal.

Start a debugging session from the command line

Before you start a debugging session with PhpStorm when running CLI scripts, make sure that any of the following requirements is met:

  • Xdebug’s remote_autostart (for Xdebug 2) or start_with_request (for Xdebug 3) option is enabled.
  • XDEBUG_CONFIG environment variable exists.

Listening for incoming debugger connections

In PhpStorm, enable listening to incoming debug connections by either clicking on the toolbar or selecting Run | Start Listening for PHP Debug Connections . This will ensure PhpStorm reacts when a debugging session is started and opens the debug tool window automatically. Before launching the script, make sure that either a breakpoint is set or the Break at first line in PHP scripts option is enabled on the Debug page of the Settings dialog Ctrl+Alt+S .

Start the script with debugger options

Since we’ll be starting the script from the command line, we will have to make sure it is started with the required settings to enable the debugger.

Starting the script with Xdebug

Xdebug has various configuration options which we can use to let the PHP interpreter reach out to PhpStorm. These parameters have to be passed to the PHP interpreter using the -d command line parameter. Alternatively, you can set an environment variable so that you don’t need to provide the -d parameters every time.

Start the script with debugging using PHP command-line switches

php -dxdebug.mode=debug -dxdebug.client_host=127.0.0.1 -dxdebug.client_port=9003 -dxdebug.start_with_request=yes path/to/script.php

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

Start the script with debugging using an environment variable

set XDEBUG_CONFIG=remote_enable=1 remote_mode=req remote_host=127.0.0.1 remote_port=9000 remote_connect_back=0

export XDEBUG_CONFIG=»remote_enable=1 remote_mode=req remote_host=127.0.0.1 remote_port=9000 remote_connect_back=0″

Starting the script with Zend Debugger

Zend Debugger has various configuration options which we can use to let the PHP interpreter reach out to PhpStorm. These parameters have to be passed to the PHP interpreter using an environment variable.

Start the script with debugging

Optionally, to tell PhpStorm which path mapping configuration should be used for a connection from a certain machine, the value of the PHP_IDE_CONFIG environment variable should be set to serverName=SomeName , where SomeName is the name of the server configured on the PHP | Servers page of the Settings dialog ( Ctrl+Alt+S ).

If this environment variable is not set, you’ll be prompted to specify path mappings manually once IDE detects an incoming Xdebug connection.

Debug

Once the script is started, PhpStorm will open the Debug tool window and break at the first breakpoint that was set in the script. You can now continue debugging a PHP CLI script as described in Examine a suspended program.

If the script that is being debugged is not a part of the project that’s open in PhpStorm, the IDE will still open the script in the editor and pause execution at the first statement. This makes it possible to quickly debug any PHP CLI script, even if there is no PhpStorm project for it yet.

Источник

Оцените статью