|Liberty BASIC Help Online
Using the Debugger
HOW THE DEBUGGER HELPS
You can use the debugger when a program doesn't behave the way you expect, or when there are errors that prevent it from running. You can watch as each line of code is executed to see if the variables contain the correct values.
The TRACE command is used in conjunction with the debugger. It allows you to mark places in code that will cause the debugger to change modes between "step", "animate" and "run." This allows you to use the "run" button to debug a program, and when it hits a "TRACE 2" command in the code, it will automatically drop down into "step" mode.
USING THE DEBUGGER
You can write a short program that shows how to use the debugger. In the Liberty BASIC editor, click on the "File" menu and select "New File." You'll see:
Type in the following simple program:
for x = 1 to 1000
Now click on the debug button. The program runs and a special "Debugging" window also appears. Position the two windows so that you can see them both. Now focus on the debugger:
Notice the pane on the top. This lists your program's variables. There's a lot of information in there already because each program comes with some special variables already declared. Scroll down to the bottom of the list where the variable named x is located. Focus on this variable for this example:
There are buttons for the possible debugging modes:
-Resume runs your program at full speed in the debugger. While in this mode, you won't see variables change or program source code highlighted.
-Stop will cause your program to stop, and it will highlight the line where it stopped, and it will show the current variable contents
-Step Into will execute the next line of code. If the next line is inside a subroutine or function it will follow execution into the subroutine or function.
-Step Over will execute the next line of code. It will not step into subroutines or functions, but skips over them.
-Step Out will run until the current subroutine or function exits, and then stops to show the next line of code and variables.
-Animate runs your program, showing each line as it executes, and also updating variables as it runs.
Click on the "Step Into" button. You'll see that the first line of code is executed. The the variable x is no longer 0, but 1. Also the next line of code is highlighted:
Now click on the "Step Into" button again. Notice that the value of x gets printed to the program window, and also the next line of code is highlighted in the debugger:
Click a few more times on the "Step Into" button until the value of x is 3 or maybe 4. The "Step Into" button must be pressed each time you want the program to execute a new line of code. Now click on the "Animate" button. This mode causes the program to execute, while still documenting the current line of code and the values of variables in the debugging window. This time you'll see the program running really fast, and printing lots of numbers. Quickly press the "Step Into" button again to stop the program. The program will stop executing and you can see the current values for variables and the line that is to be executed next:
Press the "Resume" button but get ready to press the "Step Into" button again. "Resume" mode executes the code at normal speed and it doesn't document anything in the panes of the debugging window. The numbers will start printing really quickly in the program window, but the debugger doesn't show any activity at all. You should still be able to click on the "Step Into" button again before the count reaches 1000. The debugger again shows you the current state of the program.
Close the debugger. This will also close the program window.
EXECUTING CODE AGAINST A RUNNING PROGRAM
The combobox in the center of the debug window allows you to type commands to your program while it is being run for debug. Clicking the "Execute" button will cause the code you have typed to be executed. In the example above, you might want to change the value of "x". Type into the code combobox:
x = 50
Start stepping though the code by clicking the Step Into button several times, then click the "Execute" button. Click the "Step Into" button again. See what happens to the printout in the mainwindow. It looks like this:
The combobox contains a list of all code that you've typed into it during the course of debugging. To go back to a previous line of code, simply choose it from the drop-down list of the combobox and then click the "Execute" button.
Liberty BASIC 4 adds the ability to add a breakpoint to your program so that when you are debugging the program will stop at the breakpoint when it reaches it. To add a breakpoint, point with your mouse at the breakpoint bar on the left side of the editor and double click. A breakpoint marker will appear. If you double-click there again, the breakpoint will disappear. Add as many breakpoints as you like, but keep in mind that adding a breakpoint to a blank line or a remark will not have any effect during debugging. You can also right click on the breakpoint bar for a little popup menu.
When you launch the debugger it also has a breakpoint bar which will contain all the breakpoints you added in the editor. You can add and remove breakpoints in the debugger. Note: Changes you make to the breakpoints in the debugger are not automatically made to the breakpoint bar in the editor.
Breakpoint in the Debugger
You can also click on a line of code in the debugging window to set the cursor, right-click to pop up a menu and then select "Run to Cursor" to make your program run at full speed until it reaches that line of code, just as if you added a breakpoint to that line. This method functions as a "just once" breakpoint.
Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/