Debugging

By now you have realized that no matter how careful we are, our first attempt to write a complex piece of software is likely to have errors, or "bugs" in it. In class we have talked about unit testing to identify whether there are errors in a component. In this lab, we concentrate on the skills of debugging, that is, or finding and fixing the errors themselves. In particular we introduce a tool called a symbolic debugger that can help you enormously in this process.

Debuggers

A debugger is a piece of software that takes your executable code and allows you to suspend execution and step through a line at a time and inspect the contents of memory. In a sense, it is much like a tape player with play, pause, and stop buttons. In this way, what happens between the time you start your program and the time it fails because of a bug doesn't have to be a mystery.

There are several features that are common to most debuggers:

Breakpoints
You can set a breakpoint at a line of your choice so that execution is suspended when the breakpoint is reached. You can examine the values of your variables, step to the next line of code, or resume normal execution until another breakpoint is reached.

Single-stepping
When the program is suspended, you have several options:
  • Step Over - execute one complete line of code and suspend again
  • Step Into - "descend" into any method calls contained in the line
  • Step Return - execute the rest of the current method and return to where it was called
  • Resume - continue execution normally (until the next breakpoint is hit)
Suspend (Pause)
Interrupt a running program to examine what it's doing.
In the next few pages you will have a chance to try out these features as implemented in Eclipse.

Aside: The Eclipse debugger vs. pythontutor

In lab 2 we introduced a tool called pythontutor that allows you to trace the execution of your code step by step and provides a nice visualization of objects and their instance variables. You might wonder why we need to learn to use the Eclipse debugger if we already have something like pythontutor. Pythontutor is great for learning and experimenting with small programs, but it is not able to handle larger programs like what we will be writing later in the semester.

The way that pythontutor works is actually very different from a "real" debugger, in that it doesn't actually execute your code as you step through it. Rather, it pre-executes the entire program ahead of time and saves a complete record of the values of all variables at every step, so that you can examine that record as though you were executing the code. It is this trick that allows you to run the code backward as well as forward in pythontutor. However, this strategy is limited to relatively small programs.