33-debugging.pdf

(608 KB) Pobierz
Mehran Sahami
Handout #33
CS 106A
November 5, 2007
Debugging
Thanks to Eric Roberts and Nick Parlante for portions of this handout.
Much of your time as a computer programmer will likely be spent debugging. This
phenomena is best described by a quotation from one of the first computer pioneers,
Maurice Wilkes:
As soon as we started programming, we found to our surprise that it wasn’t as
easy to get programs right as we had thought. We had to discover debugging. I
can remember the exact instant when I realized that a large part of my life from
then on was going to be spent in finding mistakes in my own programs.
—Maurice Wilkes, 1949
In order to be better prepared to undertake the more complex future debugging that you
will be doing, we aim to give you here both a sense of the philosophy of debugging as
well as to teach you how to use some of the practical tips that make testing and debugging
easier.
The philosophy of debugging
Debugging is one of the most creative and intellectually challenging aspects of
programming. It can also be one of the most frustrating. To a large extent, the problems
that people face debugging programs are not so much technical as they are psychological.
To become successful debuggers, you must learn to think in a different way. There is no
cookbook approach to debugging, although Nick Parlante’s 11 Truths of Debugging
(given below) will probably help. What you need is insight, creativity, logic, and
determination. I've heard that Diet Coke™ helps too, but this may be anecdotal…
As computer scientists, it is important to remember that the programming process leads
you through a series of tasks and roles:
Task
Role
Design
Architect
Coding
Engineer
Testing
Vandal
Debugging —
Detective
These roles require you to adopt distinct strategies and goals, and it is often difficult to
shift your perspective from one to another. Although debugging can often be very
difficult, it can be done. It will at times take all of the skill and creativity at your disposal,
but you can succeed if you are methodical and don’t give up on the task.
Debugging is an important skill that you will use every day if you continue in Computer
Science or any related field. Even though it is the final task of those listed above, it is
certainly not the least important. You should always plan ahead and allow sufficient time
for testing and debugging, as it is required if you expect to produce quality software. In
addition, you should make a concentrated effort to develop these skills now, as they will
be even more important as programs become more complicated later in the quarter.
 
– 2 –
The 11 Truths of Debugging
1. Intuition and hunches are great—you just have to test them out. When a hunch and a fact
collide, the fact wins. That's life in the city.
2. Don’t look for complex explanations. Even the simplest omission or typo can lead to very
weird behavior. Everyone is capable producing extremely simple and obvious errors from
time to time. Look at code critically—don’t just sweep your eye over that series of simple
statements assuming that they are too simple to be wrong.
3. The clue to what is wrong in your code is in the values of your variables and the flow of
control. Try to see what the facts are pointing to. The computer is not trying to mislead
you. Work from the facts.
4. Be systematic and persistent. Don’t panic . The bug is not moving around in your code,
trying to trick or evade you. It is just sitting in one place, doing the wrong thing in the same
way every time.
5. If you code was working a minute ago, but now it doesn’t—what was the last thing you
changed? This incredibly reliable rule of thumb is the reason your section leader told you
to test your code as you go rather than all at once.
6. Do not change your code haphazardly trying to track down a bug. This is sort of like a
scientist who changes more than one variable in an experiment at a time. It makes the
observed behavior much more difficult to interpret, and you tend to introduce new bugs.
7. If you find some wrong code that does not seem to be related to the bug you were tracking,
fix the wrong code anyway. Many times the wrong code was related to or obscured the bug
in a way you had not imagined.
8. You should be able to explain in Sherlock Holmes style the series of facts, tests, and
deductions that led you to find a bug. Alternately, if you have a bug but can’t pinpoint it,
then you should be able to give an argument to a critical third party detailing why each one
of your functions cannot contain the bug. One of these arguments will contain a flaw since
one of your functions does in fact contain a bug. Trying to construct the arguments may
help you to see the flaw.
9. Be critical of your beliefs about your code. It’s almost impossible to see a bug in a function
when your instinct is that the function is innocent. Only when the facts have proven
without question that the function is not the source of the problem should you assume it to
be correct.
10. Although you need to be systematic, there is still an enormous amount of room for beliefs,
hunches, guesses, etc. Use your intuition about where the bug probably is to direct the
order that you check things in your systematic search. Check the functions you suspect the
most first. Good instincts will come with experience.
11. Debugging depends on an objective and reasoned approach. It depends on overall
perspective and understanding of the workings of your code. Debugging code is more
mentally demanding than writing code. The longer you try to track down a bug without
success, the less perspective you tend to have. Realize when you have lost the perspective
on your code to debug. Take a break. Get some sleep. You cannot debug when you are not
seeing things clearly. Many times a programmer can spend hours late at night hunting for a
bug only to finally give up at 4:00 A.M. The next day, they find the bug in 10 minutes. What
allowed them to find the bug the next day so quickly? Maybe they just needed some sleep
and time for perspective. Or maybe their subconscious figured it out while they were asleep.
In any case, the ―go do something else for a while, come back, and find the bug
immediately‖ scenario happens too often to be an accident.
— Nick Parlante, Stanford University
824131509.621.png 824131509.632.png 824131509.643.png
 
