| Debugging a Program With dbx |
Debugging Fortran Using
dbxThis chapter introduces
dbxfeatures you might use with Fortran. Sample requests todbxare 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,
dbxdefines 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,stopat5sets different breakpoints, depending on which file is current.Uppercase Letters
If your program has uppercase letters in any identifiers,
dbxrecognizes them. You need not provide case-sensitive or case-insensitive commands, as in some earlier versions.FORTRAN 77, Fortran 95, and
dbxmust be in the same case-sensitive or case-insensitive mode:
- Compile and debug in case-insensitive mode without the
-Uoption. The default value of thedbxinput_case_sensitiveenvironment variable is thenfalse.
- If the source has a variable named
LAST, then indbx, both theLASTorlastcommands work. FORTRAN 77, Fortran 95, anddbxconsiderLASTandlastto be the same, as requested.
- Compile and debug in case-sensitive mode using
-U. The default value of thedbxinput_case_sensitiveenvironment variable is thentrue.
- If the source has a variable named
LASTand one namedlast, then indbx,LASTworks, butlastdoes not work. FORTRAN 77, Fortran 95, anddbxdistinguish betweenLASTandlast, as requested.
Note File or directory names are always case-sensitive indbx, even if you have set thedbxinput-_case_sensitiveenvironment variable tofalse.
Optimized Programs
- Compile the main program with
-gbut without-On.- Compile every other routine of the program with the appropriate
-On.- Start the execution under
dbx.- Use the
fix -gany.fcommand on the routine you want to debug, but without-On.- Use the
contcommand 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
dbxSessionThe following examples use a sample program called
my_program.1. Compile and link with the-goption.
2. Startdbxon the executable namedmy_program.
demo%dbx my_programReading symbolic information...3. Set a simple breakpoint by typingstopinsubnam, where subnam names a subroutine, function, or block data subprogram.
4. Type theruncommand, which runs the program in the executable files named when you starteddbx.
(dbx)runRunning: my_programstopped in MAIN at line 3 in file "a1.f"3 call mkidentity( twobytwo, n )
- When the breakpoint is reached,
dbxdisplays a message showing where it stopped--in this case, at line 3 of thea1.ffile.5. To print a value, type the
- Print value of
n:
(dbx)print nn = 2- Print the matrix
twobytwo; the format might vary:
(dbx)print twobytwotwobytwo =(1,1) -1.0(2,1) -1.0(1,2) -1.0(2,2) -1.0- Print the matrix
array:
(dbx)print arraydbx: "array" is not defined in the current scope(dbx)- The print fails because
arrayis not defined here--only inmkidentity.6. To advance execution to the next line, type thenextcommand.
- Advance execution to the next line:
(dbx)nextstopped in MAIN at line 4 in file "a1.f"4 print *, determinant( twobytwo )(dbx)print twobytwotwobytwo =(1,1) 1.0(2,1) 0.0(1,2) 0.0(2,2) 1.0(dbx)quitdemo%- The
nextcommand executes the current source line and stops at the next line. It counts subprogram calls as single statements.- Compare the
nextcommand with thestepcommand. Thestepcommand 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
stepcommand sets a breakpoint at the first source statement of the subprogram.- The
nextcommand sets the breakpoint at the first source statement after the call, but still in the calling program.7. To quitdbx, type thequitcommand.
(dbx)quitdemo%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
REALargument, 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
dbxto Locate ProblemsUse
dbxto find the source code line where a segmentation fault has occurred.Use a program to generate a segmentation fault:
demo%cat WhereSEGV.fINTEGER a(5)j = 2000000DO 9 i = 1,5a(j) = (i * 10)9 CONTINUEPRINT *, aENDdemo%Use
dbxto find the line number of adbxsegmentation 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=commonforces 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
wherecommand shows where in the program flow execution stopped and how execution reached this point--a stack trace of the called routines.
ShowTrace.fis 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
dbxrecognizes 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
dbxrecognizes Fortran intrinsic functions.To show an intrinsic function in
dbx, type:
Showing Complex Expressions
dbxalso recognizes Fortran complex expressions.To show a complex expression in
dbx, type:
Showing Logical Operators
dbxcan 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 prod1prod1 = (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,
dbxdisplays 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 |