I am trying to debug a fortran program that makes use of allocatable arrays on OS X El Capitan (10.11.6)
. The program is compiled with gfortran
(GNU Fortran (GCC) 8.1.0
) and I am using gdb
(GNU gdb (GDB) 8.1
). Both gfortran
and gdb
were built from source (instructions: gfortran, gdb). My problem is that gdb
does not correctly recognise/display allocatable arrays.
This is an old and known problem, that appears to have been fixed in recent Linux distributions. So I suspect that this is a problem only on OS X
. Next, I will show a minimal, complete, and verifiable example.
The problem
Test program called test.f90:
program ex
implicit none
integer, parameter :: dp = kind(1.d0)
integer :: i
real(kind=dp),allocatable :: tab(:)
real(kind=dp) :: tab2(10)
allocate(tab(1:10))
do i=1,10
tab(i)=real(i,kind=dp)
tab2(i)=tab(i)
end do
write(*, '(*(f3.0,1x))') tab
write(*, '(*(f3.0,1x))') tab2
end program ex
Compiled using:
gfortran -g test.f90 -o test
Calling gdb
on the executable:
gdb ./test
(gdb) b test.f90:12
(gdb) run
(gdb) info locals
i = 11
tab = ()
tab2 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
(gdb) p &tab
$1 = (PTR TO -> ( real(kind=8) (*))) 0x7fff5fbff930
(gdb) p &tab2
$2 = (PTR TO -> ( real(kind=8) (10))) 0x7fff5fbff8e0
Note that the size of the allocatable variable tab
is not printed/recognised correctly in gdb.
Expected result
On an Ubuntu virtual machine with gdb
(GNU gdb (Ubuntu 8.0.1-0ubuntu1) 8.0.1
), and gfortran
(GNU Fortran (Ubuntu 7.2.0-8ubuntu3.2) 7.2.0
), compiling and debugging using the same commands as above, gives the expected result:
(gdb) info locals
i = 11
tab = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tab2 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
(gdb) p &tab
$1 = (PTR TO -> ( real(kind=8) (10))) 0x55555575a240
(gdb) p &tab2
$2 = (PTR TO -> ( real(kind=8) (10))) 0x7fffffffd7f0
Thus on Ubuntu
, gdb
correctly reports the size and content of the allocatable array tab
identical to tab2
. On both Ubuntu
and OS X
the actual program output is the same and as expected, namely:
./test
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
What I have tried
There are a number of related questions already on this site, but none that I can find offer a working solution (on a more recent OS X
version like El Capitan
). Previous suggestions have been:
- using
lldb
, although others, myself included, finds that this does not work. - a workaround using pointers, e.g.:
(gdb) print *((real_8 *) tab)@10
, to view 10 element of allocatable arraytab
(I would really like to avoid this as it becomes complicated for all but the simplest cases) - installing the
jankratochvil-vla
branch from GDB Project Archer
I can find no example of a working build of the Project Archer gdb
branch on a recent version of OS X
. And my own efforts to build the Project Archer branch still results in a gdb
that does not print out the allocatable array correctly.
It seems as if gdb
installed from e.g. homebrew
also has this problem. Therefore, if anyone can give me some advice on how to obtain or build a version of gdb
on OS X El Capitan
(or newer MacOS
if need be) that can print allocatable arrays from gfortran
, I would truly appreciate it. Alternatively, any explanation as to why it is not possible would also be helpful.