Finding Exception Locations
This page contains instructions on how to find out which statement caused an exception.
This is particularly useful when a get routine caused the exception, because the information printed
when the exception occurs only tells you that the get caused the exception. It does not tell you
at what statement the get was called. The same is true if the exception occurs in some other library
The following discusses two ways to find the source of an exception. Both ways involve getting a
a traceback of the exception. A traceback is a list of the routines were currently executing when the
exception occurred. The traceback will include more information than you need, and so you will have to
know where to look to find the source of the exception.
The traceback can be printed directly from within your program, or it can be printed from within
gnatgdb, which is the gnat debugger. Instructions on printing from your program are given first.
Causing your program to print a traceback
To cause your program to print a traceback when an exception occurs, you need to do three things
(as shown in this example):
- Include a
With statement for the packages
gnat.traceback.symbolic in your program.
- Include an exception handler that prints
E is the exception parameter.
- Compile with certain options, like this:
gnatmake -f -g myprog.adb -bargs -E
- -f forces compilation (in case executable is up to date)
- -g generates debug info
- -bargs introduces arguments to the linker
- -E include exception traceback info
Introduction to using GDB on rucs to get a traceback
The GNU debugger gdb is used to find the statement that caused the exception.
The debugger will also give you a list of the procedures that were executing when the instruction occurred.
The instructions describe how to use gdb on rucs and when using AdaGIDE.
Immediately below are brief instructions on how to use gdb on rucs.
The instructions may not work on other machines.
Instructions for AdaGIDE follow the example below.
Instructions for using GDB on rucs
Assume that you want to debug the file
myprog.adb on rucs.
Execute the commands listed below.
gnatmake -g -f myprog
- the -g option causes debug info to be included in the .o file, and the -f option forces
recompilation (it's only required if the myprog is up to date)
- This will start the interactive debugger and bring up the debugger prompt
- This will initialize execution of your program by executing
system startup code. It stops at the beginning of your main procedure
- To redirect standard input to read from a file,
you can use the command
start < datafile.txt
which will initialize your program and specify that standard input will read from
- Set up a breakpoint on exceptions. This will cause the debugger to take control when an
- Now continue execution of your program
- If an exception occurs, your program will stop and give another debug prompt
- This will show all of the currently executing subroutines on the system stack
- You should be able to find line numbers of the routines from your program on the stack
- The bottom item in the list shows the number of the statement where the error occurred
- Here is file demogdb.adb:
with ada.integer_text_io; use ada.integer_text_io;
procedure demogdb is
procedure demo(result: out integer) is
get(x); -- We will input 0
result := 3 / x; -- Exception here in line 7
demo(y); -- demo called here in line 12
And here is file demogdb.dat:
rucs@/home/nokie/public_html/classes/320 > gnatmake -g -f demogdb
gcc-4.0 -c -g demogdb.adb
gnatbind -x demogdb.ali
gnatlink demogdb.ali -g
rucs@/home/nokie/public_html/classes/320 > gnatgdb demogdb
GNU gdb 6.4 for GNAT GPL 2006 (20060605)
Copyright 2005 Free Software Foundation, Inc.
Ada Core Technologies version of GDB for GNAT GPL
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
See your support agreement for details of warranty and support.
If you do not have a current support agreement, then there is absolutely
no warranty for this version of GDB. Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...Using host libthread_db library
(gdb) start < demogdb.dat
Breakpoint 1 at 0x80492ea: file demogdb.adb, line 12.
Starting program: /home/nokie/public_html/classes/320/demogdb < demogdb.dat
demogdb () at demogdb.adb:12
(gdb) break exception
Breakpoint 2 at 0xb7db5cf6
No definition of "e.full_name" in current context.
#0 0xb7db5cf6 in __gnat_raise_nodefer_with_msg () from /usr/local/gnu/lib/libgnat-4.0.so.1
#1 0xb7db5f12 in ada.exceptions.raise_with_location_and_msg () from
#2 0xb7db5cf2 in __gnat_raise_constraint_error_msg () from /usr/local/gnu/lib/libgnat-4.0.so.1
#3 0xb7db6037 in __gnat_rcheck_03 () from /usr/local/gnu/lib/libgnat-4.0.so.1
#4 0x08049323 in demogdb.demo () at demogdb.adb:7
#5 0x080492ef in demogdb () at demogdb.adb:12
The program is running. Exit anyway? (y or n) y
Using GDB from AdaGIDE
- After compilation, simply press the Debug button.
- This will bring up a console window that contains the debugger running your program.
- Follow the instructions above, starting with the command
GnatGDB Commands We've Seen
- run with gnatgdb programname
- start: initialize execution (this may be gnatgdb only)
- break exception
- continue: continue execution
Some Other GDB Commands
- break - set a breakpoint
- break routine name
- break line number
- tbreak: temporary break - breaks only once
- info break: shows break points
- Can show info on lots of things
- watch: watch for come condition
- run: run the program
- next: execute next line. Steps over routines
- step: execute next line. Steps into a routine
- until: run until a line number
- print x: show value of x
- b: break
- h: help
- bt: backtrace
Using GDB from GPS
- Set debug compiler:
- Project/Edit Project Properties/Switches/Gnatmake
- Recompile program (make have to make a dummy change to force
- Debug/myprog (or whatever your program is named)
- In debug tab:
- Catch exception
- In command window: enter data
- In debug tab:
- Don't forget to quit to get out of debug window