[Top] - [Next] - [Previous]

Advanced Debugging in Monitor Mode

  1. Tracing To an Instruction
  2. Tracing Inside an Instruction

This chapter discusses the T, I, and U commands, which supply a powerful alternative to single-stepping through a program N primitive instructions at a time. By using appropriate combinations of Ts, Is, and Us, any defined instruction can be quickly reached and tested.

This chapter also explains a debugging method that quickly allows us to locate our programming errors. This method is called stepwise debugging, and it is a direct analog to the process of stepwise refinement, which was discussed in KAREL the ROBOT as one simple program construction method. By using stepwise debugging, we can quickly survey a program's execution; a defined instruction will be examined in detail only after we know that it contains a bug. It takes some practice to use the T, I, and U commands effectively, but learning to do so results in a big gain in debugging efficiency.

Tracing To an Instruction

The T (trace to) command should be followed by one of Karel's instruction names (you can trace to either a primitive or defined instruction name). The T command takes control from the Monitor Mode's stepping mechanism and automatically executes your Karel program (in a specified or the default direction) until the given instruction name is about to be executed; if you are already executing inside the named instruction, the trace command executes your program until that instruction's end is reached. Thus, you can easily execute your program to any primitive instruction, or you can easily execute your program to the beginning or end of any defined instruction. When the simulator finds the required instruction, control again returns to the stepping mechanism in Monitor Mode.

Tracing Inside an Instruction

The I (instruction trace) command should be followed by one of Karel's defined instruction names (the reserved word BEGINNING-OF-EXECUTION is also especially useful in this command, and it is the initial default name). The I command takes control from the Monitor Mode's stepping mechanism and automatically steps to completion each instruction within the defined instruction that is being traced. One restriction on the I command is that it can be issued only after execution has entered the defined instruction that is to be instruction traced. Note that all entered defined instructions appear on the instruction list in the D command, and the currently executing instruction appears in Karel's status line. The U command Undoes (interrupts and cancels) the previously issued I command.

To stepwise debug a program, start by using the I command in the BEGINNING- OF-EXECUTION block. Each press of the return key will cause the simulator to completely execute the next instruction in the BEGINNING-OF-EXECUTION block. If a defined instruction incorrectly executes (because of an execution or intent error), change the execution direction to backward and completely unexecute the previously executed, incorrect instruction. Now use the U command to cancel the I command, and then step forward one instruction in Monitor Mode (to get inside the incorrect instruction). Finally, use the I command again, this time inside the incorrectly executing instruction. Repeat this stepwise debugging process until you find the exact location of your program bug.

The I command is a very powerful command that is easy to use, yet it is very difficult to explain on paper. To help divulge the workings of this command, let's explore a concrete example. Suppose that you are debugging the room escape program listed in Section 5.7 of KAREL the ROBOT. The BEGINNING-OF-EXECUTION block in that program contains the following five instructions:

        BEGINNING-OF-EXECUTION
           go-to-wall;
           turnleft;
           follow-until-door;
           exit-door;
           turnoff
        END-OF-EXECUTION

Now suppose that you are stepping in Monitor Mode at the beginning of this program. To start instruction tracing within the BEGINNING-OF-EXECUTION block, type the following command to the Monitor Mode prompt line:

     Ready To Step FORWARD(type ? for help)[F+1]:I_BEGINNING-OF-EXECUTION

The simulator will respond with the following prompt line, indicating that you are tracing the instructions inside of the BEGINNING-OF-EXECUTION block.

     Ready to execute "go-to-wall" in "BEGINNING-OF-EXECUTION"[F+]:

By pressing the return key, the defined instruction "go-to-wall" will be completely executed. This action is different from stepping in Monitor Mode, in which case the defined in*struction "go-to-wall" would be entered, but not fully executed. In Monitor Mode it would take multiple steps to completely execut "go-to-wall"; the I command automatically executes the correct number of steps needed to reach the end of this instruction. After executing "go-to-wall", the simulator will then respond with the following prompt line:

     Ready to execute "turnleft" in "BEGINNING-OF-EXECUTION"[F+]:

By pressing the return key again, the primitive instruction "turnleft" will be executed. The simulator will then respond with the following prompt line:

     Ready to execute "follow-until-door" in "BEGINNING-OF-EXECUTION"[F+]:

By pressing the return key one more time, the defined instruction "follow- until-door" will be comletely executed. The simulator will then respond with the following prompt line:

     Ready to execute "exit-door" in "BEGINNING-OF-EXECUTION"[F+]:

Now suppose that there is an intent error in "follow-until-door", and suppose that you have seen that this instruction did not execute correctly. You can change the execution direction to backward by typing a -. After changing the execution direction, the next simulator prompt line will be:

     Ready to unexecute "follow-until-door" in "BEGINNING-OF-EXECUTION"[F-]:

Now, by pressing the return key you can unexecute the entire "follow-until-door" defined instruction. Next, you can cancel the instruction trace by typing the U command, which puts the simulator back into Monitor Mode. Finally, you can single-step into the "follow-until-door" defined instruction and restart the instruction trace inside this instruction (by issuing the I follow-until-door command to the Monitor Mode prompt line). You can also switch the Execution Options to Explain Mode.

[Top] - [Next] - [Previous]