This section illustrates basic debugging techniques (setting breakpoints, examining/modifying variables, single stepping).
Start GPS and select
Open existing project; browse to
specify the project file sample.prj that you had created in the
New, and type in the following program:
with Ada.Text_IO; use Ada.Text_IO; procedure Example is Line : String (1..80); N : Natural; begin Put_Line("Type a line of text at each prompt; an empty line to exit"); loop Put(": "); Get_Line (Line, N); Put_Line (Line (1..N) ); exit when N=0; end loop; end Example;
Save as, and enter the file name
Example as a new main unit for the project:
Edit Project Properties.
Main filestab, click
Add, then select the file example.adb from the list, and click
Open. You will see the file name appear in the list of main units
To build the executable
Make, and then choose example.adb.
Run the program to see its effect (in the Messages area). Each line that you enter is displayed; an empty line will cause the loop to exit and the program to terminate.
Note that the -g switches to gcc and gnatlink, which are required for debugging, are on by default when you create a new project. Thus unless you intentionally remove these settings, you will be able to debug any program that you develop using GPS.
Initialize, then example
After performing the initialization step, you will observe a small icon to the right of each line number. This serves as a toggle for breakpoints; clicking the icon will set a breakpoint at the corresponding line (the icon will change to a red circle with an “x”), and clicking it again will remove the breakpoint / reset the icon.
For purposes of this example, set a breakpoint at line 10 (the
Put_Line (Line (1..N));
Run. When the
Program Arguments window appears, click
A console window will appear; enter some line of text,
abcde, at the prompt.
The program will pause execution when it gets to the
breakpoint, and the corresponding line is highlighted.
Move the mouse over one of the occurrences of the variable
You will see the value (5) displayed, in “tool tip” fashion.
Right click on
Debug, then select
You will see information about
N appear in the
pane, showing the value as 5.
Right click on the
N in the
Debugger Data pane, and
Set value of N.
When the input window appears, enter the value
4 and click
This value does not automatically appear in the
pane; to see it, right click again on the
N in the
Debugger Data pane and select
The new value, 4, will appear in red.
This will cause the next statement to be executed, in this case the
Put_Line with the string slice.
Notice in the console window that the displayed string is simply
abcd and not
abcde which you had entered.
This is because the upper bound of the slice is now 4 rather than 5.
Toggle the breakpoint icon at line 10.
The program will reach the next iteration of the loop, and
wait for input after displaying the prompt.
This time, just hit the Enter key.
The value of
N will be 0, and the program will terminate.
The console window will disappear.