Debugging CLI php script

Hello,

I've tried to find a way to debug a CLI php script I'm working on and the few solution I've found all require editing one or more .ini anc configurations files, that I don't want to edit, since it took me hours to make xDebug work on MAMP Pro (so, it's a Mac for these people that doesn't know it) and I don't want to lose this environment.

So, is it possible that debugging php should be such a pain?
What should I do to simply debug my CLI php scripts with xDebug, except praying (that does't work much)?

Thanks.

14 comments

Andrea,

ini settings are pretty straightforward for CLI debugging. Just make sure that they have
zend_extension="<full_path_to_xdebug_lib>"
xdebug.remote_enable=true

Please also be sure that CLI uses the same INI file that you're editing. Usually MAMP and CLI uses different php.ini.

0

Maxim,

Maybe I wasn't clear.

The issue is not on debugging in general: I can do this already with php script that runs in a local web server.

What I can't do is debugging a CLI php script.

What I've found so far, even here in the community, is confusing, unclear, undetailed or just non understandable.

I want to be able to debug both CLI and web php scripts and I don't want to change any .ini file each time I switch from a CLI to a Web script.

0

I understand the problem. It's possible to debug CLI script and web server script with the same INI. I'm just trying to figure out why your configuration doesn't work for CLI. Could you please copy paste output that you will get by going to Settings->PHP choosing desired interpreter and clicking "i" button?

0

Hi there,

What I can't do is debugging a CLI php script.

What I've found so far, even here in the community, is confusing, unclear, undetailed or just non understandable.

I want to be able to debug both CLI and web php scripts and I don't want to change any .ini file each time I switch from a CLI to a Web script.

Please describe what you mean by "CLI php script" .. because as far as I'm aware it's just a matter of using Run/Debug Configuration of different type in PhpStorm ("PHP Script" is for executing your php code in CLI environment).

0

Sure:

PHP version: 5.4.10

Loaded extensions:
bcmath, bz2, calendar, Core, ctype, curl,        date, dom, ereg, exif, fileinfo, filter, ftp, gd, gettext, hash, iconv,        imap, json, ldap, libxml, mbstring, mcrypt, mysql, mysqli, openssl,        pcre, PDO, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix,        Reflection, session, SimpleXML, soap, sockets, SPL, sqlite3, standard,        tokenizer, XCache, xdebug, xml, xmlreader, xmlwriter, xsl, yaz, zip, zlib

Configuration options:

allow_url_fopen 1
allow_url_include 0
always_populate_raw_post_data 0
arg_separator.input &
arg_separator.output &
asp_tags
assert.active 1
assert.bail 0
assert.callback
assert.quiet_eval 0
assert.warning 1
auto_append_file
auto_detect_line_endings 0
auto_globals_jit 1
auto_prepend_file
bcmath.scale 0
browscap
curl.cainfo
date.default_latitude 31.7667
date.default_longitude 35.2333
date.sunrise_zenith 90.583333
date.sunset_zenith 90.583333
date.timezone Europe/Berlin
default_charset
default_mimetype text/html
default_socket_timeout 60
disable_classes
disable_functions
display_errors
display_startup_errors
doc_root
docref_ext
docref_root
enable_dl 1
enable_post_data_reading 1
error_append_string
error_log /Applications/MAMP/logs/php_error.log
error_prepend_string
error_reporting 32767
exif.decode_jis_intel JIS
exif.decode_jis_motorola JIS
exif.decode_unicode_intel UCS-2LE
exif.decode_unicode_motorola UCS-2BE
exif.encode_jis
exif.encode_unicode ISO-8859-15
exit_on_timeout 0
expose_php 1
extension_dir /Applications/MAMP/bin/php/php5.4.10/lib/php/extensions/no-debug-non-zts-20100525/
file_uploads 1
filter.default unsafe_raw
filter.default_flags
from
gd.jpeg_ignore_warning 0
highlight.comment #FF8000
highlight.default #0000BB
highlight.html #000000
highlight.keyword #007700
highlight.string #DD0000
html_errors 0
iconv.input_encoding ISO-8859-1
iconv.internal_encoding ISO-8859-1
iconv.output_encoding ISO-8859-1
ignore_repeated_errors
ignore_repeated_source
ignore_user_abort 0
implicit_flush 1
include_path .:/Applications/MAMP/bin/php/php5.4.10/lib/php
ldap.max_links -1
log_errors 1
log_errors_max_len 1024
mail.add_x_header 0
mail.force_extra_parameters
mail.log
max_execution_time 0
max_file_uploads 20
max_input_nesting_level 64
max_input_time -1
max_input_vars 1000
mbstring.detect_order
mbstring.encoding_translation 0
mbstring.func_overload 0
mbstring.http_input pass
mbstring.http_output pass
mbstring.http_output_conv_mimetypes ^(text/|application/xhtml\+xml)
mbstring.internal_encoding
mbstring.language neutral
mbstring.strict_detection 0
mbstring.substitute_character
mcrypt.algorithms_dir
mcrypt.modes_dir
memory_limit 32M
mysql.allow_local_infile 1
mysql.allow_persistent 1
mysql.connect_timeout 60
mysql.default_host
mysql.default_password
mysql.default_port
mysql.default_socket /Applications/MAMP/tmp/mysql/mysql.sock
mysql.default_user
mysql.max_links -1
mysql.max_persistent -1
mysql.trace_mode
mysqli.allow_local_infile 1
mysqli.allow_persistent 1
mysqli.default_host
mysqli.default_port 3306
mysqli.default_pw
mysqli.default_socket
mysqli.default_user
mysqli.max_links -1
mysqli.max_persistent -1
mysqli.reconnect 0
open_basedir
output_buffering 0
output_handler
pcre.backtrack_limit 1000000
pcre.recursion_limit 100000
pdo_mysql.default_socket /Applications/MAMP/tmp/mysql/mysql.sock
pgsql.allow_persistent 1
pgsql.auto_reset_persistent
pgsql.ignore_notice 0
pgsql.log_notice 0
pgsql.max_links -1
pgsql.max_persistent -1
phar.cache_list
phar.readonly 1
phar.require_hash 1
post_max_size 32M
precision 12
realpath_cache_size 16K
realpath_cache_ttl 120
register_argc_argv 1
report_memleaks 1
report_zend_debug 0
request_order
sendmail_from
sendmail_path /usr/sbin/sendmail -t -i
serialize_precision 100
session.auto_start 0
session.cache_expire 180
session.cache_limiter nocache
session.cookie_domain
session.cookie_httponly
session.cookie_lifetime 0
session.cookie_path /
session.cookie_secure
session.entropy_file
session.entropy_length 0
session.gc_divisor 100
session.gc_maxlifetime 1440
session.gc_probability 1
session.hash_bits_per_character 4
session.hash_function 0
session.name PHPSESSID
session.referer_check
session.save_handler files
session.save_path /Applications/MAMP/tmp/php
session.serialize_handler php
session.upload_progress.cleanup 1
session.upload_progress.enabled 1
session.upload_progress.freq 1%
session.upload_progress.min_freq 1
session.upload_progress.name PHP_SESSION_UPLOAD_PROGRESS
session.upload_progress.prefix upload_progress_
session.use_cookies 1
session.use_only_cookies 1
session.use_trans_sid 0
short_open_tag 1
SMTP localhost
smtp_port 25
soap.wsdl_cache 1
soap.wsdl_cache_dir /tmp
soap.wsdl_cache_enabled 1
soap.wsdl_cache_limit 5
soap.wsdl_cache_ttl 86400
sql.safe_mode
sqlite3.extension_dir
track_errors
unserialize_callback_func
upload_max_filesize 32M
upload_tmp_dir /Applications/MAMP/tmp/php
url_rewriter.tags a=href,area=href,frame=src,input=src,form=,fieldset=
user_agent
user_dir
user_ini.cache_ttl 300
user_ini.filename .user.ini
variables_order EGPCS
xcache.admin.enable_auth 1
xcache.cacher 1
xcache.coredump_directory /Applications/MAMP/tmp/phpcore/
xcache.count 1
xcache.coveragedump_directory
xcache.coverager
xcache.experimental 0
xcache.gc_interval 0
xcache.mmap_path /Applications/MAMP/tmp/xcache
xcache.optimizer 1
xcache.readonly_protection
xcache.shm_scheme mmap
xcache.size 64M
xcache.slots 8K
xcache.stat 1
xcache.test
xcache.ttl 0
xcache.var_count 1
xcache.var_gc_interval 300
xcache.var_maxttl 0
xcache.var_size 64M
xcache.var_slots 8K
xcache.var_ttl 0
xdebug.auto_trace 0
xdebug.cli_color 0
xdebug.collect_assignments 0
xdebug.collect_includes 1
xdebug.collect_params 0
xdebug.collect_return 0
xdebug.collect_vars 0
xdebug.coverage_enable 1
xdebug.default_enable 1
xdebug.dump.COOKIE
xdebug.dump.ENV
xdebug.dump.FILES
xdebug.dump.GET
xdebug.dump.POST
xdebug.dump.REQUEST
xdebug.dump.SERVER
xdebug.dump.SESSION
xdebug.dump_globals 1
xdebug.dump_once 1
xdebug.dump_undefined 0
xdebug.extended_info 1
xdebug.file_link_format
xdebug.idekey
xdebug.max_nesting_level 100
xdebug.overload_var_dump 1
xdebug.profiler_aggregate 0
xdebug.profiler_append 0
xdebug.profiler_enable 0
xdebug.profiler_enable_trigger 0
xdebug.profiler_output_dir /var/tmp/
xdebug.profiler_output_name cachegrind.out.%p
xdebug.remote_autostart 0
xdebug.remote_connect_back 0
xdebug.remote_cookie_expire_time 3600
xdebug.remote_enable 0
xdebug.remote_handler dbgp
xdebug.remote_host localhost
xdebug.remote_log
xdebug.remote_mode req
xdebug.remote_port 9000
xdebug.scream 0
xdebug.show_exception_trace 0
xdebug.show_local_vars 0
xdebug.show_mem_delta 0
xdebug.trace_enable_trigger 0
xdebug.trace_format 0
xdebug.trace_options 0
xdebug.trace_output_dir /var/tmp/
xdebug.trace_output_name trace.%c
xdebug.var_display_max_children 128
xdebug.var_display_max_data 512
xdebug.var_display_max_depth 3
xmlrpc_error_number 0
xmlrpc_errors 0
xsl.security_prefs 44
yaz.keepalive 120
yaz.log_file
yaz.log_mask
yaz.max_links 100
zend.detect_unicode 1
zend.enable_gc 1
zend.multibyte 0
zend.script_encoding
zlib.output_compression
zlib.output_compression_level -1
zlib.output_handler
0

CLI php script is a php script I run from the command line (from PHPStorm, using the command line window).

When I enable the listener, and I set a breakpoint, nothing happens.

0

It's not the right way to run scripts from PhpStorm:) Open script in Editor->Right Click->Debug or you may create corresponding configuration.

0

Based on your phpinfo table: you clearly have xdebug.remote_enable = 0 (disabled) while it must be enabled.

http://confluence.jetbrains.com/display/PhpStorm/Xdebug+Installation+Guide

0

In such scenario you definitely need to edit your .php.ini (which is what you categorically do not want to do) .. or launch php interpreter/script with additional parameters (to enable those xdebug settings), which is inconvenient to do manually to say the least. "PHP Script" Run/Debug Configuration does it for you (extra parameters that tell xdebug that this script needs to be debugged).

0

In latest EAP build of PhpStorm v7, you can use built-in tool that will help you validating your debugger setup (php.ini): http://blog.jetbrains.com/phpstorm/2013/08/debugger-configuration-validation-with-phpstorm/

0

Well, that's strange, as I clearly debug php when running from a local site.

0

I have no idea why it says that is not enabled. I fI open the php.in, this is the [xdebug] section:

[xdebug]

zend_extension="/Applications/MAMP/bin/php/php5.4.10/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so"


xdebug.default_enable=1

xdebug.coverage_enable=1

xdebug.profiler_enable = 1

xdebug.profiler_output_dir = "/tmp"

xdebug.remote_enable=1

xdebug.remote_host=localhost

xdebug.remote_port=9000

xdebug.remote_autostart=1

0

Most likely you do have 2 php.ini files (they may have different names, not necessary php.ini): one for Web interface and one for CLI (you should know this already: http://devnet.jetbrains.com/message/5487515#5487515 ).

This phpinfo you showed here was taking by clicking on a button in PhpStorm (which will run PHP in CLI mode, obviously). Unfortunately it does not show ALL details, only the important settings. Instead -- write simple script that calls phpinfo() and execute it in command line / terminal -- there check what config file was used (top of the output) and see if you have edited the correct php.ini file.

0

Well, this is driving me crazy.

The thread you've linked helped me somehow to debug php scripts that are ran from a web server.
I never managed to debug clicking on the "play" button, but only to enable the listener.
After all this was for the best, as I can enable/disabel the debugger as I wish.

However, this doesn't seem to help with the CLI script.

For your information, I've checked on PhpStorm settings and the only php interpteter that is configured is the 5.4.
However, the command line still gives me the 5.3.15 version.

The EAP doesn't help much, as validating only shows the message "Failed to execute validation script", that is not much helpful: which scripts? Which errors did get the validation?

If I build a validation logic, I would at least try to give some more detailed information, instead of such a generic message.

0

Please sign in to leave a comment.