Debugging Scripts

To debug scripts, use either Notepad++ or UltraEdit. Cadmatic provides additional tools for these text editors, for example for syntax highlighting and easy selection of functions with arguments.

Tracing a Bug

Simple scripts can easily be debugged by inserting U_MESSAGE messages into suitable locations in the code. When the script becomes bigger and its structure more complicated the only proper solution is to use the script debugger.

When you know that there is a bug in your script try to figure out what is the last "safe" statement you expect to be correct. Then insert a function call into the script code:

TRAP();

TRAP is an intrinsic function to the script executor and thus always loaded, independent of application and module.

Let us have an example:

#include include/dmutil.h
main()
{
	n = 3;
	for(i=0; i <= n;i=i+1;){
		n = n-1;
	}
	U_MESSAGE(ITOASCII(n));
}

This code is supposed to prompt the number 0 but it prompts 1.

We insert the statement TRAP just before the for loop. Since the script debugger does not include a text window showing the source code we must have our text editor open. It is a good practice to have the row numbers visible.

A good way to ease debugger's life is to enable code tracing functionality in your text editor. This means that when the TRAP point is reached the associated text editor will start and show the active line of your script code. To proceed, you click for example Next in the CADMATIC script debugger and the text editor will move by one row too.

Code tracing functionality can be enabled by setting a couple of environment variables:

PMS_TRAP_EDITOR. Here you just write the executable's name that starts your text editor.

And when we run the script the script debugger tool is opened automatically when row number six is executed.

Now we can set the break point at row number 9. When we press the button "To Breakpoint" the script is run until row number 9 is encountered and the execution is halted. Now we can query the value of n.

Soon it is obvious that the bug is in the condition statement which is supposed to terminate the for loop. The logical expression needs to be changed to: i < 3.

Debug Script Tool

The tool consists of a set of control buttons, a fill-in field for entering the break point row number, message fields, and two selection tools for variables.

Control buttons

Run

Execution is continued. The tool disappears from the screen until the script terminates or TRAP() is encountered.

Step

Execution of the script is continued for one statement. This can be in another function.

Step over

Execution is continued with one statement in the current function i.e. steps over a call to another function.

To break point

Execution is continued until a set break point is encountered. The row on which the break point is located is the next row to execute.

To caller

Execution is continued to the calling function.

Kill

Execution is terminated.

Setting a break point

The user enters a row number to the fill-in field. Then the user sets the break point by pressing the button "Stop at". The row number appears to the list of currently active break points.

Showing contents of a variable

There are two selection tools in the debugger, one for local and one for global variables. The contents of a variable is easily examined by just selecting the variable. The tool shows constantly the current value of the selected variable until some other variable is selected.

Other information

Also the name of the current function, the next command to be executed, and the status of the last executed command are shown.

In the following chapters we will show how to use scripts and external functions in practical programming tasks. All script programmers should read these examples with care, both new programmers and experienced coders. In the examples we use the external script functions of different modules. See the reference manuals of each external set for details.