Debugging Fortran Using dbx |
18 |
![]() |
This chapter includes the following topics:
Debugging Fortran
The following tips and general concepts are provided to help you while debugging Fortran programs. Current Procedure and File
During a debug session, dbx defines a procedure and a source file as current. Requests to set breakpoints and to print or set variables are interpreted relative to the current function and file. Thus, stop at 5 sets one of three different breakpoints, depending on whether the current file is a1.f, a2.f, or a3.f. dbx Uppercase Letters (Fortran 77 only)
If your program has uppercase letters in any identifiers, dbx recognizes them. You need not provide case-sensitive or case-insensitive commands, as in some earlier versions. (The current release of f90 is case-insensitive.)
Note - File or directory names are always case-sensitive in dbx, even if you have set the dbxenv case insensitive environment attribute.
a1.f |
PARAMETER ( n=2 ) REAL twobytwo(2,2) / 4 *-1 / CALL mkidentity( twobytwo, n ) PRINT *, determinant( twobytwo ) END |
a2.f |
SUBROUTINE mkidentity ( array, m ) REAL array(m,m) DO 90 i = 1, m DO 20 j = 1, m IF ( i .EQ. j ) THEN array(i,j) = 1. ELSE array(i,j) = 0. END IF 20 CONTINUE 90 CONTINUE RETURN END |
a3.f |
REAL FUNCTION determinant ( a ) REAL a(2,2) determinant = a(1,1) * a(2,2) - a(1,2) / a(2,1) RETURN END |
Sample dbx Session
The following examples use a sample program called my_program.
demo% f77 -o my_program -g a1.f a2.f a3.f |
demo% f77 -c -g a1.f a2.f a3.f
demo% f77 -o my_program a1.o a2.o a3.o
demo% dbx my_program
Reading symbolic information...
(dbx) stop in MAIN (2) stop in MAIN |
(dbx) run Running: my_program stopped in MAIN at line 3 in file "a1.f" 3 call mkidentity( twobytwo, n ) |
(dbx) print n n = 2 |
(dbx) print twobytwo
twobytwo =
(1,1) -1.0
(2,1) -1.0
(1,2) -1.0
(2,2) -1.0
(dbx) print array
dbx: "array" is not defined in the current scope
(dbx)
(dbx) next stopped in MAIN at line 4 in file "a1.f" 4 print *, determinant( twobytwo ) (dbx) print twobytwo twobytwo = (1,1) 1.0 (2,1) 0.0 (1,2) 0.0 (2,2) 1.0 (dbx) quit demo% |
The most frequent causes for a segmentation fault are:
Use a program to generate a segmentation fault:
demo% cat WhereSEGV.f INTEGER a(5) j = 2000000 DO 9 i = 1,5 a(j) = (i * 10) 9 CONTINUE PRINT *, a END demo% |
Use dbx to find the line number of a dbxsegmentation fault:
Locating Exceptions
If a program gets an exception, there are many possible causes. One approach to locating the problem is to find the line number in the source program where the exception occurred, and then look for clues there.-ftrap =%all
forces trapping on all exceptions.
Tracing Calls
Sometimes a program stops with a core dump, and you need to know the sequence of calls that brought it there. This sequence is called a stack trace.
Working With Arrays
dbxdbx recognizes arrays and can print them:
Fortran 90 Allocatable Arrays
The following example shows how to work with allocated arrays in dbx.
Slicing Arrays
The syntax for Fortran array-slicing:
print arr-exp(first-exp:last-exp:stride-exp) |
Variable |
Description |
Default |
arr-exp |
Expression that should evaluate to an array type |
|
first-exp |
First element printed |
Lower bound |
last-exp |
Last element printed |
Upper bound |
stride-exp |
Stride |
1 |
|
|
|
|
|
(dbx) print a(3:3,1:4) 'ShoSli'MAIN'a(3:3, 1:4) = (3,1) 31 (3,2) 32 (3,3) 33 (3,4) 34 (dbx) |
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
|
|
|
|
|
|
(dbx) print a(1:3,4:4) 'ShoSli'MAIN'a(3:3, 1:4) = (1,4) 14 (2,4) 24 (3,4) 34 (dbx) |
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
|
dbxShowing Intrinsic Functions
dbx recognizes Fortran intrinsic functions.
dbxShowing Complex Expressions
dbx also recognizes Fortran complex expressions.
dbxShowing Logical Operators
dbx can locate Fortran logical operators and print them.
Viewing Fortran 90 Derived Types
You can show structures--f90 derived types with dbx.
(dbx) print prod1 prod1 = ( id = 82 name = 'Coffee Cup' model = 'XL' cost = 24.0 price = 104.0 ) |
Pointer to Fortran 90 Derived Type
You can show structures--f90 derived types, and pointers with dbx.
Above, dbx displays all fields of the derived type, including field names.
Fortran 90 Generic Functions
To work with Fortran 90 generic functions:
To use dbx with a generic function, cube root.