Lab 1 -- GDB

Due: 5:00am, Friday October 11, 2024

Introduction

In this activity you will review the material on assembly instructions and use gdb to debug and interpret programs. This serves as practice for the next assignment. In order to get started. The starter code for this assignment is on Linux server here:

/export/home/public/schwesin/cpsc235/assignments/lab1-handout.tar

Copy the lab1-handout.tar to a (protected) directory on the Linux server. Then execute the command:

    tar xvf lab1-handout.tar

This will cause a number of files to be unpacked in the directory. Execute the command

    make

to build the executables.

Activity 1:

In this activity you will get familiar with using gdb to interpret program behavior. Follow along with these steps. Certain steps have a questions associated with them; put the answers to the questions in the questions.txt file.

  1. gdb act1
  2. break main – tells GDB to pause right before entering main
  3. run 12345 – starts execution with the argument “15213”
  4. continue – this continues execution until another break point
  5. clear main – remove the breakpoint at function main
  6. run 12345 – Q1.1: What happens now?
  7. disassemble main – show the assembly instructions in main
  8. break main
  9. print (char*) 0x... where ... is the literal hex value in the mov instruction before the call to printf – Q1.2: Does the printed value correspond to anything in the C code?
  10. run (with some argument of your choice)
  11. print argv[1] – Q1.3: What does this print out?
  12. continue – Q1.4: Now what does this print out?
  13. quit – exit GDB; agree to kill the running process

Activity 2:

In this activity you will get familiar with using gdb to step through assembly code and try to interpret what the program does Follow along with these steps. Certain steps have a questions associated with them; put the answers to the questions in the questions.txt file.

  1. gdb act2
  2. break main
  3. run
  4. print/x $rsi/x means print in hexadecimal
  5. print /x $rdi – Q2.1: RDI and RSI are registers that pass the first two arguments. Looking at their values, which is the first argument to main (the ‘argc’ argument)? Why?
  6. disassemble main
  7. break stc – main calls the stc function, so we will study that function too
  8. continue – Q2.2: How could you view the arguments that have been passed to stc?
  9. run 18213gdb will ask if you want to restart; choose yes
  10. continue – Q2.3: Which function is in execution now?
  11. disassemble
  12. stepi
  13. nexti – step through a single x86 instruction
  14. press enter 3 to 4 times – GDB will repeat your previous instruction. Useful for single- stepping.
  15. disassemble – Q2.4. Where are the “=>” characters printed on the left side?

Activity 3:

Program act3 will print out “good args!” if you type in the right numbers into the command line. Use GDB to find what numbers to use. Follow along with these steps. Certain steps have a questions associated with them; put the answers to the questions in the questions.txt file.

  1. cat act3.c – display the source code of act3
  2. gdb act3 – Q3.1: Which register holds the return value from a function? (Hint: Use disassemble in main and look at what register is used right after the function call to compare)
  3. disassemble compare – Q3.2: Where is the return value set in compare?
  4. break compare
  5. run act3 with two numbers – Q3.3 Using nexti or stepi, how does the value in register %rbx change, leading to the cmp instruction?

Handin Instructions

The provided makefile has a target named submit. To submit your assignment execute the command

    make submit

This will copy the appropriate files to a protected directory owned by your instructor.