Introduction to gdb
- Due Nov 11, 2019 by 10pm
- Points 30
- Submitting a file upload
- File Types cast
Description
This lab will begin practicing working with gdb (the GNU DeBugger). A debugger is a powerful tool that you will want to have at your disposal because it lets you ask more effective questions about why your program is doing the things it is--especially when your program is doing things you don't want it to be doing. Some of the things you can do with a debugger include:
- Inspecting the value of a variable at program run time
- Stopping the execution of a program while still being able to continue running the program where it stopped
- Catching problems that should cause your program to crash before the program stops running
In order to begin getting comfortable with gdb, I am going to give you a program that implements a Queue. You have used a deque (double-ended queue) already for your checkout simulator, so you know that a queue adds elements to the back of the queue and removes them from the front. We call this behavior FIFO (first in, first out) as opposed to LIFO for a stack. The operation to add elements to a queue is called enqueue(); the operation to remove elements from a queue is called dequeue().
Procedure
You should download the file named queue.cpp, compile it, and run it. The program should crash when you do so. This is because I have given you a broken implementation of the Queue class. If you look real hard, you may be able to figure out what is wrong with the code, but I would like you to use the debugger to help you find and fix the errors. Yes, that is correct, there is more than one error in the program.
For each error that you find and fix, I would like you to perform the following procedure:
- Using gdb, find the problem. Remember the steps you used in gdb to diagnose the error. Please do not try to read the code deeply and just find the problem on your own.
- Run asciicast to record a scripted version of you finding the error. The script should show you running gdb, encountering the error, and using appropriate gdb commands to give you insight into the core problem. The script should also show you changing the C++ code to address the identified error, compiling the code, and running gdb a second time. Now, gdb should not show the error you fixed anymore. Since there are multiple errors in the lab, you may encounter a different error, but that is acceptable.
Repeat this procedure until you have a correctly working Queue. You should have 2-3 of these recordings by the time you are finished.
Helpful gdb commands
The following gdb commands will be helpful to you. You may wish to look online for additional help with the debugger.
- file executable : loads the executable for debugging. This is only necessary if you didn't start gdb by telling it which program to debug or if you want to switch programs that you are debugging
- break function : enables a stopping point when the program is running if it ever enters the named function
- break file.cpp:15 : enables a stopping point when the program is running if it ever runs code associated with line 15 of file.cpp
- start : starts running the program, but immediately stop when it gets to main
- next : run the very next line of code in your program. If that line is a function call, run until the function call is completed.
- step : run the very next line of code in your program. If that line is a function call, stop inside the function
- continue : run the program until it hits an enabled break
- <enter> : repeat the previous gdb command
- print variable : display the contents of variable
- display variable : display the contents of variable after every command that follows this one
- where : display the call stack to see which functions were called to get you to this point in the execution
- quit : exit the debugger
- run : start running the program from the very beginning