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.
gdb act1
break main
– tells GDB to pause right before entering mainrun 12345
– starts execution with the argument “15213”continue
– this continues execution until another break pointclear main
– remove the breakpoint at function mainrun 12345
– Q1.1: What happens now?disassemble main
– show the assembly instructions in mainbreak main
print (char*) 0x...
where...
is the literal hex value in themov
instruction before the call toprintf
– Q1.2: Does the printed value correspond to anything in the C code?run
(with some argument of your choice)print argv[1]
– Q1.3: What does this print out?continue
– Q1.4: Now what does this print out?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.
gdb act2
break main
run
print/x $rsi
–/x
means print in hexadecimalprint /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?disassemble main
break stc
– main calls thestc
function, so we will study that function toocontinue
– Q2.2: How could you view the arguments that have been passed to stc?run 18213
–gdb
will ask if you want to restart; chooseyes
continue
– Q2.3: Which function is in execution now?disassemble
stepi
nexti
– step through a single x86 instruction- press enter 3 to 4 times – GDB will repeat your previous instruction. Useful for single- stepping.
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.
cat act3.c
– display the source code of act3gdb 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)disassemble compare
– Q3.2: Where is the return value set in compare?break compare
- run
act3
with two numbers – Q3.3 Usingnexti
orstepi
, how does the value in register %rbx change, leading to thecmp
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.