Skip to main content Link Menu Expand (external link) Document Search Copy Copied

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.

  1. Run GDB on the map executable.

  2. Set a breakpoint at the beginning of the program’s execution.

  3. Run the program until the breakpoint.

  4. What memory address does argv store?

  5. Describe what’s located at that memory address. (What does argv point to?)

  6. Step until you reach the first call to recur.

  7. What is the memory address of the recur function?

  8. Step into the first call to recur.

  9. Step until you reach the if statement.

  10. Switch into assembly view.

  11. Step over instructions until you reach the callq instruction (or the call instruction if you are using QEMU).

  12. What values are in all the registers?

  13. Step into the callq instruction.

  14. Switch back to C code mode.

  15. Now print out the current call stack. Hint: what does the backtrace command do?

  16. Now set a breakpoint on the recur function which is only triggered when the argument is 0.

  17. Continue until the breakpoint is hit.

  18. Print the call stack now.

  19. Now go up the call stack until you reach main. What was argc?

  20. Now step until the return statement in recur.

  21. Switch back into the assembly view.

  22. Which instructions correspond to the return 0 in C?

  23. Now switch back to the source layout.

  24. Finish the remaining 3 function calls.

  25. Run the program to completion.

  26. Quit GDB.