Launch XDebug in Netbeans on an external request
Asked Answered
C

4

38

I'm using Netbeans 6.7 and XDebug to debug a PHP site on my machine, launching the request from within Netbeans (Project->Debug). This works fine, and is very useful.

My question is: Is it possible to attach the debugger to any request that comes in, rather just those I launch from within Netbeans?

ie, instead of clicking "Debug", put Netbeans into a mode whereby the debugger is launched and attaches to the next request that comes in.

I have a feeling this may be a stupid question, but if it is possible, that'd be great.

Edit: A bit more information

My system (Ubuntu 9.04) is set up as follows:

Contents of /etc/php5/conf.d/xdebug.ini

zend_extension=/usr/lib/php5/20060613/xdebug.so

xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.idekey=netbeans-xdebug

Netbeans PHP debugging options are at the defaults:

Debugger Port: 9000
Session ID: netbeans-xdebug
Stop at the First Line: ticked

My /etc/hosts file redirects www.mywebsite.com to localhost

If I click on the debug button in Netbeans, then Firefox is launched with the address http://www.mywebsite.com?XDEBUG_SESSION_START=netbeans-xdebug, and the debugger works as expected.

But if I just browse to http://www.mywebsite.com?XDEBUG_SESSION_START=netbeans-xdebug, this doesn't start the debugger in Netbeans.

I've also tried setting xdebug.remote_host=www.mywebsite.com , but that makes no difference.

Also, I've enabled xdebug.remote_log, and that's showing information for when I start from within netbeans, but nothing for external requests. So I don't think XDebug is seeing the external requests at all.

Contamination answered 7/10, 2009 at 12:40 Comment(2)
note: isn't it remote_handler with r at the end?!?Guthry
For me, I didn't know that there were THREE locations where I had to specify port = 9000, and the mismatch was causing “Waiting For Connection (netbeans-xdebug)”: https://mcmap.net/q/272413/-netbeans-shows-quot-waiting-for-connection-netbeans-xdebug-quotSignificative
I
48

go to project properties > run configuration > advanced > debug url and check do not open web browser (*). do not set the host under debugger proxy. save these settings. in the project window, on your project: right mouse click > debug (this starts listening for debug connections). no browser is started. enter http://www.mywebsite.com?XDEBUG_SESSION_START=netbeans-xdebug in your browser. it should break in netbeans. at least that's what happens here :)

(*) you might also have to set a path mapping - for me, it works without

Issykkul answered 9/10, 2009 at 16:15 Comment(6)
When doing the above, untick the checkbox for "Stop at First Line" if you don't want it to stop on the top e.g., index.php file all the time as well but only directly on breakpoints in a specific file reached by a subpath e.g., 'localhost/test' -> TestController.phpFamished
If you use one of the browser plug-ins listed on the 3rd step of Starting the Debugger, you don't need to add "?XDEBUG_SESSION_START=netbeans-xdebug". Just flip it on & off with your browser.Archiphoneme
If you don't even want to add ?XDEBUG_SESSION_START=netbeans-xdebug to the request go to the @Syncopation answerVersatile
@Jaime It seems easier to me to use a browser plugin to add the XDEBUG_SESSION argument per request - see colan's comment above.Issykkul
@Jaime With xdebug.remote_autostart = 1, /every/ request to PHP will try to connect to Xdebug, which (at least in my case) often results in a stalling browser / webserver.Issykkul
It will depend on the use case, there are situations where you don't have control over the URL used to query the server, for example, when making the call from an already packaged binary you don't control, in those situations it is quite easier to go for xdebug_remote_autostart, anyway it depends on the use case.Versatile
S
23

By default xdebug will not attempt to connect to the remote host when the 'XDEBUG_SESSION_START' Cookie/URL argument wasn't specified. You can enable xdebug to be "always on", by setting the xdebug.remote_autostart to 1 in the php.ini.

Furthermore, you would have to start the PHP debugger in NetBeans for this to work. I haven't found a better solution yet, but you can have the PHP debugger listening for incoming connections by starting to debug a random file (use CTRL+SHIFT+F5 to start debugging a file) and then continuing (by pressing F5) once it stopped at the breakpoint. The Netbeans debugger should run until you actually stop it.

Update: due to NetBeans checking the session id (XDEBUG_SESSION_START), you have to set the idekey variable too. eg:

xdebug.remote_autostart = 1
xdebug.idekey = "netbeans-xdebug"
Syncopation answered 17/5, 2010 at 16:31 Comment(2)
Perfect!, all of my life I've wondering if there would be any way to debug without the need to add ?XDEBUG_SESSION_START=session_name to the HTTP requestVersatile
I never, ever, got this to work with Netbeans. I can succesfully (and only) debug with an idekey, though.Floorwalker
S
3

I had a similar problem (on NetBeans, Mac OSX), after upgrading PHP and compiling/installing xdebug. phpinfo showed xdebug as loaded, but it still wouldn't connect, and after trying everything listed above still had no success. Then I tried reducing the number of parameters set in my PHP.ini file back to the minimum required. This seemed to sort the problem out for me.

; REMOVED (commented) the following
; xdebug.remote_log=/myfile.log
;xdebug.extended_info = off
;xdebug.auto_trace=1
;xdebug.trace_output_dir=/mydir/myphptracefile.txt
;xdebug.trace_output_name=php_trace.%c
;xdebug.collect_params=4

I also renamed the default xdebug.idekey from netbeans-xdebug to default, then back again.

After restarting Apache, xdebug started working again: I'm not sure what exactly fixed it, but a good starting point might be to just start with the minimum number of xdebug settings in your php.ini file, then slowly add more if you need them. I suspect it may have had something to do with the trace settings, but can't be sure.

The basic settings I ended up using were:

xdebug.remote_enable=on
xdebug.remote_port=9000
xdebug.remote_host=localhost
xdebug.remote_handler=dbgp
xdebug.idekey=netbeans-xdebug

It's also a good idea to compile debugclient (in the xdebug source package), and check debugging on the command line as this will tell you whether xdebug can at least run independently of your IDE.

Selfemployed answered 18/7, 2014 at 9:56 Comment(0)
A
1

Not sure about Netbeans, but any other IDE I have used is always listening for a debug connection. You can start a new debug session from xdebug by appending the query string ?XDEBUG_SESSION_START=name to the url. If Netbeans is listening, this should then kick off a new debug session.

See http://xdebug.org/docs/remote#browser_session for more details

Arnitaarno answered 8/10, 2009 at 22:4 Comment(1)
Thanks for that, but it doesn't seem to work - it's like Netbeans can start the debugger itself, but it's not listening for external connections. Expanded my question a bit.Contamination

© 2022 - 2024 — McMap. All rights reserved.