problem with xdebug vim plugin - php

I am using xdebug plugin for vim. After making few changes i was able to run debugger but not able to set breakpoints.
So, I enabled xdebug.remote_log and below is the log statements corresponding to setting breakpoint.
<- breakpoint_set -i 5 -t line -f file:///C:\htdocs\testLocal.php -n 36
->
Its issuing request to debugger in proper format only but no idea why debugger is returning "command is not avilable".
Please let me know if anything is wrong.

Make sure you set breakpoints on lines that contain a php expression, not on blank lines or lines containing just a closing bracket.
Setting breakpoints on blank lines doesn't work, and returns the cryptic 'command not found' error you are getting.

I had the same problem. When I checked the xdebug logs, it showed file:///http://myapp.local
As you see my ide (PHP eclipse) seems to insert the extra "http://" treating this as a URL instead of a file.
Luckily, this was the case with just my index.php. Other file breakpoints were being communicated correctly to xdebug and I was able to set and hit breakpoints on php expression as long as they didn't know span multiple lines. The latter for some reason would cause breakpoints to not hit and just pass by.
Hope this helps!

I spotted an extra "/" in "file:///C:\htdocs\testLocal.php", mayby the vim plugin isn't compatible with windows filepaths?

I recently just installed this same thing and found a tutorial by Blake Johnson all about installing and using Xdebug from within Vim.
Also, try clearing any cookies on the page you might have. This often times would cause the plugin to get screwed up in a strange way.

Related

How to debug PHP CLI scripts from the CLI

