Debugging a Program With dbx |
Debugging Fortran Using
dbx
This chapter introduces
dbx
features you might use with Fortran. Sample requests todbx
are also included to provide you with assistance when debugging Fortran code usingdbx
.This chapter includes the following topics:
- Debugging Fortran
- Debugging Segmentation Faults
- Locating Exceptions
- Tracing Calls
- Working With Arrays
- Showing Intrinsic Functions
- Showing Complex Expressions
- Showing Logical Operators
- Viewing Fortran 95 Derived Types
- Pointer to Fortran 95 Derived Type
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 different breakpoints, depending on which file is current.Uppercase Letters
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.FORTRAN 77, Fortran 95, and
dbx
must be in the same case-sensitive or case-insensitive mode:
- Compile and debug in case-insensitive mode without the
-U
option. The default value of thedbx
input_case_sensitive
environment variable is thenfalse
.
- If the source has a variable named
LAST
, then indbx
, both theLAST
orlast
commands work. FORTRAN 77, Fortran 95, anddbx
considerLAST
andlast
to be the same, as requested.
- Compile and debug in case-sensitive mode using
-U
. The default value of thedbx
input_case_sensitive
environment variable is thentrue
.
- If the source has a variable named
LAST
and one namedlast
, then indbx
,LAST
works, butlast
does not work. FORTRAN 77, Fortran 95, anddbx
distinguish betweenLAST
andlast
, as requested.
Note File or directory names are always case-sensitive indbx
, even if you have set thedbx
input-_case_sensitive
environment variable tofalse
.
Optimized Programs
- Compile the main program with
-g
but without-O
n.- Compile every other routine of the program with the appropriate
-O
n.- Start the execution under
dbx
.- Use the
fix -g
any.f
command on the routine you want to debug, but without-O
n.- Use the
cont
command with that routine compiled.Main program for debugging:
a1.f
PARAMETER ( n=2 )REAL twobytwo(2,2) / 4 *-1 /CALL mkidentity( twobytwo, n )PRINT *, determinant( twobytwo )ENDSubroutine for debugging:
a2.f
SUBROUTINE mkidentity ( array, m )REAL array(m,m)DO 90 i = 1, mDO 20 j = 1, mIF ( i .EQ. j ) THENarray(i,j) = 1.ELSEarray(i,j) = 0.END IF20 CONTINUE90 CONTINUERETURNENDFunction for debugging:
a3.f
REAL FUNCTION determinant ( a )REAL a(2,2)determinant = a(1,1) * a(2,2) - a(1,2) / a(2,1)RETURNENDSample
dbx
SessionThe following examples use a sample program called
my_program
.1. Compile and link with the-g
option.
2. Startdbx
on the executable namedmy_program
.
demo%
dbx my_program
Reading symbolic information...3. Set a simple breakpoint by typingstop
in
subnam, where subnam names a subroutine, function, or block data subprogram.
4. Type therun
command, which runs the program in the executable files named when you starteddbx
.
(dbx)
run
Running: my_programstopped in MAIN at line 3 in file "a1.f"3 call mkidentity( twobytwo, n )
- When the breakpoint is reached,
dbx
displays a message showing where it stopped--in this case, at line 3 of thea1.f
file.5. To print a value, type the
- Print value of
n
:
(dbx)
print n
n = 2- Print the matrix
twobytwo
; the format might vary:
(dbx)
print twobytwo
twobytwo =(1,1) -1.0(2,1) -1.0(1,2) -1.0(2,2) -1.0- Print the matrix
array
:
(dbx)
print array
dbx: "array" is not defined in the current scope(dbx)
- The print fails because
array
is not defined here--only inmkidentity
.6. To advance execution to the next line, type thenext
command.
- Advance execution to the next line:
(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
next
command executes the current source line and stops at the next line. It counts subprogram calls as single statements.- Compare the
next
command with thestep
command. Thestep
command executes the next source line or the next step into a subprogram. If the next executable source statement is a subroutine or function call, then:
- The
step
command sets a breakpoint at the first source statement of the subprogram.- The
next
command sets the breakpoint at the first source statement after the call, but still in the calling program.7. To quitdbx
, type thequit
command.
(dbx)
quit
demo%
Debugging Segmentation Faults
If a program gets a segmentation fault (
SIGSEGV
), it references a memory address outside of the memory available to it.The most frequent causes for a segmentation fault are:
- An array index is outside the declared range.
- The name of an array index is misspelled.
- The calling routine has a
REAL
argument, which the called routine has asINTEGER
.- An array index is miscalculated.
- The calling routine has fewer arguments than required.
- A pointer is used before it has been defined.
Using
dbx
to Locate ProblemsUse
dbx
to find the source code line where a segmentation fault has occurred.Use a program to generate a segmentation fault:
demo%
cat WhereSEGV.f
INTEGER a(5)j = 2000000DO 9 i = 1,5a(j) = (i * 10)9 CONTINUEPRINT *, aENDdemo%
Use
dbx
to find the line number of adbx
segmentation 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.
Compiling with
-ftrap=common
forces trapping on all common exceptions.To find where an exception occurred:
Tracing Calls
Sometimes a program stops with a core dump, and you need to know the sequence of calls that led it there. This sequence is called a stack trace.
The
where
command shows where in the program flow execution stopped and how execution reached this point--a stack trace of the called routines.
ShowTrace.f
is a program contrived to get a core dump a few levels deep in the call sequence--to show a stack trace.Show the sequence of calls, starting at where the execution stopped:
Working With Arrays
dbx
recognizes arrays and can print them.
For information on array slicing in Fortran, see Array Slicing Syntax for Fortran.
Fortran 95 Allocatable Arrays
The following example shows how to work with allocated arrays in
dbx
.
Showing Intrinsic Functions
dbx
recognizes Fortran intrinsic functions.To show an intrinsic function in
dbx
, type:
Showing Complex Expressions
dbx
also recognizes Fortran complex expressions.To show a complex expression in
dbx
, type:
Showing Logical Operators
dbx
can locate Fortran logical operators and print them.To show logical operators in
dbx
, type:
Viewing Fortran 95 Derived Types
You can show structures--Fortran 95 derived types--with
dbx
.
(dbx)
print prod1
prod1 = (id = 82name = 'Coffee Cup'model = 'XL'cost = 24.0price = 104.0)Pointer to Fortran 95 Derived Type
You can show structures--Fortran 95 derived types--and pointers with
dbx
.
Above,
dbx
displays all fields of the derived type, including field names.You can use structures--inquire about an item of an Fortran 95 derived type.
To print a pointer, type:
Sun Microsystems, Inc. Copyright information. All rights reserved. Feedback |
Library | Contents | Previous | Next | Index |