Holger Schmitz
Holger Schmitz

Reputation: 361

Set breakpoint in gdb on array out of bounds for gfortran program

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?

Upvotes: 5

Views: 1192

Answers (2)

Andrew
Andrew

Reputation: 4781

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.

Upvotes: 10

Coriolis
Coriolis

Reputation: 396

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

Upvotes: 0

Related Questions