8. Debug

Open the preferences dialog (menu Edit->Preferences…) and click on the Debugger item on the left; set the button Break on exceptions to Enabled: this will enable by default a special breakpoint every time an exception is raised. Click on Close to close dialog.

Now click on the Build & Debug sdc.adb toolbar button: GNAT Studio automatically switches to the Debug perspective as shown in the menu Window->Perspectives, and new windows have appeared: the variables window and the call stack window on the right, the breakpoints view on the left, and the debugger console at the bottom.

You can also look at the various debug menu item and tool bar buttons which are now activated.

On the call stack window (you can use the menu Debug->Data->Call Stack to open it if you do not have it displayed), select the local configuration menu: various pieces of information can be displayed or removed in the call stack. From this local configuration menu, add the Frame Number info by clicking on it.

Now click on the Debug continue button and type input.txt in the text input field. Check that ‘Stop at beginning of main subprogram’ and ‘Use exec dir instead of current dir’ are not selected. Click on OK: the debugger should stop on an exception (Constraint_Error in the file stack.adb, at line 49).

Go up in the call stack by clicking on the tokens.process frame (frame number will vary, depending on your GNAT version and platform).

If you move the mouse over the parameter T at line 64, a tool tip is displayed showing the value of T. You have probably noticed that tool tips, like menus, are contextual: depending on the current session and on the entity selected, different information is displayed.

Select the contextual menu Debug->Display T in Variables View: this will open the Variables view, displaying graphically the contents of the different fields of T, each clearly separated.

Special colors are used in the data display depending on your theme for fields that have been modified since last step.

From the T row, right-click to display the contextual menu and select Debug/View memory at address of T: a memory view is opened on right side. Use the up and down arrows on the right to visit memory.

Click in the memory dump, and modify it by typing numbers. Notice the green color for modified values; click on Undo Changes to cancel the modifications; then close the memory window by e.g. clicking on the x icon in the tab or pressing Ctrl-W.

In the call stack, go back to the stack.push frame. Move the mouse over Last and let the debugger display its value: 0. From the contextual menu, select Go To Declaration: this will jump to the line 16 of stack.adb, where you can see that Last is a Natural. Now click on the Goto Previous Location button in the tool bar: we’re now back at line 49 where we can see that for a Push procedure, Last should be incremented, and not decremented.

Fix the line to Last := Last + 1;

Save the file (Ctrl-S); End the debug session: Debug terminate toolbar button; Rebuild (press F4 key); Rerun (menu Build->Run->Run Main sdc, click on Execute): the program now completes as expected. Close the execution window.