Ide/Debugger

From wiki.visual-prolog.com
IDE

Visual Prolog Debugger

Visual Prolog Debugger is a powerful tool for finding errors in programs. Sometimes correcting errors in programs takes about 90% of the total development time. This proves that good tools for debugging are very important.

Visual Prolog Debugger can debug MS Windows 32/64-bit Graphical User Interface and Console applications and DLLs.

Visual Prolog Debugger provides ability to trace program execution. In the debugger one can set breakpoints, step through the source code (with viewing traced code both on Prolog and disassembler levels). The debugger allows inspecting variables used in the currently traced clause, viewing and retracting facts, viewing memory dumps, viewing the stack of predicate calls, viewing breakpoints inserted into the program, viewing registers, etc.

Visual Prolog Debugger is the IDE tool, which can be run with the Debug | Run menu command, with the F5 hot key, or with the Ide db Run.png toolbar button.

Generation of Debugging Information

Generation of the information for Visual Prolog Debugger is controlled by copmiler's option [..\Command_Line_Tools\Command_Line_.htm#DEBug /debug:full]. By default, the Visual Prolog IDE always generates debugging information while compiling project modules.

Starting Visual Prolog Debugger

When you start Visual Prolog Debugger (with the Debug | Run menu command, or with the F5 hot key, or with the Ide db Run.png toolbar button), then the IDE builds (if it is needed) the project, runs the target executable under the debugger, and breaks the program execution at the entry point of the goal section:

Ide db Start.png

If in the Run Arguments edit control (in the Build Options tab of the Project Settings dialog) you have specified a string with command line arguments, then this string is passed to the program.

Debug Menu Commands

The Debug menu is used to control execution of the program being debugged.

The Debug Menu
  • Run (F5)
    This command continues running the application. Issuing this command, when the IDE does not debug an application, builds (if needed) the application and starts its execution under the debugger. This command is often used in conjunction with setting of breakpoints.
  • Run Skipping Soft Breakpoints (Ctrl+Shift+F10)
    This command continues running the application without breaks at Soft Brakpoint (see [Breakpoints_Window.htm Breakpoints Window] for more details).
  • Stop Debugging (Shift+F5)
    This command stops the program execution. The IDE becomes active in the ordinary mode.
  • Break Program
    When a program is executed (the program instructions are executed and the debugger is waiting till the program stops and returns control to the debugger), then with this command it is possible to break the program execution and to return control to the debugger. Then it is possible to inspect the call stack to see what the program is actually doing or to set a breakpoint where to catch the execution. Notice that this command is disabled while the debugger is active (when the program instructions are not executed).
  • Restart
    This command restarts the application execution under the debugger.
  • Attach process...
    It commands the debugger to attach to a process.
  • Step Over (F10)
    It instructs the debugger to execute the next line of code. If the line contains a predicate call, Step Over executes the entire predicate, then halts at the first line outside the predicate.

    If this command is used, then when you reach a predicate call, the predicate is executed without stepping through the predicate clauses (without entering into source code of the predicate).

    Use Step Over if you want to avoid stepping into predicates.
  • Step Into (F11)
    Step Into and Step Over differ in only one aspect — how they handle predicate calls.
    Step Into instructs the debugger to execute the next line of code.
    If the line contains a predicate call, Step Into executes only the call itself, then halts at the first line of code inside the predicate.

    Step Into if you want entering into clauses of each called predicate.
  • Step out of (Shift+F11)
    This command accomplishes execution of clauses of the currently executing predicate and calls the next predicate.
  • Run to Cursor (CTRL+F10)
    It commands the debugger to run the application until it reaches the location where the cursor is set. This location can be both in Visual Prolog source code or in Disassembly window.

    Place the cursor on a line and activate this command; the application will start execution until the line pointed by the cursor (if it can come to this predicate call). The Instruction Pointer will point to this line.
  • Go to Executing Predicate Source (Ctrl+E)
    If you have moved in the program source windows and have lost the Instruction Pointer, then you may execute this command. It sets the cursor to the line containing the currently executing predicate (pointed by the Instruction Pointer).
  • Break on Exception
    When this option is checked ON and if an exception occurs, then execution of program is stopped. Then you will be able to use the Run Stack window to locate your predicate in which the exception occurs. See how to Use Break on Exception for more information.
  • Toggle Breakpoint (F9)
    Using this command you can set a breakpoint to any executable line in program source files.
  • Remove All Breakpoints
    This command removes all previously set breakpoints.

