1

I know I can print the n+1-th element of a normal integer array data in GDB as

print *((integer *)data + n)

But how can I correctly print the element out if data is an integer(INT64) allocatable array?

Kevin Powell
  • 591
  • 1
  • 5
  • 20

1 Answers1

1

Note: some older GDB versions or branches used in some unfortunate OSs or distributions may fail to support the allocatable arrays correctly. In that case use the C syntax.

If int64_t isn't recognized by an old GDB, use long or whatever old C type corresponds to a 64-bit integer.


You can really just do

 print data(n+1)

Using

 print *((integer *)data + n)

is C mode GDB syntax, but in Fortran mode it is really simple.

If you really want the complicated C syntax, you can use it even in Fortran mode, it is

 print *((int64_t *)(&data) + n)

In C mode (after set langauge c), you can also use

print *((int64_t *)data + n)

this one does not work in Fortran mode (Cannot access memory at address 0x29).


Example:

use iso_fortran_env

integer(int64), allocatable :: data(:)

integer :: n

data = [(i, i=1, 100)]

n = 5

continue

end

gdb:

GNU gdb (GDB; openSUSE Leap 15.1) 8.3.1
...
(gdb) break int64.f90:9
Breakpoint 1 at 0x4005ec: file int64.f90, line 9.
(gdb) run
Starting program: /home/lada/f/testy/stackoverflow/a.out 

Breakpoint 1, MAIN__ () at int64.f90:9
9       n = 5
Missing separate debuginfos, use: zypper install libgcc_s1-gcc10-debuginfo-10.1.1+git68-lp151.27.1.x86_64 libquadmath0-gcc10-debuginfo-10.1.1+git68-lp151.27.1.x86_64
(gdb) step
13      end
(gdb) print data(n+1)
$1 = 6
(gdb) print *((int64_t *)(&data) + n)
$2 = 6
(gdb) set language c
Warning: the current language does not match this frame.
(gdb) print *((int64_t *)data + n)
$3 = 6
(gdb) print *((long *)data + n)
$4 = 6
  • Unfortunately, ` print data(n+1)` does not work for me and I got `No symbol "int64_t" in current context.` when trying to use the C pointer method. I'm using `gdb (GDB) 8.0.1` on Mac OS X, not sure whether it is a version problem. – Kevin Powell Aug 12 '20 at 07:42
  • @KevinPowell Yes, it is very likely a version problem, please see my note at the very bottom of my answer. If your GDB is that old that it even doesn't understand `int64_t`, use just `long`. Or something else if `long` does not correspond to 64 bits in your system. But it normally should. – Vladimir F Героям слава Aug 12 '20 at 08:01
  • Also make sure you are in the Fortran mode (it should be automatic, but you can try `set language fortran`). Or in C mode when trying those commands I showed for C mode. – Vladimir F Героям слава Aug 12 '20 at 08:05
  • @KevinPowell See also https://stackoverflow.com/questions/50469327/debugging-gfortran-allocatable-array-with-gdb-on-mac-os-x Apple hates GPLv4 and will not provide any new software version that uses this license. If you want a new version, you have to look elsewhere or compile from original source. Otherwise you have to keep using old methods. – Vladimir F Героям слава Aug 12 '20 at 08:34
  • After setting `set language c`, `print *((long *)data + 1)` does not bother with `long` any more but gives wrong result. I just installed `gdb` on `CentOS 7` and everything works perfectly as you have shown. Thanks a lot! – Kevin Powell Aug 12 '20 at 11:51
  • 1
    @VladimirF GPLv**4**? –  Aug 14 '20 at 06:02