1 / 36

Debugging

Debugging. Objectives. In this chapter you will learn: About debugging concepts How to use basic debugging techniques About the Visual C++ debugger How to trace program execution with step commands How to trace variables and expressions with debug windows How to use the Call Stack window

jaden
Download Presentation

Debugging

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Debugging Microsoft Visual C++ .NET Chapter 4

  2. Objectives In this chapter you will learn: • About debugging concepts • How to use basic debugging techniques • About the Visual C++ debugger • How to trace program execution with step commands • How to trace variables and expressions with debug windows • How to use the Call Stack window • About Visual C++ language bugs and debugging resources Microsoft Visual C++ .NET Chapter 4

  3. Understanding Debugging • Regardless of experience, knowledge, and ability, all programmers create errors in their programs at one time or another • As you learned at the start of this textbook, debugging describes the act of tracing and resolving errors in a program • Debugging is an essential skill for any programmer, regardless of the programming language and the programmer’s level of experience Microsoft Visual C++ .NET Chapter 4

  4. Error Types • Three main types of errors can occur in a Visual C++ program: syntax errors, run-time errors, and logic errors • Syntax errors occur when you enter code that the compiler does not recognize • Syntax errors in C++ include invalid statements or statements that are entered incorrectly, such as when a closing parenthesis for a function is missing • If you Visual C++ program encounters a problem while it is executing, the problem is called a run-time error Microsoft Visual C++ .NET Chapter 4

  5. Error Types • Run-time errors differ from syntax errors in that they do not necessarily represent C++ language errors • Instead, run-time errors occur when your program encounters code that it cannot handle • Logic errors are problems in the design of a program that prevent it from running as you anticipate it will run • The logic behind any program involves executing the various statements and procedures in the correct order to produce the desired results Microsoft Visual C++ .NET Chapter 4

  6. Interpreting Build Messages • Build messages are vital to the debugging process • There are two main types of build messages: compiler error messages and warning messages • Compiler error messages occur for any syntax errors in a program • Compiler error messages contain the name of the file in which the error occurred, the line number in the file, and a description of the error • To use build messages to help locate bugs in the Moving Estimator program, follow the steps on pages 185 and 186 of the textbook Microsoft Visual C++ .NET Chapter 4

  7. Basic Debugging Techniques • If you know that a bug in your program is being caused by a complicated program design that includes classes and other advanced techniques, then you should use Visual C++’s advanced debugging tools • If you are fairly certain, however, that the bug in your program is being caused by something simple, such as a variable being assigned the wrong value at some point, then you can use some basic debugging tools with which you are already familiar to help you find the error Microsoft Visual C++ .NET Chapter 4

  8. Tracing Console Application Errors with Output Statements • Tracing is the examination of individual statements in an executing program • You use output statements placed at different points in your program to print the contents of a variable, an array, or the value returned from a function • Using this technique, you can monitor values as they change during program execution • Simplified, temporary programs that are used for testing functions and other code are called driver programs Microsoft Visual C++ .NET Chapter 4

  9. Tracing Console Application Errors with Output Statements • Driver programs do not have to be elaborate; they can be as simple as a main() function and the function you are testing • A testing technique that is essentially the opposite of driver programs is the use of stub functions • Stub functions are empty functions that serve as placeholders (or “stubs”) for a program’s actual functions Microsoft Visual C++ .NET Chapter 4

  10. Tracing Console Application Errors with Output Statements • To use output statements to help locate bugs in the Moving Estimator program’s calcTotalEstimate() function, use the directions listed on pages 190 through 194 Microsoft Visual C++ .NET Chapter 4

  11. Tracing Console Application Errors with Output Statements Microsoft Visual C++ .NET Chapter 4

  12. Tracing Console Application Errors with Output Statements Microsoft Visual C++ .NET Chapter 4

  13. Using Comments to Locate Bugs • Another method of locating bugs in a C++ program is to comment out lines that you think might be causing the problem • You can comment out individual lines that might be causing the error, or comment out all lines except for the lines that you know are correct • The last five statements in Figure 4-14 are commented out because they generate compiler error messages stating that dYearlyInterest is not defined Microsoft Visual C++ .NET Chapter 4

  14. Using Comments to Locate Bugs Microsoft Visual C++ .NET Chapter 4

  15. Analyzing Your Logic • At times, errors in your code will be logic problems that are difficult to spot using tracing techniques • When you suspect that your code contains logic errors, you must analyze each statement on a case-by-case basis. Microsoft Visual C++ .NET Chapter 4

  16. The Visual C++ Debugger • Many high-level programming languages have debugging capabilities built directly into their development environments • Examining your code manually is usually the first step to take when you have a logic error, or you may use a driver program to track values assigned to a function’s variables • Visual C++ provides a program called the debugger that contains several tools that can help you trace each line of code, creating a much more efficient method of finding and resolving logic errors • Break mode temporarily suspends, or pauses, program execution so that you can monitor values and trace program execution Microsoft Visual C++ .NET Chapter 4

  17. Build Configurations • To use the Visual C++ debugger, you need to build a Debug version of your program • A Debug build contains additional information that is required by the debugger tools • You can also create a Win32 Release build • A Release build does not contain any debugging information • By default when you first create a project, it is set to compile as a Debug build Microsoft Visual C++ .NET Chapter 4

  18. Build Configurations • To make sure your project is set to build a Debug version, use the directions illustrated on page 200 of the textbook Microsoft Visual C++ .NET Chapter 4

  19. Tracing Program Execution with Step Commands • The Step Into, Step Over, and Step Out commands on the Debug menu are used for tracing program execution once you enter break mode • The Step Into command executes an individual line of code and then pauses until you instruct the debugger to continue • The Step Over command allows you to skip function calls • The program still executes the function that you step over, but it appears in the debugger as if a single statement executes Microsoft Visual C++ .NET Chapter 4

  20. Tracing Program Execution with Step Commands • The Step Out command executes all remaining code in the current function • If the current function is called from another function, all remaining code in the current function executes and the debugger stops at the next statement in the calling function • When you select the Run To Cursor command, the program runs normally until it reaches the statement where your cursor is located, at which point the program enters break mode Microsoft Visual C++ .NET Chapter 4

  21. Tracing Program Execution with Step Commands • The Continue command resumes program execution, which continues until it encounters the next breakpoint • To practice tracing program execution, follow the steps listed on pages 201 and 202 Microsoft Visual C++ .NET Chapter 4

  22. Tracing Program Execution with Step Commands • A breakpoint is a position in the code at which program execution enters break mode • You add a breakpoint to your code by clicking the left margin next to a statement or by right-clicking a statement and selecting Insert Breakpoint from the Shortcut menu • To practice using breakpoints, perform the processes shown on pages 203 and 204 Microsoft Visual C++ .NET Chapter 4

  23. Breakpoints in the Code Editor Window Microsoft Visual C++ .NET Chapter 4

  24. Tracing Variables and Expressions with Debug Windows • As you trace program execution using step commands and breakpoints, you might also need to trace how variables and expressions change during the course of program execution • In addition to using the debug windows to learn the value of a variable during program execution, in break mode you can learn the value of a variable by holding your mouse over the variable Microsoft Visual C++ .NET Chapter 4

  25. Monitoring Variables • The debugger contains three windows that monitor variables as you step through a program in break mode: • Autos • Locals • Watch • In any of these windows, the most recently modified variable is highlighted in red Microsoft Visual C++ .NET Chapter 4

  26. Using the Autos Window • The Autos window displays variables within the current statement and the previous statement • You use the Autos window to monitor variables within a specific statement • The Autos window helps you see how different values affect program execution Microsoft Visual C++ .NET Chapter 4

  27. Using the Locals Window • The Locals window displays all local variables within the currently executing function, regardless of whether they have been initialized • The Locals window helps you see how different values in the currently executing function affect program execution • To practice tracing variables with the Autos and Locals windows, use the instructions shown on pages 206 and 207 of the textbook Microsoft Visual C++ .NET Chapter 4

  28. The Watch Window • The Watch window is used for monitoring and changing specific variables that you enter • Unlike Autos and Locals windows, which automatically show the values of variables according to the current scope, you must manually enter into the Watch window the variables that you want to monitor Microsoft Visual C++ .NET Chapter 4

  29. QuickWatch • QuickWatch is a dialog box that you can use to quickly examine the value of a single variable or expression • Once you are in break mode, you can display QuickWatch by selecting QuickWatch from the Debug menu Microsoft Visual C++ .NET Chapter 4

  30. QuickWatch • You can also view the values of variables or expressions by highlighting them in the Code Editor window and then selecting QuickWatch from the Debug menu • To use the Watch window and the QuickWatch dialog box to find a bug in the Moving Estimator program, follow the processes illustrated on pages 209 through 212 Microsoft Visual C++ .NET Chapter 4

  31. Watch Window with the iPianos Data Members Microsoft Visual C++ .NET Chapter 4

  32. The Call Stack Window • When you are working with a C++ program that contains multiple functions, the computer must remember the order in which functions are executed • The call stack refers to the order in which functions execute in a program • The ability to view the contents of a call stack is very useful when you are tracing logic errors in large programs with multiple functions Microsoft Visual C++ .NET Chapter 4

  33. The Call Stack Window • To step through some of the functions in the Moving Estimator program to observe the contents of the Call Stack window, use the steps listed on pages 212 and 213 of the textbook Microsoft Visual C++ .NET Chapter 4

  34. C++ Language Bugs and Debugging Resources • If you have tried everything you can think of to fix a bug in your program, consider the possibility that you might be encountering one of the known bugs in Visual C++ • Bugs can occur with Visual C++’s implementation of the C++ language or with the Visual C++ application itself • To see a list of known bugs in Visual C++ (as well as in other Microsoft development tools), visit the MSDN Bug Center at http://msdn.microsoft.com Microsoft Visual C++ .NET Chapter 4

  35. Summary • Syntax errors occur when you enter code that the compiler does not recognize • Logic errors are problems in the design of a program that prevent it from running as you anticipate it will run • You can use comments in your code to help locate bugs • Visual C++ provides a program called the debugger that contains several tools that can help you trace each line of code, creating a much more efficient method of finding and resolving logic errors Microsoft Visual C++ .NET Chapter 4

  36. Summary • A Debug build contains additional information that is required by the debugger tools • When you select the Run to Cursor command, the program runs normally until it reaches the statement where your cursor is located, at which point the program enters break mode • QuickWatch is a dialog box that you can use to quickly examine the value of a single variable or expression • If you have tried everything you can think of to fix a bug in your program, consider the possibility that you may be encountering one of the known bugs in Visual C++ Microsoft Visual C++ .NET Chapter 4

More Related