Docsity

Debugging with Xcode: A Handout for CS106B Students, Study notes of Programming Abstractions

Toggle sidebar
bg1
CS106B Handout #7M
Winter 07-08 January 14, 2008
Debugging with Xcode
This handout was originally by Eric Roberts and Stacey Dorr. Revised by the Justins and Julie, and again by Jason and Julie
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
The purpose of this handout is twofold: to give you a sense of the philosophy of debugging and
to teach you how to use some of the practical tools 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 rules below will probably help. What you need is insight,
creativity, logic, and determination.
The programming process leads you through a series of tasks and roles:
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 is extremely 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. Debugging will almost always take more time than the first three tasks
combined. Therefore, 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 and later in your career as a programmer.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Debugging with Xcode: A Handout for CS106B Students and more Study notes Programming Abstractions in PDF only on Docsity!

CS106B Handout #7M Winter 07-08 January 14, 2008

Debugging with Xcode

This handout was originally by Eric Roberts and Stacey Dorr. Revised by the Justins and Julie, and again by Jason and Julie 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 The purpose of this handout is twofold: to give you a sense of the philosophy of debugging and to teach you how to use some of the practical tools 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 rules below will probably help. What you need is insight, creativity, logic, and determination. The programming process leads you through a series of tasks and roles: 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 is extremely 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. Debugging will almost always take more time than the first three tasks combined. Therefore, 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 and later in your career as a programmer.

The Eleven 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.
  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. Be 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 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 buggy code which does not seem to be related to the bug you were tracking, fix the bug code anyway. Many times the buggy code is related to or obscures the original 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 which 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. In that case, only when the facts have proven without question that the function is the source of the problem will you be able to see the bug.
  10. You need to be systematic, but 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. Many times a programmer can spend

