CS70, Fall 2000

Homework Assignment #5: Debugging

This assignment is due at 9 PM on Wednesday, October 4th, 2000. The assignment is a written one, and must be submitted as a single file, a README. Refer to the homework policies page for general homework guidelines.

This assignment has only one purpose, which is to familiarize you with interactive debuggers.

Overview

The program assign_05 is a simple file shuffler. It reads lines from a file (provided on the standard input device, cin), shuffles them into a random order, and writes them to its standard output, cout. Although it is quite simple, the program can be used to perform relatively complex tasks. For example, the class summary schedule was produced by running the CS70 class roster through a C version of the same program. A random line can be chosen from a file such as /etc/passwd with the command:

    ./assign_05 < /etc/passwd | head -1

To the best of my knowledge, the program is free of bugs. However, for instructional reasons it is much less efficient than it could be.

For this assignment, you will run the program under an interactive debugger and follow a list of steps. Most steps will require you to record the answer to a question. The final result will be submitted as a README file. Other than a file header (name, date, etc.), the README should not have "documentation" components such as the program purpose or input format. Please number your answers corresponding to the step in which the question is asked, leaving gaps if a step asks no question.

The assignment is written for use with the interactive debugger ddd on Turing. It is not possible to do the assignment with a different debugger, primarily because minor debugger variations make grading impossible. Also, it is probably not possible to do the assignment by using a Windows machine to connect to Turing, because ddd expects that you will be connecting from an X-compatible display, and to the best of my knowledge Windows machines can't do that securely.

The program for the assignment is implemented using 4 files: a Makefile, a main program (assign_05.cc), and a header file (line.hh) and implementation (line.cc) for a pair of classes that support some simple operations on text lines. There is also a sample input file (hw5-input.txt). As usual, you can download all of the files as a gzipped tar file or a zip archive.

Assignment Procedure

  1. Download the program, unpack it, and compile it by typing make.
  2. Make sure your preferred editor is set properly by typing "echo $EDITOR". If the response is the name of some other editor, fix it by typing "setenv EDITOR vi", "setenv EDITOR pico", or whatever is appropriate to your taste.
  3. Bring up the ddd debugger from the command line by typing ddd assign_05.
  4. Choose Run... from the Program menu. Specify hw5-input.txt in response to the prompt for arguments. You will see 16 words scroll by in the window at the bottom of the screen. Question: In order, what are they? (You may use cut-and-paste, and you don't have to list them one per line).
  5. Use File/Open Source... to bring up a list of the source files involved in the program. Most of them are system files that aren't of interest to us, so find line.cc and open it.
  6. Scroll down to find the function named LineGetter::getLine. Select the full name (including the scope qualifier) and click on the stop sign labeled "Break" to set a breakpoint. Run the program again, this time giving the arguments "-s 0 hw5-input.txt". Question: What happens?
  7. Use the Next button in the pop-up toolbar to step through the program until you reach the first "else if" statement. Rest the mouse on the string "nextCharacter". Question: What happens?
  8. Click anywhere on nextCharacter. Then click Display on the main toolbar. Click Next repeatedly until nextCharacter changes. Question: What is it?
  9. Question: Where are you in the program?
  10. Scroll down to find the line beginning "line = expand...". Click at the start of the line, and set a breakpoint. Hit the Cont (continue) button. Question: Where does the program stop?
  11. Run the program again, this time with the argument "assign_05.cc". Press Cont repeatedly until you reach the breakpoint at the expand call. Question: How many times did you hit Cont?
  12. Use the green Run button to run again. This time, type "continue 2" on your keyboard (the characters will appear in the small gdb window at the bottom). Question: Where does the program stop?
  13. Use Source/Breakpoints... to disable breakpoint #1 and run the program yet again. Question: What is the value of nextCharacter?
  14. Question: What is the value of length?
  15. Hit the Step button. Question: Where are we?
  16. Continue to get back to the breakpoint. Display line. Question: What is its hexadecimal value?
  17. Hit the Next button. Question: Where are we?
  18. The hex value of line represents the address at which that array of characters is stored. Question: Where is line stored?
  19. Use Source/Breakpoints to delete breakpoint 2 and enable breakpoint 1. Hit the Finish button. Question: Where are we?
  20. Question: What data is now displayed in the display window?
  21. Continue, then press the Up button. Question: Where are we?
  22. Display whereToPutIt. Select the ... inside the curly braces, and choose Show/Show All. Question: What is the value of _M_start?
  23. Double-click on _M_start or its value. Question: What happens?
  24. Click Undispl to un-display what you just saw. Select _M_start with a single click, then click and hold on Display to pick the get a pop-down menu. Choose Other.... Put an asterisk at the front of the expression, and "@5" at the end, and then display. Scroll to the right. Question: What do you see?
  25. Click Show. Question: Where are the 5 contents variables stored?
  26. Question: Why do the last two variables have those particular values?
  27. Continue to the next breakpoint, in getLine. Type next 4 to get to the while loop quickly; this makes length a valid variable. Click to select length, and then click Watch to set a watchpoint that will stop the program whenever length changes. (Warning: this can make your program run very slowly.) Finally, hit Cont. Question: Where does the program stop?
  28. Question: What are the old and new values of length?
  29. Click to select length, and then click Watch to delete the watchpoint. (This also works with breakpoints.) Also delete the breakpoint at the beginning of getLine. Scroll down to find any constructor for Line, and select the text Line::Line (without any parentheses). Click the Break button. Choose the all option in the dialog box. Continue the program. Question: Where does it stop?
  30. Select Status/Backtrace... to see who called whom. Question: Who called the function that called us?
  31. Delete the breakpoints on the default constructor and on the from-arguments constructor, and continue. Question: Ignoring functions that live in source files starting with "stl_", who called us?
  32. Click on the line for that routine to see its source file. Question: Why do you think the copy constructor got called?
  33. Use the Kill button to kill the current program. Make sure you are displaying assign_05.cc, and hit the Edit button to bring up your favorite editor. Find the call to drand48 at the very end of the file (inside the randomize function), and change it to read:
            unsigned int j = (unsigned int) lrand48();
    
    Exit your editor and hit the Make button in ddd to recompile the program. Use Source/Breakpoints to delete the remaining breakpoint, and run the program again with the arguments "-s 0 hw5-input.txt". Question: What happens, and where?
  34. Rest the mouse on source> in the line that refers to source.lineNumber. Question: What does the status line say?
  35. Rest the mouse at the lineNumber on the left of the assignment statement. Question: What does the popup say?
  36. Go up. Question: Where are you?
  37. Go up again. Question: What is the value of j?
  38. Select j and choose Display/Convert to Hex. Question: What is the value in hex?
  39. Question: Is the value "clean" in the sense of being a round number (ending in zeros)?
  40. Put the original code back in assign_05.cc (in case you've forgotten it, the line is:
            unsigned int j = (unsigned int) (drand48() * array.size());
    
    Recompile the program, and run it one last time with the arguments "-d -n -s 1 hw5-input.txt". Question: What are the 17 lines that are printed?

Submission Details

The only file you need to submit for this assignment is the README. Please use cs70submit to submit it, rather than cs70submitall, so that only the README will get picked up. If you use cs70submitall, you will merely clutter up the grader directories.

Don't forget to spell-check your file.

Future Efficiency

In a couple of places, assign_05.cc deliberately uses a very inefficient implementation for illustrative purposes. If you ever expect to use it in the future, you should change randomize accepts the array argument by reference, and returns void. Don't worry if you don't know how to do that right now; we'll cover references in class soon.


This page maintained by Geoff Kuenning.