• Keine Ergebnisse gefunden

Controlling program execution

Im Dokument or an (Seite 118-122)

The most important element of debugging is controlling the execution of your program. Because you can control when each statement is executed, it's easier to determine which part of your program is causing a problem.

Stepping and tracing let you run your program one statement at a time; the next statement won't execute until you tell the debugger to continue. You can step or trace until you reach the spot in your code where things go awry. You can then examine the state of the program and its data, view the

Watching program output

Stepping through code

program's output and the value of its variables, or modify or evaluate expressions in your program before you tell the debugger to execute the next line.

As you step or trace through your program, you can watch your application's output in its window. Set up your windows so you can see both your source code and your application's window as you step and trace. If the IDE desktop window and your application window overlap as you debug, you'll see some flickering in your application window. If you arrange these windows so they don't overlap, your program's execution will be quicker and smoother.

All execution in the debugger, including stepping, tracing, and halting at breakpoints, is based on lines of source code. If a statement is more than·

one line on the screen, the statement is still considered to be one line.

You can control the rate of debugging to the level of a single line of source code. If you string several statements together on one line, you can't debug those statements individually. On the other hand, you can spread a single statement out over multiple lines for debugging purposes, and the statement still executes as a single step.

Each time you tell the debugger to step or trace, the execution point (the highlighted line that marks your place in the program you're debugging) moves to the next line. The execution point always shows you the next line to be executed.

Stepping is the simplest way to move through your code a little bit at a time. To step through your code, choose Debug I Step Over (or FB or the Step Over button on the SpeedBar) to execute the code indicated by the execution point, including any functions it might call before returning control to you. The execution point then indicates the next complete line.

The following example helps explain how stepping works. Let's say that these are the first lines of a program loaded into an edit window:

BOOL InitApplication ( HINSTANCE hlnstance )

{

WNDCLASS WCi

wc.style = CS_HREDRAW I CS_VREDRAWi

wc.lpfnWndProc = (long (FAR PASCAL*) ())MainWndProcj wc.cbClsExtra = OJ

wc.cbWndExtra = OJ

1 06 Borland C++ tJ~er's Guide

Tracing into code

Stepping and tracing class member functions

return ( RegisterClass ( &wc ) )i

int PASCAL WinMain ( HINSTANCE hlnstance, HINSTANCE hPrevlnstance, LPSTR lpCmdLine, int nCmdShow )

{

MSG msgi

if ( !hPrevlnstance )

if ( !InitApplication ( hlnstance ) ) return ( FALSE )i

if ( !Initlnstance (,hlnstance, nCmdShow ) return ( FALSE ) i

In this example, InitApplication is a function you defined in a module that has been compiled with debugging information. If you were actually debugging this program, each time you chose Debug I Step Over (or pressed FB), the debugger would execute the line highlighted by the execution point, and then the execution point would advance to the next line. If you chose Step Over when the execution point got to the statement

if ( !InitApplication ( hlnstance ) )

the debugger would execute the InitApplication function and return a Boolean value, but you W_Quldn't see the execution point move through the actuallnitApplication function. Instead, the debugger would step over the function. Stepping returns control to you after the function finishes.

Tracing into code is very much like stepping through code, except that when you come to a line that calls a function, tracing into the code moves the execution point into the code in the function. In Listing 6.0, if you chose Debug I Trace In~o (F7 or the Trace Into SpeedBar button) to execute each statement, you'd see the execution point jump to the code that implements the InitApplication function when the debugger reached the statement that evaluates the return value of InitApplication. As you debug, you can choose to trace into some functions and step over others, depending upon your needs.

If you use classes in your programs, you can still use the integrated debugger to step and trace. The debugger handles member functions the same way it would step over or trace through functions in a program that is not object-oriented.

Stepping and tracing external code

Stepping over large sections of code

Running to a

specific location

Locating a function

Returning to the execution point

Navigating backward

108

If you link external code into your program, you can step over or trace into that code if the .OBJ file you link in contains debugging information.

You can debug external code written in any language, including C, C++, Pascal, and assembly language. As long as the code meets all the

requirements for external linking and contains full Borl~nd symbolic debugging information, the IDE's debugger can step or trace through it.

Sometimes you don't want to step through each line of your code just to get to the part that is causing problems. With the integrated debugger, you can step over large amounts of code and regain control at the point where you want to begin executing line-by-line again.

You can tell the debugger you want to execute your program normally (not step-by-step) until a certain location in your code is reached:

1. Position the cursor at the line where you want to resume debugging control.

2. Choose Run to Cursor on the SpeedMenu (or press F4).

You can use Run to Cursor as a way to start your debugging session or after you've already been stepping and tracing.

You can locate a particular function quickly with the Locate Function command on the Search menu. Locate Function asks you for the name of a function, then positions the cursor on the proper line in the file where that function is defined. You must be debugging (stepping or tracing through code) before you can use Locate Function.

While you are debugging, you are free to browse through any file in any edit window, go to any place in your file, and even open and close files.

You can then return to the execution-point location very quickly.

To go to the execution-point location, choose Debug I Find Execution Point.

The debugger positions the cursor at the execution point. If you closed the edit window containing the execution point, Find Execution Point opens an edit window and displays the source code containing the execution point.

While you're debugging, it can be useful to know how you got to where you are. The Call Stack window shows you the sequence of fUhction calls

Borland C++ Users Guide

Stopping the program

Starting over

that brought you to your current state. Use View I Call Stack to display the Call Stack window.

The Call Stack window is particularly useful if you accidentally trace into code you want to step over. You can step back, and then resume stepping and tracing where you originally intended:

1. In the Call Stack window, double-click the call that calls the function you traced into by mistake. (It will be the second call from the top of the Call Stack window.)

The edit window becomes the active window with your cursor positioned at the place the call was made.

2. In the edit window, move the cursor completely past the call.

3. Choose Run to Cursor on the edit window SpeedMenu.

The Call Stack window is also useful when you want to view the arguments passed to each function.

You can view or edit the source code that contains a particular call. Select the call in the Call Stack window and right-click to display the SpeedMenu to display view and edit commands.

Instead of stepping over or tracing through code, you can use a simpler technique to pause your program. Choose Debug I Pause Program, and your program will stop executing. Then you can examine the value of variables and inspect data at this state of the program. When you're done, choose Debug I Run to continue the execution of your program.

If for some reason your program assumes control and won't allow you to return to the debugger (for example, it might be in an infinite loop), you can press Ctrl+AIt+Sys Req to stop your program.

While debugging, you might occasionally want to start over from the beginning. Choose the Debug I Terminate Program command or press Ctrl+F2. This ends your program so that all subsequent running, stepping, or tracing begins at the start of the main program.

Im Dokument or an (Seite 118-122)