Does anybody know how to debug CLI PHP scripts from the CLI?!? I don't want to debug a PHP web page - I don't have a PHP web page. I don't want to debug a remote script either - I'm running/debugging right here on this system. I don't want to (at this time) try to get some IDE (Eclipse, PhpStorm or whatever) to debug a CLI PHP rather I just want to debug some PHP CLI script at the Linux command line itself. In Perl this would be simply perl -d <script.pl> <options>. Debugging a script, to me, is not figuring out compile errors or other simple things. To me it's setting break points, running code, examine the contents of variables and being able to arbitrarily execute or eval ('<php code>') at the debugger.
Sure later I'd like to configure this into my IDE of choice (at this time this is Eclipse) but I have not managed to get that working. Debugging from the CLI a PHP CLI script would be a good start for me.
Thanks.
I don't know why I'm limited to a character count when posting a comment. Perhaps I can add more text here.
Here's what I have tried in order to use xdebug and/or Zend debugger with Eclipse:
Base Eclipse version Mars.1 Release 4.5.
Eclipse PDT UI Plugin version 3.7.0.2015112
Tried installing xdebug using pecl install xdebug. Says I need to add "zend_extension=xdebug.so" to php.ini. Really? Which php.ini? I have several:
Andromeda:sudo find / -xdev -name php.ini
/etc/php5/cli/php.ini
/etc/php5/apache2/php.in
/opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/php53/php.ini
/opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/php5/php.ini
/opt/eclipse/plugins/com.zend.php.debug.debugger.php56.linux.x86_64_13.0.1.v20151112-2045/resources/php56/php.ini
Andromeda:
I put the zend_extension thing in both /etc/php5/cli/php.ini and /etc/php5/apache2/php.ini. Made a phpinfo.php page and I see Xdebug in there (yea!). Configure a Debug Configuration in Eclipse to use xdebug and try to debug:
Launching renameUser has encountered a problem. An internal error occurred during "Launching renameUser" java.lang.NullPointerException.
Oh goodie...
I had also installed the Zend Debugger and added the following to those same two php.ini files:
zend_extension=/usr/lib/php5/20121212/ZendDebugger.so
zend_debugger.allow_hosts=127.0.0.1/32, 192.168.0.0/16
zend_debugger.expose_remotely=always
Changed debug configuration to use Zend Debugger and attempted to debug. Received:
Error launching 'renameUser' The debug session could not be started. Please make sure that the debugger is properly configured as a php.ini directive.
Restarted Eclipse and now the debugger attempts to run but simply terminates with a 255 exit value attempting to run /opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/php5/php-cgi. Why it's runnign php-cgi is beyond me. I said this was a CLI not a CGI. In any event I get the following when trying to run this from the command line:
Andromeda:/opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/php5/php-cgi
/opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/php5/php-cgi: error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory
Found a libiconv.so.2 in /opt/eclipse/plugins/org.zend.php.debug.debugger.linux.x86_64_5.3.18.v20110322/resources/lib and set LD_LIBRARY_PATH to include that but this just fails to launch the debugger at all stating to make sure that the debugger is properly configured as a php.ini directive... UGH!
Other odd stuff:
When Eclipse starts up it fails to open my RSE based project thus displaying edit buffers from the last run as empty
Eclipse will no longer exit! Select File: Exit. Nothing happens. Click on the X in the title bar - nothing happens. Now I have to kill it to close it!
When debugging on the command line you need to tell PHP you want to debug also
http://xdebug.org/docs/remote
says
export XDEBUG_CONFIG="idekey=session_name"
php myscript.php
assuming xdebug is enabled (it doesnt matter which .ini file its in, but there is a standard place per OS for this, usually in a conf.d folder called xdebug.ini which is auto included)
This allows you to debug a cmdline script.
Personally I use Vim with Vdebug extention (xdebug for vim) to debug and nothing other than a command line is needed
What worked for me was using dephpugger. I found the steps to get CLI debugging working here:
https://hackernoon.com/how-debug-php-applications-with-dephpugger-98cc234d917c
I had previously installed xdebug and tried out exussum's answer.
The steps I followed are included here for completeness.
Install dephpugger:
composer global require “tacnoman/dephpugger”:”dev-master”
Put dephpugger on the path:
export PATH=$PATH:$HOME/.composer/vendor/bin
make sure to include in the php file at the breakpoint:
xdebug_break();
open up 2 terminal instances. In one run:
dephpugger debug
In the other run
dephpugger cli /path/file.php
replacing /path/file.php with the path to your file. if you need command line arguments to the php script put the path and arguments in quotes. It still seems a little hacky. I think the real trick is to write in a language that has a better debugger built in.
exussums answer works for me.
In addition I have the following in /etc/php/7.0/cli/conf.d/20-xdebug.ini
zend_extension=xdebug.so
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_enable=true
#xdebug.remote_enable=false
And I had to do this: https://github.com/vim-vdebug/vdebug/issues/363
Being able to set break points in a PHP script requires some kind of debugger extension being loaded, either XDebug or Zend Debugger.
And then you need some kind of interface to this debugger to make your wishes about break points known. I am unfamiliar with the Perl debugging, but I haven't heard about any PHP CLI based debugging - it is always taking place in an IDE that is able to handle the debugging protocol of one of the extensions.
Of course there is the good old var_dump();die('hi'); debugging, but this does not include continuing code execution after the script ended. ;)

PHP cli script does not output anything

