How to debug Fortran programs using GDB

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.