How to Use Break on Exception

Some times, it is difficult to locate which your source code predicate generates an exception. In such situation the Break on Exception option can be helpful.

Start to debug your program. When its execution will be close to the expected exception, turn ON the Break on Exception option. (It can be inconvenient to turn the Break on Exception option from the begin of the debugger execution since this can bother you with some otherwise masked exceptions.)

When an exception occurs, Debugger catches it and execution of a debugged program is stopped.

Then you will be able to use the Run Stack window to locate the predicate in which the exception occurs and to inspect the sequence of calls which lead the exception. The topmost in the Run Stack window line corresponds to the last executed predicate. You can double click a line in this window to inspect a clause of the correspondent predicate. If there are no available sources for the Run Stack window entry, the Debugger will show the machine instructions in the Disassembler window.

Debugger Views

Several debugger windows can be opened from the View menu:

The View Sub-menu

The windows, which can be used in the debugger (debugger views), are:

  • Run Stack (CTRL+Alt+C)
  • Variables for Current Clause (CTRL+Alt+V)
  • Facts (CTRL+Alt+F)
  • [Breakpoints_Window.htm BreakPoints (CTRL+Alt+B)]
  • [Threads.htm Threads (CTRL+Alt+H)]
  • [Modules_Window.htm Modules ]
  • [Disassembly_Window.htm Disassembly (CTRL+Alt+D)]
  • [Registers_Window.htm Registers (CTRL+Alt+G)]
  • [Memory_window.htm Memory Dump (CTRL+Alt+M)]

Also the following two windows, which are not only debugger windows (debugger views), can also be used while debugging:

Project Tree

When the debugger is started, you can click the View | Project Window to display the Project Files Tree (the Project window) and the structure of the project files can be examined by exploring the project tree.

The Project Tree View

In this view, it is possible to double-click files (the project consists of) and double-click individual predicates. The result will be that a source code editor opens up with the caret pointing to the clicked item.

Source Code Windows

When the IDE starts debugging a project, it automatically runs the target executable under the debugger, and breaks the program execution at the goal section entry point. The debugger opens the correspondent (containing the goal section) Visual Prolog source file in the IDE text editor.

The Source Code View

Notice the blue arrow Ide db ArrowBlue.png pointing to the executing predicate:

mainExe::run(main::run).

This arrow is the Instruction Pointer, which always points to the currently executing predicate in source code windows. That is, you can follow the Instruction Pointer moving to trace execution of the program.

The debugger changes the color of the Instruction Pointer arrow when it executes a predicate, which fails or which raises an exception.

If you have moved in the program source windows and have lost the Instruction Pointer, then you can easy find it with the menu command Debug | Go to Executing Predicate Source (Ctrl+E). It sets the cursor to the line containing the currently executing predicate (to the line pointed by the Instruction Pointer).

Run Stack Window

The Run Stack consists of three kinds of items:

  • Continue item – marked with Ide db RunStack up.png. It describes ordinary executable clauses, which do not produce backtrack points and are not trapped.
  • BackTrack item – marked with Ide db RunStack dn.png.It describes a clause of a nondeterministic predicate. The next clause of this predicate can be executed when a program failure of this clause occurs. Such items occur when a clause of a predicate, which creates a backtrack point (can produce more than one solution) is called.
  • TrapTrack item – marked with Ide db RunStack rh.png or Ide db RunStack dn rh.png. They describe a continue item (clause), which will be resumed in any case independently whether an error condition occurs or no. For example, such item is created when a predicate call is trapped with the trap/3 predicate. The Ide db RunStack rh.png icon is used to mark trapped clauses of deterministic predicates. The Ide db RunStack dn rh.png icon is used to mark trapped clauses of nondeterministic predicates.

