Thursday, October 21, 2010

 

Web 2.0 programming with Object Pascal (Part 3)


Debugging CGI applications

If you build web applications, surely often you'll face the need of a way to debug them. Remember that CGI or FastCGI applications run inside the Web Server, and you can't debug them by placing breakpoints in your Lazarus code.

The easiest way I found to debug web applications is by using the "dbugintf" unit, provided by FreePascal. This unit, according to the FCL documentation can be used "..to add debug messages to your application. The messages are not sent to standard output, but are sent to a debug server process which collects messages from various clients and displays them somehow on screen", both, FreePascal and Lazarus include example debug servers to cath messages generated in apps.

How to use dbugintf unit

This is easy, just add dbugintf to the "uses" of the units you want to debug, then use SendDebug to send messages to the debug server.

Example:

Using Lazarus, go to Project -> New Project -> CGI Application to create a new CGI application, then go to the FPWebModule1 automatically created by Lazarus, and then to the Object Inspector, select Events and double click on the "OnRequest" event handler to create the skeleton for this handler, then adapt it to look like this:


unit Unit1;

{$mode objfpc}{$H+}

interface

uses
dbugintf,
Classes, SysUtils, FileUtil, HTTPDefs, websession, fpHTTP, fpWeb;

type

{ TFPWebModule1 }

TFPWebModule1 = class(TFPWebModule)
procedure DataModuleRequest(Sender: TObject; ARequest: TRequest;
AResponse: TResponse; var Handled: Boolean);
private
{ private declarations }
public
{ public declarations }
end;

var
FPWebModule1: TFPWebModule1;

implementation

{$R *.lfm}

{ TFPWebModule1 }

procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;
AResponse: TResponse; var Handled: Boolean);
begin
AResponse.Content := 'Hello!';
SendDebug('--->Debug this!<---');
Handled := True;
end;

initialization
RegisterHTTPModule('TFPWebModule1', TFPWebModule1);
end.


Then compile the program, and copy it to your web server's cgi-bin directory and test using your web browser. If everything is ok, you'll note just a web page containing the text "Hello!".

Introducing debugserver

As I explained at the beginning of this article, to catch the debugging messages you need to use a debug server. In the sources of Lazarus you can find a tool called "debugserver" placed in lazarus-sources/tools/debugserver, open it with Lazarus and compile, then Run.

In Unix/Linux, After running the debugserver, you'll note it creates the file /tmp/debugserver. In my case, that file is created with 600 permissions, that is, read only. To let the cgi program write to that file, just do this:

sudo chmod 666 /tmp/debugserver


Now you can try again your program using the web browser, and look at the debugserver screen, it should add the message "--->Debug this!<---" to the messages list.

That's all, hopefully you find this interesting.
Comments:
Hello Leonardo

In IE7, if the browser window isn't wide enough, your page content (not the blog post title, but the actual text) drops all the way down. In Firefox it works fine.
 
Hmmm? Lazarus não pode fazer Run/Attach Process para debugar um executável/dll?
De qualquer modo, esse tipo de unit tem para o Delphi (vem uma com o GExperts) e tem diversos applicativos que interceptam as mensagens da API SendDebugString (ou coisa parecida) como por exemplo o DebugView da Microsoft Technet ou o próprio applicativo do Gexperts.
 
Fabricio, I remember attaching Apache from Delphi, but that doesn't works as smoothly as this.

BTW, Lazarus can attach to process.
 
Hi, have you ever considered WebDesign package? It allows you to change between CGI, FastCGI and embedded webserver through a simple define (which actually change one used unit) in the lpr. Using the embedded webserver, I can single step the web application from IDE. Wonderful...
 
Yes Leledumbo, a long time ago, I've tried WebDesign, but it had some issues while debugging web applications with database access. Maybe those problems were fixed now.
 
Hola Leonardo, en este link (http://www.hu.freepascal.org/lists/fpc-pascal/2007-March/013448.html) hay referencia a un web server embebido pero el link esta roto. Tenes info del tema? gracias
 
La verdad no tengo idea donde conseguir el nYume, igual yo lo probé hace un tiempo y lo desestimé porque sólo corria en FreePascal.

Personalmente uso Synapse (http://synapse.ararat.cz/), ahi podés encontrar un ejemplo de servidor HTTP que podés tomar de base para tu propio servidor.
 
Hello

I followed your article, but i didnt receive the debug mesages in debugserver.
I tried with, IE8 and Firefox.

Im using FPC 2.5.1 and Lazarus 0.9.31 - 28871

Thanks
 
Rafael, I didn't tested debugserver on Windows.

You could try asking in the Lazarus mailing list.
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?