• Keine Ergebnisse gefunden

Examining values

Im Dokument or an (Seite 122-129)

Stepping and tracing through your code can help you find problems in program flow, but you'll usually want to watch what happens to the values of variables while you step. For example, when you step through a for loop,

Whats an expression?

Watching expressions

Figure 6.1 The Watch window

Adding a watch

110

it's helpful to know the value of the index variable. The IDE has several tools to help you examine the contents of your program's variables:

• The Watch window lets you track the value of a variable or expression.

• The Evaluate Expression dialog box lets you evaluate any expression

· meaningful to the program you're debugging, and it lets you change the value of a variable while you're debugging your program.

• The Data Inspector window lets you examine and modify the value in a data element.

Watching, evaluating, and inspecting operate at the level of expressions. An expression consists of constants, variables, and data structures combined with operators. Almost anything you can use as the right side of an assignment statement can be used as a debugging expression.

If you want to keep track of the value of a variable or expression while you step through your code, use a watch. A watch is an expression you enter in the Watch window, which then displays the current value of the

expression. As you step through your program, the value of the watch expression changes when the program does something to change it.

If the execution point steps out of the scope of a watch expression, the 'watch expression is undefined. Once the execution point enters the scope of the expression once again, the Watch displays the current value of the expression once more.

To open the Watch window, choose View I Watch. If you haven't added any watches yet, the window is empty.

To add a variable to the Watch window, choose Debug I Add Watch. The IDE opens a Watch Properties dialog box, prompting you to type in a watch expression.

Just like other IDE windows, you can move, resize, and close the Watch window. If the Watch window is closed, you can display it again with View I Watch. If the Watch window is the active window, you can add a watch by choosing Add Watch from the SpeedMenu.

Borland C++ User's Guide

Formatting watch expressions

Figure 6.2 The Watch Properties dialog box

If the edit window is the active window, you can quickly add the expression at your cursor into the Watch window. To do so, choose Set

Watch on the SpeedMenu. '

If you double-click a watch in the Watch window, a Watch Properties dialog box appears. You can also display a Watch Properties dialog box by selecting a watch in the Watch window and then choosing Set Properties on

th~ Watch window SpeedMenu.

Watch Properties EHPression:

Display as ...

r;:-~D~fau,-t --~-,,),ftT:r;cter' -'--ID;ing --~: L - _ - . i

I, .... ,,'.· •. · .. > .

Slruc.tUte ... '. ,> f.. ointe.r . . floating pOint. '.',.

~j Memory dump , \' • "

· l.~~~e~al,:~~~~.;~T~--~. t ,.~~~i!i~~~t.'d!~i!~:J7, . , .. ,., ..•. ' .•. , [ii

The default expression in a Watch Properties dialog box is the word at the cursor in the current edit window. A history list keeps track of expressions you've entered as watches previously.

You can format watch expression results by selecting options in the Watch Properties dialog box. For example, although integer values normally display in decimal form, you can specify that an expression be displayed as hexadecimal by selecting the Hexadecimal button .. After selecting either Decimal or Hexadecimal, you can modify the format of the expression further with the Display As options.

If you're watching a data element such as an array, you can display the values of the consecutive data elements. For example, for an array of five integers named xarray, you would type the number 5 in the Repeat Count box of a Watch Properties dialog box to see all five values of the array. An expression used with a repeat count must represent a single data element.

The debugger views the data element as the first element of an array if the element isn't a pointer, or as a pointer to an array if it is.

If you select the Floating Point display option, you can also indicate the number of significant digits you want displayed in the watch expression result. Specify the number of digits in the Significant box.

Disabling a watch

Deleting a watch

Editing a watch

Evaluating and modifying expressions Evaluating expressions

112

If you prefer not to watch an expression you've entered in the Watch window, but you don't want to delete it because you might want to use it later, you can disable the watch.

If the IDE must evaluate many watch expressions in the Watch window~

stepping might slow down. You can choose to disable a watch, and then enable it later when you need it. To disable a watch, click the check box next to the watch. To enable it again, click the check box again.

You can also disable and enable watches with SpeedMenu commands. To disable or enable multiple watches at a time, click and drag over a block of watches to select them in the Watch window, or press etr/while you click all the watches you want to disable or enable. Then choose the appropriate command on the SpeedMenu.

To delete a watch expression, select the expression in the Watch window and choose Delete Watch on the SpeedMenu. To delete all watch

expressions, choose Delete All Watches on the SpeedMenu.

To change the properties of a watch, display the Watch Properties dialog box for that watch and edit the properties you want changed.

In addition to watching variables as your program executes, you can evaluate expressions at any time and you can change the values of variables at run time.

To evaluate an expression, choose Debug I Evaluate/Modify. The debugger displays an Expression Evaluator dialog box. By default, the word at the cursor position in the current edit window is highlighted in the Expressions box. You can edit the expression, type in another, or choose one from the history list of expressions you evaluated previously.

The current value of the expression in the Expression box shows in·the Result box when you choose Evaluate. You can evaluate any valid C or c++ expressions except those that contain these things:

• Symbols or macros defined with #define

• Local or static variables not in the scope of the function being executed

• Function calls

Borland C++ Users Guide

You can format expressions by adding a comma and one or more format specifiers. For example, to display the result in hexadecimal, type ,H after the expression. Table 6.1 shows all the legal format specifiers and their effects.

Table 6.1: Format specifiers for debugger expressions Character

Hexadecimal. Shows integer values in hexadecimal with the Ox prefix, including those in data structures.

Character. Shows special display characters for ASCII 0 .. 31. By default, such characters are shown using the appropriate C escape sequences ~n, \t, and so on).