– 3 –
Using an online debugger
Because debugging is a difficult but nonetheless critical task, it is important to learn the
tricks of the trade. The most important of these tricks is to get the computer to show you
what it’s doing, which is the key to debugging. The computer, after all, is there in front
of you. You can watch it work. You can’t ask the computer why it isn’t working, but
you can have it show you its work as it goes. Modern programming environments like
Eclipse come equipped with a debugger, which is a special facility for monitoring a
program as it runs. By using the Eclipse debugger, for example, you can step through the
operation of your program and watch it work. Using the debugger helps you build up a
good sense of what your program is doing, and often points the way to the mistake.
To illustrate the operation of the Eclipse debugger in as concrete a way as possible, let's
look at how we might us the debugger to find bugs in the Roulette.java program
shown in Figure 2 below. As the bug icons indicate, the program is buggy. As a
programmer, it is your job to figure out why. The remainder of this handout describes the
techniques you might use to look for bugs with the help of the Eclipse debugger.
– 4 –
Figure 2. Buggy program intended to play a simplified form of roulette
/*
* File: Roulette.java
* -------------------
* This program simulates a small part of the casino game of
* roulette.
*/
import acm.program.*;
import acm.util.*;
public class Roulette extends ConsoleProgram {
/** Amount of cash with which the player starts */
private static final int STARTING_MONEY = 100;
/** Amount wagered in each game */
private static final int WAGER_AMOUNT = 10;
/** Runs the program */
public void run() {
giveInstructions();
playRoulette();
}
/**
* Plays roulette until the user runs out of money.
*/
private void playRoulette() {
int money = STARTING_MONEY;
while (money > 0) {
println("You now have $" + money + ".");
String bet = readLine("Enter betting category: ");
int outcome = spinRouletteWheel();
if (isWinningCategory(outcome, bet)) {
println("That number is " + bet + " so you win.");
money += WAGER_AMOUNT;
} else {
println("That number is not " + bet + " so you lose.");
money -= WAGER_AMOUNT;
}
}
println("You ran out of money.");
}
824131509.011.png
 
824131509.032.png 824131509.043.png 824131509.054.png 824131509.065.png 824131509.076.png 824131509.086.png 824131509.097.png 824131509.108.png 824131509.119.png 824131509.130.png 824131509.141.png 824131509.152.png 824131509.163.png 824131509.174.png 824131509.185.png 824131509.196.png 824131509.207.png 824131509.218.png 824131509.229.png 824131509.240.png 824131509.251.png 824131509.262.png 824131509.273.png 824131509.284.png 824131509.295.png 824131509.306.png 824131509.317.png 824131509.328.png 824131509.339.png 824131509.350.png 824131509.361.png 824131509.372.png 824131509.383.png 824131509.394.png 824131509.405.png 824131509.416.png 824131509.427.png 824131509.438.png 824131509.449.png 824131509.460.png 824131509.471.png 824131509.482.png 824131509.493.png 824131509.504.png 824131509.515.png 824131509.526.png 824131509.537.png 824131509.548.png 824131509.559.png 824131509.570.png 824131509.581.png 824131509.592.png 824131509.603.png 824131509.614.png 824131509.618.png 824131509.619.png 824131509.620.png 824131509.622.png 824131509.623.png 824131509.624.png 824131509.625.png 824131509.626.png 824131509.627.png 824131509.628.png 824131509.629.png 824131509.630.png 824131509.631.png 824131509.633.png 824131509.634.png 824131509.635.png 824131509.636.png 824131509.637.png 824131509.638.png 824131509.639.png 824131509.640.png 824131509.641.png 824131509.642.png 824131509.644.png 824131509.645.png 824131509.646.png 824131509.647.png 824131509.648.png 824131509.649.png 824131509.650.png 824131509.651.png 824131509.652.png 824131509.653.png 824131509.001.png 824131509.002.png 824131509.003.png 824131509.004.png 824131509.005.png 824131509.006.png 824131509.007.png 824131509.008.png 824131509.009.png 824131509.010.png 824131509.012.png 824131509.013.png 824131509.014.png 824131509.015.png 824131509.016.png 824131509.017.png 824131509.018.png 824131509.019.png 824131509.020.png 824131509.021.png 824131509.022.png 824131509.023.png 824131509.024.png 824131509.025.png 824131509.026.png 824131509.027.png 824131509.028.png 824131509.029.png 824131509.030.png 824131509.031.png 824131509.033.png 824131509.034.png 824131509.035.png 824131509.036.png 824131509.037.png 824131509.038.png 824131509.039.png 824131509.040.png 824131509.041.png 824131509.042.png 824131509.044.png 824131509.045.png 824131509.046.png 824131509.047.png 824131509.048.png 824131509.049.png 824131509.050.png 824131509.051.png 824131509.052.png 824131509.053.png 824131509.055.png 824131509.056.png 824131509.057.png 824131509.058.png 824131509.059.png 824131509.060.png 824131509.061.png 824131509.062.png 824131509.063.png 824131509.064.png 824131509.066.png 824131509.067.png 824131509.068.png 824131509.069.png 824131509.070.png 824131509.071.png 824131509.072.png 824131509.073.png 824131509.074.png 824131509.075.png
 
– 5 –
Figure 2. Buggy program intended to play a simplified form of roulette (continued)
/**
* Simulates the spinning of the roulette wheel. The method
* returns the number of the slot into which the ball fell.
*/
private int spinRouletteWheel() {
println("The ball lands in " + rgen.nextInt(0, 36) + ".");
return rgen.nextInt(0, 36);
}
/*
* Returns true if the outcome matches the category specified
* by bet. If the player chooses an illegal betting
* category, this function always returns false.
*/
private boolean isWinningCategory(int outcome, String bet) {
if (bet == "odd") {
return outcome % 2 == 1;
} else if (bet == "even") {
return (outcome % 2 == 0);
} else if (bet == "low") {
return (1 <= outcome && outcome <= 18);
} else if (bet == "high") {
return (19 <= outcome && outcome <= 36);
} else {
return (false);
}
}
/**
* Welcomes the player to the game and gives instructions on
* the rules of roulette.
*/
private void giveInstructions() {
println("Welcome to the roulette table!");
println("Roulette is played with a large wheel divided into");
println("compartments numbered from 0 to 36. Each player");
println("places bets on a playing field marked with the");
println("numbers and various categories. In this game,");
println("the only legal bets are the following categories:");
println("odd, even, low, or high. Note that 0 is not in any");
println("category. After the bet is placed, the wheel is");
println("spun, and a marble is dropped inside, which bounces");
println("around until it lands in a compartment. If the");
println("compartment matches the betting category you chose,");
println("you win back your wager plus an equal amount. If");
println("not, you lose your wager.");
}
/* Private instance variables */
private RandomGenerator rgen = new RandomGenerator();
}
824131509.077.png 824131509.078.png 824131509.079.png 824131509.080.png 824131509.081.png 824131509.082.png 824131509.083.png 824131509.084.png 824131509.085.png 824131509.087.png 824131509.088.png 824131509.089.png 824131509.090.png 824131509.091.png 824131509.092.png 824131509.093.png 824131509.094.png 824131509.095.png 824131509.096.png 824131509.098.png 824131509.099.png 824131509.100.png 824131509.101.png 824131509.102.png 824131509.103.png 824131509.104.png 824131509.105.png 824131509.106.png 824131509.107.png 824131509.109.png 824131509.110.png 824131509.111.png 824131509.112.png 824131509.113.png 824131509.114.png 824131509.115.png 824131509.116.png 824131509.117.png 824131509.118.png 824131509.120.png 824131509.121.png 824131509.122.png 824131509.123.png 824131509.124.png 824131509.125.png 824131509.126.png 824131509.127.png 824131509.128.png 824131509.129.png 824131509.131.png 824131509.132.png 824131509.133.png 824131509.134.png 824131509.135.png 824131509.136.png 824131509.137.png 824131509.138.png 824131509.139.png 824131509.140.png 824131509.142.png 824131509.143.png 824131509.144.png 824131509.145.png 824131509.146.png 824131509.147.png 824131509.148.png 824131509.149.png 824131509.150.png 824131509.151.png 824131509.153.png 824131509.154.png 824131509.155.png 824131509.156.png 824131509.157.png 824131509.158.png 824131509.159.png 824131509.160.png 824131509.161.png 824131509.162.png 824131509.164.png 824131509.165.png 824131509.166.png 824131509.167.png 824131509.168.png 824131509.169.png 824131509.170.png 824131509.171.png 824131509.172.png 824131509.173.png 824131509.175.png 824131509.176.png 824131509.177.png 824131509.178.png 824131509.179.png 824131509.180.png 824131509.181.png 824131509.182.png 824131509.183.png 824131509.184.png 824131509.186.png 824131509.187.png 824131509.188.png 824131509.189.png 824131509.190.png 824131509.191.png 824131509.192.png 824131509.193.png 824131509.194.png 824131509.195.png 824131509.197.png 824131509.198.png 824131509.199.png 824131509.200.png 824131509.201.png 824131509.202.png 824131509.203.png 824131509.204.png 824131509.205.png 824131509.206.png 824131509.208.png 824131509.209.png 824131509.210.png 824131509.211.png 824131509.212.png 824131509.213.png 824131509.214.png 824131509.215.png 824131509.216.png 824131509.217.png 824131509.219.png 824131509.220.png 824131509.221.png 824131509.222.png 824131509.223.png 824131509.224.png 824131509.225.png 824131509.226.png 824131509.227.png 824131509.228.png 824131509.230.png 824131509.231.png 824131509.232.png 824131509.233.png 824131509.234.png 824131509.235.png 824131509.236.png 824131509.237.png 824131509.238.png 824131509.239.png 824131509.241.png 824131509.242.png 824131509.243.png 824131509.244.png 824131509.245.png 824131509.246.png 824131509.247.png 824131509.248.png 824131509.249.png 824131509.250.png 824131509.252.png 824131509.253.png 824131509.254.png 824131509.255.png 824131509.256.png 824131509.257.png 824131509.258.png 824131509.259.png 824131509.260.png 824131509.261.png 824131509.263.png 824131509.264.png 824131509.265.png 824131509.266.png 824131509.267.png 824131509.268.png 824131509.269.png 824131509.270.png 824131509.271.png 824131509.272.png 824131509.274.png 824131509.275.png 824131509.276.png 824131509.277.png 824131509.278.png 824131509.279.png 824131509.280.png 824131509.281.png 824131509.282.png 824131509.283.png 824131509.285.png 824131509.286.png 824131509.287.png 824131509.288.png 824131509.289.png 824131509.290.png 824131509.291.png 824131509.292.png 824131509.293.png 824131509.294.png 824131509.296.png 824131509.297.png 824131509.298.png 824131509.299.png 824131509.300.png 824131509.301.png 824131509.302.png 824131509.303.png 824131509.304.png 824131509.305.png 824131509.307.png 824131509.308.png 824131509.309.png 824131509.310.png 824131509.311.png 824131509.312.png 824131509.313.png 824131509.314.png 824131509.315.png 824131509.316.png 824131509.318.png 824131509.319.png 824131509.320.png 824131509.321.png 824131509.322.png 824131509.323.png 824131509.324.png 824131509.325.png 824131509.326.png 824131509.327.png 824131509.329.png 824131509.330.png 824131509.331.png 824131509.332.png 824131509.333.png 824131509.334.png 824131509.335.png 824131509.336.png 824131509.337.png 824131509.338.png 824131509.340.png 824131509.341.png 824131509.342.png 824131509.343.png 824131509.344.png 824131509.345.png 824131509.346.png 824131509.347.png 824131509.348.png 824131509.349.png 824131509.351.png 824131509.352.png 824131509.353.png 824131509.354.png 824131509.355.png 824131509.356.png 824131509.357.png 824131509.358.png 824131509.359.png 824131509.360.png 824131509.362.png 824131509.363.png 824131509.364.png 824131509.365.png 824131509.366.png 824131509.367.png 824131509.368.png 824131509.369.png 824131509.370.png 824131509.371.png 824131509.373.png 824131509.374.png 824131509.375.png 824131509.376.png 824131509.377.png 824131509.378.png 824131509.379.png 824131509.380.png 824131509.381.png 824131509.382.png 824131509.384.png 824131509.385.png 824131509.386.png 824131509.387.png 824131509.388.png 824131509.389.png 824131509.390.png 824131509.391.png 824131509.392.png 824131509.393.png 824131509.395.png 824131509.396.png 824131509.397.png 824131509.398.png 824131509.399.png 824131509.400.png 824131509.401.png 824131509.402.png 824131509.403.png 824131509.404.png 824131509.406.png 824131509.407.png 824131509.408.png 824131509.409.png 824131509.410.png 824131509.411.png 824131509.412.png 824131509.413.png 824131509.414.png 824131509.415.png 824131509.417.png 824131509.418.png 824131509.419.png 824131509.420.png 824131509.421.png 824131509.422.png 824131509.423.png 824131509.424.png 824131509.425.png 824131509.426.png 824131509.428.png 824131509.429.png 824131509.430.png 824131509.431.png 824131509.432.png 824131509.433.png 824131509.434.png 824131509.435.png 824131509.436.png 824131509.437.png 824131509.439.png 824131509.440.png 824131509.441.png 824131509.442.png 824131509.443.png 824131509.444.png 824131509.445.png 824131509.446.png 824131509.447.png 824131509.448.png 824131509.450.png 824131509.451.png 824131509.452.png 824131509.453.png 824131509.454.png 824131509.455.png 824131509.456.png 824131509.457.png 824131509.458.png 824131509.459.png 824131509.461.png 824131509.462.png 824131509.463.png 824131509.464.png 824131509.465.png 824131509.466.png 824131509.467.png 824131509.468.png 824131509.469.png 824131509.470.png 824131509.472.png 824131509.473.png 824131509.474.png 824131509.475.png 824131509.476.png 824131509.477.png 824131509.478.png 824131509.479.png 824131509.480.png 824131509.481.png 824131509.483.png 824131509.484.png 824131509.485.png 824131509.486.png 824131509.487.png 824131509.488.png 824131509.489.png 824131509.490.png 824131509.491.png 824131509.492.png 824131509.494.png 824131509.495.png 824131509.496.png 824131509.497.png 824131509.498.png 824131509.499.png 824131509.500.png 824131509.501.png 824131509.502.png 824131509.503.png 824131509.505.png 824131509.506.png 824131509.507.png 824131509.508.png 824131509.509.png 824131509.510.png 824131509.511.png 824131509.512.png 824131509.513.png 824131509.514.png 824131509.516.png 824131509.517.png 824131509.518.png 824131509.519.png 824131509.520.png 824131509.521.png 824131509.522.png 824131509.523.png 824131509.524.png 824131509.525.png 824131509.527.png 824131509.528.png 824131509.529.png 824131509.530.png 824131509.531.png 824131509.532.png 824131509.533.png 824131509.534.png 824131509.535.png 824131509.536.png 824131509.538.png 824131509.539.png 824131509.540.png 824131509.541.png 824131509.542.png 824131509.543.png 824131509.544.png 824131509.545.png 824131509.546.png 824131509.547.png 824131509.549.png 824131509.550.png 824131509.551.png 824131509.552.png 824131509.553.png 824131509.554.png 824131509.555.png 824131509.556.png 824131509.557.png 824131509.558.png 824131509.560.png 824131509.561.png 824131509.562.png 824131509.563.png 824131509.564.png 824131509.565.png 824131509.566.png 824131509.567.png 824131509.568.png 824131509.569.png 824131509.571.png 824131509.572.png 824131509.573.png 824131509.574.png 824131509.575.png 824131509.576.png 824131509.577.png 824131509.578.png 824131509.579.png 824131509.580.png 824131509.582.png 824131509.583.png 824131509.584.png 824131509.585.png 824131509.586.png 824131509.587.png 824131509.588.png 824131509.589.png 824131509.590.png 824131509.591.png 824131509.593.png 824131509.594.png 824131509.595.png 824131509.596.png 824131509.597.png 824131509.598.png 824131509.599.png 824131509.600.png 824131509.601.png 824131509.602.png 824131509.604.png 824131509.605.png 824131509.606.png 824131509.607.png 824131509.608.png 824131509.609.png 824131509.610.png 824131509.611.png 824131509.612.png 824131509.613.png 824131509.615.png 824131509.616.png 824131509.617.png
 
Zgłoś jeśli naruszono regulamin