Fortran print allocatable array in gdb

Solution 1:

Use

(gdb) print *((real *)A+m)@n

where A is the array, m is the shift in the position (no need to write +m in the case m=0) and n is the number of elements that you want to print. If you're working with double precision reals then replace real by real_8. Also, for integers replace real by int, and for long integers use long_int. Credit goes to http://numericalnoob.blogspot.fr/2012/08/fortran-allocatable-arrays-and-pointers.html

Solution 2:

UPDATE: Works out-of-the-box in Ubuntu 16.04 LTS

The issue is being discussed for almost a decade already (e.g. https://sourceware.org/bugzilla/show_bug.cgi?id=9395) and is partially fixed in some distros. I have reported the same problem in gdb that comes with Ubuntu 14.04 LTS. The workaround was in a similar fashion as Francois Jacq suggested here, but without damaging dpkg-controlled directories.

I have used alien tool from native Ubuntu repo to convert a gdb RPM from Fedora (namely GNU gdb (GDB) Fedora 7.9-10.fc23) into .deb package, and then used dpkg to install it. Now i have installed gdb-7.9.1-7.1.x86_64.rpm from OpenSUSE in a similar manner:

fakeroot alien gdb-7.9.1-7.1.x86_64.rpm
sudo dpkg -i gdb_7.9.1-8.1_amd64.deb

In many cases it works correctly with simple pointers and allocatable arrays. Though segfaults of gdb are often when trying to touch large and/or complex structures. Probably, that is why many maintainers prefer not to include the fortran patch into mainstream...

Please consider confirming the bug in your distro bug trackers, so maintainers will pay more attention to it.

Solution 3:

Unfortunately GDB trunk comes with no support of allocatable arrays. But the Archer branch includes some support.

I recently read the following post of Tobias Burnus :

https://gcc.gnu.org/ml/fortran/2012-09/msg00013.html

I work with Ubuntu 14.02. I downloaded the most recent OpenSUSE rpm package of gdb and extracted it by a command like

rpm2cpio myrpmfile.rpm | cpio -idmv

Then I copied the gdb executable program in /usr/local/bin (with sudo cp) and I was happy to see it working correctly !