Tuesday, November 26, 2013

Debug Fortran Program with gdb

1.    Compile the program with “-g” option. Eg,
gfortran -g foo.f90 -o foo
2.    Start program with gdb
gdb foo
3.    Get help about the debug commands
help
help command
4.    Set breakpoints
break line_number                                   (break at a given line number)
break function_name                              (break at the entry of a specified function)
If multiple source files are involved, specify the file name like
break file_name:line_number
break file_name:function_name
5.    Set command line arguments (optional)
set args option1 option2
6.    Start running under gdb (and stop at the first breakpoint if set)
run
run option1 option2                                (give command line arguments)
7.    List all breakpoints
info breakpoints
8.    Delete breakpoint(s)
delete number                                              (delete the breakpoint numbered number)
delete                                                           (delete all breakpoints)
clear  line_number                                       (delete breakpoint at a particular line number)
clear function_number                                 (delete breakpoint at a particular function)
9.    List program source code
list                                                (print out some lines of codes around the current stop point)
list line_number                            (print out some lines of codes around line_number)
list line_number_start,line_number_end
list file_name:line_number
10.    Inspect variables
print variable_name
To print values of an allocatable array
print *((real_8 *)my_array + 1)  (print the second element of a real*8 array)
print *((real_8 *)my_array + 1)@5 (print five elements from number 2)
print my_array[0]@25                                  (print the first 25 elements in an array)
11.    Continue running until the next breakpoint
continue
12.    Execute current line and then stop execution again (function call will be completely executed before stop execution if current line calls a function)
next
13.    Execute current line and stop (will stop at the first line of a function called if current line calls a function)
step
14.    Step out of a loop (if at the last line of a loop) and then stop at the first line after the loop
until
15.    Exit gdb
quit