1

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:

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.

7
  • Please use the fortran tag for all Fortran questions. Commented May 22, 2018 at 13:47
  • @VladimirF sorry about that, just edited my question to also include the flag (I've been too broad with flags in the past, so tried to be very specific this time) Commented May 22, 2018 at 13:50
  • Why would you expect the result that you have shown? These arrays are uninitialized so they can contain anything at that particular step in the debugger. On Arch Linux gfortran v8.1.0 I get $1 = (1, 0, 0, 0, 0, 0, 4.3477776834029696e-322, 1.6391144015104463e+59, 3.4584595208887258e-323, 1.6391141290730679e+59). Commented May 22, 2018 at 14:28
  • @RaulLaasner, bad example perhaps, the problem is not the uninitialised entries, but the first entry in the array. I just showed debug results after one iteration, so tab(1) should be the same as tab2(1) = 1. If you run through all iterations, they should both read (1, 2, ..., 8, 9, 10), but this does not happen for tab Commented May 22, 2018 at 14:50
  • 1
    The problem is almost certainly caused by Apple rejecting anything with the GPLv4 license and just hating GPL in general. Did you try building GDB from official sources? Commented Aug 12, 2020 at 8:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.