GNAT Programming Studio Tutorial

Table of Contents


1 Introduction

This document provides a guide through the major capabilities of the GNAT Programming Studio by working on a code example: sdc, a simple desktop calculator.

It is important to realize that the features that you are about to experiment with are available on multiple platforms, using the same user interface and capabilities, providing a user-friendly environment with a tight integration between the tools.

Start GPS in the directory containing the tutorial files, or if the directory is read-only, copy the tutorial directory and its subdirectories in a local (writable) area, and start GPS from the tutorial directory, so that GPS will load the right context.

By default, the tutorial sources can be found under <prefix>/share/examples/gps/tutorial, where <prefix> is the prefix directory of the GPS installation.

Alternatively, if you have already started GPS in another directory, you can load the project sdc.gpr by using the menu Project->Open...


2 Quick overview of the GPS areas

Having launched GPS, you should now have access to a main window composed of several areas:


3 Editing sources

In the project explorer, open the common directory by clicking on the [+] sign (a triangle under unix systems) on the left of common. This will open the directory and display a list of source files located in this directory.

Now, double click on sdc.adb: this will open a source editor on this file. The source code is syntax-highlighted: keywords, comments, strings and characters have different colors.

As with many other properties, colors are configurable in GPS:

Select the menu Edit->Preferences. This will open a preferences dialog window.

Select the Editor->Fonts & Colors page by clicking on the cross next to the item Editor and then selecting the Fonts & Colors item.

As you go over the various lines and labels, you will notice that by holding the mouse over a label, a tool tip pops up displaying on-line help about the selected item.

Change the background color of the Keywords by clicking on the last down arrow, at the right of the Keywords line.

Choose a color, e.g a light green. When you're done with the color selection, simply click again on the arrow.

Click on the Apply button and look at the effects in the source editor. If you like the new display, click on OK to confirm the changes, otherwise clicking on Cancel will revert to the previous color.


4 Building applications

Select the menu Build->Make->sdc.adb: this will launch a complete build of the sdc application. Note also that a key binding is associated with this menu item (<F4>).

The build has generated a number of errors in a new window: the Locations tree, displayed in the bottom area. The errors are also highlighted in the corresponding source editor.

GPS has automatically jumped to the first error message (sdc.adb, 28:6 : (style) bad indentation), at the line (28) and column (6) of the error.

Fix the error by hand by inserting a space.

Now you can fix the next error by moving the cursor to the line 30 (press the <down> arrow twice), and by using <Ctrl-Tab> (press first the <Control> key, and then the <Tab> key on your keyboard): this key combination asks the source editor to automatically re-indent the current line.

Note that on some window managers or desktop environments, <Ctrl-Tab> is already defined. If this is the case, you can change this shortcut from the key shortcuts dialog (menu Edit->Key shortcuts, Source editor section, Format selection item).

You can then fix all the remaining errors by selecting the whole block (from line 28 to line 40) and pressing <Ctrl-Tab>. To select a block, you can either click on the left mouse button and select the area while holding the button, or using the keyboard by pressing the <Shift> key and moving the cursor using the <Up> or <Down> keys.

Press the <F4> key to build again. GPS will automatically save the modified files, and start a build. This behavior (automatic saving of files before building) can be configured in the preferences dialog.

If you look at the bottom right of the GPS window, you will notice that a progress bar has appeared, displaying the current number of files compiled, and the number of remaining files. This progress bar disappears when the build is finished.

This should now report a successful build.


5 Source Navigation

Now let's try to understand a little bit about how the program is working by looking at the sdc.adb editor: there's a loop, the main processing is done by the functions Process and Next (at line 30).

Click around line 30, move the mouse over Process and let a tool tip appear (global procedure tokens.process declared at tokens.ads:19): this gives information about the kind of entity and the location (file and line) of the declaration of this procedure, the profile of the parameters, and documentation for this function, as extracted from the comments surrounding the procedure declaration.

Do the same for Next (global function tokens.next declared at tokens.ads:15).

Keeping the mouse over Next, display the contextual menu by clicking on the right mouse button, then click on Goto declaration of Next: we're now in the package Tokens, in file tokens.ads; but where is this file in the project?


6 Search Dialog

Select the menu Navigate->Find or Replace...: this will open a search dialog. In the Search for: text entry type tokens.ads. Then select Project explorer in the Look in: area. The search area provides an easy way to search for text or regular expressions in several contexts including the current file, the project explorer, ...

Now click on Find. The file tokens.ads, in directory struct is highlighted.

Close the search dialog by clicking on the Close button.

Note that in this specific case, a simpler way to locate a file in the explorer is to use the contextual menu from the source editor: Locate in explorer: tokens.ads.


7 Project Explorer (entities)

Click on the [+] sign (or triangle) to open tokens.ads entities. When you click on a file in the explorer, you get language sensitive information about the file, such as packages, subprograms, tasks, ... for Ada.

Open the subprogram category, click on Process: this will open tokens.ads and move the cursor on the first line corresponding to the procedure Process.

Similarly, click on Next, and move your mouse on Next in the source editor.


8 Back to Source Navigation

Using the contextual menu, select Goto body of Next; scroll through the procedure Next, move the mouse on Instructions.Read at line 46 and from the contextual menu, select Goto body of Read.

We've now navigated quite a bit through the application source code, which you can verify by clicking on the left arrow in the tool bar, to go back to the previous locations visited.

Repeat the operation until you're back in sdc.adb. As with the undo/redo capability in the source editor, the goto previous/next location is infinite.


9 Run

It is now time to run the application: select the menu Build->Run->sdc, which will open a dialog window. Type input.txt in the text entry: this is the name of a text file that will be passed as argument to the sdc program.

Now click on OK: a new window titled Run: sdc input.txt is created at the bottom of the main window where the sdc application runs and displays an unexpected internal error: this is a good opportunity to use the integrated debugger.

Close the execution window by clicking on the x icon on the top right corner of this window.


10 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 OK to confirm your change.

Now select the menu Debug->Initialize->sdc: a new window is created: this is the debugger console. You can also look at the various debug menu item and tool bar buttons which are now activated.

Use the menu Debug->Data->Call Stack: this opens a new window on the right of the source editors. If you select the contextual menu in the call stack, various pieces of information can be displayed or removed in the call stack. From this contextual menu, add the Frame Number info by clicking on it.

Now select the menu Debug->Run.... Notice that input.txt has been filled automatically for you since the two menus Build->Run... and Debug->Run... are synchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. 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 ecific case, asynchronized. Click on OK: the debugger should stop on an exception (Constraint_Error in the file stack.adb, at line 49).