So I have a php script which I execute using the following command:
php -f my_script.php myArguments
The script is under version control using svn. I just updated it, pasted the command to run it into a terminal, and executed it. However, there is no output. Not a failure message, not it printing anything, nothing. It looks like it never starts. Kind of like the following:
me:/srv/scripts# php -f my_script.php myArguments
me:/srv/scripts#
Other scripts will run just fine.
It is difficult for me to come up with an SSCCE, as I can't really share the code that is causing this, and I haven't been able to replicate this behavior intentionally. I have, however, seen this twice now. If I save my changes, revert the file, and paste them back in, there is a strong chance it will run just fine.
However, I am concerned by not knowing what is causing this odd behavior. Is there a whitespace character or something that tells PHP not to start, or output anything?
Here is what I've tried after seeing this behavior:
Modifying the script so it is a simple echo 'hello'
Putting nonsense at the beginning of the script, so it is unparseable.
Pasting in code from a working script
Banging my head on a wall in frustration
Trying it in another terminal/putty ssh connection.
Here's where it gets interesting: It actually works in a different terminal. It does everything as expected.
So does anyone have any ideas what might be causing this, or things I should try in order to determine the problem?
EDIT:
The "different terminal" is still the terminal application, just a new one.
I have sufficient permissions to execute the file, but even if I didn't, it should spit out a message saying I don't.
I intentionally introduced syntax errors in hopes that I would get PHP to spit out a parse error. There was still no output.
display_errors might be disabled before runtime. You can turn it on manually with the -d switch:
php -d display_errors=1 -f my_script.php myArguments
I came across the same issue, and no amount of coercing PHP to display_errors or checking for syntax with -l helped
I finally solved our problem, and perhaps you can find some help with this solution
Test your script without using your php.ini:
php -n test_script.php
This will help you hone in on the real cause - PHP configuration, someone else's script, or your script
In my case, it was a problem with someone else's script which was being added via the auto_prepend_file directive in the php.ini. (Or more specifically, several files and functions later as I drilled through all the code adding debug as I went - on a side note, you may find that using fwrite(STDOUT, "debug text\n"); invaluable when trying to debug this type of issue)
Someone had added a function that was getting run through the prepend file, but had used the # symbol to suppress errors on a particular function call. (You might have a similar problem but not specifically related to the php.ini if you have any includes in your test script bringing in other code)
The function was failing and caused the silent death of PHP, nothing to do with my test script
You will find all sorts of warnings about how using the # symbol causes the exact problem I had, and perhaps you're having, http://php.net/manual/en/language.operators.errorcontrol.php.
Reproduction of similar symptoms:
Take a fully functional PHP environment, and break your CLI output by adding this the top of your script
#xxx_not_a_real_function_name_xxx();
So you may just have a problem with the php.ini, or you (or someone else) may have used # not realising the serious (and frustrating and time consuming) consequences that it causes in debugging
I experienced PHP CLI failing silently on a good script because of a memory limit issue. Try with:
php -d memory_limit=512M script.php

sublime text 2 php tidy

