Previously, I thought the debugging functionality provided by GDB-Fortran program is quite limited due to experiences Of a couple of failed attempts to print allocatable arrays. However, having given several another tries today, I discovered the GDB does support printing values of allocatable array s, although it may be a little inconvenient. Now, let's see the details:
We have the test code as follows, in which a derived type cmp, an allocatable array a and a static array b is defined.
Program Test Implicit none Type cmp integer a integer b integer, Dimension (:),allocatable:: C integer, Dimension (5):: D end Type CMP integer, Dimension (:),allocatable:: A integer, Dimension (5):: B = (/1,2,3,4,5/) type (CMP) x allocate (A (5)) Allocate (x%c (5)) a= (/1,2,3,4,5/) X%a= 5x%b= 10x%c=a x%d=a write (STDOUT,*),a write (STDOUT,*),b Write (STDOUT,*),x%c deallocate (a) deallocate (x%c) End program Test
For the static array b, there are no need to mention it because GDB have a good support whether or isn't it is define D inside a derived type. However, if we want to directly print the allocatable array in GDB, a null value 0 would be printed:
(GDB) p a$1 = (0)
If We treat the allocatable array as a C pointer,
(GDB) p *aattempt to take contents of a Non-pointer value. (GDB) p A (1) $2 = 0 # note:this is a wrong value (GDB) p a[1]a syntax Error in expression, near '[1]'. # note:square Brackets is invalid
The correct form is:
(GDB) p * ((integer *) a) $3 = 1(GDB) p * ((integer *) A +1) $4 = 2# ... and so on.
The whole array can also is printed out all at once:
(GDB) p * ((integer *) a) @5$5 = (1, 2, 3, 4, 5)
For the allocatable array in the derived type cmp, we should note this member variable along with its parent Should is wrapped in brackets:
(GDB) p * ((integer *) x%c) Attempt to extract a component of a value, a structure. # wrong (GDB) p * ((integer *) (X%C)) $1 = 1 # Right (GDB) p * ((integer *) (x%c) +1) $2 = 2(GDB) p * ((integer *) (x%c)) @5 $3 = (1, 2, 3, 4, 5)
ok, up to now, we Fortran program can be debugged efficiently, without Havi ng to insert debugging print or write subroutine any more!