tech day 2015: a gentle introduction to gps and gnatbench

61
A Gentle Introduction to GPS and GNATbench Albert Lee November 3, 2015

Upload: adacore

Post on 21-Jan-2017

454 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

A Gentle Introduction toGPS and GNATbench

Albert LeeNovember 3, 2015

Page 2: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Agenda

• GNAT Projects

• GPS

• GNATbench

Page 3: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

GNAT Project Fundamentals

Input Files Output Files

Dependencies

Command Options and

SwitchesGNAT

Project

Page 4: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

The GNAT Project File: Hello World

project My_Project is

end My_Project;

my_project.gpr

Page 5: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

The GNAT Project File: Input Files

end My_Project;

project My_Project is

for Source_Dirs use (“src”); for Source_Dirs use (“src”);

Page 6: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

The GNAT Project File: Output Files

end My_Project;

project My_Project is

for Source_Dirs use (“src”); for Object_Dir use “obj”; for Exec_Dir use “.”; for Object_Dir use “obj”; for Exec_Dir use “.”;

Page 7: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

The GNAT Project File: Dependencies

end My_Project;

project My_Project is

for Source_Dirs use (“src”); for Object_Dir use “obj”; for Exec_Dir use “.”;

with “../some_library/my_library.gpr”;with “../some_library/my_library.gpr”;

Page 8: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

The GNAT Project File: Switches

end My_Project;

project My_Project is

for Source_Dirs use (“src”); for Object_Dir use “obj”; for Exec_Dir use “.”;

with “../some_library/my_library.gpr”;

package Builder is for Default_Switches (“Ada”) use (“-g”); end Builder;

package Builder is for Default_Switches (“Ada”) use (“-g”); end Builder;

Page 9: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

The GNAT Project File

project My_Project is

end My_Project;

for Source_Dirs use (“src”); for Object_Dir use “obj”; for Exec_Dir use “.”;

with “../some_library/my_library.gpr”;

package Builder is for Default_Switches (“Ada”) use (“-g”); end Builder;

Page 10: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

A Complete GNAT Project

src/

utility.ads

utility.adb

my_project.gpr

obj/

utility.o

utility.ali

Page 11: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

The GNAT Project Manager

-P• GPS• GNATbench• GPRbuild• GPRinstall• GPRconfig• GPRclean• CodePeer• SPARK• GNATtest• Your Custom Tool

Page 12: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Many More Features to Explore• Multi-language capabilities

• Configurable source file naming schemes for every language

• Switching settings and properties according to “scenarios”

• Library projects

• Configuration of compilation, binding, and linking switches

• Project organization– Imported projects– Extended projects

Page 13: Tech Day 2015: A Gentle Introduction to GPS and GNATbench
Page 14: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Invoking GPS Using Icons• There are several ways to open GPS iconically.

Open GPS Open GPS

on Source

Open GPS on ProjectOpen GPS

on Source

Page 15: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Invoking GPS from a Shell• Just say “gps”

– If one project file is present in the current directory, run with that project loaded.

– Otherwise, ask the user what to do.

• Or, invoke with filenames: “gps <filename> [<filename> …]”– This will open files for editing

• Or, invoke with “gps –P<project>”– Starts GPS with that project loaded

Page 16: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

What is a “Default Project”?• Ada files only

– Using the default GNAT naming scheme

• Current directory is the single source directory – All the Ada files found in this

directory will be considered as part of the default project

• Current directory is where the object and executable files will be placed

• Current directory can be set by modifying the text entry or by clicking on the “Browse” button

Page 17: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Choose Your Own Adventure

A. Create new project from template

B. Start with default project

C. Create new project with wizard

D. Open existing project

Page 18: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Copy the GPS Tutorial Project<gps_dir>/share/examples/gps/tutorial

Page 19: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Invoke GPS on the SDC Project• Use the Start/Windows menus to invoke GPS

– Then, click the “Open existing project” option.– Then, browse to your copy of sdc.gpr.

• Double-click the sdc.gpr file icon(though this method doesn’t work on Linux)

• Command line: Invoke “gps –P <dir>/sdc.gpr”

• Command line: “chdir” into your “tutorial” directory and enter “gps”

Page 20: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Welcome to GPS.

