Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print Fortran arrays in GDB?

In C/C++ to print a pointer as an array I usually do name@dimension. What is the equivalent for Fortran?

like image 277
RSFalcon7 Avatar asked Aug 02 '12 22:08

RSFalcon7


1 Answers

Fortran 90 uses descriptors to represent the dimensions (the shape) of its arrays and to pass assumed-shape array arguments. Also pointers in Fortran are special - they can only point to qualified targets. This allows much better debugger introspection in Fortran than in C/C++. Just use print arr(index) or one of the info commands - no need for fancy stuff.

Sample code:

program arr
  real, dimension(40) :: stack_array
  real, allocatable, dimension(:), target :: heap_array
  real, dimension(:), pointer :: ptr_array
  integer :: i
  ! Interface required because of the assumed-shape array argument
  interface
    subroutine foo(bar, baz, qux, ptr)
      real, dimension(:) :: bar
      real, dimension(40) :: baz
      real, dimension(*) :: qux
      real, dimension(:), pointer :: ptr
    end subroutine foo
  end interface

  allocate(heap_array(40))

  forall(i = 1:40) stack_array(i) = i
  heap_array = stack_array + 2
  ptr_array => heap_array

  print *, stack_array(1)

  call foo(stack_array, stack_array, stack_array, ptr_array)

  deallocate(heap_array)
end program arr

subroutine foo(bar, baz, qux, ptr)
  real, dimension(:) :: bar
  real, dimension(40) :: baz
  real, dimension(*) :: qux
  real, dimension(:), pointer :: ptr

  print *, bar(1), baz(1), qux(1), ptr(1)
end subroutine foo

Compile with debug information and run with gdb:

$ gfortran -g -o arr.x arr.f90 && gdb ./arr.x
...
(gdb) info locals
heap_array = (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ...
ptr_array = (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ...
stack_array = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...
(gdb) print heap_array
$1 = (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ...
(gdb) print ptr_array(3:7)
$2 = (5, 6, 7, 8, 9)
...
(gdb) info args
bar = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ...
baz = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ...
qux = ()
ptr = (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ...

It cannot show the content of assumed-size array arguments for obvious reasons but you can print each element individually:

(gdb) print qux(1)
$5 = 1
(gdb) print qux(2)
$6 = 2
(gdb) print qux(15)
$7 = 15

Note that printing array sections doesn't work on assumed-size array arguments as they are not passed by descriptor and gdb runs into trouble:

(gdb) print qux(1:8)
$8 = (0, 0, 0, 0, 0, 0, 2.25609053e-43, 0)
(gdb) print qux(2:9)
$9 = (0, 0, 0, 0, 0, 0, 2.25609053e-43, 0)
like image 157
Hristo Iliev Avatar answered Sep 28 '22 06:09

Hristo Iliev