Decimal. Shows integer values in decimal form, including those in data structures.

Floating point. Shows n significant digits (where n is in the range 2 .. 18, and 7 is the default).

Memory dump. Shows n bytes starting at the address of the indicated expression. If n is not specified, it defaults to the size in bytes of the type of the variable.

By default, each byte shows as two hex digits. The C, D, H, and S specifiers can be used with M to change the byte formatting.

Pointer. Shows pointers as seg:ofs with additional information about the address pointed to. It tells you the region of memory in which the segment is located, and the name of the variable at the offset address, if appropriate.

Structure/Union. Shows both field names and values such as (X:1; Y:1 0;Z:5).

String. Shows ASCII 0 .. 31 as C escape sequences. Use only to modify memory dumps (see nM above).

Modifying variables While debugging, you can change the value of a variable by using the Expression Evaluator dialog box. Enter the variable in the Expression box, then type the new value in the New Value box. If you want the modified value to take effect in your program, choose Modify. Otherwise, when you close the dialog box, the debugger ignores the modified value.

Keep these points in mind when you change the values of variables:

• You can change individual variables or elements of arrays or structures, but not arrays or structures themselves .

• The expression in the New Value box must evaluate to a result that is assignment-compatible with the variable you want to assign it to. A good rule of thumb is that if the assignment would cause a compile-time or run-time error, it's not a legal modification value.

Warning!

Inspecting data elements

114

• You can't directly modify untyped arguments passed into a function, but you can typecast them and then assign new values.

• You can use the Expression Evaluator dialog box to examine and modify values in registers, including the flags register. For example, you can enter expressions such as these: _CS,_BX,_FLAGS. These values are bitmasks.

• Modifying values, and especially pointer values and array indexes, can have undesirable effects because you might overwrite other variables and data structures. Be careful.

You can examine and modify values in a data element in an Inspector window. To inspect a data element,

1. Choose Debug I Inspect to display the Data Inspector window.

2. Type in the expression you want to inspect.

3. Choose Inspect to display an Inspector window.

If the execution point is in the scope of the expression you are inspecting, the value appears in the Data Inspector window. If the execution point is outside the scope of the expression, the value is undefined.

You can also display an Inspector directly from the edit window:

1. Po~ition your cursor on the data element you want to inspect.

2. Choose Inspect on the SpeedMenu or press Enter.

~f you choose this method, the data element is always evaluated within the scope of the line on which the data element appears.

With either method, the appearance of the data in an Inspector window depends on the type of data being inspected. For example, if you inspect an array, you'll see a line for each member of the array with the array index of the member. The value of the member follows in its display format, followed by the value in hexadecimal.

Once you're in an Inspector window, you can inspect certain elements to isolate the view. To inspect an item,

, 1. Select the item you want to inspect further.

2. Choose Inspect on the SpeedMenu or press Enter.

Borland C++ Users Guide

Examining register values

Table 6.2 CPU flags in the Register window

You can change the value of inspector items. To change the value of a single inspector item:

1. Select the item.

2. Choose Change on the SpeedMenu.

3. Type.in a new value and choose OK.

If you're inspecting a data structure, it's possible the number of items displayed might be so great that you'll have to scroll in the Inspector window to see data you're interested in. For easier viewing, you can narrow the display to a range of data items.

To display a range of items, 1. Click in the Inspector window.

2. Choose Range on the SpeedMenu.

3. In the Start Index box, enter the index of the item you want to see first in the window.

4. In the Count box, enter the number of items you want to see in the Inspector window.

While debugging, you can display the values in the data, pointer, index, segment, and instruction pointer registers, as well as the settings of the status word or flags. Choose View I Register to display the Registers window.

You can also view register values with the Expression Evaluator dialog box.

See page 114.

Letter in pane Flag name

c Carry

z Zero

s Sign

0 Overflow

p Parity

a Auxiliary carry i Interrupt enable

d Direction

The Registers SpeedMenu lets you select the format to display the values in the registers; choose from Hexadecimal or Decimal. You can also choose to view the 16-bit (word) or 32-bit (double word) registers.

Im Dokument or an (Seite 122-129)