what you would expect. Usually there is a buffer that stores the data piped to calls to cout. The data in the buffer is only displayed on the screen once endl is sent, or a function requiring input is called (like GetInteger() or GetLine() ). For example: int main() { int i, int j; i = ComplicatedFn(); cout << "got past ComplicatedFn(); i = " << i << "."; // debug j = ComplicatedFn2(); cout << "got past ComplicatedFn2(); j = " << j << "."; // debug ComplicatedFn3(&i, &j); cout << "done: i = " << i << ", j = " << j << endl; // debug return 0; } Assuming no other i/o calls are in the other functions, no cout statements will print until the last cout call is executed, since it has the first (and only) endl call. Therefore, if your program crashes in the middle of ComplicatedFn2() , the output window will not have printed out anything, even though a cout call had been passed. Therefore, it is always a good idea to attach endl to all debugging statements, so the buffer is flushed and the debug statements print to the screen when they are passed. Even though cout statements may seem like a sufficient means for debugging, it becomes largely inadequate for more complicated programs, especially those which make many iterations over the same code during a single program run. In cases where cout -style debugging is largely inefficient and/or inadequate, we turn to the debugger. 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 usually come equipped with a debugger, which is a special facility for monitoring a program as it runs. By using the Xcode 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.

Using the Xcode debugger Figure 1: The Xcode Debugger Window The Xcode debugger is a complicated environment, but with a little patience, you should be able to learn it to the point where you code more efficiently and productively. Under the Build menu, there are two menu items for executing your code: the Run ( Cmd-R ) option and the Debug ( Cmd-Y ) option. So far you've probably been using Run, which sets up and runs your program on its own until it completes successfully or halts abnormally because of a fatal error. The Debug option, on the other hand, will set up and run your program in the context of the debugger. The debugger gives you the ability to stop your program mid-stream, poke around and examine the values of variables, and investigate the aftermath after a fatal error

Once your program is stopped, the bottom pane of the debugger window will show the current function that is executing and a red arrow to the left of the code shows the next line to be executed. Choosing Continue would cause your program to continue executing freely while the three Step buttons allow you to manually control what to execute and then return control to the debugger. Step Over is useful whenever you know that the function on that line works and that the bug is further on. For example, you would hate to trace through the steps involved in each call to cout , and the Step Over button allows us to skip all of the details. In Figure 1, calling Step Over would execute the GetDistance() call and assign it to distanceFromOrigin before control returns to the debugger after executing that line. The S t e p Into command makes it possible to drop down one level in the stack and trace through the execution of a function or a procedure. In Figure 1, the debugger would create the new stack frame for the GetDistance() function and return control back to the debugger at the first statement of GetDistance(). The Step Out command executes the remainder of the current function and returns control to the debugger once that function returns. At this stopped point, there are three panes in the window filled with information. The left pane shows the current stack frame. Here we see what calls have been made that have led to the current call. The most recent call is listed at the top, while _ pthread_start (which is an Xcode system function) is at the top. In Figure 1, we see that main() called function Binky() , which called GetCoordsAndReturnDistance(). When we click on a function listed in this pane, the frames on the right and bottom (discussed below) will update to show the values and information for the selected function. This essentially allows you to look at the entire state of your program, not just the current function, any time the program is stopped. The right pane contains all variables and associated values contained in the stack frame highlighted in the left pane. In Figure 1, we see distanceFromOrigin has quite a strange value. Looking at the code (and using our mad debugging skills) we see that distanceFromOrigin has yet to be initialized at this point in execution and thus has a junk value. Also note that some variable types (such as structs, classes, and pointers) have an arrow to the left of them. This means that there is more information pertaining to this data type that can be viewed. For instance, by clicking on the arrow next to p , which is a variable of type point , we see the values for the x and y variables, which are members of the point struct. The value of ySquared is 0x168fa0. This is the address (in hexadecimal) of the chunk of heap memory to which it is pointing. This may be interesting, but usually not what we want. Clicking on the arrow to the left reveals what is truly useful, the value of *ySquared , set to 16 in this case. In Xcode, when a variable changes values it is highlighted in red, so as to inform you of the update. A highlighted value returns to black once the next call is executed. The bottom pane contains the program’s code with breakpoint information and current point of execution. The red arrow in the bottom frame indicates the next line of code to be executed. Clicking on functions listed in the stack frame will adjust this window accordingly and change the red arrow to indicate the next statement to be executed for that particular function. Thread Switching Occasionally, when you get an error or press the Pause button, the stack trace will be some thing that looks totally unlike any code you’ve written. This is because the compiler is actually running

a few other “threads” of code other than your own to enable the code to run properly. If you get such a stack trace, click on the name of the thread: You will get a list of different threads. Select different ones until you get one that looks like your code (usually it will be Thread-2). Using breakpoints Clicking in the narrow column to the left of the code displayed in the bottom pane sets a breakpoint at the indicated line. A second or two after you click on the side, a blue pentagon sign appears to indicate that there is a breakpoint set on that line. When the program is started by the Build and Debug command, it checks to see whether there is a breakpoint on each line that it executes. If there is, the program stops at that point. Clicking on the pentagon removes the breakpoint. Breakpoints are an essential component of debugging. A general strategy is to set a few breakpoints throughout your program; usually around key spots that you know may be bug- prone, such as computationally-intensive or pointer-intensive areas. Then, run your program until you get to a breakpoint. Step over things for a few lines, and look at your variable values. Maybe step out to the outer context, and take a look to see that the values of your variables are still what they should be. If they're not, then you have just executed over code that contains one or more bugs. If things look well, continue running your program until you hit the next breakpoint. Wash, rinse, repeat. Getting to the scene of the crime While running the debugger, it is usually easy to see where exactly a program crashes. On a memory error where a dialog box pops up, such as an "Access fault exception", the program immediately halts, and the debugger window presents the current state of the program right up to the illegal memory access. Even though the program has terminated, you can see exactly where it stopped, dig around and look at variable values, look at other stack frames and the variable values in those calls, and do some serious detective work to see what went wrong. A call to Error() has a similar behavior. If there are cases which shouldn’t happen when the code is running correctly but might if there is a bug, you can add checks for them and call Error() if the checks turn out true. This means that if one of those cases occurs, the debugger will stop on the Error() line so you can look around and see what’s going wrong. Sometimes it is not obvious at all as to what is going on and you don't know where to start. Error() s aren't being triggered, and there aren't memory exceptions being raised, but you know