Page 21: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

The Build Menu Locations of errors or warnings appear in the Locations Tree

One for each main in the project

Compile all files in top-level project

Consider as a main file and build it

Displays a text entry to enter any external command

Compile all main subprograms

Page 22: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Toolbar Build Buttons

CheckSemantic

CompileFile

Build Main Build All Clean All Run Main

Configure behavior through Build Settings Targets

Page 23: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Build Main: sdc.adb• Press F4 (and press “Execute” in

the resulting dialog), or

• Click the Build Main toolbar button, or

• Click the Build All toolbar button, or

• Use the analogous items in theBuild Project drop-down menu(and press “Execute” in any resulting dialogs).

Build Main:sdc.adb

Build All

Page 24: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Hey your example is broken!

Try CodeFix

Page 25: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Or, Fix by Hand• Highlight lines

• Press <Tab>

Page 26: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Build again

• Success!

• The sdc.gpr file provides an authoritative, consistent description of your project to all tools.

F4

Page 27: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

GPS Editing

• Syntax highlighting

• Current block indicator

• Delimiter highlighting

• Current line indicator

• Block folding controls

• Tooltips

Page 28: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Editing Features• Automatic indentation

– Available while typing

• Source code reformatting– Bulk indentation of source code and more

• Smart completion– Available while typing– Available by explicitly typing <Ctrl>-<Space>

Page 29: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Understanding Source Code• Navigation is based on “library information” files produced by

GNAT compiler

• Hence, sources must be compiled (at least partially)

• Use -gnatQ for sources that will not compile yet

• Recompile modified sources to ensure consistent navigation

• Can take a significant amount of CPU time for large projects

• Works for C/C++ too!– Use the -fdump-xref switch

Page 30: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Ways to Navigate• Click on an item anywhere in GPS (not limited to the editor) and

use actions in the Navigate menu

• Control-click entities in the editor window to jump to their definitions

• Right-click entities in the editor window to show navigation items in a contextual menu

• Make use of “Goto next location” (Forward) and “Goto previous location” (Back) actions in the toolbar or Navigate menu

Page 31: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Run SDC• Press Shift-F2 (and press

“Execute” in the resulting dialog), or

• Click the Run Main toolbar button, or

• Invoke the Build Run sdc menu action (and press “Execute” in the resulting dialog).

• Then, try entering “1 2 +” in the Run: pane below:

Page 32: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

About The Debugger• GPS presents a graphical interface to an underlying debugger• Uses industry-standard debugging formats and ABIs• Can edit and navigate sources while debugging• Multiple languages supported

– Ada, C, C++ code in the same application• Able to use in a wide variety of situations

– Multiple processes, multiple tasks– Exception debugging– Elaboration debugging

Page 33: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Invoke the Debugger

Debug Initialize sdc

Page 34: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

The Debugging Perspective

Page 35: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Start /Continue

The Debugging Toolbar

Step Next Finish Up Down

Page 36: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

The Debug Menu

Only current

debuggerAll debuggers

As per toolbar

One machine instructio

n

Opens a dialog

window

Page 37: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Stop on All Exceptions• Invoke Debug Data Edit

Breakpoints menu action

• Click the Exception tab

• Click the + Add button

• Observe the action in the Debugger Console

Page 38: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Stop on Exceptions the Easy Way

(gdb) catch exception

Page 39: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Run SDC Under the Debugger• Press F2 (and click “OK” in the

resulting dialog), or

• Click the Continue toolbar button, or

• Invoke the Debug Run … menu action (and click“OK” in the resulting dialog).

• Then, you should have stopped at the beginning of your program. Press F8 or click the Continue toolbar button to continue your program.

Page 40: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Interact with Your Program• GPS will appear to be stopped.• Find the external execution window.• Enter “1 2 +” in it.

Page 41: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

We Caught Something

Catchpoint 1,CONSTRAINT_ERROR at 0x00403131in stack.push (v=0x2602510)

at C:\Users\Albert Lee\Desktop\tutorial\common\stack.adb:49

Page 42: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

A Sampling of Available Tools• The Call Stack shows all stack frames, including Ada runtime

frames as well as application frames; click to switch frames.

• Hover to reveal tooltips that show values stored in variables

