CSE 4208: Studio 3

Control Flow and Debugging


In today's studio you will explore how to track program control flow and corresponding changes to program state, using the gdb debugger. Specifically, you will build and run and trace a program that implements a simple prefix addition calculator for expressions involving symbols for an addition (+) operator and unsigned integers, which are given to the program as command line arguments. Your experience running using gdb to trace program behavior will help you prepare to dubug program behavior in your subsequent lab and studio assignments this semester, especially if you complete the optional enrichment exercises where you will trace the program's behavior using gdb in emacs, or using the Visual Studio debugger available on the windows machines in some of the teaching labs in Urbauer Hall.


Please complete the following required exercises. I encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is also allowed.

As you work through these exercises, please record your answers, and when you finish them please log into Canvas, select this course in this semester, and then upload a .txt file containing your answers on the Canvas page for this studio assignment. Only one submission per team, please, and if you need to re-submit it the person who originally submitted the studio should please be the one to do that.

Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.


Required Exercises

  1. As the answer to the first exercise, list the names of the people who worked together on this studio.
  2. First, ssh into shell.cec.wustl.edu using your WUSTL Key id and password and then use qlogin to log into one of the Linux Lab machines and then confirm that the version of g++ there is correct (as you did in Studio 0). Then cd into your directory for this course this semester, create a new subdirectory for this studio, and cd into it.

    Copy the CMakeLists.txt from your Studio 0 directory into that directory and update it so that it will build an executable program called studio3 from the following studio3.h header file and studio3.cpp) source file, both of which you should also copy into that directory.

    Compile the program, checking that the CMakeLists.txt caused cmake -B build && cmake --build build -- VERBOSE=1 to pass the -g flag (to embed debugging information for gdb to use) when it called g++. Run the compiled program with a command line consisting of multiple space-separated addition (+) operators and unsigned integers that make up a valid prefix addition expression (there should be one more unsigned integer than the number of addition operators, and the ith addition operator should always be to the left of the ith unsigned integer). Then, immediately issue the following command to confirm that the program returned 0 to indicate success:

    echo $?

    Run the program again, but with an invalid prefix addition expression (e.g., by removing one of the unsigned integers from the command line so that there are too few of them, or rearranging the command line so that one of the unsigned integers appears before its corresponding addition operator), and then immediately issue the echo $? command to confirm that the program returned a non-zero value to indicate failure.

    As the answer to this exercise please show the command lines that were used to run the program both successfully and unsuccessfully, and for each of them show the output the program produced and the value it returned.

  3. Use the gdb build/studio3 command to load the program within the gdb debugger, and as the answer to this exercise please show the lines that appeared within the terminal window where you ran that command, when you did that.

  4. Within gdb, issue the break parse_and_compute command to set a breakpoint that will stop the debugger whenever the program's parse_and_compute function is called, which it will be multiple times (recursively) as the program parses and computes the prefix addition expression that was given on its command line. As the answer to this exercise please show the additional lines that were printed within the terminal window when you ran that command.

  5. Within gdb, issue the command run + 1 + 2 + 3 4 to run the program, and then issue the continue command to cause the debugger to run it further until the next breakpoint is reached. As the answer to this exercise please show the additional lines that were printed within the terminal window when you ran those commands.

  6. Within gdb, issue the where command to display the program's call stack at the current point in its execution, and as the answer to this exercise please show the additional lines that were printed within the terminal window when you ran that command.

  7. Review the source file (and/or today's lecture slides) to determine what the appropriate expressions are for (1) the position (index) of the current command line argument that is being parsed, and (2) the (C-style) string corresponding to that command line argument. Within gdb, then use the print command twice to display the values of those expressions, and as the answer to this exercise please show the additional lines that were printed within the terminal window when you did that.

    Optional Enrichment Exercises

  8. Use the step and next commands within gdb to step into and step over different statements and functions in the program. As the answer to this exercise, please note any interesting things you noticed when you did that, including whether you were able to step all the way down into the assembly code for funtions that the program uses but that do not appear in its compiled source file (e.g., that the program gets from the C++ libraries).

  9. Repeat any of the required or optional exercises above after opening the program source and header files in emacs and running the gdb debugger from within emacs as shown in the lecture slides. As the answer to this exercise please describe briefly what you did, and any useful features that emacs provided when running the gdb debugger from within it.

  10. Repeat any of the required or optional exercises above after transfering the program source and header files to a Windows computer with Visual Studio installed, compiling them in Visual Studio into a console window application, and then stepping through the program using Visual Studio's debugging features. As the answer to this exercise please describe briefly what you did, and any useful features that Visual Studio provided when debugging your program. Note: VS Code is not Visual Studio

Things to Turn In:

For this studio, please turn in the following: