Debugging Scripts

Before writing scripts of your own, make sure that your web server is correctly configured to run the Gsharp Web Edition and that the standard examples are working.

If the examples are running you are now ready to debug your own scripts.

Debugging Your Own Scripts

Problem
Suggestion
Nothing works Make sure that the examples are working. Copy an example similar to your own script and then slowly modify it to match your script until you find the error.
No image appears

View the source of your HTML page to see the name that the browser is expecting. It will look something like:

 <img src=/GsharpTmpFiles/plot.gif>

Try viewing this image directly with your browser e.g. http://yourwebserver/GsharpTmpFiles/plot.gif.

Check the actual directory to make sure that the file actually exists. You can make sure the actual file and the reference to it are correct by using code like this to create the file:

imgfile = "start"+getpid()+".gif";
make_image(700, 500,
          getenv("GsharpTmpFilesActualDir")+imgfile); 

And code like this to make the reference:

HTMLimage(stdout,
          getenv("GsharpTmpFilesWebDir")+imgfile);

You must also make sure that the image is created before it is referenced in your HTML as some browser will request an image as soon as it sees a reference to it.

The image never changes

Make sure the physical image is changing by viewing the image outside of your browser.

If it is not changing check the date of the file to make sure it is being updated. If it is being updated then add some debug statements to check that the FORM input is being correctly handled.

If the image is changing, but you don't see any changes in the browser, this is probably because the image is being created with the same name each time and the image is being cached. Use a function like getpid() to make sure that every different image has a unique file name.

Server Error

The script is not even creating the correct headers. This could be because:

  • You don't start your script with HTTPheader() or echo("Content-type: text/html"//"");
  • There is a syntax error in your GSL - try running it from the command line.
  • You are mixing your GSL code with calls to system. Some web servers cannot capture output in the order is created. The output of your system calls appears first and the HTTP header is no longer the first text encountered.
The script runs with default values, but not with the real ones.

Use Handling Form Data - Method 2 to capture the real values within the browser and then test them from the command line

The image appears, but is not quite what I expect Run your script with the Gsharp Professional Edition so that you can see exactly what is going on.

Testing From the Command Line

When running GSL scripts in cgi-bin, your browser may suppress any error messages, making it difficult for you to determine what went wrong. You may also see a message like "Document contains no data". To view these important messages you must run your scripts from the command line. For example:
Windows
UNIX
cd c:\gsharp\example\cgi-bin
c:\gsharp\bin\gsharpwe <your script>.gsw  
cd /WWW/cgi-bin
./<your script>.gsw                 

This will run your script in exactly the same way as it is run by the CGI on the web server. On Windows you could also double click on the file in the explorer, but the output will probably disappear before you can read it.

Windows only. If you find yourself testing from the command line a lot - then it probably makes sense to add the GsharpWE bin directory to your path so that you can just type "GsharpWE myscript.gsw".

N.B. Before testing your own scripts, make sure that you can run the example script $UNIDIR/example/cgi-bin/gs_gallery.gsw. If this script works in the browser, but not from the command line you will need to make sure the environment variables set up for Gsharp by the browser (UNIDIR, LM_LICENSE_FILE, etc) are also set up in your command window.

Debugging Scripts with the Gsharp Professional Edition

The Gsharp Professional Edition can be used to create your templates, but it can also be used to run your finished Gsharp Web Edition scripts. The advantage of running your scripts through the professional edition is that you can see exactly what is going on, see the error messages and explore the values in datasets.

If your script references functions found only in GsharpWE, such as bounding_polygon() or the Java driver, then you can still test the rest of the script by making references to Gsharp Web Edition functions conditional. This can be done using the webedition() function:

  if webedition() then
    npoly = bounding_polygon(nvert, x, y);
  endif

Handling Form Data - Method 1

If you are writing a script than takes input from a form, then you should write it so that your variables take a default value if the form variable is not defined. This makes it possible to run the script from the command line or from within Gsharp Professional Edition. For example:
  text = "Hello Wide World";
  if exists(WORK.FORM_text) text = WORK.FORM_text;
  page_1.view1.title.XuNtitleText = text;

In this example the form data is used if present, but the script will run even if it is not specified or if debugging from the command line.

A convenience function, HTMLget_value(), performs the steps above. This function is included in $UNIDIR/lib/libhtml.gsl. It can be used return a form value, if given, or a default value:

  HTMLget_value("text","Hello Wide World");

If your script only fails when a variable only has a certain value, HTMLget_value can be used to initialize a variable to the problem value.

Handling Form Data - Method 2

The second way of handling form data is to run your script from within the browser once and save all the form fields in a folder. Then run your script on the command line or from within Gsharp and get your form fields from the folder. To test a different set of form inputs just return to your form, enter the values, click submit to run your script again. The values of the form are saved again in a folder and will be used when you run Gsharp.

The following code can be used:

if webedition() then
  export_folder("c:\tmp\debug.fol");
else
  import_folder("c:\tmp\debug.fol");
endif

The convenience function HTMLdebugform() in $UNIDIR/lib/libhtml.gsl will do the above for you. Just add it to the top of your script. e.g.

include "$UNIDIR/lib/libhtml.gsl";
HTMLdebugform("c:\tmp\script2.fol");