9.4 Display variable values


9.4 Display variable values

9.4.1 Basic tactic

Insert a statement in the source to print the value of a variable.

9.4.2 Purpose

  • Determine the sequence of the pattern of values assigned to a variable.

  • Determine whether the variable ever had an incorrect value.

  • Find the earliest point in the execution of the program when the variable had an incorrect value.

  • Find the first location in the program source where the variable had an incorrect value.

9.4.3 Questions answered

  • What was the value of the variable each time the code block that con tains the output statement was executed?

  • Was that same code block ever executed?

  • How many times was that code block executed?

9.4.4 Potential problems

  • If the problem is caused by stack or heap corruption, the presence of an output statement may cause the symptom to disappear. This is because the runtime library that generates the output will shift things on the stack and may allocate memory on the heap.

  • If the problem is caused by optimizations performed by the compiler, the presence of an output statement may cause the optimizer not to perform the offending optimization or to perform it differently.

9.4.5 Refined tactics

  1. Insert a statement in the source that tests a variable and prints the value if it’s unexpected.

  2. In addition to the inserted print statement, do the following:

    • Add an execution option to the program.

    • Insert a conditional statement that executes the print statement only if the execution option was used.

  1. Insert print statements immediately before each reference to a variable of interest and after each assignment of that variable.

  2. Print the value of the variable in several different formats: floating-point, decimal integer, hexadecimal, address.

9.4.6 Related tactics

  • Use a high-level debugger or interpreter to do the following:

    • Set a breakpoint at the code block of interest.

    • Add a command list to the breakpoint to print the value of the variable and continue execution.

  • Use a high-level debugger or interpreter to do the following:

    • Set a conditional breakpoint at the code block of interest that tests whether the value of the variable is unexpected.

    • Add a command list to the breakpoint to print the value of the variable and continue execution.

    • Add an execution option to the program.

  • Use a high-level debugger or interpreter to do the following:

  • Set a conditional breakpoint at the code block of interest that tests whether the execution option was used.

  • Add a command list to the breakpoint to print the value of the variable and continue execution.

9.4.7 Choosing tactics

Use the basic tactic when one of the following conditions is true:

  • No high-level debugger is available.

  • You don’t want to have to interact with a high-level debugger.

  • It is quicker to recompile with a new statement inserted than to recompile, relink, and start up with a high-level debugger.

  • You want a file containing all values the variable contains at that point in the program.

Use the refined tactic 1 when the variable takes on a range of values that is predictable. Examine the source code and input data to determine whether this condition is true and what the range is. The refined tactic is more important when an entire array is under question. Typically, you can reduce the amount of output by just printing the nonzero values. Or, you can just print the values in some particular section of the array, such as the main diagonal of a matrix.

Use refined tactic 2 when compile or link times are relatively long, or when you anticipate wanting to display this variable again in the future.




Debugging by Thinking. A Multidisciplinary Approach
Debugging by Thinking: A Multidisciplinary Approach (HP Technologies)
ISBN: 1555583075
EAN: 2147483647
Year: 2002
Pages: 172

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net