Compile-time and run-time debugging in emacs

The goal of this lab is to make sure you can use two crucial tools in this course: the g++ compiler and the gdb debugger. Both of these tools are run through emacs, which provides much support for their convenient usage.

Both tools provide good support for an essential programming task: debugging. The compiler itself helps you find compile-time errors, which are typically syntactic in nature, by (hopefully) pointing you to the offending line in the source code. The debugger helps you with what is often a more difficult task: finding run-time errors, which are typically errors in your programming logic.

We are providing two auxilliary documents which you should read while doing this lab:

You may, in fact, want to peruse these files first before doing the corresponding part of this lab. At any rate, by the end of this lab you should have completely read these other documents.

Compile-time errors (1 point)

  1. Go to your mc38 directory and copy the lab files to a subdirectory gdb by typing
      mkdir gdb
      cp ~mc38/labs/compile-debug/stats.cc gdb
    

  2. Change directory to the gdb directory.

  3. Launch emacs and load the program file stats.cc. Recall that you can invoke the emacs command find-file by typing C-x C-f. Alternatively, you can load the file while invoking emacs by typing
      emacs stats.cc &
    
    assuming you are in the directory containing stats.cc.

    The file stats.cc is a simple program which finds the mean, median and mode of an unsorted array. It almost works, but needs your help in fixing it.

  4. Compile the program within emacs using
      M-x compile
    
    The default command emacs offers to execute for compilation is make -k. This assumes the existence of a makefile which we do not have for this lab. You need to replace this with the appropriate call to g++. Using the online notes on Using g++  in the course homepage, figure out the one call to g++ which will compile the file into an executable program, give all possible warnings, generate information for gdb (the debugger), and name the executable file stats.

  5. You will find that g++ cannot compile the program. The code contains a few syntax errors which you are quite likely to make when you write programs. Use the control sequence C-x <backquote> to jump directly to errors found by g++. (The <backquote> key is the opposite of <apostrphe> and is usually in the upper left corner of the keyboard.) Find all the errors and correct them (there are six syntax errors altogether).

    You will find it useful to recompile after correcting a few errors as g++'s messages can change significantly after some corrections.

    You may also find it useful to use Emacs color highlighting as a guide to spotting syntax errors. Hitting C-l will update the colors to match the changes you've made.

  6. Check-off: Compile your program in Emacs, leaving the stats.cc and *compilation* buffers on your screen. Ask to be checked off for this part.

Run-time (or logic) error (1 point)

  1. Now that g++ is able to compile the code, you should get an executable file for output. Using either emacs's shell or the actual shell window, execute this file by simply typing:
    	./stats 
    
    Can you tell there is something wrong? This type of error, which occurs at run-time, tends to be significantly more tricky to correct than compile-time errors. It is for this type of error that the debugger gdb is particularly helpful.

  2. Run gdb within emacs using
    	M-x gdb
    
    This command will ask for an argument: the name of the executable we are going to debug. In this case, it is stats.

    Before actually starting the debugging process, you should get a basic understanding of what can gdb can do by reading through the online documentation Using gdb  in the course homepage. Additional documentation can be found through emac's Info system (C-h i).

    At this point, you should examine the program, and experiment with gdb to isolate the error (which function is likely to be responsible?). In particular, you will need to set breakpoints (either use C-x SPC in the code, or tell it to break at a given procedure), examine the contents of variables, and step through program statements. We find it especially useful to display the variables using display instead of print, since display will display the specified variables each time you step through the program.

    Do not forget to execute the program within gdb (using run), otherwise nothing will happen. Also, a plain run thereafter will restart your program from the top.

    We reassure you that the error can be corrected by changing only one line.

    When you think you have found the error: correct it, save the file, recompile and execute to see if the problem is solved.

  3. Just to exercise your gdb skills, restart gdb on your corrected version and find the values of a[j] and a[j+1] at the test statement on the third pass of both the outer and inner loops (i.e. pass = 3 and j = 2) of the bubbleSort function.

  4. Check-off: Ask to be checked off on this portion of the lab. You should have on your screen the line(s) of the program you changed and the *gud-stats* buffer.