Previous: Building a New Program with GPS, Up: Introduction to GPS
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
earlier example.
Select File
, then 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;
Select File
, then Save as
, and enter the file name
example.adb.
Add Example
as a new main unit for the project:
Project
, then Edit Project Properties
.
Main files
tab, 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
OK
To build the executable
select Build
, then 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.
Select Debug
, then 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
statement Put_Line (Line (1..N));
Select Debug
, then Run
. When the
Program Arguments
window appears, click OK
.
A console window will appear; enter some line of text,
e.g. 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 N
.
You will see the value (5) displayed, in “tool tip” fashion.
Right click on N
, select Debug
, then select Display N
.
You will see information about N
appear in the Debugger Data
pane, showing the value as 5.
Right click on the N
in the Debugger Data
pane, and
select Set value of N
.
When the input window appears, enter the value 4
and click
OK
.
This value does not automatically appear in the Debugger Data
pane; to see it, right click again on the N
in the
Debugger Data
pane and select Update value
.
The new value, 4, will appear in red.
Select Debug
, then Next
.
This will cause the next statement to be executed, in this case the
call of 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.
Select Debug
, then Continue
.
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.