[SparForte][Banner]
[Top Main Menu] Intro | Tutorials | Reference | Packages | Examples | Contributors   [Back Page]      [Next Page]  

Debugging Tutorial - Using the SparForte Debugger

This is a detailed look at SparForte's debugger (the breakout mode), and related features.

Syntax Checking

A script is run by using the script name as an argument to SparForte. SparForte performs a syntax check before running the script. You can run just the syntax check with the --check option.

bash$ spar --check demo_script.sp

The Trace Command

The execution of a script can be traced with the SparForte trace command.  Suppose you want to trace the execution of the following script:

#!/usr/local/bin/spar
 
trace true;
 
procedure main is
  type paint is ( black, white, blue );
begin
  for p in black..blue loop
      put_line( p );
  end loop;
end main;
 

Example: Using the trace command in a script

Once tracing is turned on by trace true (or with spar --trace), each line will be shown before it's executed, with the line number in square brackets to the right of the line.  Some statements will also show additional information.  For example, an assignment statement will show the value being assigned.  The trace information is written to standard error, so the output from the command may appear in the wrong order because some versions of UNIX buffer their output.

bash$ spar trace_demo.sp
Trace is on
=> " " [ 4]
=> "procedure main is" [ 5]
=> "  type paint is ( black, white, blue );" [ 6]
=> "begin" [ 7]
=> "  for p in black..blue loop" [ 8]
=> "    put_line( p );" [ 9]
=> (p := ' 0')
black
=> "  end loop;" [ 10]
=> "  for p in black..blue loop" [ 8]
=> "    put_line( p );" [ 9]
=> (p := ' 1')
white
=> "  end loop;" [ 10]
=> "  for p in black..blue loop" [ 8]
=> "    put_line( p );" [ 9]
=> (p := ' 2')
blue
=> "  end loop;" [ 10]
=> "  for p in black..blue loop" [ 8]
=> "    put_line( p );" [ 9]
=> "  end loop;" [ 10]
=> (There are 0 array elements declared by all arrays)
=> "end main;" [ 11]
=> (There are 0 array elements declared by all arrays)
=> "End of File" [ 12]
=> (Script exit status is 0)
 

Example: Example Output from the trace command

Breakout Mode: The Debugger

If a script is started with the --break (or -b) option, a SIGINT (e.g. a control-c) will pause the script and give the user a command prompt. This breakout prompt is identical to a normal interactive session except for the command to quit. return will retry the last statement. step will run the next statement and then stop. logout will terminate the script.

The Breakout Prompt

Without a --break, a SIGINT will terminate the script, just as if a return command was executed.

bash$ cat breakdemo.sp
for i in 1..10000 loop
   put_line( i );
   delay 1;
end loop;
bash$ spar breakdemo.sp
1
2
3
$
 

Example: Control-C Used on a Script without --break

Since --break was not used, the script terminated.

bash$ cat breakdemo.sp
for i in 1..10000 loop
   put_line( i );
   delay 1;
end loop;
bash$ spar --break breakdemo.sp
1
2
3
breakdemo.sp: 4: 1: for loop
  put_line( i );
^ Break: return to continue, logout to quit
=>
 

Example: Breakout Prompt Appears when Control-C is used on a Script with --break

Since --break was used, SparForte breaks out of the script and provides an interactive prompt.

=> ? i
4
=> i := 9999
=> (Warning: assigning a new value to a constant)
=> return
=> (returning to script)
9.99900000000000E+03
10000
$
 

Example: How to Change a Variable and Resume a Script from the Breakout Prompt

Breakout Pragmas

To monitor variables, use pragma inspect. Each time the script stops because of a Control-C, the value of all the variables marked for inspection will be shown. You can also use pragma inspect at the breakout prompt to start inspecting a variable, or pragma uninspect to stop inspecting a variable.

To break at a specific point in a script without using Control-C, use pragma inspection_point. When this pragma is reached, SparForte will stop as if you typed a Control-C. That is, it marks a "break point" in your program. All variables selected with pragma inspect will be displayed. Use pragma inspection_peek to see the inspected variables without stopping the program.

Study Questions

  1. Why is detecting errors in an early in the development cycle valuable?
  2. How can creating your own types with the "type" statement reveal design errors?
  3. Why does "pragma debug" run commands in restricted shell mode?
 
[Right Submenu]

 Command Prompt Tutorial 1: SparForte as a Calculator

 Command Prompt Tutorial 2: Basic Shell Commands

 Command Prompt Tutorial 3: Working with Databases

 Script Tutorial 1: Basic Commands Scripts

 Script Tutorial 2: Intermediate Program Scripts

 Script Tutorial 3: Data Types

 Template Tutorial 1: Basic Templates

 Template Tutorial 2: Intermediate Templates

 GCC Tutorial: Compiling SparForte Scripts

 Debugging Tutorial - Using the SparForte Debugger

 Creating a Profile Script

 Calling SparForte from C: A Tutorial

 SparForte For PHP Developers

 SparForte Best Practices

[Back to Top] Back To Top [Small Forte Symbol]