Lab - GDB Refresher

For this lab you will be using gdb to analyze a binary. You will notice that this handout does not give you specific, line-by-line commands to execute. This is intentional. You are free to consult each other and the internet for tips and tricks.

You should solve this lab on either unix.qatar.cmu.edu or linux.andrew.cmu.edu.

Getting Started

  1. Download and unpack the starter files from here. (You should probably download them directly onto the remote unix server using wget or curl.)
  2. Look at the source code to make sure you understand what this simple code does.
  3. Use the Makefile to compile the binary.
  4. Run the program.

Using GDB to view assembly

  1. Use GDB to view the assembly code for orange and red. After you successfully do this, if you are a remote student then download this document containing the source code for orange. If you are in class, then simply ask the instructor for a print out of the document.
  2. On the document, write comments next to each line describing its purpose. You need to described the purpose, not the functionality. For example, don’t say “Move the value of RSP into RBP”, instead say “Save the current stack pointer as the base pointer for this frame”.

Using GDB to Analyze the Stack

Now that you can download and run the program, you will use GDB to analyze it and write down a copy of the stack frame (by hand, on paper).

  1. Load the program inside GDB.
  2. Set a breakpoint on the function red right before the call to printf.
  3. Run the program, and when the breakpoint triggers, take out a piece of paper and draw a diagram showing the current contents of the stack. Label each item on the stack with its purpose. (Note: This step will take you a while and may require typing multiple GDB commands.) Only include items related to stack frames for the functions orange and red.

Submission

When you finish, you should have two pieces of paper. The first contains an annotated version of the assembly code for orange. The second contains a diagram of the stack that shows the addresses, contents, and purpose of every item on the stack related to the stack frames for orange and red. Feel free to compare your results with that of other students, but do not copy from other students. Do the work yourself.