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
.
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.
Run GDB on the
map
executable.Set a breakpoint at the beginning of the program’s execution.
Run the program until the breakpoint.
What memory address does
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
callq
instruction (or thecall
instruction if you are using QEMU).What values are in all the registers?
Step into the
callq
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 wasargc
?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.