C H A P T E R 16 |
Debugging Fortran Using dbx |
This chapter introduces dbx features you might use with Fortran. Sample requests to dbx are also included to provide you with assistance when debugging Fortran code using dbx.
This chapter includes the following topics:
The following tips and general concepts are provided to help you while debugging Fortran programs. For information on debugging Fortran OpenMP code with dbx, see Chapter 12.
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.
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 95 and dbx must be in the same case-sensitive or case-insensitive mode:
Note - File or directory names are always case-sensitive in dbx, even if you have set the dbx input_case_sensitive environment variable to false. |
The following examples use a sample program called my_program.
Main program for debugging, a1.f:
PARAMETER ( n=2 ) REAL twobytwo(2,2) / 4 *-1 / CALL mkidentity( twobytwo, n ) PRINT *, determinant( twobytwo ) END |
Subroutine for debugging, 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 |
REAL FUNCTION determinant ( a ) REAL a(2,2) determinant = a(1,1) * a(2,2) - a(1,2) / a(2,1) RETURN END |
1. Compile and link with the -g option.
You can do this in one or two steps.
Compile and link in one step, with -g:
demo% f95 -o my_program -g a1.f a2.f a3.f |
Or, compile and link in separate steps:
demo% f95 -c -g a1.f a2.f a3.f demo% f95 -o my_program a1.o a2.o a3.o |
2. Start dbx on the executable named my_program.
demo% dbx my_program Reading symbolic information... |
3. Set a simple breakpoint by typing stop in subnam, where subnam names a subroutine, function, or block data subprogram.
To stop at the first executable statement in a main program.
(dbx) stop in MAIN (2) stop in MAIN |
Although MAIN must be all uppercase, subnam can be uppercase or lowercase.
4. Type the run command, which runs the program in the executable files named when you started dbx.
(dbx) run Running: my_program stopped 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 the a1.f file.
5. To print a value, type the print command.
(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 |
(dbx) print array dbx: "array" is not defined in the current scope (dbx) |
The print fails because array is not defined here--only in mkidentity.
6. To advance execution to the next line, type the next 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 the step command. The step 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:
7. To quit dbx, type the quit command.
(dbx)quit demo% |
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:
Use 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 = 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 dbx segmentation fault:
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:
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:
dbx recognizes arrays and can print them.
For information on array slicing in Fortran, see Array Slicing Syntax for Fortran.
The following example shows how to work with allocated arrays in dbx.
dbx recognizes Fortran intrinsic functions (SPARC platforms only).
To show an intrinsic function in dbx, type:
dbx also recognizes Fortran complex expressions.
To show a complex expression in dbx, type:
To show an interval expression in dbx, type:
dbx can locate Fortran logical operators and print them.
To show logical operators in dbx, type:
You can show structures--Fortran 95 derived types--with dbx.
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.
Copyright © 2003, Sun Microsystems, Inc. All rights reserved.