Tru64 UNIX
Ladebug Debugger Manual


Previous Contents Index

2.2.5 Displaying the Current Value of a Variable

You can display the current value of a variable or expression using several methods described in Table 2-1 as well as by entering commands in the command interface.

The following sections describe these methods.

2.2.5.1 Using the Local Variables View

In the Local Variables View, you can monitor the values of all local variables and parameters passed to a routine, as shown in Figure 2-3.

Figure 2-3 Local Variables View


The debugger automatically displays these values. It checks and updates them whenever the debugger regains control from your program (for example, after a step or at a breakpoint).

To monitor a local variable or parameter using the Local Variables View, select Views:Local Variables View or select Views:Manage Views and toggle the Local Variables View button on the Main or Optional Views Window.

The debugger automatically lists all local variable and parameter names (in the Monitor Expression column) and current values (in the Value/Assign column).

You cannot add or remove an entry to the local variables and parameters list. The debugger automatically removes previous entries and adds new entries when a new routine appears at the top of the Call Stack.

To monitor a global variable, use the Monitor View (see Section 2.2.5.4). Section 2.2.5.4 also explains how to monitor an aggregate variable and a pointer variable. The technique is the same whether you use the Monitor View or the Local Variables View.

2.2.5.2 Using the Print Button

To display the current value of a variable using the Print button:

  1. Find and select the variable name or expression in a window, as explained in Section 2.2.3.
  2. Click on the Print button on the push-button panel of the Main Window. The debugger displays the variable or expression and its current value in the Command Message View. (This is the value of the variable or expression in the current scope, which might not be the same as the source location where you selected the variable name or expression.)

2.2.5.3 Using the Print Dialog Box

The Print dialog box allows you to request typecasting or an altered output radix in the displayed result.

To display the current value with the Print dialog box, select the variable or expression using the method described in Section 2.2.3 and click on Print or:

  1. Choose Commands:Print... on the Main Window. The Print dialog box appears.
  2. Enter the variable name or expression in the Variable/Expression entry box.
  3. If you are changing the output type, pull down the menu in the Typecast entry box and click on the desired data type.
  4. If you are changing the output radix, pull down the menu in the Output Radix entry box and click on the desired radix.
  5. Click on OK.

The debugger displays the variable or expression and its current value in the Command Message View.

Your echoed command and the current value appear in the Command Message View.

Figure 2-4 shows a typecast to int for the variable length.

Figure 2-4 Typecasting the Value of a Variable


The debugger displays your echoed command and the variable or expression and its current value in the Command Message View.

You can also use the print command in the command interface to display the value of specific variables or expressions in functions active on the stack. You can also use the print command to evaluate complex expressions involving typecasts, pointer dereferences, multiple variables, constants, and any legal operators allowed by the language of the program you are debugging.

The debugger formats expression values according to the type defined for them in the program. You can print an expression's type using the whatis command.

Example 2-6 uses the whatis command to examine the contexts of the data item SUB-1 in the COBOL program TESTA.

Example 2-6 Examining Data Items in a COBOL program

(ladebug) whatis sub-1
 unsigned short SUB-1 
(ladebug) print sub-1
0 
(ladebug) 

Consider the following declarations in an Ada program:


type DAY is (MON,TUES,WED,THURS,FRI,SAT,SUN); 
MY_DAY : DAY := MON; 

Example 2-7 uses the whatis command to determine the storage representation for the variable MY_DAY.

Example 2-7 Determining the Type of a Variable

(ladebug) whatis MY_DAY
enum DAY { MON, TUES, WED, THURS, FRI, SAT, SUN }  MY_DAY; 
(ladebug) print MY_DAY
MON 

For an array, the debugger prints every cell in the array if you do not specify a specific cell.

Consider the following declarations in an Ada program:


type CAR is (BUICK,FORD,HONDA); 
type CAR_ARRAY is array (CAR) of INTEGER; 
 
CAR_NUM : CAR_ARRAY := (1,2,3); 

Example 2-8 uses the print command to display a nonstring array.

