How to get a stack-trace via api-call in xdebug? - php

We are using xdebug and the tracing works as advertised during code execution via..
function someGetUsersHelper() {
xdebug_start_trace();
[SOME CODE HERE]
xdebug_stop_trace();
}
It however only traces the wrapped part of the code if the page/script is executed via loading the full page in the browser via requesting
index.php
But tracing does not work, when we call the function as an api call via
index.php?api=getUsers
Even though the very same function is executed successfully it does not trace.
As an additional note: Tracing also works if the function is called from phpunit testcases or if we set xdebug.auto_trace = 1 in the php.ini, but that leaves us with a very long, messed up trace file.
So the question is this:
What might be the issue/reason for this and how can we manage to get the
desired clear and succinct trace via api call?
Thanks a lot!!

I could not say why the trace is not working. But you could try this:
Add the option xdebug.trace_enable_trigger=1 in your php configuration
Now you could start a xdebug by adding XDEBUG_TRACE as POST/GET Parameter or as a cookie
There is also a Firefox Plugin called Easy XDebug to set the flags

Related

PHP SoapClient call generates an ERR_NO_RESPONSE in browser and then a page reload

I am using an external SOAP API (Interworx API - http://docs.interworx.com/interworx/api/index-Using-the-API.php#toc-Chapter-2) and when the SOAP call executes successfully I receive an 'ERR_NO_RESPONSE' or 'Unable to connect' or similar message in my browser (Chrome or Safari or Firefox). When the SOAP command does not succeed, the page works fine and I receive the appropriate return array in $result.
The call is:
$client = new SoapClient($interworx_soap_url);
$result = $client->route($api_key, $api_controller, $action, $input);
The $client->route() command is creating a new domain pointer in my control panel.
Interestingly, the API call always works, but somehow it is interrupting my code execution. I have tried adding an ob_start() and ob_clean() around these lines but it makes no difference.
So when my command $client->route() is successful (it creates a new domain pointer), it causes the problem (ERR_NO_RESPONSE in browser) but when the command is not successful (like when the domain pointer already exists) it works fine (returns the status array with the error message).
Any help solving or isolating the problem would be greatly appreciated. Incidentally, this problem does not generate any messages in my PHP or server Apache error log. Thanks!
OK, I found my problem and it was not exactly a coding issue. When I called the $client->route() command and a new pointer domain was created, the API also restarted Apache to make the new Server Alias live. This was killing my script at exactly that point! It turns out that there is a setting in Interworx that lets me force the restart to be "graceful" and this solved my problem. The code was all good. Thanks

xdebug with qcachegrind reports "There is no source available for the following function ..."

I am starting to use xdebug in combination with qcachegrind and first tests work well. The log file gets recorded and I can open it with qcachegrind. But now I have the issue that I dot't get detailed information for every function. For php::exec_curl I only get the information
"There is no source
available for the following function: php::curl_exec. This is because
its source file cannot be found: php:internal. Add the folder of this
file to the source folder list. This list can be found in the
configuration dialog.
I am using MAMP (/Applications/MAMP/bin/php/php5.6.2/bin/php) on OSX. My first try was to add simply the whole MAMP folder to the source list, but that did not work.
Now I wanted to ask if anybody maybe already had the same challenge and knows how to solve it?
exec_curl is an internal PHP function, meaning that it's been implemented in C and QCacheGrind doesn't have access to it's source nor to any trace of what that function did. For XDebug/PHP/QCacheGrind this function is just a black box, that takes some parameters and returns some input.
Why would you need to see the source? Frankly, you shouldn't care about what happens inside. All internal functions have been tested and proven to work.

More indepth info while debugging with Xdebug in PHPStorm

I am trying to analyze what's what in my Yii application by using XDebug paired with PHPStorm. In the "Debug" tool window I can see function calls and the variable list associated with a function call.
There are setting in php.ini that allow xdebug to also collect parameters passed to functions, function return values and also variables that were modified by a particular function.
Is there a way to see all of that in PHPStorm?
The "collect_params" and "collect_returns" extra php.ini settings are for function and stack traces. Xdebug doesn't provide information on which variables where modified by which function. There is information on how to turn on tracing at http://xdebug.org/docs/execution_trace
In PHP Storm, you can easily see the arguments passed to a function by setting a breakpoint on the first line of the function, and inspecting the incoming values. You can also monitor the return value, by setting up a breakpoint at the return statements of the functions that you are interested in. PHP Storm does not allow you to set a break point on a function's entry point and/or exit point - although Xdebug's DBGp protocol does support that: http://xdebug.org/docs-dbgp.php#breakpoints You could file a feature request at https://youtrack.jetbrains.com/dashboard if you want.

json_decode doesn't work in local server

I just installed my first LAMP and NetBean in Ubuntu. I imported a small website and tried to run it both locally and remotely, but the pages that use json_decode (and other functions) don't work on the local server.
For example this page:
<?php
echo "X ";
var_dump(json_decode("{}"));
echo " Y";
?>
On the managed server shows X object(stdClass)#1 (0) { } Y, but on the local server shows X only.
I ctrl+clicked on the json_decode function name, and the file json.php opened with a json_decode function with the empty body. I don't understand if that's just the definition of an interface or the real function (I'm learning PHP), but the fact that the rendering stops there, without error messages, sounds weird.
UPDATE:
I uninstalled all the packages listed at the "Starting over" section in this page, and re-installed with sudo tasksel install lamp-server.
The next day there was a massive automatic update. I don't know if there is a relation between the installation and the update, or if the update would have come anyway.
Right now I'm still at the same position: some pages are rendered correctly locally, but my website uses json on almost every page, so I can't use the local server. (I was hoping to be able to use it in the next days, when I will be visiting the in-laws, where Internet is still an unknown concept.)
UPDATE 2:
The fact mentioned earlier that json_decode() has an empty body is not a problem. I just right clicked on session_start();, and the file session.php opened with the function defined as function session_start () {}. I don't understand why the library functions contain empty bodies, but session_start() works, so json_decode() should as well.
The fact that you are not seeing the "Y" would probably indicate a fatal error occuring during your json_decode (you could use ini_set("display_errors",1); at the top of your script to be sure)
The fatal would probably be something like:
PHP Fatal error: Call to undefined function json_decode()
Do some googling on that error. This post may help, if I am right about the fatal error:
PHP Fatal error: Call to undefined function json_decode()
Try installing the json extension separately. I think it's called php-services-json on Ubuntu, so run apt-get install php-services-json
Try not to use LAMP and install apache2 manually try if that works, maybe the LAMP package is causing you trouble. That is if you're not afraid of some extra work.
You could also check your configs if they forbid anything.

PHP Header("Location:...") works on one computer and not another

So I just got a nasty surprise when I deployed some code I thought I'd tested. It would seem there must be some difference between my test machine and my server. The exact same code, featuring a header redirect, worked perfectly on my test machine and not at all on the server. The redirect on the server simply didn't happen, leaving a blank page as a result.
The header is called somewhere in the middle of the script - but nothing will have been output yet. It doesn't output anything until the very end of the script. Long after everything else is run. It buffers everything.
Both server and test machine are running the same PhP version, the same Apache version. Is there something in the configuration files that would allow the header to happen for one and not in the other? Is there something else going on here that would cause it to fail?
EDIT:
Here's the line that sets the header:
public function setRedirect($url) {
header('Location: '.$url);
}
And here's the code that calls that:
$url = new URL('index');
$this->layout->setRedirect($url->toString());
Where URL::toString() always generates a fully qualified domain name, in this case: http://domain/index.php?action=index
I checked both Php and Apache error logs. Nada.
Probably there was some whitespace or other form of output before the header call.
This is only work if you the ini setting output-buffering is on (or if you explicitly start output buffering, but in that case, the redirect should work in both computers).
You can confirm this by turning on error reporting.
Use Fiddler or some other client-side tool to check your headers. Determine that the Location: header is actually being sent. Also, some browsers are picky in the order that headers need to be sent.
I think the most likely explanation is that an error is causing the script to exit on your server, and you have display errors turned off (hence the blank screen). I would suggest checking the Apache error long on your server to see if PHP is putting something in there.
Otherwise you could use a browser extension like LiveHTTPHeaders (for Firefox) to see if the location header is being sent at all, or try debugging the script to see if it's even getting as far as that header call.
I think your server puts some script in your pages to track visitors and give you traffic stats or for a similar purpose. Ideally, you should get an error for this but may be your server has error reporting disabled which gives you a blank page.
I suggest you to run a script with a syntax error and check weather your server has error reporting disabled.

Categories