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?

0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

6 Comments

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.
@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.
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.
@KevinPowell See also stackoverflow.com/questions/50469327/… 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.
@VladimirF GPLv4?
|

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.