• Right-click on an item, then Debug Display My_Item to show the item graphically in the Debugger Data window

• Ctrl-click on items to navigate to their definitions

Page 43: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Spoilers• The bug is:

– Last is a variable of type Natural, and it is set to 0– The line at stack.adb:49 is decrementing it, hence the CE– The stack should be growing by incrementing Last

• Fix the bug!– Modify the source—change that ‘–’ to a ‘+’– Terminate the debugger with Debug Terminate– Rebuild the program with F4– Rerun the program with Shift-F2

Page 44: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Expected Behavior

Welcome to sdc. Go ahead type your commands ...1 2 +1 2 +printprint -> 3quitquitThank you for using sdc.

Page 45: Tech Day 2015: A Gentle Introduction to GPS and GNATbench
Page 46: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Same Brains, Different Body• Similar Concepts and Features

• Common AdaCore IDE code is used in GPS and GNATbench

• Uses the same technology to find the reference information: the GNAT Project File Facility

• Files must be compiled for full navigation capability

• You can switch back and forth between GPS and GNATbench– If some things are more convenient in one IDE than in another

Page 47: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Projects in GNATbenchEclipse projects are not GNAT projects; an Eclipse project needs to be linked to a

GNAT project in order for AdaCore GNATbench functionality to work.

• Automatic when using wizards– Create New Project wizard– Import wizard– Fairly limited so mostly works for simple cases

• For existing/complex projects, needs to be done by hand using Conversion wizard– New Other Ada Convert

Page 48: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Choose Your Own Adventure

A. Create a new Ada project using a wizard

B. Import an existing GNAT project

Page 49: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Import the SDC Project• Connect an external GNAT

project to Eclipse workspace

• Two ways to start:– Use File Import…, or– Use contextual menu in

the Navigator and select "Import…”

• Then, click Next >

Page 50: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Locate Your Project for ImportEnter path to .gpr file

or browse to it

“Linking” uses the existing external files

• Then, click Finish

Page 51: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

GNATbench Sees Your SDC Project• Straightforward mapping of

sdc.gpr properties– GNAT project file sdc.gpr– Source directories struct and

common– Object directory obj– Executable directory maps to

Binaries– The Ada runtime dependency

The Project Explorer after linking the sdc.gpr GNAT project into our workspace

Page 52: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Ada Editor Features• Syntax highlighting

• Parenthesis/bracket matching

• Block folding

• Several indentation modes

• Code Assist (just like Code Completion in GPS – use Ctrl+Space)

• Automatic formatting/casing

• Current line indicator

• Current block indicator

Page 53: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Understanding Your Sources• Like GPS, GNATbench requires “library information” files from

compilation for navigation features.

• Ctrl-click works to hyperlink to references similar to GPS

• Or, use right-click contextual menu Ada submenu– Open Ada Declaration (F3)– Open Ada Body (Shift + F3)

Page 54: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Building Projects• Right-click on the Sdc node in

the Project Explorer

• Go to Build Main sdc.adb

• Then in the resulting dialog, click Proceed

Page 55: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Running Executables• Right-click on Sdc in the Project Explorer

• Run As 1 Local Ada Application

Page 56: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Be Sure to Clean Up!• Consoles remain until removed so you can see output after

program completion• Must be manually removed when ready

– On Windows you cannot build again until they are removed

Page 57: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Start the GNATbench Debugger• Right-click on Sdc in the Project Explorer

• Debug As 1 Local Ada Application

• Open Debug perspective as is suggested

Page 58: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

The Debug Perspective

Page 59: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Try Poking Around• Debugger has similar functionality as GPS

– Both GUIs use an underlying debugger like GDB– Data display is a major difference

• Make sure to clean up terminated launches!

Page 60: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Summary• GNAT Projects are at the heart of GPS and GNATbench IDEs

• Compile-Edit-Debug covered– Not all principles explained– Not all features covered– Not all situations covered

• This was a whirlwind tour of the tip of the iceberg– You know enough to be dangerous– And you should continue to learn more.

Page 61: Tech Day 2015: A Gentle Introduction to GPS and GNATbench

Resources• GPRbuild User’s Guide

• GPS and GNATbench Tutorials

• GPS and GNATbench Users Guides

• AdaCore Training Courses