Return To CS 110 Home

Colour Theme   Font Size Options
 
   
   
   
Debugger Exercise

Using a Debugger


Video Summary: https://youtu.be/Ujo01Qi2zH8


As you write code, you will make mistakes that will result in incorrect output, referred to as bugs. While you could write output to the console every time a value potentially changes to see how it behaves over time, this would involve having to write and remove cout statements throughout your program, and is rather tedious.

Instead, compilers provide a tool called the debugger, which allows you to pick a point in the program you want to examine in detail and follow it's execution line by line, and see the values your variables are holding as they change.

Replit has a debugger you can find on the left of the files menu. Before you can use it, you need to make sure:

  1. Your Replit is configured to run the program you want to debug
  2. You do not have multiple files with a main function defined. You may need to temporarily rename main functions on files you aren't debugging
  3. Your program compiles. You can't debug a program that doesn't run in the first place

To debug a program:

  1. Click the debug window on the left
  2. Set "breakpoints" in your program where you want to examine the variables by clicking on the left side of the code, which should place a dot and show up on your debug window. Breakpoints tell the debugger where it should suspend execution, or "break", while you debug the program.
  3. Click the run button in the debugger interface (not the run button at the top, that will run the program normally)
  4. Execution will pause at the breakpoints you set. Examine the values of your variables on the window on the left
  5. Click the "next step" button the step through the code one line at a time. Click the "next breakpoint" button to skip multiple lines and go to the next breakpoint
  6. As you step through the code, examine what values get set, and try to understand what is causing your mistake

Consider the following example code:


Here are the main things to notice about this program:

  1. On line 15, a "infinite loop" is intentionally created. This means the code will continuously run until the stop command is issued. You will learn about loops later.
  2. The user will be prompted for a number, which is read on line 19 with cin
  3. On line 23 and 24, the number is transformed to something unpredictable by multiplying it with a "random" number. The modulus operator is then used to change the number to between 0 and 10 for simplicity

Since this program does not write any output to the console, you will need to use the debugger to see how the number is changed. To debug this program:

  1. Place breakpoints where you want to see the value of the number, such as lines 19 and 23. Keep in mind Replit may move your breakpoints if it thinks there is a more optimal position
  2. Click the debugger menu on the left and click the run button within this menu (not the run button at the top of the screen)
  3. Notice that you can see the current value of number under variables, and what line you are currently on under call stack
  4. Press the "next step" button to start walking through the code one line at a time
  5. Enter a number into the console window when you are prompted, and press enter. You will not be able to advance the debugger until you do
  6. See that your value was stored in number, and you advanced a line. Continue pressing next step
  7. After a few steps, you will see the value change to a "random" number. You will only be able to read it in the debugger, since it is not output anywhere
  8. Notice that the value in the debugger does not change on the line the random number is generated. This is because it is still reading the value on the left of the equation, which is the original number. It will not change until you've stepped past that line

You could repeat stepping through the code as many times as you like and see how the buttons and number behave. For example, you can use "next breakpoint" to skip some lines you don't care about.