CS 54 - Lab 6


Introduction & Warnings

Today we will be looking at debugging tools and techniques that can be used both in UNIX and Windows. What that means is, you will be dealing with code that has bugs intentionally written into it and may go into infinite loops if you're not careful. Here are the well- and lesser-known ways to stop a UNIX program from going out of control:

UNIX Panic Buttons
ActionComment
Control + CInterrupt signal. This should be your first line of defense.
Control + \Quit signal. Try this when Control + C doesn't work.
kill -1 -1UNIX command that kills ALL processes you own. Use when neither of the above work.

Use these “Panic buttons” as necessary - no more no less (they're not a toy). Now that you know how to pull the emergency brake, go ahead and download today's files: (C++ Files) (Worksheet)
Untar the C++ files by using the following UNIX command:
tar zxvf lab06.tar.gz
You may do the worksheet using vim, TextPad, or any other text editor; it is your choice. Hard copies will NOT be accepted.


assert()

assert() is a function that takes in one argument: a boolean expression. If the expression is true, execution will continue normally. If it is false, then the program will abort itself and the operating system will print a message saying the assert() failed.

#include <cassert> /* include this for the assert() function */
#include <iostream>
using namespace std;

int main()
{
  double pi;

  cout << "Algebra Quiz: What is pi? ";
  cin >> pi;
  assert(pi == 3.141592);
  cout << "Correct! You know the Archimedes Ratio!" << endl;
  return 0;
}

Note there is no need for an if here because, if the user fails algebra, the program will abort before it congratulates the user on knowing the value of π. You are now ready to do Worksheet Questions 1 - 3.


g++ Revisited

In lab 00, we learned the compiler command:
g++ -Wall -W -s -pedantic-errors file.cpp -o executable
Remember what the s flag did? It meant “strip”, which strips away all excess information to make the executable as small as possible. But for this debugging lab, we want to add extra debug information to our executable. This raises the question: how will this help if our debug information if “stripped” away? The answer, obviously, is that stripping debug information away is of no help to us and we will therefore eliminate the s flag from our g++ command. This gives rise to another question: if we no longer strip away such debug information, how do we add this debug information in the first place? Well, we just add the ggdb flag (the g flag adds debug information and the gdb subflag adds information the gdb debugger). So, our new compiler command for this lab will be:
g++ -Wall -W -ggdb -pedantic-errors file.cpp -o executable

Now we can compile any file from lab06.tar.gz as above. For instance, to compile assert.cpp, try this command:
g++ -Wall -W -ggdb -pedantic-errors assert.cpp -o assert


gdb Debugger

After compiling the file you're interested in, you can now load it in gdb by typing the command:
gdb executable
gdb will now load and wait for commands at a (gdb) prompt:
(introductory copyright information omitted)

(gdb)

Think of this prompt like any other: a stupid slave waiting for commands from its cruel master. Here is a sketch of gdb commands:

gdb Commands
CommandComment
runRuns the loaded program
break nPlaces a breakpoint at line n (where n is an integer)
break fPlaces a breakpoint at the beginning of the function f (where f is a function name)
stepSteps the code one line, proceeding through functions
continueContinues until the next breakpoint or the program ends/crashes.
backtraceTraces and displays the function calls that lead us to this point in the code
print xPrint the value of the variable x
quitQuits gdb

One word on what a breakpoint is: a breakpoint is a place where you are telling the debugger to pause the program's execution so you can run any of the other commands from the table above. You are now ready to do Worksheet Questions 3 - 5.


Grading & Submission

Each question on the worksheet is worth 2 points. Questions 1 - 5 are required, and Question 6 is extra credit. Turn in your completed worksheet on Blackboard when you're done.


[Dilbert]