Example 2-8 Printing Values of an Array

(ladebug) print CAR_NUM
[0] = 1,[1] = 2,[2] = 3 

Example 2-9 shows how to print individual values of an array.

Example 2-9 Printing Individual Values of an Array

(ladebug) list 1, 4
      1 main() { 
      2     int c[5],d; 
      3     for (d=0 ; d<=5 ; d++) { 
      4         c[d] = d; 
(ladebug) stop in main
[#1: stop in main ] 
(ladebug) run
[1] stopped at [main:3 0x1200010b4] 
      3     for (d=0 ; d<=5 ; d++) { 
(ladebug) whatis c
array [subrange 0 ... 4 of int] of int c 
(ladebug) print c
[0] = 0,[1] = 0,[2] = -2147474264,[3] = 1023,[4] = 0 
(ladebug) step
stopped at [main:4 0x120001068] 
      4                c[d]=d; 
(ladebug) [Return]
stopped at [main:6 0x1200010dc] 
      6 } 
(ladebug) print c
[0] = 0,[1] = 1,[2] = 2,[3] = 3,[4] = 4 
(ladebug) print c[4]
4 
(ladebug) 

Expressions containing labels are not supported. Variables involving static anonymous unions and enumerated types cannot be printed. Printing a structure that is declared but not defined in a compilation unit generates an error message indicating that the structure is opaque.

2.2.5.4 Using the Monitor View

When you monitor a variable, the debugger displays the value in the Monitor View and automatically checks and updates the displayed value whenever the debugger regains control from your program (for example, after a step or at a breakpoint). If the variable is not in scope, it is displayed grayed out.

To monitor a variable or expression with the Monitor View:

  1. Choose Views:Monitor View or Views:Manage Views... and toggle the Monitor View button. The Monitor View appears in the Optional Views Window (see Figure 2-5).
  2. Find and select the variable name in a window, as explained in Section 2.2.3.
  3. Click on the Monitor push button on the push-button panel of the Main Window. The debugger does the following:
    1. Puts the selected variable name or expression in the Monitor Expression column
    2. Puts the current value of the variable in the Value/Assign column or, if the variable is an aggregate, displays the full type name of the aggregate.

Figure 2-5 Monitoring a Variable


2.2.5.4.1 Monitoring an Aggregate (Array, Structure) Variable

If you select the name of an aggregate variable such as an array or structure (record) and click on the Monitor push button, the debugger displays the full type name of the aggregate in the Value/Assign column of the Monitor View. To display the values of all elements (components) of an aggregate variable, double click on the variable name in the Monitor Expression column.

The displayed element names are indented relative to the parent name (see Figure 2-6). If an element is also an aggregate, you can double click on its name to display its elements.

Figure 2-6 Expanded Aggregate Variable (Array) in Monitor View


To collapse an expanded display so that only the aggregate parent name is shown in the Monitor View, double click on the name in the Monitor Expression column.

If you have selected a component of an aggregate variable, and the component expression is itself a variable, the debugger monitors the component that was active when you made the selection. For example, if you select the array component arr[i] and the current value of i is 9, the debugger monitors arr[9] even if the value of i subsequently changes to 10.

If the aggregate is a local variable, you can also use the Local Variables View to monitor the aggregate.

2.2.5.4.2 Monitoring a Pointer (Access) Variable

If you select the name of a pointer (access) variable and click on the Monitor push button, the debugger displays the address and value of the referenced object in the Value/Assign column of the Monitor View (see Figure 2-7).

If a referenced object is an aggregate, you can double click on its name to display its elements.

If the pointer is local, you can also use the Local Variables View to monitor the pointer.

Figure 2-7 Pointer Variable and Referenced Object in Monitor View


2.2.5.4.3 Dereferencing Pointers

Variables containing addresses are called pointers. By dereferencing a pointer in the command interface, you can print the value at the address pointed to by the pointer. In C programs, variables containing a pointer are dereferenced using the * operator. Example 2-10 shows how to dereference a pointer in C programs.

Example 2-10 Dereferencing a Pointer

(ladebug) list 1
      1 main() 
      2 { 
      3     int x, *c; 
      4 
      5     c = &x; 
      6     x = 2; 
      7     *c = 0; 
      8 } 
(ladebug) stop in main
[#1: stop in main ] 
(ladebug) run
[1] stopped at [main:5 0x12000045c]    
      5     c = &x; 
(ladebug) whatis c
int * c 
 
(ladebug) print c
0x11ffffe4c 
(ladebug) print *c
2 
(ladebug) 

2.2.6 Changing the Current Value of a Variable

You can change the value of a variable with any of the following methods:

2.2.6.1 Clicking on a Monitored Value Within the Local Variables View or Monitor View

To change a value monitored within the Local Variables View or Monitor View (see Figure 2-8):

  1. Select the variable as explained in Section 2.2.3.
  2. Click on the variable's value in the Value/Assign column of the Local Variable View or Monitor View. A small text edit box appears over that value, which you can now edit.
  3. Enter the new value in the text edit box.
  4. Click on the check mark (OK) in the text edit box. The text edit box is removed and replaced by the new value, which indicates that the variable now has that value. The debugger notifies you if you try to enter a value that is incompatible with the variable's type and range.

Figure 2-8 Changing the Value of a Monitored Variable


To cancel a text entry and dismiss the text edit box, click on X (Cancel).

You can change the value of only one component of an aggregate variable at a time (such as an array or structure). To change the value of an aggregate variable component (see Figure 2-9):

  1. Display the value of the component as explained in Section 2.2.5.4.1.
  2. Follow the procedure for changing the value of a scalar variable.

Figure 2-9 Changing the Value of a Component of an Aggregate Variable


2.2.6.2 Changing the Value of a Variable with the Assign Dialog Box

To change the value of a variable with the Assign dialog box:

  1. Select the variable as explained in Section 2.2.3.
  2. Choose Commands:Assign... on the Main window. The Assign dialog box appears.
  3. Enter the variable name and new value in the Variable and Value entry boxes.
  4. If you are changing the input radix, pull down the menu in the Input Radix entry box and click on the desired radix.
  5. Click on OK.

Figure 2-10 shows a new value and input radix for the variable prime.

You can also display the Assign dialog box by clicking MB3 in the source pane and selecting Assign from the pop-up menu.

Figure 2-10 Changing the Value of a Variable


You can also use the assign command in the command interface to change the value associated with an expression.

Example 2-11 shows how to deposit the value -42 into the data item SUB-2 of a COBOL program named TESTA.

Example 2-11 Depositing a Value in an Expression

(ladebug) assign sub-2=-42
(ladebug) print sub-2
-42 

Example 2-12 shows how to use the command interface to change the value associated with a variable and the value associated with an expression.

Example 2-12 Assigning Values to a Variable and an Expression

(ladebug) assign i = 5
(ladebug) print i
5 
(ladebug) assign f = factorial(8)   (1)
(ladebug) print f
40320 
(ladebug) assign i = f
(ladebug) print i
40320 
(ladebug) 

  1. This command calls the function factorial.

You can use the patch command in the command interface to change the value associated with an expression in the object file on the disk as well as the debugger process you are running.

2.3 Displaying and Modifying Values Stored in Registers

The Register View displays the current contents of all machine registers.

To display the Register View, choose Views:Register View or Views:Manage Views... and toggle the Register View button. The Register View appears in the Optional Views Window.

Figure 2-11 Changing a Value in the Register View


By default, the Register View automatically displays the register values associated with the routine in which execution is currently paused. Any values that change as your program executes are highlighted whenever the debugger regains control from your program.

To display the register values associated with any routine on the call stack, choose its name from the Call Stack menu on the Main Window (see Section 2.5).

To change the value stored in a register:

  1. Click on the register value in the Register View. A text edit box appears over the current value, which you can now edit.
  2. Enter the new value in the text edit box.
  3. Click on the check mark (OK) in the text edit box, as shown in Figure 2-11.

The text edit box is removed and replaced by the new value, indicating that the register now contains that value.

To cancel a text entry and dismiss the text edit box, click on X (Cancel).

You can also change the radix for modifying values stored in all registers as follows:

  1. Position your mouse pointer in the Register View and select the Register pulldown menu.
  2. Select Change All Radix-> and choose from a list of radix choices for modifying values.

2.4 Displaying the Decoded Instruction Stream of Your Program

The Instruction View displays the decoded instruction stream of your program: the code that is actually executing. This is useful if the program you are debugging has been optimized by the compiler. This means that the information in the Main Window does not exactly reflect the code that is executing (see Section 1.6.1).

Figure 2-12 Instruction View


To display the Instruction View, choose Views:Instruction View or Views:Manage Views... and toggle the Instruction View button on the Main or Optional Views Window.

By default, the Instruction View automatically displays the decoded instruction stream of the routine in which execution is currently paused. The current location pointer (to the left of the instructions) marks the instruction that will execute next.

By default, the debugger displays line numbers to the left of the instructions with which they are associated. You can choose not to display these line numbers so that more space is devoted to showing instructions. To hide or display line numbers, choose File:Show Instruction Addresses on the Instruction View.

To copy memory addresses or instructions into a command you are entering at the command-entry prompt, select text and choose Edit:Copy in the Instruction View. Then position your mouse pointer at the command you have entered and choose Edit:Paste on the Main Window. (You can also select instruction text to be used with a push-button command you click in the push-button panel of the Main Window.)

To set breakpoints from the Instruction View, toggle the breakpoint button next to the instruction of interest. The breakpoint is set in the source display, Instruction View, and Breakpoint View (if the Breakpoint View is open). Information on the breakpoint is continuously updated in the source display and the Instruction View and Breakpoint View if they are open.

You can also set breakpoints and change breakpoint status by pulling down the Break menu from the Optional Views Window.

After navigating through the Instruction View, to redisplay the location at which execution is paused, click on the Call Stack menu.

To display the instruction stream of any routine on the call stack, choose the routine's name from the Call Stack menu on the Main Window (see Section 2.5).

2.5 Displaying the Call Stack

While debugging a routine in your program, you might want to set the current scope to a calling routine (a routine down the stack from the routine in which execution is currently paused). This enables you to:

The Call Stack menu, shown in Figure 2-13 lists the names of the routines of your program that are currently active on the stack, up to the maximum number of lines that can be displayed on your screen.

Figure 2-13 Current Scope Set to a Calling Routine


The numbers on the left side of the menu (referred to as stack level) indicate the level of each routine on the stack relative to level 0, which denotes the routine in which execution is paused.

To set the current scope to a particular routine on the stack, choose the routine's name from the Call Stack menu; for example, 1:trycol in Figure 2-13. This causes the following to occur:

When you set the scope to a calling routine, the current-location pointer (which is cleared) marks the source line to which execution will return in that routine. Depending on the source language and coding style used, this might be the line that contains the call statement or some subsequent line.

You can also use the where command in the command interface to display the stack trace, shown in Figure 2-14.

Figure 2-14 Sample Stack Trace


The stack trace provides the following information for each activation level:
Stack level The number used to refer to an activation level on the stack. The function entered most recently is on activation level 0.
Memory address The address of the next instruction to be executed in the named function.
Function name The name of the function active at this activation level of the stack. The values of any variables passed to this function are also listed.
File name The name of the file containing the source code for the function.
Function
parameters
The list of parameter names and values from previous function calls.
Line number The number of the last executed source line in the function.

Example 2-13 uses the where command to display the stack trace of a COBOL program.

Example 2-13 Displaying the Stack Trace in a COBOL Program

(ladebug) where
>0  0x3ff81808744 in cob_acc_display() cob_accdis.c:349 
#1  0x120001fbc in testa() testa.cob:20 
#2  0x3ff8181f054 in main() cob_main.c:253 


Previous Next Contents Index