Debugging scripts
To debug scripts, install the Cadmatic script development environment for Notepad++
Tracing a bug
Simple scripts can be debugged by inserting U_MESSAGE calls into suitable locations in the code. However, as script grow larger and their structure becomes more complex, the proper solution is to use the script debugger.
When you know that there is a bug in your script, try to determine the last statement that you expect to provide correct results. Then insert the following function call into the code:
TRAP();
TRAP() is an intrinsic function of the script executor and is therefore always available, regardless of the application or module.
Consider the following 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 expected to display the value 0 but it displays 1.
To debug the issue, insert the statement TRAP() just before the for loop. Because the script debugger does not include a text window for viewing source code, you should keep your text editor open. It is good practice to display line numbers.
A useful improvement is to enable code tracing in your text editor. When a TRAP statement is reached, the associated editor opens and displays the active line in your script. When you click Next in the CADMATIC script debugger, the editor advances to the next line as well.
Code tracing can be enabled by setting the following environment variable:
-
PMS_TRAP_EDITOR – Specifies the executable used to start your text editor.
When the script is run, the debugger opens automatically when execution reaches the TRAP statement.
You can then set a breakpoint, for example, at line 9. When you select To break point, execution continues until that line is reached at then stops. You can now examine the value of n.
It soon becomes clear that the issue lies in the loop condition. The correct condition should be i < 3.
Debug script tool
The debugger consists of:
-
Control buttons
-
A field for entering breakpoint line numbers
-
Message fields
-
Two selection tools for variables
Control buttons
-
Run – Continues execution. The debugger window is hidden until the script terminates or another TRAP() statement is reached.
-
Step – Executes the next statement. Execution may move into another function.
-
Step over – Executes the next statement in the current function, skipping over function calls.
-
To break point – Continues execution until a defined breakpoint is reached. The breakpoint line is the next line to be executed.
-
To caller – Continues execution until control returns to the calling function.
-
Kill – Terminates execution.
Setting a break point
Enter a line number in the input field, then press Stop at to set the breakpoint. The line number is added to the list of active breakpoints.
Inspecting variables
The debugger includes two selection tools: one for local variables and one for global variables. To inspect a variable, select it from the list. The current value of the variable is displayed and updated continuously until another variable is selected.
Additional information
The debugger also shows:
-
The name of the current function.
-
The next statement to be executed.
-
The status of the last executed statement.
In the following chapters, you will learn how to use scripts and external functions in practical programming tasks. All script developers—both beginners and experienced programmers—should study these examples carefully. The examples use external script functions from different modules.