5

I have a Fortran program compiled with gfortran with the -fcheck=bounds compiler option. This causes the code to report "array out of bounds" errors and subsequently exit.

I would like to debug my program using gdb to find the cause of the error. Unfortunately in gdb the code will still just exit on an out of bounds error.

Is there a way to tell gdb to stop execution when an out of bounds error occurs?

Holger Schmitz
  • 361
  • 1
  • 6

2 Answers2

10

Compile with -g to get debugging information. Then, first, I placed a break point on exit, this works fine, once the program stops you'll be able to backtrace from exit to the point of the error.

The backtrace also passes through a function called _gfortran_runtime_error_at, so you might have more luck placing the breakpoint there, this worked for me, and obviously will only trigger when you get a run time error.

Andrew
  • 3,770
  • 15
  • 22
0

To set a breakpoint on gdb, use the command break then the name of the file you are debugging, a colon and the number of the line from which you want to break execution :

break main.f90:24

will stop the execution at line 24 of program main. Then you can use the step command to jump to the next line and so on. At this point you can use print to check the value of any variable you want. If you have defined another breakpoint, you can use the command next to jump to the next breakpoint directly.

You will need to compile your program with the -g flag to be able tu use gdb

Coriolis
  • 396
  • 3
  • 10
  • 1
    That doesn't really answer my question. I know how to set a breakpoint at a specific line of code. I need to break when an "array out of bounds" error is detected. The particular line of code might be evaluated a million times before such an error occurs so setting a breakpoint on that line is not helpful. – Holger Schmitz Jun 15 '16 at 16:36