The BackTrack (marked with Ide db RunStack dn.png) and TrapTrack items (marked with Ide db RunStack dn rh.png) are backtracking points. The clause, marked by one of these items, will be resumed and the program execution will be continued after the corresponding failure or an error occurs.

The typical example of the Run Stack window is presented in the following picture:

File:Ide db RunStack 1.png

Pop-up Menu

The Run Stack window has the pop-up context menu:

Ide db RunStack menu.png

For each selected item in the tree, this menu contains items:

  • Refresh
    It refreshes all the Run Stack window contents, rebuilds the tree.

    It also refreshes the Variables window (if it is displayed).
  • Go To Code
    Activates the Prolog source editor and places the cursor at the Prolog clause corresponding to the item selected in the Run Stack window. If the item does not correspond to a Prolog module with debug information, then the Disassembly window will be opened and the cursor will be placed onto the corresponding assembler instruction. The same action is caused by double-click a predicate call in the Run Stack window.
  • Copy Line
    It copies the selected line contents to the clipboard.
  • Show Domains
    This option turns ON/OFF displaying domains of variables.
  • Show Values
    This option turns ON/OFF displaying values of variables.

The Run Stack window can be activated by the IDE menu View | Run Stack.

Variables Window

The Variables window can be activated either by Ctrl+Alt+V or from the IDE menu View | Variables for Current Clause.

Variables Window Contents

The Variables window displays the tree of all program variables and facts from the traced clause, which are already created by the program at the current tracing step. These are: all variables and all object facts (fact variables), which are created in the clause before the executing instruction, and all class facts (fact variables), which can be used in this clause (declared in this class implementation).

The top line of the window displays the declaration of the predicate, whose clause is executed.

Ide db Vars Top.png

The Variables window content is updated after every trace step.

Pop-up Context Menu

The Variables in the Current Clause window has the following pop-up context menu:

Ide db Vars Menu.png

This menu contains items:

  • Insert into Watch Window
    Opens the Watch Window and moves there selected variable.
  • Copy
    This command copies the contents of the line selected in the Variables window to the clipboard.
  • Show as Hexadecimal
    Shows integral values in the hexadecimal format.
  • Show as Decimal
    Shows integral values in the decimal format.
  • Show as Octal
    Shows integral values in the octal format.
  • Show Domains
    This option turns ON/OFF displaying the domains after variable names.
  • Show Variable Addresses
    This option turns ON/OFF displaying the addresses after variable names.
  • Find
    This command allows search variable by name.
  • Copy Tree
    This command copies the contents of the window to the clipboard.

Facts Window

Class Facts

The Facts window always shows the current contents of all class fact databases defined in the program being debugged.

Simple example:

Ide db Facts View.png

Pressing CTRL+ALT+F opens Facts window:

Ide db Facts View3.png

Clicking icons opens sub-trees:

Ide db Facts View4.png

Facts window automatically goes to the fact that is selected in the text editor (if any):

Ide db Facts View11.png

Ide db Facts View41.png

Objects Facts you cat look like a components of object variables in the Variables window

Pop-up Context Menu

The Facts Window' has the following pop-up context menu:

Ide db Vars Menu.png

This menu contains items:

  • Insert into Watch Window
    Opens the Watch Window and moves there selected fact.
  • Copy
    This command copies the contents of the line selected in the Facts window to the clipboard.
  • Show as Hexadecimal
    Shows integral values in the hexadecimal format.
  • Show as Decimal
    Shows integral values in the decimal format.
  • Show as Octal
    Shows integral values in the octal format.
  • Show Domains
    This option turns ON/OFF displaying the domains after facts names.
  • Show Addresses
    This option turns ON/OFF displaying the addresses after facts names.
  • Find
    This command allows search fact by name.
  • Copy Tree
    This command copies the contents of the window to the clipboard.