| Debugging a Program With dbx |
Controlling Program Execution
The commands used for running, stepping, and continuing (
run,rerun,next,step, andcont)are called process control commands. Used together with the event management commands described in Chapter 6, you can control the run-time behavior of a program as it executes underdbx.This chapter is organized into the following sections:
- Running a Program
- Attaching dbx to a Running Process
- Detaching dbx From a Process
- Stepping Through a Program
- Using Ctrl+C to Stop a Process
Running a Program
When you first load a program into
dbx,dbxvisits the program's "main" block (mainfor C, C++, and Fortran 90;MAINfor FORTRAN 77).dbxwaits for you to issue further commands; you can visit code or use event management commands.You can set breakpoints in the program before running it. Use the
runcommand to start program execution.To run a program in
dbxwithout arguments, type:
(dbx)runYou can optionally add command-line arguments and redirection of input and output
.
(dbx)run[arguments][ <input_file] [ >output_file]Output from the
runcommand overwrites an existing file even if you have setnoclobberfor the shell in which you are runningdbx.The
runcommand without arguments restarts the program using the previous arguments and redirection. Thereruncommand restarts the program and clears the original arguments and redirection. For more information, see "rerun Command" in the Using dbx Commands section of the Sun WorkShop online help.Attaching
dbxto a Running ProcessYou might need to debug a program that is already running. You would attach to a running process if:
- You wanted to debug a running server, and you did not want to stop or kill it.
- You wanted to debug a running program that has a graphical user interface, and you didn't want to restart it.
- Your program was looping indefinitely, and you want to debug it without killing it.
You can attach
dbxto a running program by using the program's pid number as an argument to thedbxdebugcommand.Once you have debugged the program, you can then use the
detachcommand to take the program out from the control ofdbxwithout terminating the process.If you quit
dbxafter attaching it to a running process,dbximplicitly detaches before terminating.To attach
dbxto a program that is running independently ofdbx, you can use either theattachcommand or thedebugcommand.To attach
dbxto a process that is already running, type:
(dbx)debugprogram_name process_IDor(dbx)attachprogram_name process_ID
- You can substitute a - (dash) for the program_name;
dbxautomatically finds the program associated with the process_ID and loads it.- For more information, see "debug Command" and "attach Command" in the Using dbx Commands section of the Sun WorkShop online help.
If
dbxis not running, startdbxby typing:
%dbxprogram_name process_IDAfter you have attached
dbxto a program, the program stops executing. You can examine it as you would any program loaded intodbx. You can use any event management or process control command to debug it.You can also attach
dbxto a running process in the Sun WorkShop Debugging window by choosing DebugAttach Process. For more information, see "Attaching to a Running Process" in the Using the Debugging Window section of the Sun WorkShop online help.
Detaching
dbxFrom a ProcessWhen you have finished debugging the program, use the
detachcommand to detachdbxfrom the program. The program then resumes running independently ofdbx.To detach a process from running under the control of
dbx:
(dbx)detachFor more information, see "detach Command" in the Using dbx Commands section of the Sun WorkShop online help.
You can also detach dbx from a process in the Sun WorkShop Debugging window by choosing Execute
Detach Process. For more information, see "Detaching From a Process" in the Using the Debugging Window section of the Sun WorkShop online help.
Stepping Through a Program
dbxsupports two basic single-step commands:nextandstep, plus a variant ofstep, calledstep up. Both thenextcommand and thestepcommand let the program execute one source line before stopping again.If the line executed contains a function call, the
nextcommand allows the call to be executed and stops at the following line ("steps over" the call). Thestepcommand stops at the first line in a called function ("steps into" the call). You can also step over a function in the Sun WorkShop Debugging window by choosing ExecuteStep Over or clicking the Step Over button on the tool bar. You can step into a function by choosing Execute
Step Into or clicking the Step Into button on the tool bar.
The
step upcommand returns the program to the caller function after you have stepped into a function. You can also step out of a function in the Sun WorkShop Debugging window by choosing ExecuteStep Out or clicking the Step Out button on the tool bar.
For more information, see "Program Stepping" in the Using the Debugging Window section of the Sun WorkShop online help.
Single Stepping
To single step a specified number of lines of code, use the
dbxcommandsnextorstepfollowed by the number of lines[n]of code you want executed.
(dbx)nextnor(dbx)stepnThe
step_granularityenvironment variable determines the granularity of source line stepping; that is, the number ofnextcommands needed to step through a line of code. For more information, see "step_granularity Environment Variable" in the Using dbx Commands section of the Sun WorkShop online help.For more information on the commands, see "next Command" and "step Command" in the Using dbx Commands section of the Sun WorkShop online help.
Continuing Execution of a Program
To continue a program, use the
contcommand.
(dbx)contYou can also continue execution of a program in the Sun WorkShop Debugging window by choosing Execute
Continue or clicking the Continue button on the tool bar.
The
contcommand has a variant,cont atline_number, which lets you specify a line other than the current program location line at which to resume program execution. This allows you to skip over one or more lines of code that you know are causing problems, without having to recompile.To continue a program at a specified line, type:
(dbx)cont at 124The line number is evaluated relative to the file in which the program is stopped; the line number given must be within the scope of the current function.
Using
cont atline_number withassign, you can avoid executing a line of code that contains a call to a function that might be incorrectly computing the value of some variable.To resume program execution at a specific line:
1. Useassignto give the variable a correct value.2. Usecontatline_number to skip the line that contains the function call that would have computed the value incorrectly.Assume that a program is stopped at line 123. Line 123 calls a function,
how_fast(), that computes incorrectly a variable,speed. You know what the value of speed should be, so you assign a value tospeed. Then you continue program execution at line 124, skipping the call tohow_fast().
(dbx)assign speed = 180; cont at 124;For more information, see "cont Command" in the Using dbx Commands section of the Sun WorkShop online help.
If you use the
contcommand with awhenbreakpoint command, the program skips the call tohow_fast()each time the program attempts to execute line 123.
(dbx)when at 123 { assign speed = 180; cont at 124;}For more information on the
whencommand, see:
- Setting a stop Breakpoint at a Line of Source Code
- Setting Breakpoints in Member Functions of Different Classes
- Setting Breakpoints in Member Functions of the Same Class
- Setting Multiple Breakpoints in Nonmember Functions
- "when Command" in the Using dbx Commands section of the Sun WorkShop online help
Calling a Function
When a program is stopped, you can call a function using the
dbxcallcommand, which accepts values for the parameters that must be passed to the called function.To call a procedure, type the name of the function and supply its parameters. For example:
(dbx)call change_glyph(1,3)While the parameters are optional, you must type the parentheses after the function_name. For example:
(dbx)call type_vehicle()You can call a function explicitly, using the
callcommand, or implicitly, by evaluating an expression containing function calls or using a conditional modifier such asstop in glyph -if animate().A C++ virtual function can be called like any other function using the
callcommand (see "print Command" or "call Command" in the Using dbx Commands section of the Sun WorkShop online help), or any other command that executes a function call.If the source file in which the function is defined was compiled with the
-goption, or if the prototype declaration is visible at the current scope,dbxchecks the number and type of arguments and issues an error message if there is a mismatch. Otherwise,dbxdoes not check the number of parameters and proceeds with the call.By default, after every
callcommand,dbxautomatically callsfflush(stdout)to ensure that any information stored in the I/O buffer is printed. To turn off automatic flushing, set thedbxenvironment variableoutput_autoflushtooff.For C++,
dbxhandles the implicitthispointer, default arguments, and function overloading. The C++ overloaded functions are resolved automatically if possible. If any ambiguity remains (for example, functions not compiled with-g),dbxdisplays a list of the overloaded names.When you use the
callcommand,dbxbehaves as though you used thenextcommand, returning from the called function. However, if the program encounters a breakpoint in the called function,dbxstops the program at the breakpoint and issues a message. If you now type awherecommand, the stack trace shows that the call originated fromdbxcommand level.If you continue execution, the call returns normally. If you attempt to kill, run, rerun, or debug, the command aborts as
dbxtries to recover from the nesting. You can then re-issue the command. Alternatively, you can use the commandpop -cto pop all frames up to the most recent call.Using Ctrl+C to Stop a Process
You can stop a process running in
dbxby pressing Ctrl+C (^C). When you stop a process using^C,dbxignores the^C, but the child process accepts it as aSIGINTand stops. You can then inspect the process as if it had been stopped by a breakpoint.To resume execution after stopping a program with
^C, use thecontcommand. You do not need to use thecontoptional modifier,sigsignal_name, to resume execution. Thecontcommand resumes the child process after cancelling the pending signal.
|
Sun Microsystems, Inc. Copyright information. All rights reserved. Feedback |
Library | Contents | Previous | Next | Index |