GDB basics
Now we’re going to use a sample program, map
, for some GDB practice. The map
program is designed to print out its own executing structure. Before you start, be sure to take a look at map.c
and recurse.c
which form the program. Once you feel familiar with the program, you can compile it by running make map
.
Important: To run the program please use i386-exec ./map
..
If you’re curious: The “i386-“ tools in this homework are necessary to ensure consistent results across architectures. “i386” was the first 32-bit x86 CPU, which is the architecture we’ll be using in the Pintos projects. We’ve installed wrapper scripts around qemu-i386
to enable x86 user space emulation on your machine—even if it’s ARM-based. This allows you to run x86 executables on any architecture by “imitating” a virtual CPU. If you’re (even more) curious about how this magic works, take a look at QEMU!
Write down the commands you use to complete each step of the following walk-through. Be sure to also record and submit your answers to all questions in bold to Gradescope. We highly recommend this site for an easy-to-read GDB refresher.
Run GDB on the
map
executable by running:bash i386-gdb-map.sh
. This ensures a consistent environment. Do NOT usegdb map
ori386-gdb map
directly; these will lead to different results.Set a breakpoint at the beginning of the program’s execution.
Continue the program until the breakpoint.
What memory address does the variable
argv
store?Describe what’s located at that memory address. (What does
argv
point to?)Step until you reach the first call to
recur
.What is the memory address of the
recur
function?Step into the first call to
recur
.Step until you reach the
if
statement.Switch into assembly view.
Step over instructions until you reach the
call
instruction.What values are in the
eax
,esp
, andeip
registers?Step into the
call
instruction.Switch back to C code mode.
Now print out the current call stack. Hint: what does the
backtrace
command do?Now set a breakpoint on the
recur
function which is only triggered when the argument is 0.Continue until the breakpoint is hit.
Print the call stack now.
Now go up the call stack until you reach
main
. What is the return address to themain
function? (To elaborate, this is the pointer to the next instruction that will be executed inmain
.)Now step until the return statement in
recur
.Switch back into the assembly view.
Which instructions correspond to the
return 0
in C?Now switch back to the source layout.
Finish the remaining 3 function calls.
Run the program to completion.
Quit GDB.