I installed the phptidy plugin for Sublime Text 2 and tried to use it to clean up some ugly code like
$tdt="<td class=\"tit2\" ";
$linka='<a href="products.php?action=history5&item=';
while ($row=mysql_fetch_array($r))
{ extract($row);
But after running Php Tidy, a console flashed and it seems to have edited the entire file, but nothing actually changed. Does anyone know if the plugin below still works?
https://github.com/welovewordpress/SublimePhpTidy
I ran into the same problem. Console would open and close too fast to read what was going on.
Step 1: I opened up the Sublime Console (CTRL + ` on Windows)
Step 2: Reading the output gave me the hint. PhpTidy relies on php.exe being installed on your machine (duh)
This last step might seem obvious, but I was networked into another computer where the code lived and coding it over the wire. As such, when I ran phpTidy on my local machine, it failed due to the lack of php itself.
At first you must add the <?php ?> to beginning and end of your PHP file.
If you based-on Win, see the output of PHPTidy Processes on the 'Ctrl+`' Console.
There is a tip:
PhpTidy: calling php.exe -v returned: 0.
If it returns 1, maybe 'php.exe' wasn't found, add the path of your ‘php.exe’ to ENV.

Netbeans file debugging

I have netbeans setup with xdebug so it can debug php. However, this only works if I create a php project. It will not work if I try opening a stand alone php file. So my question is, is it possible to debug a stand alone php file which is not part of a netbeans php project?
If that is not possible, how do I debug stand alone php files with netbeans?
No, There is none that I am aware of. As Myrddin mentioned the debugger needs some configurations that is a part of netbeans project.
but the best way you can debug a single file is to copy it on a project folder, and click the debug project, once the debug session is set then you can browse the PHP File that you want to debug and it will actually go through xdebug.
Good Luck!
Each project can have it's own configuration (you can have 1 project that has PHP5.4 interpreter, one the is PHP5.6, one that is a command line and another that is a web project), but if you configure a general PHP 5 Interpreter:
If you work on a windows machine you can use this code (filename is php.cmd)
set XDEBUG_CONFIG="idekey=netbeans-xdebug"
#php.exe %*
If you want to be able to debug, your interpreter should have the XDEBUG_CONFIG system variable and make sure it's connected to netbeans. You should set this to the same value in your Debbugging section of the PHP's config:
Next thing - if you right click inside the editor you will have the Debug File option, and a prompt window will pop:
You don't really need anything here. Just hit the "OK" button.
As you can see, this final result is debug session of the t1.php file within c:\TEMP\ (which is not a working project):
Short answer: CTRL + SHIFT + F5
You can find the answer here:
https://blogs.oracle.com/netbeansphp/entry/run_file_without_project
I'm not entirely sure, but I think it is not possible, because you need some configuration to get the debugging working, and this configuration is part of a project.
You can always use print_r and var_dump to debug a single file. But that is probably not the answer you're looking for.
xdebug is very heavy and old tool you can use Kint php debuger here.
its free, so you can download Here
it's pretty replacement for var_dump(), print_r() and debug_backtrace().
you need to add kint.class.php file using include or require function.
require '/kint/Kint.class.php';
that's it.
and you can use like
########## DUMP VARIABLE ###########################
Kint::dump($GLOBALS, $_SERVER); // pass any number of parameters
// or simply use d() as a shorthand:
d($_SERVER);
########## DEBUG BACKTRACE #########################
Kint::trace();
more help is available on https://github.com/raveren/kint/
Good Luck :)

Vim: undefined variables are unmarked

Lately I've been making a couple of mistakes when refactoring in Vim, the result was undefined and unused variables. Any decent IDE (like NetBeans) would mark them as such, but I've yet to come across a Vim plugin that does the same.
Can anyone help me out? I'm mainly programming in PHP.
There should be a solution with the Syntastic plugin, on which you would need to put a PHP static code analyzer like PHPLint.
However I never spent some time to test this !
Other PHP programs can be found on this SO answer.
You can run Zend's PHP code analyzer from within VIM. I currently do this. The catch is that Zend Code Analyzer is no longer packaged as a separate binary when installing Zend Studio. I'm not sure which OS you are running. I'm running on OS X. If you don't already have the binary, use steps 1 & 2 on this site to get it - http://michalf.me/blog:zend-code-analyzer-in-textmate. You may have to adjust for your OS.
After getting the binary add the following to your .vimrc and replace the /usr/local/... with the path to your ZendCodeAnalyzer.
if !exists("autocommands_loaded")
let autocommands_loaded = 1
"PHP Make
autocmd BufRead *.inc,*.php set makeprg=/usr/local/bin/ZendCodeAnalyzer\ %
autocmd BufRead *.inc,*.php set errorformat=%f(line\ %l):\ %m
endif
map <F7> :silent lmake<cr>:lwindow <cr>:redraw!<cr>
Now when you enter F7 it will run make which is set to run the ZendCodeAnalyzer. It will put the results into a location list - :help location. You can scroll through the location list and hit enter on a line and it will take you to that line in your file. If it doesn't find anything, then it won't open anything.
Well, this might not be what you are looking for, but if you must have Vim keybindings (I know I need them), then jVi brings this to NetBeans. I don't know if this is a viable option for you, but maybe this will help.
When renaming vars in a whole file type in vi cmd line:
:%s/\$oldName/\$newName/
When renaming the between line 14 and 21 (e.g inside a function) type
:14,21s/\$oldName/\$newName/
When renaming vars recursively in a directory type in vi cmd line:
:!find DIRECTORY_PATH -name "*.php" | xargs sed -ni 's/\$oldName/\$newName/'
Make a backup of the folder before to avoid headaches. ;)
I'm not sure how intelligent this plugin is but it seems to do what you want: https://github.com/vim-scripts/php_localvarcheck.vim

Categories