|Fortran Programming Guide|
Program Analysis and Debugging
This chapter presents a number of Sun Fortran compiler features that facilitate program analysis and debugging.
Global Program Checking (
-Xlistoptions provide a valuable way to analyze a source program for inconsistencies and possible runtime problems. The analysis performed by the compiler is global, across subprograms.
-Xlistreports errors in alignment, agreement in number and type for subprogram arguments, common block, parameter, and various other kinds of errors.
-Xlistalso can be used to make detailed source code listings and cross-reference tables.
Note Not all the
-Xlistsuboptions are available with
Global program checking (GPC), invoked by the
-Xlistx option, does the following:
- Enforces type-checking rules of Fortran more stringently than usual, especially between separately compiled routines
- Enforces some portability restrictions needed to move programs between different machines or operating systems
- Detects legal constructions that nevertheless might be suboptimal or error-prone
- Reveals other potential bugs and obscurities
In particular, global checking reports problems such as:
- Interface problems
- Conflicts in number and type of dummy and actual arguments
- Wrong types of function values
- Possible conflicts due to data type mismatches in common blocks between different subprograms
- Usage problems
- Function used as a subroutine or subroutine used as a function
- Declared but unused functions, subroutines, variables, and labels
- Referenced but not declared functions, subroutines, variables, and labels
- Usage of unset variables
- Unreachable statements
- Implicit type variables
- Inconsistency of the named common block lengths, names, and layouts
How to Invoke Global Program Checking
-Xlistoption on the command line invokes the compiler's global program analyzer. There are a number of suboptions, as described in the sections that follow.
In the preceding example, the compiler:
Normally, output listings produced by
-Xlistx are written to a file. To display directly to the screen, use
-Xlistoto write the output file to
Default Output Features
-Xlistoption provides a combination of features available for output. With no other
-Xlistoptions, you get the following by default:
- The listing file name is taken from the first input source or object file that appears, with the extension replaced by
- A line-numbered source listing
- Error messages (embedded in listing) for inconsistencies across routines
- Cross-reference table of the identifiers
- Pagination at 66 lines per page and 79 columns per line
- No call graph
- No expansion of
The checking process recognizes all the files in the compiler command line that end in
.ofiles supply the process with information regarding only global names, such as subroutine and function names.
Analysis Files (
Programs compiled with
-Xlistoptions have their analysis data built into the binary files automatically. This enables global program checking over programs in libraries.
Alternatively, the compiler will save individual source file analysis results into files with a
.flnsuffix if the
-Xlistflndir option is also specified. dir indicates the directory to receive these files.
f77 -Xlistfln/tmp *.f
Some Examples of
-Xlistand Global Program Checking
Here is a listing of the
Repeat.fsource code used in the following examples:
cat Repeat.fPROGRAM repeatpn1 = REAL( LOC ( rp1 ) )CALL subr1 ( pn1 )CALL nwfrk ( pn1 )PRINT *, pn1END ! PROGRAM repeatSUBROUTINE subr1 ( x )IF ( x .GT. 1.0 ) THENCALL subr1 ( x * 0.5 )END IFENDSUBROUTINE nwfrk( ix )EXTERNAL forkINTEGER prnok, forkPRINT *, prnok ( ix ), fork ( )ENDINTEGER FUNCTION prnok ( x )prnok = INT ( x ) + LOC(x)ENDSUBROUTINE unreach_sub()CALL sleep(1)END
-XlistEto show errors and warnings:
f77 -XlistE -silent Repeat.fdemo%
cat Repeat.lstFILE "Repeat.f"program repeat4 CALL nwfrk ( pn1 )^**** ERR #418: argument "pn1" is real, but dummy argument isinteger*4See: "Repeat.f" line #144 CALL nwfrk ( pn1 )^**** ERR #317: variable "pn1" referenced as integer*4 acrossrepeat/nwfrk//prnok in line #21 but set as realby repeat in line #2subroutine subr110 CALL subr1 ( x * 0.5 )^**** WAR #348: recursive call for "subr1". See dynamic calls:"Repeat.f" line #3subroutine nwfrk17 PRINT *, prnok ( ix ), fork ( )^**** ERR #418: argument "ix" is integer*4, but dummy argumentis realSee: "Repeat.f" line #20subroutine unreach_sub24 SUBROUTINE unreach_sub()^**** WAR #338: subroutine "unreach_sub" isn't called from programDate: Wed Feb 24 10:40:32 1999Files: 2 (Sources: 1; libraries: 1)Lines: 26 (Sources: 26; Library subprograms:2)Routines: 5 (MAIN: 1; Subroutines: 3; Functions: 1)Messages: 5 (Errors: 3; Warnings: 2)demo%
Compiling the same program with
-Xlistalso produces a cross-reference table on standard output:
C R O S S R E F E R E N C E T A B L ESource file: Repeat.fLegend:D Definition/DeclarationU Simple useM Modified occurrenceA Actual argumentC Subroutine/Function callI Initialization: DATA or extended declarationE Occurrence in EQUIVALENCEN Occurrence in NAMELISTP R O G R A M F O R MProgram-------repeat <repeat> D 1:DFunctions and Subroutines-------------------------fork int*4 <nwfrk> DC 15:D 16:D 17:Cint intrinsic<prnok> C 21:Cloc intrinsic<repeat> C 2:C<prnok> C 21:Cnwfrk <repeat> C 4:C<nwfrk> D 14:Dprnok int*4 <nwfrk> DC 16:D 17:C<prnok> DM 20:D 21:Mreal intrinsic<repeat> C 2:Csleep <unreach_sub> C 25:Csubr1 <repeat> C 3:C<subr1> DC 8:D 10:Cunreach_sub <unreach_sub> D 24:D
Output from compiling
f77 -Xlist Repeat.f(Continued)
Variables and Arrays--------------------ix int*4 dummy<nwfrk> DA 14:D 17:Apn1 real*4 <repeat> UMA 2:M 3:A 4:A 5:Urp1 real*4 <repeat> A 2:Ax real*4 dummy<subr1> DU 8:D 9:U 10:U<prnok> DUA 20:D 21:A 21:U----------------------------------------------------------------------Date: Tue Feb 22 13:15:39 1995Files: 2 (Sources: 1; libraries: 1)Lines: 26 (Sources: 26; Library subprograms:2)Routines: 5 (MAIN: 1; Subroutines: 3; Functions: 1)Messages: 5 (Errors: 3; Warnings: 2)demo%
In the cross-reference table in the preceding example:
ixis a 4-byte integer:
- Used as an argument in the routine
- At line 14, used as a declaration of argument
- At line 17, used as an actual argument
pn1is a 4-byte real in the routine
rp1is a 4-byte real in the routine,
repeat. At line 2, it is an argument.
xis a 4-byte real in the routines
Suboptions for Global Checking Across Routines
The basic global cross-checking option is
-Xlistwith no suboption. It is a combination of suboptions, each of which could have been specified separately.
The following sections describe options for producing the listing, errors, and cross-reference table. Multiple suboptions may appear on the command line.
Add suboptions according to the following rules:
- Append the suboption to
- Put no space between the
-Xlistand the suboption.
- Use only one suboption per
-Xlistand its Suboptions
Combine suboptions according to the following rules:
- The most general option is
-Xlist(listing, errors, cross-reference table).
- Specific features can be combined using
-XlistL, or -
- Other suboptions specify further details.
f77 -Xlistc any.f
The following table shows the reports generated by these basic
Errors, listing, cross-reference
Errors and source listing only
Errors and cross-reference table only
Errors and call graph only
The following table summarizes all
TABLE 5-2 Summary of
Shows errors, listing, and cross-reference table
Shows call graphs and errors (f77 only)
Suppresses error nnn in the verification report
Produces fast output
.flnfiles in dir (f77 only)
Shows errors from cross-checking stop compilation (f77 only)
Lists and cross-checks
Shows the listing and errors
Sets page breaks
-Xlistoutput report file
Suppresses unreferenced symbols from cross-reference (f77 only)
Sets checking "strictness" level (f77 only)
Sets the width of output lines (f77 only)
Suppresses warning nnn in the report
Shows just the cross-reference table and errors
This section describes the
-Xlistsuboptions. As noted, some are only available with
f77: -Xlistc-- Show call graphs and cross-routine errors
-Xlistcdoes not show a listing or cross-reference. It produces the call graph in a tree form, using printable characters. If some subroutines are not called from
MAIN, more than one graph is shown. Each
BLOCKDATAis printed separately with no connection to
The default is not to show the call graph.
-XlistE- Show cross-routine errors
-XlistEshows only cross-routine errors and does not show a listing or a cross-reference.
-Xlisterr[nnn] - Suppress error nnn
-Xlisterrto suppress a numbered error message from the listing or cross-reference.
-Xlisterr338suppresses error message 338. If nnn is not specified, all error messages are suppressed. To suppress additional specific errors, use this option repeatedly.
-Xlistf- Produce faster output
-Xlistfto produce source file listings and a cross-checking report and to verify sources, but without generating object files.
The default without this option is to generate object files.
Xlistflndir - Put
.flnfiles into dir directory
-Xlistflnto specify the directory to receive
.flnsource analysis files. The directory specified (dir) must already exist. The default is to include the source analysis information directly within the object
.ofiles (and not generate
f77: -Xlisth- Halt on errors
-Xlisth, compilation stops if errors are detected while cross-checking the program. In this case, the report is redirected to
stdoutinstead of the
-XlistI- List and cross-check include files
-XlistIis the only suboption used,
includefiles are shown or scanned along with the standard
-Xlistoutput (line numbered listing, error messages, and a cross-reference table).
- Listing--If the listing is not suppressed, then the
includefiles are listed in place. Files are listed as often as they are included. The files are:
- Cross-Reference Table--If the cross reference table is not suppressed, the following files are all scanned while the cross reference table is generated:
-XlistL- Show listing and cross routine errors
-XlistLto produce only a listing and a list of cross routine errors. This suboption by itself does not show a cross reference table. The default is to show the listing and cross reference table.
-Xlistln - Set the page length for pagination to n lines
-Xlistlto set the page length to something other than the default page size. For example,
-Xlistl45sets the page length to 45 lines. The default is 66.
With n=0 (
-Xlistl0)this option shows listings and cross-references with no page breaks for easier on-screen viewing.
-Xlistoname - Rename the
-Xlistoutput report file
-Xlistoto rename the generated report output file. (A space between
oand name is required.) With
-Xlisto name,the output is to
To display directly to the screen, use the command:
f77: -Xlists- Suppress unreferenced identifiers
-Xliststo suppress from the cross reference table any identifiers defined in the
includefiles but not referenced in the source files.
This suboption has no effect if the suboption
The default is not to show the occurrences in
f77: -Xlistvn - Set level of checking strictness
4. The default is
- Shows the cross-checked information of all names in summary form only, with no line numbers. This is the lowest level of checking strictness--syntax errors only.
- Shows cross-checked information with summaries and line numbers. This is the default level of checking strictness and includes argument inconsistency errors and variable usage errors.
- Shows cross-checking with summaries, line numbers, and common block maps. This is a high level of checking strictness and includes errors caused by incorrect usage of data types in common blocks in different subprograms.
- Shows cross-checking with summaries, line numbers, common block maps, and equivalence block maps. This is the strictest level of checking with maximum error detection.
f77: -Xlistw[nnn] - Set width of output line to n columns
-Xlistwto set the width of the output line. For example,
-Xlistw132sets the page width to 132 columns. The default is 79.
-Xlistwar[nnn] - Suppress warning nnn in the report
-Xlistwarto suppress a specific warning message from the output reports. If nnn is not specified, then all warning messages are suppressed from printing. For example,
-Xlistwar338suppresses warning message number 338. To suppress more than one, but not all warnings, use this option repeatedly.
-XlistX- Show cross-reference table and cross routine errors
-XlistXproduces a cross reference table and cross routine error list but no source listing.
Some Examples Using Suboptions
-Xlistwarnnn to suppress two warnings from a preceding example:
f77 -Xlistwar338 -Xlistwar348 -XlistE -silent Repeat.fdemo%
cat Repeat.lstFILE "Repeat.f"program repeat4 CALL nwfrk ( pn1 )^**** ERR #418: argument "pn1" is real, but dummy argument isinteger*4See: "Repeat.f" line #144 CALL nwfrk ( pn1 )^**** ERR #317: variable "pn1" referenced as integer*4 acrossrepeat/nwfrk//prnok in line #21 but set as realby repeat in line #2subroutine nwfrk17 PRINT *, prnok ( ix ), fork ( )^**** ERR #418: argument "ix" is integer*4, but dummy argumentis realSee: "Repeat.f" line #20Date: Wed Feb 24 10:40:32 1999Files: 2 (Sources: 1; libraries: 1)Lines: 26 (Sources: 26; Library subprograms:2)Routines: 5 (MAIN: 1; Subroutines: 3; Functions: 1)Messages: 5 (Errors: 3; Warnings: 2)demo%
Example: Explain a message and find a type mismatch in program
cat ShoGetc.fCHARACTER*1 ci = getc(c)ENDdemo%
f77 -silent ShoGetc.fCompile program
a.outProgram waits for input...
ZType "Z" on keyboard. This causes run-time message. Why?
Note: IEEE floating-point exception flags raised:Invalid Operation;See the Numerical Computation Guide, ieee_flags(3M) demo%
f77 -XlistE -silent ShoGetc.fCompile with Global Program Checkingdemo%
cat ShoGetc.lstand view listingFILE "ShoGetc.f"program MAIN2 i = getc(c)^**** WAR #320: variable "i" set but never referenced2 i = getc(c)^**** ERR #412: function "getc" used as real but declared asinteger*4Here is the error - function must be declared INTEGER.2 i = getc(c)^**** WAR #320: variable "c" set but never referenced
cat ShoGetc.fModify program to declare getc INTEGER and run again.CHARACTER*1 cINTEGER getci = getc(c)ENDdemo%
f77 -silent ShoGetc.fdemo%
ZType "Z" on keyboard
demo% Now no error.
Special Compiler Options
Some compiler options are useful for debugging. They check subscripts, spot undeclared variables, show stages of the compile-link sequence, display versions of software, and so on.
The Solaris linker has additional debugging aids. See
ld(1), or run the command
-Dhelpat a shell prompt to see the online documentation.
Subscript Bounds (
-Coption adds checks for out-of-bounds array subscripts.
If you compile with
-C, the compiler adds checks at runtime for out-of-bounds references on each array subscript. This action helps catch some situations that cause segmentation faults.
Example: Index out of range:
cat indrange.fREAL a(10,10)k = 11a(k,2) = 1.0ENDdemo%
f77-C -silent indrange.fdemo%
a.outSubscript out of range on file indrange.f, line 3, procedure MAIN.Subscript number 1 has value 11 in array a.Abort (core dumped)demo%
Undeclared Variable Types (
-uoption checks for any undeclared variables.
-uoption causes all variables to be initially identified as undeclared, so that all variables that are not explicitly declared by type statements, or by an
IMPLICITstatement, are flagged with an error. The
-uflag is useful for discovering mistyped variables. If
-uis set, all variables are treated as undeclared until explicitly declared. Use of an undeclared variable is accompanied by an error message.
Version Checking (
-Voption causes the name and version ID of each phase of the compiler to be displayed. This option can be useful in tracking the origin of ambiguous error messages and in reporting compiler failures, and to verify the level of installed compiler patches.
Interactive Debugging With
dbxand Sun WorkShop
The Sun WorkShop provides a tightly integrated development environment for building and browsing, as well as debugging applications written in Fortran, C, and C++.
The Sun WorkShop debugging facility is a window-based interface to
dbxitself is an interactive, line-oriented, source-level symbolic debugger. Either can be used to determine where a program crashed, to view or trace the values of variables and expressions in a running code, and to set breakpoints.
Sun WorkShop adds a sophisticated graphical environment to the debugging process that is integrated with tools for editing, building, and source code version control. It includes a data visualization capability to display and explore large and complex datasets, simulate results, and interactively steer computations.
For details, see the Sun manual Debugging a Program With Sun WorkShop, and the
dbx(1) man pages.
dbxprogram provides event management, process control, and data inspection. You can watch what is happening during program execution, and perform the following tasks:
- Fix one routine, then continue executing without recompiling the others
- Set watchpoints to stop or trace if a specified item changes
- Collect data for performance tuning
- Graphically monitor variables, structures, and arrays
- Set breakpoints (set places to halt in the program) at lines or in functions
- Show values--once halted, show or modify variables, arrays, structures
- Step through a program, one source or assembly line at a time
- Trace program flow--show sequence of calls taken
- Invoke procedures in the program being debugged
- Step over or into function calls; step up and out of a function call
- Run, stop, and continue execution at the next line or at some other line
- Save and then replay all or part of a debugging run
- Examine the call stack, or move up and down the call stack
- Program scripts in the embedded Korn shell
- Follow programs as they fork(2) and exec(2)
To debug optimized programs, use the
dbx fixcommand to recompile the routines you want to debug:1. Compile the program with the appropriate
-On optimization level.2. Start the execution under
.fwithout optimization on the routine you want to debug.4. Use
continuewith that routine compiled.
Some optimizations will be inhibited by the presence of
-gon the compilation command. See the
dbxdocumentation for details.
f77: Viewing Compiler Listing Diagnostics
errorutility program to view compiler diagnostics merged with the source code.
errorinserts compiler diagnostics above the relevant line in the source file. The diagnostics include the standard compiler error and warning messages, but not the
-Xlisterror and warning messages.
errorutility rewrites your source files and does not work if the source files are read-only, or are in a read only directory.
error(1) is included as part of a "developer" installation of the Solaris operating environment; it can also be installed from the package,
Facilities also exist in the Sun WorkShop for viewing compiler diagnostics. See Introduction to Sun WorkShop.
Sun Microsystems, Inc.
Copyright information. All rights reserved.
|Library | Contents | Previous | Next | Index|