| Debugging a Program With dbx |
Viewing and Visiting Code
Each time the program you are debugging stops,
dbxprints the source line associated with the stop location. At each program stop,dbxresets the value of the current function to the function in which the program is stopped. Before the program starts running and when it is stopped, you can move to, or visit, functions and files elsewhere in the program.This chapter describes how
dbxnavigates through code and locates functions and symbols. It also covers how to use commands to visit code or look up declarations for identifiers, types, and classes.This chapter is organized into the following sections
- Mapping to the Location of the Code
- Visiting Code
- Qualifying Symbols With Scope Resolution Operators
- Locating Symbols
- Viewing Variables, Members, Types, and Classes
- Using the Auto-Read Facility
Mapping to the Location of the Code
dbxmust know the location of the source and object code files associated with a program. The default directory for the object files is the directory the files were in when the program was last linked. The default directory for the source files is the one they were in when last compiled. If you move the source or object files, or copy them to a new location, you must either relink the program or change to the new location before debugging.If you move the source or object files, you can add their new location to the search path. The
pathmapcommand creates a mapping from your current view of the file system to the name in the executable image. The mapping is applied to source paths and object file paths.To establish a new mapping from the directory from to the directory to:
(dbx)pathmap [-c]from toIf
-cis used, the mapping is applied to the current working directory as well.The
pathmapcommand is also useful for dealing with automounted and explicit NFS mounted file systems with different base paths on differing hosts. Use-cwhen you try to correct problems due to the automounter because current working directories are inaccurate on automounted file systems.The mapping of
/tmp_mntto/exists by default.For more information, see "pathmap Command" in the Using dbx Commands section of the Sun WorkShop online help.
Visiting Code
You can visit code elsewhere in the program any time the program is not running. You can visit any function or file that is part of the program.
Visiting a File
You can visit any file
dbxrecognizes as part of the program (even if a module or file was not compiled with the-goption). Visiting a file does not change the current function. To visit a file:
(dbx)filefilenameUsing the
filecommand by itself echoes the file you are currently visiting.
(dbx)file
dbxdisplays the file from its first line unless you specify a line number.
(dbx)filefilename ;listline_numberVisiting Functions
You can use the
funccommand to visit a function. To visit a function, type the commandfuncfollowed by the function name. For example:
(dbx)func adjust_speedThe
funccommand by itself echoes the currently visited function.Selecting From a List of C++ Ambiguous Function Names
If you try to visit a C++ member function with an ambiguous name or an overloaded function name, a list is displayed, showing all functions with the overloaded name.
If the specified function is ambiguous, type the number of the function you want to visit. If you know which specific class a function belongs to, you can type:
(dbx)func block::blockChoosing Among Multiple Occurrences
If multiple symbols are accessible from the same scope level,
dbxprints a message reporting the ambiguity.
(dbx)func main(dbx)which C::fooMore than one identifier 'foo'.Select one of the following:0) Cancel1) `a.out`t.cc`C::foo(int)2) `a.out`t.cc`C::foo()>1`a.out`t.cc`C::foo(int)In the context of the
whichcommand, choosing from the list of occurrences does not affect the state ofdbxor the program. Whichever occurrence you choose,dbxechoes the name.The
whichcommand tells you which symboldbxwould choose. In the case of ambiguous names, the overload display list indicates thatdbxhas not yet determined which occurrence of two or more names it would use.dbxlists the possibilities and waits for you to choose one.For more information, see "func Command" in the Using dbx commands section of the Sun WorkShop online help.
Printing a Source Listing
Use the
listcommand to print the source listing for a file or function. Once you visit a file,listprints number lines from the top. Once you visit a function,listprints its lines.
(dbx)list [-i | -instr] [+] [-]number[function|filename]For more information on the
listcommand, see "list Command" in the Using dbx Commands section of the Sun WorkShop online help.Walking the Call Stack to Visit Code
Another way to visit code when a live process exists is to "walk the call stack," using the stack commands to view functions currently on the stack.
Walking the stack causes the current function and file to change each time you display a stack function. The stop location is considered to be at the "bottom" of the stack, so to move away from it, use the
upcommand, that is, move toward themainorbeginfunction. Use thedowncommand to move toward the current frame.For more information on walking the call stack, see Walking the Stack and Returning Home.
Qualifying Symbols With Scope Resolution Operators
When using the
funcorfilecommand, you might need to use scope resolution operators to qualify the names of the functions that you give as targets.
dbxprovides three scope resolution operators with which to qualify symbols: the backquote operator (`), the C++ double colon operator (::), and the block local operator (:lineno). You use them separately or, in some cases, together.In addition to qualifying file and function names when visiting code, symbol name qualifying is also necessary for printing and displaying out-of-scope variables and expressions, and for displaying type and class declarations (
whatiscommand). The symbol qualifying rules are the same in all cases; this section covers the rules for all types of symbol name qualifying.Backquote Operator
Use the backquote character (
`) to find a variable of global scope:
(dbx)print `itemA program can use the same function name in two different files (or compilation modules). In this case, you must also qualify the function name to
dbxso that it registers which function you will visit. To qualify a function name with respect to its file name, use the general purpose backquote (`) scope resolution operator.
(dbx)func `file_name`function_nameC++ Double Colon Scope Resolution Operator
Use the double colon operator (
::) to qualify a C++ member function or top level function with:
- An overloaded name (same name used with different argument types)
- An ambiguous name (same name used in different classes)
You might want to qualify an overloaded function name. If you do not qualify it,
dbxdisplays an overload list so you can choose which function you will visit. If you know the function class name, you can use it with the double colon scope resolution operator to qualify the name.
(dbx)funcclass::function_name(args)For example, if
handis the class name anddrawis the function name, type:
(dbx)funchand::drawBlock Local Operator
The block local operator (:lineno) is used in conjunction with the backquote operator. It identifies the line number of an expression that references the instance you're interested in.
In the following example,
:230is the block local operator.
(dbx)stop in `animate.o`change_glyph:230`itemLinker Names
dbxprovides a special syntax for looking up symbols by their linker names (mangled names in C++). Prefix the symbol name with a # (pound sign) character (use the ksh escape character \ (backslash) before any $ (dollar sign) characters), as in these examples:
(dbx)stop in #.mul(dbx)whatis #\$FEcopyPc(dbx)print `foo.c`#staticvarScope Resolution Search Path
When you issue a debugging command with a symbol target name, the search order is as follows:
- Within the scope of the current function. If the program is stopped in a nested block,
dbxsearches within that block, then in the scope of all enclosing blocks.- For C++ only: class members of the current function`s class and its base class.
- For C++ only: the current name space.
- The immediately enclosing "compilation unit," generally, the file containing the current function.
- The LoadObject scope.
- The global scope.
- If none of the above searches are successful,
dbxassumes you are referencing a private, or file static, variable or function.dbxoptionally searches for a file static symbol in every compilation unit depending on the value of thedbxenvsettingscope_look_aside.
dbxuses whichever occurrence of the symbol it first finds along this search path. Ifdbxcannot find a variable, it reports an error.Locating Symbols
In a program, the same name might refer to different types of program entities and occur in many scopes. The
dbxwhereiscommand lists the fully qualified name--hence, the location--of all symbols of that name. Thedbxwhichcommand tells you which occurrence of a symboldbxuses if you give that name as the target of a debugging command.Printing a List of Occurrences of a Symbol
To print a list of all the occurrences of a specified symbol, use
whereissymbol, where symbol can be any user-defined identifier. For example:
The output includes the name of the loadable object(s) where the program defines symbol, as well as the kind of entity each object is: class, function, or variable.
Because information from the
dbxsymbol table is read in as it is needed, thewhereiscommand registers only occurrences of a symbol that are already loaded. As a debugging session gets longer, the list of occurrences can grow.For more information, see "whereis Command" in the Using dbx Commands section of the Sun WorkShop online help.
Determining Which Symbol
dbxUsesThe
whichcommand tells you which symbol with a given namedbxuses if you specify that name (without fully qualifying it) as the target of a debugging command. For example:
(dbx)funcwedge::wedge(char*, int, int, const point&, load_bearing_block*)(dbx)which draw`block_draw.cc`wedge::draw(unsigned long)If a specified symbol name is not in a local scope, the
whichcommand searches for the first occurrence of the symbol along the scope resolution search path. Ifwhichfinds the name, it reports the fully qualified name.If at any place along the search path, the search finds multiple occurrences of symbol at the same scope level,
dbxprints a message in the command pane reporting the ambiguity.
(dbx)which fidMore than one identifier 'fid'.Select one of the following:0) Cancel1) `example`file1.c`fid2) `example`file2.c`fid
dbxshows the overload display, listing the ambiguous symbols names. In the context of thewhichcommand, choosing from the list of occurrences does not affect the state ofdbxor the program. Whichever occurrence you choose,dbxechoes the name.The
whichcommand gives you a preview of what happens if you make symbol (in this example,block) an argument of a command that must operate on symbol (for example, adbxdoes not yet register which occurrence of two or more names it uses.dbxlists the possibilities and waits for you to choose one.Viewing Variables, Members, Types, and Classes
The
whatiscommand prints the declarations or definitions of identifiers, structs, types and C++ classes, or the type of an expression. The identifiers you can look up include variables, functions, fields, arrays, and enumeration constants.For more information, see "whatis Command" and "whatis Command Used With C++" in the Using dbx Commands section of the Sun WorkShop online help.
Looking Up Definitions of Variables, Members, and Functions
To print out the declaration of an identifier, type:
(dbx)whatisidentifierQualify the identifier name with file and function information as needed.
For C++,
whatisidentifier lists function template instantiations. Template definitions are displayed withwhatis -tidentifier. See Looking Up Definitions of Types and Classes.To print out the member function, type
:
To print out the data member, type:
(dbx)whatis block::movableint movable;On a variable,
.whatistells you the variable`s type
(dbx)whatis the_tableclass table *the_table;On a field,
whatisgives the field`s type.
(dbx)whatis the_table->drawvoid table::draw(unsigned long pw);When you are stopped in a member function, you can look up the
thispointer. In this example, the output from thewhatisshows that the compiler automatically allocated this variable to a register.
Looking Up Definitions of Types and Classes
The
-toption of thewhatiscommand displays a type. For C++, the list displayed bywhatis -tincludes template definitions and class template instantiations.To print the declaration of a type or C++ class, type:
(dbx)whatis -ttype_or_class_nameTo see inherited members, the
whatiscommand takes an-roption (for recursive) that displays the declaration of a specified class together with the members it inherits from parent classes.
(dbx)whatis -t -rclass_nameThe output from a
whatis -rquery may be long, depending on the class hierarchy and the size of the classes. The output begins with the list of members inherited from the most ancestral class. The inserted comment lines separate the list of members into their respective parent classes.Here are two examples, using the class
table, a child class of the parent classload_bearing_block, which is, in turn, a child class ofblock.Without
-r,whatisreports the members declared in classtable:
Here are results when
whatis -ris used on a child class to see members it inherits:
Using the Auto-Read Facility
In general, compile the entire program you want to debug using the
-goption. Depending on how the program was compiled, the debugging information generated for each program and shared library module is stored in either the object code file (.ofile) for each program and shared library module, or the program executable file.When you compile with the
-g -ccompiler option, debugging information for each module remains stored in its.ofile.dbxthen reads in debugging information for each module automatically, as it is needed, during a session. This read-on-demand facility is called Auto-Read. Auto-Read is the default fordbx.Auto-Read saves considerable time when yau are loading a large program into
dbx. Auto-Read depends on the continued presence of the program.ofiles in a location known todbx.
Note If you archive.ofiles into.afiles, and link using the archive libraries, you can then remove the associated.ofiles, but you must keep the.afiles.
By default,
dbxlooks for files in the directory where they were when the program was compiled and the.ofiles in the location from which they were linked, using the absolute path recorded at compile time. If the files are not there, use thepathmapcommand to set the search path.If no object file is produced, debugging information is stored in the executable. That is, for a compilation that does not produce
.ofiles, the compiler stores all the debugging information in the executable. The debugging information is read the same way as for applications compiled with the-xsoption. See "Debugging Without the Presence of.oFiles" next.Debugging Without the Presence of
.oFilesPrograms compiled with
-g -cstore debugging information for each module in the module's.ofile. Auto-Read requires the continued presence of the program and shared library.ofiles.When it is not feasible to keep program
.ofiles or shared library.ofiles for modules that you want to debug, compile the program using the compiler-xsoption (in addition to-g). You can have some modules compiled with-xsand some without. The-xsoption instructs the compiler to have the linker place all the debugging information in the program executable; therefore the.ofiles do not have to be present to debug those modules.In
dbx4.0, the debugging information for modules compiled with the-xsoption is loaded duringdbxstartup. For a large program compiled with-xs, this might causedbxto start slowly.In
dbx5.0, the loading of debugging information for modules compiled with-xsis also delayed in the same way as the debugging information stored in.ofiles. However, you can instructdbxto load the debugging information for modules compiled with-xsduring startup. Thedbxenvironment variabledelay_xslets you turn off the delayed loading of debugging information for modules compiled with-xs. To set the environment variable, add this line to your.dbxrcfile:
dbxenv delay_xs offYou can also set this variable using the Debugging Options dialog box in Sun WorkShop Debugging. See "Delaying Loading of Modules Compiled with -xs" in the Using the Debugging Window section of the Sun WorkShop online help.
Listing Debugging Information for Modules
The
modulecommand and its options help you to keep track of program modules during the course of a debugging session. Use themodulecommand to read in debugging information for one or all modules. Normally,dbxautomatically and "lazily" reads in debugging information for modules as needed.To read in debugging information for a module name, type:
(dbx)module [-f] [-q]nameTo read in debugging information for all modules, type:
(dbx)module [-f] [-q] -awhere:
-fForces reading of debugging information, even if the file is newer than the executable. -qSpecifies quiet mode. -vSpecifies verbose mode, which prints language, file names, and so on.
To print the name of the current module, type:
(dbx)moduleListing Modules
The
modulescommand helps you keep track of modules by listing module names.To list the names of modules containing debugging information that have already been read into
dbx, type:
(dbx)modules [-v] -readTo list names of all program modules (whether or not they contain debugging information), type:
(dbx)modules [-v]To list all program modules that contain debugging information, type:
(dbx)modules [-v] -debugwhere:
-v Specifies verbose mode, which prints language, file names, and so on.
|
Sun Microsystems, Inc. Copyright information. All rights reserved. Feedback |
Library | Contents | Previous | Next | Index |