Previous: Building a New Program with GPS, Up: Introduction to GPS

1.5.2 Simple Debugging with GPS

This section illustrates basic debugging techniques (setting breakpoints, examining/modifying variables, single stepping).

  1. Opening a project

    Start GPS and select Open existing project; browse to specify the project file sample.prj that you had created in the earlier example.

  2. Creating a source file

    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;
                 Put_Line("Type a line of text at each prompt; an empty line to exit");
                    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.

  3. Updating the project file

    Add Example as a new main unit for the project:

    1. Select Project, then Edit Project Properties.
    2. Select the 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
    3. Click OK
  4. Building/running the executable

    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.

  5. Debugging the program

    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.

    1. Initializing

      Select Debug, then Initialize, then example

    2. Setting a breakpoint

      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));

    3. Starting program execution

      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.

    4. Examining a variable

      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.

    5. Assigning a new value to a variable

      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.

    6. Single stepping

      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.

    7. Removing a breakpoint

      Toggle the breakpoint icon at line 10.

    8. Resuming execution from a breakpoint

      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.