Author
Topic: CGI debugging (Read 11768 times)

I am trying to debug a fcl-web CGI application with Windows 8.1 (preview) and Lazarus 1.0.10.

On http://wiki.lazarus.freepascal.org/CGI_Web_Programming is a unit shown that should enable debugging with dbg. So I created that unit and added it to the project. The resulting application is an .exe file that is called from Apache 2.2 as a CGI bin file. Without the cgidebug.pas unit my test application works fine. But when I add cgidebug then the application hangs (returns no data to the browser).

The source of cgidebug states: 'if the program to debug is running as a service (e.g. CGI application from Apache running as a service) , make sure the service is configured with "Interact with desktop" checked. Failing to do so, the debugger will be started without a console, break the program and hang waiting for input. Killing the debugger is your only option'.

Well killing the dbg process is indeed the only option. However, my app is not a service, so this should not apply ? And anyway, I have looked through all the project options, but I do not see a 'Interact with desktop' option ?

The comment says "CGI application from Apache running as a service". The important part here is "Apache running as a service". Apache will spawn your cgi application but since there is no handle to the desktop it can't pass that handle to your application. So the "Interact with desktop" should be set for Apache, not for your application.

I don't have Win8 but in older versions you would find the "Interact with desktop" checkbox in the service configuration panel. From memory: Control Panel/Administrative Tools/Services , right click on the apache service and then properties. The "Interact with desktop" is considered as "legacy" starting from Vista because of the security implications. So I'm not sure if it is still there in Win8.1 or if it is still as apparent as it was before. One more reason to stick to XP as a developer

The 'interact with desktop' option is still there. So one less reason to stick with the old stuff

However this did not solve the problem...

If I run the test project with CGIDebug included, then the browser receives no response and I have to kill the dbg process to break off the test project. So the CGIDebug unit does work in one way: it starts the dbg. But somehow the dbg still does not get through to the desktop ?

I added some code to write the resulting commandline to a file and executed that manually from the system prompt. That gave an error that indicated dbg did not know what to do with the number (the process id). So I changed "%d" to --pid=%d:AProcess.CommandLine := format('cmd /C START "Debugging %s" /WAIT "%s" "%s" --pid=%d',[paramstr(0),debugger,paramstr(0),GetProcessID]);

When you start your app from explorer or command line, the debugger should also open and break at the sleep instruction. Test this before trying it with Apache.

The process ID is the id of your application. So when you execute the commandline you saved in the file, the process is long gone.

Quote

If I run the test project with CGIDebug included, then the browser receives no response and I have to kill the dbg process to break off the test project. So the CGIDebug unit does work in one way: it starts the dbg. But somehow the dbg still does not get through to the desktop ?

I did realize the pid is only valid while running, so I kept the process running. But seems that was not enough, because...

Good remark about running from the commandline: indeed then the dbg is correctly shown in a separate window. And it connects correctly to the cgi app. Great !

Your other links gave a lot of less cheerful info:Windows also has an overall 'interact with desktop' - setting:http://msdn.microsoft.com/en-us/library/windows/desktop/ms683502(v=vs.85).aspxIn Win 8.x this option defaults to being off, so I set it on.But now all applications running in 'session id 0' mode cannot not ever be interactive in any way. An improvement over the early days, according to Microsoft... (so much for running at the front of the Windows pack with 8.x....)I checked (run 'tasklist' at dos prompt) and indeed apache, cgitest.exe and dbg.exe all run in session id 0 - mode :-( Meaning what I want is impossible.

So my conclusion, its not possible anymore to use dbg for this way of debugging for Windows cgi executables.

I did realize the pid is only valid while running, so I kept the process running. But seems that was not enough, because...

Because you can have only one debugger attaching to a process. Remember, your process hung because it stopped at a breakpoint set by the debugger launched by itself.

Quote

So my conclusion, its not possible anymore to use dbg for this way of debugging for Windows cgi executables.

A clumsy alternative is that you write the full gdb command line to a log file like you did before. Then start a while fileexists(logfile) loop with a short sleep. From the command line, as soon as you see the file appear, run the command line stored in the log file to attach to the process. There is no hurry, the cgi app will wait as long as needed. When the debugger is attached, delete the logfile in explorer or another console, to get out of your cgi loop.

I am trying to debug a fcl-web CGI application with Windows 8.1 (preview) and Lazarus 1.0.10.

There are a few options...1) Use the 'dbugintf' unit inside your CGI app. Then use the SendDebug() procedure to send debug information. Use the console (included with FPC) or GUI (included with Lazarus) Debug Server applications to listen to the debug messages.

2) Use tiOPF's tiLog unit. You can log to a file and monitor the output.

3) Do what the Delphi developers do. Debug within the IDE, using GDB. Create a infinite loop at the beginning of your CGI app as follows:

Execute the CGI app via your web browser. Now use Lazarus (actually GDB) to attach to the process ID. This will automatically pause your CGI application. For some reason GDB switches to other threads if they exist. Make sure you are debugging Thread 1. Set a breakpoint on the 'while ... do' line. Run your app again via Lazarus IDE. Now modify the lDebug value to 0, or change the EIP register to jump over the while statement. Then continue to step your CGI application like any other GUI application.

I am trying to debug a fcl-web CGI application with Windows 8.1 (preview) and Lazarus 1.0.10.

There are a few options...1) Use the 'dbugintf' unit inside your CGI app. Then use the SendDebug() procedure to send debug information. Use the console (included with FPC) or GUI (included with Lazarus) Debug Server applications to listen to the debug messages.

2) Use tiOPF's tiLog unit. You can log to a file and monitor the output.

3) Do what the Delphi developers do. Debug within the IDE, using GDB. Create a infinite loop at the beginning of your CGI app as follows:

Execute the CGI app via your web browser. Now use Lazarus (actually GDB) to attach to the process ID. This will automatically pause your CGI application. For some reason GDB switches to other threads if they exist. Make sure you are debugging Thread 1. Set a breakpoint on the 'while ... do' line. Run your app again via Lazarus IDE. Now modify the lDebug value to 0, or change the EIP register to jump over the while statement. Then continue to step your CGI application like any other GUI application.

Hope this helps.

Great Job, on Linux the only detail is that you have to start Lazarus as root...